~ubuntu-branches/ubuntu/gutsy/upstart/gutsy

« back to all changes in this revision

Viewing changes to util/tests/test_jobs.c

  • Committer: Bazaar Package Importer
  • Author(s): Scott James Remnant
  • Date: 2007-03-11 19:19:00 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20070311191900-y59f23fd1c6t493g
Tags: 0.3.8-1
* New upstream release:
  - much improved initctl tool.

* Update my standard prep_/undo_/rm_conffile functions to take into account
  current dpkg behaviour wrt obsolete conffiles.  The conffile is now moved
  out of the way in preinst and the moved file deleted in postinst, or moved
  back in postrm abort-upgrade.  This means it's not there when dpkg
  configures the new version, so the conffile is not left in the list.
* Purge backups of modified obsolete conffiles when the package is purged.

* Update runlevel and respawn rule generated in migrate-inittab.pl
  LP: #89314

* Drop 00-libnih-update.patch and 01-libnih-sparc-ftbfs.patch; new upstream
  release includes an up-to-date libnih which contains both patches.
* Drop 10-cant-stop-execless-job.patch; included upstream.
* Drop 20-complex-event-config.patch; this is going to be significantly
  changed upstream, and we don't want to ship something strange.
* Drop 30-fix-warnings.patch; included upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* upstart
2
 
 *
3
 
 * test_jobs.c - test suite for util/jobs.c
4
 
 *
5
 
 * Copyright © 2007 Canonical Ltd.
6
 
 * Author: Scott James Remnant <scott@ubuntu.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 as published by
10
 
 * the Free Software Foundation; either version 2 of the License, or
11
 
 * (at your option) any later version.
12
 
 *
13
 
 * This program is distributed in the hope that it will be useful,
14
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 
 * GNU General Public License for more details.
17
 
 *
18
 
 * You should have received a copy of the GNU General Public License
19
 
 * along with this program; if not, write to the Free Software
20
 
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
21
 
 */
22
 
 
23
 
#include <nih/test.h>
24
 
 
25
 
#include <sys/types.h>
26
 
#include <sys/socket.h>
27
 
#include <sys/wait.h>
28
 
 
29
 
#include <stdio.h>
30
 
#include <stdlib.h>
31
 
 
32
 
#include <nih/macros.h>
33
 
#include <nih/alloc.h>
34
 
#include <nih/main.h>
35
 
#include <nih/io.h>
36
 
#include <nih/logging.h>
37
 
#include <nih/error.h>
38
 
#include <nih/errors.h>
39
 
 
40
 
#include <upstart/enum.h>
41
 
#include <upstart/message.h>
42
 
 
43
 
#include <util/jobs.h>
44
 
 
45
 
 
46
 
int control_sock = -1;
47
 
int destination_pid = 0;
48
 
 
49
 
extern int upstart_disable_safeties;
50
 
 
51
 
 
52
 
void
53
 
test_start_action (void)
54
 
