~jamesodhunt/upstart/bug-530779-tmp

« back to all changes in this revision

Viewing changes to init/tests/test_main.c

  • Committer: James Hunt
  • Date: 2013-11-27 13:49:49 UTC
  • mfrom: (1538.2.40 upstart)
  • Revision ID: james.hunt@ubuntu.com-20131127134949-l16igbtl5q47ukj9
* Sync with lp:upstart.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
20
 */
21
21
 
 
22
#ifdef HAVE_CONFIG_H
 
23
# include <config.h>
 
24
#endif /* HAVE_CONFIG_H */
 
25
 
22
26
#include <nih/string.h>
23
27
#include <nih/main.h>
24
28
#include <nih/test.h>
42
46
{
43
47
        char             confdir_a[PATH_MAX];
44
48
        char             confdir_b[PATH_MAX];
45
 
        char             xdg_config_home[PATH_MAX];
46
 
        char             xdg_runtime_dir[PATH_MAX];
 
49
        char            *xdg_config_home;
 
50
        char            *xdg_runtime_dir;
47
51
        char             logdir[PATH_MAX];
48
52
        pid_t            upstart_pid = 0;
49
53
        pid_t            dbus_pid = 0;
50
54
        char           **output;
51
55
        size_t           lines;
52
56
        nih_local char  *cmd = NULL;
53
 
        nih_local char  *orig_xdg_config_home = NULL;
54
 
        nih_local char  *orig_xdg_runtime_dir = NULL;
55
57
        nih_local char  *xdg_conf_dir = NULL;
56
58
        nih_local char  *session_file = NULL;
57
59
        nih_local char  *path = NULL;
59
61
        /* space for 2 sets of confdir options and a terminator */
60
62
        char            *extra[5];
61
63
 
 
64
        xdg_config_home = getenv ("XDG_CONFIG_HOME");
 
65
        TEST_NE_P (xdg_config_home, NULL);
 
66
 
 
67
        xdg_runtime_dir = getenv ("XDG_RUNTIME_DIR");
 
68
        TEST_NE_P (xdg_runtime_dir, NULL);
 
69
 
62
70
        TEST_GROUP ("--confdir command-line option handling");
63
71
 
64
72
        TEST_FILENAME (confdir_a);
67
75
        TEST_FILENAME (confdir_b);
68
76
        assert0 (mkdir (confdir_b, 0755));
69
77
 
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
78
        xdg_conf_dir = nih_sprintf (NULL, "%s/%s", xdg_config_home, "upstart");
77
79
        TEST_NE_P (xdg_conf_dir, NULL);
78
80
        assert0 (mkdir (xdg_conf_dir, 0755));
80
82
        TEST_FILENAME (logdir);
81
83
        assert0 (mkdir (logdir, 0755));
82
84
 
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
85
        /* disable system default job dir */
99
86
        assert0 (setenv ("UPSTART_NO_SYSTEM_USERCONFDIR", "1", 1));
100
87
 
107
94
        CREATE_FILE (xdg_conf_dir, "bar.conf", "exec true");
108
95
        CREATE_FILE (xdg_conf_dir, "baz.conf", "exec true");
109
96
 
110
 
        start_upstart_common (&upstart_pid, TRUE, NULL, logdir, NULL);
 
97
        start_upstart_common (&upstart_pid, TRUE, FALSE, NULL, logdir, NULL);
111
98
 
112
99
        /* Should be running */
113
100
        assert0 (kill (upstart_pid, 0));
139
126
        CREATE_FILE (xdg_conf_dir, "xdg_dir_job.conf", "exec true");
140
127
        CREATE_FILE (confdir_a, "conf_dir_job.conf", "exec true");
141
128
 
142
 
        start_upstart_common (&upstart_pid, TRUE, confdir_a, logdir, NULL);
 
129
        start_upstart_common (&upstart_pid, TRUE, FALSE, confdir_a, logdir, NULL);
143
130
 
144
131
        /* Should be running */
145
132
        assert0 (kill (upstart_pid, 0));
177
164
        extra[4] = NULL;
178
165
 
179
166
        /* pass 2 confdir directories */
180
 
        start_upstart_common (&upstart_pid, TRUE, NULL, logdir, extra);
 
167
        start_upstart_common (&upstart_pid, TRUE, FALSE, NULL, logdir, extra);
181
168
 
182
169
        /* Should be running */
183
170
        assert0 (kill (upstart_pid, 0));
217
204
        extra[4] = NULL;
218
205
 
219
206
        /* pass 2 confdir directories */
220
 
        start_upstart_common (&upstart_pid, TRUE, NULL, logdir, extra);
 
207
        start_upstart_common (&upstart_pid, TRUE, FALSE, NULL, logdir, extra);
221
208
 
222
209
        /* Should be running */
223
210
        assert0 (kill (upstart_pid, 0));
266
253
        /* Disable user mode */
267
254
        test_user_mode = FALSE;
268
255
 
269
 
        start_upstart_common (&upstart_pid, FALSE, NULL, logdir, NULL);
 
256
        start_upstart_common (&upstart_pid, FALSE, FALSE, NULL, logdir, NULL);
270
257
 
271
258
        /* Should be running */
272
259
        assert0 (kill (upstart_pid, 0));
296
283
        CREATE_FILE (confdir_a, "bar.conf", "exec true");
297
284
        CREATE_FILE (confdir_b, "baz.conf", "exec true");
298
285
 
299
 
        start_upstart_common (&upstart_pid, FALSE, confdir_b, logdir, NULL);
 
286
        start_upstart_common (&upstart_pid, FALSE, FALSE, confdir_b, logdir, NULL);
300
287
 
301
288
        /* Should be running */
302
289
        assert0 (kill (upstart_pid, 0));
333
320
        extra[3] = confdir_b;
334
321
        extra[4] = NULL;
335
322
 
336
 
        start_upstart_common (&upstart_pid, FALSE, NULL, logdir, extra);
 
323
        start_upstart_common (&upstart_pid, FALSE, FALSE, NULL, logdir, extra);
337
324
 
338
325
        /* Should be running */
339
326
        assert0 (kill (upstart_pid, 0));
376
363
        extra[3] = confdir_b;
377
364
        extra[4] = NULL;
378
365
 
379
 
        start_upstart_common (&upstart_pid, FALSE, NULL, logdir, extra);
 
366
        start_upstart_common (&upstart_pid, FALSE, FALSE, NULL, logdir, extra);
380
367
 
381
368
        /* Should be running */
382
369
        assert0 (kill (upstart_pid, 0));
412
399
 
413
400
        TEST_DBUS_END (dbus_pid);
414
401
 
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
402
        assert0 (rmdir (confdir_a));
430
403
        assert0 (rmdir (confdir_b));
431
404
        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
405
        assert0 (rmdir (logdir));
442
406
        assert0 (unsetenv ("UPSTART_CONFDIR"));
443
407
}
446
410
main (int   argc,
447
411
      char *argv[])
448
412
{
 
413
        test_common_setup ();
 
414
 
449
415
        test_confdir ();
450
416
 
 
417
        test_common_cleanup ();
 
418
 
451
419
        return 0;
452
420
}