~xnox/upstart/shrink-notifications

« back to all changes in this revision

Viewing changes to init/tests/test_job_process.c

  • Committer: Dimitri John Ledkov
  • Date: 2014-07-02 11:27:22 UTC
  • Revision ID: dimitri.ledkov@canonical.com-20140702112722-clfvfzt9ykwy7xh8
Wait for the right pid to exit, reset main-loop state between tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
125
125
                TEST_EQ (ok, TRUE); \
126
126
        } while (0)
127
127
 
 
128
 
 
129
/* 
 
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
 
135
 */
 
136
 
 
137
#define TEST_INSTALL_CHILD_HANDLERS()                           \
 
138
        NIH_MUST (nih_child_add_watch (NULL,                    \
 
139
                                       -1,                      \
 
140
                                       NIH_CHILD_ALL,           \
 
141
                                       job_process_handler,     \
 
142
                                       NULL));                  \
 
143
        NIH_MUST (nih_child_add_watch (NULL,                    \
 
144
                                       -1,                      \
 
145
                                       NIH_CHILD_ALL,           \
 
146
                                       test_job_process_handler,\
 
147
                                       NULL));                  \
 
148
        NIH_MUST (nih_main_loop_add_func (NULL, (NihMainLoopCb)event_poll, \
 
149
                                          NULL))
 
150
 
 
151
 
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);
152
176
 
153
 
static int child_exit_status;
 
177
static int   child_exit_status;
 
178
static pid_t child_watch_pid;
154
179
 
155
180
/**
156
181
 * test_job_process_handler:
170
195
                          NihChildEvents  event,
171
196
                          int             status)
172
197
{
173
 
        child_exit_status = status;
174
 
        nih_main_loop_exit (0);
 
198
        if (pid == child_watch_pid) {
 
199
                child_exit_status = status;
 
200
                nih_main_loop_exit (0);
 
201
        }
175
202
}
176
203
 
177
204
 
1084
1111
                nih_free (class);
1085
1112
        }
1086
1113
 
1087
 
        NIH_MUST (nih_child_add_watch (NULL,
1088
 
                                -1,
1089
 
                                NIH_CHILD_ALL,
1090
 
                                job_process_handler,
1091
 
                                NULL)); 
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.
1095
 
         */
1096
 
        NIH_MUST (nih_child_add_watch (NULL,
1097
 
                                -1,
1098
 
                                NIH_CHILD_ALL,
1099
 
                                test_job_process_handler,
1100
 
                                NULL)); 
1101
 
        /* Process the event queue each time through the main loop */
1102
 
        NIH_MUST (nih_main_loop_add_func (NULL, (NihMainLoopCb)event_poll,
1103
 
                                          NULL));
1104
 
 
1105
1114
 
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
1110
1119
        TEST_FEATURE ("with no such file");
1111
1120
        TEST_HASH_EMPTY (job_classes);
1112
1121
 
 
1122
        TEST_RESET_MAIN_LOOP ();
 
1123
        TEST_INSTALL_CHILD_HANDLERS ();
 
1124
 
1113
1125
        output = tmpfile ();
1114
1126
 
1115
1127
        TEST_ALLOC_FAIL {
1131
1143
 
1132
1144
                TEST_DIVERT_STDERR (output) {
1133
1145
                        job_process_start (job, PROCESS_MAIN);
1134
 
                        pid = job->pid[PROCESS_MAIN];
1135
 
                        TEST_NE (pid, -1);
 
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);
1138
1150
                }
1151
1163
 
1152
1164
        TEST_EQ (rmdir (dirname), 0);
1153
1165
 
 
1166
        TEST_RESET_MAIN_LOOP ();
 
1167
 
1154
1168
        TEST_FILENAME (dirname);       
1155
1169
        TEST_EQ (mkdir (dirname, 0755), 0);
1156
1170
 
3071
3085
        TEST_FEATURE ("with single-line command running an invalid command");
3072
3086
        TEST_HASH_EMPTY (job_classes);
3073
3087
 
 
3088
        TEST_RESET_MAIN_LOOP ();
 
3089
        TEST_INSTALL_CHILD_HANDLERS ();
 
3090
 
3074
3091
        class = job_class_new (NULL, "buzz", NULL);
3075
3092
        TEST_NE_P (class, NULL);
3076
3093
 
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];
3104
 
                TEST_NE (pid, -1);
 
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);
3107
3124
        }
3117
3134
        TEST_EQ (errno, ENOENT);
