]> git.ipfire.org Git - thirdparty/git.git/blob - reftable/stack_test.c
Merge branch 'ps/reftable-styles'
[thirdparty/git.git] / reftable / stack_test.c
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"
16 #include "record.h"
17 #include "test_framework.h"
18 #include "reftable-tests.h"
19 #include "reader.h"
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;
37 if (!dir)
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);
80 n = write_in_full(fd, out, strlen(out));
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);
92 (void) remove(fn);
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__);
141 struct strbuf scratch = STRBUF_INIT;
142 int mask = umask(002);
143 struct reftable_write_options cfg = {
144 .default_permissions = 0660,
145 };
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 };
155 struct stat stat_result = { 0 };
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));
165 EXPECT(st->readers_len > 0);
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
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
193 reftable_ref_record_release(&dest);
194 reftable_stack_destroy(st);
195 strbuf_release(&scratch);
196 clear_dir(dir);
197 umask(mask);
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
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
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 .default_permissions = 0660,
447 };
448 struct reftable_stack *st = NULL;
449 char *dir = get_tmp_dir(__LINE__);
450 struct reftable_ref_record refs[2] = { { NULL } };
451 struct reftable_log_record logs[2] = { { NULL } };
452 struct strbuf path = STRBUF_INIT;
453 struct stat stat_result;
454 int N = ARRAY_SIZE(refs);
455
456 err = reftable_new_stack(&st, dir, cfg);
457 EXPECT_ERR(err);
458 st->disable_auto_compact = 1;
459
460 for (i = 0; i < N; i++) {
461 char buf[256];
462 snprintf(buf, sizeof(buf), "branch%02d", i);
463 refs[i].refname = xstrdup(buf);
464 refs[i].update_index = i + 1;
465 refs[i].value_type = REFTABLE_REF_VAL1;
466 set_test_hash(refs[i].value.val1, i);
467
468 logs[i].refname = xstrdup(buf);
469 logs[i].update_index = N + i + 1;
470 logs[i].value_type = REFTABLE_LOG_UPDATE;
471
472 logs[i].value.update.new_hash = reftable_malloc(GIT_SHA1_RAWSZ);
473 logs[i].value.update.email = xstrdup("identity@invalid");
474 set_test_hash(logs[i].value.update.new_hash, i);
475 }
476
477 for (i = 0; i < N; i++) {
478 int err = reftable_stack_add(st, &write_test_ref, &refs[i]);
479 EXPECT_ERR(err);
480 }
481
482 for (i = 0; i < N; i++) {
483 struct write_log_arg arg = {
484 .log = &logs[i],
485 .update_index = reftable_stack_next_update_index(st),
486 };
487 int err = reftable_stack_add(st, &write_test_log, &arg);
488 EXPECT_ERR(err);
489 }
490
491 err = reftable_stack_compact_all(st, NULL);
492 EXPECT_ERR(err);
493
494 for (i = 0; i < N; i++) {
495 struct reftable_ref_record dest = { NULL };
496
497 int err = reftable_stack_read_ref(st, refs[i].refname, &dest);
498 EXPECT_ERR(err);
499 EXPECT(reftable_ref_record_equal(&dest, refs + i,
500 GIT_SHA1_RAWSZ));
501 reftable_ref_record_release(&dest);
502 }
503
504 for (i = 0; i < N; i++) {
505 struct reftable_log_record dest = { NULL };
506 int err = reftable_stack_read_log(st, refs[i].refname, &dest);
507 EXPECT_ERR(err);
508 EXPECT(reftable_log_record_equal(&dest, logs + i,
509 GIT_SHA1_RAWSZ));
510 reftable_log_record_release(&dest);
511 }
512
513 #ifndef GIT_WINDOWS_NATIVE
514 strbuf_addstr(&path, dir);
515 strbuf_addstr(&path, "/tables.list");
516 err = stat(path.buf, &stat_result);
517 EXPECT(!err);
518 EXPECT((stat_result.st_mode & 0777) == cfg.default_permissions);
519
520 strbuf_reset(&path);
521 strbuf_addstr(&path, dir);
522 strbuf_addstr(&path, "/");
523 /* do not try at home; not an external API for reftable. */
524 strbuf_addstr(&path, st->readers[0]->name);
525 err = stat(path.buf, &stat_result);
526 EXPECT(!err);
527 EXPECT((stat_result.st_mode & 0777) == cfg.default_permissions);
528 #else
529 (void) stat_result;
530 #endif
531
532 /* cleanup */
533 reftable_stack_destroy(st);
534 for (i = 0; i < N; i++) {
535 reftable_ref_record_release(&refs[i]);
536 reftable_log_record_release(&logs[i]);
537 }
538 strbuf_release(&path);
539 clear_dir(dir);
540 }
541
542 static void test_reftable_stack_log_normalize(void)
543 {
544 int err = 0;
545 struct reftable_write_options cfg = {
546 0,
547 };
548 struct reftable_stack *st = NULL;
549 char *dir = get_tmp_dir(__LINE__);
550
551 uint8_t h1[GIT_SHA1_RAWSZ] = { 0x01 }, h2[GIT_SHA1_RAWSZ] = { 0x02 };
552
553 struct reftable_log_record input = { .refname = "branch",
554 .update_index = 1,
555 .value_type = REFTABLE_LOG_UPDATE,
556 .value = { .update = {
557 .new_hash = h1,
558 .old_hash = h2,
559 } } };
560 struct reftable_log_record dest = {
561 .update_index = 0,
562 };
563 struct write_log_arg arg = {
564 .log = &input,
565 .update_index = 1,
566 };
567
568 err = reftable_new_stack(&st, dir, cfg);
569 EXPECT_ERR(err);
570
571 input.value.update.message = "one\ntwo";
572 err = reftable_stack_add(st, &write_test_log, &arg);
573 EXPECT(err == REFTABLE_API_ERROR);
574
575 input.value.update.message = "one";
576 err = reftable_stack_add(st, &write_test_log, &arg);
577 EXPECT_ERR(err);
578
579 err = reftable_stack_read_log(st, input.refname, &dest);
580 EXPECT_ERR(err);
581 EXPECT(0 == strcmp(dest.value.update.message, "one\n"));
582
583 input.value.update.message = "two\n";
584 arg.update_index = 2;
585 err = reftable_stack_add(st, &write_test_log, &arg);
586 EXPECT_ERR(err);
587 err = reftable_stack_read_log(st, input.refname, &dest);
588 EXPECT_ERR(err);
589 EXPECT(0 == strcmp(dest.value.update.message, "two\n"));
590
591 /* cleanup */
592 reftable_stack_destroy(st);
593 reftable_log_record_release(&dest);
594 clear_dir(dir);
595 }
596
597 static void test_reftable_stack_tombstone(void)
598 {
599 int i = 0;
600 char *dir = get_tmp_dir(__LINE__);
601
602 struct reftable_write_options cfg = { 0 };
603 struct reftable_stack *st = NULL;
604 int err;
605 struct reftable_ref_record refs[2] = { { NULL } };
606 struct reftable_log_record logs[2] = { { NULL } };
607 int N = ARRAY_SIZE(refs);
608 struct reftable_ref_record dest = { NULL };
609 struct reftable_log_record log_dest = { NULL };
610
611
612 err = reftable_new_stack(&st, dir, cfg);
613 EXPECT_ERR(err);
614
615 /* even entries add the refs, odd entries delete them. */
616 for (i = 0; i < N; i++) {
617 const char *buf = "branch";
618 refs[i].refname = xstrdup(buf);
619 refs[i].update_index = i + 1;
620 if (i % 2 == 0) {
621 refs[i].value_type = REFTABLE_REF_VAL1;
622 set_test_hash(refs[i].value.val1, i);
623 }
624
625 logs[i].refname = xstrdup(buf);
626 /* update_index is part of the key. */
627 logs[i].update_index = 42;
628 if (i % 2 == 0) {
629 logs[i].value_type = REFTABLE_LOG_UPDATE;
630 logs[i].value.update.new_hash =
631 reftable_malloc(GIT_SHA1_RAWSZ);
632 set_test_hash(logs[i].value.update.new_hash, i);
633 logs[i].value.update.email =
634 xstrdup("identity@invalid");
635 }
636 }
637 for (i = 0; i < N; i++) {
638 int err = reftable_stack_add(st, &write_test_ref, &refs[i]);
639 EXPECT_ERR(err);
640 }
641
642 for (i = 0; i < N; i++) {
643 struct write_log_arg arg = {
644 .log = &logs[i],
645 .update_index = reftable_stack_next_update_index(st),
646 };
647 int err = reftable_stack_add(st, &write_test_log, &arg);
648 EXPECT_ERR(err);
649 }
650
651 err = reftable_stack_read_ref(st, "branch", &dest);
652 EXPECT(err == 1);
653 reftable_ref_record_release(&dest);
654
655 err = reftable_stack_read_log(st, "branch", &log_dest);
656 EXPECT(err == 1);
657 reftable_log_record_release(&log_dest);
658
659 err = reftable_stack_compact_all(st, NULL);
660 EXPECT_ERR(err);
661
662 err = reftable_stack_read_ref(st, "branch", &dest);
663 EXPECT(err == 1);
664
665 err = reftable_stack_read_log(st, "branch", &log_dest);
666 EXPECT(err == 1);
667 reftable_ref_record_release(&dest);
668 reftable_log_record_release(&log_dest);
669
670 /* cleanup */
671 reftable_stack_destroy(st);
672 for (i = 0; i < N; i++) {
673 reftable_ref_record_release(&refs[i]);
674 reftable_log_record_release(&logs[i]);
675 }
676 clear_dir(dir);
677 }
678
679 static void test_reftable_stack_hash_id(void)
680 {
681 char *dir = get_tmp_dir(__LINE__);
682
683 struct reftable_write_options cfg = { 0 };
684 struct reftable_stack *st = NULL;
685 int err;
686
687 struct reftable_ref_record ref = {
688 .refname = "master",
689 .value_type = REFTABLE_REF_SYMREF,
690 .value.symref = "target",
691 .update_index = 1,
692 };
693 struct reftable_write_options cfg32 = { .hash_id = GIT_SHA256_FORMAT_ID };
694 struct reftable_stack *st32 = NULL;
695 struct reftable_write_options cfg_default = { 0 };
696 struct reftable_stack *st_default = NULL;
697 struct reftable_ref_record dest = { NULL };
698
699 err = reftable_new_stack(&st, dir, cfg);
700 EXPECT_ERR(err);
701
702 err = reftable_stack_add(st, &write_test_ref, &ref);
703 EXPECT_ERR(err);
704
705 /* can't read it with the wrong hash ID. */
706 err = reftable_new_stack(&st32, dir, cfg32);
707 EXPECT(err == REFTABLE_FORMAT_ERROR);
708
709 /* check that we can read it back with default config too. */
710 err = reftable_new_stack(&st_default, dir, cfg_default);
711 EXPECT_ERR(err);
712
713 err = reftable_stack_read_ref(st_default, "master", &dest);
714 EXPECT_ERR(err);
715
716 EXPECT(reftable_ref_record_equal(&ref, &dest, GIT_SHA1_RAWSZ));
717 reftable_ref_record_release(&dest);
718 reftable_stack_destroy(st);
719 reftable_stack_destroy(st_default);
720 clear_dir(dir);
721 }
722
723 static void test_log2(void)
724 {
725 EXPECT(1 == fastlog2(3));
726 EXPECT(2 == fastlog2(4));
727 EXPECT(2 == fastlog2(5));
728 }
729
730 static void test_sizes_to_segments(void)
731 {
732 uint64_t sizes[] = { 2, 3, 4, 5, 7, 9 };
733 /* .................0 1 2 3 4 5 */
734
735 size_t seglen = 0;
736 struct segment *segs =
737 sizes_to_segments(&seglen, sizes, ARRAY_SIZE(sizes));
738 EXPECT(segs[2].log == 3);
739 EXPECT(segs[2].start == 5);
740 EXPECT(segs[2].end == 6);
741
742 EXPECT(segs[1].log == 2);
743 EXPECT(segs[1].start == 2);
744 EXPECT(segs[1].end == 5);
745 reftable_free(segs);
746 }
747
748 static void test_sizes_to_segments_empty(void)
749 {
750 size_t seglen = 0;
751 struct segment *segs = sizes_to_segments(&seglen, NULL, 0);
752 EXPECT(seglen == 0);
753 reftable_free(segs);
754 }
755
756 static void test_sizes_to_segments_all_equal(void)
757 {
758 uint64_t sizes[] = { 5, 5 };
759 size_t seglen = 0;
760 struct segment *segs =
761 sizes_to_segments(&seglen, sizes, ARRAY_SIZE(sizes));
762 EXPECT(seglen == 1);
763 EXPECT(segs[0].start == 0);
764 EXPECT(segs[0].end == 2);
765 reftable_free(segs);
766 }
767
768 static void test_suggest_compaction_segment(void)
769 {
770 uint64_t sizes[] = { 128, 64, 17, 16, 9, 9, 9, 16, 16 };
771 /* .................0 1 2 3 4 5 6 */
772 struct segment min =
773 suggest_compaction_segment(sizes, ARRAY_SIZE(sizes));
774 EXPECT(min.start == 2);
775 EXPECT(min.end == 7);
776 }
777
778 static void test_suggest_compaction_segment_nothing(void)
779 {
780 uint64_t sizes[] = { 64, 32, 16, 8, 4, 2 };
781 struct segment result =
782 suggest_compaction_segment(sizes, ARRAY_SIZE(sizes));
783 EXPECT(result.start == result.end);
784 }
785
786 static void test_reflog_expire(void)
787 {
788 char *dir = get_tmp_dir(__LINE__);
789
790 struct reftable_write_options cfg = { 0 };
791 struct reftable_stack *st = NULL;
792 struct reftable_log_record logs[20] = { { NULL } };
793 int N = ARRAY_SIZE(logs) - 1;
794 int i = 0;
795 int err;
796 struct reftable_log_expiry_config expiry = {
797 .time = 10,
798 };
799 struct reftable_log_record log = { NULL };
800
801
802 err = reftable_new_stack(&st, dir, cfg);
803 EXPECT_ERR(err);
804
805 for (i = 1; i <= N; i++) {
806 char buf[256];
807 snprintf(buf, sizeof(buf), "branch%02d", i);
808
809 logs[i].refname = xstrdup(buf);
810 logs[i].update_index = i;
811 logs[i].value_type = REFTABLE_LOG_UPDATE;
812 logs[i].value.update.time = i;
813 logs[i].value.update.new_hash = reftable_malloc(GIT_SHA1_RAWSZ);
814 logs[i].value.update.email = xstrdup("identity@invalid");
815 set_test_hash(logs[i].value.update.new_hash, i);
816 }
817
818 for (i = 1; i <= N; i++) {
819 struct write_log_arg arg = {
820 .log = &logs[i],
821 .update_index = reftable_stack_next_update_index(st),
822 };
823 int err = reftable_stack_add(st, &write_test_log, &arg);
824 EXPECT_ERR(err);
825 }
826
827 err = reftable_stack_compact_all(st, NULL);
828 EXPECT_ERR(err);
829
830 err = reftable_stack_compact_all(st, &expiry);
831 EXPECT_ERR(err);
832
833 err = reftable_stack_read_log(st, logs[9].refname, &log);
834 EXPECT(err == 1);
835
836 err = reftable_stack_read_log(st, logs[11].refname, &log);
837 EXPECT_ERR(err);
838
839 expiry.min_update_index = 15;
840 err = reftable_stack_compact_all(st, &expiry);
841 EXPECT_ERR(err);
842
843 err = reftable_stack_read_log(st, logs[14].refname, &log);
844 EXPECT(err == 1);
845
846 err = reftable_stack_read_log(st, logs[16].refname, &log);
847 EXPECT_ERR(err);
848
849 /* cleanup */
850 reftable_stack_destroy(st);
851 for (i = 0; i <= N; i++) {
852 reftable_log_record_release(&logs[i]);
853 }
854 clear_dir(dir);
855 reftable_log_record_release(&log);
856 }
857
858 static int write_nothing(struct reftable_writer *wr, void *arg)
859 {
860 reftable_writer_set_limits(wr, 1, 1);
861 return 0;
862 }
863
864 static void test_empty_add(void)
865 {
866 struct reftable_write_options cfg = { 0 };
867 struct reftable_stack *st = NULL;
868 int err;
869 char *dir = get_tmp_dir(__LINE__);
870
871 struct reftable_stack *st2 = NULL;
872
873
874 err = reftable_new_stack(&st, dir, cfg);
875 EXPECT_ERR(err);
876
877 err = reftable_stack_add(st, &write_nothing, NULL);
878 EXPECT_ERR(err);
879
880 err = reftable_new_stack(&st2, dir, cfg);
881 EXPECT_ERR(err);
882 clear_dir(dir);
883 reftable_stack_destroy(st);
884 reftable_stack_destroy(st2);
885 }
886
887 static void test_reftable_stack_auto_compaction(void)
888 {
889 struct reftable_write_options cfg = { 0 };
890 struct reftable_stack *st = NULL;
891 char *dir = get_tmp_dir(__LINE__);
892
893 int err, i;
894 int N = 100;
895
896 err = reftable_new_stack(&st, dir, cfg);
897 EXPECT_ERR(err);
898
899 st->disable_auto_compact = 1; /* call manually below for coverage. */
900 for (i = 0; i < N; i++) {
901 char name[100];
902 struct reftable_ref_record ref = {
903 .refname = name,
904 .update_index = reftable_stack_next_update_index(st),
905 .value_type = REFTABLE_REF_SYMREF,
906 .value.symref = "master",
907 };
908 snprintf(name, sizeof(name), "branch%04d", i);
909
910 err = reftable_stack_add(st, &write_test_ref, &ref);
911 EXPECT_ERR(err);
912
913 err = reftable_stack_auto_compact(st);
914 EXPECT_ERR(err);
915 EXPECT(i < 3 || st->merged->stack_len < 2 * fastlog2(i));
916 }
917
918 EXPECT(reftable_stack_compaction_stats(st)->entries_written <
919 (uint64_t)(N * fastlog2(N)));
920
921 reftable_stack_destroy(st);
922 clear_dir(dir);
923 }
924
925 static void test_reftable_stack_add_performs_auto_compaction(void)
926 {
927 struct reftable_write_options cfg = { 0 };
928 struct reftable_stack *st = NULL;
929 struct strbuf refname = STRBUF_INIT;
930 char *dir = get_tmp_dir(__LINE__);
931 int err, i, n = 20;
932
933 err = reftable_new_stack(&st, dir, cfg);
934 EXPECT_ERR(err);
935
936 for (i = 0; i <= n; i++) {
937 struct reftable_ref_record ref = {
938 .update_index = reftable_stack_next_update_index(st),
939 .value_type = REFTABLE_REF_SYMREF,
940 .value.symref = "master",
941 };
942
943 /*
944 * Disable auto-compaction for all but the last runs. Like this
945 * we can ensure that we indeed honor this setting and have
946 * better control over when exactly auto compaction runs.
947 */
948 st->disable_auto_compact = i != n;
949
950 strbuf_reset(&refname);
951 strbuf_addf(&refname, "branch-%04d", i);
952 ref.refname = refname.buf;
953
954 err = reftable_stack_add(st, &write_test_ref, &ref);
955 EXPECT_ERR(err);
956
957 /*
958 * The stack length should grow continuously for all runs where
959 * auto compaction is disabled. When enabled, we should merge
960 * all tables in the stack.
961 */
962 if (i != n)
963 EXPECT(st->merged->stack_len == i + 1);
964 else
965 EXPECT(st->merged->stack_len == 1);
966 }
967
968 reftable_stack_destroy(st);
969 strbuf_release(&refname);
970 clear_dir(dir);
971 }
972
973 static void test_reftable_stack_compaction_concurrent(void)
974 {
975 struct reftable_write_options cfg = { 0 };
976 struct reftable_stack *st1 = NULL, *st2 = NULL;
977 char *dir = get_tmp_dir(__LINE__);
978
979 int err, i;
980 int N = 3;
981
982 err = reftable_new_stack(&st1, dir, cfg);
983 EXPECT_ERR(err);
984
985 for (i = 0; i < N; i++) {
986 char name[100];
987 struct reftable_ref_record ref = {
988 .refname = name,
989 .update_index = reftable_stack_next_update_index(st1),
990 .value_type = REFTABLE_REF_SYMREF,
991 .value.symref = "master",
992 };
993 snprintf(name, sizeof(name), "branch%04d", i);
994
995 err = reftable_stack_add(st1, &write_test_ref, &ref);
996 EXPECT_ERR(err);
997 }
998
999 err = reftable_new_stack(&st2, dir, cfg);
1000 EXPECT_ERR(err);
1001
1002 err = reftable_stack_compact_all(st1, NULL);
1003 EXPECT_ERR(err);
1004
1005 reftable_stack_destroy(st1);
1006 reftable_stack_destroy(st2);
1007
1008 EXPECT(count_dir_entries(dir) == 2);
1009 clear_dir(dir);
1010 }
1011
1012 static void unclean_stack_close(struct reftable_stack *st)
1013 {
1014 /* break abstraction boundary to simulate unclean shutdown. */
1015 int i = 0;
1016 for (; i < st->readers_len; i++) {
1017 reftable_reader_free(st->readers[i]);
1018 }
1019 st->readers_len = 0;
1020 FREE_AND_NULL(st->readers);
1021 }
1022
1023 static void test_reftable_stack_compaction_concurrent_clean(void)
1024 {
1025 struct reftable_write_options cfg = { 0 };
1026 struct reftable_stack *st1 = NULL, *st2 = NULL, *st3 = NULL;
1027 char *dir = get_tmp_dir(__LINE__);
1028
1029 int err, i;
1030 int N = 3;
1031
1032 err = reftable_new_stack(&st1, dir, cfg);
1033 EXPECT_ERR(err);
1034
1035 for (i = 0; i < N; i++) {
1036 char name[100];
1037 struct reftable_ref_record ref = {
1038 .refname = name,
1039 .update_index = reftable_stack_next_update_index(st1),
1040 .value_type = REFTABLE_REF_SYMREF,
1041 .value.symref = "master",
1042 };
1043 snprintf(name, sizeof(name), "branch%04d", i);
1044
1045 err = reftable_stack_add(st1, &write_test_ref, &ref);
1046 EXPECT_ERR(err);
1047 }
1048
1049 err = reftable_new_stack(&st2, dir, cfg);
1050 EXPECT_ERR(err);
1051
1052 err = reftable_stack_compact_all(st1, NULL);
1053 EXPECT_ERR(err);
1054
1055 unclean_stack_close(st1);
1056 unclean_stack_close(st2);
1057
1058 err = reftable_new_stack(&st3, dir, cfg);
1059 EXPECT_ERR(err);
1060
1061 err = reftable_stack_clean(st3);
1062 EXPECT_ERR(err);
1063 EXPECT(count_dir_entries(dir) == 2);
1064
1065 reftable_stack_destroy(st1);
1066 reftable_stack_destroy(st2);
1067 reftable_stack_destroy(st3);
1068
1069 clear_dir(dir);
1070 }
1071
1072 int stack_test_main(int argc, const char *argv[])
1073 {
1074 RUN_TEST(test_empty_add);
1075 RUN_TEST(test_log2);
1076 RUN_TEST(test_names_equal);
1077 RUN_TEST(test_parse_names);
1078 RUN_TEST(test_read_file);
1079 RUN_TEST(test_reflog_expire);
1080 RUN_TEST(test_reftable_stack_add);
1081 RUN_TEST(test_reftable_stack_add_one);
1082 RUN_TEST(test_reftable_stack_auto_compaction);
1083 RUN_TEST(test_reftable_stack_add_performs_auto_compaction);
1084 RUN_TEST(test_reftable_stack_compaction_concurrent);
1085 RUN_TEST(test_reftable_stack_compaction_concurrent_clean);
1086 RUN_TEST(test_reftable_stack_hash_id);
1087 RUN_TEST(test_reftable_stack_lock_failure);
1088 RUN_TEST(test_reftable_stack_log_normalize);
1089 RUN_TEST(test_reftable_stack_tombstone);
1090 RUN_TEST(test_reftable_stack_transaction_api);
1091 RUN_TEST(test_reftable_stack_transaction_api_performs_auto_compaction);
1092 RUN_TEST(test_reftable_stack_update_index_check);
1093 RUN_TEST(test_reftable_stack_uptodate);
1094 RUN_TEST(test_reftable_stack_validate_refname);
1095 RUN_TEST(test_sizes_to_segments);
1096 RUN_TEST(test_sizes_to_segments_all_equal);
1097 RUN_TEST(test_sizes_to_segments_empty);
1098 RUN_TEST(test_suggest_compaction_segment);
1099 RUN_TEST(test_suggest_compaction_segment_nothing);
1100 return 0;
1101 }