]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/test-journal-interleaving.c
tests: use a helper function to parse environment and open logging
[thirdparty/systemd.git] / src / journal / test-journal-interleaving.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright © 2013 Marius Vollmer
4 ***/
5
6 #include <fcntl.h>
7 #include <unistd.h>
8
9 #include "sd-journal.h"
10
11 #include "alloc-util.h"
12 #include "journal-file.h"
13 #include "journal-vacuum.h"
14 #include "log.h"
15 #include "parse-util.h"
16 #include "rm-rf.h"
17 #include "util.h"
18 #include "tests.h"
19
20 /* This program tests skipping around in a multi-file journal. */
21
22 static bool arg_keep = false;
23
24 _noreturn_ static void log_assert_errno(const char *text, int error, const char *file, int line, const char *func) {
25 log_internal(LOG_CRIT, error, file, line, func,
26 "'%s' failed at %s:%u (%s): %m", text, file, line, func);
27 abort();
28 }
29
30 #define assert_ret(expr) \
31 do { \
32 int _r_ = (expr); \
33 if (_unlikely_(_r_ < 0)) \
34 log_assert_errno(#expr, -_r_, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
35 } while (false)
36
37 static JournalFile *test_open(const char *name) {
38 JournalFile *f;
39 assert_ret(journal_file_open(-1, name, O_RDWR|O_CREAT, 0644, true, (uint64_t) -1, false, NULL, NULL, NULL, NULL, &f));
40 return f;
41 }
42
43 static void test_close(JournalFile *f) {
44 (void) journal_file_close (f);
45 }
46
47 static void append_number(JournalFile *f, int n, uint64_t *seqnum) {
48 char *p;
49 dual_timestamp ts;
50 static dual_timestamp previous_ts = {};
51 struct iovec iovec[1];
52
53 dual_timestamp_get(&ts);
54
55 if (ts.monotonic <= previous_ts.monotonic)
56 ts.monotonic = previous_ts.monotonic + 1;
57
58 if (ts.realtime <= previous_ts.realtime)
59 ts.realtime = previous_ts.realtime + 1;
60
61 previous_ts = ts;
62
63 assert_se(asprintf(&p, "NUMBER=%d", n) >= 0);
64 iovec[0].iov_base = p;
65 iovec[0].iov_len = strlen(p);
66 assert_ret(journal_file_append_entry(f, &ts, NULL, iovec, 1, seqnum, NULL, NULL));
67 free(p);
68 }
69
70 static void test_check_number (sd_journal *j, int n) {
71 const void *d;
72 _cleanup_free_ char *k;
73 size_t l;
74 int x;
75
76 assert_ret(sd_journal_get_data(j, "NUMBER", &d, &l));
77 assert_se(k = strndup(d, l));
78 printf("%s\n", k);
79
80 assert_se(safe_atoi(k + 7, &x) >= 0);
81 assert_se(n == x);
82 }
83
84 static void test_check_numbers_down (sd_journal *j, int count) {
85 int i;
86
87 for (i = 1; i <= count; i++) {
88 int r;
89 test_check_number(j, i);
90 assert_ret(r = sd_journal_next(j));
91 if (i == count)
92 assert_se(r == 0);
93 else
94 assert_se(r == 1);
95 }
96
97 }
98
99 static void test_check_numbers_up (sd_journal *j, int count) {
100 for (int i = count; i >= 1; i--) {
101 int r;
102 test_check_number(j, i);
103 assert_ret(r = sd_journal_previous(j));
104 if (i == 1)
105 assert_se(r == 0);
106 else
107 assert_se(r == 1);
108 }
109
110 }
111
112 static void setup_sequential(void) {
113 JournalFile *one, *two;
114 one = test_open("one.journal");
115 two = test_open("two.journal");
116 append_number(one, 1, NULL);
117 append_number(one, 2, NULL);
118 append_number(two, 3, NULL);
119 append_number(two, 4, NULL);
120 test_close(one);
121 test_close(two);
122 }
123
124 static void setup_interleaved(void) {
125 JournalFile *one, *two;
126 one = test_open("one.journal");
127 two = test_open("two.journal");
128 append_number(one, 1, NULL);
129 append_number(two, 2, NULL);
130 append_number(one, 3, NULL);
131 append_number(two, 4, NULL);
132 test_close(one);
133 test_close(two);
134 }
135
136 static void test_skip(void (*setup)(void)) {
137 char t[] = "/tmp/journal-skip-XXXXXX";
138 sd_journal *j;
139 int r;
140
141 assert_se(mkdtemp(t));
142 assert_se(chdir(t) >= 0);
143
144 setup();
145
146 /* Seek to head, iterate down.
147 */
148 assert_ret(sd_journal_open_directory(&j, t, 0));
149 assert_ret(sd_journal_seek_head(j));
150 assert_ret(sd_journal_next(j));
151 test_check_numbers_down(j, 4);
152 sd_journal_close(j);
153
154 /* Seek to tail, iterate up.
155 */
156 assert_ret(sd_journal_open_directory(&j, t, 0));
157 assert_ret(sd_journal_seek_tail(j));
158 assert_ret(sd_journal_previous(j));
159 test_check_numbers_up(j, 4);
160 sd_journal_close(j);
161
162 /* Seek to tail, skip to head, iterate down.
163 */
164 assert_ret(sd_journal_open_directory(&j, t, 0));
165 assert_ret(sd_journal_seek_tail(j));
166 assert_ret(r = sd_journal_previous_skip(j, 4));
167 assert_se(r == 4);
168 test_check_numbers_down(j, 4);
169 sd_journal_close(j);
170
171 /* Seek to head, skip to tail, iterate up.
172 */
173 assert_ret(sd_journal_open_directory(&j, t, 0));
174 assert_ret(sd_journal_seek_head(j));
175 assert_ret(r = sd_journal_next_skip(j, 4));
176 assert_se(r == 4);
177 test_check_numbers_up(j, 4);
178 sd_journal_close(j);
179
180 log_info("Done...");
181
182 if (arg_keep)
183 log_info("Not removing %s", t);
184 else {
185 journal_directory_vacuum(".", 3000000, 0, 0, NULL, true);
186
187 assert_se(rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
188 }
189
190 puts("------------------------------------------------------------");
191 }
192
193 static void test_sequence_numbers(void) {
194
195 char t[] = "/tmp/journal-seq-XXXXXX";
196 JournalFile *one, *two;
197 uint64_t seqnum = 0;
198 sd_id128_t seqnum_id;
199
200 assert_se(mkdtemp(t));
201 assert_se(chdir(t) >= 0);
202
203 assert_se(journal_file_open(-1, "one.journal", O_RDWR|O_CREAT, 0644,
204 true, (uint64_t) -1, false, NULL, NULL, NULL, NULL, &one) == 0);
205
206 append_number(one, 1, &seqnum);
207 printf("seqnum=%"PRIu64"\n", seqnum);
208 assert_se(seqnum == 1);
209 append_number(one, 2, &seqnum);
210 printf("seqnum=%"PRIu64"\n", seqnum);
211 assert_se(seqnum == 2);
212
213 assert_se(one->header->state == STATE_ONLINE);
214 assert_se(!sd_id128_equal(one->header->file_id, one->header->machine_id));
215 assert_se(!sd_id128_equal(one->header->file_id, one->header->boot_id));
216 assert_se(sd_id128_equal(one->header->file_id, one->header->seqnum_id));
217
218 memcpy(&seqnum_id, &one->header->seqnum_id, sizeof(sd_id128_t));
219
220 assert_se(journal_file_open(-1, "two.journal", O_RDWR|O_CREAT, 0644,
221 true, (uint64_t) -1, false, NULL, NULL, NULL, one, &two) == 0);
222
223 assert_se(two->header->state == STATE_ONLINE);
224 assert_se(!sd_id128_equal(two->header->file_id, one->header->file_id));
225 assert_se(sd_id128_equal(one->header->machine_id, one->header->machine_id));
226 assert_se(sd_id128_equal(one->header->boot_id, one->header->boot_id));
227 assert_se(sd_id128_equal(one->header->seqnum_id, one->header->seqnum_id));
228
229 append_number(two, 3, &seqnum);
230 printf("seqnum=%"PRIu64"\n", seqnum);
231 assert_se(seqnum == 3);
232 append_number(two, 4, &seqnum);
233 printf("seqnum=%"PRIu64"\n", seqnum);
234 assert_se(seqnum == 4);
235
236 test_close(two);
237
238 append_number(one, 5, &seqnum);
239 printf("seqnum=%"PRIu64"\n", seqnum);
240 assert_se(seqnum == 5);
241
242 append_number(one, 6, &seqnum);
243 printf("seqnum=%"PRIu64"\n", seqnum);
244 assert_se(seqnum == 6);
245
246 test_close(one);
247
248 /* restart server */
249 seqnum = 0;
250
251 assert_se(journal_file_open(-1, "two.journal", O_RDWR, 0,
252 true, (uint64_t) -1, false, NULL, NULL, NULL, NULL, &two) == 0);
253
254 assert_se(sd_id128_equal(two->header->seqnum_id, seqnum_id));
255
256 append_number(two, 7, &seqnum);
257 printf("seqnum=%"PRIu64"\n", seqnum);
258 assert_se(seqnum == 5);
259
260 /* So..., here we have the same seqnum in two files with the
261 * same seqnum_id. */
262
263 test_close(two);
264
265 log_info("Done...");
266
267 if (arg_keep)
268 log_info("Not removing %s", t);
269 else {
270 journal_directory_vacuum(".", 3000000, 0, 0, NULL, true);
271
272 assert_se(rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
273 }
274 }
275
276 int main(int argc, char *argv[]) {
277 test_setup_logging(LOG_DEBUG);
278
279 /* journal_file_open requires a valid machine id */
280 if (access("/etc/machine-id", F_OK) != 0)
281 return log_tests_skipped("/etc/machine-id not found");
282
283 arg_keep = argc > 1;
284
285 test_skip(setup_sequential);
286 test_skip(setup_interleaved);
287
288 test_sequence_numbers();
289
290 return 0;
291 }