~vorlon/ubuntu/raring/upstart/lp.1199778

« back to all changes in this revision

Viewing changes to init/tests/test_main.c

  • Committer: Steve Langasek
  • Date: 2013-11-07 03:06:51 UTC
  • Revision ID: steve.langasek@canonical.com-20131107030651-f1jzeyi7ifvvw1h8
Attempt to cherry-pick fixes for bug #1199778 to raring; something is still
missing though, the test suite segfaults on one of the json loads.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* upstart
 
2
 *
 
3
 * test_main.c - test suite for init/main.c
 
4
 *
 
5
 * Copyright © 2013 Canonical Ltd.
 
6
 * Author: James Hunt <james.hunt@canonical.com>
 
7
 *
 
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.
 
11
 *
 
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.
 
16
 *
 
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.
 
20
 */
 
21
 
 
22
#include <nih/string.h>
 
23
#include <nih/main.h>
 
24
#include <nih/test.h>
 
25
 
 
26
#include <limits.h>
 
27
#include <stdlib.h>
 
28
#include <sys/types.h>
 
29
#include <sys/stat.h>
 
30
#include <unistd.h>
 
31
 
 
32
#include "conf.h"
 
33
#include "job_class.h"
 
34
#include "job.h"
 
35
#include "xdg.h"
 
36
 
 
37
#include "test_util_common.h"
 
38
 
 
39
 
 
40
void
 
41
test_confdir (void)
 
42
{
 
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;
 
49
        pid_t            dbus_pid = 0;
 
50
        char           **output;
 
51
        size_t           lines;
 
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;
 
58
 
 
59
        /* space for 2 sets of confdir options and a terminator */
 
60
        char            *extra[5];
 
61
 
 
62
        TEST_GROUP ("--confdir command-line option handling");
 
63
 
 
64
        TEST_FILENAME (confdir_a);
 
65
        assert0 (mkdir (confdir_a, 0755));
 
66
 
 
67
        TEST_FILENAME (confdir_b);
 
68
        assert0 (mkdir (confdir_b, 0755));
 
69
 
 
70
        TEST_FILENAME (xdg_config_home);
 
71
        assert0 (mkdir (xdg_config_home, 0755));
 
72
 
 
73
        TEST_FILENAME (xdg_runtime_dir);
 
74
        assert0 (mkdir (xdg_runtime_dir, 0755));
 
75
 
 
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));
 
79
 
 
80
        TEST_FILENAME (logdir);
 
81
        assert0 (mkdir (logdir, 0755));
 
82
 
 
83
        /* Take care to avoid disrupting users environment by saving and
 
84
         * restoring these variable (assuming the tests all pass...).
 
85
         */
 
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));
 
89
 
 
90
        assert0 (setenv ("XDG_CONFIG_HOME", xdg_config_home, 1));
 
91
 
 
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));
 
95
 
 
96
        assert0 (setenv ("XDG_RUNTIME_DIR", xdg_runtime_dir, 1));
 
97
 
 
98
        /* disable system default job dir */
 
99
        assert0 (setenv ("UPSTART_NO_SYSTEM_USERCONFDIR", "1", 1));
 
100
 
 
101
        TEST_DBUS (dbus_pid);
 
102
 
 
103
        /************************************************************/
 
104
        TEST_FEATURE ("Session Init without --confdir");
 
105
 
 
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");
 
109
 
 
110
        start_upstart_common (&upstart_pid, TRUE, NULL, logdir, NULL);
 
111
 
 
112
        /* Should be running */
 
113
        assert0 (kill (upstart_pid, 0));
 
114
 
 
115
        session_file = get_session_file (xdg_runtime_dir, upstart_pid);
 
116
 
 
117
        cmd = nih_sprintf (NULL, "%s list 2>&1", get_initctl ());
 
118
        TEST_NE_P (cmd, NULL);
 
119
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
120
 
 
121
        qsort (output, lines, sizeof (output[0]), strcmp_compar);
 
122
 
 
123
        TEST_EQ (lines, 3);
 
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");
 
127
        nih_free (output);
 
128
 
 
129
        DELETE_FILE (xdg_conf_dir, "foo.conf");
 
130
        DELETE_FILE (xdg_conf_dir, "bar.conf");
 
