~daniel-sonck/upstart/cron-replacement

« back to all changes in this revision

Viewing changes to util/tests/test_initctl.c

  • Committer: dsonck
  • Date: 2011-10-14 14:32:00 UTC
  • mfrom: (1282.1.47 upstart)
  • Revision ID: dsonck@daniel-desktop-20111014143200-ezekcd0k43ykog2l
Merged with the latest startup branch

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
 * test_initctl.c - test suite for util/initctl.c
4
4
 *
5
5
 * Copyright © 2010 Canonical Ltd.
6
 
 * Author: Scott James Remnant <scott@netsplit.com>.
 
6
 * Authors: Scott James Remnant <scott@netsplit.com>,
 
7
 *          James Hunt <james.hunt@canonical.com>.
7
8
 *
8
9
 * This program is free software; you can redistribute it and/or modify
9
10
 * it under the terms of the GNU General Public License version 2, as
27
28
#include <stdio.h>
28
29
#include <signal.h>
29
30
#include <unistd.h>
 
31
#include <sys/types.h>        
 
32
#include <sys/stat.h>
30
33
 
31
34
#include <nih-dbus/dbus_error.h>
32
35
#include <nih-dbus/dbus_connection.h>
40
43
#include <nih/main.h>
41
44
#include <nih/command.h>
42
45
#include <nih/error.h>
 
46
#include <nih/string.h>
43
47
 
44
48
#include "dbus/upstart.h"
45
49
 
46
 
 
47
 
extern int system_bus;
 
50
/* remember we run from the 'util' directory */
 
51
#define UPSTART_BINARY "../init/init"
 
52
#define INITCTL_BINARY "./initctl --session"
 
53
 
 
54
#define BUFFER_SIZE 1024
 
55
 
 
56
/**
 
57
 * START_UPSTART:
 
58
 *
 
59
 * @pid: pid_t that will contain pid of running instance on success.
 
60
 *
 
61
 * Start an instance of Upstart. Fork errors are fatal, but after
 
62
 * a successful fork, waits for up to a somewhat arbitrary (but
 
63
 * more than adequate!) amount of time for Upstart to initialize.
 
64
 **/
 
65
#define START_UPSTART(pid)                                           \
 
66
{                                                                    \
 
67
        nih_local NihDBusProxy *upstart = NULL;                      \
 
68
        DBusConnection         *connection;                          \
 
69
        DBusError               dbus_error;                          \
 
70
                                                                     \
 
71
        /* XXX: arbitrary value */                                   \
 
72
        int                     attempts = 10;                       \
 
73
                                                                     \
 
74
                                                                     \
 
75
        TEST_NE (pid = fork (), -1);                                 \
 
76
                                                                     \
 
77
        if (pid == 0)                                                \
 
78
                execlp (UPSTART_BINARY, UPSTART_BINARY,              \
 
79
                                "--session",                         \
 
80
                                "--no-startup-event",                \
 
81
                                "--no-sessions",                     \
 
82
                                NULL);                               \
 
83
                                                                     \
 
84
        while (attempts) {                                           \
 
85
                attempts--;                                          \
 
86
                sleep (1);                                           \
 
87
                dbus_error_init (&dbus_error);                       \
 
88
                connection = dbus_bus_get (DBUS_BUS_SESSION,         \
 
89
                                &dbus_error);                        \
 
90
                                                                     \
 
91
                if (! connection) {                                  \
 
92
                        dbus_error_free (&dbus_error);               \
 
93
                        continue;                                    \
 
94
                }                                                    \
 
95
                dbus_error_free (&dbus_error);                       \
 
96
                                                                     \
 
97
                upstart = nih_dbus_proxy_new (NULL, connection,      \
 
98
                                      DBUS_SERVICE_UPSTART,          \
 
99
                                      DBUS_PATH_UPSTART,             \
 
100
                                      NULL, NULL);                   \
 
101
                                                                     \
 
102
                if (! upstart) {                                     \
 
103
                        NihError *err;                               \
 
104
                        err = nih_error_get ();                      \
 
105
                        nih_free (err);                              \
 
106
                        dbus_connection_unref (connection);          \
 
107
                }                                                    \
 
108
                else {                                               \
 
109
                        break;                                       \
 
110
                }                                                    \
 
111
        }                                                            \
 
112
}
 
113
 
 
114
/**
 
115
 * STOP_UPSTART:
 
116
 *
 
117
 * @pid: pid of upstart to kill.
 
118
 *
 
119
 * Stop upstart process @pid.
 
120
 **/
 
121
#define STOP_UPSTART(pid)                                            \
 
122
{                                                                    \
 
123
        assert (pid);                                                \
 
124
                                                                     \
 
125
        if (kill (pid, 0) == 0) {                                    \
 
126
                kill (pid, SIGTERM);                                 \
 
127
                sleep (1);                                           \
 
128
        }                                                            \
 
129
                                                                     \
 
130
        if (kill (pid, 0) == 0) {                                    \
 
131
                kill (pid, SIGKILL);                                 \
 
132
        }                                                            \
 
133
}
 
134
 
 
135
/**
 
136
 * RUN_COMMAND:
 
137
 *
 
138
 * @parent: pointer to parent object,
 
139
 * @cmd: string representing command to run,
 
140
 * @result: "char ***" pointer which will contain an array of string
 
141
 * values corresponding to lines of standard output generated by @cmd,
 
142
 * @len: size_t pointer which will be set to length of @result.
 
143
 *
 
144
 * Run a command and return its standard output. It is the callers
 
145
 * responsibility to free @result. Errors from running @cmd are fatal.
 
146
 **/
 
147
#define RUN_COMMAND(parent, cmd, result, len)                        \
 
148
{                                                                    \
 
149
        FILE    *f;                                                  \
 
150
        char     buffer[BUFFER_SIZE];                                \
 
151
        char   **ret;                                                \
 
152
                                                                     \
 
153
        assert (cmd[0]);                                             \
 
154
                                                                     \
 
155
        *(result) = nih_str_array_new (parent);                      \
 
156
        TEST_NE_P (*result, NULL);                                   \
 
157
        *(len) = 0;                                                  \
 
158
                                                                     \
 
159
        f = popen (cmd, "r");                                        \
 
160
        TEST_NE_P (f, NULL);                                         \
 
161
                                                                     \
 
162
        while (fgets (buffer, BUFFER_SIZE, f)) {                     \
 
163
                size_t l = strlen (buffer)-1;                        \
 
164
                                                                     \
 
165
                if ( buffer[l] == '\n')                              \
 
166
                        buffer[l] = '\0';                            \
 
167
                ret = nih_str_array_add (result, parent, len,        \
 
168
                        buffer);                                     \
 
169
                TEST_NE_P (ret, NULL);                               \
 
170
        }                                                            \
 
171
                                                                     \
 
172
        TEST_NE ( pclose (f), -1);                                   \
 
173
}
 
174
 
 
175
/**
 
176
 * CREATE_FILE:
 
177
 *
 
178
 * @dirname: directory name (assumed to already exist),
 
179
 * @name: name of file to create (no leading slash),
 
180
 * @contents: string contents of @name.
 
181
 *
 
182
 * Create a file in the specified directory with the specified
 
183
 * contents.
 
184
 *
 
185
 * Notes: A newline character is added in the case where @contents does
 
186
 * not end with one.
 
187
 **/
 
188
#define CREATE_FILE(dirname, name, contents)                         \
 
189
{                                                                    \
 
190
        FILE    *f;                                                  \
 
191
        char     filename[PATH_MAX];                                 \
 
192
                                                                     \
 
193
        assert (dirname[0]);                                         \
 
194
        assert (name[0]);                                            \
 
195
                                                                     \
 
196
        strcpy (filename, dirname);                                  \
 
197
        if ( name[0] != '/' )                                        \
 
198
          strcat (filename, "/");                                    \
 
199
        strcat (filename, name);                                     \
 
200
        f = fopen (filename, "w");                                   \
 
201
        TEST_NE_P (f, NULL);                                         \
 
202
        fprintf (f, contents);                                       \
 
203
        if ( contents[strlen(contents)-1] != '\n')                   \
 
204
          fprintf (f, "\n");                                         \
 
205
        fclose (f);                                                  \
 
206
}
 
207
 
 
208
/**
 
209
 * DELETE_FILE:
 
210
 *
 
211
 * @dirname: directory in which file to delete exists,
 
212
 * @name: name of file in @dirname to delete.
 
213
 *
 
214
 * Delete specified file.
 
215
 *
 
216
 **/
 