{
55
 
        NihCommand    cmd;
56
 
        NihIoMessage *msg;
57
 
        size_t        len;
58
 
        FILE         *output;
59
 
        pid_t         pid;
60
 
        char         *args[3];
61
 
        int           ret, sock, status;
62
 
 
63
 
 
64
 
        TEST_FUNCTION ("start_action");
65
 
        program_name = "test";
66
 
 
67
 
        nih_error_push_context ();
68
 
        nih_error_pop_context ();
69
 
 
70
 
        output = tmpfile ();
71
 
 
72
 
        sock = upstart_open ();
73
 
        destination_pid = getpid ();
74
 
 
75
 
        setenv ("UPSTART_JOB", "oops", TRUE);
76
 
 
77
 
 
78
 
        /* Check that the start command sends the start job message to
79
 
         * the server, and then receives a single status reply and outputs it.
80
 
         */
81
 
        TEST_FEATURE ("with start command");
82
 
        cmd.command = "start";
83
 
        args[0] = "foo";
84
 
        args[1] = NULL;
85
 
 
86
 
        TEST_CHILD (pid) {
87
 
                TEST_DIVERT_STDOUT (output) {
88
 
                        upstart_disable_safeties = TRUE;
89
 
 
90
 
                        control_sock = upstart_open ();
91
 
                        ret = start_action (&cmd, args);
92
 
                        exit (ret);
93
 
                }
94
 
        }
95
 
 
96
 
        /* Should receive UPSTART_JOB_START */
97
 
        assert (msg = nih_io_message_recv (NULL, sock, &len));
98
 
 
99
 
        TEST_EQ (msg->data->len, 20);
100
 
        TEST_EQ_MEM (msg->data->buf, "upstart\n\0\0\0\01s\0\0\0\03foo", 20);
101
 
 
102
 
        nih_free (msg);
103
 
 
104
 
 
105
 
        /* Send back the status */
106
 
        msg = upstart_message_new (NULL, pid, UPSTART_JOB_STATUS, "foo",
107
 
                                   JOB_START, JOB_RUNNING, 1000);
108
 
        assert (nih_io_message_send (msg, sock) > 0);
109
 
        nih_free (msg);
110
 
 
111
 
        msg = upstart_message_new (NULL, pid, UPSTART_JOB_LIST_END);
112
 
        assert (nih_io_message_send (msg, sock) > 0);
113
 
        nih_free (msg);
114
 
 
115
 
        /* Reap the child, check the output */
116
 
        waitpid (pid, &status, 0);
117
 
        rewind (output);
118
 
 
119
 
        TEST_TRUE (WIFEXITED (status));
120
 
        TEST_EQ (WEXITSTATUS (status), 0);
121
 
 
122
 
        TEST_FILE_EQ (output, "foo (start) running, process 1000\n");
123
 
        TEST_FILE_END (output);
124
 
        TEST_FILE_RESET (output);
125
 
 
126
 
 
127
 
        /* Check that the stop command sends the stop job message to
128
 
         * the server, and then receives a single status reply and outputs it.
129
 
         */
130
 
        TEST_FEATURE ("with stop command");
131
 
        cmd.command = "stop";
132
 
        args[0] = "foo";
133
 
        args[1] = NULL;
134
 
 
135
 
        TEST_CHILD (pid) {
136
 
                TEST_DIVERT_STDOUT (output) {
137
 
                        upstart_disable_safeties = TRUE;
138
 
 
139
 
                        control_sock = upstart_open ();
140
 
                        ret = start_action (&cmd, args);
141
 
                        exit (ret);
142
 
                }
143
 
        }
144
 
 
145
 
        /* Should receive UPSTART_JOB_STOP */
146
 
        assert (msg = nih_io_message_recv (NULL, sock, &len));
147
 
 
148
 
        TEST_EQ (msg->data->len, 20);
149
 
        TEST_EQ_MEM (msg->data->buf, "upstart\n\0\0\0\02s\0\0\0\03foo", 20);
150
 
 
151
 
        nih_free (msg);
152
 
 
153
 
 
154
 
        /* Send back the status */
155
 
        msg = upstart_message_new (NULL, pid, UPSTART_JOB_STATUS, "foo",
156
 
                                   JOB_STOP, JOB_KILLED, 1000);
157
 
        assert (nih_io_message_send (msg, sock) > 0);
158
 
        nih_free (msg);
159
 
 
160
 
        msg = upstart_message_new (NULL, pid, UPSTART_JOB_LIST_END);
161
 
        assert (nih_io_message_send (msg, sock) > 0);
162
 
        nih_free (msg);
163
 
 
164
 
        /* Reap the child, check the output */
165
 
        waitpid (pid, &status, 0);
166
 
        rewind (output);
167
 
 
168
 
        TEST_TRUE (WIFEXITED (status));
169
 
        TEST_EQ (WEXITSTATUS (status), 0);
170
 
 
171
 
        TEST_FILE_EQ (output, "foo (stop) killed, process 1000\n");
172
 
        TEST_FILE_END (output);
173
 
        TEST_FILE_RESET (output);
174
 
 
175
 
 
176
 
        /* Check that the status command sends the query job message to
177
 
         * the server, and then receives a single status reply and outputs it.
178
 
         */
179
 
        TEST_FEATURE ("with status command");
180
 
        cmd.command = "status";
181
 
        args[0] = "foo";
182
 
        args[1] = NULL;
183
 
 
184
 
        TEST_CHILD (pid) {
185
 
                TEST_DIVERT_STDOUT (output) {
186
 
                        upstart_disable_safeties = TRUE;
187
 
 
188
 
                        control_sock = upstart_open ();
189
 
                        ret = start_action (&cmd, args);
190
 
                        exit (ret);
191
 
                }
192
 
        }
193
 
 
194
 
        /* Should receive UPSTART_JOB_QUERY */
195
 
        assert (msg = nih_io_message_recv (NULL, sock, &len));
196
 
 
197
 
        TEST_EQ (msg->data->len, 20);
198
 
        TEST_EQ_MEM (msg->data->buf, "upstart\n\0\0\0\03s\0\0\0\03foo", 20);
199
 
 
200
 
        nih_free (msg);
201
 
 
202
 
 
203
 
        /* Send back the status */
204
 
        msg = upstart_message_new (NULL, pid, UPSTART_JOB_STATUS, "foo",
205
 
                                   JOB_STOP, JOB_WAITING, 0);
206
 
        assert (nih_io_message_send (msg, sock) > 0);
207
 
        nih_free (msg);
208
 
 
209
 
        msg = upstart_message_new (NULL, pid, UPSTART_JOB_LIST_END);
210
 
        assert (nih_io_message_send (msg, sock) > 0);
211
 
        nih_free (msg);
212
 
 
213
 
        /* Reap the child, check the output */
214
 
        waitpid (pid, &status, 0);
215
 
        rewind (output);
216
 
 
217
 
        TEST_TRUE (WIFEXITED (status));
218
 
        TEST_EQ (WEXITSTATUS (status), 0);
219
 
 
220
 
        TEST_FILE_EQ (output, "foo (stop) waiting\n");
221
 
        TEST_FILE_END (output);
222
 
        TEST_FILE_RESET (output);
223
 
 
224
 
 
225
 
        /* Check that the command accepts multiple named jobs and sends
226
 
         * and receives a message for each one.
227
 
         */
228
 
        TEST_FEATURE ("with status command");
229
 
        cmd.command = "status";
230
 
        args[0] = "foo";
231
 
        args[1] = "bar";
232
 
        args[2] = NULL;
233
 
 
234
 
        TEST_CHILD (pid) {
235
 
                TEST_DIVERT_STDOUT (output) {
236
 
                        upstart_disable_safeties = TRUE;
237
 
 
238
 
                        control_sock = upstart_open ();
239
 
                        ret = start_action (&cmd, args);
240
 
                        exit (ret);
241
 
                }
242
 
        }
243
 
 
244
 
        /* Should receive UPSTART_JOB_QUERY */
245
 
        assert (msg = nih_io_message_recv (NULL, sock, &len));
246
 
 
247
 
        TEST_EQ (msg->data->len, 20);
248
 
        TEST_EQ_MEM (msg->data->buf, "upstart\n\0\0\0\03s\0\0\0\03foo", 20);
249
 
 
250
 
        nih_free (msg);
251
 
 
252
 
        /* Send back the status */
253
 
        msg = upstart_message_new (NULL, pid, UPSTART_JOB_STATUS, "foo",
254
 
                                   JOB_STOP, JOB_PRE_STOP, 1000);
255
 
        assert (nih_io_message_send (msg, sock) > 0);
256
 
        nih_free (msg);
257
 
 
258
 
        msg = upstart_message_new (NULL, pid, UPSTART_JOB_LIST_END);
259
 
        assert (nih_io_message_send (msg, sock) > 0);
260
 
        nih_free (msg);
261
 
 
262
 
        /* Should receive another UPSTART_JOB_QUERY */
263
 
        assert (msg = nih_io_message_recv (NULL, sock, &len));
264
 
 
265
 
        TEST_EQ (msg->data->len, 20);
266
 
        TEST_EQ_MEM (msg->data->buf, "upstart\n\0\0\0\03s\0\0\0\03bar", 20);
267
 
 
268
 
        nih_free (msg);
269
 
 
270
 
        /* Send back the status */
271
 
        msg = upstart_message_new (NULL, pid, UPSTART_JOB_STATUS, "bar",
272
 
                                   JOB_START, JOB_SPAWNED, 2000);
273
 
        assert (nih_io_message_send (msg, sock) > 0);
274
 
        nih_free (msg);
275
 
 
276
 
        msg = upstart_message_new (NULL, pid, UPSTART_JOB_LIST_END);
277
 
        assert (nih_io_message_send (msg, sock) > 0);
278
 
        nih_free (msg);
279
 
 
280
 
        /* Reap the child, check the output */
281
 
        waitpid (pid, &status, 0);
282
 
        rewind (output);
283
 
 
284
 
        TEST_TRUE (WIFEXITED (status));
285
 
        TEST_EQ (WEXITSTATUS (status), 0);
286
 
 
287
 
        TEST_FILE_EQ (output, "foo (stop) pre-stop, process 1000\n");
288
 
        TEST_FILE_EQ (output, "bar (start) spawned, process 2000\n");
289
 
        TEST_FILE_END (output);
290
 
        TEST_FILE_RESET (output);
291
 
 
292
 
 
293
 
        /* Check that if the command returns multiple replies, they are all
294
 
         * output.
295
 
         */
296
 
        TEST_FEATURE ("with multiple replies");
297
 
        cmd.command = "status";
298
 
        args[0] = "foo";
299
 
        args[1] = NULL;
300
 
 
301
 
        TEST_CHILD (pid) {
302
 
                TEST_DIVERT_STDOUT (output) {
303
 
                        upstart_disable_safeties = TRUE;
304
 
 
305
 
                        control_sock = upstart_open ();
306
 
                        ret = start_action (&cmd, args);
307
 
                        exit (ret);
308
 
                }
309
 
        }
310
 
 
311
 
        /* Should receive UPSTART_JOB_QUERY */
312
 
        assert (msg = nih_io_message_recv (NULL, sock, &len));
313
 
 
314
 
        TEST_EQ (msg->data->len, 20);
315
 
        TEST_EQ_MEM (msg->data->buf, "upstart\n\0\0\0\03s\0\0\0\03foo", 20);
316
 
 
317
 
        nih_free (msg);
318
 
 
319
 
        /* Send back the status */
320
 
        msg = upstart_message_new (NULL, pid, UPSTART_JOB_STATUS, "foo",
321
 
                                   JOB_STOP, JOB_WAITING, 0);
322
 
        assert (nih_io_message_send (msg, sock) > 0);
323
 
        nih_free (msg);
324
 
 
325
 
        msg = upstart_message_new (NULL, pid, UPSTART_JOB_STATUS, "foo",
326
 
                                   JOB_START, JOB_PRE_START, 1000);
327
 
        assert (nih_io_message_send (msg, sock) > 0);
328
 
        nih_free (msg);
329
 
 
330
 
        msg = upstart_message_new (NULL, pid, UPSTART_JOB_STATUS, "foo",
331
 
                                   JOB_STOP, JOB_PRE_STOP, 2000);
332
 
        assert (nih_io_message_send (msg, sock) > 0);
333
 
        nih_free (msg);
334
 
 
335
 
        msg = upstart_message_new (NULL, pid, UPSTART_JOB_LIST_END);
336
 
        assert (nih_io_message_send (msg, sock) > 0);
337
 
        nih_free (msg);
338
 
 
339
 
        /* Reap the child, check the output */
340
 
        waitpid (pid, &status, 0);
341
 
        rewind (output);
342
 
 
343
 
        TEST_TRUE (WIFEXITED (status));
344
 
        TEST_EQ (WEXITSTATUS (status), 0);
345
 
 
346
 
        TEST_FILE_EQ (output, "foo (stop) waiting\n");
347
 
        TEST_FILE_EQ (output, "foo (start) pre-start, process 1000\n");
348
 
        TEST_FILE_EQ (output, "foo (stop) pre-stop, process 2000\n");
349
 
        TEST_FILE_END (output);
350
 
        TEST_FILE_RESET (output);
351
 
 
352
 
 
353
 
        /* Check that the command can respond to an unknown job message
354
 
         * and output it properly.
355
 
         */
356
 
        TEST_FEATURE ("with unknown job");
357
 
        cmd.command = "start";
358
 
        args[0] = "foo";
359
 
        args[1] = NULL;
360
 
 
361
 
        TEST_CHILD (pid) {
362
 
                TEST_DIVERT_STDERR (output) {
363
 
                        upstart_disable_safeties = TRUE;
364
 
 
365
 
                        control_sock = upstart_open ();
366
 
                        ret = start_action (&cmd, args);
367
 
                        exit (ret);
368
 
                }
369
 
        }
370
 
 
371
 
        /* Should receive UPSTART_JOB_START */
372
 
        assert (msg = nih_io_message_recv (NULL, sock, &len));
373
 
 
374
 
        TEST_EQ (msg->data->len, 20);
375
 
        TEST_EQ_MEM (msg->data->buf, "upstart\n\0\0\0\01s\0\0\0\03foo", 20);
376
 
 
377
 
        nih_free (msg);
378
 
 
379
 
        /* Send back unknown job */
380
 
        msg = upstart_message_new (NULL, pid, UPSTART_JOB_UNKNOWN, "foo");
381
 
        assert (nih_io_message_send (msg, sock) > 0);
382
 
        nih_free (msg);
383
 
 
384
 
        /* Reap the child, check the output */
385
 
        waitpid (pid, &status, 0);
386
 
        rewind (output);
387
 
 
388
 
        TEST_TRUE (WIFEXITED (status));
389
 
        TEST_EQ (WEXITSTATUS (status), 0);
390
 
 
391
 
        TEST_FILE_EQ (output, "test: unknown job: foo\n");
392
 
        TEST_FILE_END (output);
393
 
        TEST_FILE_RESET (output);
394
 
 
395
 
 
396
 
        /* Check that calling start without any argument results in an error
397
 
         * being sent to stderr.
398
 
         */
399
 
        TEST_FEATURE ("with missing argument");
400
 
        unsetenv ("UPSTART_JOB");
401
 
        cmd.command = "start";
402
 
        args[0] = NULL;
403
 
        TEST_DIVERT_STDERR (output) {
404
 
                ret = start_action (&cmd, args);
405
 
        }
406
 
        rewind (output);
407
 
 
408
 
        TEST_NE (ret, 0);
409
 
 
410
 
        TEST_FILE_EQ (output, "test: missing job name\n");
411
 
        TEST_FILE_EQ (output, "Try `test --help' for more information.\n");
412
 
        TEST_FILE_END (output);
413
 
        TEST_FILE_RESET (output);
414
 
 
415
 
 
416
 
        /* Check that it's ok to call start without any arguments if the
417
 
         * UPSTART_JOB environment variable is set, as that variable can
418
 
         * be used instead.
419
 
         */
420
 
        TEST_FEATURE ("with UPSTART_JOB in environment");
421
 
        setenv ("UPSTART_JOB", "galen", TRUE);
422
 
 
423
 
        TEST_CHILD (pid) {
424
 
                TEST_DIVERT_STDOUT (output) {
425
 
                        upstart_disable_safeties = TRUE;
426
 
 
427
 
                        control_sock = upstart_open ();
428
 
                        ret = start_action (&cmd, args);
429
 
                        exit (ret);
430
 
                }
431
 
        }
432
 
 
433
 
        /* Should receive UPSTART_JOB_START */
434
 
        assert (msg = nih_io_message_recv (NULL, sock, &len));
435
 
 
436
 
        TEST_EQ (msg->data->len, 22);
437
 
        TEST_EQ_MEM (msg->data->buf, "upstart\n\0\0\0\01s\0\0\0\05galen", 22);
438
 
 
439
 
        nih_free (msg);
440
 
 
441
 
 
442
 
        /* Send back the status */
443
 
        msg = upstart_message_new (NULL, pid, UPSTART_JOB_STATUS, "galen",
444
 
                                   JOB_START, JOB_RUNNING, 1000);
445
 
        assert (nih_io_message_send (msg, sock) > 0);
446
 
        nih_free (msg);
447
 
 
448
 
        msg = upstart_message_new (NULL, pid, UPSTART_JOB_LIST_END);
449
 
        assert (nih_io_message_send (msg, sock) > 0);
450
 
        nih_free (msg);
451
 
 
452
 
        /* Reap the child, check the output */
453
 
        waitpid (pid, &status, 0);
454
 
        rewind (output);
455
 
 
456
 
        TEST_TRUE (WIFEXITED (status));
457
 
        TEST_EQ (WEXITSTATUS (status), 0);
458
 
 
459
 
        TEST_FILE_EQ (output, "galen (start) running, process 1000\n");
460
 
        TEST_FILE_END (output);
461
 
        TEST_FILE_RESET (output);
462
 
 
463
 
 
464
 
        fclose (output);
465
 
 
466
 
        close (sock);
467
 
}
468
 
 
469
 
