]> git.ipfire.org Git - thirdparty/systemd.git/blame - 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
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
7a050b54 2/***
96b2fb93 3 Copyright © 2013 Marius Vollmer
7a050b54
MV
4***/
5
7a050b54 6#include <fcntl.h>
cf0fbc49 7#include <unistd.h>
7a050b54 8
c6878637 9#include "sd-journal.h"
6bedfcbb 10
b5efdb8a 11#include "alloc-util.h"
7a050b54 12#include "journal-file.h"
5cb24cd3 13#include "journal-vacuum.h"
7a050b54 14#include "log.h"
6bedfcbb 15#include "parse-util.h"
c6878637 16#include "rm-rf.h"
6bedfcbb 17#include "util.h"
317bb217 18#include "tests.h"
7a050b54 19
317bb217 20/* This program tests skipping around in a multi-file journal. */
7a050b54 21
5cb24cd3
ZJS
22static bool arg_keep = false;
23
848e863a 24_noreturn_ static void log_assert_errno(const char *text, int error, const char *file, int line, const char *func) {
9eec7d12
ZJS
25 log_internal(LOG_CRIT, error, file, line, func,
26 "'%s' failed at %s:%u (%s): %m", text, file, line, func);
7a050b54
MV
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
510b857f 37static JournalFile *test_open(const char *name) {
7a050b54 38 JournalFile *f;
57850536 39 assert_ret(journal_file_open(-1, name, O_RDWR|O_CREAT, 0644, true, (uint64_t) -1, false, NULL, NULL, NULL, NULL, &f));
7a050b54
MV
40 return f;
41}
42
510b857f 43static void test_close(JournalFile *f) {
69a3a6fd 44 (void) journal_file_close (f);
7a050b54
MV
45}
46
510b857f 47static void append_number(JournalFile *f, int n, uint64_t *seqnum) {
7a050b54
MV
48 char *p;
49 dual_timestamp ts;
44cf96e3 50 static dual_timestamp previous_ts = {};
7a050b54
MV
51 struct iovec iovec[1];
52
53 dual_timestamp_get(&ts);
54
44cf96e3
LP
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
7a050b54
MV
63 assert_se(asprintf(&p, "NUMBER=%d", n) >= 0);
64 iovec[0].iov_base = p;
65 iovec[0].iov_len = strlen(p);
d180c349 66 assert_ret(journal_file_append_entry(f, &ts, NULL, iovec, 1, seqnum, NULL, NULL));
510b857f 67 free(p);
7a050b54
MV
68}
69
510b857f 70static void test_check_number (sd_journal *j, int n) {
7a050b54 71 const void *d;
510b857f 72 _cleanup_free_ char *k;
7a050b54
MV
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
510b857f
LP
84static void test_check_numbers_down (sd_journal *j, int count) {
85 int i;
86
87 for (i = 1; i <= count; i++) {
7a050b54
MV
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
510b857f 99static void test_check_numbers_up (sd_journal *j, int count) {
7a050b54
MV
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
112static void setup_sequential(void) {
113 JournalFile *one, *two;
114 one = test_open("one.journal");
115 two = test_open("two.journal");
5cb24cd3
ZJS
116 append_number(one, 1, NULL);
117 append_number(one, 2, NULL);
118 append_number(two, 3, NULL);
119 append_number(two, 4, NULL);
7a050b54
MV
120 test_close(one);
121 test_close(two);
122}
123
124static void setup_interleaved(void) {
125 JournalFile *one, *two;
126 one = test_open("one.journal");
127 two = test_open("two.journal");
5cb24cd3
ZJS
128 append_number(one, 1, NULL);
129 append_number(two, 2, NULL);
130 append_number(one, 3, NULL);
131 append_number(two, 4, NULL);
7a050b54
MV
132 test_close(one);
133 test_close(two);
134}
135
510b857f 136static void test_skip(void (*setup)(void)) {
7a050b54
MV
137 char t[] = "/tmp/journal-skip-XXXXXX";
138 sd_journal *j;
139 int r;
140
7a050b54
MV
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
5cb24cd3
ZJS
180 log_info("Done...");
181
182 if (arg_keep)
183 log_info("Not removing %s", t);
184 else {
8580d1f7 185 journal_directory_vacuum(".", 3000000, 0, 0, NULL, true);
5cb24cd3 186
c6878637 187 assert_se(rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
5cb24cd3
ZJS
188 }
189
190 puts("------------------------------------------------------------");
191}
192
193static 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
5d1ce257 203 assert_se(journal_file_open(-1, "one.journal", O_RDWR|O_CREAT, 0644,
57850536 204 true, (uint64_t) -1, false, NULL, NULL, NULL, NULL, &one) == 0);
5cb24cd3
ZJS
205
206 append_number(one, 1, &seqnum);
207 printf("seqnum=%"PRIu64"\n", seqnum);
0c0cdb06 208 assert_se(seqnum == 1);
5cb24cd3
ZJS
209 append_number(one, 2, &seqnum);
210 printf("seqnum=%"PRIu64"\n", seqnum);
0c0cdb06 211 assert_se(seqnum == 2);
5cb24cd3 212
0c0cdb06
RC
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));
5cb24cd3
ZJS
217
218 memcpy(&seqnum_id, &one->header->seqnum_id, sizeof(sd_id128_t));
219
5d1ce257 220 assert_se(journal_file_open(-1, "two.journal", O_RDWR|O_CREAT, 0644,
57850536 221 true, (uint64_t) -1, false, NULL, NULL, NULL, one, &two) == 0);
5cb24cd3 222
0c0cdb06
RC
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));
5cb24cd3
ZJS
228
229 append_number(two, 3, &seqnum);
230 printf("seqnum=%"PRIu64"\n", seqnum);
0c0cdb06 231 assert_se(seqnum == 3);
5cb24cd3
ZJS
232 append_number(two, 4, &seqnum);
233 printf("seqnum=%"PRIu64"\n", seqnum);
0c0cdb06 234 assert_se(seqnum == 4);
5cb24cd3
ZJS
235
236 test_close(two);
237
238 append_number(one, 5, &seqnum);
239 printf("seqnum=%"PRIu64"\n", seqnum);
0c0cdb06 240 assert_se(seqnum == 5);
5cb24cd3
ZJS
241
242 append_number(one, 6, &seqnum);
243 printf("seqnum=%"PRIu64"\n", seqnum);
0c0cdb06 244 assert_se(seqnum == 6);
5cb24cd3
ZJS
245
246 test_close(one);
247
248 /* restart server */
249 seqnum = 0;
250
5d1ce257 251 assert_se(journal_file_open(-1, "two.journal", O_RDWR, 0,
57850536 252 true, (uint64_t) -1, false, NULL, NULL, NULL, NULL, &two) == 0);
5cb24cd3 253
0c0cdb06 254 assert_se(sd_id128_equal(two->header->seqnum_id, seqnum_id));
5cb24cd3
ZJS
255
256 append_number(two, 7, &seqnum);
257 printf("seqnum=%"PRIu64"\n", seqnum);
0c0cdb06 258 assert_se(seqnum == 5);
5cb24cd3
ZJS
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 {
8580d1f7 270 journal_directory_vacuum(".", 3000000, 0, 0, NULL, true);
5cb24cd3 271
c6878637 272 assert_se(rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
5cb24cd3 273 }
7a050b54
MV
274}
275
276int main(int argc, char *argv[]) {
6d7c4033 277 test_setup_logging(LOG_DEBUG);
5cb24cd3 278
143bfdaf 279 /* journal_file_open requires a valid machine id */
317bb217
ZJS
280 if (access("/etc/machine-id", F_OK) != 0)
281 return log_tests_skipped("/etc/machine-id not found");
143bfdaf 282
5cb24cd3
ZJS
283 arg_keep = argc > 1;
284
7a050b54
MV
285 test_skip(setup_sequential);
286 test_skip(setup_interleaved);
287
5cb24cd3
ZJS
288 test_sequence_numbers();
289
7a050b54
MV
290 return 0;
291}