217
#define DELETE_FILE(dirname, name)                                   \
 
218
{                                                                    \
 
219
        char     filename[PATH_MAX];                                 \
 
220
                                                                     \
 
221
        assert (dirname[0]);                                         \
 
222
        assert (name[0]);                                            \
 
223
                                                                     \
 
224
        strcpy (filename, dirname);                                  \
 
225
        if ( name[0] != '/' )                                        \
 
226
          strcat (filename, "/");                                    \
 
227
        strcat (filename, name);                                     \
 
228
                                                                     \
 
229
        TEST_EQ (unlink (filename), 0);                              \
 
230
}
 
231
 
 
232
extern int use_dbus;
 
233
extern int dbus_bus_type;
48
234
extern char *dest_name;
49
235
extern const char *dest_address;
50
236
extern int no_wait;
103
289
         */
104
290
        TEST_FEATURE ("with private connection");
105
291
        TEST_ALLOC_FAIL {
106
 
                system_bus = FALSE;
 
292
                use_dbus = FALSE;
107
293
                dest_name = NULL;
108
294
                dest_address = "unix:abstract=/com/ubuntu/upstart/test";
109
295
 
179
365
         */
180
366
        TEST_FEATURE ("with system bus connection");
181
367
        TEST_ALLOC_FAIL {
182
 
                system_bus = TRUE;
 
368
                use_dbus = TRUE;
 
369
                dbus_bus_type = DBUS_BUS_SYSTEM;
183
370
                dest_name = NULL;
184
371
                dest_address = DBUS_ADDRESS_UPSTART;
185
372
 
240
427
         */
241
428
        TEST_FEATURE ("with system bus connection and different name");
242
429
        TEST_ALLOC_FAIL {
243
 
                system_bus = TRUE;
 
430
                use_dbus = TRUE;
 
431
                dbus_bus_type = DBUS_BUS_SYSTEM;
244
432
                dest_name = "com.ubuntu.UpstartTest";
245
433
                dest_address = DBUS_ADDRESS_UPSTART;
246
434
 
302
490
         */
303
491
        TEST_FEATURE ("with non-listening private connection");
304
492
        TEST_ALLOC_FAIL {
305
 
                system_bus = FALSE;
 
493
                use_dbus = FALSE;
306
494
                dest_name = NULL;
307
495
                dest_address = "unix:abstract=/com/ubuntu/upstart/test";
308
496
 
328
516
         */
329
517
        TEST_FEATURE ("with non-listening system bus");
330
518
        TEST_ALLOC_FAIL {
331
 
                system_bus = TRUE;
 
519
                use_dbus = TRUE;
 
520
                dbus_bus_type = DBUS_BUS_SYSTEM;
332
521
                dest_name = NULL;
333
522
                dest_address = DBUS_ADDRESS_UPSTART;
334
523
 
360
549
         */
361
550
        TEST_FEATURE ("with --dest but without --system");
362
551
        TEST_ALLOC_FAIL {
363
 
                system_bus = FALSE;
 
552
                use_dbus = FALSE;
364
553
                dest_name = "com.ubuntu.Upstart";
365
554
                dest_address = DBUS_ADDRESS_UPSTART;
366
555
 
3229
3418
                                        "NameAcquired"));
3230
3419
        dbus_message_unref (method_call);
3231
3420
 
3232
 
        system_bus = TRUE;
 
3421
        use_dbus = TRUE;
 
3422
        dbus_bus_type = DBUS_BUS_SYSTEM;
3233
3423
        dest_name = DBUS_SERVICE_UPSTART;
3234
3424
        dest_address = DBUS_ADDRESS_UPSTART;
3235
3425
 
4999
5189
                                        "NameAcquired"));
5000
5190
        dbus_message_unref (method_call);
5001
5191
 
5002
 
        system_bus = TRUE;
 
5192
        use_dbus = TRUE;
 
5193
        dbus_bus_type = DBUS_BUS_SYSTEM;
5003
5194
        dest_name = DBUS_SERVICE_UPSTART;
5004
5195
        dest_address = DBUS_ADDRESS_UPSTART;
5005
5196
 
6584
6775
                                        "NameAcquired"));
6585
6776
        dbus_message_unref (method_call);
6586
6777
 
6587
 
        system_bus = TRUE;
 
6778
        use_dbus = TRUE;
 
6779
        dbus_bus_type = DBUS_BUS_SYSTEM;
6588
6780
        dest_name = DBUS_SERVICE_UPSTART;
6589
6781
        dest_address = DBUS_ADDRESS_UPSTART;
6590
6782
 
8357
8549
                                        "NameAcquired"));
8358
8550
        dbus_message_unref (method_call);
8359
8551
 
8360
 
        system_bus = TRUE;
 
8552
        use_dbus = TRUE;
 
8553
        dbus_bus_type = DBUS_BUS_SYSTEM;
8361
8554
        dest_name = DBUS_SERVICE_UPSTART;
8362
8555
        dest_address = DBUS_ADDRESS_UPSTART;
8363
8556
 
9239
9432
                                        "NameAcquired"));
9240
9433
        dbus_message_unref (method_call);
9241
9434
 
9242
 
        system_bus = TRUE;
 
9435
        use_dbus = TRUE;
 
9436
        dbus_bus_type = DBUS_BUS_SYSTEM;
9243
9437
        dest_name = DBUS_SERVICE_UPSTART;
9244
9438
        dest_address = DBUS_ADDRESS_UPSTART;
9245
9439
 
10730
10924
        dbus_shutdown ();
10731
10925
}
10732
10926
 
 
10927
void
 
10928
test_show_config (void)
 