void
470
 
test_list_action (void)
471
 
{
472
 
        NihCommand    cmd;
473
 
        NihIoMessage *msg;
474
 
        size_t        len;
475
 
        FILE         *output;
476
 
        pid_t         pid;
477
 
        char         *args[3];
478
 
        int           ret, sock, status;
479
 
 
480
 
 
481
 
        /* Check that the list command sends the list jobs message to
482
 
         * the server, and then receives all replies and outputs each job
483
 
         * one per-line.
484
 
         */
485
 
        TEST_FUNCTION ("list_action");
486
 
        program_name = "test";
487
 
 
488
 
        nih_error_push_context ();
489
 
        nih_error_pop_context ();
490
 
 
491
 
        output = tmpfile ();
492
 
 
493
 
        sock = upstart_open ();
494
 
        destination_pid = getpid ();
495
 
 
496
 
        cmd.command = "list";
497
 
        args[0] = "foo";
498
 
        args[1] = NULL;
499
 
 
500
 
        TEST_CHILD (pid) {
501
 
                TEST_DIVERT_STDOUT (output) {
502
 
                        upstart_disable_safeties = TRUE;
503
 
 
504
 
                        control_sock = upstart_open ();
505
 
                        ret = list_action (&cmd, args);
506
 
                        exit (ret);
507
 
                }
508
 
        }
509
 
 
510
 
        /* Should receive UPSTART_JOB_LIST */
511
 
        assert (msg = nih_io_message_recv (NULL, sock, &len));
512
 
 
513
 
        TEST_EQ (msg->data->len, 12);
514
 
        TEST_EQ_MEM (msg->data->buf, "upstart\n\0\0\0\06", 12);
515
 
 
516
 
        nih_free (msg);
517
 
 
518
 
 
519
 
        /* Send back a couple of jobs */
520
 
        msg = upstart_message_new (NULL, pid, UPSTART_JOB_STATUS, "frodo",
521
 
                                   JOB_START, JOB_RUNNING, 1000);
522
 
        assert (nih_io_message_send (msg, sock) > 0);
523
 
        nih_free (msg);
524
 
 
525
 
        msg = upstart_message_new (NULL, pid, UPSTART_JOB_STATUS, "bilbo",
526
 
                                   JOB_STOP, JOB_KILLED, 2000);
527
 
        assert (nih_io_message_send (msg, sock) > 0);
528
 
        nih_free (msg);
529
 
 
530
 
        msg = upstart_message_new (NULL, pid, UPSTART_JOB_STATUS, "merry",
531
 
                                   JOB_STOP, JOB_STOPPING, 3000);
532
 
        assert (nih_io_message_send (msg, sock) > 0);
533
 
        nih_free (msg);
534
 
 
535
 
        msg = upstart_message_new (NULL, pid, UPSTART_JOB_STATUS, "pippin",
536
 
                                   JOB_STOP, JOB_WAITING, 0);
537
 
        assert (nih_io_message_send (msg, sock) > 0);
538
 
        nih_free (msg);
539
 
 
540
 
        msg = upstart_message_new (NULL, pid, UPSTART_JOB_LIST_END);
541
 
        assert (nih_io_message_send (msg, sock) > 0);
542
 
        nih_free (msg);
543
 
 
544
 
        /* Reap the child, check the output */
545
 
        waitpid (pid, &status, 0);
546
 
        rewind (output);
547
 
 
548
 
        TEST_TRUE (WIFEXITED (status));
549
 
        TEST_EQ (WEXITSTATUS (status), 0);
550
 
 
551
 
        TEST_FILE_EQ (output, "frodo (start) running, process 1000\n");
552
 
        TEST_FILE_EQ (output, "bilbo (stop) killed, process 2000\n");
553
 
        TEST_FILE_EQ (output, "merry (stop) stopping, process 3000\n");
554
 
        TEST_FILE_EQ (output, "pippin (stop) waiting\n");
555
 
        TEST_FILE_END (output);
556
 
        TEST_FILE_RESET (output);
557
 
 
558
 
 
559
 
        fclose (output);
560
 
 
561
 
        close (sock);
562
 
}
563
 
 
564
 
 
565
 
