fix: report mount not found only when all instances return 404

This commit is contained in:
Gabriel20xx committed 2026-10-08 11:28:14 +00:00
1 parent c7a3796f26
commit a14187a621
2 files changed
+93 -2

No files matched your search

@@ -2,6 +2,7 @@ package carriernodeclient
import (
"context"
"errors"
"net/http"
"net/http/httptest"
"testing"
@@ -194,3 +195,71 @@ func TestProxyForwardsLastInstanceErrorWhenNoneHaveTheFile(t *testing.T) {
t.Fatalf("status = %d, want 404 forwarded from the last instance", rec.Code)
}
}
func mountListingServer(status int, body string) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
_, _ = w.Write([]byte(body))
}))
}
func TestListMountFilesReportsNotFoundWhenEveryInstanceMisses(t *testing.T) {
first := mountListingServer(http.StatusNotFound, `{"error":"mount directory not found"}`)
defer first.Close()
second := mountListingServer(http.StatusNotFound, `{"error":"mount directory not found"}`)
defer second.Close()
client := NewMulti("", func(ctx context.Context) []string { return []string{first.URL, second.URL} })
_, err := client.ListMountFiles(context.Background(), "autocode", "repos", "gone")
if !errors.Is(err, ErrMountNotFound) {
t.Fatalf("ListMountFiles error = %v, want ErrMountNotFound", err)
}
}
func TestListMountFilesDoesNotReportNotFoundWhenAnInstanceIsUnreachable(t *testing.T) {
missing := mountListingServer(http.StatusNotFound, `{"error":"mount directory not found"}`)
defer missing.Close()
down := httptest.NewServer(nil)
down.Close() // closed immediately so requests to it fail outright
client := NewMulti("", func(ctx context.Context) []string { return []string{missing.URL, down.URL} })
_, err := client.ListMountFiles(context.Background(), "autocode", "repos", "maybe-on-the-down-instance")
if err == nil {
t.Fatal("ListMountFiles succeeded, want an error")
}
if errors.Is(err, ErrMountNotFound) {
t.Fatalf("ListMountFiles error = %v, must not claim the directory is missing when an instance could not be checked", err)
}
}
func TestListMountFilesDoesNotReportNotFoundOnServerError(t *testing.T) {
broken := mountListingServer(http.StatusInternalServerError, `{"error":"boom"}`)
defer broken.Close()
client := NewMulti("", func(ctx context.Context) []string { return []string{broken.URL} })
_, err := client.ListMountFiles(context.Background(), "autocode", "repos", "")
if err == nil || errors.Is(err, ErrMountNotFound) {
t.Fatalf("ListMountFiles error = %v, want a non-ErrMountNotFound failure", err)
}
}
func TestListMountFilesUsesTheInstanceThatHasTheDirectory(t *testing.T) {
missing := mountListingServer(http.StatusNotFound, `{"error":"mount directory not found"}`)
defer missing.Close()
present := mountListingServer(http.StatusOK, `{"files":[{"name":"repo","path":"repo","directory":true}]}`)
defer present.Close()
client := NewMulti("", func(ctx context.Context) []string { return []string{missing.URL, present.URL} })
files, err := client.ListMountFiles(context.Background(), "autocode", "repos", "")
if err != nil {
t.Fatalf("ListMountFiles error: %v", err)
}
if len(files) != 1 || files[0].Name != "repo" || !files[0].Directory {
t.Fatalf("ListMountFiles = %+v, want the one directory from the instance that has it", files)
}
}