| new file mode 100644 |
| index 000000000..0ecd4fa20 |
| --- /dev/null |
| +++ b/internal/release/batchdelete_test.go |
| @@ -0,0 +1,302 @@ |
| +package release |
| + |
| +import ( |
| + "context" |
| + "encoding/xml" |
| + "errors" |
| + "fmt" |
| + "io" |
| + "net/http" |
| + "net/http/httptest" |
| + "os" |
| + "strings" |
| + "sync" |
| + "testing" |
| +) |
| + |
| +// fakeDeleteS3 is a local HTTP S3 that answers DeleteObjects (POST ?delete) |
| +// through a caller hook and records the keys of every request. It also |
| +// answers the change-index GET (404) and PUT (200), recording PUT count. |
| +type fakeDeleteS3 struct { |
| + mu sync.Mutex |
| + chunks [][]string |
| + puts int |
| + // answer returns the response status and body for the n-th (0-based) request. |
| + answer func(n int, keys []string) (int, string) |
| +} |
| + |
| +func (f *fakeDeleteS3) serve(t *testing.T) *httptest.Server { |
| + t.Helper() |
| + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| + w.Header().Set("Content-Type", "application/xml") |
| + if _, ok := r.URL.Query()["delete"]; ok && r.Method == http.MethodPost { |
| + raw, _ := io.ReadAll(r.Body) |
| + var req struct { |
| + Objects []struct { |
| + Key string `xml:"Key"` |
| + } `xml:"Object"` |
| + } |
| + if err := xml.Unmarshal(raw, &req); err != nil { |
| + t.Errorf("bad DeleteObjects body: %v", err) |
| + } |
| + keys := make([]string, 0, len(req.Objects)) |
| + for _, o := range req.Objects { |
| + keys = append(keys, o.Key) |
| + } |
| + f.mu.Lock() |
| + n := len(f.chunks) |
| + f.chunks = append(f.chunks, keys) |
| + f.mu.Unlock() |
| + status, body := 200, `<?xml version="1.0" encoding="UTF-8"?><DeleteResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/"></DeleteResult>` |
| + if f.answer != nil { |
| + status, body = f.answer(n, keys) |
| + } |
| + w.WriteHeader(status) |
| + fmt.Fprint(w, body) |
| + return |
| + } |
| + switch r.Method { |
| + case http.MethodGet: // change index absent |
| + w.WriteHeader(404) |
| + fmt.Fprint(w, `<Error><Code>NoSuchKey</Code><Message>no</Message></Error>`) |
| + case http.MethodPut: |
| + f.mu.Lock() |
| + f.puts++ |
| + f.mu.Unlock() |
| + w.Header().Set("ETag", `"e"`) |
| + } |
| + })) |
| + t.Cleanup(srv.Close) |
| + return srv |
| +} |
| + |
| +func (f *fakeDeleteS3) sizes() []int { |
| + f.mu.Lock() |
| + defer f.mu.Unlock() |
| + var out []int |
| + for _, c := range f.chunks { |
| + out = append(out, len(c)) |
| + } |
| + return out |
| +} |
| + |
| +func newDeleteS3(t *testing.T, f *fakeDeleteS3) *S3Store { |
| + t.Helper() |
| + rgwEnv(t, f.serve(t).URL) |
| + s, err := NewS3Store(context.Background(), "b") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + return s |
| +} |
| + |
| +func batchKeys(n int) []string { |
| + keys := make([]string, n) |
| + for i := range keys { |
| + keys[i] = fmt.Sprintf("apps/a/x/analytics/box/days/k%04d.json", i) |
| + } |
| + return keys |
| +} |
| + |
| +func s3Err(code string) (int, string) { |
| + return 403, `<?xml version="1.0" encoding="UTF-8"?><Error><Code>` + code + `</Code><Message>m</Message></Error>` |
| +} |
| + |
| +func TestBatchDeleteS3SplitsAt1000(t *testing.T) { |
| + f := &fakeDeleteS3{} |
| + s := newDeleteS3(t, f) |
| + var _ BatchDeleter = s |
| + keys := batchKeys(1001) |
| + keys = append(keys, keys[0], keys[1]) // duplicates count once |
| + if err := s.DeleteKeys(context.Background(), keys); err != nil { |
| + t.Fatal(err) |
| + } |
| + if got := f.sizes(); len(got) != 2 || got[0] != 1000 || got[1] != 1 { |
| + t.Fatalf("request sizes = %v, want [1000 1]", got) |
| + } |
| + if err := s.DeleteKeys(context.Background(), nil); err != nil || len(f.sizes()) != 2 { |
| + t.Fatalf("empty call: err=%v requests=%v", err, f.sizes()) |
| + } |
| +} |
| + |
| +func TestBatchDeleteS3PerKeyErrors(t *testing.T) { |
| + f := &fakeDeleteS3{answer: func(_ int, keys []string) (int, string) { |
| + return 200, `<?xml version="1.0" encoding="UTF-8"?><DeleteResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/"> |
| +<Error><Key>` + keys[1] + `</Key><Code>AccessDenied</Code><Message>nope</Message></Error> |
| +<Error><Key>` + keys[3] + `</Key><Code>InternalError</Code><Message>boom</Message></Error></DeleteResult>` |
| + }} |
| + s := newDeleteS3(t, f) |
| + keys := batchKeys(5) |
| + err := s.DeleteKeys(context.Background(), keys) |
| + var be *BatchDeleteError |
| + if !errors.As(err, &be) { |
| + t.Fatalf("err = %v, want *BatchDeleteError", err) |
| + } |
| + if len(be.Failed) != 2 || be.Failed[keys[1]] == nil || be.Failed[keys[3]] == nil { |
| + t.Fatalf("Failed = %v, want exactly keys 1 and 3", be.Failed) |
| + } |
| + if !strings.Contains(be.Failed[keys[1]].Error(), "AccessDenied") { |
| + t.Fatalf("per-key error lost its code: %v", be.Failed[keys[1]]) |
| + } |
| + if got := len(keys) - len(FailedKeys(err)); got != 3 { |
| + t.Fatalf("precise success count = %d, want 3", got) |
| + } |
| +} |
| + |
| +func TestBatchDeleteS3RequestFailure(t *testing.T) { |
| + // Second request fails outright: only its keys are reported, the first |
| + // chunk's success is not. |
| + f := &fakeDeleteS3{answer: func(n int, _ []string) (int, string) { |
| + if n == 1 { |
| + return s3Err("AccessDenied") |
| + } |
| + return 200, `<DeleteResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/"></DeleteResult>` |
| + }} |
| + s := newDeleteS3(t, f) |
| + keys := batchKeys(1001) |
| + err := s.DeleteKeys(context.Background(), keys) |
| + failed := FailedKeys(err) |
| + if len(failed) != 1 || failed[keys[1000]] == nil { |
| + t.Fatalf("Failed = %d keys (%v), want only the second request's key", len(failed), err) |
| + } |
| + if !strings.Contains(failed[keys[1000]].Error(), "AccessDenied") { |
| + t.Fatalf("request failure not identified: %v", failed[keys[1000]]) |
| + } |
| + |
| + // Every request fails: every key is named. |
| + f2 := &fakeDeleteS3{answer: func(int, []string) (int, string) { return s3Err("AccessDenied") }} |
| + s2 := newDeleteS3(t, f2) |
| + err = s2.DeleteKeys(context.Background(), keys) |
| + if got := len(FailedKeys(err)); got != 1001 { |
| + t.Fatalf("Failed = %d keys, want all 1001", got) |
| + } |
| + if !errors.Is(err, FailedKeys(err)[keys[0]]) { |
| + t.Fatal("BatchDeleteError does not unwrap to its per-key errors") |
| + } |
| +} |
| + |
| +func TestBatchDeleteCancelledContextReportsEveryKey(t *testing.T) { |
| + f := &fakeDeleteS3{} |
| + s := newDeleteS3(t, f) |
| + ctx, cancel := context.WithCancel(context.Background()) |
| + cancel() |
| + keys := batchKeys(3) |
| + err := s.DeleteKeys(ctx, keys) |
| + if got := len(FailedKeys(err)); got != 3 || !errors.Is(err, context.Canceled) { |
| + t.Fatalf("err = %v, failed = %d; want all 3 with context.Canceled", err, got) |
| + } |
| + if len(f.sizes()) != 0 { |
| + t.Fatal("a cancelled call still sent requests") |
| + } |
| + d := NewDirStore(t.TempDir()) |
| + if got := len(FailedKeys(d.DeleteKeys(ctx, keys))); got != 3 { |
| + t.Fatalf("DirStore cancelled: failed = %d, want 3", got) |
| + } |
| +} |
| + |
| +func TestBatchDeleteEmptyKeyFailsAlone(t *testing.T) { |
| + d := NewDirStore(t.TempDir()) |
| + ctx := context.Background() |
| + if _, err := d.Put(ctx, "q/a", []byte("x"), ""); err != nil { |
| + t.Fatal(err) |
| + } |
| + err := d.DeleteKeys(ctx, []string{"", "q/a"}) |
| + if f := FailedKeys(err); len(f) != 1 || f[""] == nil { |
| + t.Fatalf("err = %v, want only the empty key failed", err) |
| + } |
| + if _, _, err := d.Get(ctx, "q/a"); !errors.Is(err, os.ErrNotExist) { |
| + t.Fatalf("valid key not deleted: %v", err) |
| + } |
| +} |
| + |
| +func TestBatchDeleteDirStoreMissingIsIdempotent(t *testing.T) { |
| + ctx := context.Background() |
| + d := NewDirStore(t.TempDir()) |
| + for _, k := range []string{"q/a", "q/b"} { |
| + if _, err := d.Put(ctx, k, []byte("x"), ""); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + var _ BatchDeleter = d |
| + if err := d.DeleteKeys(ctx, []string{"q/a", "q/never", "q/b"}); err != nil { |
| + t.Fatalf("missing key reported as error: %v", err) |
| + } |
| + if err := d.DeleteKeys(ctx, []string{"q/a", "q/b"}); err != nil { |
| + t.Fatalf("second run: %v", err) |
| + } |
| + left, _ := d.List(ctx, "q/") |
| + if len(left) != 0 { |
| + t.Fatalf("left = %v", left) |
| + } |
| +} |
| + |
| +func TestBatchDeleteDirStoreChangeIndex(t *testing.T) { |
| + ctx := context.Background() |
| + d := indexedDirStore(t) |
| + other := "apps/bab/blog/instances/h.json" |
| + analytics := "apps/bab/shop/analytics/box/days/2024-01-01.json" |
| + keys := []string{bumpTestKey, "apps/bab/shop/instances/i.json", other, analytics} |
| + for _, k := range keys { |
| + if _, err := d.Put(ctx, k, []byte("x"), ""); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + shop, blog := scopeGen(t, d, bumpTestScope), scopeGen(t, d, "apps/bab/blog") |
| + if err := d.DeleteKeys(ctx, keys); err != nil { |
| + t.Fatal(err) |
| + } |
| + // Same as Delete: each deleted scoped key moves its scope (two shop keys, two bumps). |
| + if got := scopeGen(t, d, bumpTestScope); got != shop+2 { |
| + t.Fatalf("shop gen = %d, want %d", got, shop+2) |
| + } |
| + if got := scopeGen(t, d, "apps/bab/blog"); got != blog+1 { |
| + t.Fatalf("blog gen = %d, want %d", got, blog+1) |
| + } |
| + // Analytics-only batch: no scope exists, so the index is untouched. |
| + idx, _, _ := ReadChangeIndex(ctx, d) |
| + if _, err := d.Put(ctx, analytics, []byte("x"), ""); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := d.DeleteKeys(ctx, []string{analytics}); err != nil { |
| + t.Fatal(err) |
| + } |
| + after, _, _ := ReadChangeIndex(ctx, d) |
| + if fmt.Sprint(idx.Entries) != fmt.Sprint(after.Entries) { |
| + t.Fatalf("analytics delete moved the index: %v -> %v", idx.Entries, after.Entries) |
| + } |
| +} |
| + |
| +func TestBatchDeleteS3ChangeIndex(t *testing.T) { |
| + f := &fakeDeleteS3{} |
| + s := newDeleteS3(t, f) |
| + s.EnableChangeIndex() |
| + // Analytics only: no index write at all. |
| + if err := s.DeleteKeys(context.Background(), batchKeys(3)); err != nil { |
| + t.Fatal(err) |
| + } |
| + if f.puts != 0 { |
| + t.Fatalf("analytics batch wrote the change index %d times", f.puts) |
| + } |
| + // Two keys in one scope plus an analytics key: one bump for the scope. |
| + keys := []string{bumpTestKey, "apps/bab/shop/instances/i.json", batchKeys(1)[0]} |
| + if err := s.DeleteKeys(context.Background(), keys); err != nil { |
| + t.Fatal(err) |
| + } |
| + if f.puts != 1 { |
| + t.Fatalf("change index writes = %d, want 1 for one scope", f.puts) |
| + } |
| +} |
| + |
| +func TestBatchDeleteS3FailedKeysDoNotBump(t *testing.T) { |
| + f := &fakeDeleteS3{answer: func(_ int, keys []string) (int, string) { |
| + return 200, `<DeleteResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Error><Key>` + keys[0] + `</Key><Code>AccessDenied</Code><Message>m</Message></Error></DeleteResult>` |
| + }} |
| + s := newDeleteS3(t, f) |
| + s.EnableChangeIndex() |
| + if err := s.DeleteKeys(context.Background(), []string{bumpTestKey}); err == nil { |
| + t.Fatal("expected failure") |
| + } |
| + if f.puts != 0 { |
| + t.Fatalf("a failed delete bumped the index (%d writes)", f.puts) |
| + } |
| +} |