This source file includes following definitions.
- symlink_schema
- rm_files
- rmtree
- setup
- teardown
- assert_schema
- extra_schema_files
1
2
3
4
5
6
7
8
9
10 #include <crm_internal.h>
11
12 #include <ftw.h>
13 #include <unistd.h>
14
15 #include <crm/common/xml.h>
16 #include <crm/common/unittest_internal.h>
17 #include <crm/common/xml_internal.h>
18 #include "crmcommon_private.h"
19
20 static char *remote_schema_dir = NULL;
21
22 static int
23 symlink_schema(const char *tmpdir, const char *target_file, const char *link_file)
24 {
25 int rc = 0;
26 char *oldpath = NULL;
27 char *newpath = NULL;
28
29 oldpath = crm_strdup_printf("%s/%s", PCMK__TEST_SCHEMA_DIR, target_file);
30 newpath = crm_strdup_printf("%s/%s", tmpdir, link_file);
31
32 rc = symlink(oldpath, newpath);
33
34 free(oldpath);
35 free(newpath);
36 return rc;
37 }
38
39 static int
40 rm_files(const char *pathname, const struct stat *sbuf, int type, struct FTW *ftwb)
41 {
42 return remove(pathname);
43 }
44
45 static int
46 rmtree(const char *dir)
47 {
48 return nftw(dir, rm_files, 10, FTW_DEPTH|FTW_MOUNT|FTW_PHYS);
49 }
50
51 static int
52 setup(void **state)
53 {
54 char *dir = NULL;
55
56
57
58
59
60 dir = crm_strdup_printf("%s/test-schemas.XXXXXX", pcmk__get_tmpdir());
61 remote_schema_dir = mkdtemp(dir);
62
63 if (remote_schema_dir == NULL) {
64 free(dir);
65 return -1;
66 }
67
68
69
70
71
72
73 if (symlink_schema(dir, "pacemaker-3.0.rng", "pacemaker-3.1.rng") != 0) {
74 rmdir(dir);
75 free(dir);
76 return -1;
77 }
78
79 if (symlink_schema(dir, "pacemaker-3.0.rng", "pacemaker-3.2.rng") != 0) {
80 rmdir(dir);
81 free(dir);
82 return -1;
83 }
84
85 setenv("PCMK_remote_schema_directory", remote_schema_dir, 1);
86 setenv("PCMK_schema_directory", PCMK__TEST_SCHEMA_DIR, 1);
87
88
89
90
91
92
93 return 0;
94 }
95
96 static int
97 teardown(void **state)
98 {
99 int rc = 0;
100 char *f = NULL;
101
102 crm_schema_cleanup();
103 unsetenv("PCMK_remote_schema_directory");
104 unsetenv("PCMK_schema_directory");
105
106 rc = rmtree(remote_schema_dir);
107
108 free(remote_schema_dir);
109 free(f);
110 return rc;
111 }
112
113 static void
114 assert_schema(const char *schema_name, int schema_index)
115 {
116 GList *entry = NULL;
117 pcmk__schema_t *schema = NULL;
118
119 entry = pcmk__get_schema(schema_name);
120 assert_non_null(entry);
121
122 schema = entry->data;
123 assert_non_null(schema);
124
125 assert_int_equal(schema_index, schema->schema_index);
126 }
127
128 static void
129 extra_schema_files(void **state)
130 {
131 crm_schema_init();
132
133
134
135
136
137 assert_schema("pacemaker-1.0", 0);
138 assert_schema("pacemaker-1.2", 1);
139 assert_schema("pacemaker-2.0", 3);
140 assert_schema("pacemaker-3.0", 14);
141 assert_schema("pacemaker-3.1", 15);
142 assert_schema("pacemaker-3.2", 16);
143
144
145 assert_schema("pacemaker-next", 17);
146
147
148 assert_schema(PCMK_VALUE_NONE, 18);
149 }
150
151 PCMK__UNIT_TEST(setup, teardown,
152 cmocka_unit_test(extra_schema_files));