forked from longtao.wu/droboticflow
148 lines
5.3 KiB
Go
148 lines
5.3 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/store"
|
|
|
|
"github.com/baidubce/bce-sdk-go/bce"
|
|
"github.com/baidubce/bce-sdk-go/services/bos"
|
|
)
|
|
|
|
func TestRenameDoesNotOverwriteDifferentTarget(t *testing.T) {
|
|
for _, same := range []bool{false, true} {
|
|
t.Run(map[bool]string{false: "different target retained", true: "identical retry skips copy"}[same], func(t *testing.T) {
|
|
writes, deletes := 0, 0
|
|
endpoint := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method == http.MethodHead {
|
|
w.Header().Set("Content-Length", "4")
|
|
w.Header().Set("Content-Type", "application/octet-stream")
|
|
etag := "source-etag"
|
|
if strings.HasSuffix(r.URL.Path, "target.bin") && !same {
|
|
etag = "different-etag"
|
|
}
|
|
w.Header().Set("ETag", etag)
|
|
return
|
|
}
|
|
if r.Method == http.MethodDelete {
|
|
deletes++
|
|
w.WriteHeader(http.StatusNoContent)
|
|
return
|
|
}
|
|
writes++
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
}))
|
|
defer endpoint.Close()
|
|
client, err := bos.NewClient("test-ak", "test-sk", endpoint.URL)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
browser := &bosObjectStorageBrowser{client: client}
|
|
_, err = browser.RenameObject(context.Background(), "bucket", "source.bin", "target.bin")
|
|
if same && err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !same && (err == nil || !strings.Contains(err.Error(), "different content")) {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if writes != 0 {
|
|
t.Fatalf("unexpected overwrite requests: %d", writes)
|
|
}
|
|
wantDeletes := 0
|
|
if same {
|
|
wantDeletes = 1
|
|
}
|
|
if deletes != wantDeletes {
|
|
t.Fatalf("source deletes=%d, want %d", deletes, wantDeletes)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestExactObjectStorageKeyPreservesTrailingSlash(t *testing.T) {
|
|
if got := exactObjectStorageKey(" algo/nested/ "); got != "algo/nested/" {
|
|
t.Fatalf("exactObjectStorageKey preserved key = %q, want %q", got, "algo/nested/")
|
|
}
|
|
}
|
|
|
|
func TestIsBOSNoSuchKeyRecognizesServiceError(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
err error
|
|
want bool
|
|
}{
|
|
{name: "explicit no such key", err: bce.NewBceServiceError("NoSuchKey", "The specified key does not exist.", "req-1", http.StatusNotFound), want: true},
|
|
{name: "empty body head response", err: bce.NewBceServiceError("NotFound", "404 Not Found", "req-2", http.StatusNotFound), want: true},
|
|
{name: "missing bucket remains an error", err: bce.NewBceServiceError("NoSuchBucket", "The specified bucket does not exist.", "req-3", http.StatusNotFound), want: false},
|
|
{name: "not found code requires 404", err: bce.NewBceServiceError("NotFound", "unexpected", "req-4", http.StatusInternalServerError), want: false},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
if got := isBOSNoSuchKey(test.err); got != test.want {
|
|
t.Fatalf("isBOSNoSuchKey() = %t, want %t", got, test.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestRetryObjectStorageSummaryRetriesTransientNotFound(t *testing.T) {
|
|
attempts := 0
|
|
want := domain.ObjectStorageObjectSummary{Bucket: "datastorage", Key: "large/file.bin"}
|
|
got, err := retryObjectStorageSummary(context.Background(), 4, 0, func() (domain.ObjectStorageObjectSummary, error) {
|
|
attempts++
|
|
if attempts < 3 {
|
|
return domain.ObjectStorageObjectSummary{}, bce.NewBceServiceError("NotFound", "404 Not Found", "req-retry", http.StatusNotFound)
|
|
}
|
|
return want, nil
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("retryObjectStorageSummary() error = %v", err)
|
|
}
|
|
if attempts != 3 || got.Bucket != want.Bucket || got.Key != want.Key {
|
|
t.Fatalf("retryObjectStorageSummary() attempts = %d summary = %#v, want attempts=3 summary=%#v", attempts, got, want)
|
|
}
|
|
}
|
|
|
|
func TestRetryObjectStorageSummaryDoesNotRetryOtherErrors(t *testing.T) {
|
|
attempts := 0
|
|
wantErr := bce.NewBceServiceError("NoSuchBucket", "missing bucket", "req-bucket", http.StatusNotFound)
|
|
_, err := retryObjectStorageSummary(context.Background(), 4, 0, func() (domain.ObjectStorageObjectSummary, error) {
|
|
attempts++
|
|
return domain.ObjectStorageObjectSummary{}, wantErr
|
|
})
|
|
if !errors.Is(err, wantErr) || attempts != 1 {
|
|
t.Fatalf("retryObjectStorageSummary() error = %v attempts = %d, want original error and one attempt", err, attempts)
|
|
}
|
|
}
|
|
|
|
func TestRenderObjectStorageWriteErrorTreatsBOSNoSuchKeyAsNotFound(t *testing.T) {
|
|
recorder := httptest.NewRecorder()
|
|
err := bce.NewBceServiceError("NoSuchKey", "The specified key does not exist.", "req-2", http.StatusNotFound)
|
|
|
|
renderObjectStorageWriteError(recorder, err)
|
|
|
|
if recorder.Code != http.StatusNotFound {
|
|
t.Fatalf("status = %d, want %d", recorder.Code, http.StatusNotFound)
|
|
}
|
|
if !strings.Contains(recorder.Body.String(), "NoSuchKey") {
|
|
t.Fatalf("expected response to include NoSuchKey, got %s", recorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestObjectStorageJSONPutRejectsOversizedBody(t *testing.T) {
|
|
body := `{"key":"big.txt","content":"` + strings.Repeat("x", int(objectStorageJSONPutMaxBytes)) + `"}`
|
|
handler := NewHandler(store.NewMemoryStore(), nil)
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, "/api/object-storage/objects", strings.NewReader(body)))
|
|
|
|
if recorder.Code != http.StatusRequestEntityTooLarge {
|
|
t.Fatalf("expected oversized JSON put to return 413, got %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
}
|