Line data Source code
1 : /* SPDX-License-Identifier: LGPL-2.1+ */
2 :
3 : #include "path-lookup.h"
4 : #include "set.h"
5 : #include "strv.h"
6 : #include "tests.h"
7 : #include "unit-file.h"
8 :
9 1 : static void test_unit_validate_alias_symlink_and_warn(void) {
10 1 : log_info("/* %s */", __func__);
11 :
12 1 : assert_se(unit_validate_alias_symlink_and_warn("/path/a.service", "/other/b.service") == 0);
13 1 : assert_se(unit_validate_alias_symlink_and_warn("/path/a.service", "/other/b.socket") == -EXDEV);
14 1 : assert_se(unit_validate_alias_symlink_and_warn("/path/a.service", "/other/b.foobar") == -EXDEV);
15 1 : assert_se(unit_validate_alias_symlink_and_warn("/path/a@.service", "/other/b@.service") == 0);
16 1 : assert_se(unit_validate_alias_symlink_and_warn("/path/a@.service", "/other/b@.socket") == -EXDEV);
17 1 : assert_se(unit_validate_alias_symlink_and_warn("/path/a@XXX.service", "/other/b@YYY.service") == -EXDEV);
18 1 : assert_se(unit_validate_alias_symlink_and_warn("/path/a@XXX.service", "/other/b@YYY.socket") == -EXDEV);
19 1 : assert_se(unit_validate_alias_symlink_and_warn("/path/a@.service", "/other/b@YYY.service") == -EXDEV);
20 1 : assert_se(unit_validate_alias_symlink_and_warn("/path/a@XXX.service", "/other/b@XXX.service") == 0);
21 1 : assert_se(unit_validate_alias_symlink_and_warn("/path/a@XXX.service", "/other/b@.service") == 0);
22 1 : assert_se(unit_validate_alias_symlink_and_warn("/path/a@.service", "/other/b.service") == -EXDEV);
23 1 : assert_se(unit_validate_alias_symlink_and_warn("/path/a.service", "/other/b@.service") == -EXDEV);
24 1 : assert_se(unit_validate_alias_symlink_and_warn("/path/a@.slice", "/other/b.slice") == -EINVAL);
25 1 : assert_se(unit_validate_alias_symlink_and_warn("/path/a.slice", "/other/b.slice") == -EINVAL);
26 1 : }
27 :
28 1 : static void test_unit_file_build_name_map(char **ids) {
29 1 : _cleanup_(lookup_paths_free) LookupPaths lp = {};
30 1 : _cleanup_hashmap_free_ Hashmap *unit_ids = NULL;
31 1 : _cleanup_hashmap_free_ Hashmap *unit_names = NULL;
32 : Iterator i;
33 : const char *k, *dst;
34 : char **v;
35 1 : usec_t mtime = 0;
36 : int r;
37 :
38 1 : assert_se(lookup_paths_init(&lp, UNIT_FILE_SYSTEM, 0, NULL) >= 0);
39 :
40 1 : assert_se(unit_file_build_name_map(&lp, &mtime, &unit_ids, &unit_names, NULL) == 1);
41 :
42 508 : HASHMAP_FOREACH_KEY(dst, k, unit_ids, i)
43 507 : log_info("ids: %s → %s", k, dst);
44 :
45 475 : HASHMAP_FOREACH_KEY(v, k, unit_names, i) {
46 474 : _cleanup_free_ char *j = strv_join(v, ", ");
47 474 : log_info("aliases: %s ← %s", k, j);
48 : }
49 :
50 : char buf[FORMAT_TIMESTAMP_MAX];
51 1 : log_debug("Last modification time: %s", format_timestamp(buf, sizeof buf, mtime));
52 :
53 1 : r = unit_file_build_name_map(&lp, &mtime, &unit_ids, &unit_names, NULL);
54 1 : assert_se(IN_SET(r, 0, 1));
55 1 : if (r == 0)
56 1 : log_debug("Cache rebuild skipped based on mtime.");
57 :
58 :
59 : char **id;
60 1 : STRV_FOREACH(id, ids) {
61 : const char *fragment, *name;
62 : Iterator it;
63 0 : _cleanup_set_free_free_ Set *names = NULL;
64 0 : log_info("*** %s ***", *id);
65 0 : r = unit_file_find_fragment(unit_ids,
66 : unit_names,
67 : *id,
68 : &fragment,
69 : &names);
70 0 : assert(r == 0);
71 0 : log_info("fragment: %s", fragment);
72 0 : log_info("names:");
73 0 : SET_FOREACH(name, names, it)
74 0 : log_info(" %s", name);
75 : }
76 1 : }
77 :
78 1 : int main(int argc, char **argv) {
79 1 : test_setup_logging(LOG_DEBUG);
80 :
81 1 : test_unit_validate_alias_symlink_and_warn();
82 1 : test_unit_file_build_name_map(strv_skip(argv, 1));
83 :
84 1 : return 0;
85 : }
|