10929
{
 
10930
        char             dirname[PATH_MAX];
 
10931
        nih_local char  *cmd;
 
10932
        pid_t            upstart_pid = 0;
 
10933
        pid_t            dbus_pid    = 0;
 
10934
        char           **output;
 
10935
        size_t           lines;
 
10936
        char             expected_output[] = "foo";
 
10937
 
 
10938
        TEST_GROUP ("show_config");
 
10939
 
 
10940
        TEST_FILENAME (dirname);
 
10941
        TEST_EQ (mkdir (dirname, 0755), 0);
 
10942
 
 
10943
        /* Use the "secret" interface */
 
10944
        TEST_EQ (setenv ("UPSTART_CONFDIR", dirname, 1), 0);
 
10945
 
 
10946
        TEST_DBUS (dbus_pid);
 
10947
        START_UPSTART (upstart_pid);
 
10948
 
 
10949
        TEST_FEATURE ("no emits, no start on, no stop on");
 
10950
        CREATE_FILE (dirname, "foo.conf",
 
10951
                        "author \"foo\"\n"
 
10952
                        "description \"wibble\"");
 
10953
 
 
10954
        cmd = nih_sprintf (NULL, "%s show-config foo 2>&1", INITCTL_BINARY);
 
10955
        TEST_NE_P (cmd, NULL);
 
10956
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
10957
        TEST_EQ_STR (output[0], expected_output);
 
10958
        TEST_EQ (lines, 1);
 
10959
        nih_free (output);
 
10960
 
 
10961
        cmd = nih_sprintf (NULL, "%s show-config -e foo 2>&1", INITCTL_BINARY);
 
10962
        TEST_NE_P (cmd, NULL);
 
10963
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
10964
        TEST_EQ_STR (output[0], expected_output);
 
10965
        TEST_EQ (lines, 1);
 
10966
        nih_free (output);
 
10967
 
 
10968
        DELETE_FILE (dirname, "foo.conf");
 
10969
 
 
10970
        /*******************************************************************/
 
10971
 
 
10972
        TEST_FEATURE ("1 emits, no start on, no stop on");
 
10973
 
 
10974
        CREATE_FILE (dirname, "foo.conf",
 
10975
                        "author \"foo\"\n"
 
10976
                        "emits \"thing\"\n"
 
10977
                        "description \"wibble\"");
 
10978
 
 
10979
        cmd = nih_sprintf (NULL, "%s show-config foo 2>&1", INITCTL_BINARY);
 
10980
        TEST_NE_P (cmd, NULL);
 
10981
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
10982
        TEST_EQ_STR (output[0], expected_output);
 
10983
        TEST_EQ_STR (output[1], "  emits thing");
 
10984
        TEST_EQ (lines, 2);
 
10985
        nih_free (output);
 
10986
 
 
10987
        cmd = nih_sprintf (NULL, "%s show-config -e foo 2>&1", INITCTL_BINARY);
 
10988
        TEST_NE_P (cmd, NULL);
 
10989
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
10990
        TEST_EQ_STR (output[0], expected_output);
 
10991
        TEST_EQ_STR (output[1], "  emits thing");
 
10992
        TEST_EQ (lines, 2);
 
10993
        nih_free (output);
 
10994
 
 
10995
        DELETE_FILE (dirname, "foo.conf");
 
10996
 
 
10997
        /*******************************************************************/
 
10998
 
 
10999
        TEST_FEATURE ("2 emits, no start on, no stop on");
 
11000
 
 
11001
        CREATE_FILE (dirname, "foo.conf",
 
11002
                        "author \"foo\"\n"
 
11003
                        "emits \"thing\"\n"
 
11004
                        "emits \"thong\"\n"
 
11005
                        "description \"wibble\"");
 
11006
 
 
11007
        cmd = nih_sprintf (NULL, "%s show-config foo 2>&1", INITCTL_BINARY);
 
11008
        TEST_NE_P (cmd, NULL);
 
11009
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
11010
        TEST_EQ_STR (output[0], expected_output);
 
11011
        TEST_EQ_STR (output[1], "  emits thing");
 
11012
        TEST_EQ_STR (output[2], "  emits thong");
 
11013
        TEST_EQ (lines, 3);
 
11014
        nih_free (output);
 
11015
 
 
11016
        cmd = nih_sprintf (NULL, "%s show-config -e foo 2>&1", INITCTL_BINARY);
 
11017
        TEST_NE_P (cmd, NULL);
 
11018
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
11019
        TEST_EQ_STR (output[0], expected_output);
 
11020
        TEST_EQ_STR (output[1], "  emits thing");
 
11021
        TEST_EQ_STR (output[2], "  emits thong");
 
11022
        TEST_EQ (lines, 3);
 
11023
        nih_free (output);
 
11024
 
 
11025
        DELETE_FILE (dirname, "foo.conf");
 
11026
 
 
11027
        /*******************************************************************/
 
11028
 
 
11029
        TEST_FEATURE ("no emits, start on, no stop on");
 
11030
 
 
11031
        CREATE_FILE (dirname, "foo.conf",
 
11032
                        "author \"foo\"\n"
 
11033
                        "start on (A and B)\n"
 
11034
                        "description \"wibble\"");
 
11035
 
 
11036
        cmd = nih_sprintf (NULL, "%s show-config foo 2>&1", INITCTL_BINARY);
 
11037
        TEST_NE_P (cmd, NULL);
 
11038
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
11039
        TEST_EQ_STR (output[0], expected_output);
 
11040
        TEST_EQ_STR (output[1], "  start on (A and B)");
 
11041
        TEST_EQ (lines, 2);
 
11042
        nih_free (output);
 
11043
 
 
11044
        cmd = nih_sprintf (NULL, "%s show-config -e foo 2>&1", INITCTL_BINARY);
 
11045
        TEST_NE_P (cmd, NULL);
 
11046
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
11047
        TEST_EQ_STR (output[0], expected_output);
 
11048
        TEST_EQ_STR (output[1], "  start on A (job:, env:)");
 
11049
        TEST_EQ_STR (output[2], "  start on B (job:, env:)");
 
11050
        TEST_EQ (lines, 3);
 
11051
        nih_free (output);
 
11052
 
 
11053
        DELETE_FILE (dirname, "foo.conf");
 
11054
 
 
11055
        /*******************************************************************/
 
11056
 
 
11057
        TEST_FEATURE ("1 emits, start on, no stop on");
 
11058
 
 
11059
        CREATE_FILE (dirname, "foo.conf",
 
11060
                        "author \"foo\"\n"
 
11061
                        "emits \"bong\"\n"
 
11062
                        "start on (A and B)\n"
 
11063
                        "description \"wibble\"");
 
11064
 
 
11065
        cmd = nih_sprintf (NULL, "%s show-config foo 2>&1", INITCTL_BINARY);
 
11066
        TEST_NE_P (cmd, NULL);
 
11067
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
11068
        TEST_EQ_STR (output[0], expected_output);
 
11069
        TEST_EQ_STR (output[1], "  emits bong");
 
11070
        TEST_EQ_STR (output[2], "  start on (A and B)");
 
11071
        TEST_EQ (lines, 3);
 
11072
        nih_free (output);
 
11073
 
 
11074
        cmd = nih_sprintf (NULL, "%s show-config -e foo 2>&1", INITCTL_BINARY);
 
11075
        TEST_NE_P (cmd, NULL);
 
11076
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
11077
        TEST_EQ_STR (output[0], expected_output);
 
11078
        TEST_EQ_STR (output[1], "  emits bong");
 
11079
        TEST_EQ_STR (output[2], "  start on A (job:, env:)");
 
11080
        TEST_EQ_STR (output[3], "  start on B (job:, env:)");
 
11081
        TEST_EQ (lines, 4);
 
11082
        nih_free (output);
 
11083
 
 
11084
        DELETE_FILE (dirname, "foo.conf");
 
11085
 
 
11086
        /*******************************************************************/
 
11087
 
 
11088
        TEST_FEATURE ("2 emits, start on, no stop on");
 
11089
 
 
11090
        CREATE_FILE (dirname, "foo.conf",
 
11091
                        "emits \"bong\"\n"
 
11092
                        "author \"foo\"\n"
 
11093
                        "start on (A and B)\n"
 
11094
                        "emits \"stime\"\n"
 
11095
                        "description \"wibble\"");
 
11096
 
 
11097
        cmd = nih_sprintf (NULL, "%s show-config foo 2>&1", INITCTL_BINARY);
 
11098
        TEST_NE_P (cmd, NULL);
 
11099
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
11100
        TEST_EQ_STR (output[0], expected_output);
 
11101
        TEST_EQ_STR (output[1], "  emits bong");
 
11102
        TEST_EQ_STR (output[2], "  emits stime");
 
11103
        TEST_EQ_STR (output[3], "  start on (A and B)");
 
11104
        TEST_EQ (lines, 4);
 
11105
        nih_free (output);
 
11106
 
 
11107
        cmd = nih_sprintf (NULL, "%s show-config -e foo 2>&1", INITCTL_BINARY);
 
11108
        TEST_NE_P (cmd, NULL);
 
11109
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
11110
        TEST_EQ_STR (output[0], expected_output);
 
11111
        TEST_EQ_STR (output[1], "  emits bong");
 
11112
        TEST_EQ_STR (output[2], "  emits stime");
 
11113
        TEST_EQ_STR (output[3], "  start on A (job:, env:)");
 
11114
        TEST_EQ_STR (output[4], "  start on B (job:, env:)");
 
11115
        TEST_EQ (lines, 5);
 
11116
        nih_free (output);
 
11117
 
 
11118
        DELETE_FILE (dirname, "foo.conf");
 
11119
 
 
11120
        /*******************************************************************/
 
11121
 
 
11122
        TEST_FEATURE ("no emits, no start on, stop on");
 
11123
 
 
11124
        CREATE_FILE (dirname, "foo.conf",
 
11125
                        "author \"foo\"\n"
 
11126
                        "stop on (A or B)\n"
 
11127
                        "description \"wibble\"");
 
11128
 
 
11129
        cmd = nih_sprintf (NULL, "%s show-config foo 2>&1", INITCTL_BINARY);
 
11130
        TEST_NE_P (cmd, NULL);
 
11131
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
11132
        TEST_EQ_STR (output[0], expected_output);
 
11133
        TEST_EQ_STR (output[1], "  stop on (A or B)");
 
11134
        TEST_EQ (lines, 2);
 
11135
        nih_free (output);
 
11136
 
 
11137
        cmd = nih_sprintf (NULL, "%s show-config -e foo 2>&1", INITCTL_BINARY);
 
11138
        TEST_NE_P (cmd, NULL);
 
11139
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
11140
        TEST_EQ_STR (output[0], expected_output);
 
11141
        TEST_EQ_STR (output[1], "  stop on A (job:, env:)");
 
11142
        TEST_EQ_STR (output[2], "  stop on B (job:, env:)");
 
11143
        TEST_EQ (lines, 3);
 
11144
        nih_free (output);
 
11145
 
 
11146
        DELETE_FILE (dirname, "foo.conf");
 
11147
 
 
11148
        /*******************************************************************/
 
11149
 
 
11150
        TEST_FEATURE ("1 emits, no start on, stop on");
 
11151
 
 
11152
        CREATE_FILE (dirname, "foo.conf",
 
11153
                        "author \"foo\"\n"
 
11154
                        "emits \"bong\"\n"
 
11155
                        "stop on (A or B)\n"
 
11156
                        "description \"wibble\"");
 
11157
 
 
11158
        cmd = nih_sprintf (NULL, "%s show-config foo 2>&1", INITCTL_BINARY);
 
11159
        TEST_NE_P (cmd, NULL);
 
11160
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
11161
        TEST_EQ_STR (output[0], expected_output);
 
11162
        TEST_EQ_STR (output[1], "  emits bong");
 
11163
        TEST_EQ_STR (output[2], "  stop on (A or B)");
 
11164
        TEST_EQ (lines, 3);
 
11165
        nih_free (output);
 
11166
 
 
11167
        cmd = nih_sprintf (NULL, "%s show-config -e foo 2>&1", INITCTL_BINARY);
 
11168
        TEST_NE_P (cmd, NULL);
 
11169
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
11170
        TEST_EQ_STR (output[0], expected_output);
 
11171
        TEST_EQ_STR (output[1], "  emits bong");
 
11172
        TEST_EQ_STR (output[2], "  stop on A (job:, env:)");
 
11173
        TEST_EQ_STR (output[3], "  stop on B (job:, env:)");
 
11174
        TEST_EQ (lines, 4);
 
11175
        nih_free (output);
 
11176
 
 
11177
        DELETE_FILE (dirname, "foo.conf");
 
11178
 
 
11179
        /*******************************************************************/
 
11180
 
 
11181
        TEST_FEATURE ("2 emits, no start on, stop on");
 
11182
 
 
11183
        CREATE_FILE (dirname, "foo.conf",
 
11184
                        "emits \"bong\"\n"
 
11185
                        "author \"foo\"\n"
 
11186
                        "stop on (A or B)\n"
 
11187
                        "emits \"stime\"\n"
 
11188
                        "description \"wibble\"");
 
11189
 
 
11190
        cmd = nih_sprintf (NULL, "%s show-config foo 2>&1", INITCTL_BINARY);
 
11191
        TEST_NE_P (cmd, NULL);
 
11192
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
11193
        TEST_EQ_STR (output[0], expected_output);
 
11194
        TEST_EQ_STR (output[1], "  emits bong");
 
11195
        TEST_EQ_STR (output[2], "  emits stime");
 
11196
        TEST_EQ_STR (output[3], "  stop on (A or B)");
 
11197
        TEST_EQ (lines, 4);
 
11198
        nih_free (output);
 
11199
 
 
11200
        cmd = nih_sprintf (NULL, "%s show-config -e foo 2>&1", INITCTL_BINARY);
 
11201
        TEST_NE_P (cmd, NULL);
 
11202
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
11203
        TEST_EQ_STR (output[0], expected_output);
 
11204
        TEST_EQ_STR (output[1], "  emits bong");
 
11205
        TEST_EQ_STR (output[2], "  emits stime");
 
11206
        TEST_EQ_STR (output[3], "  stop on A (job:, env:)");
 
11207
        TEST_EQ_STR (output[4], "  stop on B (job:, env:)");
 
11208
        TEST_EQ (lines, 5);
 
11209
        nih_free (output);
 
11210
 
 
11211
        DELETE_FILE (dirname, "foo.conf");
 
11212
 
 
11213
        /*******************************************************************/
 
11214
 
 
11215
        TEST_FEATURE ("2 emits, start on with only initial JOB, stop on with JOB at end of env");
 
11216
 
 
11217
        CREATE_FILE (dirname, "foo.conf",
 
11218
                        "emits \"bong\"\n"
 
11219
                        "author \"foo\"\n"
 
11220
                        "stop on (A or stopping c=d e=f g=h JOB=\"bang\")\n"
 
11221
                        "emits \"stime\"\n"
 
11222
                        "start on (starting JOB=\"boo\" or B x=y)\n"
 
11223
                        "description \"wibble\"");
 
11224
 
 
11225
        cmd = nih_sprintf (NULL, "%s show-config foo 2>&1", INITCTL_BINARY);
 
11226
        TEST_NE_P (cmd, NULL);
 
11227
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
11228
        TEST_EQ_STR (output[0], expected_output);
 
11229
        TEST_EQ_STR (output[1], "  emits bong");
 
11230
        TEST_EQ_STR (output[2], "  emits stime");
 
11231
        TEST_EQ_STR (output[3], "  start on (starting JOB=boo or B x=y)");
 
11232
        TEST_EQ_STR (output[4], "  stop on (A or stopping c=d e=f g=h JOB=bang)");
 
11233
        TEST_EQ (lines, 5);
 
11234
        nih_free (output);
 
11235
 
 
11236
        cmd = nih_sprintf (NULL, "%s show-config -e foo 2>&1", INITCTL_BINARY);
 
11237
        TEST_NE_P (cmd, NULL);
 
11238
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
11239
        TEST_EQ_STR (output[0], expected_output);
 
11240
        TEST_EQ_STR (output[1], "  emits bong");
 
11241
        TEST_EQ_STR (output[2], "  emits stime");
 
11242
        TEST_EQ_STR (output[3], "  start on starting (job: boo, env:)");
 
11243
        TEST_EQ_STR (output[4], "  start on B (job:, env: x=y)");
 
11244
        TEST_EQ_STR (output[5], "  stop on A (job:, env:)");
 
11245
        TEST_EQ_STR (output[6], "  stop on stopping (job: bang, env: c=d e=f g=h)");
 
11246
        TEST_EQ (lines, 7);
 
11247
        nih_free (output);
 
11248
 
 
11249
        DELETE_FILE (dirname, "foo.conf");
 
11250
 
 
11251
        /*******************************************************************/
 
11252
 
 
11253
        TEST_FEATURE ("2 emits, start on with initial JOB+env, stop on with JOB at end of env");
 
11254
 
 
11255
        CREATE_FILE (dirname, "foo.conf",
 
11256
                        "emits \"bong\"\n"
 
11257
                        "author \"foo\"\n"
 
11258
                        "stop on (A or stopping c=d e=f g=h JOB=\"bang\")\n"
 
11259
                        "emits \"stime\"\n"
 
11260
                        "start on (starting JOB=\"boo\" P=Q c=sea or B x=y)\n"
 
11261
                        "description \"wibble\"");
 
11262
 
 
11263
        cmd = nih_sprintf (NULL, "%s show-config foo 2>&1", INITCTL_BINARY);
 
11264
        TEST_NE_P (cmd, NULL);
 
11265
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
11266
        TEST_EQ_STR (output[0], expected_output);
 
11267
        TEST_EQ_STR (output[1], "  emits bong");
 
11268
        TEST_EQ_STR (output[2], "  emits stime");
 
11269
        TEST_EQ_STR (output[3], "  start on (starting JOB=boo P=Q c=sea or B x=y)");
 
11270
        TEST_EQ_STR (output[4], "  stop on (A or stopping c=d e=f g=h JOB=bang)");
 
11271
        TEST_EQ (lines, 5);
 
11272
        nih_free (output);
 
11273
 
 
11274
        cmd = nih_sprintf (NULL, "%s show-config -e foo 2>&1", INITCTL_BINARY);
 
11275
        TEST_NE_P (cmd, NULL);
 
11276
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
11277
        TEST_EQ_STR (output[0], expected_output);
 
11278
        TEST_EQ_STR (output[1], "  emits bong");
 
11279
        TEST_EQ_STR (output[2], "  emits stime");
 
11280
        TEST_EQ_STR (output[3], "  start on starting (job: boo, env: P=Q c=sea)");
 
11281
        TEST_EQ_STR (output[4], "  start on B (job:, env: x=y)");
 
11282
        TEST_EQ_STR (output[5], "  stop on A (job:, env:)");
 
11283
        TEST_EQ_STR (output[6], "  stop on stopping (job: bang, env: c=d e=f g=h)");
 
11284
        TEST_EQ (lines, 7);
 
11285
        nih_free (output);
 
11286
 
 
11287
        DELETE_FILE (dirname, "foo.conf");
 
11288
 
 
11289
        /*******************************************************************/
 
11290
 
 
11291
        TEST_FEATURE ("3 emits, start on (with env), stop on (with env)");
 
11292
 
 
11293
        CREATE_FILE (dirname, "foo.conf",
 
11294
                        "emits \"bong\"\n"
 
11295
                        "stop on starting D and (stopping E or F hello=world)\n"
 
11296
                        "author \"foo\"\n"
 
11297
                        "emits \"bar\"\n"
 
11298
                        "emits \"stime\"\n"
 
11299
                        "start on A and (B FOO=BAR or starting C x=y)\n"
 
11300
                        "description \"wibble\"");
 
11301
 
 
11302
        cmd = nih_sprintf (NULL, "%s show-config foo 2>&1", INITCTL_BINARY);
 
11303
        TEST_NE_P (cmd, NULL);
 
11304
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
11305
        TEST_EQ_STR (output[0], expected_output);
 
11306
        TEST_EQ_STR (output[1], "  emits bong");
 
11307
        TEST_EQ_STR (output[2], "  emits bar");
 
11308
        TEST_EQ_STR (output[3], "  emits stime");
 
11309
        /* note the extra brackets! */
 
11310
        TEST_EQ_STR (output[4], "  start on (A and (B FOO=BAR or starting C x=y))");
 
11311
        /* note the extra brackets! */
 
11312
        TEST_EQ_STR (output[5], "  stop on (starting D and (stopping E or F hello=world))");
 
11313
        TEST_EQ (lines, 6);
 
11314
        nih_free (output);
 
11315
 
 
11316
        cmd = nih_sprintf (NULL, "%s show-config -e foo 2>&1", INITCTL_BINARY);
 
11317
        TEST_NE_P (cmd, NULL);
 
11318
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
11319
        TEST_EQ_STR (output[0], expected_output);
 
11320
        TEST_EQ_STR (output[1], "  emits bong");
 
11321
        TEST_EQ_STR (output[2], "  emits bar");
 
11322
        TEST_EQ_STR (output[3], "  emits stime");
 
11323
        TEST_EQ_STR (output[4], "  start on A (job:, env:)");
 
11324
        TEST_EQ_STR (output[5], "  start on B (job:, env: FOO=BAR)");
 
11325
        TEST_EQ_STR (output[6], "  start on starting (job: C, env: x=y)");
 
11326
        TEST_EQ_STR (output[7], "  stop on starting (job: D, env:)");
 
11327
        TEST_EQ_STR (output[8], "  stop on stopping (job: E, env:)");
 
11328
        TEST_EQ_STR (output[9], "  stop on F (job:, env: hello=world)");
 
11329
        TEST_EQ (lines, 10);
 
11330
        nih_free (output);
 
11331
 
 
11332
        DELETE_FILE (dirname, "foo.conf");
 
11333
 
 
11334
        /*******************************************************************/
 
11335
 
 
11336
        TEST_FEATURE ("3 emits, complex start on (with env), complex stop on (with env)");
 
11337
 
 
11338
        CREATE_FILE (dirname, "foo.conf",
 
11339
                        "emits \"bong\"\n"
 
11340
                        "stop on runlevel [!2345] colour=blue or starting rocket\n"
 
11341
                        "author \"foo\"\n"
 
11342
                        "emits \"bar\"\n"
 
11343
                        "emits \"stime\"\n"
 
11344
                        "start on (starting mountall or (runlevel [016] and "
 
11345
                        "(stopped gdm or stopped kdm or stopped xdm A=B or stopping lxdm)))\n"
 
11346
                        "description \"wibble\"");
 
11347
 
 
11348
        cmd = nih_sprintf (NULL, "%s show-config foo 2>&1", INITCTL_BINARY);
 
11349
        TEST_NE_P (cmd, NULL);
 
11350
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
11351
        TEST_EQ_STR (output[0], expected_output);
 
11352
        TEST_EQ_STR (output[1], "  emits bong");
 
11353
        TEST_EQ_STR (output[2], "  emits bar");
 
11354
        TEST_EQ_STR (output[3], "  emits stime");
 
11355
        /* note the extra brackets! */
 
11356
        TEST_EQ_STR (output[4], "  start on (starting mountall or (runlevel [016] and "
 
11357
                        "(((stopped gdm or stopped kdm) or stopped xdm A=B) or stopping lxdm)))");
 
11358
        /* note the extra brackets! */
 
11359
        TEST_EQ_STR (output[5], "  stop on (runlevel [!2345] colour=blue or starting rocket)");
 
11360
        TEST_EQ (lines, 6);
 
11361
        nih_free (output);
 
11362
 
 
11363
        cmd = nih_sprintf (NULL, "%s show-config -e foo 2>&1", INITCTL_BINARY);
 
11364
        TEST_NE_P (cmd, NULL);
 
11365
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
11366
        TEST_EQ_STR (output[0],  expected_output);
 
11367
        TEST_EQ_STR (output[1],  "  emits bong");
 
11368
        TEST_EQ_STR (output[2],  "  emits bar");
 
11369
        TEST_EQ_STR (output[3],  "  emits stime");
 
11370
        TEST_EQ_STR (output[4],  "  start on starting (job: mountall, env:)");
 
11371
        TEST_EQ_STR (output[5],  "  start on runlevel (job:, env: [016])");
 
11372
        TEST_EQ_STR (output[6],  "  start on stopped (job: gdm, env:)");
 
11373
        TEST_EQ_STR (output[7],  "  start on stopped (job: kdm, env:)");
 
11374
        TEST_EQ_STR (output[8],  "  start on stopped (job: xdm, env: A=B)");
 
11375
        TEST_EQ_STR (output[9],  "  start on stopping (job: lxdm, env:)");
 
11376
        TEST_EQ_STR (output[10], "  stop on runlevel (job:, env: [!2345] colour=blue)");
 
11377
        TEST_EQ_STR (output[11], "  stop on starting (job: rocket, env:)");
 
11378
        TEST_EQ (lines, 12);
 
11379
        nih_free (output);
 
11380
 
 
11381
        DELETE_FILE (dirname, "foo.conf");
 
11382
 
 
11383
        /*******************************************************************/
 
11384
 
 
11385
        STOP_UPSTART (upstart_pid);
 
11386
        TEST_EQ (unsetenv ("UPSTART_CONFDIR"), 0);
 
11387
        TEST_DBUS_END (dbus_pid);
 
11388
}
 
