1033
1041
nih_free (session2);
1034
1042
nih_free (new_session1);
1035
1043
nih_free (new_session2);
1045
/*******************************/
1046
TEST_FEATURE ("Ensure session deserialisation does not create JobClasses");
1050
TEST_LIST_EMPTY (sessions);
1051
TEST_HASH_EMPTY (job_classes);
1052
TEST_LIST_EMPTY (conf_sources);
1054
old_perms = umask (0);
1056
TEST_FILENAME (chroot_path);
1057
assert0 (mkdir (chroot_path, 0755));
1059
path = NIH_MUST (nih_sprintf (NULL, "%s/etc", chroot_path));
1060
assert0 (mkdir (path, 0755));
1061
path = NIH_MUST (nih_sprintf (NULL, "%s/etc/init", chroot_path));
1062
assert0 (mkdir (path, 0755));
1064
CREATE_FILE (path, "foo.conf", "manual");
1066
session1 = session_new (NULL, chroot_path);
1067
TEST_NE_P (session1, NULL);
1068
session1->conf_path = NIH_MUST (nih_sprintf (session1, "%s//etc/init", chroot_path));
1069
TEST_LIST_NOT_EMPTY (sessions);
1071
TEST_HASH_EMPTY (job_classes);
1072
TEST_LIST_EMPTY (conf_sources);
1074
json = json_object_new_object ();
1075
TEST_NE_P (json, NULL);
1077
json_sessions = session_serialise_all ();
1078
TEST_NE_P (json_sessions, NULL);
1080
json_object_object_add (json, "sessions", json_sessions);
1082
/* Remove the session from the master list (but don't free it)
1084
nih_list_remove (&session1->entry);
1085
TEST_LIST_EMPTY (sessions);
1089
/* Convert the JSON back into Session objects */
1090
ret = session_deserialise_all (json);
1093
TEST_LIST_NOT_EMPTY (sessions);
1095
/* Ensure no ConfSources, ConfFiles or JobClasses were created
1096
* as part of the session deserialisation.
1098
TEST_HASH_EMPTY (job_classes);
1099
TEST_LIST_EMPTY (conf_sources);
1101
session2 = (Session *)nih_list_remove (sessions->next);
1102
assert0 (session_diff (session1, session2));
1107
json_object_put (json);
1109
DELETE_FILE (path, "foo.conf");
1111
path = NIH_MUST (nih_sprintf (NULL, "%s/etc/init", chroot_path));
1112
assert0 (rmdir (path));
1114
path = NIH_MUST (nih_sprintf (NULL, "%s/etc", chroot_path));
1115
assert0 (rmdir (path));
1117
assert0 (rmdir (chroot_path));
3400
* test_session_upgrade
3402
* @path: full path to JSON data file to deserialise.
3404
* Test for original Upstart 1.6 serialisation data format containing
3405
* a blocked object that does not contain a 'session' element.
3407
* Note that this test is NOT testing whether a JobClass with an
3408
* associated Upstart session is handled correctly, it is merely
3409
* testing that a JobClass with the NULL session encoded in the JSON
3410
* is handled correctly.
3413
test_session_upgrade (const char *path)
3415
nih_local char *json_string = NULL;
3416
struct stat statbuf;
3418
int got_tty1 = FALSE;
3428
TEST_LIST_EMPTY (sessions);
3429
TEST_LIST_EMPTY (events);
3430
TEST_LIST_EMPTY (conf_sources);
3431
TEST_HASH_EMPTY (job_classes);
3433
/* Check data file exists */
3434
TEST_EQ (stat (path, &statbuf), 0);
3436
json_string = nih_file_read (NULL, path, &len);
3437
TEST_NE_P (json_string, NULL);
3439
/* Recreate state from JSON data file */
3440
assert0 (state_from_string (json_string));
3442
TEST_LIST_NOT_EMPTY (conf_sources);
3443
TEST_LIST_NOT_EMPTY (events);
3444
TEST_HASH_NOT_EMPTY (job_classes);
3445
TEST_LIST_NOT_EMPTY (sessions);
3447
int session_count = 0;
3448
NIH_LIST_FOREACH (sessions, iter) {
3449
Session *session = (Session *)iter;
3450
TEST_EQ_STR (session->chroot, "/mnt");
3451
TEST_EQ_STR (session->conf_path, "/mnt/etc/init");
3454
TEST_EQ (session_count, 1);
3456
int source_types[3] = {0, 0, 0};
3458
NIH_LIST_FOREACH (conf_sources, iter) {
3459
ConfSource *source = (ConfSource *) iter;
3460
if (! source->session) {
3461
switch (source->type) {
3469
nih_assert_not_reached ();
3472
switch (source->type) {
3477
nih_assert_not_reached ();
3481
TEST_EQ (source_types[0], 1);
3482
TEST_EQ (source_types[1], 1);
3483
TEST_EQ (source_types[2], 1);
3485
NIH_HASH_FOREACH (job_classes, iter) {
3486
JobClass *class = (JobClass *)iter;
3487
TEST_EQ_P (class->session, NULL);
3488
if (! strcmp (class->name, "tty1"))
3492
/* XXX: The json contains 2 tty1 jobs: one in the NULL session,
3493
* and the other in the chroot session.
3495
* Make sure that duplicate job names (albeit in different sessions)
3496
* do not stop the NULL session job from being recreated.
3498
TEST_EQ (got_tty1, TRUE);
3500
nih_free (conf_sources);
3501
nih_free (job_classes);
3503
nih_free (sessions);
3505
conf_sources = NULL;
3518
* test_session_upgrade2
3520
* @path: full path to JSON data file that needs pre-processing.
3522
* XXX: @path contains multiple occurences of @CHROOT_PATH@ which must
3523
* be replaced by a valid temporary directory path before attempting
3526
* Test to ensure Upstart can deserialise a state file containing a
3527
* chroot session where that chroot path actually exists with jobs on disk.
3528
* This was added since that exact scenario caught a bug in an early fix
3529
* for LP:#1199778 (resulting in stateless re-exec).
3532
test_session_upgrade2 (const char *original_path)
3534
nih_local char *json_string = NULL;
3535
struct stat statbuf;
3537
int got_tty1 = FALSE;
3538
char chroot_path[PATH_MAX];
3539
nih_local char *path = NULL;
3540
nih_local char *file = NULL;
3541
nih_local char *processed_json = NULL;
3544
nih_assert (original_path);
3552
TEST_LIST_EMPTY (sessions);
3553
TEST_LIST_EMPTY (events);
3554
TEST_LIST_EMPTY (conf_sources);
3555
TEST_HASH_EMPTY (job_classes);
3557
/* Check data file exists */
3558
TEST_EQ (stat (original_path, &statbuf), 0);
3560
/* Read the original file */
3561
file = nih_file_read (NULL, original_path, &len);
3562
TEST_NE_P (file, NULL);
3564
old_perms = umask (0);
3566
TEST_FILENAME (chroot_path);
3567
assert0 (mkdir (chroot_path, 0755));
3569
path = NIH_MUST (nih_sprintf (NULL, "%s/etc", chroot_path));
3570
assert0 (mkdir (path, 0755));
3571
path = NIH_MUST (nih_sprintf (NULL, "%s/etc/init", chroot_path));
3572
assert0 (mkdir (path, 0755));
3574
/* Replace @CHROOT_PATH@ with our temporary path */
3575
processed_json = search_and_replace (NULL, file, "@CHROOT_PATH@", chroot_path);
3576
TEST_NE_P (processed_json, NULL);
3578
/* Create some jobs which are also specified in the original
3581
CREATE_FILE (path, "tty1.conf", "manual");
3582
CREATE_FILE (path, "tty2.conf", "manual");
3584
/* Recreate state from JSON data file */
3585
assert0 (state_from_string (processed_json));
3587
TEST_LIST_NOT_EMPTY (conf_sources);
3588
TEST_LIST_NOT_EMPTY (events);
3589
TEST_HASH_NOT_EMPTY (job_classes);
3590
TEST_LIST_NOT_EMPTY (sessions);
3592
int session_count = 0;
3593
NIH_LIST_FOREACH (sessions, iter) {
3594
Session *session = (Session *)iter;
3595
nih_local char *new_path = NULL;
3597
/* yes, there is a double-slash in this path */
3598
new_path = nih_sprintf (NULL, "%s/%s", chroot_path,
3601
TEST_EQ_STR (session->chroot, chroot_path);
3602
TEST_EQ_STR (session->conf_path, new_path);
3605
TEST_EQ (session_count, 1);
3607
int source_types[3] = {0, 0, 0};
3609
NIH_LIST_FOREACH (conf_sources, iter) {
3610
ConfSource *source = (ConfSource *) iter;
3612
if (! source->session) {
3613
switch (source->type) {
3621
nih_assert_not_reached ();
3624
switch (source->type) {
3629
nih_assert_not_reached ();
3633
TEST_EQ (source_types[0], 1);
3634
TEST_EQ (source_types[1], 1);
3635
TEST_EQ (source_types[2], 1);
3637
NIH_HASH_FOREACH (job_classes, iter) {
3638
JobClass *class = (JobClass *)iter;
3640
TEST_EQ_P (class->session, NULL);
3641
if (! strcmp (class->name, "tty1"))
3645
/* XXX: The json contains 2 tty1 jobs: one in the NULL session,
3646
* and the other in the chroot session.
3648
* Make sure that duplicate job names (albeit in different sessions)
3649
* do not stop the NULL session job from being recreated.
3651
TEST_EQ (got_tty1, TRUE);
3653
nih_free (conf_sources);
3654
nih_free (job_classes);
3656
nih_free (sessions);
3658
conf_sources = NULL;
3668
DELETE_FILE (path, "tty1.conf");
3669
DELETE_FILE (path, "tty2.conf");
3671
path = NIH_MUST (nih_sprintf (NULL, "%s/etc/init", chroot_path));
3672
assert0 (rmdir (path));
3674
path = NIH_MUST (nih_sprintf (NULL, "%s/etc", chroot_path));
3675
assert0 (rmdir (path));
3677
assert0 (rmdir (chroot_path));
3315
3684
* test_upstart1_8_upgrade:
3317
3686
* @path: full path to JSON data file to deserialise.