fix: report mount not found only when all instances return 404
This commit is contained in:
1 parent
c7a3796f26
commit
a14187a621
2 files changed
+93
-2
No files matched your search
@@ -14,6 +14,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -264,9 +265,19 @@ func (c *Client) ProxyMount(w http.ResponseWriter, r *http.Request, project, mou
|
|||||||
c.Proxy(w, r, path)
|
c.Proxy(w, r, path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ErrMountNotFound is returned by ListMountFiles when every CarrierNode
|
||||||
|
// instance that answered said the directory does not exist and none failed
|
||||||
|
// some other way. It is a definitive "not there", unlike an unreachable
|
||||||
|
// instance or an error response, where the directory may well exist but could
|
||||||
|
// not be checked - callers can use errors.Is to tell the two apart.
|
||||||
|
var ErrMountNotFound = errors.New("carriernode mount directory not found")
|
||||||
|
|
||||||
// ListMountFiles lists the direct children of a project/type mount directory.
|
// ListMountFiles lists the direct children of a project/type mount directory.
|
||||||
// An empty relative path lists the mount root. The CarrierNode endpoint
|
// An empty relative path lists the mount root. The CarrierNode endpoint
|
||||||
// returns both files and directories so callers can build directory browsers.
|
// returns both files and directories so callers can build directory browsers.
|
||||||
|
// When no instance can list the directory, the error wraps ErrMountNotFound
|
||||||
|
// only if every instance answered 404; any unreachable instance or other
|
||||||
|
// failure yields a plain error instead, since the directory may live there.
|
||||||
func (c *Client) ListMountFiles(ctx context.Context, project, mountType, relative string) ([]MountFileEntry, error) {
|
func (c *Client) ListMountFiles(ctx context.Context, project, mountType, relative string) ([]MountFileEntry, error) {
|
||||||
candidates := c.candidateBaseURLs(ctx)
|
candidates := c.candidateBaseURLs(ctx)
|
||||||
if len(candidates) == 0 {
|
if len(candidates) == 0 {
|
||||||
@@ -286,6 +297,7 @@ func (c *Client) ListMountFiles(ctx context.Context, project, mountType, relativ
|
|||||||
var entries []MountFileEntry
|
var entries []MountFileEntry
|
||||||
var lastErr error
|
var lastErr error
|
||||||
loaded := false
|
loaded := false
|
||||||
|
missing := false
|
||||||
for _, baseURL := range candidates {
|
for _, baseURL := range candidates {
|
||||||
request, err := c.newRequest(ctx, baseURL, http.MethodGet, path, nil)
|
request, err := c.newRequest(ctx, baseURL, http.MethodGet, path, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -297,6 +309,11 @@ func (c *Client) ListMountFiles(ctx context.Context, project, mountType, relativ
|
|||||||
lastErr = err
|
lastErr = err
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
if response.StatusCode == http.StatusNotFound {
|
||||||
|
response.Body.Close()
|
||||||
|
missing = true
|
||||||
|
continue
|
||||||
|
}
|
||||||
if response.StatusCode != http.StatusOK {
|
if response.StatusCode != http.StatusOK {
|
||||||
response.Body.Close()
|
response.Body.Close()
|
||||||
lastErr = fmt.Errorf("carriernode mount listing failed: %s", response.Status)
|
lastErr = fmt.Errorf("carriernode mount listing failed: %s", response.Status)
|
||||||
@@ -314,8 +331,13 @@ func (c *Client) ListMountFiles(ctx context.Context, project, mountType, relativ
|
|||||||
loaded = true
|
loaded = true
|
||||||
entries = append(entries, payload.Files...)
|
entries = append(entries, payload.Files...)
|
||||||
}
|
}
|
||||||
if !loaded && lastErr != nil {
|
if !loaded {
|
||||||
return nil, lastErr
|
if lastErr != nil {
|
||||||
|
return nil, lastErr
|
||||||
|
}
|
||||||
|
if missing {
|
||||||
|
return nil, ErrMountNotFound
|
||||||
|
}
|
||||||
}
|
}
|
||||||
unique := make([]MountFileEntry, 0, len(entries))
|
unique := make([]MountFileEntry, 0, len(entries))
|
||||||
seen := make(map[string]struct{}, len(entries))
|
seen := make(map[string]struct{}, len(entries))
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package carriernodeclient
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -194,3 +195,71 @@ func TestProxyForwardsLastInstanceErrorWhenNoneHaveTheFile(t *testing.T) {
|
|||||||
t.Fatalf("status = %d, want 404 forwarded from the last instance", rec.Code)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user