11389
 
 
11390
void
 
11391
test_check_config (void)
 
11392
{
 
11393
        char             dirname[PATH_MAX];
 
11394
        nih_local char  *cmd;
 
11395
        pid_t            upstart_pid = 0;
 
11396
        pid_t            dbus_pid    = 0;
 
11397
        char           **output;
 
11398
        size_t           lines;
 
11399
 
 
11400
        TEST_GROUP ("check_config");
 
11401
 
 
11402
        TEST_FILENAME (dirname);
 
11403
        TEST_EQ (mkdir (dirname, 0755), 0);
 
11404
 
 
11405
        /* Use the "secret" interface */
 
11406
        TEST_EQ (setenv ("UPSTART_CONFDIR", dirname, 1), 0);
 
11407
 
 
11408
        TEST_DBUS (dbus_pid);
 
11409
        START_UPSTART (upstart_pid);
 
11410
 
 
11411
        /*******************************************************************/
 
11412
 
 
11413
        TEST_FEATURE ("no unreachable jobs/events (satisfied by job or event)");
 
11414
 
 
11415
        CREATE_FILE (dirname, "foo.conf",
 
11416
                        "start on (starting bar or wibble)");
 
11417
 
 
11418
        CREATE_FILE (dirname, "bar.conf",
 
11419
                        "task\n"
 
11420
                        "exec true");
 
11421
 
 
11422
        CREATE_FILE (dirname, "baz.conf",
 
11423
                        "emits wibble");
 
11424
 
 
11425
        cmd = nih_sprintf (NULL, "%s check-config 2>&1", INITCTL_BINARY);
 
11426
        TEST_NE_P (cmd, NULL);
 
11427
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
11428
        TEST_EQ (lines, 0);
 
11429
 
 
11430
        DELETE_FILE (dirname, "foo.conf");
 
11431
        DELETE_FILE (dirname, "bar.conf");
 
11432
        DELETE_FILE (dirname, "baz.conf");
 
11433
 
 
11434
        /*******************************************************************/
 
11435
 
 
11436
        TEST_FEATURE ("no unreachable jobs/events (satisfied by job)");
 
11437
 
 
11438
        CREATE_FILE (dirname, "foo.conf",
 
11439
                        "start on (starting bar or wibble)");
 
11440
 
 
11441
        CREATE_FILE (dirname, "bar.conf",
 
11442
                        "task\n"
 
11443
                        "exec true");
 
11444
 
 
11445
        cmd = nih_sprintf (NULL, "%s check-config 2>&1", INITCTL_BINARY);
 
11446
        TEST_NE_P (cmd, NULL);
 
11447
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
11448
        TEST_EQ (lines, 0);
 
11449
 
 
11450
        DELETE_FILE (dirname, "foo.conf");
 
11451
        DELETE_FILE (dirname, "bar.conf");
 
11452
 
 
11453
        /*******************************************************************/
 
11454
 
 
11455
        TEST_FEATURE ("no unreachable jobs/events (satisfied by event)");
 
11456
 
 
11457
        CREATE_FILE (dirname, "foo.conf",
 
11458
                        "start on (starting bar or wibble)");
 
11459
 
 
11460
        CREATE_FILE (dirname, "baz.conf",
 
11461
                        "emits wibble");
 
11462
 
 
11463
        cmd = nih_sprintf (NULL, "%s check-config 2>&1", INITCTL_BINARY);
 
11464
        TEST_NE_P (cmd, NULL);
 
11465
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
11466
        TEST_EQ (lines, 0);
 
11467
 
 
11468
        DELETE_FILE (dirname, "foo.conf");
 
11469
        DELETE_FILE (dirname, "baz.conf");
 
11470
 
 
11471
        /*******************************************************************/
 
11472
 
 
11473
        TEST_FEATURE ("unreachable event");
 
11474
 
 
11475
        CREATE_FILE (dirname, "foo.conf",
 
11476
                        "start on (starting bar and wibble)");
 
11477
 
 
11478
        CREATE_FILE (dirname, "bar.conf",
 
11479
                        "task\n"
 
11480
                        "exec true");
 
11481
 
 
11482
        cmd = nih_sprintf (NULL, "%s check-config 2>&1", INITCTL_BINARY);
 
11483
        TEST_NE_P (cmd, NULL);
 
11484
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
11485
        TEST_EQ_STR (output[0], "foo");
 
11486
        TEST_EQ_STR (output[1], "  start on: unknown event wibble");
 
11487
        TEST_EQ (lines, 2);
 
11488
 
 
11489
        DELETE_FILE (dirname, "foo.conf");
 
11490
        DELETE_FILE (dirname, "bar.conf");
 
11491
 
 
11492
        /*******************************************************************/
 
11493
 
 
11494
        TEST_FEATURE ("unreachable job");
 
11495
 
 
11496
        CREATE_FILE (dirname, "foo.conf",
 
11497
                        "start on (starting bar and wibble)");
 
11498
 
 
11499
        CREATE_FILE (dirname, "baz.conf",
 
11500
                        "emits wibble");
 
11501
 
 
11502
        cmd = nih_sprintf (NULL, "%s check-config 2>&1", INITCTL_BINARY);
 
11503
        TEST_NE_P (cmd, NULL);
 
11504
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
11505
        TEST_EQ_STR (output[0], "foo");
 
11506
        TEST_EQ_STR (output[1], "  start on: unknown job bar");
 
11507
        TEST_EQ (lines, 2);
 
11508
 
 
11509
        DELETE_FILE (dirname, "foo.conf");
 
11510
        DELETE_FILE (dirname, "baz.conf");
 
11511
 
 
11512
        /*******************************************************************/
 
11513
 
 
11514
        TEST_FEATURE ("unreachable event with forced ignore");
 
11515
 
 
11516
        CREATE_FILE (dirname, "foo.conf",
 
11517
                        "start on (starting bar and wibble)");
 
11518
 
 
11519
        CREATE_FILE (dirname, "bar.conf",
 
11520
                        "task\n"
 
11521
                        "exec true");
 
11522
 
 
11523
        cmd = nih_sprintf (NULL, "%s check-config --ignore-events=wibble 2>&1",
 
11524
                        INITCTL_BINARY);
 
11525
        TEST_NE_P (cmd, NULL);
 
11526
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
11527
        TEST_EQ (lines, 0);
 
11528
 
 
11529
        DELETE_FILE (dirname, "bar.conf");
 
11530
 
 
11531
        /*******************************************************************/
 
11532
 
 
11533
        TEST_FEATURE ("unreachable events with forced ignores");
 
11534
 
 
11535
        CREATE_FILE (dirname, "foo.conf",
 
11536
                        "start on (fred and wilma)");
 
11537
 
 
11538
        cmd = nih_sprintf (NULL, "%s check-config --ignore-events=wilma,foo,fred 2>&1",
 
11539
                        INITCTL_BINARY);
 
11540
        TEST_NE_P (cmd, NULL);
 
11541
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
11542
        TEST_EQ (lines, 0);
 
11543
 
 
11544
        DELETE_FILE (dirname, "foo.conf");
 
11545
 
 
11546
        /*******************************************************************/
 
11547
 
 
11548
        TEST_FEATURE ("satisfiable complex start on");
 
11549
 
 
11550
        /* Yes folks, it's the classic */
 
11551
        CREATE_FILE (dirname, "plymouth.conf",
 
11552
                        "start on (starting mountall\n"
 
11553
                        "      or (runlevel [016]\n"
 
11554
                        "          and (stopped gdm\n"
 
11555
                        "              or stopped kdm\n"
 
11556
                        "              or stopped xdm\n"
 
11557
                        "              or stopped lxdm)))");
 
11558
 
 
11559
        CREATE_FILE (dirname, "mountall.conf", "exec true");
 
11560
        CREATE_FILE (dirname, "gdm.conf"     , "exec true");
 
11561
 
 
11562
        cmd = nih_sprintf (NULL, "%s check-config --ignore-events=runlevel 2>&1",
 
11563
                        INITCTL_BINARY);
 
11564
        TEST_NE_P (cmd, NULL);
 
11565
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
11566
        TEST_EQ (lines, 0);
 
11567
 
 
11568
        DELETE_FILE (dirname, "plymouth.conf");
 
11569
        DELETE_FILE (dirname, "mountall.conf");
 
11570
        DELETE_FILE (dirname, "gdm.conf");
 
11571
 
 
11572
        /*******************************************************************/
 
11573
 
 
11574
        TEST_FEATURE ("unsatisfiable complex start on");
 
11575
 
 
11576
        CREATE_FILE (dirname, "plymouth.conf",
 
11577
                        "start on (starting mountall\n"
 
11578
                        "      or (runlevel [016]\n"
 
11579
                        "          and (stopped gdm\n"
 
11580
                        "              or stopped kdm\n"
 
11581
                        "              or stopped xdm\n"
 
11582
                        "              or stopped lxdm)))");
 
11583
 
 
11584
        CREATE_FILE (dirname, "mountall.conf", "exec true");
 
11585
 
 
11586
        cmd = nih_sprintf (NULL, "%s check-config --ignore-events=runlevel 2>&1",
 
11587
                        INITCTL_BINARY);
 
11588
        TEST_NE_P (cmd, NULL);
 
11589
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
11590
 
 
11591
        TEST_EQ_STR (output[0], "plymouth");
 
11592
        TEST_EQ_STR (output[1], "  start on: unknown job lxdm");
 
11593
        TEST_EQ_STR (output[2], "  start on: unknown job xdm");
 
11594
        TEST_EQ_STR (output[3], "  start on: unknown job kdm");
 
11595
        TEST_EQ_STR (output[4], "  start on: unknown job gdm");
 
11596
        TEST_EQ (lines, 5);
 
11597
 
 
11598
        DELETE_FILE (dirname, "plymouth.conf");
 
11599
        DELETE_FILE (dirname, "mountall.conf");
 
11600
 
 
11601
        /*******************************************************************/
 
11602
 
 
11603
        TEST_FEATURE ("satisfiable complex stop on");
 
11604
 
 
11605
        /* Yes folks, it's the classic */
 
11606
        CREATE_FILE (dirname, "plymouth.conf",
 
11607
                        "stop on (starting mountall\n"
 
11608
                        "      or (runlevel [016]\n"
 
11609
                        "          and (stopped gdm\n"
 
11610
                        "              or stopped kdm\n"
 
11611
                        "              or stopped xdm\n"
 
11612
                        "              or stopped lxdm)))");
 
11613
 
 
11614
        CREATE_FILE (dirname, "mountall.conf", "exec true");
 
11615
        CREATE_FILE (dirname, "gdm.conf"     , "exec true");
 
11616
 
 
11617
        cmd = nih_sprintf (NULL, "%s check-config --ignore-events=runlevel 2>&1",
 
11618
                        INITCTL_BINARY);
 
11619
        TEST_NE_P (cmd, NULL);
 
11620
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
11621
        TEST_EQ (lines, 0);
 
11622
 
 
11623
        DELETE_FILE (dirname, "plymouth.conf");
 
11624
        DELETE_FILE (dirname, "mountall.conf");
 
11625
        DELETE_FILE (dirname, "gdm.conf");
 
11626
 
 
11627
        /*******************************************************************/
 
11628
 
 
11629
        TEST_FEATURE ("unsatisfiable complex stop on");
 
11630
 
 
11631
        CREATE_FILE (dirname, "plymouth.conf",
 
11632
                        "stop on (starting mountall\n"
 
11633
                        "      or (runlevel [016]\n"
 
11634
                        "          and (stopped gdm\n"
 
11635
                        "              or stopped kdm\n"
 
11636
                        "              or stopped xdm\n"
 
11637
                        "              or stopped lxdm)))");
 
11638
 
 
11639
        CREATE_FILE (dirname, "mountall.conf", "exec true");
 
11640
 
 
11641
        cmd = nih_sprintf (NULL, "%s check-config --ignore-events=runlevel 2>&1",
 
11642
                        INITCTL_BINARY);
 
11643
        TEST_NE_P (cmd, NULL);
 
11644
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
11645
 
 
11646
        TEST_EQ_STR (output[0], "plymouth");
 
11647
        TEST_EQ_STR (output[1], "  stop on: unknown job lxdm");
 
11648
        TEST_EQ_STR (output[2], "  stop on: unknown job xdm");
 
11649
        TEST_EQ_STR (output[3], "  stop on: unknown job kdm");
 
11650
        TEST_EQ_STR (output[4], "  stop on: unknown job gdm");
 
11651
        TEST_EQ (lines, 5);
 
11652
 
 
11653
        DELETE_FILE (dirname, "plymouth.conf");
 
11654
        DELETE_FILE (dirname, "mountall.conf");
 
11655
 
 
11656
        /*******************************************************************/
 
11657
 
 
11658
        TEST_FEATURE ("unsatisfiable complex stop on, satisfiable complex start on");
 
11659
 
 
11660
        CREATE_FILE (dirname, "plymouth.conf",
 
11661
                        "stop on (starting mountall\n"
 
11662
                        "      or (runlevel [016]\n"
 
11663
                        "          and (stopped gdm\n"
 
11664
                        "              or stopped kdm\n"
 
11665
                        "              or stopped xdm\n"
 
11666
                        "              or stopped lxdm)))\n"
 
11667
                        "start on (stopping portmap\n"
 
11668
                        "         or (runlevel [06] or starting beano))\n");
 
11669
 
 
11670
        CREATE_FILE (dirname, "mountall.conf", "exec true");
 
11671
        CREATE_FILE (dirname, "portmap.conf", "exec true");
 
11672
        CREATE_FILE (dirname, "beano.conf", "exec true");
 
11673
 
 
11674
        cmd = nih_sprintf (NULL, "%s check-config --ignore-events=runlevel 2>&1",
 
11675
                        INITCTL_BINARY);
 
11676
        TEST_NE_P (cmd, NULL);
 
11677
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
11678
 
 
11679
        TEST_EQ_STR (output[0], "plymouth");
 
11680
        TEST_EQ_STR (output[1], "  stop on: unknown job lxdm");
 
11681
        TEST_EQ_STR (output[2], "  stop on: unknown job xdm");
 
11682
        TEST_EQ_STR (output[3], "  stop on: unknown job kdm");
 
11683
        TEST_EQ_STR (output[4], "  stop on: unknown job gdm");
 
11684
        TEST_EQ (lines, 5);
 
11685
 
 
11686
        DELETE_FILE (dirname, "plymouth.conf");
 
11687
        DELETE_FILE (dirname, "mountall.conf");
 
11688
        DELETE_FILE (dirname, "portmap.conf");
 
11689
        DELETE_FILE (dirname, "beano.conf");
 
11690
 
 
11691
        /*******************************************************************/
 
11692
 
 
11693
        TEST_FEATURE ("satisfiable complex start on, unsatisfiable complex stop on");
 
11694
 
 
11695
        CREATE_FILE (dirname, "plymouth.conf",
 
11696
                        "start on (starting mountall\n"
 
11697
                        "      or (hello\n"
 
11698
                        "          and (stopped gdm\n"
 
11699
                        "              or stopped kdm\n"
 
11700
                        "              or stopped xdm\n"
 
11701
                        "              or stopped lxdm)))\n"
 
11702
                        "stop on (stopping portmap\n"
 
11703
                        "         or (wibble or starting beano))\n");
 
11704
 
 
11705
        CREATE_FILE (dirname, "mountall.conf", "exec true");
 
11706
        CREATE_FILE (dirname, "portmap.conf",
 
11707
                        "exec true\n"
 
11708
                        "emits hello");
 
11709
        CREATE_FILE (dirname, "gdm.conf", "exec true");
 
11710
 
 
11711
        cmd = nih_sprintf (NULL, "%s check-config >&1",
 
11712
                        INITCTL_BINARY);
 
11713
        TEST_NE_P (cmd, NULL);
 
11714
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
11715
 
 
11716
        TEST_EQ_STR (output[0], "plymouth");
 
11717
        TEST_EQ_STR (output[1], "  stop on: unknown job beano");
 
11718
        TEST_EQ_STR (output[2], "  stop on: unknown event wibble");
 
11719
        TEST_EQ (lines, 3);
 
11720
 
 
11721
        DELETE_FILE (dirname, "plymouth.conf");
 
11722
        DELETE_FILE (dirname, "mountall.conf");
 
11723
        DELETE_FILE (dirname, "portmap.conf");
 
11724
        DELETE_FILE (dirname, "gdm.conf");
 
11725
 
 
11726
        /*******************************************************************/
 
11727
 
 
11728
        TEST_FEATURE ("unsatisfiable complex start on, unsatisfiable complex stop on");
 
11729
 
 
11730
        CREATE_FILE (dirname, "plymouth.conf",
 
11731
                        "start on (starting mountall\n"
 
11732
                        "      or (hello\n"
 
11733
                        "          and (stopped gdm\n"
 
11734
                        "              or stopped kdm\n"
 
11735
                        "              or stopped xdm\n"
 
11736
                        "              or stopped lxdm)))\n"
 
11737
                        "stop on (stopping portmap\n"
 
11738
                        "         or (wibble or starting beano))\n");
 
11739
 
 
11740
        CREATE_FILE (dirname, "mountall.conf", "exec true");
 
11741
        CREATE_FILE (dirname, "portmap.conf", "exec true");
 
11742
 
 
11743
        cmd = nih_sprintf (NULL, "%s check-config 2>&1",
 
11744
                        INITCTL_BINARY);
 
11745
        TEST_NE_P (cmd, NULL);
 
11746
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
11747
 
 
11748
        TEST_EQ_STR (output[0], "plymouth");
 
11749
        TEST_EQ_STR (output[1], "  start on: unknown job lxdm");
 
11750
        TEST_EQ_STR (output[2], "  start on: unknown job xdm");
 
11751
        TEST_EQ_STR (output[3], "  start on: unknown job kdm");
 
11752
        TEST_EQ_STR (output[4], "  start on: unknown job gdm");
 
11753
        TEST_EQ_STR (output[5], "  start on: unknown event hello");
 
11754
        TEST_EQ_STR (output[6], "  stop on: unknown job beano");
 
11755
        TEST_EQ_STR (output[7], "  stop on: unknown event wibble");
 
11756
        TEST_EQ (lines, 8);
 
11757
 
 
11758
        DELETE_FILE (dirname, "plymouth.conf");
 
11759
        DELETE_FILE (dirname, "mountall.conf");
 
11760
        DELETE_FILE (dirname, "portmap.conf");
 
11761
 
 
11762
        /*******************************************************************/
 
11763
 
 
11764
        TEST_FEATURE ("satisfiable complex start on, satisfiable complex stop on");
 
11765
 
 
11766
        CREATE_FILE (dirname, "plymouth.conf",
 
11767
                        "start on (starting mountall\n"
 
11768
                        "      or (hello\n"
 
11769
                        "          and (stopped gdm\n"
 
11770
                        "              or (stopped kdm\n"
 
11771
                        "              or (stopped xdm\n"
 
11772
                        "              or stopped lxdm)))))\n"
 
11773
                        "stop on (stopping portmap\n"
 
11774
                        "         or (wibble or starting beano))\n");
 
11775
 
 
11776
        CREATE_FILE (dirname, "mountall.conf", "exec true\n");
 
11777
        CREATE_FILE (dirname, "portmap.conf",
 
11778
                        "exec true\n"
 
11779
                        "emits hello");
 
11780
        CREATE_FILE (dirname, "lxdm.conf", "exec true");
 
11781
        CREATE_FILE (dirname, "wibble.conf", "emits wibble");
 
11782
        CREATE_FILE (dirname, "beano.conf", "exec true");
 
11783
 
 
11784
        cmd = nih_sprintf (NULL, "%s check-config 2>&1",
 
11785
                        INITCTL_BINARY);
 
11786
        TEST_NE_P (cmd, NULL);
 
11787
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
11788
 
 
11789
        TEST_EQ (lines, 0);
 
11790
 
 
11791
        DELETE_FILE (dirname, "plymouth.conf");
 
11792
        DELETE_FILE (dirname, "mountall.conf");
 
11793
        DELETE_FILE (dirname, "portmap.conf");
 
11794
        DELETE_FILE (dirname, "lxdm.conf");
 
11795
        DELETE_FILE (dirname, "beano.conf");
 
11796
        DELETE_FILE (dirname, "wibble.conf");
 
11797
 
 
11798
        /*******************************************************************/
 
11799
 
 
11800
        TEST_FEATURE (
 
11801
                "satisfiable complex start on, satisfiable complex stop on with warnings");
 
11802
 
 
11803
        CREATE_FILE (dirname, "plymouth.conf",
 
11804
                        "start on (starting mountall\n"
 
11805
                        "      or (hello\n"
 
11806
                        "          and (stopped gdm\n"
 
11807
                        "              or (stopped kdm\n"
 
11808
                        "              or (stopped xdm\n"
 
11809
                        "              or stopped lxdm)))))\n"
 
11810
                        "stop on (stopping portmap\n"
 
11811
                        "         or (wibble or starting beano))\n");
 
11812
 
 
11813
        CREATE_FILE (dirname, "mountall.conf", "exec true\n");
 
11814
        CREATE_FILE (dirname, "portmap.conf",
 
11815
                        "exec true\n"
 
11816
                        "emits hello");
 
11817
        CREATE_FILE (dirname, "lxdm.conf", "exec true");
 
11818
        CREATE_FILE (dirname, "wibble.conf", "emits wibble");
 
11819
        CREATE_FILE (dirname, "beano.conf", "exec true");
 
11820
 
 
11821
        cmd = nih_sprintf (NULL, "%s check-config --warn 2>&1",
 
11822
                        INITCTL_BINARY);
 
11823
        TEST_NE_P (cmd, NULL);
 
11824
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
11825
 
 
11826
        TEST_EQ_STR (output[0], "plymouth");
 
11827
        TEST_EQ_STR (output[1], "  start on: unknown job xdm");
 
11828
        TEST_EQ_STR (output[2], "  start on: unknown job kdm");
 
11829
        TEST_EQ_STR (output[3], "  start on: unknown job gdm");
 
11830
        TEST_EQ (lines, 4);
 
11831
 
 
11832
        DELETE_FILE (dirname, "plymouth.conf");
 
11833
        DELETE_FILE (dirname, "mountall.conf");
 
11834
        DELETE_FILE (dirname, "portmap.conf");
 
11835
        DELETE_FILE (dirname, "lxdm.conf");
 
11836
        DELETE_FILE (dirname, "beano.conf");
 
11837
        DELETE_FILE (dirname, "wibble.conf");
 
11838
 
 
11839
        /*******************************************************************/
 
11840
 
 
11841
        STOP_UPSTART (upstart_pid);
 
11842
        TEST_EQ (unsetenv ("UPSTART_CONFDIR"), 0);
 
11843
        TEST_DBUS_END (dbus_pid);
 
11844
}
 
