Branch data Line data Source code
1 : : /* SPDX-License-Identifier: LGPL-2.1+ */
2 : :
3 : : #include <errno.h>
4 : : #include <stdio.h>
5 : : #include <string.h>
6 : :
7 : : #include "bus-util.h"
8 : : #include "manager.h"
9 : : #include "rm-rf.h"
10 : : #include "strv.h"
11 : : #include "test-helper.h"
12 : : #include "tests.h"
13 : : #include "service.h"
14 : :
15 : 4 : int main(int argc, char *argv[]) {
16 : 4 : _cleanup_(rm_rf_physical_and_freep) char *runtime_dir = NULL;
17 : 4 : _cleanup_(sd_bus_error_free) sd_bus_error err = SD_BUS_ERROR_NULL;
18 : 4 : _cleanup_(manager_freep) Manager *m = NULL;
19 : 4 : Unit *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL, *g = NULL,
20 : 4 : *h = NULL, *i = NULL, *a_conj = NULL, *unit_with_multiple_dashes = NULL;
21 : : Job *j;
22 : : int r;
23 : :
24 : 4 : test_setup_logging(LOG_DEBUG);
25 : :
26 : 4 : r = enter_cgroup_subroot();
27 [ - + ]: 4 : if (r == -ENOMEDIUM)
28 : 0 : return log_tests_skipped("cgroupfs not available");
29 : :
30 : : /* prepare the test */
31 [ - + ]: 4 : assert_se(set_unit_path(get_testdata_dir()) >= 0);
32 [ - + ]: 4 : assert_se(runtime_dir = setup_fake_runtime_dir());
33 : 4 : r = manager_new(UNIT_FILE_USER, MANAGER_TEST_RUN_BASIC, &m);
34 [ - + - + ]: 4 : if (MANAGER_SKIP_TEST(r))
35 : 0 : return log_tests_skipped_errno(r, "manager_new");
36 [ - + ]: 4 : assert_se(r >= 0);
37 [ - + ]: 4 : assert_se(manager_startup(m, NULL, NULL) >= 0);
38 : :
39 : 4 : printf("Load1:\n");
40 [ - + ]: 4 : assert_se(manager_load_startable_unit_or_warn(m, "a.service", NULL, &a) >= 0);
41 [ - + ]: 4 : assert_se(manager_load_startable_unit_or_warn(m, "b.service", NULL, &b) >= 0);
42 [ - + ]: 4 : assert_se(manager_load_startable_unit_or_warn(m, "c.service", NULL, &c) >= 0);
43 : 4 : manager_dump_units(m, stdout, "\t");
44 : :
45 : 4 : printf("Test1: (Trivial)\n");
46 : 4 : r = manager_add_job(m, JOB_START, c, JOB_REPLACE, NULL, &err, &j);
47 [ - + ]: 4 : if (sd_bus_error_is_set(&err))
48 [ # # ]: 0 : log_error("error: %s: %s", err.name, err.message);
49 [ - + ]: 4 : assert_se(r == 0);
50 : 4 : manager_dump_jobs(m, stdout, "\t");
51 : :
52 : 4 : printf("Load2:\n");
53 : 4 : manager_clear_jobs(m);
54 [ - + ]: 4 : assert_se(manager_load_startable_unit_or_warn(m, "d.service", NULL, &d) >= 0);
55 [ - + ]: 4 : assert_se(manager_load_startable_unit_or_warn(m, "e.service", NULL, &e) >= 0);
56 : 4 : manager_dump_units(m, stdout, "\t");
57 : :
58 : 4 : printf("Test2: (Cyclic Order, Unfixable)\n");
59 [ - + ]: 4 : assert_se(manager_add_job(m, JOB_START, d, JOB_REPLACE, NULL, NULL, &j) == -EDEADLK);
60 : 4 : manager_dump_jobs(m, stdout, "\t");
61 : :
62 : 4 : printf("Test3: (Cyclic Order, Fixable, Garbage Collector)\n");
63 [ - + ]: 4 : assert_se(manager_add_job(m, JOB_START, e, JOB_REPLACE, NULL, NULL, &j) == 0);
64 : 4 : manager_dump_jobs(m, stdout, "\t");
65 : :
66 : 4 : printf("Test4: (Identical transaction)\n");
67 [ - + ]: 4 : assert_se(manager_add_job(m, JOB_START, e, JOB_FAIL, NULL, NULL, &j) == 0);
68 : 4 : manager_dump_jobs(m, stdout, "\t");
69 : :
70 : 4 : printf("Load3:\n");
71 [ - + ]: 4 : assert_se(manager_load_startable_unit_or_warn(m, "g.service", NULL, &g) >= 0);
72 : 4 : manager_dump_units(m, stdout, "\t");
73 : :
74 : 4 : printf("Test5: (Colliding transaction, fail)\n");
75 [ - + ]: 4 : assert_se(manager_add_job(m, JOB_START, g, JOB_FAIL, NULL, NULL, &j) == -EDEADLK);
76 : :
77 : 4 : printf("Test6: (Colliding transaction, replace)\n");
78 [ - + ]: 4 : assert_se(manager_add_job(m, JOB_START, g, JOB_REPLACE, NULL, NULL, &j) == 0);
79 : 4 : manager_dump_jobs(m, stdout, "\t");
80 : :
81 : 4 : printf("Test7: (Unmergeable job type, fail)\n");
82 [ - + ]: 4 : assert_se(manager_add_job(m, JOB_STOP, g, JOB_FAIL, NULL, NULL, &j) == -EDEADLK);
83 : :
84 : 4 : printf("Test8: (Mergeable job type, fail)\n");
85 [ - + ]: 4 : assert_se(manager_add_job(m, JOB_RESTART, g, JOB_FAIL, NULL, NULL, &j) == 0);
86 : 4 : manager_dump_jobs(m, stdout, "\t");
87 : :
88 : 4 : printf("Test9: (Unmergeable job type, replace)\n");
89 [ - + ]: 4 : assert_se(manager_add_job(m, JOB_STOP, g, JOB_REPLACE, NULL, NULL, &j) == 0);
90 : 4 : manager_dump_jobs(m, stdout, "\t");
91 : :
92 : 4 : printf("Load4:\n");
93 [ - + ]: 4 : assert_se(manager_load_startable_unit_or_warn(m, "h.service", NULL, &h) >= 0);
94 : 4 : manager_dump_units(m, stdout, "\t");
95 : :
96 : 4 : printf("Test10: (Unmergeable job type of auxiliary job, fail)\n");
97 [ - + ]: 4 : assert_se(manager_add_job(m, JOB_START, h, JOB_FAIL, NULL, NULL, &j) == 0);
98 : 4 : manager_dump_jobs(m, stdout, "\t");
99 : :
100 : 4 : printf("Load5:\n");
101 : 4 : manager_clear_jobs(m);
102 [ - + ]: 4 : assert_se(manager_load_startable_unit_or_warn(m, "i.service", NULL, &i) >= 0);
103 : 4 : SERVICE(a)->state = SERVICE_RUNNING;
104 : 4 : SERVICE(d)->state = SERVICE_RUNNING;
105 : 4 : manager_dump_units(m, stdout, "\t");
106 : :
107 : 4 : printf("Test11: (Start/stop job ordering, execution cycle)\n");
108 [ - + ]: 4 : assert_se(manager_add_job(m, JOB_START, i, JOB_FAIL, NULL, NULL, &j) == 0);
109 [ + - - + ]: 4 : assert_se(a->job && a->job->type == JOB_STOP);
110 [ + - - + ]: 4 : assert_se(d->job && d->job->type == JOB_STOP);
111 [ + - - + ]: 4 : assert_se(b->job && b->job->type == JOB_START);
112 : 4 : manager_dump_jobs(m, stdout, "\t");
113 : :
114 : 4 : printf("Load6:\n");
115 : 4 : manager_clear_jobs(m);
116 [ - + ]: 4 : assert_se(manager_load_startable_unit_or_warn(m, "a-conj.service", NULL, &a_conj) >= 0);
117 : 4 : SERVICE(a)->state = SERVICE_DEAD;
118 : 4 : manager_dump_units(m, stdout, "\t");
119 : :
120 : 4 : printf("Test12: (Trivial cycle, Unfixable)\n");
121 [ - + ]: 4 : assert_se(manager_add_job(m, JOB_START, a_conj, JOB_REPLACE, NULL, NULL, &j) == -EDEADLK);
122 : 4 : manager_dump_jobs(m, stdout, "\t");
123 : :
124 [ - + ]: 4 : assert_se(!hashmap_get(a->dependencies[UNIT_PROPAGATES_RELOAD_TO], b));
125 [ - + ]: 4 : assert_se(!hashmap_get(b->dependencies[UNIT_RELOAD_PROPAGATED_FROM], a));
126 [ - + ]: 4 : assert_se(!hashmap_get(a->dependencies[UNIT_PROPAGATES_RELOAD_TO], c));
127 [ - + ]: 4 : assert_se(!hashmap_get(c->dependencies[UNIT_RELOAD_PROPAGATED_FROM], a));
128 : :
129 [ - + ]: 4 : assert_se(unit_add_dependency(a, UNIT_PROPAGATES_RELOAD_TO, b, true, UNIT_DEPENDENCY_UDEV) == 0);
130 [ - + ]: 4 : assert_se(unit_add_dependency(a, UNIT_PROPAGATES_RELOAD_TO, c, true, UNIT_DEPENDENCY_PROC_SWAP) == 0);
131 : :
132 [ - + ]: 4 : assert_se(hashmap_get(a->dependencies[UNIT_PROPAGATES_RELOAD_TO], b));
133 [ - + ]: 4 : assert_se(hashmap_get(b->dependencies[UNIT_RELOAD_PROPAGATED_FROM], a));
134 [ - + ]: 4 : assert_se(hashmap_get(a->dependencies[UNIT_PROPAGATES_RELOAD_TO], c));
135 [ - + ]: 4 : assert_se(hashmap_get(c->dependencies[UNIT_RELOAD_PROPAGATED_FROM], a));
136 : :
137 : 4 : unit_remove_dependencies(a, UNIT_DEPENDENCY_UDEV);
138 : :
139 [ - + ]: 4 : assert_se(!hashmap_get(a->dependencies[UNIT_PROPAGATES_RELOAD_TO], b));
140 [ - + ]: 4 : assert_se(!hashmap_get(b->dependencies[UNIT_RELOAD_PROPAGATED_FROM], a));
141 [ - + ]: 4 : assert_se(hashmap_get(a->dependencies[UNIT_PROPAGATES_RELOAD_TO], c));
142 [ - + ]: 4 : assert_se(hashmap_get(c->dependencies[UNIT_RELOAD_PROPAGATED_FROM], a));
143 : :
144 : 4 : unit_remove_dependencies(a, UNIT_DEPENDENCY_PROC_SWAP);
145 : :
146 [ - + ]: 4 : assert_se(!hashmap_get(a->dependencies[UNIT_PROPAGATES_RELOAD_TO], b));
147 [ - + ]: 4 : assert_se(!hashmap_get(b->dependencies[UNIT_RELOAD_PROPAGATED_FROM], a));
148 [ - + ]: 4 : assert_se(!hashmap_get(a->dependencies[UNIT_PROPAGATES_RELOAD_TO], c));
149 [ - + ]: 4 : assert_se(!hashmap_get(c->dependencies[UNIT_RELOAD_PROPAGATED_FROM], a));
150 : :
151 [ - + ]: 4 : assert_se(manager_load_unit(m, "unit-with-multiple-dashes.service", NULL, NULL, &unit_with_multiple_dashes) >= 0);
152 : :
153 [ - + ]: 4 : assert_se(strv_equal(unit_with_multiple_dashes->documentation, STRV_MAKE("man:test", "man:override2", "man:override3")));
154 [ - + ]: 4 : assert_se(streq_ptr(unit_with_multiple_dashes->description, "override4"));
155 : :
156 : 4 : return 0;
157 : : }
|