void
566
 
term_handler (int signum)
567
 
{
568
 
        exit (0);
569
 
}
570
 
 
571
 
void
572
 
test_jobs_action (void)
573
 
{
574
 
        NihCommand    cmd;
575
 
        NihIoMessage *msg;
576
 
        size_t        len;
577
 
        FILE         *output;
578
 
        pid_t         pid;
579
 
        char         *args[3];
580
 
        int           ret, sock, status;
581
 
 
582
 
 
583
 
        /* Check that the jobs command sends the watch jobs message to
584
 
         * the server, and then receives all replies and outputs each job
585
 
         * one per-line.
586
 
         */
587
 
        TEST_FUNCTION ("jobs_action");
588
 
        program_name = "test";
589
 
 
590
 
        nih_error_push_context ();
591
 
        nih_error_pop_context ();
592
 
 
593
 
        output = tmpfile ();
594
 
 
595
 
        sock = upstart_open ();
596
 
        destination_pid = getpid ();
597
 
 
598
 
        cmd.command = "jobs";
599
 
        args[0] = "foo";
600
 
        args[1] = NULL;
601
 
 
602
 
        TEST_CHILD (pid) {
603
 
                TEST_DIVERT_STDOUT (output) {
604
 
                        signal (SIGTERM, term_handler);
605
 
                        upstart_disable_safeties = TRUE;
606
 
 
607
 
                        control_sock = upstart_open ();
608
 
                        ret = jobs_action (&cmd, args);
609
 
                        exit (ret);
610
 
                }
611
 
        }
612
 
 
613
 
        /* Should receive UPSTART_WATCH_JOBS */
614
 
        assert (msg = nih_io_message_recv (NULL, sock, &len));
615
 
 
616
 
        TEST_EQ (msg->data->len, 12);
617
 
        TEST_EQ_MEM (msg->data->buf, "upstart\n\0\0\0\xa", 12);
618
 
 
619
 
        nih_free (msg);
620
 
 
621
 
 
622
 
        /* Send back a couple of jobs */
623
 
        msg = upstart_message_new (NULL, pid, UPSTART_JOB_STATUS, "frodo",
624
 
                                   JOB_START, JOB_RUNNING, 1000);
625
 
        assert (nih_io_message_send (msg, sock) > 0);
626
 
        nih_free (msg);
627
 
 
628
 
        msg = upstart_message_new (NULL, pid, UPSTART_JOB_STATUS, "frodo",
629
 
                                   JOB_STOP, JOB_KILLED, 2000);
630
 
        assert (nih_io_message_send (msg, sock) > 0);
631
 
        nih_free (msg);
632
 
 
633
 
        msg = upstart_message_new (NULL, pid, UPSTART_JOB_STATUS, "frodo",
634
 
                                   JOB_STOP, JOB_STOPPING, 3000);
635
 
        assert (nih_io_message_send (msg, sock) > 0);
636
 
        nih_free (msg);
637
 
 
638
 
        msg = upstart_message_new (NULL, pid, UPSTART_JOB_STATUS, "frodo",
639
 
                                   JOB_STOP, JOB_WAITING, 0);
640
 
        assert (nih_io_message_send (msg, sock) > 0);
641
 
        nih_free (msg);
642
 
 
643
 
 
644
 
        /* Meh; no real way to know ... */
645
 
        usleep (500000);
646
 
        kill (pid, SIGTERM);
647
 
 
648
 
 
649
 
        /* Reap the child, check the output */
650
 
        waitpid (pid, &status, 0);
651
 
        rewind (output);
652
 
 
653
 
        TEST_TRUE (WIFEXITED (status));
654
 
        TEST_EQ (WEXITSTATUS (status), 0);
655
 
 
656
 
        TEST_FILE_EQ (output, "frodo (start) running, process 1000\n");
657
 
        TEST_FILE_EQ (output, "frodo (stop) killed, process 2000\n");
658
 
        TEST_FILE_EQ (output, "frodo (stop) stopping, process 3000\n");
659
 
        TEST_FILE_EQ (output, "frodo (stop) waiting\n");
660
 
        TEST_FILE_END (output);
661
 
        TEST_FILE_RESET (output);
662
 
 
663
 
 
664
 
        fclose (output);
665
 
 
666
 
        close (sock);
667
 
}
668
 
 
669
 
 
670
 
int
671
 
main (int   argc,
672
 
      char *argv[])
673
 
{
674
 
        test_start_action ();
675
 
        test_list_action ();
676
 
        test_jobs_action ();
677
 
 
678
 
        return 0;
679
 
}