788
967
nih_free (class);
970
TEST_FILENAME (dirname);
971
TEST_EQ (mkdir (dirname, 0755), 0);
973
/* Override default location to ensure job output goes to a
976
TEST_EQ (setenv ("UPSTART_LOGDIR", dirname, 1), 0);
978
/************************************************************/
979
TEST_FEATURE ("ensure that no log file written for single-line no-output script");
981
class = job_class_new (NULL, "test", NULL);
982
TEST_NE_P (class, NULL);
983
TEST_GT (sprintf (filename, "%s/test.log", dirname), 0);
985
class->console = CONSOLE_LOG;
986
class->process[PROCESS_MAIN] = process_new (class);
987
class->process[PROCESS_MAIN]->command = nih_strdup (
988
class->process[PROCESS_MAIN],
990
class->process[PROCESS_MAIN]->script = TRUE;
992
job = job_new (class, "");
993
job->goal = JOB_START;
994
job->state = JOB_SPAWNED;
996
ret = job_process_run (job, PROCESS_MAIN);
999
TEST_NE (job->pid[PROCESS_MAIN], 0);
1001
waitpid (job->pid[PROCESS_MAIN], &status, 0);
1002
TEST_TRUE (WIFEXITED (status));
1003
TEST_EQ (WEXITSTATUS (status), 0);
1005
TEST_FORCE_WATCH_UPDATE ();
1007
ENSURE_DIRECTORY_EMPTY (dirname);
1010
TEST_TRUE (stat (filename, &statbuf) < 0 && errno == ENOENT);
1014
/************************************************************/
1015
TEST_FEATURE ("ensure that no log file written for single-line no-output command");
1017
class = job_class_new (NULL, "test", NULL);
1018
TEST_NE_P (class, NULL);
1019
TEST_GT (sprintf (filename, "%s/test.log", dirname), 0);
1021
class->console = CONSOLE_LOG;
1022
class->process[PROCESS_MAIN] = process_new (class);
1023
class->process[PROCESS_MAIN]->command = nih_strdup (
1024
class->process[PROCESS_MAIN],
1026
class->process[PROCESS_MAIN]->script = FALSE;
1028
job = job_new (class, "");
1029
job->goal = JOB_START;
1030
job->state = JOB_SPAWNED;
1032
ret = job_process_run (job, PROCESS_MAIN);
1035
TEST_NE (job->pid[PROCESS_MAIN], 0);
1037
waitpid (job->pid[PROCESS_MAIN], &status, 0);
1038
TEST_TRUE (WIFEXITED (status));
1039
TEST_EQ (WEXITSTATUS (status), 0);
1041
TEST_FORCE_WATCH_UPDATE ();
1043
ENSURE_DIRECTORY_EMPTY (dirname);
1046
TEST_TRUE (stat (filename, &statbuf) < 0 && errno == ENOENT);
1050
/************************************************************/
1051
TEST_FEATURE ("ensure that no log file written for CONSOLE_NONE");
1053
class = job_class_new (NULL, "test", NULL);
1054
TEST_NE_P (class, NULL);
1055
TEST_GT (sprintf (filename, "%s/test.log", dirname), 0);
1057
class->console = CONSOLE_NONE;
1058
class->process[PROCESS_MAIN] = process_new (class);
1059
class->process[PROCESS_MAIN]->command = nih_sprintf (
1060
class->process[PROCESS_MAIN],
1061
"%s hello world", TEST_CMD_ECHO);
1062
class->process[PROCESS_MAIN]->script = TRUE;
1064
job = job_new (class, "");
1065
job->goal = JOB_START;
1066
job->state = JOB_SPAWNED;
1068
ret = job_process_run (job, PROCESS_MAIN);
1071
TEST_NE (job->pid[PROCESS_MAIN], 0);
1073
waitpid (job->pid[PROCESS_MAIN], &status, 0);
1074
TEST_TRUE (WIFEXITED (status));
1076
TEST_EQ (WEXITSTATUS (status), 0);
1078
/* If a log is written, select(2) will inform us, but we don't
1079
* expect this, hence specify a timeout.
1085
TEST_FORCE_WATCH_UPDATE_TIMEOUT (t);
1088
ENSURE_DIRECTORY_EMPTY (dirname);
1091
TEST_TRUE (stat (filename, &statbuf) < 0 && errno == ENOENT);
1095
/************************************************************/
1096
TEST_FEATURE ("ensure that no log file written for multi-line no-output script");
1098
class = job_class_new (NULL, "test", NULL);
1099
TEST_NE_P (class, NULL);
1100
TEST_GT (sprintf (filename, "%s/test.log", dirname), 0);
1102
class->console = CONSOLE_LOG;
1103
class->process[PROCESS_MAIN] = process_new (class);
1104
class->process[PROCESS_MAIN]->command = nih_strdup (
1105
class->process[PROCESS_MAIN],
1106
"/bin/true\n/bin/false");
1107
class->process[PROCESS_MAIN]->script = TRUE;
1109
job = job_new (class, "");
1110
job->goal = JOB_START;
1111
job->state = JOB_SPAWNED;
1113
ret = job_process_run (job, PROCESS_MAIN);
1116
TEST_NE (job->pid[PROCESS_MAIN], 0);
1118
/* XXX: call 1: wait for script write to child shell */
1119
TEST_FORCE_WATCH_UPDATE ();
1121
waitpid (job->pid[PROCESS_MAIN], &status, 0);
1122
TEST_TRUE (WIFEXITED (status));
1124
/* we've just run /bin/false remember? :) */
1125
TEST_EQ (WEXITSTATUS (status), 1);
1127
/* XXX: call 2: wait for read from pty allowing logger to write to log file */
1128
TEST_FORCE_WATCH_UPDATE ();
1130
ENSURE_DIRECTORY_EMPTY (dirname);
1133
TEST_TRUE (stat (filename, &statbuf) < 0 && errno == ENOENT);
1137
/************************************************************/
1138
TEST_FEATURE ("with single-line script that writes 1 line to stdout");
1140
/* Note we can't use TEST_ALLOC_FAIL() for this test since on
1141
* the ENOMEM loop all we could do is discard the error and
1142
* continue since job_process_run() calls job_process_spawn()
1143
* repeatedly until it works, but the alloc fails in log_new()
1144
* invoked by job_process_spawn() such that when we've left
1145
* job_process_run(), it's too late.
1147
* However, we test this scenario in test_spawn() so all is not
1150
class = job_class_new (NULL, "test", NULL);
1151
TEST_NE_P (class, NULL);
1153
TEST_GT (sprintf (filename, "%s/test.log", dirname), 0);
1155
class->console = CONSOLE_LOG;
1156
class->process[PROCESS_MAIN] = process_new (class);
1157
class->process[PROCESS_MAIN]->command = nih_sprintf (
1158
class->process[PROCESS_MAIN],
1159
"%s hello world", TEST_CMD_ECHO);
1160
class->process[PROCESS_MAIN]->script = TRUE;
1162
job = job_new (class, "");
1163
job->goal = JOB_START;
1164
job->state = JOB_SPAWNED;
1166
ret = job_process_run (job, PROCESS_MAIN);
1169
TEST_NE (job->pid[PROCESS_MAIN], 0);
1171
waitpid (job->pid[PROCESS_MAIN], &status, 0);
1172
TEST_TRUE (WIFEXITED (status));
1173
TEST_EQ (WEXITSTATUS (status), 0);
1175
TEST_FORCE_WATCH_UPDATE ();
1177
TEST_EQ (stat (filename, &statbuf), 0);
1179
TEST_TRUE (S_ISREG (statbuf.st_mode));
1181
TEST_TRUE (statbuf.st_mode & S_IRUSR);
1182
TEST_TRUE (statbuf.st_mode & S_IWUSR);
1183
TEST_FALSE (statbuf.st_mode & S_IXUSR);
1185
TEST_TRUE (statbuf.st_mode & S_IRGRP);
1186
TEST_FALSE (statbuf.st_mode & S_IWGRP);
1187
TEST_FALSE (statbuf.st_mode & S_IXGRP);
1189
TEST_FALSE (statbuf.st_mode & S_IROTH);
1190
TEST_FALSE (statbuf.st_mode & S_IWOTH);
1191
TEST_FALSE (statbuf.st_mode & S_IXOTH);
1193
output = fopen (filename, "r");
1194
TEST_NE_P (output, NULL);
1196
TEST_FILE_EQ (output, "hello world\r\n");
1197
TEST_FILE_END (output);
1200
TEST_EQ (unlink (filename), 0);
1203
/************************************************************/
1204
TEST_FEATURE ("with single-line script that is killed");
1206
class = job_class_new (NULL, "test", NULL);
1207
TEST_NE_P (class, NULL);
1209
TEST_GT (sprintf (filename, "%s/test.log", dirname), 0);
1211
class->console = CONSOLE_LOG;
1212
class->process[PROCESS_MAIN] = process_new (class);
1213
class->process[PROCESS_MAIN]->command = nih_sprintf (
1214
class->process[PROCESS_MAIN],
1215
"%s hello world;sleep 999", TEST_CMD_ECHO);
1216
class->process[PROCESS_MAIN]->script = TRUE;
1218
job = job_new (class, "");
1219
job->goal = JOB_START;
1220
job->state = JOB_SPAWNED;
1222
ret = job_process_run (job, PROCESS_MAIN);
1225
TEST_NE (job->pid[PROCESS_MAIN], 0);
1227
/* wait for read from pty allowing logger to write to log file */
1228
TEST_FORCE_WATCH_UPDATE ();
1230
TEST_EQ (kill (job->pid[PROCESS_MAIN], SIGKILL), 0);
1231
waitpid (job->pid[PROCESS_MAIN], &status, 0);
1232
TEST_TRUE (WIFSIGNALED (status));
1233
TEST_EQ (WTERMSIG (status), SIGKILL);
1235
TEST_EQ (stat (filename, &statbuf), 0);
1237
TEST_TRUE (S_ISREG (statbuf.st_mode));
1239
TEST_TRUE (statbuf.st_mode & S_IRUSR);
1240
TEST_TRUE (statbuf.st_mode & S_IWUSR);
1241
TEST_FALSE (statbuf.st_mode & S_IXUSR);
1243
TEST_TRUE (statbuf.st_mode & S_IRGRP);
1244
TEST_FALSE (statbuf.st_mode & S_IWGRP);
1245
TEST_FALSE (statbuf.st_mode & S_IXGRP);
1247
TEST_FALSE (statbuf.st_mode & S_IROTH);
1248
TEST_FALSE (statbuf.st_mode & S_IWOTH);
1249
TEST_FALSE (statbuf.st_mode & S_IXOTH);
1251
output = fopen (filename, "r");
1252
TEST_NE_P (output, NULL);
1254
TEST_FILE_EQ (output, "hello world\r\n");
1255
TEST_FILE_END (output);
1258
TEST_EQ (unlink (filename), 0);
1261
/************************************************************/
1262
TEST_FEATURE ("with single-line command that is killed");
1264
class = job_class_new (NULL, "test", NULL);
1265
TEST_NE_P (class, NULL);
1267
TEST_GT (sprintf (filename, "%s/test.log", dirname), 0);
1269
class->console = CONSOLE_LOG;
1270
class->process[PROCESS_MAIN] = process_new (class);
1271
class->process[PROCESS_MAIN]->command = nih_sprintf (
1272
class->process[PROCESS_MAIN],
1273
"%s", TEST_CMD_YES);
1274
class->process[PROCESS_MAIN]->script = FALSE;
1276
job = job_new (class, "");
1277
job->goal = JOB_START;
1278
job->state = JOB_SPAWNED;
1280
ret = job_process_run (job, PROCESS_MAIN);
1283
TEST_NE (job->pid[PROCESS_MAIN], 0);
1285
/* wait for read from pty allowing logger to write to log file */
1286
TEST_FORCE_WATCH_UPDATE ();
1288
TEST_EQ (kill (job->pid[PROCESS_MAIN], SIGKILL), 0);
1289
waitpid (job->pid[PROCESS_MAIN], &status, 0);
1290
TEST_TRUE (WIFSIGNALED (status));
1291
TEST_EQ (WTERMSIG (status), SIGKILL);
1293
TEST_EQ (stat (filename, &statbuf), 0);
1295
TEST_TRUE (S_ISREG (statbuf.st_mode));
1297
TEST_TRUE (statbuf.st_mode & S_IRUSR);
1298
TEST_TRUE (statbuf.st_mode & S_IWUSR);
1299
TEST_FALSE (statbuf.st_mode & S_IXUSR);
1301
TEST_TRUE (statbuf.st_mode & S_IRGRP);
1302
TEST_FALSE (statbuf.st_mode & S_IWGRP);
1303
TEST_FALSE (statbuf.st_mode & S_IXGRP);
1305
TEST_FALSE (statbuf.st_mode & S_IROTH);
1306
TEST_FALSE (statbuf.st_mode & S_IWOTH);
1307
TEST_FALSE (statbuf.st_mode & S_IXOTH);
1309
output = fopen (filename, "r");
1310
TEST_NE_P (output, NULL);
1312
/* XXX: this _might_ be a kernel(?) bug - sometimes we don't read
1313
* the final line end character (presumably since the process
1314
* was forcibly killed).
1316
while (fgets (filebuf, sizeof(filebuf), output) != NULL) {
1317
if (! strcmp (filebuf, "y\r\n"))
1319
else if (! strcmp (filebuf, "y") && feof (output))
1329
TEST_FILE_END (output);
1332
TEST_EQ (unlink (filename), 0);
1335
/************************************************************/
1336
TEST_FEATURE ("with multi-line script that is killed");
1338
/* Note we can't use TEST_ALLOC_FAIL() for this test since on
1339
* the ENOMEM loop all we could do is discard the error and
1340
* continue since job_process_run() calls job_process_spawn()
1341
* repeatedly until it works, but the alloc fails in log_new()
1342
* invoked by job_process_spawn() such that when we've left
1343
* job_process_run(), it's too late.
1345
* However, we test this scenario in test_spawn() so all is not
1348
class = job_class_new (NULL, "test", NULL);
1349
TEST_NE_P (class, NULL);
1351
TEST_GT (sprintf (filename, "%s/test.log", dirname), 0);
1353
class->console = CONSOLE_LOG;
1354
class->process[PROCESS_MAIN] = process_new (class);
1355
class->process[PROCESS_MAIN]->command = nih_sprintf (
1356
class->process[PROCESS_MAIN],
1357
"%s hello world\nsleep 999", TEST_CMD_ECHO);
1358
class->process[PROCESS_MAIN]->script = TRUE;
1360
job = job_new (class, "");
1361
job->goal = JOB_START;
1362
job->state = JOB_SPAWNED;
1364
ret = job_process_run (job, PROCESS_MAIN);
1367
TEST_NE (job->pid[PROCESS_MAIN], 0);
1369
/* XXX: call 1: wait for script write to child shell */
1370
TEST_FORCE_WATCH_UPDATE ();
1372
/* XXX: call 2: wait for read from pty allowing logger to write to log file */
1373
TEST_FORCE_WATCH_UPDATE ();
1375
TEST_EQ (kill (job->pid[PROCESS_MAIN], SIGKILL), 0);
1376
waitpid (job->pid[PROCESS_MAIN], &status, 0);
1377
TEST_TRUE (WIFSIGNALED (status));
1378
TEST_EQ (WTERMSIG (status), SIGKILL);
1380
TEST_EQ (stat (filename, &statbuf), 0);
1382
TEST_TRUE (S_ISREG (statbuf.st_mode));
1384
TEST_TRUE (statbuf.st_mode & S_IRUSR);
1385
TEST_TRUE (statbuf.st_mode & S_IWUSR);
1386
TEST_FALSE (statbuf.st_mode & S_IXUSR);
1388
TEST_TRUE (statbuf.st_mode & S_IRGRP);
1389
TEST_FALSE (statbuf.st_mode & S_IWGRP);
1390
TEST_FALSE (statbuf.st_mode & S_IXGRP);
1392
TEST_FALSE (statbuf.st_mode & S_IROTH);
1393
TEST_FALSE (statbuf.st_mode & S_IWOTH);
1394
TEST_FALSE (statbuf.st_mode & S_IXOTH);
1396
output = fopen (filename, "r");
1397
TEST_NE_P (output, NULL);
1399
TEST_FILE_EQ (output, "hello world\r\n");
1400
TEST_FILE_END (output);
1403
TEST_EQ (unlink (filename), 0);
1406
/************************************************************/
1407
TEST_FEATURE ("with single-line script that writes 1 byte and is killed");
1409
class = job_class_new (NULL, "test", NULL);
1410
TEST_NE_P (class, NULL);
1412
TEST_GT (sprintf (filename, "%s/test.log", dirname), 0);
1414
class->console = CONSOLE_LOG;
1415
class->process[PROCESS_MAIN] = process_new (class);
1416
class->process[PROCESS_MAIN]->command = nih_sprintf (
1417
class->process[PROCESS_MAIN],
1418
"%s -ne X;sleep 999", TEST_CMD_ECHO);
1419
class->process[PROCESS_MAIN]->script = TRUE;
1421
job = job_new (class, "");
1422
job->goal = JOB_START;
1423
job->state = JOB_SPAWNED;
1425
ret = job_process_run (job, PROCESS_MAIN);
1428
TEST_NE (job->pid[PROCESS_MAIN], 0);
1430
/* wait for read from pty allowing logger to write to log file */
1431
TEST_FORCE_WATCH_UPDATE ();
1433
TEST_EQ (kill (job->pid[PROCESS_MAIN], SIGKILL), 0);
1434
waitpid (job->pid[PROCESS_MAIN], &status, 0);
1435
TEST_TRUE (WIFSIGNALED (status));
1436
TEST_EQ (WTERMSIG (status), SIGKILL);
1438
TEST_EQ (stat (filename, &statbuf), 0);
1440
TEST_TRUE (S_ISREG (statbuf.st_mode));
1442
TEST_TRUE (statbuf.st_mode & S_IRUSR);
1443
TEST_TRUE (statbuf.st_mode & S_IWUSR);
1444
TEST_FALSE (statbuf.st_mode & S_IXUSR);
1446
TEST_TRUE (statbuf.st_mode & S_IRGRP);
1447
TEST_FALSE (statbuf.st_mode & S_IWGRP);
1448
TEST_FALSE (statbuf.st_mode & S_IXGRP);
1450
TEST_FALSE (statbuf.st_mode & S_IROTH);
1451
TEST_FALSE (statbuf.st_mode & S_IWOTH);
1452
TEST_FALSE (statbuf.st_mode & S_IXOTH);
1454
output = fopen (filename, "r");
1455
TEST_NE_P (output, NULL);
1457
TEST_FILE_EQ (output, "X");
1458
TEST_FILE_END (output);
1461
TEST_EQ (unlink (filename), 0);
1464
/************************************************************/
1465
/* Can't think of a command that would echo 1 byte and then
1466
* either sleep or read a file forever to allow us time to kill
1467
* it *after* it had written the single byte. Answers on a
1470
* TEST_FEATURE ("with single-line command that writes 1 byte and is killed");
1473
/************************************************************/
1474
TEST_FEATURE ("with multi-line script that writes 1 byte and is killed");
1476
class = job_class_new (NULL, "multiline", NULL);
1477
TEST_NE_P (class, NULL);
1479
TEST_FILENAME (filename);
1480
TEST_GT (sprintf (filename, "%s/multiline.log", dirname), 0);
1482
class->console = CONSOLE_LOG;
1483
class->process[PROCESS_MAIN] = process_new (class);
1484
class->process[PROCESS_MAIN]->command = nih_sprintf (
1485
class->process[PROCESS_MAIN],
1486
"/bin/true\n%s -ne F", TEST_CMD_ECHO);
1487
class->process[PROCESS_MAIN]->script = TRUE;
1489
job = job_new (class, "");
1490
job->goal = JOB_START;
1491
job->state = JOB_SPAWNED;
1493
ret = job_process_run (job, PROCESS_MAIN);
1496
TEST_NE (job->pid[PROCESS_MAIN], 0);
1498
/* XXX: call 1: wait for script write to child shell */
1499
TEST_FORCE_WATCH_UPDATE ();
1501
waitpid (job->pid[PROCESS_MAIN], &status, 0);
1502
TEST_TRUE (WIFEXITED (status));
1503
TEST_EQ (WEXITSTATUS (status), 0);
1505
/* XXX: call 2: wait for read from pty allowing logger to write to log file */
1506
TEST_FORCE_WATCH_UPDATE ();
1508
TEST_EQ (stat (filename, &statbuf), 0);
1510
TEST_TRUE (S_ISREG (statbuf.st_mode));
1512
TEST_TRUE (statbuf.st_mode & S_IRUSR);
1513
TEST_TRUE (statbuf.st_mode & S_IWUSR);
1514
TEST_FALSE (statbuf.st_mode & S_IXUSR);
1516
TEST_TRUE (statbuf.st_mode & S_IRGRP);
1517
TEST_FALSE (statbuf.st_mode & S_IWGRP);
1518
TEST_FALSE (statbuf.st_mode & S_IXGRP);
1520
TEST_FALSE (statbuf.st_mode & S_IROTH);
1521
TEST_FALSE (statbuf.st_mode & S_IWOTH);
1522
TEST_FALSE (statbuf.st_mode & S_IXOTH);
1524
output = fopen (filename, "r");
1525
TEST_NE_P (output, NULL);
1527
TEST_FILE_EQ (output, "F");
1528
TEST_FILE_END (output);
1531
TEST_EQ (unlink (filename), 0);
1534
/************************************************************/
1535
TEST_FEATURE ("with single-line command that writes 1 line to stdout");
1537
/* Note we can't use TEST_ALLOC_FAIL() for this test since on
1538
* the ENOMEM loop all we could do is discard the error and
1539
* continue since job_process_run() calls job_process_spawn()
1540
* repeatedly until it works, but the alloc fails in log_new()
1541
* invoked by job_process_spawn() such that when we've left
1542
* job_process_run(), it's too late.
1544
* However, we test this scenario in test_spawn() so all is not
1547
class = job_class_new (NULL, "test", NULL);
1548
TEST_NE_P (class, NULL);
1550
TEST_GT (sprintf (filename, "%s/test.log", dirname), 0);
1552
class->console = CONSOLE_LOG;
1553
class->process[PROCESS_MAIN] = process_new (class);
1554
class->process[PROCESS_MAIN]->command = nih_sprintf (
1555
class->process[PROCESS_MAIN],
1556
"%s hello world", TEST_CMD_ECHO);
1557
class->process[PROCESS_MAIN]->script = FALSE;
1559
job = job_new (class, "");
1560
job->goal = JOB_START;
1561
job->state = JOB_SPAWNED;
1563
ret = job_process_run (job, PROCESS_MAIN);
1566
TEST_NE (job->pid[PROCESS_MAIN], 0);
1568
waitpid (job->pid[PROCESS_MAIN], &status, 0);
1569
TEST_TRUE (WIFEXITED (status));
1570
TEST_EQ (WEXITSTATUS (status), 0);
1572
TEST_FORCE_WATCH_UPDATE ();
1574
TEST_EQ (stat (filename, &statbuf), 0);
1576
TEST_TRUE (S_ISREG (statbuf.st_mode));
1578
TEST_TRUE (statbuf.st_mode & S_IRUSR);
1579
TEST_TRUE (statbuf.st_mode & S_IWUSR);
1580
TEST_FALSE (statbuf.st_mode & S_IXUSR);
1582
TEST_TRUE (statbuf.st_mode & S_IRGRP);
1583
TEST_FALSE (statbuf.st_mode & S_IWGRP);
1584
TEST_FALSE (statbuf.st_mode & S_IXGRP);
1586
TEST_FALSE (statbuf.st_mode & S_IROTH);
1587
TEST_FALSE (statbuf.st_mode & S_IWOTH);
1588
TEST_FALSE (statbuf.st_mode & S_IXOTH);
1590
output = fopen (filename, "r");
1591
TEST_NE_P (output, NULL);
1593
TEST_FILE_EQ (output, "hello world\r\n");
1594
TEST_FILE_END (output);
1597
TEST_EQ (unlink (filename), 0);
1600
/************************************************************/
1601
TEST_FEATURE ("with multi-line script that writes 1 line to stdout");
1603
/* XXX: Note that all tests which use multi-line scripts (but
1604
* XXX: *NOT* commands!) and produce output must call
1605
* XXX: TEST_FORCE_WATCH_UPDATE() *TWICE* to ensure select(2) is
1606
* XXX: called twice.
1608
* This is required since job_process_run() uses an NihIo object
1609
* to squirt the script to the shell sub-process and this
1610
* triggers select to return when the data is written to the shell.
1611
* However, we don't care about that directly - we care more about
1612
* a subsequent fd becoming ready to read data from - the fd
1613
* associated with the pty which will trigger the log file to be
1616
* Note that the 2nd call to TEST_FORCE_WATCH_UPDATE would not be
1617
* required should job_process_run() simple invoke write(2) to
1621
class = job_class_new (NULL, "multiline", NULL);
1622
TEST_NE_P (class, NULL);
1624
TEST_FILENAME (filename);
1625
TEST_GT (sprintf (filename, "%s/multiline.log", dirname), 0);
1627
class->console = CONSOLE_LOG;
1628
class->process[PROCESS_MAIN] = process_new (class);
1629
class->process[PROCESS_MAIN]->command = nih_sprintf (
1630
class->process[PROCESS_MAIN],
1631
"/bin/true\n%s hello world\n\n\n\n\n\n\n\n\n\n", TEST_CMD_ECHO);
1632
class->process[PROCESS_MAIN]->script = TRUE;
1634
job = job_new (class, "");
1635
job->goal = JOB_START;
1636
job->state = JOB_SPAWNED;
1638
ret = job_process_run (job, PROCESS_MAIN);
1641
TEST_NE (job->pid[PROCESS_MAIN], 0);
1643
/* XXX: call 1: wait for script write to child shell */
1644
TEST_FORCE_WATCH_UPDATE ();
1646
waitpid (job->pid[PROCESS_MAIN], &status, 0);
1647
TEST_TRUE (WIFEXITED (status));
1648
TEST_EQ (WEXITSTATUS (status), 0);
1650
/* XXX: call 2: wait for read from pty allowing logger to write to log file */
1651
TEST_FORCE_WATCH_UPDATE ();
1653
TEST_EQ (stat (filename, &statbuf), 0);
1655
TEST_TRUE (S_ISREG (statbuf.st_mode));
1657
TEST_TRUE (statbuf.st_mode & S_IRUSR);
1658
TEST_TRUE (statbuf.st_mode & S_IWUSR);
1659
TEST_FALSE (statbuf.st_mode & S_IXUSR);
1661
TEST_TRUE (statbuf.st_mode & S_IRGRP);
1662
TEST_FALSE (statbuf.st_mode & S_IWGRP);
1663
TEST_FALSE (statbuf.st_mode & S_IXGRP);
1665
TEST_FALSE (statbuf.st_mode & S_IROTH);
1666
TEST_FALSE (statbuf.st_mode & S_IWOTH);
1667
TEST_FALSE (statbuf.st_mode & S_IXOTH);
1669
output = fopen (filename, "r");
1670
TEST_NE_P (output, NULL);
1672
TEST_FILE_EQ (output, "hello world\r\n");
1673
TEST_FILE_END (output);
1676
TEST_EQ (unlink (filename), 0);
1679
/************************************************************/
1680
TEST_FEATURE ("with instance job and single-line script that writes 1 line to stdout");
1682
class = job_class_new (NULL, "test", NULL);
1683
TEST_NE_P (class, NULL);
1685
TEST_GT (sprintf (filename, "%s/test-instance.log", dirname), 0);
1687
class->console = CONSOLE_LOG;
1688
class->process[PROCESS_MAIN] = process_new (class);
1689
class->process[PROCESS_MAIN]->command = nih_sprintf (
1690
class->process[PROCESS_MAIN],
1691
"%s hello world", TEST_CMD_ECHO);
1692
class->process[PROCESS_MAIN]->script = TRUE;
1694
job = job_new (class, "instance");
1695
job->goal = JOB_START;
1696
job->state = JOB_SPAWNED;
1698
ret = job_process_run (job, PROCESS_MAIN);
1701
TEST_NE (job->pid[PROCESS_MAIN], 0);
1703
waitpid (job->pid[PROCESS_MAIN], &status, 0);
1704
TEST_TRUE (WIFEXITED (status));
1705
TEST_EQ (WEXITSTATUS (status), 0);
1707
TEST_FORCE_WATCH_UPDATE ();
1709
TEST_EQ (stat (filename, &statbuf), 0);
1711
TEST_TRUE (S_ISREG (statbuf.st_mode));
1713
TEST_TRUE (statbuf.st_mode & S_IRUSR);
1714
TEST_TRUE (statbuf.st_mode & S_IWUSR);
1715
TEST_FALSE (statbuf.st_mode & S_IXUSR);
1717
TEST_TRUE (statbuf.st_mode & S_IRGRP);
1718
TEST_FALSE (statbuf.st_mode & S_IWGRP);
1719
TEST_FALSE (statbuf.st_mode & S_IXGRP);
1721
TEST_FALSE (statbuf.st_mode & S_IROTH);
1722
TEST_FALSE (statbuf.st_mode & S_IWOTH);
1723
TEST_FALSE (statbuf.st_mode & S_IXOTH);
1725
output = fopen (filename, "r");
1726
TEST_NE_P (output, NULL);
1728
TEST_FILE_EQ (output, "hello world\r\n");
1729
TEST_FILE_END (output);
1732
TEST_EQ (unlink (filename), 0);
1735
/************************************************************/
1736
TEST_FEATURE ("with single-line script that writes >1 lines to stdout");
1738
class = job_class_new (NULL, "foo", NULL);
1739
TEST_NE_P (class, NULL);
1741
TEST_FILENAME (filename);
1742
TEST_GT (sprintf (filename, "%s/foo.log", dirname), 0);
1744
class->console = CONSOLE_LOG;
1745
class->process[PROCESS_MAIN] = process_new (class);
1746
class->process[PROCESS_MAIN]->command = nih_sprintf (
1747
class->process[PROCESS_MAIN],
1748
/* XXX: note the required quoting */
1749
"%s -ne \"hello world\\n\\n\\n\"", TEST_CMD_ECHO);
1750
class->process[PROCESS_MAIN]->script = TRUE;
1752
job = job_new (class, "");
1753
job->goal = JOB_START;
1754
job->state = JOB_SPAWNED;
1756
ret = job_process_run (job, PROCESS_MAIN);
1759
TEST_NE (job->pid[PROCESS_MAIN], 0);
1761
waitpid (job->pid[PROCESS_MAIN], &status, 0);
1762
TEST_TRUE (WIFEXITED (status));
1763
TEST_EQ (WEXITSTATUS (status), 0);
1765
TEST_FORCE_WATCH_UPDATE ();
1767
TEST_EQ (stat (filename, &statbuf), 0);
1769
TEST_TRUE (S_ISREG (statbuf.st_mode));
1771
TEST_TRUE (statbuf.st_mode & S_IRUSR);
1772
TEST_TRUE (statbuf.st_mode & S_IWUSR);
1773
TEST_FALSE (statbuf.st_mode & S_IXUSR);
1775
TEST_TRUE (statbuf.st_mode & S_IRGRP);
1776
TEST_FALSE (statbuf.st_mode & S_IWGRP);
1777
TEST_FALSE (statbuf.st_mode & S_IXGRP);
1779
TEST_FALSE (statbuf.st_mode & S_IROTH);
1780
TEST_FALSE (statbuf.st_mode & S_IWOTH);
1781
TEST_FALSE (statbuf.st_mode & S_IXOTH);
1783
output = fopen (filename, "r");
1784
TEST_NE_P (output, NULL);
1786
/* Yup, pseudo-terminals record *everything*,
1787
* even the carriage returns.
1789
TEST_FILE_EQ (output, "hello world\r\n");
1790
TEST_FILE_EQ (output, "\r\n");
1791
TEST_FILE_EQ (output, "\r\n");
1792
TEST_FILE_END (output);
1795
TEST_EQ (unlink (filename), 0);
1798
/************************************************************/
1799
TEST_FEATURE ("with single-line command that writes >1 lines to stdout");
1801
class = job_class_new (NULL, "foo", NULL);
1802
TEST_NE_P (class, NULL);
1804
TEST_FILENAME (filename);
1805
TEST_GT (sprintf (filename, "%s/foo.log", dirname), 0);
1807
class->console = CONSOLE_LOG;
1808
class->process[PROCESS_MAIN] = process_new (class);
1809
class->process[PROCESS_MAIN]->command = nih_sprintf (
1810
class->process[PROCESS_MAIN],
1811
/* XXX: note the required quoting */
1812
"%s -ne \"hello world\\n\\n\\n\"", TEST_CMD_ECHO);
1813
class->process[PROCESS_MAIN]->script = FALSE;
1815
job = job_new (class, "");
1816
job->goal = JOB_START;
1817
job->state = JOB_SPAWNED;
1819
ret = job_process_run (job, PROCESS_MAIN);
1822
TEST_NE (job->pid[PROCESS_MAIN], 0);
1824
waitpid (job->pid[PROCESS_MAIN], &status, 0);
1825
TEST_TRUE (WIFEXITED (status));
1826
TEST_EQ (WEXITSTATUS (status), 0);
1828
TEST_FORCE_WATCH_UPDATE ();
1830
TEST_EQ (stat (filename, &statbuf), 0);
1832
TEST_TRUE (S_ISREG (statbuf.st_mode));
1834
TEST_TRUE (statbuf.st_mode & S_IRUSR);
1835
TEST_TRUE (statbuf.st_mode & S_IWUSR);
1836
TEST_FALSE (statbuf.st_mode & S_IXUSR);
1838
TEST_TRUE (statbuf.st_mode & S_IRGRP);
1839
TEST_FALSE (statbuf.st_mode & S_IWGRP);
1840
TEST_FALSE (statbuf.st_mode & S_IXGRP);
1842
TEST_FALSE (statbuf.st_mode & S_IROTH);
1843
TEST_FALSE (statbuf.st_mode & S_IWOTH);
1844
TEST_FALSE (statbuf.st_mode & S_IXOTH);
1846
output = fopen (filename, "r");
1847
TEST_NE_P (output, NULL);
1849
TEST_FILE_EQ (output, "hello world\r\n");
1850
TEST_FILE_EQ (output, "\r\n");
1851
TEST_FILE_EQ (output, "\r\n");
1852
TEST_FILE_END (output);
1855
TEST_EQ (unlink (filename), 0);
1858
/************************************************************/
1859
TEST_FEATURE ("with multi-line script that writes >1 lines to stdout");
1861
class = job_class_new (NULL, "elf", NULL);
1862
TEST_NE_P (class, NULL);
1864
TEST_FILENAME (filename);
1865
TEST_GT (sprintf (filename, "%s/elf.log", dirname), 0);
1867
class->console = CONSOLE_LOG;
1868
class->process[PROCESS_MAIN] = process_new (class);
1869
class->process[PROCESS_MAIN]->command = nih_sprintf (
1870
class->process[PROCESS_MAIN],
1871
/* XXX: note the required quoting */
1872
"\n/bin/true\n%s -ne \"hello world\\n\\n\\n\"\n\n", TEST_CMD_ECHO);
1873
class->process[PROCESS_MAIN]->script = TRUE;
1875
job = job_new (class, "");
1876
job->goal = JOB_START;
1877
job->state = JOB_SPAWNED;
1879
ret = job_process_run (job, PROCESS_MAIN);
1882
TEST_NE (job->pid[PROCESS_MAIN], 0);
1884
TEST_FORCE_WATCH_UPDATE ();
1885
waitpid (job->pid[PROCESS_MAIN], &status, 0);
1886
TEST_TRUE (WIFEXITED (status));
1887
TEST_EQ (WEXITSTATUS (status), 0);
1888
TEST_FORCE_WATCH_UPDATE ();
1890
TEST_EQ (stat (filename, &statbuf), 0);
1892
TEST_TRUE (S_ISREG (statbuf.st_mode));
1894
TEST_TRUE (statbuf.st_mode & S_IRUSR);
1895
TEST_TRUE (statbuf.st_mode & S_IWUSR);
1896
TEST_FALSE (statbuf.st_mode & S_IXUSR);
1898
TEST_TRUE (statbuf.st_mode & S_IRGRP);
1899
TEST_FALSE (statbuf.st_mode & S_IWGRP);
1900
TEST_FALSE (statbuf.st_mode & S_IXGRP);
1902
TEST_FALSE (statbuf.st_mode & S_IROTH);
1903
TEST_FALSE (statbuf.st_mode & S_IWOTH);
1904
TEST_FALSE (statbuf.st_mode & S_IXOTH);
1906
output = fopen (filename, "r");
1907
TEST_NE_P (output, NULL);
1909
TEST_FILE_EQ (output, "hello world\r\n");
1910
TEST_FILE_EQ (output, "\r\n");
1911
TEST_FILE_EQ (output, "\r\n");
1912
TEST_FILE_END (output);
1915
TEST_EQ (unlink (filename), 0);
1918
/************************************************************/
1919
TEST_FEATURE ("with single-line script that writes 1 line to stderr");
1921
class = job_class_new (NULL, "test", NULL);
1922
TEST_NE_P (class, NULL);
1924
TEST_GT (sprintf (filename, "%s/test.log", dirname), 0);
1926
class->console = CONSOLE_LOG;
1927
class->process[PROCESS_MAIN] = process_new (class);
1928
class->process[PROCESS_MAIN]->command = nih_sprintf (
1929
class->process[PROCESS_MAIN],
1930
"%s hello world >&2", TEST_CMD_ECHO);
1931
class->process[PROCESS_MAIN]->script = TRUE;
1933
job = job_new (class, "");
1934
job->goal = JOB_START;
1935
job->state = JOB_SPAWNED;
1937
ret = job_process_run (job, PROCESS_MAIN);
1940
TEST_NE (job->pid[PROCESS_MAIN], 0);
1942
waitpid (job->pid[PROCESS_MAIN], &status, 0);
1943
TEST_TRUE (WIFEXITED (status));
1944
TEST_EQ (WEXITSTATUS (status), 0);
1946
TEST_FORCE_WATCH_UPDATE ();
1948
TEST_EQ (stat (filename, &statbuf), 0);
1950
TEST_TRUE (S_ISREG (statbuf.st_mode));
1952
TEST_TRUE (statbuf.st_mode & S_IRUSR);
1953
TEST_TRUE (statbuf.st_mode & S_IWUSR);
1954
TEST_FALSE (statbuf.st_mode & S_IXUSR);
1956
TEST_TRUE (statbuf.st_mode & S_IRGRP);
1957
TEST_FALSE (statbuf.st_mode & S_IWGRP);
1958
TEST_FALSE (statbuf.st_mode & S_IXGRP);
1960
TEST_FALSE (statbuf.st_mode & S_IROTH);
1961
TEST_FALSE (statbuf.st_mode & S_IWOTH);
1962
TEST_FALSE (statbuf.st_mode & S_IXOTH);
1964
output = fopen (filename, "r");
1965
TEST_NE_P (output, NULL);
1967
TEST_FILE_EQ (output, "hello world\r\n");
1968
TEST_FILE_END (output);
1971
TEST_EQ (unlink (filename), 0);
1974
/************************************************************/
1975
TEST_FEATURE ("with single-line command that writes 1 line to stderr");
1977
/* Run a command that generates output to stderr without having
1978
* to use script redirection.
1980
* dd(1) is a good choice as it always writes to stderr.
1982
class = job_class_new (NULL, "test", NULL);
1983
TEST_NE_P (class, NULL);
1985
TEST_GT (sprintf (filename, "%s/test.log", dirname), 0);
1987
class->console = CONSOLE_LOG;
1988
class->process[PROCESS_MAIN] = process_new (class);
1989
class->process[PROCESS_MAIN]->command = nih_sprintf (
1990
class->process[PROCESS_MAIN],
1991
"%s if=/dev/zero of=/dev/null bs=1 count=0", TEST_CMD_DD);
1992
class->process[PROCESS_MAIN]->script = TRUE;
1994
job = job_new (class, "");
1995
job->goal = JOB_START;
1996
job->state = JOB_SPAWNED;
1998
ret = job_process_run (job, PROCESS_MAIN);
2001
TEST_NE (job->pid[PROCESS_MAIN], 0);
2003
waitpid (job->pid[PROCESS_MAIN], &status, 0);
2004
TEST_TRUE (WIFEXITED (status));
2005
TEST_EQ (WEXITSTATUS (status), 0);
2007
TEST_FORCE_WATCH_UPDATE ();
2009
TEST_EQ (stat (filename, &statbuf), 0);
2011
TEST_TRUE (S_ISREG (statbuf.st_mode));
2013
TEST_TRUE (statbuf.st_mode & S_IRUSR);
2014
TEST_TRUE (statbuf.st_mode & S_IWUSR);
2015
TEST_FALSE (statbuf.st_mode & S_IXUSR);
2017
TEST_TRUE (statbuf.st_mode & S_IRGRP);
2018
TEST_FALSE (statbuf.st_mode & S_IWGRP);
2019
TEST_FALSE (statbuf.st_mode & S_IXGRP);
2021
TEST_FALSE (statbuf.st_mode & S_IROTH);
2022
TEST_FALSE (statbuf.st_mode & S_IWOTH);
2023
TEST_FALSE (statbuf.st_mode & S_IXOTH);
2025
output = fopen (filename, "r");
2026
TEST_NE_P (output, NULL);
2028
TEST_FILE_EQ (output, "0+0 records in\r\n");
2029
TEST_FILE_EQ (output, "0+0 records out\r\n");
2030
TEST_FILE_MATCH (output, "0 bytes (0 B) copied,*\r\n");
2031
TEST_FILE_END (output);
2034
TEST_EQ (unlink (filename), 0);
2037
/************************************************************/
2038
TEST_FEATURE ("with multi-line script that writes 1 line to stderr");
2040
class = job_class_new (NULL, "test", NULL);
2041
TEST_NE_P (class, NULL);
2043
TEST_GT (sprintf (filename, "%s/test.log", dirname), 0);
2045
class->console = CONSOLE_LOG;
2046
class->process[PROCESS_MAIN] = process_new (class);
2047
class->process[PROCESS_MAIN]->command = nih_sprintf (
2048
class->process[PROCESS_MAIN],
2049
"/bin/true\n%s hello world >&2\n\n\n", TEST_CMD_ECHO);
2050
class->process[PROCESS_MAIN]->script = TRUE;
2052
job = job_new (class, "");
2053
job->goal = JOB_START;
2054
job->state = JOB_SPAWNED;
2056
ret = job_process_run (job, PROCESS_MAIN);
2059
TEST_NE (job->pid[PROCESS_MAIN], 0);
2061
TEST_FORCE_WATCH_UPDATE ();
2063
waitpid (job->pid[PROCESS_MAIN], &status, 0);
2064
TEST_TRUE (WIFEXITED (status));
2065
TEST_EQ (WEXITSTATUS (status), 0);
2067
TEST_FORCE_WATCH_UPDATE ();
2069
TEST_EQ (stat (filename, &statbuf), 0);
2071
TEST_TRUE (S_ISREG (statbuf.st_mode));
2073
TEST_TRUE (statbuf.st_mode & S_IRUSR);
2074
TEST_TRUE (statbuf.st_mode & S_IWUSR);
2075
TEST_FALSE (statbuf.st_mode & S_IXUSR);
2077
TEST_TRUE (statbuf.st_mode & S_IRGRP);
2078
TEST_FALSE (statbuf.st_mode & S_IWGRP);
2079
TEST_FALSE (statbuf.st_mode & S_IXGRP);
2081
TEST_FALSE (statbuf.st_mode & S_IROTH);
2082
TEST_FALSE (statbuf.st_mode & S_IWOTH);
2083
TEST_FALSE (statbuf.st_mode & S_IXOTH);
2085
output = fopen (filename, "r");
2086
TEST_NE_P (output, NULL);
2088
TEST_FILE_EQ (output, "hello world\r\n");
2089
TEST_FILE_END (output);
2092
TEST_EQ (unlink (filename), 0);
2095
/************************************************************/
2096
TEST_FEATURE ("with single-line script that writes >1 lines to stderr");
2098
class = job_class_new (NULL, "foo", NULL);
2099
TEST_NE_P (class, NULL);
2101
TEST_FILENAME (filename);
2102
TEST_GT (sprintf (filename, "%s/foo.log", dirname), 0);
2104
class->console = CONSOLE_LOG;
2105
class->process[PROCESS_MAIN] = process_new (class);
2106
class->process[PROCESS_MAIN]->command = nih_sprintf (
2107
class->process[PROCESS_MAIN],
2108
/* XXX: note the required quoting */
2109
"%s -ne \"hello\\nworld\\n\\n\\n\" >&2", TEST_CMD_ECHO);
2110
class->process[PROCESS_MAIN]->script = TRUE;
2112
job = job_new (class, "");
2113
job->goal = JOB_START;
2114
job->state = JOB_SPAWNED;
2116
ret = job_process_run (job, PROCESS_MAIN);
2119
TEST_NE (job->pid[PROCESS_MAIN], 0);
2121
waitpid (job->pid[PROCESS_MAIN], &status, 0);
2122
TEST_TRUE (WIFEXITED (status));
2123
TEST_EQ (WEXITSTATUS (status), 0);
2125
TEST_FORCE_WATCH_UPDATE ();
2127
TEST_EQ (stat (filename, &statbuf), 0);
2129
TEST_TRUE (S_ISREG (statbuf.st_mode));
2131
TEST_TRUE (statbuf.st_mode & S_IRUSR);
2132
TEST_TRUE (statbuf.st_mode & S_IWUSR);
2133
TEST_FALSE (statbuf.st_mode & S_IXUSR);
2135
TEST_TRUE (statbuf.st_mode & S_IRGRP);
2136
TEST_FALSE (statbuf.st_mode & S_IWGRP);
2137
TEST_FALSE (statbuf.st_mode & S_IXGRP);
2139
TEST_FALSE (statbuf.st_mode & S_IROTH);
2140
TEST_FALSE (statbuf.st_mode & S_IWOTH);
2141
TEST_FALSE (statbuf.st_mode & S_IXOTH);
2143
output = fopen (filename, "r");
2144
TEST_NE_P (output, NULL);
2146
/* Yup, pseudo-terminals record *everything*,
2147
* even the carriage returns.
2149
TEST_FILE_EQ (output, "hello\r\n");
2150
TEST_FILE_EQ (output, "world\r\n");
2151
TEST_FILE_EQ (output, "\r\n");
2152
TEST_FILE_EQ (output, "\r\n");
2153
TEST_FILE_END (output);
2156
TEST_EQ (unlink (filename), 0);
2159
/************************************************************/
2160
TEST_FEATURE ("with single-line command that writes >1 lines to stderr");
2162
class = job_class_new (NULL, "foo", NULL);
2163
TEST_NE_P (class, NULL);
2165
TEST_FILENAME (filename);
2166
TEST_GT (sprintf (filename, "%s/foo.log", dirname), 0);
2168
class->console = CONSOLE_LOG;
2169
class->process[PROCESS_MAIN] = process_new (class);
2170
class->process[PROCESS_MAIN]->command = nih_sprintf (
2171
class->process[PROCESS_MAIN],
2172
/* XXX: note the required quoting */
2173
"%s -ne \"hello world\\n\\n\\n\"", TEST_CMD_ECHO);
2174
class->process[PROCESS_MAIN]->script = FALSE;
2176
job = job_new (class, "");
2177
job->goal = JOB_START;
2178
job->state = JOB_SPAWNED;
2180
ret = job_process_run (job, PROCESS_MAIN);
2183
TEST_NE (job->pid[PROCESS_MAIN], 0);
2185
waitpid (job->pid[PROCESS_MAIN], &status, 0);
2186
TEST_TRUE (WIFEXITED (status));
2187
TEST_EQ (WEXITSTATUS (status), 0);
2189
TEST_FORCE_WATCH_UPDATE ();
2191
TEST_EQ (stat (filename, &statbuf), 0);
2193
TEST_TRUE (S_ISREG (statbuf.st_mode));
2195
TEST_TRUE (statbuf.st_mode & S_IRUSR);
2196
TEST_TRUE (statbuf.st_mode & S_IWUSR);
2197
TEST_FALSE (statbuf.st_mode & S_IXUSR);
2199
TEST_TRUE (statbuf.st_mode & S_IRGRP);
2200
TEST_FALSE (statbuf.st_mode & S_IWGRP);
2201
TEST_FALSE (statbuf.st_mode & S_IXGRP);
2203
TEST_FALSE (statbuf.st_mode & S_IROTH);
2204
TEST_FALSE (statbuf.st_mode & S_IWOTH);
2205
TEST_FALSE (statbuf.st_mode & S_IXOTH);
2207
output = fopen (filename, "r");
2208
TEST_NE_P (output, NULL);
2210
TEST_FILE_EQ (output, "hello world\r\n");
2211
TEST_FILE_EQ (output, "\r\n");
2212
TEST_FILE_EQ (output, "\r\n");
2213
TEST_FILE_END (output);
2216
TEST_EQ (unlink (filename), 0);
2219
/************************************************************/
2220
TEST_FEATURE ("with multi-line script that writes >1 lines to stderr");
2222
class = job_class_new (NULL, "elf", NULL);
2223
TEST_NE_P (class, NULL);
2225
TEST_FILENAME (filename);
2226
TEST_GT (sprintf (filename, "%s/elf.log", dirname), 0);
2228
class->console = CONSOLE_LOG;
2229
class->process[PROCESS_MAIN] = process_new (class);
2230
class->process[PROCESS_MAIN]->command = nih_sprintf (
2231
class->process[PROCESS_MAIN],
2232
/* XXX: note the required quoting */
2233
"/bin/true\n%s -ne \"hello world\\n\\n\" 1>&2\n\n\n", TEST_CMD_ECHO);
2234
class->process[PROCESS_MAIN]->script = TRUE;
2236
job = job_new (class, "");
2237
job->goal = JOB_START;
2238
job->state = JOB_SPAWNED;
2240
ret = job_process_run (job, PROCESS_MAIN);
2243
TEST_NE (job->pid[PROCESS_MAIN], 0);
2245
TEST_FORCE_WATCH_UPDATE ();
2247
waitpid (job->pid[PROCESS_MAIN], &status, 0);
2248
TEST_TRUE (WIFEXITED (status));
2249
TEST_EQ (WEXITSTATUS (status), 0);
2251
TEST_FORCE_WATCH_UPDATE ();
2253
TEST_EQ (stat (filename, &statbuf), 0);
2255
TEST_TRUE (S_ISREG (statbuf.st_mode));
2257
TEST_TRUE (statbuf.st_mode & S_IRUSR);
2258
TEST_TRUE (statbuf.st_mode & S_IWUSR);
2259
TEST_FALSE (statbuf.st_mode & S_IXUSR);
2261
TEST_TRUE (statbuf.st_mode & S_IRGRP);
2262
TEST_FALSE (statbuf.st_mode & S_IWGRP);
2263
TEST_FALSE (statbuf.st_mode & S_IXGRP);
2265
TEST_FALSE (statbuf.st_mode & S_IROTH);
2266
TEST_FALSE (statbuf.st_mode & S_IWOTH);
2267
TEST_FALSE (statbuf.st_mode & S_IXOTH);
2269
output = fopen (filename, "r");
2270
TEST_NE_P (output, NULL);
2272
TEST_FILE_EQ (output, "hello world\r\n");
2273
TEST_FILE_EQ (output, "\r\n");
2274
TEST_FILE_END (output);
2277
TEST_EQ (unlink (filename), 0);
2280
/************************************************************/
2281
TEST_FEATURE ("with single-line script that writes 1 line to stdout then 1 line to stderr");
2283
class = job_class_new (NULL, "blah", NULL);
2284
TEST_NE_P (class, NULL);
2286
TEST_FILENAME (filename);
2287
TEST_GT (sprintf (filename, "%s/blah.log", dirname), 0);
2289
class->console = CONSOLE_LOG;
2290
class->process[PROCESS_MAIN] = process_new (class);
2291
class->process[PROCESS_MAIN]->command = nih_sprintf (
2292
class->process[PROCESS_MAIN],
2293
/* XXX: note the required quoting */
2294
"%s stdout;%s stderr >&2",
2295
TEST_CMD_ECHO, TEST_CMD_ECHO);
2296
class->process[PROCESS_MAIN]->script = TRUE;
2298
job = job_new (class, "");
2299
job->goal = JOB_START;
2300
job->state = JOB_SPAWNED;
2302
ret = job_process_run (job, PROCESS_MAIN);
2305
TEST_NE (job->pid[PROCESS_MAIN], 0);
2307
waitpid (job->pid[PROCESS_MAIN], &status, 0);
2308
TEST_TRUE (WIFEXITED (status));
2309
TEST_EQ (WEXITSTATUS (status), 0);
2311
TEST_FORCE_WATCH_UPDATE ();
2313
TEST_EQ (stat (filename, &statbuf), 0);
2315
TEST_TRUE (S_ISREG (statbuf.st_mode));
2317
TEST_TRUE (statbuf.st_mode & S_IRUSR);
2318
TEST_TRUE (statbuf.st_mode & S_IWUSR);
2319
TEST_FALSE (statbuf.st_mode & S_IXUSR);
2321
TEST_TRUE (statbuf.st_mode & S_IRGRP);
2322
TEST_FALSE (statbuf.st_mode & S_IWGRP);
2323
TEST_FALSE (statbuf.st_mode & S_IXGRP);
2325
TEST_FALSE (statbuf.st_mode & S_IROTH);
2326
TEST_FALSE (statbuf.st_mode & S_IWOTH);
2327
TEST_FALSE (statbuf.st_mode & S_IXOTH);
2329
output = fopen (filename, "r");
2330
TEST_NE_P (output, NULL);
2332
TEST_FILE_EQ (output, "stdout\r\n");
2333
TEST_FILE_EQ (output, "stderr\r\n");
2334
TEST_FILE_END (output);
2337
TEST_EQ (unlink (filename), 0);
2340
/************************************************************/
2341
TEST_FEATURE ("with single-line script that writes 1 line to stderr then 1 line to stdout");
2343
class = job_class_new (NULL, "blah", NULL);
2344
TEST_NE_P (class, NULL);
2346
TEST_FILENAME (filename);
2347
TEST_GT (sprintf (filename, "%s/blah.log", dirname), 0);
2349
class->console = CONSOLE_LOG;
2350
class->process[PROCESS_MAIN] = process_new (class);
2351
class->process[PROCESS_MAIN]->command = nih_sprintf (
2352
class->process[PROCESS_MAIN],
2353
/* XXX: note the required quoting */
2354
"%s stderr >&2;%s stdout",
2355
TEST_CMD_ECHO, TEST_CMD_ECHO);
2356
class->process[PROCESS_MAIN]->script = TRUE;
2358
job = job_new (class, "");
2359
job->goal = JOB_START;
2360
job->state = JOB_SPAWNED;
2362
ret = job_process_run (job, PROCESS_MAIN);
2365
TEST_NE (job->pid[PROCESS_MAIN], 0);
2367
waitpid (job->pid[PROCESS_MAIN], &status, 0);
2368
TEST_TRUE (WIFEXITED (status));
2369
TEST_EQ (WEXITSTATUS (status), 0);
2371
TEST_FORCE_WATCH_UPDATE ();
2373
TEST_EQ (stat (filename, &statbuf), 0);
2375
TEST_TRUE (S_ISREG (statbuf.st_mode));
2377
TEST_TRUE (statbuf.st_mode & S_IRUSR);
2378
TEST_TRUE (statbuf.st_mode & S_IWUSR);
2379
TEST_FALSE (statbuf.st_mode & S_IXUSR);
2381
TEST_TRUE (statbuf.st_mode & S_IRGRP);
2382
TEST_FALSE (statbuf.st_mode & S_IWGRP);
2383
TEST_FALSE (statbuf.st_mode & S_IXGRP);
2385
TEST_FALSE (statbuf.st_mode & S_IROTH);
2386
TEST_FALSE (statbuf.st_mode & S_IWOTH);
2387
TEST_FALSE (statbuf.st_mode & S_IXOTH);
2389
output = fopen (filename, "r");
2390
TEST_NE_P (output, NULL);
2392
TEST_FILE_EQ (output, "stderr\r\n");
2393
TEST_FILE_EQ (output, "stdout\r\n");
2394
TEST_FILE_END (output);
2397
TEST_EQ (unlink (filename), 0);
2400
/************************************************************/
2401
TEST_FEATURE ("with single-line command that writes to stdout and stderr");
2403
class = job_class_new (NULL, "blah", NULL);
2404
TEST_NE_P (class, NULL);
2406
TEST_FILENAME (filename);
2407
TEST_GT (sprintf (filename, "%s/blah.log", dirname), 0);
2409
class->console = CONSOLE_LOG;
2410
class->process[PROCESS_MAIN] = process_new (class);
2411
class->process[PROCESS_MAIN]->command = nih_sprintf (
2412
class->process[PROCESS_MAIN],
2413
"%s if=/dev/zero bs=1 count=7", TEST_CMD_DD);
2414
class->process[PROCESS_MAIN]->script = FALSE;
2416
job = job_new (class, "");
2417
job->goal = JOB_START;
2418
job->state = JOB_SPAWNED;
2420
ret = job_process_run (job, PROCESS_MAIN);
2423
TEST_NE (job->pid[PROCESS_MAIN], 0);
2425
waitpid (job->pid[PROCESS_MAIN], &status, 0);
2426
TEST_TRUE (WIFEXITED (status));
2427
TEST_EQ (WEXITSTATUS (status), 0);
2429
TEST_FORCE_WATCH_UPDATE ();
2431
TEST_EQ (stat (filename, &statbuf), 0);
2433
TEST_TRUE (S_ISREG (statbuf.st_mode));
2435
TEST_TRUE (statbuf.st_mode & S_IRUSR);
2436
TEST_TRUE (statbuf.st_mode & S_IWUSR);
2437
TEST_FALSE (statbuf.st_mode & S_IXUSR);
2439
TEST_TRUE (statbuf.st_mode & S_IRGRP);
2440
TEST_FALSE (statbuf.st_mode & S_IWGRP);
2441
TEST_FALSE (statbuf.st_mode & S_IXGRP);
2443
TEST_FALSE (statbuf.st_mode & S_IROTH);
2444
TEST_FALSE (statbuf.st_mode & S_IWOTH);
2445
TEST_FALSE (statbuf.st_mode & S_IXOTH);
2447
output = fopen (filename, "r");
2448
TEST_NE_P (output, NULL);
2450
TEST_NE_P (fgets (filebuf, sizeof(filebuf), output), NULL);
2451
TEST_EQ (memcmp (filebuf, "\000\000\000\000\000\000", 7), 0);
2453
TEST_EQ_STR (p, "7+0 records in\r\n");
2455
TEST_FILE_EQ (output, "7+0 records out\r\n");
2456
TEST_FILE_MATCH (output, "7 bytes (7 B) copied,*\r\n");
2457
TEST_FILE_END (output);
2460
TEST_EQ (unlink (filename), 0);
2463
/************************************************************/
2464
TEST_FEATURE ("with single-line script running an invalid command");
2466
class = job_class_new (NULL, "blah", NULL);
2467
TEST_NE_P (class, NULL);
2469
TEST_FILENAME (filename);
2470
TEST_GT (sprintf (filename, "%s/blah.log", dirname), 0);
2472
class->console = CONSOLE_LOG;
2473
class->process[PROCESS_MAIN] = process_new (class);
2474
class->process[PROCESS_MAIN]->command = nih_strdup (
2475
class->process[PROCESS_MAIN],
2476
"/this/command/does/not/exist");
2477
class->process[PROCESS_MAIN]->script = TRUE;
2479
job = job_new (class, "");
2480
job->goal = JOB_START;
2481
job->state = JOB_SPAWNED;
2483
ret = job_process_run (job, PROCESS_MAIN);
2486
TEST_NE (job->pid[PROCESS_MAIN], 0);
2488
waitpid (job->pid[PROCESS_MAIN], &status, 0);
2489
TEST_TRUE (WIFEXITED (status));
2490
TEST_NE (WEXITSTATUS (status), 0);
2492
TEST_FORCE_WATCH_UPDATE ();
2494
TEST_EQ (stat (filename, &statbuf), 0);
2496
TEST_TRUE (S_ISREG (statbuf.st_mode));
2498
TEST_TRUE (statbuf.st_mode & S_IRUSR);
2499
TEST_TRUE (statbuf.st_mode & S_IWUSR);
2500
TEST_FALSE (statbuf.st_mode & S_IXUSR);
2502
TEST_TRUE (statbuf.st_mode & S_IRGRP);
2503
TEST_FALSE (statbuf.st_mode & S_IWGRP);
2504
TEST_FALSE (statbuf.st_mode & S_IXGRP);
2506
TEST_FALSE (statbuf.st_mode & S_IROTH);
2507
TEST_FALSE (statbuf.st_mode & S_IWOTH);
2508
TEST_FALSE (statbuf.st_mode & S_IXOTH);
2510
output = fopen (filename, "r");
2511
TEST_NE_P (output, NULL);
2513
TEST_FILE_EQ (output, "/bin/sh: 1: /this/command/does/not/exist: not found\r\n");
2514
TEST_FILE_END (output);
2517
TEST_EQ (unlink (filename), 0);
2520
/************************************************************
2521
* No point in running a test for:
2523
* TEST_FEATURE ("with single-line command running an invalid command");
2525
* Since as such commands are exec'ed directly, there is no shell to report
2526
* an error back - exec just fails.
2528
************************************************************/
2530
/************************************************************/
2531
TEST_FEATURE ("with multi-line script running an invalid command");
2533
class = job_class_new (NULL, "blah", NULL);
2534
TEST_NE_P (class, NULL);
2536
TEST_FILENAME (filename);
2537
TEST_GT (sprintf (filename, "%s/blah.log", dirname), 0);
2539
class->console = CONSOLE_LOG;
2540
class->process[PROCESS_MAIN] = process_new (class);
2541
class->process[PROCESS_MAIN]->command = nih_strdup (
2542
class->process[PROCESS_MAIN],
2543
"true\n/this/command/does/not/exist");
2544
class->process[PROCESS_MAIN]->script = TRUE;
2546
job = job_new (class, "");
2547
job->goal = JOB_START;
2548
job->state = JOB_SPAWNED;
2550
ret = job_process_run (job, PROCESS_MAIN);
2553
TEST_NE (job->pid[PROCESS_MAIN], 0);
2555
TEST_FORCE_WATCH_UPDATE ();
2556
waitpid (job->pid[PROCESS_MAIN], &status, 0);
2557
TEST_TRUE (WIFEXITED (status));
2558
TEST_NE (WEXITSTATUS (status), 0);
2559
TEST_FORCE_WATCH_UPDATE ();
2561
TEST_EQ (stat (filename, &statbuf), 0);
2563
TEST_TRUE (S_ISREG (statbuf.st_mode));
2565
TEST_TRUE (statbuf.st_mode & S_IRUSR);
2566
TEST_TRUE (statbuf.st_mode & S_IWUSR);
2567
TEST_FALSE (statbuf.st_mode & S_IXUSR);
2569
TEST_TRUE (statbuf.st_mode & S_IRGRP);
2570
TEST_FALSE (statbuf.st_mode & S_IWGRP);
2571
TEST_FALSE (statbuf.st_mode & S_IXGRP);
2573
TEST_FALSE (statbuf.st_mode & S_IROTH);
2574
TEST_FALSE (statbuf.st_mode & S_IWOTH);
2575
TEST_FALSE (statbuf.st_mode & S_IXOTH);
2577
output = fopen (filename, "r");
2578
TEST_NE_P (output, NULL);
2580
TEST_FILE_EQ (output, "/proc/self/fd/9: 3: /this/command/does/not/exist: not found\r\n");
2581
TEST_FILE_END (output);
2584
TEST_EQ (unlink (filename), 0);
2587
/************************************************************/
2588
TEST_FEATURE ("with multi-line script that writes 1 line to stdout then 1 line to stderr");
2590
class = job_class_new (NULL, "blah", NULL);
2591
TEST_NE_P (class, NULL);
2593
TEST_FILENAME (filename);
2594
TEST_GT (sprintf (filename, "%s/blah.log", dirname), 0);
2596
class->console = CONSOLE_LOG;
2597
class->process[PROCESS_MAIN] = process_new (class);
2598
class->process[PROCESS_MAIN]->command = nih_sprintf (
2599
class->process[PROCESS_MAIN],
2600
"%s stdout\n%s stderr >&2\n",
2601
TEST_CMD_ECHO, TEST_CMD_ECHO);
2602
class->process[PROCESS_MAIN]->script = TRUE;
2604
job = job_new (class, "");
2605
job->goal = JOB_START;
2606
job->state = JOB_SPAWNED;
2608
ret = job_process_run (job, PROCESS_MAIN);
2611
TEST_NE (job->pid[PROCESS_MAIN], 0);
2613
TEST_FORCE_WATCH_UPDATE ();
2615
waitpid (job->pid[PROCESS_MAIN], &status, 0);
2616
TEST_TRUE (WIFEXITED (status));
2617
TEST_EQ (WEXITSTATUS (status), 0);
2619
TEST_FORCE_WATCH_UPDATE ();
2621
TEST_EQ (stat (filename, &statbuf), 0);
2623
TEST_TRUE (S_ISREG (statbuf.st_mode));
2625
TEST_TRUE (statbuf.st_mode & S_IRUSR);
2626
TEST_TRUE (statbuf.st_mode & S_IWUSR);
2627
TEST_FALSE (statbuf.st_mode & S_IXUSR);
2629
TEST_TRUE (statbuf.st_mode & S_IRGRP);
2630
TEST_FALSE (statbuf.st_mode & S_IWGRP);
2631
TEST_FALSE (statbuf.st_mode & S_IXGRP);
2633
TEST_FALSE (statbuf.st_mode & S_IROTH);
2634
TEST_FALSE (statbuf.st_mode & S_IWOTH);
2635
TEST_FALSE (statbuf.st_mode & S_IXOTH);
2637
output = fopen (filename, "r");
2638
TEST_NE_P (output, NULL);
2640
TEST_FILE_EQ (output, "stdout\r\n");
2641
TEST_FILE_EQ (output, "stderr\r\n");
2642
TEST_FILE_END (output);
2645
TEST_EQ (unlink (filename), 0);
2648
/************************************************************/
2649
TEST_FEATURE ("with multi-line script that writes 1 line to stderr then 1 line to stdout");
2651
class = job_class_new (NULL, "blah", NULL);
2652
TEST_NE_P (class, NULL);
2654
TEST_FILENAME (filename);
2655
TEST_GT (sprintf (filename, "%s/blah.log", dirname), 0);
2657
class->console = CONSOLE_LOG;
2658
class->process[PROCESS_MAIN] = process_new (class);
2659
class->process[PROCESS_MAIN]->command = nih_sprintf (
2660
class->process[PROCESS_MAIN],
2661
"%s stderr >&2\n%s stdout\n",
2662
TEST_CMD_ECHO, TEST_CMD_ECHO);
2663
class->process[PROCESS_MAIN]->script = TRUE;
2665
job = job_new (class, "");
2666
job->goal = JOB_START;
2667
job->state = JOB_SPAWNED;
2669
ret = job_process_run (job, PROCESS_MAIN);
2672
TEST_NE (job->pid[PROCESS_MAIN], 0);
2674
TEST_FORCE_WATCH_UPDATE ();
2676
waitpid (job->pid[PROCESS_MAIN], &status, 0);
2677
TEST_TRUE (WIFEXITED (status));
2678
TEST_EQ (WEXITSTATUS (status), 0);
2680
TEST_FORCE_WATCH_UPDATE ();
2682
TEST_EQ (stat (filename, &statbuf), 0);
2684
TEST_TRUE (S_ISREG (statbuf.st_mode));
2686
TEST_TRUE (statbuf.st_mode & S_IRUSR);
2687
TEST_TRUE (statbuf.st_mode & S_IWUSR);
2688
TEST_FALSE (statbuf.st_mode & S_IXUSR);
2690
TEST_TRUE (statbuf.st_mode & S_IRGRP);
2691
TEST_FALSE (statbuf.st_mode & S_IWGRP);
2692
TEST_FALSE (statbuf.st_mode & S_IXGRP);
2694
TEST_FALSE (statbuf.st_mode & S_IROTH);
2695
TEST_FALSE (statbuf.st_mode & S_IWOTH);
2696
TEST_FALSE (statbuf.st_mode & S_IXOTH);
2698
output = fopen (filename, "r");
2699
TEST_NE_P (output, NULL);
2701
TEST_FILE_EQ (output, "stderr\r\n");
2702
TEST_FILE_EQ (output, "stdout\r\n");
2703
TEST_FILE_END (output);
2706
TEST_EQ (unlink (filename), 0);
2709
/* Check that we can succesfully setuid and setgid to
2710
* ourselves. This should always work, privileged or
2713
TEST_FEATURE ("with setuid me");
2716
class = job_class_new (NULL, "test", NULL);
2717
class->process[PROCESS_MAIN] = process_new (class);
2718
class->process[PROCESS_MAIN]->command = nih_sprintf (
2719
class->process[PROCESS_MAIN],
2720
"touch %s", filename);
2722
pwd = getpwuid (getuid ());
2723
TEST_NE (pwd, NULL);
2724
class->setuid = nih_strdup (class, pwd->pw_name);
2726
grp = getgrgid (getgid ());
2727
TEST_NE (grp, NULL);
2728
class->setuid = nih_strdup (class, grp->gr_name);
2730
job = job_new (class, "");
2731
job->goal = JOB_START;
2732
job->state = JOB_SPAWNED;
2735
ret = job_process_run (job, PROCESS_MAIN);
2738
TEST_NE (job->pid[PROCESS_MAIN], 0);
2740
waitpid (job->pid[PROCESS_MAIN], NULL, 0);
2741
TEST_EQ (stat (filename, &statbuf), 0);
2747
/************************************************************/
2748
/* Final clean-up */
2750
TEST_EQ (rmdir (dirname), 0);
2751
TEST_EQ (unsetenv ("UPSTART_LOGDIR"), 0);
2756
* XXX: Note that none of these tests attempts to test with a Session
2757
* passed to job_class_new() since to do so would modify the home
2758
* directory of the user running these tests (BAD!!).
2760
* (Such tests are handled in the bundled test_user_sessions.sh script).
794
2763
test_spawn (void)
2765
FILE *output, *input;
797
2766
char function[PATH_MAX], filename[PATH_MAX];
2767
char dirname[PATH_MAX];
2768
char script[PATH_MAX];
799
char *env[3], *args[4];
2773
nih_local char **args_array = NULL;
800
2775
JobClass *class;
1033
3071
TEST_EQ (WEXITSTATUS (status), 0);
1035
3073
nih_free (class);
3075
/* Check that when the job process is execed that no unexpected
3076
* signals are blocked or ignored.
3078
TEST_FEATURE ("ensure sane signal state with no console");
3080
sprintf (function, "%d", TEST_SIGNALS);
3087
class = job_class_new (NULL, "test", NULL);
3088
class->console = CONSOLE_NONE;
3089
job = job_new (class, "");
3091
pid = job_process_spawn (job, args, NULL, FALSE, -1);
3094
waitpid (pid, NULL, 0);
3095
output = fopen (filename, "r");
3097
TEST_NE_P (output, NULL);
3100
unsigned long int value;
3102
/* No signals should be blocked */
3103
TEST_TRUE (fgets (filebuf, sizeof(filebuf), output));
3104
TEST_EQ (sscanf (filebuf, "SigBlk: %lx", &value), 1);
3105
TEST_EQ (value, 0x0);
3107
/* No signals should be ignored */
3108
TEST_TRUE (fgets (filebuf, sizeof(filebuf), output));
3109
TEST_EQ (sscanf (filebuf, "SigIgn: %lx", &value), 1);
3110
TEST_EQ (value, 0x0);
3112
TEST_FILE_END (output);
3120
TEST_FEATURE ("ensure sane signal state with log console");
3122
sprintf (function, "%d", TEST_SIGNALS);
3129
class = job_class_new (NULL, "test", NULL);
3130
class->console = CONSOLE_LOG;
3131
job = job_new (class, "");
3133
pid = job_process_spawn (job, args, NULL, FALSE, -1);
3136
waitpid (pid, NULL, 0);
3137
output = fopen (filename, "r");
3139
TEST_NE_P (output, NULL);
3142
unsigned long int value;
3144
/* No signals should be blocked */
3145
TEST_TRUE (fgets (filebuf, sizeof(filebuf), output));
3146
TEST_EQ (sscanf (filebuf, "SigBlk: %lx", &value), 1);
3147
TEST_EQ (value, 0x0);
3149
/* No signals should be ignored */
3150
TEST_TRUE (fgets (filebuf, sizeof(filebuf), output));
3151
TEST_EQ (sscanf (filebuf, "SigIgn: %lx", &value), 1);
3152
TEST_EQ (value, 0x0);
3154
TEST_FILE_END (output);
3162
/************************************************************/
3163
TEST_FEATURE ("simple test");
3165
TEST_FILENAME (dirname);
3167
TEST_EQ (mkdir (dirname, 0755), 0);
3169
TEST_EQ (setenv ("UPSTART_LOGDIR", dirname, 1), 0);
3172
class = job_class_new (NULL, "test", NULL);
3173
TEST_NE_P (class, NULL);
3175
TEST_GT (sprintf (filename, "%s/test.log", dirname), 0);
3176
job = job_new (class, "");
3177
TEST_NE_P (job, NULL);
3180
args_array = NIH_MUST (nih_str_array_new (NULL));
3182
TEST_FILENAME (script);
3183
input = fopen (script, "w");
3184
TEST_NE_P (input, NULL);
3185
TEST_GT (fprintf (input, "%s hello world\n", TEST_CMD_ECHO), 0);
3188
NIH_MUST (nih_str_array_add (&args_array, NULL, &argc, TEST_SHELL));
3189
NIH_MUST (nih_str_array_add (&args_array, NULL, &argc, TEST_SHELL_ARG));
3190
NIH_MUST (nih_str_array_add (&args_array, NULL, &argc, script));
3193
pid = job_process_spawn (job, args_array, NULL, FALSE, -1);
3195
if (test_alloc_failed) {
3196
err = nih_error_get ();
3197
TEST_NE_P (err, NULL);
3198
TEST_EQ (err->number, ENOMEM);
3206
/************************************************************/
3207
TEST_FEATURE ("with single-line script and 'console log'");
3209
/* Check that we can spawn a job and retrieve its output.
3212
TEST_FILENAME (dirname);
3214
TEST_EQ (mkdir (dirname, 0755), 0);
3216
/* Override default location to ensure job output goes to a
3217
* writeable location
3219
TEST_EQ (setenv ("UPSTART_LOGDIR", dirname, 1), 0);
3221
class = job_class_new (NULL, "test", NULL);
3222
TEST_NE_P (class, NULL);
3224
TEST_GT (sprintf (filename, "%s/test.log", dirname), 0);
3225
job = job_new (class, "");
3226
TEST_NE_P (job, NULL);
3229
args_array = NIH_MUST (nih_str_array_new (NULL));
3231
TEST_FILENAME (script);
3232
input = fopen (script, "w");
3233
TEST_NE_P (input, NULL);
3234
TEST_GT (fprintf (input, "%s hello world\n", TEST_CMD_ECHO), 0);
3237
NIH_MUST (nih_str_array_add (&args_array, NULL, &argc, TEST_SHELL));
3238
NIH_MUST (nih_str_array_add (&args_array, NULL, &argc, TEST_SHELL_ARG));
3239
NIH_MUST (nih_str_array_add (&args_array, NULL, &argc, script));
3241
pid = job_process_spawn (job, args_array, NULL, FALSE, -1);
3244
TEST_EQ (waitpid (pid, &status, 0), pid);
3245
TEST_TRUE (WIFEXITED (status));
3247
TEST_FORCE_WATCH_UPDATE ();
3249
output = fopen (filename, "r");
3250
TEST_NE_P (output, NULL);
3252
TEST_FILE_EQ (output, "hello world\r\n");
3253
TEST_FILE_END (output);
3255
TEST_EQ (fclose (output), 0);
3259
TEST_EQ (rmdir (dirname), 0);
3260
TEST_EQ (unsetenv ("UPSTART_LOGDIR"), 0);
3264
/************************************************************/
3265
TEST_FEATURE ("with multi-line script and 'console log'");
3267
/* Check that we can spawn a job and retrieve its output.
3269
TEST_FILENAME (dirname);
3271
TEST_EQ (mkdir (dirname, 0755), 0);
3273
TEST_EQ (setenv ("UPSTART_LOGDIR", dirname, 1), 0);
3275
class = job_class_new (NULL, "test", NULL);
3276
TEST_NE_P (class, NULL);
3278
TEST_GT (sprintf (filename, "%s/test.log", dirname), 0);
3279
job = job_new (class, "");
3280
TEST_NE_P (job, NULL);
3283
args_array = NIH_MUST (nih_str_array_new (NULL));
3285
TEST_FILENAME (script);
3286
input = fopen (script, "w");
3287
TEST_NE_P (input, NULL);
3288
TEST_GT (fprintf (input, "/bin/true\n%s hello world\n", TEST_CMD_ECHO), 0);
3291
NIH_MUST (nih_str_array_add (&args_array, NULL, &argc, TEST_SHELL));
3292
NIH_MUST (nih_str_array_add (&args_array, NULL, &argc, TEST_SHELL_ARG));
3293
NIH_MUST (nih_str_array_add (&args_array, NULL, &argc, script));
3295
pid = job_process_spawn (job, args_array, NULL, FALSE, -1);
3298
TEST_EQ (waitpid (pid, &status, 0), pid);
3299
TEST_TRUE (WIFEXITED (status));
3301
TEST_FORCE_WATCH_UPDATE ();
3303
output = fopen (filename, "r");
3304
TEST_NE_P (output, NULL);
3306
TEST_FILE_EQ (output, "hello world\r\n");
3307
TEST_FILE_END (output);
3309
TEST_EQ (fclose (output), 0);
3313
TEST_EQ (rmdir (dirname), 0);
3314
TEST_EQ (unsetenv ("UPSTART_LOGDIR"), 0);
3318
/************************************************************/
3319
TEST_FEATURE ("read single null byte with 'console log'");
3321
/* Check that we can spawn a job and read a single byte written
3325
TEST_FILENAME (dirname);
3327
TEST_EQ (mkdir (dirname, 0755), 0);
3329
TEST_EQ (setenv ("UPSTART_LOGDIR", dirname, 1), 0);
3331
class = job_class_new (NULL, "test", NULL);
3332
TEST_NE_P (class, NULL);
3334
TEST_GT (sprintf (filename, "%s/test.log", dirname), 0);
3335
job = job_new (class, "");
3336
TEST_NE_P (job, NULL);
3339
args_array = NIH_MUST (nih_str_array_new (NULL));
3341
NIH_MUST (nih_str_array_add (&args_array, NULL, &argc, TEST_CMD_ECHO));
3342
NIH_MUST (nih_str_array_add (&args_array, NULL, &argc, "-en"));
3343
NIH_MUST (nih_str_array_add (&args_array, NULL, &argc, "\\000"));
3345
pid = job_process_spawn (job, args_array, NULL, FALSE, -1);
3348
TEST_EQ (waitpid (pid, &status, 0), pid);
3349
TEST_TRUE (WIFEXITED (status));
3351
TEST_FORCE_WATCH_UPDATE ();
3353
output = fopen (filename, "r");
3354
TEST_NE_P (output, NULL);
3356
TEST_NE_P (fgets (filebuf, sizeof(filebuf), output), NULL);
3357
TEST_EQ (memcmp (filebuf, "\000", 1), 0);
3358
TEST_FILE_END (output);
3360
TEST_EQ (fclose (output), 0);
3364
TEST_EQ (rmdir (dirname), 0);
3365
TEST_EQ (unsetenv ("UPSTART_LOGDIR"), 0);
3369
/************************************************************/
3370
TEST_FEATURE ("read data from forked process");
3372
TEST_FILENAME (dirname);
3374
TEST_EQ (mkdir (dirname, 0755), 0);
3376
TEST_EQ (setenv ("UPSTART_LOGDIR", dirname, 1), 0);
3378
class = job_class_new (NULL, "test", NULL);
3379
TEST_NE_P (class, NULL);
3381
TEST_GT (sprintf (filename, "%s/test.log", dirname), 0);
3382
job = job_new (class, "");
3383
TEST_NE_P (job, NULL);
3385
sprintf (function, "%d", TEST_OUTPUT);
3388
sprintf (filebuf, "%d", 1);
3389
TEST_FILENAME (script);
3397
pid = job_process_spawn (job, args, NULL, FALSE, -1);
3400
TEST_EQ (waitpid (pid, &status, 0), pid);
3401
TEST_TRUE (WIFEXITED (status));
3403
TEST_FORCE_WATCH_UPDATE ();
3405
output = fopen (filename, "r");
3406
TEST_NE_P (output, NULL);
3408
TEST_FILE_EQ (output, "stdout\r\n");
3409
TEST_FILE_EQ (output, "stderr\r\n");
3410
TEST_FILE_END (output);
3412
TEST_EQ (fclose (output), 0);
3416
TEST_EQ (rmdir (dirname), 0);
3417
TEST_EQ (unsetenv ("UPSTART_LOGDIR"), 0);
3421
/************************************************************/
3422
TEST_FEATURE ("read data from daemon process");
3424
TEST_FILENAME (dirname);
3426
TEST_EQ (mkdir (dirname, 0755), 0);
3428
TEST_EQ (setenv ("UPSTART_LOGDIR", dirname, 1), 0);
3430
class = job_class_new (NULL, "test", NULL);
3431
TEST_NE_P (class, NULL);
3433
TEST_GT (sprintf (filename, "%s/test.log", dirname), 0);
3434
job = job_new (class, "");
3435
TEST_NE_P (job, NULL);
3437
sprintf (function, "%d", TEST_OUTPUT);
3440
sprintf (filebuf, "%d", 2);
3441
TEST_FILENAME (script);
3449
pid = job_process_spawn (job, args, NULL, FALSE, -1);
3452
TEST_EQ (waitpid (pid, &status, 0), pid);
3453
TEST_TRUE (WIFEXITED (status));
3455
TEST_FORCE_WATCH_UPDATE ();
3457
output = fopen (filename, "r");
3458
TEST_NE_P (output, NULL);
3460
TEST_FILE_EQ (output, "stdout\r\n");
3461
TEST_FILE_EQ (output, "stderr\r\n");
3462
TEST_FILE_END (output);
3464
TEST_EQ (fclose (output), 0);
3468
TEST_EQ (rmdir (dirname), 0);
3469
TEST_EQ (unsetenv ("UPSTART_LOGDIR"), 0);
3476
/************************************************************/
3477
TEST_FEATURE ("when no free ptys");
3481
struct rlimit rlimit;
3483
TEST_FILENAME (dirname);
3485
TEST_EQ (mkdir (dirname, 0755), 0);
3487
TEST_EQ (setenv ("UPSTART_LOGDIR", dirname, 1), 0);
3489
class = job_class_new (NULL, "test", NULL);
3490
TEST_NE_P (class, NULL);
3491
TEST_EQ (class->console, CONSOLE_LOG);
3493
TEST_GT (sprintf (filename, "%s/test.log", dirname), 0);
3494
job = job_new (class, "");
3495
TEST_NE_P (job, NULL);
3497
available_ptys = get_available_pty_count ();
3498
TEST_GT (available_ptys, 1);
3500
TEST_EQ (getrlimit (RLIMIT_NOFILE, &rlimit), 0);
3502
if ((unsigned int)rlimit.rlim_cur <= (unsigned int)available_ptys) {
3503
/* Since we do not run as root, we are unable to
3504
* raise our limit to allow us to consume all
3507
printf ("WARNING:\n");
3508
printf ("WARNING: Test not run as insufficient files available\n");
3509
printf ("WARNING: (%u available files for uid %d, need atleast %u)\n",
3510
(unsigned int)rlimit.rlim_cur,
3512
(unsigned int)available_ptys);
3513
printf ("WARNING:\n");
3514
printf ("WARNING: See limits(5).\n");
3515
printf ("WARNING:\n");
3517
/* Consume all free ptys */
3521
for (pty = 0; pty < available_ptys; ++pty) {
3522
ret = posix_openpt (O_RDWR | O_NOCTTY);
3528
pid = job_process_spawn (job, args, NULL, FALSE, -1);
3530
err = nih_error_get ();
3531
TEST_EQ (err->number, ENOMEM);
3538
TEST_FEATURE ("WARNING: FIXME: test 'when no free ptys' disabled due to kernel bug");
3542
TEST_EQ (unsetenv ("UPSTART_LOGDIR"), 0);
3546
test_log_path (void)
3548
JobClass *class = NULL;
3550
nih_local char *log_path = NULL;
3551
nih_local char *expected = NULL;
3552
nih_local char *home = NULL;
3553
char dirname[PATH_MAX];
3555
TEST_FILENAME (dirname);
3557
TEST_FUNCTION ("job_process_log_path");
3559
/************************************************************/
3560
TEST_FEATURE ("with system job with simple name");
3562
class = job_class_new (NULL, "system", NULL);
3563
TEST_NE_P (class, NULL);
3564
job = job_new (class, "");
3565
TEST_NE_P (job, NULL);
3567
log_path = job_process_log_path (job, FALSE);
3568
TEST_NE_P (log_path, NULL);
3570
expected = NIH_MUST (nih_sprintf (NULL, "%s/%s.log",
3571
EXPECTED_JOB_LOGDIR, "system"));
3572
TEST_EQ_STR (log_path, expected);
3575
/************************************************************/
3576
TEST_FEATURE ("with system job containing illegal path characters");
3578
class = job_class_new (NULL, "//hello_foo bar.z/", NULL);
3579
TEST_NE_P (class, NULL);
3580
job = job_new (class, "");
3581
TEST_NE_P (job, NULL);
3583
log_path = job_process_log_path (job, FALSE);
3584
TEST_NE_P (log_path, NULL);
3586
expected = NIH_MUST (nih_sprintf (NULL, "%s/%s.log",
3587
EXPECTED_JOB_LOGDIR, "__hello_foo bar.z_"));
3588
TEST_EQ_STR (log_path, expected);
3591
/************************************************************/
3592
TEST_FEATURE ("with system job with named instance");
3594
class = job_class_new (NULL, "foo bar", NULL);
3595
TEST_NE_P (class, NULL);
3596
job = job_new (class, "bar foo");
3597
TEST_NE_P (job, NULL);
3599
log_path = job_process_log_path (job, FALSE);
3600
TEST_NE_P (log_path, NULL);
3602
expected = NIH_MUST (nih_sprintf (NULL, "%s/%s.log",
3603
EXPECTED_JOB_LOGDIR, "foo bar-bar foo"));
3604
TEST_EQ_STR (log_path, expected);
3607
/************************************************************/
3608
TEST_FEATURE ("with system job with named instance and illegal path characters");
3610
class = job_class_new (NULL, "a/b", NULL);
3611
TEST_NE_P (class, NULL);
3612
job = job_new (class, "c/d_?/");
3613
TEST_NE_P (job, NULL);
3615
log_path = job_process_log_path (job, FALSE);
3616
TEST_NE_P (log_path, NULL);
3618
expected = NIH_MUST (nih_sprintf (NULL, "%s/%s.log",
3619
EXPECTED_JOB_LOGDIR, "a_b-c_d_?_"));
3620
TEST_EQ_STR (log_path, expected);
3623
/************************************************************/
3624
TEST_FEATURE ("with subverted logdir and system job with named instance and illegal path characters");
3626
TEST_EQ (setenv ("UPSTART_LOGDIR", dirname, 1), 0);
3628
class = job_class_new (NULL, "a/b", NULL);
3629
TEST_NE_P (class, NULL);
3630
job = job_new (class, "c/d_?/");
3631
TEST_NE_P (job, NULL);
3633
log_path = job_process_log_path (job, FALSE);
3634
TEST_NE_P (log_path, NULL);
3636
expected = NIH_MUST (nih_sprintf (NULL, "%s/%s.log",
3637
dirname, "a_b-c_d_?_"));
3638
TEST_EQ_STR (log_path, expected);
3641
TEST_EQ (unsetenv ("UPSTART_LOGDIR"), 0);