|
| 1 | +package remote |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os/exec" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/go-git/go-git/v6/plumbing" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + |
| 11 | + "github.com/entireio/cli/cmd/entire/cli/testutil" |
| 12 | +) |
| 13 | + |
| 14 | +// checkpointRefFixture creates a work repo whose origin is a local bare repo |
| 15 | +// holding (or not holding) a checkpoint ref, and chdirs into the work repo so |
| 16 | +// fetch-target resolution finds origin. |
| 17 | +func checkpointRefFixture(t *testing.T, withRef bool) (workDir string, ref plumbing.ReferenceName) { |
| 18 | + t.Helper() |
| 19 | + bareDir := t.TempDir() |
| 20 | + out, err := exec.CommandContext(t.Context(), "git", "init", "--bare", bareDir).CombinedOutput() |
| 21 | + require.NoError(t, err, "git init --bare: %s", out) |
| 22 | + |
| 23 | + workDir = t.TempDir() |
| 24 | + testutil.InitRepo(t, workDir) |
| 25 | + testutil.WriteFile(t, workDir, "f.txt", "content") |
| 26 | + testutil.GitAdd(t, workDir, "f.txt") |
| 27 | + testutil.GitCommit(t, workDir, "init") |
| 28 | + out, err = exec.CommandContext(t.Context(), "git", "-C", workDir, "remote", "add", "origin", bareDir).CombinedOutput() |
| 29 | + require.NoError(t, err, "git remote add: %s", out) |
| 30 | + |
| 31 | + ref = plumbing.ReferenceName("refs/entire/checkpoints/Z9/01KVBJCWYA4YW6J5M9GP655HZ9") |
| 32 | + if withRef { |
| 33 | + out, err = exec.CommandContext(t.Context(), "git", "-C", workDir, "push", "--quiet", "origin", "HEAD:"+ref.String()).CombinedOutput() |
| 34 | + require.NoError(t, err, "git push checkpoint ref: %s", out) |
| 35 | + } |
| 36 | + |
| 37 | + t.Chdir(workDir) |
| 38 | + return workDir, ref |
| 39 | +} |
| 40 | + |
| 41 | +// TestFetchCheckpointRef_RemoteMissingRefIsAbsence: a remote that does not |
| 42 | +// have the requested checkpoint ref is ABSENCE, not a transport failure — the |
| 43 | +// error must wrap plumbing.ErrReferenceNotFound so store probes (reads and |
| 44 | +// backfill writes) classify it as "checkpoint not found" and, under kind |
| 45 | +// routing, may legitimately fall through to another backend. Before this |
| 46 | +// distinction, git fetch of a missing refspec failed like a network error, |
| 47 | +// which made wiring a fetcher into write paths unsafe. |
| 48 | +func TestFetchCheckpointRef_RemoteMissingRefIsAbsence(t *testing.T) { |
| 49 | + _, ref := checkpointRefFixture(t, false) |
| 50 | + |
| 51 | + err := FetchCheckpointRef(context.Background(), ref) |
| 52 | + require.Error(t, err) |
| 53 | + require.ErrorIs(t, err, plumbing.ErrReferenceNotFound, |
| 54 | + "a ref the remote does not have must classify as absence") |
| 55 | +} |
| 56 | + |
| 57 | +// TestFetchCheckpointRef_PresentRefFetches: the ref exists on the remote but |
| 58 | +// not locally; the fetch must create the local ref of the same name. |
| 59 | +func TestFetchCheckpointRef_PresentRefFetches(t *testing.T) { |
| 60 | + workDir, ref := checkpointRefFixture(t, true) |
| 61 | + |
| 62 | + require.NoError(t, FetchCheckpointRef(context.Background(), ref)) |
| 63 | + |
| 64 | + out, err := exec.CommandContext(t.Context(), "git", "-C", workDir, "show-ref", "--verify", ref.String()).CombinedOutput() |
| 65 | + require.NoError(t, err, "fetched ref must exist locally: %s", out) |
| 66 | +} |
| 67 | + |
| 68 | +// TestFetchCheckpointRef_UnreachableRemoteIsFailure: a transport-level |
| 69 | +// failure (unreachable remote) must NOT classify as absence. |
| 70 | +func TestFetchCheckpointRef_UnreachableRemoteIsFailure(t *testing.T) { |
| 71 | + workDir, ref := checkpointRefFixture(t, false) |
| 72 | + out, err := exec.CommandContext(t.Context(), "git", "-C", workDir, "remote", "set-url", "origin", workDir+"/nonexistent-remote").CombinedOutput() |
| 73 | + require.NoError(t, err, "%s", out) |
| 74 | + |
| 75 | + err = FetchCheckpointRef(context.Background(), ref) |
| 76 | + require.Error(t, err) |
| 77 | + require.NotErrorIs(t, err, plumbing.ErrReferenceNotFound, |
| 78 | + "a transport failure must stay distinguishable from absence") |
| 79 | +} |
0 commit comments