]>
Commit | Line | Data |
---|---|---|
e48d4272 HWN |
1 | /* |
2 | Copyright 2020 Google LLC | |
3 | ||
4 | Use of this source code is governed by a BSD-style | |
5 | license that can be found in the LICENSE file or at | |
6 | https://developers.google.com/open-source/licenses/bsd | |
7 | */ | |
8 | ||
9 | #include "stack.h" | |
10 | ||
11 | #include "system.h" | |
12 | ||
13 | #include "reftable-reader.h" | |
14 | #include "merged.h" | |
15 | #include "basics.h" | |
e48d4272 HWN |
16 | #include "record.h" |
17 | #include "test_framework.h" | |
18 | #include "reftable-tests.h" | |
cd1799de | 19 | #include "reader.h" |
e48d4272 HWN |
20 | |
21 | #include <sys/types.h> | |
22 | #include <dirent.h> | |
23 | ||
24 | static void clear_dir(const char *dirname) | |
25 | { | |
26 | struct strbuf path = STRBUF_INIT; | |
27 | strbuf_addstr(&path, dirname); | |
28 | remove_dir_recursively(&path, 0); | |
29 | strbuf_release(&path); | |
30 | } | |
31 | ||
32 | static int count_dir_entries(const char *dirname) | |
33 | { | |
34 | DIR *dir = opendir(dirname); | |
35 | int len = 0; | |
36 | struct dirent *d; | |
72a4ea71 | 37 | if (!dir) |
e48d4272 HWN |
38 | return 0; |
39 | ||
40 | while ((d = readdir(dir))) { | |
41 | if (!strcmp(d->d_name, "..") || !strcmp(d->d_name, ".")) | |
42 | continue; | |
43 | len++; | |
44 | } | |
45 | closedir(dir); | |
46 | return len; | |
47 | } | |
48 | ||
49 | /* | |
50 | * Work linenumber into the tempdir, so we can see which tests forget to | |
51 | * cleanup. | |
52 | */ | |
53 | static char *get_tmp_template(int linenumber) | |
54 | { | |
55 | const char *tmp = getenv("TMPDIR"); | |
56 | static char template[1024]; | |
57 | snprintf(template, sizeof(template) - 1, "%s/stack_test-%d.XXXXXX", | |
58 | tmp ? tmp : "/tmp", linenumber); | |
59 | return template; | |
60 | } | |
61 | ||
62 | static char *get_tmp_dir(int linenumber) | |
63 | { | |
64 | char *dir = get_tmp_template(linenumber); | |
65 | EXPECT(mkdtemp(dir)); | |
66 | return dir; | |
67 | } | |
68 | ||
69 | static void test_read_file(void) | |
70 | { | |
71 | char *fn = get_tmp_template(__LINE__); | |
72 | int fd = mkstemp(fn); | |
73 | char out[1024] = "line1\n\nline2\nline3"; | |
74 | int n, err; | |
75 | char **names = NULL; | |
76 | char *want[] = { "line1", "line2", "line3" }; | |
77 | int i = 0; | |
78 | ||
79 | EXPECT(fd > 0); | |
85a8c899 | 80 | n = write_in_full(fd, out, strlen(out)); |
e48d4272 HWN |
81 | EXPECT(n == strlen(out)); |
82 | err = close(fd); | |
83 | EXPECT(err >= 0); | |
84 | ||
85 | err = read_lines(fn, &names); | |
86 | EXPECT_ERR(err); | |
87 | ||
88 | for (i = 0; names[i]; i++) { | |
89 | EXPECT(0 == strcmp(want[i], names[i])); | |
90 | } | |
91 | free_names(names); | |
f5f6a6cd | 92 | (void) remove(fn); |
e48d4272 HWN |
93 | } |
94 | ||
95 | static void test_parse_names(void) | |
96 | { | |
97 | char buf[] = "line\n"; | |
98 | char **names = NULL; | |
99 | parse_names(buf, strlen(buf), &names); | |
100 | ||
101 | EXPECT(NULL != names[0]); | |
102 | EXPECT(0 == strcmp(names[0], "line")); | |
103 | EXPECT(NULL == names[1]); | |
104 | free_names(names); | |
105 | } | |
106 | ||
107 | static void test_names_equal(void) | |
108 | { | |
109 | char *a[] = { "a", "b", "c", NULL }; | |
110 | char *b[] = { "a", "b", "d", NULL }; | |
111 | char *c[] = { "a", "b", NULL }; | |
112 | ||
113 | EXPECT(names_equal(a, a)); | |
114 | EXPECT(!names_equal(a, b)); | |
115 | EXPECT(!names_equal(a, c)); | |
116 | } | |
117 | ||
118 | static int write_test_ref(struct reftable_writer *wr, void *arg) | |
119 | { | |
120 | struct reftable_ref_record *ref = arg; | |
121 | reftable_writer_set_limits(wr, ref->update_index, ref->update_index); | |
122 | return reftable_writer_add_ref(wr, ref); | |
123 | } | |
124 | ||
125 | struct write_log_arg { | |
126 | struct reftable_log_record *log; | |
127 | uint64_t update_index; | |
128 | }; | |
129 | ||
130 | static int write_test_log(struct reftable_writer *wr, void *arg) | |
131 | { | |
132 | struct write_log_arg *wla = arg; | |
133 | ||
134 | reftable_writer_set_limits(wr, wla->update_index, wla->update_index); | |
135 | return reftable_writer_add_log(wr, wla->log); | |
136 | } | |
137 | ||
138 | static void test_reftable_stack_add_one(void) | |
139 | { | |
140 | char *dir = get_tmp_dir(__LINE__); | |
cd1799de HWN |
141 | struct strbuf scratch = STRBUF_INIT; |
142 | int mask = umask(002); | |
143 | struct reftable_write_options cfg = { | |
144 | .default_permissions = 0660, | |
145 | }; | |
e48d4272 HWN |
146 | struct reftable_stack *st = NULL; |
147 | int err; | |
148 | struct reftable_ref_record ref = { | |
149 | .refname = "HEAD", | |
150 | .update_index = 1, | |
151 | .value_type = REFTABLE_REF_SYMREF, | |
152 | .value.symref = "master", | |
153 | }; | |
154 | struct reftable_ref_record dest = { NULL }; | |
cd1799de | 155 | struct stat stat_result = { 0 }; |
e48d4272 HWN |
156 | err = reftable_new_stack(&st, dir, cfg); |
157 | EXPECT_ERR(err); | |
158 | ||
159 | err = reftable_stack_add(st, &write_test_ref, &ref); | |
160 | EXPECT_ERR(err); | |
161 | ||
162 | err = reftable_stack_read_ref(st, ref.refname, &dest); | |
163 | EXPECT_ERR(err); | |
164 | EXPECT(0 == strcmp("master", dest.value.symref)); | |
cd1799de | 165 | EXPECT(st->readers_len > 0); |
e48d4272 HWN |
166 | |
167 | printf("testing print functionality:\n"); | |
168 | err = reftable_stack_print_directory(dir, GIT_SHA1_FORMAT_ID); | |
169 | EXPECT_ERR(err); | |
170 | ||
171 | err = reftable_stack_print_directory(dir, GIT_SHA256_FORMAT_ID); | |
172 | EXPECT(err == REFTABLE_FORMAT_ERROR); | |
173 | ||
cd1799de HWN |
174 | #ifndef GIT_WINDOWS_NATIVE |
175 | strbuf_addstr(&scratch, dir); | |
176 | strbuf_addstr(&scratch, "/tables.list"); | |
177 | err = stat(scratch.buf, &stat_result); | |
178 | EXPECT(!err); | |
179 | EXPECT((stat_result.st_mode & 0777) == cfg.default_permissions); | |
180 | ||
181 | strbuf_reset(&scratch); | |
182 | strbuf_addstr(&scratch, dir); | |
183 | strbuf_addstr(&scratch, "/"); | |
184 | /* do not try at home; not an external API for reftable. */ | |
185 | strbuf_addstr(&scratch, st->readers[0]->name); | |
186 | err = stat(scratch.buf, &stat_result); | |
187 | EXPECT(!err); | |
188 | EXPECT((stat_result.st_mode & 0777) == cfg.default_permissions); | |
189 | #else | |
190 | (void) stat_result; | |
191 | #endif | |
192 | ||
e48d4272 HWN |
193 | reftable_ref_record_release(&dest); |
194 | reftable_stack_destroy(st); | |
cd1799de | 195 | strbuf_release(&scratch); |
e48d4272 | 196 | clear_dir(dir); |
cd1799de | 197 | umask(mask); |
e48d4272 HWN |
198 | } |
199 | ||
200 | static void test_reftable_stack_uptodate(void) | |
201 | { | |
202 | struct reftable_write_options cfg = { 0 }; | |
203 | struct reftable_stack *st1 = NULL; | |
204 | struct reftable_stack *st2 = NULL; | |
205 | char *dir = get_tmp_dir(__LINE__); | |
206 | ||
207 | int err; | |
208 | struct reftable_ref_record ref1 = { | |
209 | .refname = "HEAD", | |
210 | .update_index = 1, | |
211 | .value_type = REFTABLE_REF_SYMREF, | |
212 | .value.symref = "master", | |
213 | }; | |
214 | struct reftable_ref_record ref2 = { | |
215 | .refname = "branch2", | |
216 | .update_index = 2, | |
217 | .value_type = REFTABLE_REF_SYMREF, | |
218 | .value.symref = "master", | |
219 | }; | |
220 | ||
221 | ||
222 | /* simulate multi-process access to the same stack | |
223 | by creating two stacks for the same directory. | |
224 | */ | |
225 | err = reftable_new_stack(&st1, dir, cfg); | |
226 | EXPECT_ERR(err); | |
227 | ||
228 | err = reftable_new_stack(&st2, dir, cfg); | |
229 | EXPECT_ERR(err); | |
230 | ||
231 | err = reftable_stack_add(st1, &write_test_ref, &ref1); | |
232 | EXPECT_ERR(err); | |
233 | ||
234 | err = reftable_stack_add(st2, &write_test_ref, &ref2); | |
235 | EXPECT(err == REFTABLE_LOCK_ERROR); | |
236 | ||
237 | err = reftable_stack_reload(st2); | |
238 | EXPECT_ERR(err); | |
239 | ||
240 | err = reftable_stack_add(st2, &write_test_ref, &ref2); | |
241 | EXPECT_ERR(err); | |
242 | reftable_stack_destroy(st1); | |
243 | reftable_stack_destroy(st2); | |
244 | clear_dir(dir); | |
245 | } | |
246 | ||
247 | static void test_reftable_stack_transaction_api(void) | |
248 | { | |
249 | char *dir = get_tmp_dir(__LINE__); | |
250 | ||
251 | struct reftable_write_options cfg = { 0 }; | |
252 | struct reftable_stack *st = NULL; | |
253 | int err; | |
254 | struct reftable_addition *add = NULL; | |
255 | ||
256 | struct reftable_ref_record ref = { | |
257 | .refname = "HEAD", | |
258 | .update_index = 1, | |
259 | .value_type = REFTABLE_REF_SYMREF, | |
260 | .value.symref = "master", | |
261 | }; | |
262 | struct reftable_ref_record dest = { NULL }; | |
263 | ||
264 | ||
265 | err = reftable_new_stack(&st, dir, cfg); | |
266 | EXPECT_ERR(err); | |
267 | ||
268 | reftable_addition_destroy(add); | |
269 | ||
270 | err = reftable_stack_new_addition(&add, st); | |
271 | EXPECT_ERR(err); | |
272 | ||
273 | err = reftable_addition_add(add, &write_test_ref, &ref); | |
274 | EXPECT_ERR(err); | |
275 | ||
276 | err = reftable_addition_commit(add); | |
277 | EXPECT_ERR(err); | |
278 | ||
279 | reftable_addition_destroy(add); | |
280 | ||
281 | err = reftable_stack_read_ref(st, ref.refname, &dest); | |
282 | EXPECT_ERR(err); | |
283 | EXPECT(REFTABLE_REF_SYMREF == dest.value_type); | |
284 | EXPECT(0 == strcmp("master", dest.value.symref)); | |
285 | ||
286 | reftable_ref_record_release(&dest); | |
287 | reftable_stack_destroy(st); | |
288 | clear_dir(dir); | |
289 | } | |
290 | ||
5c086453 PS |
291 | static void test_reftable_stack_transaction_api_performs_auto_compaction(void) |
292 | { | |
293 | char *dir = get_tmp_dir(__LINE__); | |
294 | struct reftable_write_options cfg = {0}; | |
295 | struct reftable_addition *add = NULL; | |
296 | struct reftable_stack *st = NULL; | |
297 | int i, n = 20, err; | |
298 | ||
299 | err = reftable_new_stack(&st, dir, cfg); | |
300 | EXPECT_ERR(err); | |
301 | ||
302 | for (i = 0; i <= n; i++) { | |
303 | struct reftable_ref_record ref = { | |
304 | .update_index = reftable_stack_next_update_index(st), | |
305 | .value_type = REFTABLE_REF_SYMREF, | |
306 | .value.symref = "master", | |
307 | }; | |
308 | char name[100]; | |
309 | ||
310 | snprintf(name, sizeof(name), "branch%04d", i); | |
311 | ref.refname = name; | |
312 | ||
313 | /* | |
314 | * Disable auto-compaction for all but the last runs. Like this | |
315 | * we can ensure that we indeed honor this setting and have | |
316 | * better control over when exactly auto compaction runs. | |
317 | */ | |
318 | st->disable_auto_compact = i != n; | |
319 | ||
320 | err = reftable_stack_new_addition(&add, st); | |
321 | EXPECT_ERR(err); | |
322 | ||
323 | err = reftable_addition_add(add, &write_test_ref, &ref); | |
324 | EXPECT_ERR(err); | |
325 | ||
326 | err = reftable_addition_commit(add); | |
327 | EXPECT_ERR(err); | |
328 | ||
329 | reftable_addition_destroy(add); | |
330 | ||
331 | /* | |
332 | * The stack length should grow continuously for all runs where | |
333 | * auto compaction is disabled. When enabled, we should merge | |
334 | * all tables in the stack. | |
335 | */ | |
336 | if (i != n) | |
337 | EXPECT(st->merged->stack_len == i + 1); | |
338 | else | |
339 | EXPECT(st->merged->stack_len == 1); | |
340 | } | |
341 | ||
342 | reftable_stack_destroy(st); | |
343 | clear_dir(dir); | |
344 | } | |
345 | ||
e48d4272 HWN |
346 | static void test_reftable_stack_validate_refname(void) |
347 | { | |
348 | struct reftable_write_options cfg = { 0 }; | |
349 | struct reftable_stack *st = NULL; | |
350 | int err; | |
351 | char *dir = get_tmp_dir(__LINE__); | |
352 | ||
353 | int i; | |
354 | struct reftable_ref_record ref = { | |
355 | .refname = "a/b", | |
356 | .update_index = 1, | |
357 | .value_type = REFTABLE_REF_SYMREF, | |
358 | .value.symref = "master", | |
359 | }; | |
360 | char *additions[] = { "a", "a/b/c" }; | |
361 | ||
362 | err = reftable_new_stack(&st, dir, cfg); | |
363 | EXPECT_ERR(err); | |
364 | ||
365 | err = reftable_stack_add(st, &write_test_ref, &ref); | |
366 | EXPECT_ERR(err); | |
367 | ||
368 | for (i = 0; i < ARRAY_SIZE(additions); i++) { | |
369 | struct reftable_ref_record ref = { | |
370 | .refname = additions[i], | |
371 | .update_index = 1, | |
372 | .value_type = REFTABLE_REF_SYMREF, | |
373 | .value.symref = "master", | |
374 | }; | |
375 | ||
376 | err = reftable_stack_add(st, &write_test_ref, &ref); | |
377 | EXPECT(err == REFTABLE_NAME_CONFLICT); | |
378 | } | |
379 | ||
380 | reftable_stack_destroy(st); | |
381 | clear_dir(dir); | |
382 | } | |
383 | ||
384 | static int write_error(struct reftable_writer *wr, void *arg) | |
385 | { | |
386 | return *((int *)arg); | |
387 | } | |
388 | ||
389 | static void test_reftable_stack_update_index_check(void) | |
390 | { | |
391 | char *dir = get_tmp_dir(__LINE__); | |
392 | ||
393 | struct reftable_write_options cfg = { 0 }; | |
394 | struct reftable_stack *st = NULL; | |
395 | int err; | |
396 | struct reftable_ref_record ref1 = { | |
397 | .refname = "name1", | |
398 | .update_index = 1, | |
399 | .value_type = REFTABLE_REF_SYMREF, | |
400 | .value.symref = "master", | |
401 | }; | |
402 | struct reftable_ref_record ref2 = { | |
403 | .refname = "name2", | |
404 | .update_index = 1, | |
405 | .value_type = REFTABLE_REF_SYMREF, | |
406 | .value.symref = "master", | |
407 | }; | |
408 | ||
409 | err = reftable_new_stack(&st, dir, cfg); | |
410 | EXPECT_ERR(err); | |
411 | ||
412 | err = reftable_stack_add(st, &write_test_ref, &ref1); | |
413 | EXPECT_ERR(err); | |
414 | ||
415 | err = reftable_stack_add(st, &write_test_ref, &ref2); | |
416 | EXPECT(err == REFTABLE_API_ERROR); | |
417 | reftable_stack_destroy(st); | |
418 | clear_dir(dir); | |
419 | } | |
420 | ||
421 | static void test_reftable_stack_lock_failure(void) | |
422 | { | |
423 | char *dir = get_tmp_dir(__LINE__); | |
424 | ||
425 | struct reftable_write_options cfg = { 0 }; | |
426 | struct reftable_stack *st = NULL; | |
427 | int err, i; | |
428 | ||
429 | err = reftable_new_stack(&st, dir, cfg); | |
430 | EXPECT_ERR(err); | |
431 | for (i = -1; i != REFTABLE_EMPTY_TABLE_ERROR; i--) { | |
432 | err = reftable_stack_add(st, &write_error, &i); | |
433 | EXPECT(err == i); | |
434 | } | |
435 | ||
436 | reftable_stack_destroy(st); | |
437 | clear_dir(dir); | |
438 | } | |
439 | ||
440 | static void test_reftable_stack_add(void) | |
441 | { | |
442 | int i = 0; | |
443 | int err = 0; | |
444 | struct reftable_write_options cfg = { | |
445 | .exact_log_message = 1, | |
446 | }; | |
447 | struct reftable_stack *st = NULL; | |
448 | char *dir = get_tmp_dir(__LINE__); | |
449 | ||
450 | struct reftable_ref_record refs[2] = { { NULL } }; | |
451 | struct reftable_log_record logs[2] = { { NULL } }; | |
452 | int N = ARRAY_SIZE(refs); | |
453 | ||
454 | ||
455 | err = reftable_new_stack(&st, dir, cfg); | |
456 | EXPECT_ERR(err); | |
457 | st->disable_auto_compact = 1; | |
458 | ||
459 | for (i = 0; i < N; i++) { | |
460 | char buf[256]; | |
461 | snprintf(buf, sizeof(buf), "branch%02d", i); | |
462 | refs[i].refname = xstrdup(buf); | |
463 | refs[i].update_index = i + 1; | |
464 | refs[i].value_type = REFTABLE_REF_VAL1; | |
e48d4272 HWN |
465 | set_test_hash(refs[i].value.val1, i); |
466 | ||
467 | logs[i].refname = xstrdup(buf); | |
468 | logs[i].update_index = N + i + 1; | |
469 | logs[i].value_type = REFTABLE_LOG_UPDATE; | |
470 | ||
471 | logs[i].value.update.new_hash = reftable_malloc(GIT_SHA1_RAWSZ); | |
472 | logs[i].value.update.email = xstrdup("identity@invalid"); | |
473 | set_test_hash(logs[i].value.update.new_hash, i); | |
474 | } | |
475 | ||
476 | for (i = 0; i < N; i++) { | |
477 | int err = reftable_stack_add(st, &write_test_ref, &refs[i]); | |
478 | EXPECT_ERR(err); | |
479 | } | |
480 | ||
481 | for (i = 0; i < N; i++) { | |
482 | struct write_log_arg arg = { | |
483 | .log = &logs[i], | |
484 | .update_index = reftable_stack_next_update_index(st), | |
485 | }; | |
486 | int err = reftable_stack_add(st, &write_test_log, &arg); | |
487 | EXPECT_ERR(err); | |
488 | } | |
489 | ||
490 | err = reftable_stack_compact_all(st, NULL); | |
491 | EXPECT_ERR(err); | |
492 | ||
493 | for (i = 0; i < N; i++) { | |
494 | struct reftable_ref_record dest = { NULL }; | |
495 | ||
496 | int err = reftable_stack_read_ref(st, refs[i].refname, &dest); | |
497 | EXPECT_ERR(err); | |
498 | EXPECT(reftable_ref_record_equal(&dest, refs + i, | |
499 | GIT_SHA1_RAWSZ)); | |
500 | reftable_ref_record_release(&dest); | |
501 | } | |
502 | ||
503 | for (i = 0; i < N; i++) { | |
504 | struct reftable_log_record dest = { NULL }; | |
505 | int err = reftable_stack_read_log(st, refs[i].refname, &dest); | |
506 | EXPECT_ERR(err); | |
507 | EXPECT(reftable_log_record_equal(&dest, logs + i, | |
508 | GIT_SHA1_RAWSZ)); | |
509 | reftable_log_record_release(&dest); | |
510 | } | |
511 | ||
512 | /* cleanup */ | |
513 | reftable_stack_destroy(st); | |
514 | for (i = 0; i < N; i++) { | |
515 | reftable_ref_record_release(&refs[i]); | |
516 | reftable_log_record_release(&logs[i]); | |
517 | } | |
518 | clear_dir(dir); | |
519 | } | |
520 | ||
521 | static void test_reftable_stack_log_normalize(void) | |
522 | { | |
523 | int err = 0; | |
524 | struct reftable_write_options cfg = { | |
525 | 0, | |
526 | }; | |
527 | struct reftable_stack *st = NULL; | |
528 | char *dir = get_tmp_dir(__LINE__); | |
529 | ||
530 | uint8_t h1[GIT_SHA1_RAWSZ] = { 0x01 }, h2[GIT_SHA1_RAWSZ] = { 0x02 }; | |
531 | ||
532 | struct reftable_log_record input = { .refname = "branch", | |
533 | .update_index = 1, | |
534 | .value_type = REFTABLE_LOG_UPDATE, | |
535 | .value = { .update = { | |
536 | .new_hash = h1, | |
537 | .old_hash = h2, | |
538 | } } }; | |
539 | struct reftable_log_record dest = { | |
540 | .update_index = 0, | |
541 | }; | |
542 | struct write_log_arg arg = { | |
543 | .log = &input, | |
544 | .update_index = 1, | |
545 | }; | |
546 | ||
547 | err = reftable_new_stack(&st, dir, cfg); | |
548 | EXPECT_ERR(err); | |
549 | ||
550 | input.value.update.message = "one\ntwo"; | |
551 | err = reftable_stack_add(st, &write_test_log, &arg); | |
552 | EXPECT(err == REFTABLE_API_ERROR); | |
553 | ||
554 | input.value.update.message = "one"; | |
555 | err = reftable_stack_add(st, &write_test_log, &arg); | |
556 | EXPECT_ERR(err); | |
557 | ||
558 | err = reftable_stack_read_log(st, input.refname, &dest); | |
559 | EXPECT_ERR(err); | |
560 | EXPECT(0 == strcmp(dest.value.update.message, "one\n")); | |
561 | ||
562 | input.value.update.message = "two\n"; | |
563 | arg.update_index = 2; | |
564 | err = reftable_stack_add(st, &write_test_log, &arg); | |
565 | EXPECT_ERR(err); | |
566 | err = reftable_stack_read_log(st, input.refname, &dest); | |
567 | EXPECT_ERR(err); | |
568 | EXPECT(0 == strcmp(dest.value.update.message, "two\n")); | |
569 | ||
570 | /* cleanup */ | |
571 | reftable_stack_destroy(st); | |
572 | reftable_log_record_release(&dest); | |
573 | clear_dir(dir); | |
574 | } | |
575 | ||
576 | static void test_reftable_stack_tombstone(void) | |
577 | { | |
578 | int i = 0; | |
579 | char *dir = get_tmp_dir(__LINE__); | |
580 | ||
581 | struct reftable_write_options cfg = { 0 }; | |
582 | struct reftable_stack *st = NULL; | |
583 | int err; | |
584 | struct reftable_ref_record refs[2] = { { NULL } }; | |
585 | struct reftable_log_record logs[2] = { { NULL } }; | |
586 | int N = ARRAY_SIZE(refs); | |
587 | struct reftable_ref_record dest = { NULL }; | |
588 | struct reftable_log_record log_dest = { NULL }; | |
589 | ||
590 | ||
591 | err = reftable_new_stack(&st, dir, cfg); | |
592 | EXPECT_ERR(err); | |
593 | ||
594 | /* even entries add the refs, odd entries delete them. */ | |
595 | for (i = 0; i < N; i++) { | |
596 | const char *buf = "branch"; | |
597 | refs[i].refname = xstrdup(buf); | |
598 | refs[i].update_index = i + 1; | |
599 | if (i % 2 == 0) { | |
600 | refs[i].value_type = REFTABLE_REF_VAL1; | |
e48d4272 HWN |
601 | set_test_hash(refs[i].value.val1, i); |
602 | } | |
603 | ||
604 | logs[i].refname = xstrdup(buf); | |
605 | /* update_index is part of the key. */ | |
606 | logs[i].update_index = 42; | |
607 | if (i % 2 == 0) { | |
608 | logs[i].value_type = REFTABLE_LOG_UPDATE; | |
609 | logs[i].value.update.new_hash = | |
610 | reftable_malloc(GIT_SHA1_RAWSZ); | |
611 | set_test_hash(logs[i].value.update.new_hash, i); | |
612 | logs[i].value.update.email = | |
613 | xstrdup("identity@invalid"); | |
614 | } | |
615 | } | |
616 | for (i = 0; i < N; i++) { | |
617 | int err = reftable_stack_add(st, &write_test_ref, &refs[i]); | |
618 | EXPECT_ERR(err); | |
619 | } | |
620 | ||
621 | for (i = 0; i < N; i++) { | |
622 | struct write_log_arg arg = { | |
623 | .log = &logs[i], | |
624 | .update_index = reftable_stack_next_update_index(st), | |
625 | }; | |
626 | int err = reftable_stack_add(st, &write_test_log, &arg); | |
627 | EXPECT_ERR(err); | |
628 | } | |
629 | ||
630 | err = reftable_stack_read_ref(st, "branch", &dest); | |
631 | EXPECT(err == 1); | |
632 | reftable_ref_record_release(&dest); | |
633 | ||
634 | err = reftable_stack_read_log(st, "branch", &log_dest); | |
635 | EXPECT(err == 1); | |
636 | reftable_log_record_release(&log_dest); | |
637 | ||
638 | err = reftable_stack_compact_all(st, NULL); | |
639 | EXPECT_ERR(err); | |
640 | ||
641 | err = reftable_stack_read_ref(st, "branch", &dest); | |
642 | EXPECT(err == 1); | |
643 | ||
644 | err = reftable_stack_read_log(st, "branch", &log_dest); | |
645 | EXPECT(err == 1); | |
646 | reftable_ref_record_release(&dest); | |
647 | reftable_log_record_release(&log_dest); | |
648 | ||
649 | /* cleanup */ | |
650 | reftable_stack_destroy(st); | |
651 | for (i = 0; i < N; i++) { | |
652 | reftable_ref_record_release(&refs[i]); | |
653 | reftable_log_record_release(&logs[i]); | |
654 | } | |
655 | clear_dir(dir); | |
656 | } | |
657 | ||
658 | static void test_reftable_stack_hash_id(void) | |
659 | { | |
660 | char *dir = get_tmp_dir(__LINE__); | |
661 | ||
662 | struct reftable_write_options cfg = { 0 }; | |
663 | struct reftable_stack *st = NULL; | |
664 | int err; | |
665 | ||
666 | struct reftable_ref_record ref = { | |
667 | .refname = "master", | |
668 | .value_type = REFTABLE_REF_SYMREF, | |
669 | .value.symref = "target", | |
670 | .update_index = 1, | |
671 | }; | |
672 | struct reftable_write_options cfg32 = { .hash_id = GIT_SHA256_FORMAT_ID }; | |
673 | struct reftable_stack *st32 = NULL; | |
674 | struct reftable_write_options cfg_default = { 0 }; | |
675 | struct reftable_stack *st_default = NULL; | |
676 | struct reftable_ref_record dest = { NULL }; | |
677 | ||
678 | err = reftable_new_stack(&st, dir, cfg); | |
679 | EXPECT_ERR(err); | |
680 | ||
681 | err = reftable_stack_add(st, &write_test_ref, &ref); | |
682 | EXPECT_ERR(err); | |
683 | ||
684 | /* can't read it with the wrong hash ID. */ | |
685 | err = reftable_new_stack(&st32, dir, cfg32); | |
686 | EXPECT(err == REFTABLE_FORMAT_ERROR); | |
687 | ||
688 | /* check that we can read it back with default config too. */ | |
689 | err = reftable_new_stack(&st_default, dir, cfg_default); | |
690 | EXPECT_ERR(err); | |
691 | ||
692 | err = reftable_stack_read_ref(st_default, "master", &dest); | |
693 | EXPECT_ERR(err); | |
694 | ||
695 | EXPECT(reftable_ref_record_equal(&ref, &dest, GIT_SHA1_RAWSZ)); | |
696 | reftable_ref_record_release(&dest); | |
697 | reftable_stack_destroy(st); | |
698 | reftable_stack_destroy(st_default); | |
699 | clear_dir(dir); | |
700 | } | |
701 | ||
702 | static void test_log2(void) | |
703 | { | |
704 | EXPECT(1 == fastlog2(3)); | |
705 | EXPECT(2 == fastlog2(4)); | |
706 | EXPECT(2 == fastlog2(5)); | |
707 | } | |
708 | ||
709 | static void test_sizes_to_segments(void) | |
710 | { | |
711 | uint64_t sizes[] = { 2, 3, 4, 5, 7, 9 }; | |
712 | /* .................0 1 2 3 4 5 */ | |
713 | ||
714 | int seglen = 0; | |
715 | struct segment *segs = | |
716 | sizes_to_segments(&seglen, sizes, ARRAY_SIZE(sizes)); | |
717 | EXPECT(segs[2].log == 3); | |
718 | EXPECT(segs[2].start == 5); | |
719 | EXPECT(segs[2].end == 6); | |
720 | ||
721 | EXPECT(segs[1].log == 2); | |
722 | EXPECT(segs[1].start == 2); | |
723 | EXPECT(segs[1].end == 5); | |
724 | reftable_free(segs); | |
725 | } | |
726 | ||
727 | static void test_sizes_to_segments_empty(void) | |
728 | { | |
729 | int seglen = 0; | |
730 | struct segment *segs = sizes_to_segments(&seglen, NULL, 0); | |
731 | EXPECT(seglen == 0); | |
732 | reftable_free(segs); | |
733 | } | |
734 | ||
735 | static void test_sizes_to_segments_all_equal(void) | |
736 | { | |
737 | uint64_t sizes[] = { 5, 5 }; | |
738 | ||
739 | int seglen = 0; | |
740 | struct segment *segs = | |
741 | sizes_to_segments(&seglen, sizes, ARRAY_SIZE(sizes)); | |
742 | EXPECT(seglen == 1); | |
743 | EXPECT(segs[0].start == 0); | |
744 | EXPECT(segs[0].end == 2); | |
745 | reftable_free(segs); | |
746 | } | |
747 | ||
748 | static void test_suggest_compaction_segment(void) | |
749 | { | |
750 | uint64_t sizes[] = { 128, 64, 17, 16, 9, 9, 9, 16, 16 }; | |
751 | /* .................0 1 2 3 4 5 6 */ | |
752 | struct segment min = | |
753 | suggest_compaction_segment(sizes, ARRAY_SIZE(sizes)); | |
754 | EXPECT(min.start == 2); | |
755 | EXPECT(min.end == 7); | |
756 | } | |
757 | ||
758 | static void test_suggest_compaction_segment_nothing(void) | |
759 | { | |
760 | uint64_t sizes[] = { 64, 32, 16, 8, 4, 2 }; | |
761 | struct segment result = | |
762 | suggest_compaction_segment(sizes, ARRAY_SIZE(sizes)); | |
763 | EXPECT(result.start == result.end); | |
764 | } | |
765 | ||
766 | static void test_reflog_expire(void) | |
767 | { | |
768 | char *dir = get_tmp_dir(__LINE__); | |
769 | ||
770 | struct reftable_write_options cfg = { 0 }; | |
771 | struct reftable_stack *st = NULL; | |
772 | struct reftable_log_record logs[20] = { { NULL } }; | |
773 | int N = ARRAY_SIZE(logs) - 1; | |
774 | int i = 0; | |
775 | int err; | |
776 | struct reftable_log_expiry_config expiry = { | |
777 | .time = 10, | |
778 | }; | |
779 | struct reftable_log_record log = { NULL }; | |
780 | ||
781 | ||
782 | err = reftable_new_stack(&st, dir, cfg); | |
783 | EXPECT_ERR(err); | |
784 | ||
785 | for (i = 1; i <= N; i++) { | |
786 | char buf[256]; | |
787 | snprintf(buf, sizeof(buf), "branch%02d", i); | |
788 | ||
789 | logs[i].refname = xstrdup(buf); | |
790 | logs[i].update_index = i; | |
791 | logs[i].value_type = REFTABLE_LOG_UPDATE; | |
792 | logs[i].value.update.time = i; | |
793 | logs[i].value.update.new_hash = reftable_malloc(GIT_SHA1_RAWSZ); | |
794 | logs[i].value.update.email = xstrdup("identity@invalid"); | |
795 | set_test_hash(logs[i].value.update.new_hash, i); | |
796 | } | |
797 | ||
798 | for (i = 1; i <= N; i++) { | |
799 | struct write_log_arg arg = { | |
800 | .log = &logs[i], | |
801 | .update_index = reftable_stack_next_update_index(st), | |
802 | }; | |
803 | int err = reftable_stack_add(st, &write_test_log, &arg); | |
804 | EXPECT_ERR(err); | |
805 | } | |
806 | ||
807 | err = reftable_stack_compact_all(st, NULL); | |
808 | EXPECT_ERR(err); | |
809 | ||
810 | err = reftable_stack_compact_all(st, &expiry); | |
811 | EXPECT_ERR(err); | |
812 | ||
813 | err = reftable_stack_read_log(st, logs[9].refname, &log); | |
814 | EXPECT(err == 1); | |
815 | ||
816 | err = reftable_stack_read_log(st, logs[11].refname, &log); | |
817 | EXPECT_ERR(err); | |
818 | ||
819 | expiry.min_update_index = 15; | |
820 | err = reftable_stack_compact_all(st, &expiry); | |
821 | EXPECT_ERR(err); | |
822 | ||
823 | err = reftable_stack_read_log(st, logs[14].refname, &log); | |
824 | EXPECT(err == 1); | |
825 | ||
826 | err = reftable_stack_read_log(st, logs[16].refname, &log); | |
827 | EXPECT_ERR(err); | |
828 | ||
829 | /* cleanup */ | |
830 | reftable_stack_destroy(st); | |
831 | for (i = 0; i <= N; i++) { | |
832 | reftable_log_record_release(&logs[i]); | |
833 | } | |
834 | clear_dir(dir); | |
835 | reftable_log_record_release(&log); | |
836 | } | |
837 | ||
838 | static int write_nothing(struct reftable_writer *wr, void *arg) | |
839 | { | |
840 | reftable_writer_set_limits(wr, 1, 1); | |
841 | return 0; | |
842 | } | |
843 | ||
844 | static void test_empty_add(void) | |
845 | { | |
846 | struct reftable_write_options cfg = { 0 }; | |
847 | struct reftable_stack *st = NULL; | |
848 | int err; | |
849 | char *dir = get_tmp_dir(__LINE__); | |
850 | ||
851 | struct reftable_stack *st2 = NULL; | |
852 | ||
853 | ||
854 | err = reftable_new_stack(&st, dir, cfg); | |
855 | EXPECT_ERR(err); | |
856 | ||
857 | err = reftable_stack_add(st, &write_nothing, NULL); | |
858 | EXPECT_ERR(err); | |
859 | ||
860 | err = reftable_new_stack(&st2, dir, cfg); | |
861 | EXPECT_ERR(err); | |
862 | clear_dir(dir); | |
863 | reftable_stack_destroy(st); | |
864 | reftable_stack_destroy(st2); | |
865 | } | |
866 | ||
867 | static void test_reftable_stack_auto_compaction(void) | |
868 | { | |
869 | struct reftable_write_options cfg = { 0 }; | |
870 | struct reftable_stack *st = NULL; | |
871 | char *dir = get_tmp_dir(__LINE__); | |
872 | ||
873 | int err, i; | |
874 | int N = 100; | |
875 | ||
876 | err = reftable_new_stack(&st, dir, cfg); | |
877 | EXPECT_ERR(err); | |
878 | ||
879 | st->disable_auto_compact = 1; /* call manually below for coverage. */ | |
880 | for (i = 0; i < N; i++) { | |
881 | char name[100]; | |
882 | struct reftable_ref_record ref = { | |
883 | .refname = name, | |
884 | .update_index = reftable_stack_next_update_index(st), | |
885 | .value_type = REFTABLE_REF_SYMREF, | |
886 | .value.symref = "master", | |
887 | }; | |
888 | snprintf(name, sizeof(name), "branch%04d", i); | |
889 | ||
890 | err = reftable_stack_add(st, &write_test_ref, &ref); | |
891 | EXPECT_ERR(err); | |
892 | ||
893 | err = reftable_stack_auto_compact(st); | |
f7445865 | 894 | EXPECT_ERR(err); |
e48d4272 HWN |
895 | EXPECT(i < 3 || st->merged->stack_len < 2 * fastlog2(i)); |
896 | } | |
897 | ||
898 | EXPECT(reftable_stack_compaction_stats(st)->entries_written < | |
899 | (uint64_t)(N * fastlog2(N))); | |
900 | ||
901 | reftable_stack_destroy(st); | |
902 | clear_dir(dir); | |
903 | } | |
904 | ||
15f98b60 PS |
905 | static void test_reftable_stack_add_performs_auto_compaction(void) |
906 | { | |
907 | struct reftable_write_options cfg = { 0 }; | |
908 | struct reftable_stack *st = NULL; | |
909 | struct strbuf refname = STRBUF_INIT; | |
910 | char *dir = get_tmp_dir(__LINE__); | |
911 | int err, i, n = 20; | |
912 | ||
913 | err = reftable_new_stack(&st, dir, cfg); | |
914 | EXPECT_ERR(err); | |
915 | ||
916 | for (i = 0; i <= n; i++) { | |
917 | struct reftable_ref_record ref = { | |
918 | .update_index = reftable_stack_next_update_index(st), | |
919 | .value_type = REFTABLE_REF_SYMREF, | |
920 | .value.symref = "master", | |
921 | }; | |
922 | ||
923 | /* | |
924 | * Disable auto-compaction for all but the last runs. Like this | |
925 | * we can ensure that we indeed honor this setting and have | |
926 | * better control over when exactly auto compaction runs. | |
927 | */ | |
928 | st->disable_auto_compact = i != n; | |
929 | ||
930 | strbuf_reset(&refname); | |
931 | strbuf_addf(&refname, "branch-%04d", i); | |
932 | ref.refname = refname.buf; | |
933 | ||
934 | err = reftable_stack_add(st, &write_test_ref, &ref); | |
935 | EXPECT_ERR(err); | |
936 | ||
937 | /* | |
938 | * The stack length should grow continuously for all runs where | |
939 | * auto compaction is disabled. When enabled, we should merge | |
940 | * all tables in the stack. | |
941 | */ | |
942 | if (i != n) | |
943 | EXPECT(st->merged->stack_len == i + 1); | |
944 | else | |
945 | EXPECT(st->merged->stack_len == 1); | |
946 | } | |
947 | ||
948 | reftable_stack_destroy(st); | |
949 | strbuf_release(&refname); | |
950 | clear_dir(dir); | |
951 | } | |
952 | ||
e48d4272 HWN |
953 | static void test_reftable_stack_compaction_concurrent(void) |
954 | { | |
955 | struct reftable_write_options cfg = { 0 }; | |
956 | struct reftable_stack *st1 = NULL, *st2 = NULL; | |
957 | char *dir = get_tmp_dir(__LINE__); | |
958 | ||
959 | int err, i; | |
960 | int N = 3; | |
961 | ||
962 | err = reftable_new_stack(&st1, dir, cfg); | |
963 | EXPECT_ERR(err); | |
964 | ||
965 | for (i = 0; i < N; i++) { | |
966 | char name[100]; | |
967 | struct reftable_ref_record ref = { | |
968 | .refname = name, | |
969 | .update_index = reftable_stack_next_update_index(st1), | |
970 | .value_type = REFTABLE_REF_SYMREF, | |
971 | .value.symref = "master", | |
972 | }; | |
973 | snprintf(name, sizeof(name), "branch%04d", i); | |
974 | ||
975 | err = reftable_stack_add(st1, &write_test_ref, &ref); | |
976 | EXPECT_ERR(err); | |
977 | } | |
978 | ||
979 | err = reftable_new_stack(&st2, dir, cfg); | |
980 | EXPECT_ERR(err); | |
981 | ||
982 | err = reftable_stack_compact_all(st1, NULL); | |
983 | EXPECT_ERR(err); | |
984 | ||
985 | reftable_stack_destroy(st1); | |
986 | reftable_stack_destroy(st2); | |
987 | ||
988 | EXPECT(count_dir_entries(dir) == 2); | |
989 | clear_dir(dir); | |
990 | } | |
991 | ||
992 | static void unclean_stack_close(struct reftable_stack *st) | |
993 | { | |
994 | /* break abstraction boundary to simulate unclean shutdown. */ | |
995 | int i = 0; | |
996 | for (; i < st->readers_len; i++) { | |
997 | reftable_reader_free(st->readers[i]); | |
998 | } | |
999 | st->readers_len = 0; | |
1000 | FREE_AND_NULL(st->readers); | |
1001 | } | |
1002 | ||
1003 | static void test_reftable_stack_compaction_concurrent_clean(void) | |
1004 | { | |
1005 | struct reftable_write_options cfg = { 0 }; | |
1006 | struct reftable_stack *st1 = NULL, *st2 = NULL, *st3 = NULL; | |
1007 | char *dir = get_tmp_dir(__LINE__); | |
1008 | ||
1009 | int err, i; | |
1010 | int N = 3; | |
1011 | ||
1012 | err = reftable_new_stack(&st1, dir, cfg); | |
1013 | EXPECT_ERR(err); | |
1014 | ||
1015 | for (i = 0; i < N; i++) { | |
1016 | char name[100]; | |
1017 | struct reftable_ref_record ref = { | |
1018 | .refname = name, | |
1019 | .update_index = reftable_stack_next_update_index(st1), | |
1020 | .value_type = REFTABLE_REF_SYMREF, | |
1021 | .value.symref = "master", | |
1022 | }; | |
1023 | snprintf(name, sizeof(name), "branch%04d", i); | |
1024 | ||
1025 | err = reftable_stack_add(st1, &write_test_ref, &ref); | |
1026 | EXPECT_ERR(err); | |
1027 | } | |
1028 | ||
1029 | err = reftable_new_stack(&st2, dir, cfg); | |
1030 | EXPECT_ERR(err); | |
1031 | ||
1032 | err = reftable_stack_compact_all(st1, NULL); | |
1033 | EXPECT_ERR(err); | |
1034 | ||
1035 | unclean_stack_close(st1); | |
1036 | unclean_stack_close(st2); | |
1037 | ||
1038 | err = reftable_new_stack(&st3, dir, cfg); | |
1039 | EXPECT_ERR(err); | |
1040 | ||
1041 | err = reftable_stack_clean(st3); | |
1042 | EXPECT_ERR(err); | |
1043 | EXPECT(count_dir_entries(dir) == 2); | |
1044 | ||
1045 | reftable_stack_destroy(st1); | |
1046 | reftable_stack_destroy(st2); | |
1047 | reftable_stack_destroy(st3); | |
1048 | ||
1049 | clear_dir(dir); | |
1050 | } | |
1051 | ||
1052 | int stack_test_main(int argc, const char *argv[]) | |
1053 | { | |
1054 | RUN_TEST(test_empty_add); | |
1055 | RUN_TEST(test_log2); | |
1056 | RUN_TEST(test_names_equal); | |
1057 | RUN_TEST(test_parse_names); | |
1058 | RUN_TEST(test_read_file); | |
1059 | RUN_TEST(test_reflog_expire); | |
1060 | RUN_TEST(test_reftable_stack_add); | |
1061 | RUN_TEST(test_reftable_stack_add_one); | |
1062 | RUN_TEST(test_reftable_stack_auto_compaction); | |
15f98b60 | 1063 | RUN_TEST(test_reftable_stack_add_performs_auto_compaction); |
e48d4272 HWN |
1064 | RUN_TEST(test_reftable_stack_compaction_concurrent); |
1065 | RUN_TEST(test_reftable_stack_compaction_concurrent_clean); | |
1066 | RUN_TEST(test_reftable_stack_hash_id); | |
1067 | RUN_TEST(test_reftable_stack_lock_failure); | |
1068 | RUN_TEST(test_reftable_stack_log_normalize); | |
1069 | RUN_TEST(test_reftable_stack_tombstone); | |
1070 | RUN_TEST(test_reftable_stack_transaction_api); | |
5c086453 | 1071 | RUN_TEST(test_reftable_stack_transaction_api_performs_auto_compaction); |
e48d4272 HWN |
1072 | RUN_TEST(test_reftable_stack_update_index_check); |
1073 | RUN_TEST(test_reftable_stack_uptodate); | |
1074 | RUN_TEST(test_reftable_stack_validate_refname); | |
1075 | RUN_TEST(test_sizes_to_segments); | |
1076 | RUN_TEST(test_sizes_to_segments_all_equal); | |
1077 | RUN_TEST(test_sizes_to_segments_empty); | |
1078 | RUN_TEST(test_suggest_compaction_segment); | |
1079 | RUN_TEST(test_suggest_compaction_segment_nothing); | |
1080 | return 0; | |
1081 | } |