125
125
TEST_EQ (ok, TRUE); \
130
* Register regular child handler.
131
* Register another handler to be called after the primary
132
* Upstart handler to allow the test to exit the main loop
133
* quickly on success.
134
* Process the event queue each time through the main loop
137
#define TEST_INSTALL_CHILD_HANDLERS() \
138
NIH_MUST (nih_child_add_watch (NULL, \
141
job_process_handler, \
143
NIH_MUST (nih_child_add_watch (NULL, \
146
test_job_process_handler,\
148
NIH_MUST (nih_main_loop_add_func (NULL, (NihMainLoopCb)event_poll, \
128
152
/* Sadly we can't test everything that job_process_spawn() does simply because
129
153
* a lot of it can only be done by root, or in the case of the console stuff,
130
154
* kills whatever had /dev/console (usually X).
150
174
static int get_available_pty_count (void) __attribute__((unused));
151
175
static void close_all_files (void);
153
static int child_exit_status;
177
static int child_exit_status;
178
static pid_t child_watch_pid;
156
181
* test_job_process_handler:
1084
1111
nih_free (class);
1087
NIH_MUST (nih_child_add_watch (NULL,
1090
job_process_handler,
1092
/* Register another handler to be called after the primary
1093
* Upstart handler to allow the test to exit the main loop
1094
* quickly on success.
1096
NIH_MUST (nih_child_add_watch (NULL,
1099
test_job_process_handler,
1101
/* Process the event queue each time through the main loop */
1102
NIH_MUST (nih_main_loop_add_func (NULL, (NihMainLoopCb)event_poll,
1106
1115
/* Check that if we try and run a command that doesn't exist,
1107
1116
* job_process_start() raises a ProcessError and the command doesn't
1132
1144
TEST_DIVERT_STDERR (output) {
1133
1145
job_process_start (job, PROCESS_MAIN);
1134
pid = job->pid[PROCESS_MAIN];
1146
child_watch_pid = job->pid[PROCESS_MAIN];
1147
TEST_GT (child_watch_pid, 0);
1136
1148
TEST_EQ (nih_main_loop (), 0);
1137
1149
TEST_EQ (child_exit_status, 255);
3100
3117
child_exit_status = 0;
3101
3118
TEST_DIVERT_STDERR (output) {
3102
3119
job_process_start (job, PROCESS_MAIN);
3103
pid = job->pid[PROCESS_MAIN];
3120
child_watch_pid = job->pid[PROCESS_MAIN];
3121
TEST_GT (child_watch_pid, 0);
3105
3122
TEST_EQ (nih_main_loop (), 0);
3106
3123
TEST_EQ (child_exit_status, 255);
3117
3134
TEST_EQ (errno, ENOENT);
3119
3136
nih_free (class);
3137
TEST_RESET_MAIN_LOOP ();
3121
3139
/************************************************************/
3122
3140
TEST_FEATURE ("with single-line command running an invalid command, then a 1-line post-stop script");
3123
3141
TEST_HASH_EMPTY (job_classes);
3143
TEST_INSTALL_CHILD_HANDLERS ();
3125
3145
class = job_class_new (NULL, "asterix", NULL);
3126
3146
TEST_NE_P (class, NULL);
3160
3180
child_exit_status = 0;
3161
3181
job_process_start (job, PROCESS_MAIN);
3182
child_watch_pid = job->pid[PROCESS_MAIN];
3183
TEST_GT (child_watch_pid, 0);
3162
3184
TEST_EQ (nih_main_loop (), 0);
3163
3185
TEST_EQ (child_exit_status, 255);
3171
3193
job->goal = JOB_STOP;
3172
3194
job->state = JOB_POST_STOP;
3196
child_exit_status = 0;
3174
3197
job_process_start (job, PROCESS_POST_STOP);
3176
TEST_NE (job->pid[PROCESS_POST_STOP], 0);
3177
TEST_WATCH_UPDATE ();
3179
/* Flush the io so that the shell on the client side
3180
* gets the data (the script to execute).
3182
TEST_WATCH_UPDATE ();
3184
waitpid (job->pid[PROCESS_POST_STOP], &status, 0);
3185
TEST_TRUE (WIFEXITED (status));
3186
TEST_EQ (WEXITSTATUS (status), 0);
3188
TEST_WATCH_UPDATE ();
3198
child_watch_pid = job->pid[PROCESS_POST_STOP];
3199
TEST_GT (child_watch_pid, 0);
3200
TEST_EQ (nih_main_loop (), 0);
3201
TEST_EQ (child_exit_status, 0);
3190
3203
/* .. but the post stop should have written data */
3191
3204
TEST_EQ (stat (filename, &statbuf), 0);
3194
3206
fclose (output);
3210
3222
TEST_FEATURE ("with single-line command running an invalid command, then a 2-line post-stop script");
3211
3223
TEST_HASH_EMPTY (job_classes);
3225
TEST_RESET_MAIN_LOOP ();
3226
TEST_INSTALL_CHILD_HANDLERS ();
3213
3228
class = job_class_new (NULL, "asterix", NULL);
3214
3229
TEST_NE_P (class, NULL);
3247
3262
child_exit_status = 0;
3248
3263
job_process_start (job, PROCESS_MAIN);
3264
child_watch_pid = job->pid[PROCESS_MAIN];
3265
TEST_NE (child_watch_pid, 0);
3249
3267
TEST_EQ (nih_main_loop (), 0);
3250
TEST_NE (child_exit_status, 0);
3268
TEST_EQ (child_exit_status, 255);
3252
3270
/* We don't expect a logfile to be written since there is no
3253
3271
* accompanying shell to write the error.
3261
3279
child_exit_status = 0;
3262
3280
job_process_start (job, PROCESS_POST_STOP);
3263
TEST_NE (job->pid[PROCESS_POST_STOP], 0);
3264
nih_message("pid %i", job->pid[PROCESS_POST_STOP]);
3281
child_watch_pid = job->pid[PROCESS_POST_STOP];
3282
TEST_NE (child_watch_pid, 0);
3266
3284
TEST_EQ (nih_main_loop (), 0);
3267
TEST_TRUE (WIFEXITED (status));
3268
TEST_EQ (WEXITSTATUS(child_exit_status), 0);
3271
//FIXME the above printed pid just hangs with
3272
//descriptors 9 and 10 open to pipes waiting for
3273
//script to arrive, investigate by e.g. making the
3274
//test hang in a mainloop below
3277
//expected asserts are commented out below.
3285
TEST_EQ (child_exit_status, 0);
3279
3287
/* .. but the post stop should have written data */
3280
//TEST_EQ (stat (filename, &statbuf), 0);
3288
TEST_EQ (stat (filename, &statbuf), 0);
3283
3290
fclose (output);
3285
3292
/* check file contents */
3286
//output = fopen (filename, "r");
3287
//TEST_NE_P (output, NULL);
3289
//CHECK_FILE_EQ (output, "hello\r\n", TRUE);
3290
//CHECK_FILE_EQ (output, "world\r\n", TRUE);
3292
//TEST_FILE_END (output);
3295
//TEST_EQ (unlink (filename), 0);
3293
output = fopen (filename, "r");
3294
TEST_NE_P (output, NULL);
3296
CHECK_FILE_EQ (output, "hello\r\n", TRUE);
3297
CHECK_FILE_EQ (output, "world\r\n", TRUE);
3299
TEST_FILE_END (output);
3302
TEST_EQ (unlink (filename), 0);
3297
3304
nih_free (class);
3305
TEST_RESET_MAIN_LOOP ();
3299
3307
/************************************************************/
3300
3308
TEST_FEATURE ("with single-line command running an invalid command, then a post-stop command");