[go: up one dir, main page]

File: test_main.c

package info (click to toggle)
upstart 1.11-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 12,988 kB
  • ctags: 2,100
  • sloc: ansic: 67,932; sh: 12,339; python: 1,718; makefile: 1,093; xml: 162; sed: 16
file content (420 lines) | stat: -rw-r--r-- 12,335 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/* upstart
 *
 * test_main.c - test suite for init/main.c
 *
 * Copyright © 2013 Canonical Ltd.
 * Author: James Hunt <james.hunt@canonical.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif /* HAVE_CONFIG_H */

#include <nih/string.h>
#include <nih/main.h>
#include <nih/test.h>

#include <limits.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include "conf.h"
#include "job_class.h"
#include "job.h"
#include "xdg.h"

#include "test_util_common.h"


void
test_confdir (void)
{
	char             confdir_a[PATH_MAX];
	char             confdir_b[PATH_MAX];
	char            *xdg_config_home;
	char            *xdg_runtime_dir;
	char             logdir[PATH_MAX];
	pid_t            upstart_pid = 0;
	pid_t            dbus_pid = 0;
	char           **output;
	size_t           lines;
	nih_local char  *cmd = NULL;
	nih_local char  *xdg_conf_dir = NULL;
	nih_local char  *session_file = NULL;
	nih_local char  *path = NULL;

	/* space for 2 sets of confdir options and a terminator */
	char            *extra[5];

	xdg_config_home = getenv ("XDG_CONFIG_HOME");
	TEST_NE_P (xdg_config_home, NULL);

	xdg_runtime_dir = getenv ("XDG_RUNTIME_DIR");
	TEST_NE_P (xdg_runtime_dir, NULL);

	TEST_GROUP ("--confdir command-line option handling");

	TEST_FILENAME (confdir_a);
	assert0 (mkdir (confdir_a, 0755));

	TEST_FILENAME (confdir_b);
	assert0 (mkdir (confdir_b, 0755));

	xdg_conf_dir = nih_sprintf (NULL, "%s/%s", xdg_config_home, "upstart");
	TEST_NE_P (xdg_conf_dir, NULL);
	assert0 (mkdir (xdg_conf_dir, 0755));

	TEST_FILENAME (logdir);
	assert0 (mkdir (logdir, 0755));

	/* disable system default job dir */
	assert0 (setenv ("UPSTART_NO_SYSTEM_USERCONFDIR", "1", 1));

	TEST_DBUS (dbus_pid);

	/************************************************************/
	TEST_FEATURE ("Session Init without --confdir");

	CREATE_FILE (xdg_conf_dir, "foo.conf", "exec true");
	CREATE_FILE (xdg_conf_dir, "bar.conf", "exec true");
	CREATE_FILE (xdg_conf_dir, "baz.conf", "exec true");

	start_upstart_common (&upstart_pid, TRUE, FALSE, NULL, logdir, NULL);

	/* Should be running */
	assert0 (kill (upstart_pid, 0));

	session_file = get_session_file (xdg_runtime_dir, upstart_pid);

	cmd = nih_sprintf (NULL, "%s list 2>&1", get_initctl ());
	TEST_NE_P (cmd, NULL);
	RUN_COMMAND (NULL, cmd, &output, &lines);

	qsort (output, lines, sizeof (output[0]), strcmp_compar);

	TEST_EQ (lines, 3);
	TEST_STR_MATCH (output[0], "bar stop/waiting");
	TEST_STR_MATCH (output[1], "baz stop/waiting");
	TEST_STR_MATCH (output[2], "foo stop/waiting");
	nih_free (output);

	DELETE_FILE (xdg_conf_dir, "foo.conf");
	DELETE_FILE (xdg_conf_dir, "bar.conf");
	DELETE_FILE (xdg_conf_dir, "baz.conf");

	STOP_UPSTART (upstart_pid);
	assert0 (unlink (session_file));

	/************************************************************/
	TEST_FEATURE ("Session Init with --confdir");

	CREATE_FILE (xdg_conf_dir, "xdg_dir_job.conf", "exec true");
	CREATE_FILE (confdir_a, "conf_dir_job.conf", "exec true");

	start_upstart_common (&upstart_pid, TRUE, FALSE, confdir_a, logdir, NULL);

	/* Should be running */
	assert0 (kill (upstart_pid, 0));

	session_file = get_session_file (xdg_runtime_dir, upstart_pid);

	cmd = nih_sprintf (NULL, "%s list 2>&1", get_initctl ());
	TEST_NE_P (cmd, NULL);
	RUN_COMMAND (NULL, cmd, &output, &lines);

	qsort (output, lines, sizeof (output[0]), strcmp_compar);

	/* We expect jobs in xdg_conf_dir to be ignored */
	TEST_EQ (lines, 1);
	TEST_STR_MATCH (output[0], "conf_dir_job stop/waiting");
	nih_free (output);

	DELETE_FILE (xdg_conf_dir, "xdg_dir_job.conf");
	DELETE_FILE (confdir_a, "conf_dir_job.conf");

	STOP_UPSTART (upstart_pid);
	assert0 (unlink (session_file));

	/************************************************************/
	TEST_FEATURE ("Session Init with multiple --confdir");

	CREATE_FILE (xdg_conf_dir, "xdg_dir_job.conf", "exec true");
	CREATE_FILE (confdir_a, "conf_dir_a_job.conf", "exec true");
	CREATE_FILE (confdir_b, "conf_dir_b_job.conf", "exec true");

	extra[0] = "--confdir";
	extra[1] = confdir_a;
	extra[2] = "--confdir";
	extra[3] = confdir_b;
	extra[4] = NULL;

	/* pass 2 confdir directories */
	start_upstart_common (&upstart_pid, TRUE, FALSE, NULL, logdir, extra);

	/* Should be running */
	assert0 (kill (upstart_pid, 0));

	session_file = get_session_file (xdg_runtime_dir, upstart_pid);

	cmd = nih_sprintf (NULL, "%s list 2>&1", get_initctl ());
	TEST_NE_P (cmd, NULL);
	RUN_COMMAND (NULL, cmd, &output, &lines);

	qsort (output, lines, sizeof (output[0]), strcmp_compar);

	/* We expect jobs in xdg_conf_dir to be ignored */
	TEST_EQ (lines, 2);
	TEST_STR_MATCH (output[0], "conf_dir_a_job stop/waiting");
	TEST_STR_MATCH (output[1], "conf_dir_b_job stop/waiting");
	nih_free (output);

	DELETE_FILE (xdg_conf_dir, "xdg_dir_job.conf");
	DELETE_FILE (confdir_a, "conf_dir_a_job.conf");
	DELETE_FILE (confdir_b, "conf_dir_b_job.conf");

	STOP_UPSTART (upstart_pid);
	assert0 (unlink (session_file));

	/************************************************************/
	TEST_FEATURE ("Session Init with multiple --confdir and conflicting names");

	CREATE_FILE (xdg_conf_dir, "conflict.conf", "emits xdg_conf_dir");
	CREATE_FILE (confdir_a, "conflict.conf", "emits confdir_a");
	CREATE_FILE (confdir_b, "foo.conf", "exec true");

	extra[0] = "--confdir";
	extra[1] = confdir_a;
	extra[2] = "--confdir";
	extra[3] = confdir_b;
	extra[4] = NULL;

	/* pass 2 confdir directories */
	start_upstart_common (&upstart_pid, TRUE, FALSE, NULL, logdir, extra);

	/* Should be running */
	assert0 (kill (upstart_pid, 0));

	session_file = get_session_file (xdg_runtime_dir, upstart_pid);

	cmd = nih_sprintf (NULL, "%s list 2>&1", get_initctl ());
	TEST_NE_P (cmd, NULL);
	RUN_COMMAND (NULL, cmd, &output, &lines);

	qsort (output, lines, sizeof (output[0]), strcmp_compar);

	/* We expect jobs in xdg_conf_dir to be ignored */
	TEST_EQ (lines, 2);
	TEST_STR_MATCH (output[0], "conflict stop/waiting");
	TEST_STR_MATCH (output[1], "foo stop/waiting");
	nih_free (output);

	cmd = nih_sprintf (NULL, "%s show-config %s 2>&1", get_initctl (), "conflict");
	TEST_NE_P (cmd, NULL);
	RUN_COMMAND (NULL, cmd, &output, &lines);

	/* Ensure the correct version of the conflict job is found */
	TEST_EQ (lines, 2);
	TEST_STR_MATCH (output[0], "conflict");
	TEST_STR_MATCH (output[1], "  emits confdir_a");
	nih_free (output);

	DELETE_FILE (xdg_conf_dir, "conflict.conf");
	DELETE_FILE (confdir_a, "conflict.conf");
	DELETE_FILE (confdir_b, "foo.conf");

	STOP_UPSTART (upstart_pid);
	assert0 (unlink (session_file));

	/************************************************************/
	TEST_FEATURE ("System Init without --confdir");

	/* Use the "secret" interface */
	assert0 (setenv ("UPSTART_CONFDIR", confdir_a, 1));

	CREATE_FILE (confdir_a, "foo.conf", "exec true");
	CREATE_FILE (confdir_a, "bar.conf", "exec true");
	CREATE_FILE (confdir_a, "baz.conf", "exec true");

	/* Disable user mode */
	test_user_mode = FALSE;

	start_upstart_common (&upstart_pid, FALSE, FALSE, NULL, logdir, NULL);

	/* Should be running */
	assert0 (kill (upstart_pid, 0));

	cmd = nih_sprintf (NULL, "%s list 2>&1", get_initctl ());
	TEST_NE_P (cmd, NULL);
	RUN_COMMAND (NULL, cmd, &output, &lines);

	qsort (output, lines, sizeof (output[0]), strcmp_compar);

	TEST_EQ (lines, 3);
	TEST_STR_MATCH (output[0], "bar stop/waiting");
	TEST_STR_MATCH (output[1], "baz stop/waiting");
	TEST_STR_MATCH (output[2], "foo stop/waiting");
	nih_free (output);

	DELETE_FILE (confdir_a, "foo.conf");
	DELETE_FILE (confdir_a, "bar.conf");
	DELETE_FILE (confdir_a, "baz.conf");

	STOP_UPSTART (upstart_pid);

	/************************************************************/
	TEST_FEATURE ("System Init with --confdir");

	CREATE_FILE (confdir_a, "foo.conf", "exec true");
	CREATE_FILE (confdir_a, "bar.conf", "exec true");
	CREATE_FILE (confdir_b, "baz.conf", "exec true");

	start_upstart_common (&upstart_pid, FALSE, FALSE, confdir_b, logdir, NULL);

	/* Should be running */
	assert0 (kill (upstart_pid, 0));

	cmd = nih_sprintf (NULL, "%s list 2>&1", get_initctl ());
	TEST_NE_P (cmd, NULL);
	RUN_COMMAND (NULL, cmd, &output, &lines);

	qsort (output, lines, sizeof (output[0]), strcmp_compar);

	TEST_EQ (lines, 1);
	TEST_STR_MATCH (output[0], "baz stop/waiting");
	nih_free (output);

	DELETE_FILE (confdir_a, "foo.conf");
	DELETE_FILE (confdir_a, "bar.conf");
	DELETE_FILE (confdir_b, "baz.conf");

	STOP_UPSTART (upstart_pid);

	/************************************************************/
	TEST_FEATURE ("System Init with multiple --confdir");

	assert0 (setenv ("UPSTART_CONFDIR", xdg_conf_dir, 1));

	CREATE_FILE (xdg_conf_dir, "foo.conf", "exec true");
	CREATE_FILE (confdir_a, "bar.conf", "exec true");
	CREATE_FILE (confdir_b, "baz.conf", "exec true");
	CREATE_FILE (confdir_b, "qux.conf", "exec true");

	extra[0] = "--confdir";
	extra[1] = confdir_a;
	extra[2] = "--confdir";
	extra[3] = confdir_b;
	extra[4] = NULL;

	start_upstart_common (&upstart_pid, FALSE, FALSE, NULL, logdir, extra);

	/* Should be running */
	assert0 (kill (upstart_pid, 0));

	cmd = nih_sprintf (NULL, "%s list 2>&1", get_initctl ());
	TEST_NE_P (cmd, NULL);
	RUN_COMMAND (NULL, cmd, &output, &lines);

	qsort (output, lines, sizeof (output[0]), strcmp_compar);

	TEST_EQ (lines, 2);
	/* XXX: Only the last instance of --confdir should be honoured.
	 *
	 * This behaviour deviates from running as a Session Init where *all*
	 * --confdir's specified are used.
	 */
	TEST_STR_MATCH (output[0], "baz stop/waiting");
	TEST_STR_MATCH (output[1], "qux stop/waiting");
	nih_free (output);

	DELETE_FILE (xdg_conf_dir, "foo.conf");
	DELETE_FILE (confdir_a, "bar.conf");
	DELETE_FILE (confdir_b, "baz.conf");
	DELETE_FILE (confdir_b, "qux.conf");

	STOP_UPSTART (upstart_pid);

	/************************************************************/
	TEST_FEATURE ("System Init with multiple --confdir and conflicting names");

	assert0 (setenv ("UPSTART_CONFDIR", xdg_conf_dir, 1));

	CREATE_FILE (xdg_conf_dir, "conflict.conf", "emits xdg_conf_dir");
	CREATE_FILE (confdir_a, "conflict.conf", "emits confdir_a");
	CREATE_FILE (confdir_b, "conflict.conf", "emits confdir_b");

	extra[0] = "--confdir";
	extra[1] = confdir_a;
	extra[2] = "--confdir";
	extra[3] = confdir_b;
	extra[4] = NULL;

	start_upstart_common (&upstart_pid, FALSE, FALSE, NULL, logdir, extra);

	/* Should be running */
	assert0 (kill (upstart_pid, 0));

	cmd = nih_sprintf (NULL, "%s list 2>&1", get_initctl ());
	TEST_NE_P (cmd, NULL);
	RUN_COMMAND (NULL, cmd, &output, &lines);

	qsort (output, lines, sizeof (output[0]), strcmp_compar);

	TEST_EQ (lines, 1);
	/* only the last instance of --confdir should be honoured */
	TEST_STR_MATCH (output[0], "conflict stop/waiting");
	nih_free (output);

	cmd = nih_sprintf (NULL, "%s show-config %s 2>&1", get_initctl (), "conflict");
	TEST_NE_P (cmd, NULL);
	RUN_COMMAND (NULL, cmd, &output, &lines);

	/* Ensure the correct version of the conflict job is found */
	TEST_EQ (lines, 2);
	TEST_STR_MATCH (output[0], "conflict");
	TEST_STR_MATCH (output[1], "  emits confdir_b");
	nih_free (output);

	DELETE_FILE (xdg_conf_dir, "conflict.conf");
	DELETE_FILE (confdir_a, "conflict.conf");
	DELETE_FILE (confdir_b, "conflict.conf");

	STOP_UPSTART (upstart_pid);

	/************************************************************/

	TEST_DBUS_END (dbus_pid);

	assert0 (rmdir (confdir_a));
	assert0 (rmdir (confdir_b));
	assert0 (rmdir (xdg_conf_dir));
	assert0 (rmdir (logdir));
	assert0 (unsetenv ("UPSTART_CONFDIR"));
}

int
main (int   argc,
      char *argv[])
{
	test_common_setup ();

	test_confdir ();

	test_common_cleanup ();

	return 0;
}