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