3118
3135
 
3119
3136
        nih_free (class);
 
3137
        TEST_RESET_MAIN_LOOP ();
3120
3138
 
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);
3124
3142
 
 
3143
        TEST_INSTALL_CHILD_HANDLERS ();
 
3144
 
3125
3145
        class = job_class_new (NULL, "asterix", NULL);
3126
3146
        TEST_NE_P (class, NULL);
3127
3147
 
3159
3179
 
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);
3164
3186
 
3171
3193
                job->goal = JOB_STOP;
3172
3194
                job->state = JOB_POST_STOP;
3173
3195
 
 
3196
                child_exit_status = 0;
3174
3197
                job_process_start (job, PROCESS_POST_STOP);
3175
 
 
3176
 
                TEST_NE (job->pid[PROCESS_POST_STOP], 0);
3177
 
                TEST_WATCH_UPDATE ();
3178
 
 
3179
 
                /* Flush the io so that the shell on the client side
3180
 
                 * gets the data (the script to execute).
3181
 
                 */
3182
 
                TEST_WATCH_UPDATE ();
3183
 
 
3184
 
                waitpid (job->pid[PROCESS_POST_STOP], &status, 0);
3185
 
                TEST_TRUE (WIFEXITED (status));
3186
 
                TEST_EQ (WEXITSTATUS (status), 0);
3187
 
 
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);
3189
3202
 
3190
3203
                /* .. but the post stop should have written data */
3191
3204
                TEST_EQ (stat (filename, &statbuf), 0);
3192
 
                event_poll ();
3193
3205
        }
3194
3206
        fclose (output);
3195
3207
 
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);
3212
3224
 
 
3225
        TEST_RESET_MAIN_LOOP ();
 
3226
        TEST_INSTALL_CHILD_HANDLERS ();
 
3227
 
3213
3228
        class = job_class_new (NULL, "asterix", NULL);
3214
3229
        TEST_NE_P (class, NULL);
3215
3230
 
3246
3261
 
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);
 
3266
 
3249
3267
                TEST_EQ (nih_main_loop (), 0);
3250
 
                TEST_NE (child_exit_status, 0);
 
3268
                TEST_EQ (child_exit_status, 255);
3251
3269
 
3252
3270
                /* We don't expect a logfile to be written since there is no
3253
3271
                 * accompanying shell to write the error.
3260
3278
 
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);
3265
3283
 
3266
3284
                TEST_EQ (nih_main_loop (), 0);
3267
 
                TEST_TRUE (WIFEXITED (status));
3268
 
                TEST_EQ (WEXITSTATUS(child_exit_status), 0);
3269
 
                fflush(NULL);
3270
 
 
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
3275
 
                //nih_main_loop();
3276
 
                
3277
 
                //expected asserts are commented out below.
 
3285
                TEST_EQ (child_exit_status, 0);
3278
3286
                
3279
3287
                /* .. but the post stop should have written data */
3280
 
                //TEST_EQ (stat (filename, &statbuf), 0);
3281
 
                //FIXME
 
3288
                TEST_EQ (stat (filename, &statbuf), 0);
3282
3289
        }
3283
3290
        fclose (output);
3284
3291
 
3285
3292
        /* check file contents */
3286
 
        //output = fopen (filename, "r");
3287
 
        //TEST_NE_P (output, NULL);
3288
 
 
3289
 
        //CHECK_FILE_EQ (output, "hello\r\n", TRUE);
3290
 
        //CHECK_FILE_EQ (output, "world\r\n", TRUE);
3291
 
 
3292
 
        //TEST_FILE_END (output);
3293
 
        //fclose (output);
3294
 
 
3295
 
        //TEST_EQ (unlink (filename), 0);
 
3293
        output = fopen (filename, "r");
 
3294
        TEST_NE_P (output, NULL);
 
3295
 
 
3296
        CHECK_FILE_EQ (output, "hello\r\n", TRUE);
 
3297
        CHECK_FILE_EQ (output, "world\r\n", TRUE);
 
3298
 
 
3299
        TEST_FILE_END (output);
 
3300
        fclose (output);
 
3301
 
 
3302
        TEST_EQ (unlink (filename), 0);
3296
3303
 
3297
3304
        nih_free (class);
 
3305
        TEST_RESET_MAIN_LOOP ();
3298
3306
 
3299
3307
        /************************************************************/
3300
3308
        TEST_FEATURE ("with single-line command running an invalid command, then a post-stop command");