131
        DELETE_FILE (xdg_conf_dir, "baz.conf");
 
132
 
 
133
        STOP_UPSTART (upstart_pid);
 
134
        assert0 (unlink (session_file));
 
135
 
 
136
        /************************************************************/
 
137
        TEST_FEATURE ("Session Init with --confdir");
 
138
 
 
139
        CREATE_FILE (xdg_conf_dir, "xdg_dir_job.conf", "exec true");
 
140
        CREATE_FILE (confdir_a, "conf_dir_job.conf", "exec true");
 
141
 
 
142
        start_upstart_common (&upstart_pid, TRUE, confdir_a, logdir, NULL);
 
143
 
 
144
        /* Should be running */
 
145
        assert0 (kill (upstart_pid, 0));
 
146
 
 
147
        session_file = get_session_file (xdg_runtime_dir, upstart_pid);
 
148
 
 
149
        cmd = nih_sprintf (NULL, "%s list 2>&1", get_initctl ());
 
150
        TEST_NE_P (cmd, NULL);
 
151
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
152
 
 
153
        qsort (output, lines, sizeof (output[0]), strcmp_compar);
 
154
 
 
155
        /* We expect jobs in xdg_conf_dir to be ignored */
 
156
        TEST_EQ (lines, 1);
 
157
        TEST_STR_MATCH (output[0], "conf_dir_job stop/waiting");
 
158
        nih_free (output);
 
159
 
 
160
        DELETE_FILE (xdg_conf_dir, "xdg_dir_job.conf");
 
161
        DELETE_FILE (confdir_a, "conf_dir_job.conf");
 
162
 
 
163
        STOP_UPSTART (upstart_pid);
 
164
        assert0 (unlink (session_file));
 
165
 
 
166
        /************************************************************/
 
167
        TEST_FEATURE ("Session Init with multiple --confdir");
 
168
 
 
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");
 
172
 
 
173
        extra[0] = "--confdir";
 
174
        extra[1] = confdir_a;
 
175
        extra[2] = "--confdir";
 
176
        extra[3] = confdir_b;
 
177
        extra[4] = NULL;
 
178
 
 
179
        /* pass 2 confdir directories */
 
180
        start_upstart_common (&upstart_pid, TRUE, NULL, logdir, extra);
 
181
 
 
182
        /* Should be running */
 
183
        assert0 (kill (upstart_pid, 0));
 
184
 
 
185
        session_file = get_session_file (xdg_runtime_dir, upstart_pid);
 
186
 
 
187
        cmd = nih_sprintf (NULL, "%s list 2>&1", get_initctl ());
 
188
        TEST_NE_P (cmd, NULL);
 
189
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
190
 
 
191
        qsort (output, lines, sizeof (output[0]), strcmp_compar);
 
192
 
 
193
        /* We expect jobs in xdg_conf_dir to be ignored */
 
194
        TEST_EQ (lines, 2);
 
195
        TEST_STR_MATCH (output[0], "conf_dir_a_job stop/waiting");
 
196
        TEST_STR_MATCH (output[1], "conf_dir_b_job stop/waiting");
 
197
        nih_free (output);
 
198
 
 
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");
 
202
 
 
203
        STOP_UPSTART (upstart_pid);
 
204
        assert0 (unlink (session_file));
 
205
 
 
206
        /************************************************************/
 
207
        TEST_FEATURE ("Session Init with multiple --confdir and conflicting names");
 
208
 
 
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");
 
212
 
 
213
        extra[0] = "--confdir";
 
214
        extra[1] = confdir_a;
 
215
        extra[2] = "--confdir";
 
216
        extra[3] = confdir_b;
 
217
        extra[4] = NULL;
 
218
 
 
219
        /* pass 2 confdir directories */
 
220
        start_upstart_common (&upstart_pid, TRUE, NULL, logdir, extra);
 
221
 
 
222
        /* Should be running */
 
223
        assert0 (kill (upstart_pid, 0));
 
224
 
 
225
        session_file = get_session_file (xdg_runtime_dir, upstart_pid);
 
226
 
 
227
        cmd = nih_sprintf (NULL, "%s list 2>&1", get_initctl ());
 
228
        TEST_NE_P (cmd, NULL);
 