11845
 
10733
11846
 
10734
11847
void
10735
11848
test_list_action (void)
10769
11882
                                        "NameAcquired"));
10770
11883
        dbus_message_unref (method_call);
10771
11884
 
10772
 
        system_bus = TRUE;
 
11885
        dbus_bus_type = DBUS_BUS_SYSTEM;
10773
11886
        dest_name = DBUS_SERVICE_UPSTART;
10774
11887
        dest_address = DBUS_ADDRESS_UPSTART;
10775
11888
 
12060
13173
                                        "NameAcquired"));
12061
13174
        dbus_message_unref (method_call);
12062
13175
 
12063
 
        system_bus = TRUE;
 
13176
        dbus_bus_type = DBUS_BUS_SYSTEM;
12064
13177
        dest_name = DBUS_SERVICE_UPSTART;
12065
13178
        dest_address = DBUS_ADDRESS_UPSTART;
12066
13179
 
12494
13607
                                        "NameAcquired"));
12495
13608
        dbus_message_unref (method_call);
12496
13609
 
12497
 
        system_bus = TRUE;
 
13610
        dbus_bus_type = DBUS_BUS_SYSTEM;
12498
13611
        dest_name = DBUS_SERVICE_UPSTART;
12499
13612
        dest_address = DBUS_ADDRESS_UPSTART;
