~jamesodhunt/upstart/cgroups-and-process_data-reexec-test

« back to all changes in this revision

Viewing changes to init/tests/test_state.c

  • Committer: James Hunt
  • Date: 2014-07-02 16:29:13 UTC
  • Revision ID: james.hunt@ubuntu.com-20140702162913-djhuxeu8meexjnew
* init/tests/data/upstart-1.13.json: Updated with new content containing
  both more cgroup stanza jobs and a job with a job process whose
  child setup is "in-flight", representing the JobProcessData in the
  JSON as 'process_data'.
* init/tests/test_state.c: test_cgroup_and_process_data_state():
  - Renamed from test_cgroup_state().
  - Made cgroup guards more fine-grainded to ensure this test always
    runs, but only tests cgroup content if built with cgroup support.
  - Pre-process the JSON to pass a valid fd to allow the deserialisation
    of the JobProcessData to succeed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
165
165
void test_upstart_full_serialise_with_apparmor_upgrade (const char *path);
166
166
void test_reload_signal_state (const char *path);
167
167
void test_job_environ_upgrade (const char *path);
168
 
 
169
 
#ifdef ENABLE_CGROUPS
170
 
void test_cgroup_state (const char *path);
171
 
#endif /* ENABLE_CGROUPS */
 
168
void test_cgroup_and_process_data_state (const char *path);
172
169
 
173
170
ConfSource * conf_source_from_path (const char *path,
174
171
                                    ConfSourceType type,
215
212
        { "upstart-session-infinity.json", test_session_upgrade_stale },
216
213
        { "upstart-1.9.json", test_reload_signal_state },
217
214
        { "upstart-1.11.json", test_job_environ_upgrade },
218
 
 
219
 
#ifdef ENABLE_CGROUPS
220
 
        { "upstart-1.13.json", test_cgroup_state },
221
 
#endif /* ENABLE_CGROUPS */
 
215
        { "upstart-1.13.json", test_cgroup_and_process_data_state },
222
216
 
223
217
        { NULL, NULL }
224
218
};
4567
4561
        session_init ();
4568
4562
}
4569
4563
 
4570
 
#ifdef ENABLE_CGROUPS
4571
 
 
4572
4564
/**
4573
 
 * test_cgroup_state:
 
4565
 * test_cgroup_and_process_data_state:
4574
4566
 *
4575
4567
 * @path: full path to JSON data file to deserialise.
4576
4568
 *
4577
 
 * Test that Upstart can deserialise both cgroup stanzas and the cgroup
4578
 
 * manager address.
 
4569
 * Test that Upstart can deserialise:
 
4570
 *
 
4571
 * - cgroup stanzas (if cgroups enabled).
 
4572
 * - the cgroup manager address (if cgroups enabled).
 
4573
 * - JobProcessData encoded as "process_data" in JSON.
4579
4574
 **/
4580
4575
void
4581
 
test_cgroup_state (const char *path)
 
4576
test_cgroup_and_process_data_state (const char *path)
4582
4577
{
 
4578
#ifdef ENABLE_CGROUPS
4583
4579
        nih_local char   *cgroup_address = NULL;
4584
4580
        const char       *address;
 
4581
        json_object      *json_value = NULL;
 
4582
#endif /* ENABLE_CGROUPS */
 
4583
 
4585
4584
        nih_local char   *json_string = NULL;
 
4585
        nih_local char   *processed_json_string = NULL;
4586
4586
        json_object      *json = NULL;
4587
 
        json_object      *json_value = NULL;
4588
4587
        struct stat       statbuf;
4589
4588
        size_t            len;
 
4589
        int               fds[2] = { -1, -1 };
 
4590
        nih_local char   *read_fd_str = NULL;
4590
4591
 
4591
4592
        nih_assert (path);
4592
4593
 
4601
4602
        TEST_LIST_EMPTY (conf_sources);
4602
4603
        TEST_HASH_EMPTY (job_classes);
4603
4604
 
 
4605
#ifdef ENABLE_CGROUPS
4604
4606
        cgroup_manager_address = NULL;
 
4607
#endif /* ENABLE_CGROUPS */
4605
4608
 
4606
4609
        /* Check data file exists */
4607
4610
        TEST_EQ (stat (path, &statbuf), 0);
4609
4612
        json_string = nih_file_read (NULL, path, &len);
4610
4613
        TEST_NE_P (json_string, NULL);
4611
4614
 
 
4615
        /* The JSON contains a "process_data" object that represents a
 
4616
         * JobProcessData object denoting a job process whose child
 
4617
         * setup phase is still "in-flight" (ongoing).
 
4618
         *
 
4619
         * The problem is that when the JobProcessData object is
 
4620
         * reconstructed, Upstart will attempt to operate on the
 
4621
         * job_process_fd file descriptor so that fd must be valid at
 
4622
         * the time the test is run.
 
4623
         *
 
4624
         * The solution is to create a valid fd and rewrite the JSON to
 
4625
         * specify that valid fd.
 
4626
         */
 
4627
        assert0 (pipe (fds));
 
4628
 
 
4629
        read_fd_str = nih_sprintf (NULL, "%d", fds[0]);
 
4630
        TEST_NE_P (read_fd_str, NULL);
 
4631
 
 
4632
        /* Replace @CHROOT_PATH@ with our temporary path */
 
4633
        processed_json_string = search_and_replace (NULL, json_string,
 
4634
                        "@JOB_PROCESS_FD@",
 
4635
                        read_fd_str);
 
4636
        TEST_NE_P (processed_json_string, NULL);
 
4637
 
4612
4638
        /* Read the json, checking for expected content */
4613
 
        json = json_object_from_file (path);
 
4639
        json = json_tokener_parse (processed_json_string);
4614
4640
        TEST_NE_P (json, NULL);
4615
4641
 
 
4642
#ifdef ENABLE_CGROUPS
4616
4643
        /* Ensure it's there */
4617
4644
        TEST_TRUE (json_object_object_get_ex (json, "cgroup_manager_address", &json_value));
4618
4645
 
4622
4649
 
4623
4650
        /* free the JSON */
4624
4651
        json_object_put (json);
 
4652
#endif /* ENABLE_CGROUPS */
4625
4653
 
4626
4654
        /* Recreate state from JSON data file */
4627
 
        assert0 (state_from_string (json_string));
 
4655
        assert0 (state_from_string (processed_json_string));
4628
4656
 
 
4657
#ifdef ENABLE_CGROUPS
4629
4658
        TEST_NE_P (cgroup_manager_address, NULL);
4630
4659
        TEST_EQ_STR (cgroup_manager_address, cgroup_address);
 
4660
#endif /* ENABLE_CGROUPS */
4631
4661
 
4632
4662
        TEST_LIST_EMPTY (sessions);
4633
4663
        TEST_LIST_NOT_EMPTY (events);
4644
4674
        events = NULL;
4645
4675
        sessions = NULL;
4646
4676
 
 
4677
        close (fds[0]);
 
4678
        close (fds[1]);
 
4679
 
4647
4680
        conf_init ();
4648
4681
        job_class_init ();
4649
4682
        event_init ();
4650
4683
        session_init ();
4651
4684
}
4652
4685
 
4653
 
#endif /* ENABLE_CGROUPS */
4654
 
 
4655
4686
/**
4656
4687
 * conf_source_from_path:
4657
4688
 *