229
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
230
 
 
231
        qsort (output, lines, sizeof (output[0]), strcmp_compar);
 
232
 
 
233
        /* We expect jobs in xdg_conf_dir to be ignored */
 
234
        TEST_EQ (lines, 2);
 
235
        TEST_STR_MATCH (output[0], "conflict stop/waiting");
 
236
        TEST_STR_MATCH (output[1], "foo stop/waiting");
 
237
        nih_free (output);
 
238
 
 
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);
 
242
 
 
243
        /* Ensure the correct version of the conflict job is found */
 
244
        TEST_EQ (lines, 2);
 
245
        TEST_STR_MATCH (output[0], "conflict");
 
246
        TEST_STR_MATCH (output[1], "  emits confdir_a");
 
247
        nih_free (output);
 
248
 
 
249
        DELETE_FILE (xdg_conf_dir, "conflict.conf");
 
250
        DELETE_FILE (confdir_a, "conflict.conf");
 
251
        DELETE_FILE (confdir_b, "foo.conf");
 
252
 
 
253
        STOP_UPSTART (upstart_pid);
 
254
        assert0 (unlink (session_file));
 
255
 
 
256
        /************************************************************/
 
257
        TEST_FEATURE ("System Init without --confdir");
 
258
 
 
259
        /* Use the "secret" interface */
 
260
        assert0 (setenv ("UPSTART_CONFDIR", confdir_a, 1));
 
261
 
 
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");
 
265
 
 
266
        /* Disable user mode */
 
267
        test_user_mode = FALSE;
 
268
 
 
269
        start_upstart_common (&upstart_pid, FALSE, NULL, logdir, NULL);
 
270
 
 
271
        /* Should be running */
 
272
        assert0 (kill (upstart_pid, 0));
 
273
 
 
274
        cmd = nih_sprintf (NULL, "%s list 2>&1", get_initctl ());
 
275
        TEST_NE_P (cmd, NULL);
 
276
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
277
 
 
278
        qsort (output, lines, sizeof (output[0]), strcmp_compar);
 
279
 
 
280
        TEST_EQ (lines, 3);
 
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");
 
284
        nih_free (output);
 
285
 
 
286
        DELETE_FILE (confdir_a, "foo.conf");
 
287
        DELETE_FILE (confdir_a, "bar.conf");
 
288
        DELETE_FILE (confdir_a, "baz.conf");
 
289
 
 
290
        STOP_UPSTART (upstart_pid);
 
291
 
 
292
        /************************************************************/
 
293
        TEST_FEATURE ("System Init with --confdir");
 
294
 
 
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");
 
298
 
 
299
        start_upstart_common (&upstart_pid, FALSE, confdir_b, logdir, NULL);
 
300
 
 
301
        /* Should be running */
 
302
        assert0 (kill (upstart_pid, 0));
 
303
 
 
304
        cmd = nih_sprintf (NULL, "%s list 2>&1", get_initctl ());
 
305
        TEST_NE_P (cmd, NULL);
 
306
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
307
 
 
308
        qsort (output, lines, sizeof (output[0]), strcmp_compar);
 
309
 
 
310
        TEST_EQ (lines, 1);
 
311
        TEST_STR_MATCH (output[0], "baz stop/waiting");
 
312
        nih_free (output);
 
313
 
 
314
        DELETE_FILE (confdir_a, "foo.conf");
 
315
        DELETE_FILE (confdir_a, "bar.conf");
 
316
        DELETE_FILE (confdir_b, "baz.conf");
 
317
 
 
318
        STOP_UPSTART (upstart_pid);
 
319
 
 
320
        /************************************************************/
 
321
        TEST_FEATURE ("System Init with multiple --confdir");
 
322
 
 
323
        assert0 (setenv ("UPSTART_CONFDIR", xdg_conf_dir, 1));
 
324
 
 
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");
 
329
 
 
330
        extra[0] = "--confdir";
 
331
        extra[1] = confdir_a;
 
332
        extra[2] = "--confdir";
 
333
        extra[3] = confdir_b;
 
334
        extra[4] = NULL;
 
335
 
 
336
        start_upstart_common (&upstart_pid, FALSE, NULL, logdir, extra);
 
337
 
 
338
        /* Should be running */
 