12500
13613
 
12684
13797
                                        "NameAcquired"));
12685
13798
        dbus_message_unref (method_call);
12686
13799
 
12687
 
        system_bus = TRUE;
 
13800
        dbus_bus_type = DBUS_BUS_SYSTEM;
12688
13801
        dest_name = DBUS_SERVICE_UPSTART;
12689
13802
        dest_address = DBUS_ADDRESS_UPSTART;
12690
13803
 
12904
14017
                                        "NameAcquired"));
12905
14018
        dbus_message_unref (method_call);
12906
14019
 
12907
 
        system_bus = TRUE;
 
14020
        dbus_bus_type = DBUS_BUS_SYSTEM;
12908
14021
        dest_name = DBUS_SERVICE_UPSTART;
12909
14022
        dest_address = DBUS_ADDRESS_UPSTART;
12910
14023
 
13283
14396
}
13284
14397
 
13285
14398
 
 
14399
/**
 
14400
 * in_chroot:
 
14401
 *
 
14402
 * Determine if running inside a chroot environment.
 
14403
 *
 
14404
 * Failures are fatal.
 
14405
 *
 
14406
 * Returns TRUE if within a chroot, else FALSE.
 
14407
 **/
 
14408
int
 
14409
in_chroot (void)
 
14410
{
 
14411
        struct stat st;
 
14412
        int i;
 
14413
        char dir[] = "/";
 
14414
 
 
14415
        i = stat(dir, &st);
 
14416
            
 
14417
        if ( i != 0 ) { 
 
14418
                fprintf (stderr, "ERROR: cannot stat '%s'\n", dir);
 
14419
                exit (EXIT_FAILURE);
 
14420
        }
 
14421
 
 
14422
        if ( st.st_ino == 2 )
 
14423
                return FALSE;
 
14424
 
 
14425
        return TRUE;
 
14426
}
 
