Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/t/unit-tests/u-reftable-stack.c # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / t / unit-tests / u-reftable-stack.c` 1340 lines ยท 38848 bytes Raw Blame History 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 #define DISABLE_SIGN_COMPARE_WARNINGS 10 11 #include "unit-test.h" 12 #include "dir.h" 13 #include "lib-reftable.h" 14 #include "reftable/merged.h" 15 #include "reftable/reftable-error.h" 16 #include "reftable/stack.h" 17 #include "reftable/table.h" 18 #include "strbuf.h" 19 #include "tempfile.h" 20 #include 21 22 static void clear_dir(const char *dirname) 23 { 24 struct strbuf path = REFTABLE_BUF_INIT; 25 strbuf_addstr(&path, dirname); 26 remove_dir_recursively(&path, 0); 27 strbuf_release(&path); 28 } 29 30 static int count_dir_entries(const char *dirname) 31 { 32 DIR *dir = opendir(dirname); 33 int len = 0; 34 struct dirent *d; 35 if (!dir) 36 return 0; 37 38 while ((d = readdir(dir))) { 39 /* 40 * Besides skipping over "." and "..", we also need to 41 * skip over other files that have a leading ".". This 42 * is due to behaviour of NFS, which will rename files 43 * to ".nfs*" to emulate delete-on-last-close. 44 * 45 * In any case this should be fine as the reftable 46 * library will never write files with leading dots 47 * anyway. 48 */ 49 if (starts_with(d->d_name, ".")) 50 continue; 51 len++; 52 } 53 closedir(dir); 54 return len; 55 } 56 57 /* 58 * Work linenumber into the tempdir, so we can see which tests forget to 59 * cleanup. 60 */ 61 static char *get_tmp_template(int linenumber) 62 { 63 const char *tmp = getenv("TMPDIR"); 64 static char template[1024]; 65 snprintf(template, sizeof(template) - 1, "%s/stack_test-%d.XXXXXX", 66 tmp ? tmp : "/tmp", linenumber); 67 return template; 68 } 69 70 static char *get_tmp_dir(int linenumber) 71 { 72 char *dir = get_tmp_template(linenumber); 73 cl_assert(mkdtemp(dir) != NULL); 74 return dir; 75 } 76 77 void test_reftable_stack__read_file(void) 78 { 79 char *fn = get_tmp_template(__LINE__); 80 struct tempfile *tmp = mks_tempfile(fn); 81 int fd = get_tempfile_fd(tmp); 82 char out[1024] = "line1\n\nline2\nline3"; 83 int n, err; 84 char **names = NULL; 85 const char *want[] = { "line1", "line2", "line3" }; 86 87 cl_assert(fd > 0); 88 n = write_in_full(fd, out, strlen(out)); 89 cl_assert_equal_i(n, strlen(out)); 90 err = close(fd); 91 cl_assert(err >= 0); 92 93 err = read_lines(fn, &names); 94 cl_assert(!err); 95 96 for (size_t i = 0; names[i]; i++) 97 cl_assert_equal_s(want[i], names[i]); 98 free_names(names); 99 (void) remove(fn); 100 delete_tempfile(&tmp); 101 } 102 103 static int write_test_ref(struct reftable_writer *wr, void *arg) 104 { 105 struct reftable_ref_record *ref = arg; 106 cl_assert_equal_i(reftable_writer_set_limits(wr, 107 ref->update_index, ref->update_index), 0); 108 return reftable_writer_add_ref(wr, ref); 109 } 110 111 static void write_n_ref_tables(struct reftable_stack *st, 112 size_t n) 113 { 114 struct reftable_write_options opts = { 115 .disable_auto_compact = 1, 116 }; 117 118 for (size_t i = 0; i < n; i++) { 119 struct reftable_ref_record ref = { 120 .update_index = reftable_stack_next_update_index(st), 121 .value_type = REFTABLE_REF_VAL1, 122 }; 123 char buf[128]; 124 125 snprintf(buf, sizeof(buf), "refs/heads/branch-%04"PRIuMAX, (uintmax_t)i); 126 ref.refname = buf; 127 cl_reftable_set_hash(ref.value.val1, i, REFTABLE_HASH_SHA1); 128 129 cl_assert_equal_i(reftable_stack_add(st, 130 &write_test_ref, &ref, &opts), 0); 131 } 132 } 133 134 struct write_log_arg { 135 struct reftable_log_record *log; 136 uint64_t update_index; 137 }; 138 139 static int write_test_log(struct reftable_writer *wr, void *arg) 140 { 141 struct write_log_arg *wla = arg; 142 143 cl_assert_equal_i(reftable_writer_set_limits(wr, 144 wla->update_index, 145 wla->update_index), 0); 146 return reftable_writer_add_log(wr, wla->log); 147 } 148 149 void test_reftable_stack__add_one(void) 150 { 151 char *dir = get_tmp_dir(__LINE__); 152 struct reftable_buf scratch = REFTABLE_BUF_INIT; 153 int mask = umask(002); 154 struct reftable_write_options opts = { 155 .default_permissions = 0660, 156 }; 157 struct reftable_stack *st = NULL; 158 struct reftable_ref_record ref = { 159 .refname = (char *) "HEAD", 160 .update_index = 1, 161 .value_type = REFTABLE_REF_SYMREF, 162 .value.symref = (char *) "master", 163 }; 164 struct reftable_ref_record dest = { 0 }; 165 struct stat stat_result = { 0 }; 166 int err; 167 168 err = reftable_new_stack(&st, dir, NULL); 169 cl_assert(!err); 170 171 err = reftable_stack_add(st, write_test_ref, &ref, &opts); 172 cl_assert(!err); 173 174 err = reftable_stack_read_ref(st, ref.refname, &dest); 175 cl_assert(!err); 176 cl_assert(reftable_ref_record_equal(&ref, &dest, 177 REFTABLE_HASH_SIZE_SHA1)); 178 cl_assert(st->tables_len > 0); 179 180 #ifndef GIT_WINDOWS_NATIVE 181 cl_assert_equal_i(reftable_buf_addstr(&scratch, dir), 0); 182 cl_assert_equal_i(reftable_buf_addstr(&scratch, 183 "/tables.list"), 0); 184 cl_assert_equal_i(stat(scratch.buf, &stat_result), 0); 185 cl_assert_equal_i((stat_result.st_mode & 0777), 186 opts.default_permissions); 187 188 reftable_buf_reset(&scratch); 189 cl_assert_equal_i(reftable_buf_addstr(&scratch, dir), 0); 190 cl_assert_equal_i(reftable_buf_addstr(&scratch, "/"), 0); 191 /* do not try at home; not an external API for reftable. */ 192 cl_assert(!reftable_buf_addstr(&scratch, st->tables[0]->name)); 193 err = stat(scratch.buf, &stat_result); 194 cl_assert(!err); 195 cl_assert_equal_i((stat_result.st_mode & 0777), 196 opts.default_permissions); 197 #else 198 (void) stat_result; 199 #endif 200 201 reftable_ref_record_release(&dest); 202 reftable_stack_destroy(st); 203 reftable_buf_release(&scratch); 204 clear_dir(dir); 205 umask(mask); 206 } 207 208 void test_reftable_stack__uptodate(void) 209 { 210 struct reftable_stack *st1 = NULL; 211 struct reftable_stack *st2 = NULL; 212 char *dir = get_tmp_dir(__LINE__); 213 214 struct reftable_ref_record ref1 = { 215 .refname = (char *) "HEAD", 216 .update_index = 1, 217 .value_type = REFTABLE_REF_SYMREF, 218 .value.symref = (char *) "master", 219 }; 220 struct reftable_ref_record ref2 = { 221 .refname = (char *) "branch2", 222 .update_index = 2, 223 .value_type = REFTABLE_REF_SYMREF, 224 .value.symref = (char *) "master", 225 }; 226 227 228 /* simulate multi-process access to the same stack 229 by creating two stacks for the same directory. 230 */ 231 cl_assert_equal_i(reftable_new_stack(&st1, dir, NULL), 0); 232 cl_assert_equal_i(reftable_new_stack(&st2, dir, NULL), 0); 233 cl_assert_equal_i(reftable_stack_add(st1, write_test_ref, 234 &ref1, NULL), 0); 235 cl_assert_equal_i(reftable_stack_add(st2, write_test_ref, 236 &ref2, NULL), 0); 237 reftable_stack_destroy(st1); 238 reftable_stack_destroy(st2); 239 clear_dir(dir); 240 } 241 242 void test_reftable_stack__transaction_api(void) 243 { 244 char *dir = get_tmp_dir(__LINE__); 245 struct reftable_stack *st = NULL; 246 struct reftable_addition *add = NULL; 247 248 struct reftable_ref_record ref = { 249 .refname = (char *) "HEAD", 250 .update_index = 1, 251 .value_type = REFTABLE_REF_SYMREF, 252 .value.symref = (char *) "master", 253 }; 254 struct reftable_ref_record dest = { 0 }; 255 256 cl_assert_equal_i(reftable_new_stack(&st, dir, NULL), 0); 257 258 reftable_addition_destroy(add); 259 260 cl_assert_equal_i(reftable_stack_addition_new(&add, st, NULL), 0); 261 cl_assert_equal_i(reftable_addition_add(add, write_test_ref, 262 &ref), 0); 263 cl_assert_equal_i(reftable_addition_commit(add), 0); 264 265 reftable_addition_destroy(add); 266 267 cl_assert_equal_i(reftable_stack_read_ref(st, ref.refname, 268 &dest), 0); 269 cl_assert_equal_i(REFTABLE_REF_SYMREF, dest.value_type); 270 cl_assert(reftable_ref_record_equal(&ref, &dest, 271 REFTABLE_HASH_SIZE_SHA1) != 0); 272 273 reftable_ref_record_release(&dest); 274 reftable_stack_destroy(st); 275 clear_dir(dir); 276 } 277 278 void test_reftable_stack__transaction_with_reload(void) 279 { 280 char *dir = get_tmp_dir(__LINE__); 281 struct reftable_stack *st1 = NULL, *st2 = NULL; 282 struct reftable_addition *add = NULL; 283 struct reftable_ref_record refs[2] = { 284 { 285 .refname = (char *) "refs/heads/a", 286 .update_index = 1, 287 .value_type = REFTABLE_REF_VAL1, 288 .value.val1 = { '1' }, 289 }, 290 { 291 .refname = (char *) "refs/heads/b", 292 .update_index = 2, 293 .value_type = REFTABLE_REF_VAL1, 294 .value.val1 = { '1' }, 295 }, 296 }; 297 struct reftable_ref_record ref = { 0 }; 298 299 cl_assert_equal_i(reftable_new_stack(&st1, dir, NULL), 0); 300 cl_assert_equal_i(reftable_new_stack(&st2, dir, NULL), 0); 301 cl_assert_equal_i(reftable_stack_addition_new(&add, st1, NULL), 0); 302 cl_assert_equal_i(reftable_addition_add(add, write_test_ref, 303 &refs[0]), 0); 304 cl_assert_equal_i(reftable_addition_commit(add), 0); 305 reftable_addition_destroy(add); 306 307 /* 308 * The second stack is now outdated, but it should automatically reload it 309 * with the newer updates. 310 */ 311 cl_assert_equal_i(reftable_stack_addition_new(&add, st2, NULL), 0); 312 cl_assert_equal_i(reftable_addition_add(add, write_test_ref, 313 &refs[1]), 0); 314 cl_assert_equal_i(reftable_addition_commit(add), 0); 315 reftable_addition_destroy(add); 316 317 for (size_t i = 0; i < ARRAY_SIZE(refs); i++) { 318 cl_assert_equal_i(reftable_stack_read_ref(st2, 319 refs[i].refname, &ref) , 0); 320 cl_assert(reftable_ref_record_equal(&refs[i], &ref, 321 REFTABLE_HASH_SIZE_SHA1) != 0); 322 } 323 324 reftable_ref_record_release(&ref); 325 reftable_stack_destroy(st1); 326 reftable_stack_destroy(st2); 327 clear_dir(dir); 328 } 329 330 void test_reftable_stack__transaction_api_performs_auto_compaction(void) 331 { 332 char *dir = get_tmp_dir(__LINE__); 333 struct reftable_addition *add = NULL; 334 struct reftable_stack *st = NULL; 335 size_t n = 20; 336 337 cl_assert_equal_i(reftable_new_stack(&st, dir, NULL), 0); 338 339 for (size_t i = 0; i <= n; i++) { 340 struct reftable_ref_record ref = { 341 .update_index = reftable_stack_next_update_index(st), 342 .value_type = REFTABLE_REF_SYMREF, 343 .value.symref = (char *) "master", 344 }; 345 char name[100]; 346 struct reftable_write_options write_opts = { 347 .disable_auto_compact = (i != n), 348 }; 349 350 snprintf(name, sizeof(name), "branch%04"PRIuMAX, (uintmax_t)i); 351 ref.refname = name; 352 353 /* 354 * Disable auto-compaction for all but the last runs. Like this 355 * we can ensure that we indeed honor this setting and have 356 * better control over when exactly auto compaction runs. 357 */ 358 cl_assert_equal_i(reftable_stack_addition_new(&add, 359 st, &write_opts), 0); 360 cl_assert_equal_i(reftable_addition_add(add, 361 write_test_ref, &ref), 0); 362 cl_assert_equal_i(reftable_addition_commit(add), 0); 363 364 reftable_addition_destroy(add); 365 366 /* 367 * The stack length should grow continuously for all runs where 368 * auto compaction is disabled. When enabled, we should merge 369 * all tables in the stack. 370 */ 371 if (i != n) 372 cl_assert_equal_i(st->merged->tables_len, i + 1); 373 else 374 cl_assert_equal_i(st->merged->tables_len, 1); 375 } 376 377 reftable_stack_destroy(st); 378 clear_dir(dir); 379 } 380 381 void test_reftable_stack__auto_compaction_fails_gracefully(void) 382 { 383 struct reftable_ref_record ref = { 384 .refname = (char *) "refs/heads/master", 385 .update_index = 1, 386 .value_type = REFTABLE_REF_VAL1, 387 .value.val1 = {0x01}, 388 }; 389 struct reftable_stack *st; 390 struct reftable_buf table_path = REFTABLE_BUF_INIT; 391 char *dir = get_tmp_dir(__LINE__); 392 int err; 393 394 cl_assert_equal_i(reftable_new_stack(&st, dir, NULL), 0); 395 cl_assert_equal_i(reftable_stack_add(st, write_test_ref, 396 &ref, NULL), 0); 397 cl_assert_equal_i(st->merged->tables_len, 1); 398 cl_assert_equal_i(st->stats.attempts, 0); 399 cl_assert_equal_i(st->stats.failures, 0); 400 401 /* 402 * Lock the newly written table such that it cannot be compacted. 403 * Adding a new table to the stack should not be impacted by this, even 404 * though auto-compaction will now fail. 405 */ 406 cl_assert(!reftable_buf_addstr(&table_path, dir)); 407 cl_assert(!reftable_buf_addstr(&table_path, "/")); 408 cl_assert(!reftable_buf_addstr(&table_path, 409 st->tables[0]->name)); 410 cl_assert(!reftable_buf_addstr(&table_path, ".lock")); 411 write_file_buf(table_path.buf, "", 0); 412 413 ref.update_index = 2; 414 err = reftable_stack_add(st, write_test_ref, &ref, NULL); 415 cl_assert(!err); 416 cl_assert_equal_i(st->merged->tables_len, 2); 417 cl_assert_equal_i(st->stats.attempts, 1); 418 cl_assert_equal_i(st->stats.failures, 1); 419 420 reftable_stack_destroy(st); 421 reftable_buf_release(&table_path); 422 clear_dir(dir); 423 } 424 425 static int write_error(struct reftable_writer *wr UNUSED, void *arg) 426 { 427 return *((int *)arg); 428 } 429 430 void test_reftable_stack__update_index_check(void) 431 { 432 char *dir = get_tmp_dir(__LINE__); 433 struct reftable_stack *st = NULL; 434 struct reftable_ref_record ref1 = { 435 .refname = (char *) "name1", 436 .update_index = 1, 437 .value_type = REFTABLE_REF_SYMREF, 438 .value.symref = (char *) "master", 439 }; 440 struct reftable_ref_record ref2 = { 441 .refname = (char *) "name2", 442 .update_index = 1, 443 .value_type = REFTABLE_REF_SYMREF, 444 .value.symref = (char *) "master", 445 }; 446 447 cl_assert_equal_i(reftable_new_stack(&st, dir, NULL), 0); 448 cl_assert_equal_i(reftable_stack_add(st, write_test_ref, 449 &ref1, NULL), 0); 450 cl_assert_equal_i(reftable_stack_add(st, write_test_ref, 451 &ref2, NULL), REFTABLE_API_ERROR); 452 reftable_stack_destroy(st); 453 clear_dir(dir); 454 } 455 456 void test_reftable_stack__lock_failure(void) 457 { 458 char *dir = get_tmp_dir(__LINE__); 459 struct reftable_stack *st = NULL; 460 int i; 461 462 cl_assert_equal_i(reftable_new_stack(&st, dir, NULL), 0); 463 for (i = -1; i != REFTABLE_EMPTY_TABLE_ERROR; i--) 464 cl_assert_equal_i(reftable_stack_add(st, write_error, 465 &i, NULL), i); 466 467 reftable_stack_destroy(st); 468 clear_dir(dir); 469 } 470 471 void test_reftable_stack__add(void) 472 { 473 struct reftable_write_options opts = { 474 .exact_log_message = 1, 475 .default_permissions = 0660, 476 .disable_auto_compact = 1, 477 }; 478 struct reftable_stack *st = NULL; 479 char *dir = get_tmp_dir(__LINE__); 480 struct reftable_ref_record refs[2] = { 0 }; 481 struct reftable_log_record logs[2] = { 0 }; 482 struct reftable_buf path = REFTABLE_BUF_INIT; 483 struct stat stat_result; 484 size_t i, N = ARRAY_SIZE(refs); 485 int err = 0; 486 487 err = reftable_new_stack(&st, dir, NULL); 488 cl_assert(!err); 489 490 for (i = 0; i < N; i++) { 491 char buf[256]; 492 snprintf(buf, sizeof(buf), "branch%02"PRIuMAX, (uintmax_t)i); 493 refs[i].refname = xstrdup(buf); 494 refs[i].update_index = i + 1; 495 refs[i].value_type = REFTABLE_REF_VAL1; 496 cl_reftable_set_hash(refs[i].value.val1, i, 497 REFTABLE_HASH_SHA1); 498 499 logs[i].refname = xstrdup(buf); 500 logs[i].update_index = N + i + 1; 501 logs[i].value_type = REFTABLE_LOG_UPDATE; 502 logs[i].value.update.email = xstrdup("identity@invalid"); 503 cl_reftable_set_hash(logs[i].value.update.new_hash, i, 504 REFTABLE_HASH_SHA1); 505 } 506 507 for (i = 0; i < N; i++) 508 cl_assert_equal_i(reftable_stack_add(st, write_test_ref, 509 &refs[i], &opts), 0); 510 511 for (i = 0; i < N; i++) { 512 struct write_log_arg arg = { 513 .log = &logs[i], 514 .update_index = reftable_stack_next_update_index(st), 515 }; 516 cl_assert_equal_i(reftable_stack_add(st, write_test_log, 517 &arg, &opts), 0); 518 } 519 520 cl_assert_equal_i(reftable_stack_compact_all(st, &opts, NULL), 0); 521 522 for (i = 0; i < N; i++) { 523 struct reftable_ref_record dest = { 0 }; 524 525 cl_assert_equal_i(reftable_stack_read_ref(st, 526 refs[i].refname, &dest), 0); 527 cl_assert(reftable_ref_record_equal(&dest, refs + i, 528 REFTABLE_HASH_SIZE_SHA1) != 0); 529 reftable_ref_record_release(&dest); 530 } 531 532 for (i = 0; i < N; i++) { 533 struct reftable_log_record dest = { 0 }; 534 cl_assert_equal_i(reftable_stack_read_log(st, 535 refs[i].refname, &dest), 0); 536 cl_assert(reftable_log_record_equal(&dest, logs + i, 537 REFTABLE_HASH_SIZE_SHA1) != 0); 538 reftable_log_record_release(&dest); 539 } 540 541 #ifndef GIT_WINDOWS_NATIVE 542 cl_assert_equal_i(reftable_buf_addstr(&path, dir), 0); 543 cl_assert_equal_i(reftable_buf_addstr(&path, "/tables.list"), 0); 544 cl_assert_equal_i(stat(path.buf, &stat_result), 0); 545 cl_assert_equal_i((stat_result.st_mode & 0777), opts.default_permissions); 546 547 reftable_buf_reset(&path); 548 cl_assert_equal_i(reftable_buf_addstr(&path, dir), 0); 549 cl_assert_equal_i(reftable_buf_addstr(&path, "/"), 0); 550 /* do not try at home; not an external API for reftable. */ 551 cl_assert(!reftable_buf_addstr(&path, st->tables[0]->name)); 552 err = stat(path.buf, &stat_result); 553 cl_assert(!err); 554 cl_assert_equal_i((stat_result.st_mode & 0777), 555 opts.default_permissions); 556 #else 557 (void) stat_result; 558 #endif 559 560 /* cleanup */ 561 reftable_stack_destroy(st); 562 for (i = 0; i < N; i++) { 563 reftable_ref_record_release(&refs[i]); 564 reftable_log_record_release(&logs[i]); 565 } 566 reftable_buf_release(&path); 567 clear_dir(dir); 568 } 569 570 void test_reftable_stack__iterator(void) 571 { 572 struct reftable_stack *st = NULL; 573 char *dir = get_tmp_dir(__LINE__); 574 struct reftable_ref_record refs[10] = { 0 }; 575 struct reftable_log_record logs[10] = { 0 }; 576 struct reftable_iterator it = { 0 }; 577 size_t N = ARRAY_SIZE(refs), i; 578 int err; 579 580 cl_assert_equal_i(reftable_new_stack(&st, dir, NULL), 0); 581 582 for (i = 0; i < N; i++) { 583 refs[i].refname = xstrfmt("branch%02"PRIuMAX, (uintmax_t)i); 584 refs[i].update_index = i + 1; 585 refs[i].value_type = REFTABLE_REF_VAL1; 586 cl_reftable_set_hash(refs[i].value.val1, i, 587 REFTABLE_HASH_SHA1); 588 589 logs[i].refname = xstrfmt("branch%02"PRIuMAX, (uintmax_t)i); 590 logs[i].update_index = i + 1; 591 logs[i].value_type = REFTABLE_LOG_UPDATE; 592 logs[i].value.update.email = xstrdup("johndoe@invalid"); 593 logs[i].value.update.message = xstrdup("commit\n"); 594 cl_reftable_set_hash(logs[i].value.update.new_hash, i, 595 REFTABLE_HASH_SHA1); 596 } 597 598 for (i = 0; i < N; i++) 599 cl_assert_equal_i(reftable_stack_add(st, write_test_ref, 600 &refs[i], NULL), 0); 601 602 for (i = 0; i < N; i++) { 603 struct write_log_arg arg = { 604 .log = &logs[i], 605 .update_index = reftable_stack_next_update_index(st), 606 }; 607 608 cl_assert_equal_i(reftable_stack_add(st, write_test_log, 609 &arg, NULL), 0); 610 } 611 612 reftable_stack_init_ref_iterator(st, &it); 613 reftable_iterator_seek_ref(&it, refs[0].refname); 614 for (i = 0; ; i++) { 615 struct reftable_ref_record ref = { 0 }; 616 617 err = reftable_iterator_next_ref(&it, &ref); 618 if (err > 0) 619 break; 620 cl_assert(!err); 621 cl_assert(reftable_ref_record_equal(&ref, &refs[i], 622 REFTABLE_HASH_SIZE_SHA1) != 0); 623 reftable_ref_record_release(&ref); 624 } 625 cl_assert_equal_i(i, N); 626 627 reftable_iterator_destroy(&it); 628 629 cl_assert_equal_i(reftable_stack_init_log_iterator(st, &it), 0); 630 631 reftable_iterator_seek_log(&it, logs[0].refname); 632 for (i = 0; ; i++) { 633 struct reftable_log_record log = { 0 }; 634 635 err = reftable_iterator_next_log(&it, &log); 636 if (err > 0) 637 break; 638 cl_assert(!err); 639 cl_assert(reftable_log_record_equal(&log, &logs[i], 640 REFTABLE_HASH_SIZE_SHA1) != 0); 641 reftable_log_record_release(&log); 642 } 643 cl_assert_equal_i(i, N); 644 645 reftable_stack_destroy(st); 646 reftable_iterator_destroy(&it); 647 for (i = 0; i < N; i++) { 648 reftable_ref_record_release(&refs[i]); 649 reftable_log_record_release(&logs[i]); 650 } 651 clear_dir(dir); 652 } 653 654 void test_reftable_stack__log_normalize(void) 655 { 656 struct reftable_stack *st = NULL; 657 char *dir = get_tmp_dir(__LINE__); 658 struct reftable_log_record input = { 659 .refname = (char *) "branch", 660 .update_index = 1, 661 .value_type = REFTABLE_LOG_UPDATE, 662 .value = { 663 .update = { 664 .new_hash = { 1 }, 665 .old_hash = { 2 }, 666 }, 667 }, 668 }; 669 struct reftable_log_record dest = { 670 .update_index = 0, 671 }; 672 struct write_log_arg arg = { 673 .log = &input, 674 .update_index = 1, 675 }; 676 677 cl_assert_equal_i(reftable_new_stack(&st, dir, NULL), 0); 678 679 input.value.update.message = (char *) "one\ntwo"; 680 cl_assert_equal_i(reftable_stack_add(st, write_test_log, 681 &arg, NULL), REFTABLE_API_ERROR); 682 683 input.value.update.message = (char *) "one"; 684 cl_assert_equal_i(reftable_stack_add(st, write_test_log, 685 &arg, NULL), 0); 686 cl_assert_equal_i(reftable_stack_read_log(st, input.refname, 687 &dest), 0); 688 cl_assert_equal_s(dest.value.update.message, "one\n"); 689 690 input.value.update.message = (char *) "two\n"; 691 arg.update_index = 2; 692 cl_assert_equal_i(reftable_stack_add(st, write_test_log, 693 &arg, NULL), 0); 694 cl_assert_equal_i(reftable_stack_read_log(st, input.refname, 695 &dest), 0); 696 cl_assert_equal_s(dest.value.update.message, "two\n"); 697 698 /* cleanup */ 699 reftable_stack_destroy(st); 700 reftable_log_record_release(&dest); 701 clear_dir(dir); 702 } 703 704 void test_reftable_stack__tombstone(void) 705 { 706 char *dir = get_tmp_dir(__LINE__); 707 struct reftable_stack *st = NULL; 708 struct reftable_ref_record refs[2] = { 0 }; 709 struct reftable_log_record logs[2] = { 0 }; 710 size_t i, N = ARRAY_SIZE(refs); 711 struct reftable_ref_record dest = { 0 }; 712 struct reftable_log_record log_dest = { 0 }; 713 714 cl_assert_equal_i(reftable_new_stack(&st, dir, NULL), 0); 715 716 /* even entries add the refs, odd entries delete them. */ 717 for (i = 0; i < N; i++) { 718 const char *buf = "branch"; 719 refs[i].refname = xstrdup(buf); 720 refs[i].update_index = i + 1; 721 if (i % 2 == 0) { 722 refs[i].value_type = REFTABLE_REF_VAL1; 723 cl_reftable_set_hash(refs[i].value.val1, i, 724 REFTABLE_HASH_SHA1); 725 } 726 727 logs[i].refname = xstrdup(buf); 728 /* 729 * update_index is part of the key so should be constant. 730 * The value itself should be less than the writer's upper 731 * limit. 732 */ 733 logs[i].update_index = 1; 734 if (i % 2 == 0) { 735 logs[i].value_type = REFTABLE_LOG_UPDATE; 736 cl_reftable_set_hash(logs[i].value.update.new_hash, i, REFTABLE_HASH_SHA1); 737 logs[i].value.update.email = 738 xstrdup("identity@invalid"); 739 } 740 } 741 for (i = 0; i < N; i++) 742 cl_assert_equal_i(reftable_stack_add(st, write_test_ref, 743 &refs[i], NULL), 0); 744 745 for (i = 0; i < N; i++) { 746 struct write_log_arg arg = { 747 .log = &logs[i], 748 .update_index = reftable_stack_next_update_index(st), 749 }; 750 cl_assert_equal_i(reftable_stack_add(st, write_test_log, 751 &arg, NULL), 0); 752 } 753 754 cl_assert_equal_i(reftable_stack_read_ref(st, "branch", 755 &dest), 1); 756 reftable_ref_record_release(&dest); 757 758 cl_assert_equal_i(reftable_stack_read_log(st, "branch", 759 &log_dest), 1); 760 reftable_log_record_release(&log_dest); 761 762 cl_assert_equal_i(reftable_stack_compact_all(st, NULL, NULL), 0); 763 cl_assert_equal_i(reftable_stack_read_ref(st, "branch", 764 &dest), 1); 765 cl_assert_equal_i(reftable_stack_read_log(st, "branch", 766 &log_dest), 1); 767 reftable_ref_record_release(&dest); 768 reftable_log_record_release(&log_dest); 769 770 /* cleanup */ 771 reftable_stack_destroy(st); 772 for (i = 0; i < N; i++) { 773 reftable_ref_record_release(&refs[i]); 774 reftable_log_record_release(&logs[i]); 775 } 776 clear_dir(dir); 777 } 778 779 void test_reftable_stack__hash_id(void) 780 { 781 char *dir = get_tmp_dir(__LINE__); 782 struct reftable_stack *st = NULL; 783 784 struct reftable_ref_record ref = { 785 .refname = (char *) "master", 786 .value_type = REFTABLE_REF_SYMREF, 787 .value.symref = (char *) "target", 788 .update_index = 1, 789 }; 790 struct reftable_stack_options opts32 = { .hash_id = REFTABLE_HASH_SHA256 }; 791 struct reftable_stack *st32 = NULL; 792 struct reftable_stack *st_default = NULL; 793 struct reftable_ref_record dest = { 0 }; 794 795 cl_assert_equal_i(reftable_new_stack(&st, dir, NULL), 0); 796 cl_assert_equal_i(reftable_stack_add(st, write_test_ref, 797 &ref, NULL), 0); 798 799 /* can't read it with the wrong hash ID. */ 800 cl_assert_equal_i(reftable_new_stack(&st32, dir, 801 &opts32), REFTABLE_FORMAT_ERROR); 802 803 /* check that we can read it back with default opts too. */ 804 cl_assert_equal_i(reftable_new_stack(&st_default, dir, 805 NULL), 0); 806 cl_assert_equal_i(reftable_stack_read_ref(st_default, "master", 807 &dest), 0); 808 cl_assert(reftable_ref_record_equal(&ref, &dest, 809 REFTABLE_HASH_SIZE_SHA1) != 0); 810 reftable_ref_record_release(&dest); 811 reftable_stack_destroy(st); 812 reftable_stack_destroy(st_default); 813 clear_dir(dir); 814 } 815 816 void test_reftable_stack__suggest_compaction_segment(void) 817 { 818 uint64_t sizes[] = { 512, 64, 17, 16, 9, 9, 9, 16, 2, 16 }; 819 struct segment min = 820 suggest_compaction_segment(sizes, ARRAY_SIZE(sizes), 2); 821 cl_assert_equal_i(min.start, 1); 822 cl_assert_equal_i(min.end, 10); 823 } 824 825 void test_reftable_stack__suggest_compaction_segment_nothing(void) 826 { 827 uint64_t sizes[] = { 64, 32, 16, 8, 4, 2 }; 828 struct segment result = 829 suggest_compaction_segment(sizes, ARRAY_SIZE(sizes), 2); 830 cl_assert_equal_i(result.start, result.end); 831 } 832 833 void test_reftable_stack__reflog_expire(void) 834 { 835 char *dir = get_tmp_dir(__LINE__); 836 struct reftable_stack *st = NULL; 837 struct reftable_log_record logs[20] = { 0 }; 838 size_t i, N = ARRAY_SIZE(logs) - 1; 839 struct reftable_log_expiry_config expiry = { 840 .time = 10, 841 }; 842 struct reftable_log_record log = { 0 }; 843 844 cl_assert_equal_i(reftable_new_stack(&st, dir, NULL), 0); 845 846 for (i = 1; i <= N; i++) { 847 char buf[256]; 848 snprintf(buf, sizeof(buf), "branch%02"PRIuMAX, (uintmax_t)i); 849 850 logs[i].refname = xstrdup(buf); 851 logs[i].update_index = i; 852 logs[i].value_type = REFTABLE_LOG_UPDATE; 853 logs[i].value.update.time = i; 854 logs[i].value.update.email = xstrdup("identity@invalid"); 855 cl_reftable_set_hash(logs[i].value.update.new_hash, i, 856 REFTABLE_HASH_SHA1); 857 } 858 859 for (i = 1; i <= N; i++) { 860 struct write_log_arg arg = { 861 .log = &logs[i], 862 .update_index = reftable_stack_next_update_index(st), 863 }; 864 cl_assert_equal_i(reftable_stack_add(st, write_test_log, 865 &arg, NULL), 0); 866 } 867 868 cl_assert_equal_i(reftable_stack_compact_all(st, NULL, NULL), 0); 869 cl_assert_equal_i(reftable_stack_compact_all(st, NULL, &expiry), 0); 870 cl_assert_equal_i(reftable_stack_read_log(st, logs[9].refname, 871 &log), 1); 872 cl_assert_equal_i(reftable_stack_read_log(st, logs[11].refname, 873 &log), 0); 874 875 expiry.min_update_index = 15; 876 cl_assert_equal_i(reftable_stack_compact_all(st, NULL, &expiry), 0); 877 cl_assert_equal_i(reftable_stack_read_log(st, logs[14].refname, 878 &log), 1); 879 cl_assert_equal_i(reftable_stack_read_log(st, logs[16].refname, 880 &log), 0); 881 882 /* cleanup */ 883 reftable_stack_destroy(st); 884 for (i = 0; i <= N; i++) 885 reftable_log_record_release(&logs[i]); 886 clear_dir(dir); 887 reftable_log_record_release(&log); 888 } 889 890 static int write_nothing(struct reftable_writer *wr, void *arg UNUSED) 891 { 892 cl_assert_equal_i(reftable_writer_set_limits(wr, 1, 1), 0); 893 return 0; 894 } 895 896 void test_reftable_stack__empty_add(void) 897 { 898 struct reftable_stack *st = NULL; 899 char *dir = get_tmp_dir(__LINE__); 900 struct reftable_stack *st2 = NULL; 901 902 cl_assert_equal_i(reftable_new_stack(&st, dir, NULL), 0); 903 cl_assert_equal_i(reftable_stack_add(st, write_nothing, 904 NULL, NULL), 0); 905 cl_assert_equal_i(reftable_new_stack(&st2, dir, NULL), 0); 906 clear_dir(dir); 907 reftable_stack_destroy(st); 908 reftable_stack_destroy(st2); 909 } 910 911 static int fastlogN(uint64_t sz, uint64_t N) 912 { 913 int l = 0; 914 if (sz == 0) 915 return 0; 916 for (; sz; sz /= N) 917 l++; 918 return l - 1; 919 } 920 921 void test_reftable_stack__auto_compaction(void) 922 { 923 struct reftable_write_options opts = { 924 .disable_auto_compact = 1, 925 }; 926 struct reftable_stack *st = NULL; 927 char *dir = get_tmp_dir(__LINE__); 928 size_t i, N = 100; 929 int err; 930 931 cl_assert_equal_i(reftable_new_stack(&st, dir, NULL), 0); 932 933 for (i = 0; i < N; i++) { 934 char name[100]; 935 struct reftable_ref_record ref = { 936 .refname = name, 937 .update_index = reftable_stack_next_update_index(st), 938 .value_type = REFTABLE_REF_SYMREF, 939 .value.symref = (char *) "master", 940 }; 941 snprintf(name, sizeof(name), "branch%04"PRIuMAX, (uintmax_t)i); 942 943 err = reftable_stack_add(st, write_test_ref, &ref, &opts); 944 cl_assert(!err); 945 946 err = reftable_stack_auto_compact(st, &opts); 947 cl_assert(!err); 948 cl_assert(i < 2 || st->merged->tables_len < 2 * fastlogN(i, 2)); 949 } 950 951 cl_assert(reftable_stack_compaction_stats(st)->entries_written < 952 (uint64_t)(N * fastlogN(N, 2))); 953 954 reftable_stack_destroy(st); 955 clear_dir(dir); 956 } 957 958 void test_reftable_stack__auto_compaction_factor(void) 959 { 960 struct reftable_write_options opts = { 961 .auto_compaction_factor = 5, 962 }; 963 struct reftable_stack *st = NULL; 964 char *dir = get_tmp_dir(__LINE__); 965 size_t N = 100; 966 int err; 967 968 cl_assert_equal_i(reftable_new_stack(&st, dir, NULL), 0); 969 970 for (size_t i = 0; i < N; i++) { 971 char name[20]; 972 struct reftable_ref_record ref = { 973 .refname = name, 974 .update_index = reftable_stack_next_update_index(st), 975 .value_type = REFTABLE_REF_VAL1, 976 }; 977 xsnprintf(name, sizeof(name), "branch%04"PRIuMAX, (uintmax_t)i); 978 979 err = reftable_stack_add(st, &write_test_ref, &ref, &opts); 980 cl_assert(!err); 981 982 cl_assert(i < 5 || st->merged->tables_len < 5 * fastlogN(i, 5)); 983 } 984 985 reftable_stack_destroy(st); 986 clear_dir(dir); 987 } 988 989 void test_reftable_stack__auto_compaction_with_locked_tables(void) 990 { 991 struct reftable_write_options opts = { 992 .disable_auto_compact = 1, 993 }; 994 struct reftable_stack *st = NULL; 995 struct reftable_buf buf = REFTABLE_BUF_INIT; 996 char *dir = get_tmp_dir(__LINE__); 997 int err; 998 999 cl_assert_equal_i(reftable_new_stack(&st, dir, NULL), 0); 1000 1001 write_n_ref_tables(st, 5); 1002 cl_assert_equal_i(st->merged->tables_len, 5); 1003 1004 /* 1005 * Given that all tables we have written should be roughly the same 1006 * size, we expect that auto-compaction will want to compact all of the 1007 * tables. Locking any of the tables will keep it from doing so. 1008 */ 1009 cl_assert(!reftable_buf_addstr(&buf, dir)); 1010 cl_assert(!reftable_buf_addstr(&buf, "/")); 1011 cl_assert(!reftable_buf_addstr(&buf, st->tables[2]->name)); 1012 cl_assert(!reftable_buf_addstr(&buf, ".lock")); 1013 write_file_buf(buf.buf, "", 0); 1014 1015 /* 1016 * When parts of the stack are locked, then auto-compaction does a best 1017 * effort compaction of those tables which aren't locked. So while this 1018 * would in theory compact all tables, due to the preexisting lock we 1019 * only compact the newest two tables. 1020 */ 1021 err = reftable_stack_auto_compact(st, &opts); 1022 cl_assert(!err); 1023 cl_assert_equal_i(st->stats.failures, 0); 1024 cl_assert_equal_i(st->merged->tables_len, 4); 1025 1026 reftable_stack_destroy(st); 1027 reftable_buf_release(&buf); 1028 clear_dir(dir); 1029 } 1030 1031 void test_reftable_stack__add_performs_auto_compaction(void) 1032 { 1033 struct reftable_stack *st = NULL; 1034 char *dir = get_tmp_dir(__LINE__); 1035 size_t i, n = 20; 1036 1037 cl_assert_equal_i(reftable_new_stack(&st, dir, NULL), 0); 1038 1039 for (i = 0; i <= n; i++) { 1040 struct reftable_ref_record ref = { 1041 .update_index = reftable_stack_next_update_index(st), 1042 .value_type = REFTABLE_REF_SYMREF, 1043 .value.symref = (char *) "master", 1044 }; 1045 struct reftable_write_options write_opts = { 1046 .disable_auto_compact = (i != n), 1047 }; 1048 bool required = false; 1049 char buf[128]; 1050 1051 /* 1052 * Disable auto-compaction for all but the last runs. Like this 1053 * we can ensure that we indeed honor this setting and have 1054 * better control over when exactly auto compaction runs. 1055 */ 1056 snprintf(buf, sizeof(buf), "branch-%04"PRIuMAX, (uintmax_t)i); 1057 ref.refname = buf; 1058 1059 cl_assert_equal_i(reftable_stack_add(st, write_test_ref, 1060 &ref, &write_opts), 0); 1061 1062 /* 1063 * The stack length should grow continuously for all runs where 1064 * auto compaction is disabled. When enabled, we should merge 1065 * all tables in the stack. 1066 */ 1067 cl_assert_equal_i(reftable_stack_compaction_required(st, NULL, true, &required), 0); 1068 if (i != n) { 1069 cl_assert_equal_i(st->merged->tables_len, i + 1); 1070 if (i < 1) 1071 cl_assert_equal_b(required, false); 1072 else 1073 cl_assert_equal_b(required, true); 1074 } else { 1075 cl_assert_equal_i(st->merged->tables_len, 1); 1076 cl_assert_equal_b(required, false); 1077 } 1078 } 1079 1080 reftable_stack_destroy(st); 1081 clear_dir(dir); 1082 } 1083 1084 void test_reftable_stack__compaction_with_locked_tables(void) 1085 { 1086 struct reftable_write_options opts = { 1087 .disable_auto_compact = 1, 1088 }; 1089 struct reftable_stack *st = NULL; 1090 struct reftable_buf buf = REFTABLE_BUF_INIT; 1091 char *dir = get_tmp_dir(__LINE__); 1092 int err; 1093 1094 cl_assert_equal_i(reftable_new_stack(&st, dir, NULL), 0); 1095 1096 write_n_ref_tables(st, 3); 1097 cl_assert_equal_i(st->merged->tables_len, 3); 1098 1099 /* Lock one of the tables that we're about to compact. */ 1100 cl_assert(!reftable_buf_addstr(&buf, dir)); 1101 cl_assert(!reftable_buf_addstr(&buf, "/")); 1102 cl_assert(!reftable_buf_addstr(&buf, st->tables[1]->name)); 1103 cl_assert(!reftable_buf_addstr(&buf, ".lock")); 1104 write_file_buf(buf.buf, "", 0); 1105 1106 /* 1107 * Compaction is expected to fail given that we were not able to 1108 * compact all tables. 1109 */ 1110 err = reftable_stack_compact_all(st, &opts, NULL); 1111 cl_assert_equal_i(err, REFTABLE_LOCK_ERROR); 1112 cl_assert_equal_i(st->stats.failures, 1); 1113 cl_assert_equal_i(st->merged->tables_len, 3); 1114 1115 reftable_stack_destroy(st); 1116 reftable_buf_release(&buf); 1117 clear_dir(dir); 1118 } 1119 1120 void test_reftable_stack__compaction_concurrent(void) 1121 { 1122 struct reftable_stack *st1 = NULL, *st2 = NULL; 1123 char *dir = get_tmp_dir(__LINE__); 1124 1125 cl_assert_equal_i(reftable_new_stack(&st1, dir, NULL), 0); 1126 write_n_ref_tables(st1, 3); 1127 1128 cl_assert_equal_i(reftable_new_stack(&st2, dir, NULL), 0); 1129 cl_assert_equal_i(reftable_stack_compact_all(st1, NULL, NULL), 0); 1130 1131 reftable_stack_destroy(st1); 1132 reftable_stack_destroy(st2); 1133 1134 cl_assert_equal_i(count_dir_entries(dir), 2); 1135 clear_dir(dir); 1136 } 1137 1138 static void unclean_stack_close(struct reftable_stack *st) 1139 { 1140 /* break abstraction boundary to simulate unclean shutdown. */ 1141 for (size_t i = 0; i < st->tables_len; i++) 1142 reftable_table_decref(st->tables[i]); 1143 st->tables_len = 0; 1144 REFTABLE_FREE_AND_NULL(st->tables); 1145 } 1146 1147 void test_reftable_stack__compaction_concurrent_clean(void) 1148 { 1149 struct reftable_stack *st1 = NULL, *st2 = NULL, *st3 = NULL; 1150 char *dir = get_tmp_dir(__LINE__); 1151 1152 cl_assert_equal_i(reftable_new_stack(&st1, dir, NULL), 0); 1153 write_n_ref_tables(st1, 3); 1154 1155 cl_assert_equal_i(reftable_new_stack(&st2, dir, NULL), 0); 1156 cl_assert_equal_i(reftable_stack_compact_all(st1, NULL, NULL), 0); 1157 1158 unclean_stack_close(st1); 1159 unclean_stack_close(st2); 1160 1161 cl_assert_equal_i(reftable_new_stack(&st3, dir, NULL), 0); 1162 cl_assert_equal_i(reftable_stack_clean(st3), 0); 1163 cl_assert_equal_i(count_dir_entries(dir), 2); 1164 1165 reftable_stack_destroy(st1); 1166 reftable_stack_destroy(st2); 1167 reftable_stack_destroy(st3); 1168 1169 clear_dir(dir); 1170 } 1171 1172 void test_reftable_stack__read_across_reload(void) 1173 { 1174 struct reftable_stack *st1 = NULL, *st2 = NULL; 1175 struct reftable_ref_record rec = { 0 }; 1176 struct reftable_iterator it = { 0 }; 1177 char *dir = get_tmp_dir(__LINE__); 1178 int err; 1179 1180 /* Create a first stack and set up an iterator for it. */ 1181 cl_assert_equal_i(reftable_new_stack(&st1, dir, NULL), 0); 1182 write_n_ref_tables(st1, 2); 1183 cl_assert_equal_i(st1->merged->tables_len, 2); 1184 reftable_stack_init_ref_iterator(st1, &it); 1185 cl_assert_equal_i(reftable_iterator_seek_ref(&it, ""), 0); 1186 1187 /* Set up a second stack for the same directory and compact it. */ 1188 err = reftable_new_stack(&st2, dir, NULL); 1189 cl_assert(!err); 1190 cl_assert_equal_i(st2->merged->tables_len, 2); 1191 err = reftable_stack_compact_all(st2, NULL, NULL); 1192 cl_assert(!err); 1193 cl_assert_equal_i(st2->merged->tables_len, 1); 1194 1195 /* 1196 * Verify that we can continue to use the old iterator even after we 1197 * have reloaded its stack. 1198 */ 1199 err = reftable_stack_reload(st1); 1200 cl_assert(!err); 1201 cl_assert_equal_i(st1->merged->tables_len, 1); 1202 err = reftable_iterator_next_ref(&it, &rec); 1203 cl_assert(!err); 1204 cl_assert_equal_s(rec.refname, "refs/heads/branch-0000"); 1205 err = reftable_iterator_next_ref(&it, &rec); 1206 cl_assert(!err); 1207 cl_assert_equal_s(rec.refname, "refs/heads/branch-0001"); 1208 err = reftable_iterator_next_ref(&it, &rec); 1209 cl_assert(err > 0); 1210 1211 reftable_ref_record_release(&rec); 1212 reftable_iterator_destroy(&it); 1213 reftable_stack_destroy(st1); 1214 reftable_stack_destroy(st2); 1215 clear_dir(dir); 1216 } 1217 1218 void test_reftable_stack__reload_with_missing_table(void) 1219 { 1220 struct reftable_stack *st = NULL; 1221 struct reftable_ref_record rec = { 0 }; 1222 struct reftable_iterator it = { 0 }; 1223 struct reftable_buf table_path = REFTABLE_BUF_INIT, content = REFTABLE_BUF_INIT; 1224 char *dir = get_tmp_dir(__LINE__); 1225 int err; 1226 1227 /* Create a first stack and set up an iterator for it. */ 1228 cl_assert_equal_i(reftable_new_stack(&st, dir, NULL), 0); 1229 write_n_ref_tables(st, 2); 1230 cl_assert_equal_i(st->merged->tables_len, 2); 1231 reftable_stack_init_ref_iterator(st, &it); 1232 cl_assert_equal_i(reftable_iterator_seek_ref(&it, ""), 0); 1233 1234 /* 1235 * Update the tables.list file with some garbage data, while reusing 1236 * our old tables. This should trigger a partial reload of the stack, 1237 * where we try to reuse our old tables. 1238 */ 1239 cl_assert(!reftable_buf_addstr(&content, st->tables[0]->name)); 1240 cl_assert(!reftable_buf_addstr(&content, "\n")); 1241 cl_assert(!reftable_buf_addstr(&content, st->tables[1]->name)); 1242 cl_assert(!reftable_buf_addstr(&content, "\n")); 1243 cl_assert(!reftable_buf_addstr(&content, "garbage\n")); 1244 cl_assert(!reftable_buf_addstr(&table_path, st->list_file)); 1245 cl_assert(!reftable_buf_addstr(&table_path, ".lock")); 1246 write_file_buf(table_path.buf, content.buf, content.len); 1247 cl_assert_equal_i(rename(table_path.buf, st->list_file), 0); 1248 1249 err = reftable_stack_reload(st); 1250 cl_assert_equal_i(err, -4); 1251 cl_assert_equal_i(st->merged->tables_len, 2); 1252 1253 /* 1254 * Even though the reload has failed, we should be able to continue 1255 * using the iterator. 1256 */ 1257 cl_assert_equal_i(reftable_iterator_next_ref(&it, &rec), 0); 1258 cl_assert_equal_s(rec.refname, "refs/heads/branch-0000"); 1259 cl_assert_equal_i(reftable_iterator_next_ref(&it, &rec), 0); 1260 cl_assert_equal_s(rec.refname, "refs/heads/branch-0001"); 1261 cl_assert(reftable_iterator_next_ref(&it, &rec) > 0); 1262 1263 reftable_ref_record_release(&rec); 1264 reftable_iterator_destroy(&it); 1265 reftable_stack_destroy(st); 1266 reftable_buf_release(&table_path); 1267 reftable_buf_release(&content); 1268 clear_dir(dir); 1269 } 1270 1271 static int write_limits_after_ref(struct reftable_writer *wr, void *arg) 1272 { 1273 struct reftable_ref_record *ref = arg; 1274 cl_assert_equal_i(reftable_writer_set_limits(wr, 1275 ref->update_index, ref->update_index), 0); 1276 cl_assert_equal_i(reftable_writer_add_ref(wr, ref), 0); 1277 return reftable_writer_set_limits(wr, ref->update_index, ref->update_index); 1278 } 1279 1280 void test_reftable_stack__invalid_limit_updates(void) 1281 { 1282 struct reftable_ref_record ref = { 1283 .refname = (char *) "HEAD", 1284 .update_index = 1, 1285 .value_type = REFTABLE_REF_SYMREF, 1286 .value.symref = (char *) "master", 1287 }; 1288 struct reftable_write_options opts = { 1289 .default_permissions = 0660, 1290 }; 1291 struct reftable_addition *add = NULL; 1292 char *dir = get_tmp_dir(__LINE__); 1293 struct reftable_stack *st = NULL; 1294 1295 cl_assert_equal_i(reftable_new_stack(&st, dir, NULL), 0); 1296 1297 reftable_addition_destroy(add); 1298 1299 cl_assert_equal_i(reftable_stack_addition_new(&add, st, &opts), 0); 1300 1301 /* 1302 * write_limits_after_ref also updates the update indexes after adding 1303 * the record. This should cause an err to be returned, since the limits 1304 * must be set at the start. 1305 */ 1306 cl_assert_equal_i(reftable_addition_add(add, 1307 write_limits_after_ref, &ref), REFTABLE_API_ERROR); 1308 1309 reftable_addition_destroy(add); 1310 reftable_stack_destroy(st); 1311 clear_dir(dir); 1312 } 1313 1314 void test_reftable_stack__two_additions(void) 1315 { 1316 struct reftable_stack *st = NULL; 1317 char *dir = get_tmp_dir(__LINE__); 1318 struct reftable_addition *add1 = NULL; 1319 struct reftable_addition *add2 = NULL; 1320 1321 struct reftable_ref_record ref = { 1322 .refname = (char *) "HEAD", 1323 .update_index = 1, 1324 .value_type = REFTABLE_REF_SYMREF, 1325 .value.symref = (char *) "master", 1326 }; 1327 1328 cl_assert_equal_i(reftable_new_stack(&st, dir, NULL), 0); 1329 1330 cl_assert_equal_i(reftable_stack_addition_new(&add1, st, NULL), 0); 1331 cl_assert_equal_i(reftable_stack_addition_new(&add2, st, NULL), REFTABLE_LOCK_ERROR); 1332 1333 cl_assert_equal_i(reftable_addition_add(add1, write_test_ref, &ref), 0); 1334 1335 cl_assert_equal_i(reftable_addition_commit(add1), 0); 1336 1337 reftable_addition_destroy(add1); 1338 reftable_stack_destroy(st); 1339 clear_dir(dir); 1340 }