3
* test_main.c - test suite for init/main.c
5
* Copyright © 2013 Canonical Ltd.
6
* Author: James Hunt <james.hunt@canonical.com>
8
* This program is free software; you can redistribute it and/or modify
9
* it under the terms of the GNU General Public License version 2, as
10
* published by the Free Software Foundation.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License along
18
* with this program; if not, write to the Free Software Foundation, Inc.,
19
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22
#include <nih/string.h>
28
#include <sys/types.h>
33
#include "job_class.h"
37
#include "test_util_common.h"
43
char confdir_a[PATH_MAX];
44
char confdir_b[PATH_MAX];
45
char xdg_config_home[PATH_MAX];
46
char xdg_runtime_dir[PATH_MAX];
47
char logdir[PATH_MAX];
48
pid_t upstart_pid = 0;
52
nih_local char *cmd = NULL;
53
nih_local char *orig_xdg_config_home = NULL;
54
nih_local char *orig_xdg_runtime_dir = NULL;
55
nih_local char *xdg_conf_dir = NULL;
56
nih_local char *session_file = NULL;
57
nih_local char *path = NULL;
59
/* space for 2 sets of confdir options and a terminator */
62
TEST_GROUP ("--confdir command-line option handling");
64
TEST_FILENAME (confdir_a);
65
assert0 (mkdir (confdir_a, 0755));
67
TEST_FILENAME (confdir_b);
68
assert0 (mkdir (confdir_b, 0755));
70
TEST_FILENAME (xdg_config_home);
71
assert0 (mkdir (xdg_config_home, 0755));
73
TEST_FILENAME (xdg_runtime_dir);
74
assert0 (mkdir (xdg_runtime_dir, 0755));
76
xdg_conf_dir = nih_sprintf (NULL, "%s/%s", xdg_config_home, "upstart");
77
TEST_NE_P (xdg_conf_dir, NULL);
78
assert0 (mkdir (xdg_conf_dir, 0755));
80
TEST_FILENAME (logdir);
81
assert0 (mkdir (logdir, 0755));
83
/* Take care to avoid disrupting users environment by saving and
84
* restoring these variable (assuming the tests all pass...).
86
orig_xdg_config_home = getenv ("XDG_CONFIG_HOME");
87
if (orig_xdg_config_home)
88
orig_xdg_config_home = NIH_MUST (nih_strdup (NULL, orig_xdg_config_home));
90
assert0 (setenv ("XDG_CONFIG_HOME", xdg_config_home, 1));
92
orig_xdg_runtime_dir = getenv ("XDG_RUNTIME_DIR");
93
if (orig_xdg_runtime_dir)
94
orig_xdg_runtime_dir = NIH_MUST (nih_strdup (NULL, orig_xdg_runtime_dir));
96
assert0 (setenv ("XDG_RUNTIME_DIR", xdg_runtime_dir, 1));
98
/* disable system default job dir */
99
assert0 (setenv ("UPSTART_NO_SYSTEM_USERCONFDIR", "1", 1));
101
TEST_DBUS (dbus_pid);
103
/************************************************************/
104
TEST_FEATURE ("Session Init without --confdir");
106
CREATE_FILE (xdg_conf_dir, "foo.conf", "exec true");
107
CREATE_FILE (xdg_conf_dir, "bar.conf", "exec true");
108
CREATE_FILE (xdg_conf_dir, "baz.conf", "exec true");
110
start_upstart_common (&upstart_pid, TRUE, NULL, logdir, NULL);
112
/* Should be running */
113
assert0 (kill (upstart_pid, 0));
115
session_file = get_session_file (xdg_runtime_dir, upstart_pid);
117
cmd = nih_sprintf (NULL, "%s list 2>&1", get_initctl ());
118
TEST_NE_P (cmd, NULL);
119
RUN_COMMAND (NULL, cmd, &output, &lines);
121
qsort (output, lines, sizeof (output[0]), strcmp_compar);
124
TEST_STR_MATCH (output[0], "bar stop/waiting");
125
TEST_STR_MATCH (output[1], "baz stop/waiting");
126
TEST_STR_MATCH (output[2], "foo stop/waiting");
129
DELETE_FILE (xdg_conf_dir, "foo.conf");
130
DELETE_FILE (xdg_conf_dir, "bar.conf");
131
DELETE_FILE (xdg_conf_dir, "baz.conf");
133
STOP_UPSTART (upstart_pid);
134
assert0 (unlink (session_file));
136
/************************************************************/
137
TEST_FEATURE ("Session Init with --confdir");
139
CREATE_FILE (xdg_conf_dir, "xdg_dir_job.conf", "exec true");
140
CREATE_FILE (confdir_a, "conf_dir_job.conf", "exec true");
142
start_upstart_common (&upstart_pid, TRUE, confdir_a, logdir, NULL);
144
/* Should be running */
145
assert0 (kill (upstart_pid, 0));
147
session_file = get_session_file (xdg_runtime_dir, upstart_pid);
149
cmd = nih_sprintf (NULL, "%s list 2>&1", get_initctl ());
150
TEST_NE_P (cmd, NULL);
151
RUN_COMMAND (NULL, cmd, &output, &lines);
153
qsort (output, lines, sizeof (output[0]), strcmp_compar);
155
/* We expect jobs in xdg_conf_dir to be ignored */
157
TEST_STR_MATCH (output[0], "conf_dir_job stop/waiting");
160
DELETE_FILE (xdg_conf_dir, "xdg_dir_job.conf");
161
DELETE_FILE (confdir_a, "conf_dir_job.conf");
163
STOP_UPSTART (upstart_pid);
164
assert0 (unlink (session_file));
166
/************************************************************/
167
TEST_FEATURE ("Session Init with multiple --confdir");
169
CREATE_FILE (xdg_conf_dir, "xdg_dir_job.conf", "exec true");
170
CREATE_FILE (confdir_a, "conf_dir_a_job.conf", "exec true");
171
CREATE_FILE (confdir_b, "conf_dir_b_job.conf", "exec true");
173
extra[0] = "--confdir";
174
extra[1] = confdir_a;
175
extra[2] = "--confdir";
176
extra[3] = confdir_b;
179
/* pass 2 confdir directories */
180
start_upstart_common (&upstart_pid, TRUE, NULL, logdir, extra);
182
/* Should be running */
183
assert0 (kill (upstart_pid, 0));
185
session_file = get_session_file (xdg_runtime_dir, upstart_pid);
187
cmd = nih_sprintf (NULL, "%s list 2>&1", get_initctl ());
188
TEST_NE_P (cmd, NULL);
189
RUN_COMMAND (NULL, cmd, &output, &lines);
191
qsort (output, lines, sizeof (output[0]), strcmp_compar);
193
/* We expect jobs in xdg_conf_dir to be ignored */
195
TEST_STR_MATCH (output[0], "conf_dir_a_job stop/waiting");
196
TEST_STR_MATCH (output[1], "conf_dir_b_job stop/waiting");
199
DELETE_FILE (xdg_conf_dir, "xdg_dir_job.conf");
200
DELETE_FILE (confdir_a, "conf_dir_a_job.conf");
201
DELETE_FILE (confdir_b, "conf_dir_b_job.conf");
203
STOP_UPSTART (upstart_pid);
204
assert0 (unlink (session_file));
206
/************************************************************/
207
TEST_FEATURE ("Session Init with multiple --confdir and conflicting names");
209
CREATE_FILE (xdg_conf_dir, "conflict.conf", "emits xdg_conf_dir");
210
CREATE_FILE (confdir_a, "conflict.conf", "emits confdir_a");
211
CREATE_FILE (confdir_b, "foo.conf", "exec true");
213
extra[0] = "--confdir";
214
extra[1] = confdir_a;
215
extra[2] = "--confdir";
216
extra[3] = confdir_b;
219
/* pass 2 confdir directories */
220
start_upstart_common (&upstart_pid, TRUE, NULL, logdir, extra);
222
/* Should be running */
223
assert0 (kill (upstart_pid, 0));
225
session_file = get_session_file (xdg_runtime_dir, upstart_pid);
227
cmd = nih_sprintf (NULL, "%s list 2>&1", get_initctl ());
228
TEST_NE_P (cmd, NULL);
229
RUN_COMMAND (NULL, cmd, &output, &lines);
231
qsort (output, lines, sizeof (output[0]), strcmp_compar);
233
/* We expect jobs in xdg_conf_dir to be ignored */
235
TEST_STR_MATCH (output[0], "conflict stop/waiting");
236
TEST_STR_MATCH (output[1], "foo stop/waiting");
239
cmd = nih_sprintf (NULL, "%s show-config %s 2>&1", get_initctl (), "conflict");
240
TEST_NE_P (cmd, NULL);
241
RUN_COMMAND (NULL, cmd, &output, &lines);
243
/* Ensure the correct version of the conflict job is found */
245
TEST_STR_MATCH (output[0], "conflict");
246
TEST_STR_MATCH (output[1], " emits confdir_a");
249
DELETE_FILE (xdg_conf_dir, "conflict.conf");
250
DELETE_FILE (confdir_a, "conflict.conf");
251
DELETE_FILE (confdir_b, "foo.conf");
253
STOP_UPSTART (upstart_pid);
254
assert0 (unlink (session_file));
256
/************************************************************/
257
TEST_FEATURE ("System Init without --confdir");
259
/* Use the "secret" interface */
260
assert0 (setenv ("UPSTART_CONFDIR", confdir_a, 1));
262
CREATE_FILE (confdir_a, "foo.conf", "exec true");
263
CREATE_FILE (confdir_a, "bar.conf", "exec true");
264
CREATE_FILE (confdir_a, "baz.conf", "exec true");
266
/* Disable user mode */
267
test_user_mode = FALSE;
269
start_upstart_common (&upstart_pid, FALSE, NULL, logdir, NULL);
271
/* Should be running */
272
assert0 (kill (upstart_pid, 0));
274
cmd = nih_sprintf (NULL, "%s list 2>&1", get_initctl ());
275
TEST_NE_P (cmd, NULL);
276
RUN_COMMAND (NULL, cmd, &output, &lines);
278
qsort (output, lines, sizeof (output[0]), strcmp_compar);
281
TEST_STR_MATCH (output[0], "bar stop/waiting");
282
TEST_STR_MATCH (output[1], "baz stop/waiting");
283
TEST_STR_MATCH (output[2], "foo stop/waiting");
286
DELETE_FILE (confdir_a, "foo.conf");
287
DELETE_FILE (confdir_a, "bar.conf");
288
DELETE_FILE (confdir_a, "baz.conf");
290
STOP_UPSTART (upstart_pid);
292
/************************************************************/
293
TEST_FEATURE ("System Init with --confdir");
295
CREATE_FILE (confdir_a, "foo.conf", "exec true");
296
CREATE_FILE (confdir_a, "bar.conf", "exec true");
297
CREATE_FILE (confdir_b, "baz.conf", "exec true");
299
start_upstart_common (&upstart_pid, FALSE, confdir_b, logdir, NULL);
301
/* Should be running */
302
assert0 (kill (upstart_pid, 0));
304
cmd = nih_sprintf (NULL, "%s list 2>&1", get_initctl ());
305
TEST_NE_P (cmd, NULL);
306
RUN_COMMAND (NULL, cmd, &output, &lines);
308
qsort (output, lines, sizeof (output[0]), strcmp_compar);
311
TEST_STR_MATCH (output[0], "baz stop/waiting");
314
DELETE_FILE (confdir_a, "foo.conf");
315
DELETE_FILE (confdir_a, "bar.conf");
316
DELETE_FILE (confdir_b, "baz.conf");
318
STOP_UPSTART (upstart_pid);
320
/************************************************************/
321
TEST_FEATURE ("System Init with multiple --confdir");
323
assert0 (setenv ("UPSTART_CONFDIR", xdg_conf_dir, 1));
325
CREATE_FILE (xdg_conf_dir, "foo.conf", "exec true");
326
CREATE_FILE (confdir_a, "bar.conf", "exec true");
327
CREATE_FILE (confdir_b, "baz.conf", "exec true");
328
CREATE_FILE (confdir_b, "qux.conf", "exec true");
330
extra[0] = "--confdir";
331
extra[1] = confdir_a;
332
extra[2] = "--confdir";
333
extra[3] = confdir_b;
336
start_upstart_common (&upstart_pid, FALSE, NULL, logdir, extra);
338
/* Should be running */
339
assert0 (kill (upstart_pid, 0));
341
cmd = nih_sprintf (NULL, "%s list 2>&1", get_initctl ());
342
TEST_NE_P (cmd, NULL);
343
RUN_COMMAND (NULL, cmd, &output, &lines);
345
qsort (output, lines, sizeof (output[0]), strcmp_compar);
348
/* XXX: Only the last instance of --confdir should be honoured.
350
* This behaviour deviates from running as a Session Init where *all*
351
* --confdir's specified are used.
353
TEST_STR_MATCH (output[0], "baz stop/waiting");
354
TEST_STR_MATCH (output[1], "qux stop/waiting");
357
DELETE_FILE (xdg_conf_dir, "foo.conf");
358
DELETE_FILE (confdir_a, "bar.conf");
359
DELETE_FILE (confdir_b, "baz.conf");
360
DELETE_FILE (confdir_b, "qux.conf");
362
STOP_UPSTART (upstart_pid);
364
/************************************************************/
365
TEST_FEATURE ("System Init with multiple --confdir and conflicting names");
367
assert0 (setenv ("UPSTART_CONFDIR", xdg_conf_dir, 1));
369
CREATE_FILE (xdg_conf_dir, "conflict.conf", "emits xdg_conf_dir");
370
CREATE_FILE (confdir_a, "conflict.conf", "emits confdir_a");
371
CREATE_FILE (confdir_b, "conflict.conf", "emits confdir_b");
373
extra[0] = "--confdir";
374
extra[1] = confdir_a;
375
extra[2] = "--confdir";
376
extra[3] = confdir_b;
379
start_upstart_common (&upstart_pid, FALSE, NULL, logdir, extra);
381
/* Should be running */
382
assert0 (kill (upstart_pid, 0));
384
cmd = nih_sprintf (NULL, "%s list 2>&1", get_initctl ());
385
TEST_NE_P (cmd, NULL);
386
RUN_COMMAND (NULL, cmd, &output, &lines);
388
qsort (output, lines, sizeof (output[0]), strcmp_compar);
391
/* only the last instance of --confdir should be honoured */
392
TEST_STR_MATCH (output[0], "conflict stop/waiting");
395
cmd = nih_sprintf (NULL, "%s show-config %s 2>&1", get_initctl (), "conflict");
396
TEST_NE_P (cmd, NULL);
397
RUN_COMMAND (NULL, cmd, &output, &lines);
399
/* Ensure the correct version of the conflict job is found */
401
TEST_STR_MATCH (output[0], "conflict");
402
TEST_STR_MATCH (output[1], " emits confdir_b");
405
DELETE_FILE (xdg_conf_dir, "conflict.conf");
406
DELETE_FILE (confdir_a, "conflict.conf");
407
DELETE_FILE (confdir_b, "conflict.conf");
409
STOP_UPSTART (upstart_pid);
411
/************************************************************/
413
TEST_DBUS_END (dbus_pid);
415
if (orig_xdg_config_home) {
417
setenv ("XDG_CONFIG_HOME", orig_xdg_config_home, 1);
419
assert0 (unsetenv ("XDG_CONFIG_HOME"));
422
if (orig_xdg_runtime_dir) {
424
setenv ("XDG_RUNTIME_DIR", orig_xdg_runtime_dir, 1);
426
assert0 (unsetenv ("XDG_RUNTIME_DIR"));
429
assert0 (rmdir (confdir_a));
430
assert0 (rmdir (confdir_b));
431
assert0 (rmdir (xdg_conf_dir));
432
assert0 (rmdir (xdg_config_home));
434
/* Remove the directory tree the first Session Init created */
435
path = NIH_MUST (nih_sprintf (NULL, "%s/upstart/sessions", xdg_runtime_dir));
436
TEST_EQ (rmdir (path), 0);
437
path = NIH_MUST (nih_sprintf (NULL, "%s/upstart", xdg_runtime_dir));
438
TEST_EQ (rmdir (path), 0);
439
assert0 (rmdir (xdg_runtime_dir));
441
assert0 (rmdir (logdir));
442
assert0 (unsetenv ("UPSTART_CONFDIR"));