14427
 
 
14428
/**
 
14429
 * dbus_configured
 
14430
 *
 
14431
 * Determine if D-Bus has been configured (with dbus-uuidgen).
 
14432
 *
 
14433
 * Returns TRUE if D-Bus appears to have been configured,
 
14434
 * else FALSE.
 
14435
 **/
 
14436
int
 
14437
dbus_configured (void)
 
14438
{
 
14439
        struct stat st;
 
14440
        char path[] = "/var/lib/dbus/machine-id";
 
14441
 
 
14442
        return !stat (path, &st);
 
14443
}
 
14444
 
13286
14445
int
13287
14446
main (int   argc,
13288
14447
      char *argv[])
13308
14467
        test_version_action ();
13309
14468
        test_log_priority_action ();
13310
14469
 
 
14470
        if (in_chroot () && !dbus_configured ()) {
 
14471
                fprintf(stderr, "\n\n"
 
14472
                                "WARNING: not running show-config "
 
14473
                                "and check-config tests within chroot "
 
14474
                                "as no D-Bus, or D-Bus not configured (lp:#728988)"
 
14475
                                "\n\n");
 
14476
        } else {
 
14477
                test_show_config ();
 
14478
                test_check_config ();
 
14479
        }
 
14480
 
13311
14481
        return 0;
13312
14482
}