github.com/treeverse/lakefs@v1.24.1-0.20240520134607-95648127bfb0/pkg/graveler/retention/starting_point_iterator_test.go (about) 1 package retention 2 3 import ( 4 "sort" 5 "testing" 6 7 "github.com/treeverse/lakefs/pkg/graveler" 8 "github.com/treeverse/lakefs/pkg/graveler/testutil" 9 ) 10 11 func TestStartingPointIterator(t *testing.T) { 12 tests := map[string]struct { 13 branches []string 14 commits []string 15 }{ 16 "empty": {}, 17 "no_repetitions": { 18 branches: []string{"1", "3", "5"}, 19 commits: []string{"2", "4"}, 20 }, 21 "repetitions": { 22 branches: []string{"2", "3", "4"}, 23 commits: []string{"1", "4", "5"}, 24 }, 25 "commits_empty": { 26 branches: []string{"2", "3", "4"}, 27 commits: []string{}, 28 }, 29 "branches_empty": { 30 branches: []string{}, 31 commits: []string{"2", "3", "4"}, 32 }, 33 "no_overlap": { 34 branches: []string{"1", "2", "3", "4"}, 35 commits: []string{"5", "6", "7", "8"}, 36 }, 37 "no_overlap_reverse": { 38 branches: []string{"5", "6", "7", "8"}, 39 commits: []string{"1", "2", "3", "4"}, 40 }, 41 "many_repetitions": { 42 branches: []string{"1", "2", "4", "5", "6", "7", "8"}, 43 commits: []string{"0", "1", "2", "3", "6", "9"}, 44 }, 45 } 46 for name, tst := range tests { 47 t.Run(name, func(t *testing.T) { 48 var branchRecords []*graveler.BranchRecord 49 var commitRecords []*graveler.CommitRecord 50 expected := make([]string, 0) 51 branchMap := make(map[string]struct{}) 52 for _, b := range tst.branches { 53 branchMap[b] = struct{}{} 54 expected = append(expected, b) 55 branchRecords = append(branchRecords, &graveler.BranchRecord{ 56 BranchID: graveler.BranchID(b), 57 Branch: &graveler.Branch{ 58 CommitID: graveler.CommitID(b), 59 }, 60 }) 61 } 62 for _, c := range tst.commits { 63 if _, ok := branchMap[c]; !ok { 64 expected = append(expected, c) 65 } 66 commitRecords = append(commitRecords, &graveler.CommitRecord{ 67 CommitID: graveler.CommitID(c), 68 }) 69 } 70 sort.Strings(expected) 71 branchIterator := testutil.NewFakeBranchIterator(branchRecords) 72 commitIterator := testutil.NewFakeCommitIterator(commitRecords) 73 it := NewGCStartingPointIterator(commitIterator, branchIterator) 74 i := 0 75 for it.Next() { 76 val := it.Value() 77 if string(val.CommitID) != expected[i] { 78 t.Fatalf("unexpected returned commit id in index %d. expected=%s, actual=%s", i, expected[i], val) 79 } 80 expectedBranchID := "" 81 if _, ok := branchMap[string(val.CommitID)]; ok { 82 expectedBranchID = string(val.CommitID) 83 } 84 if string(val.BranchID) != expectedBranchID { 85 t.Fatalf("got unexpected branch_id for commit %s. expected=%s, got=%s", val.CommitID, expectedBranchID, val.BranchID) 86 } 87 i++ 88 } 89 if it.Err() != nil { 90 t.Fatalf("unexpected error: %v", it.Err()) 91 } 92 it.Close() 93 if i != len(expected) { 94 t.Fatalf("got unexpected number of results. expected=%d, got=%d", len(expected), i) 95 } 96 }) 97 } 98 }