|
| 1 | +package gitcache |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestRemoveDeletesARepository(t *testing.T) { |
| 10 | + upstream := newFakeUpstream(t) |
| 11 | + upstream.newRepo("repo") |
| 12 | + tc := newTestCache(t, upstream.server.URL, nil) |
| 13 | + tc.clone(t, "repo") |
| 14 | + waitIdle(t, tc.Cache) |
| 15 | + dir := filepath.Join(tc.dir, "up", "repo.git") |
| 16 | + want := tc.repo("up/repo.git").size.Load() |
| 17 | + |
| 18 | + freed, err := tc.Remove("up/repo.git") |
| 19 | + if err != nil { |
| 20 | + t.Fatalf("Remove: %v", err) |
| 21 | + } |
| 22 | + if freed != want || freed == 0 { |
| 23 | + t.Errorf("freed %d bytes, want %d", freed, want) |
| 24 | + } |
| 25 | + if fileExists(dir) || tc.repo("up/repo.git") != nil { |
| 26 | + t.Error("the repository is still cached") |
| 27 | + } |
| 28 | + if len(tc.Repositories()) != 0 { |
| 29 | + t.Errorf("Repositories() = %+v, want none", tc.Repositories()) |
| 30 | + } |
| 31 | + if _, err := tc.Remove("up/repo.git"); !errors.Is(err, ErrNotCached) { |
| 32 | + t.Errorf("second Remove: %v, want ErrNotCached", err) |
| 33 | + } |
| 34 | + |
| 35 | + // The next clone fetches it again. |
| 36 | + requests := upstream.gitRequests.Load() |
| 37 | + tc.clone(t, "repo") |
| 38 | + if upstream.gitRequests.Load() == requests || !fileExists(dir) { |
| 39 | + t.Error("the clone after Remove did not fetch the repository again") |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +// A repository that a clone, a fetch or a client is using is not removed, |
| 44 | +// so nothing reads it while it is deleted. |
| 45 | +func TestRemoveRefusesARepositoryInUse(t *testing.T) { |
| 46 | + upstream := newFakeUpstream(t) |
| 47 | + upstream.newRepo("repo") |
| 48 | + tc := newTestCache(t, upstream.server.URL, nil) |
| 49 | + tc.clone(t, "repo") |
| 50 | + waitIdle(t, tc.Cache) |
| 51 | + |
| 52 | + rp := tc.acquire("up/repo.git") |
| 53 | + if _, err := tc.Remove("up/repo.git"); !errors.Is(err, ErrInUse) { |
| 54 | + t.Errorf("Remove while in use: %v, want ErrInUse", err) |
| 55 | + } |
| 56 | + tc.release(rp) |
| 57 | + if !fileExists(filepath.Join(tc.dir, "up", "repo.git")) { |
| 58 | + t.Fatal("the repository was removed while in use") |
| 59 | + } |
| 60 | + if _, err := tc.Remove("up/repo.git"); err != nil { |
| 61 | + t.Errorf("Remove once idle: %v", err) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func TestRemoveUnknownRepository(t *testing.T) { |
| 66 | + upstream := newFakeUpstream(t) |
| 67 | + tc := newTestCache(t, upstream.server.URL, nil) |
| 68 | + for _, key := range []string{"up/missing.git", "up/../up.git", ""} { |
| 69 | + if _, err := tc.Remove(key); !errors.Is(err, ErrNotCached) { |
| 70 | + t.Errorf("Remove(%q): %v, want ErrNotCached", key, err) |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// A repository whose first clone is still running is in use, not missing. |
| 76 | +func TestRemoveRefusesARepositoryBeingCloned(t *testing.T) { |
| 77 | + upstream := newFakeUpstream(t) |
| 78 | + upstream.newRepo("repo") |
| 79 | + tc := newTestCache(t, upstream.server.URL, nil) |
| 80 | + arrived, release := upstream.holdFetches() |
| 81 | + cloned := make(chan error, 1) |
| 82 | + go func() { |
| 83 | + _, err := tryGit(t, "", "clone", "--quiet", tc.url("repo"), filepath.Join(t.TempDir(), "clone")) |
| 84 | + cloned <- err |
| 85 | + }() |
| 86 | + <-arrived |
| 87 | + if _, err := tc.Remove("up/repo.git"); !errors.Is(err, ErrInUse) { |
| 88 | + t.Errorf("Remove during the first clone: %v, want ErrInUse", err) |
| 89 | + } |
| 90 | + release() |
| 91 | + if err := <-cloned; err != nil { |
| 92 | + t.Fatalf("clone: %v", err) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +// Wants a removed repository could not get may fit once it is cloned again, |
| 97 | +// so removing it clears them, and only them. |
| 98 | +func TestRemoveForgetsTheRepositorysFailedWants(t *testing.T) { |
| 99 | + upstream := newFakeUpstream(t) |
| 100 | + upstream.newRepo("repo") |
| 101 | + tc := newTestCache(t, upstream.server.URL, nil) |
| 102 | + tc.clone(t, "repo") |
| 103 | + waitIdle(t, tc.Cache) |
| 104 | + oid := "0123456789abcdef0123456789abcdef01234567" |
| 105 | + tc.recordFailedWants("up/repo.git", []string{oid}, ErrTooLarge) |
| 106 | + tc.recordFailedWants("up/other.git", []string{oid}, ErrTooLarge) |
| 107 | + |
| 108 | + if _, err := tc.Remove("up/repo.git"); err != nil { |
| 109 | + t.Fatalf("Remove: %v", err) |
| 110 | + } |
| 111 | + if got := tc.withoutFailedWants("up/repo.git", []string{oid}); len(got) != 1 { |
| 112 | + t.Error("the removed repository's failed want is still held back") |
| 113 | + } |
| 114 | + if got := tc.withoutFailedWants("up/other.git", []string{oid}); len(got) != 0 { |
| 115 | + t.Error("another repository's failed want was cleared") |
| 116 | + } |
| 117 | +} |
0 commit comments