339
        assert0 (kill (upstart_pid, 0));
 
340
 
 
341
        cmd = nih_sprintf (NULL, "%s list 2>&1", get_initctl ());
 
342
        TEST_NE_P (cmd, NULL);
 
343
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
344
 
 
345
        qsort (output, lines, sizeof (output[0]), strcmp_compar);
 
346
 
 
347
        TEST_EQ (lines, 2);
 
348
        /* XXX: Only the last instance of --confdir should be honoured.
 
349
         *
 
350
         * This behaviour deviates from running as a Session Init where *all*
 
351
         * --confdir's specified are used.
 
352
         */
 
353
        TEST_STR_MATCH (output[0], "baz stop/waiting");
 
354
        TEST_STR_MATCH (output[1], "qux stop/waiting");
 
355
        nih_free (output);
 
356
 
 
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");
 
361
 
 
362
        STOP_UPSTART (upstart_pid);
 
363
 
 
364
        /************************************************************/
 
365
        TEST_FEATURE ("System Init with multiple --confdir and conflicting names");
 
366
 
 
367
        assert0 (setenv ("UPSTART_CONFDIR", xdg_conf_dir, 1));
 
368
 
 
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");
 
372
 
 
373
        extra[0] = "--confdir";
 
374
        extra[1] = confdir_a;
 
375
        extra[2] = "--confdir";
 
376
        extra[3] = confdir_b;
 
377
        extra[4] = NULL;
 
378
 
 
379
        start_upstart_common (&upstart_pid, FALSE, NULL, logdir, extra);
 
380
 
 
381
        /* Should be running */
 
382
        assert0 (kill (upstart_pid, 0));
 
383
 
 
384
        cmd = nih_sprintf (NULL, "%s list 2>&1", get_initctl ());
 
385
        TEST_NE_P (cmd, NULL);
 
386
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
387
 
 
388
        qsort (output, lines, sizeof (output[0]), strcmp_compar);
 
389
 
 
390
        TEST_EQ (lines, 1);
 
391
        /* only the last instance of --confdir should be honoured */
 
392
        TEST_STR_MATCH (output[0], "conflict stop/waiting");
 
393
        nih_free (output);
 
394
 
 
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);
 
398
 
 
399
        /* Ensure the correct version of the conflict job is found */
 
400
        TEST_EQ (lines, 2);
 
401
        TEST_STR_MATCH (output[0], "conflict");
 
402
        TEST_STR_MATCH (output[1], "  emits confdir_b");
 
403
        nih_free (output);
 
404
 
 
405
        DELETE_FILE (xdg_conf_dir, "conflict.conf");
 
406
        DELETE_FILE (confdir_a, "conflict.conf");
 
407
        DELETE_FILE (confdir_b, "conflict.conf");
 
408
 
 
409
        STOP_UPSTART (upstart_pid);
 
410
 
 
411
        /************************************************************/
 
412
 
 
413
        TEST_DBUS_END (dbus_pid);
 
414
 
 
415
        if (orig_xdg_config_home) {
 
416
                /* restore */
 
417
                setenv ("XDG_CONFIG_HOME", orig_xdg_config_home, 1);
 
418
        } else {
 
419
                assert0 (unsetenv ("XDG_CONFIG_HOME"));
 
420
        }
 
421
 
 
422
        if (orig_xdg_runtime_dir) {
 
423
                /* restore */
 
424
                setenv ("XDG_RUNTIME_DIR", orig_xdg_runtime_dir, 1);
 
425
        } else {
 
426
                assert0 (unsetenv ("XDG_RUNTIME_DIR"));
 
427
        }
 
428
 
 
429
        assert0 (rmdir (confdir_a));
 
430
        assert0 (rmdir (confdir_b));
 
431
        assert0 (rmdir (xdg_conf_dir));
 
432
        assert0 (rmdir (xdg_config_home));
 
433
 
 
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));
 
440
 
 
441
        assert0 (rmdir (logdir));
 
442
        assert0 (unsetenv ("UPSTART_CONFDIR"));
 
443
}
 
444
 
 
445
int
 
446
main (int   argc,
 
447
      char *argv[])
 
448
{
 
449
        test_confdir ();
 
450
 
 
451
        return 0;
 
452
}