forked from mtusi2026/mtusi2026lab7
185 lines
6.0 KiB
Go
185 lines
6.0 KiB
Go
package usecase
|
|
|
|
import (
|
|
"backend/internal/domain/model"
|
|
"backend/internal/domain/port"
|
|
"bytes"
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"errors"
|
|
"github.com/google/uuid"
|
|
"io"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
type fixtureRepo struct {
|
|
rows map[uuid.UUID]model.Attachment
|
|
failure error
|
|
more bool
|
|
}
|
|
|
|
func (f *fixtureRepo) ReserveAttachment(context.Context, model.Attachment) (model.Attachment, error) {
|
|
panic("not used")
|
|
}
|
|
func (f *fixtureRepo) GetAttachment(_ context.Context, id uuid.UUID) (model.Attachment, error) {
|
|
if f.failure != nil {
|
|
return model.Attachment{}, f.failure
|
|
}
|
|
a, ok := f.rows[id]
|
|
if !ok {
|
|
return a, model.ErrNotFound
|
|
}
|
|
return a, nil
|
|
}
|
|
func (f *fixtureRepo) SetAttachmentStatus(_ context.Context, id uuid.UUID, from, to string) (bool, error) {
|
|
a := f.rows[id]
|
|
if a.Status != from {
|
|
return false, nil
|
|
}
|
|
a.Status = to
|
|
f.rows[id] = a
|
|
return true, nil
|
|
}
|
|
func (f *fixtureRepo) ReconcileAttachments(context.Context, uuid.UUID) ([]model.Attachment, bool, error) {
|
|
out := []model.Attachment{}
|
|
for _, a := range f.rows {
|
|
out = append(out, a)
|
|
}
|
|
return out, f.more, f.failure
|
|
}
|
|
|
|
type fixtureObjects struct {
|
|
items map[string]port.Object
|
|
content []byte
|
|
failure error
|
|
removed int
|
|
changed bool
|
|
}
|
|
|
|
func (f *fixtureObjects) Put(context.Context, string, io.Reader, int64, string) error {
|
|
panic("not used")
|
|
}
|
|
func (f *fixtureObjects) Get(context.Context, string) (io.ReadCloser, error) {
|
|
return io.NopCloser(bytes.NewReader(f.content)), f.failure
|
|
}
|
|
func (f *fixtureObjects) Stat(_ context.Context, key string) (port.Object, error) {
|
|
if f.failure != nil {
|
|
return port.Object{}, f.failure
|
|
}
|
|
o, ok := f.items[key]
|
|
if !ok {
|
|
return o, model.ErrNotFound
|
|
}
|
|
if f.changed {
|
|
o.ETag = "changed"
|
|
}
|
|
return o, nil
|
|
}
|
|
func (f *fixtureObjects) Remove(_ context.Context, key string) error {
|
|
delete(f.items, key)
|
|
f.removed++
|
|
return nil
|
|
}
|
|
func (f *fixtureObjects) List(context.Context, string) ([]port.Object, bool, error) {
|
|
out := []port.Object{}
|
|
for _, o := range f.items {
|
|
out = append(out, o)
|
|
}
|
|
return out, false, f.failure
|
|
}
|
|
|
|
var fixedNow = time.Date(2026, 11, 12, 12, 0, 0, 0, time.UTC)
|
|
|
|
func TestOrphanCleanupHonorsGraceAndDryRun(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
age time.Duration
|
|
apply, used, changed bool
|
|
wantDeleted int
|
|
}{
|
|
{name: "old dry run", age: 2 * time.Minute},
|
|
{name: "old apply", age: 2 * time.Minute, apply: true, wantDeleted: 1},
|
|
{name: "fresh protected", age: 10 * time.Second, apply: true},
|
|
{name: "future timestamp protected", age: -time.Minute, apply: true},
|
|
{name: "referenced protected", age: 2 * time.Minute, apply: true, used: true},
|
|
{name: "changed inventory protected", age: 2 * time.Minute, apply: true, changed: true},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
id := uuid.MustParse("00000000-0000-0000-0000-000000000001")
|
|
key := ObjectKey(id)
|
|
repo := &fixtureRepo{rows: map[uuid.UUID]model.Attachment{}}
|
|
if tc.used {
|
|
repo.rows[id] = model.Attachment{ID: id, Status: "ready", CreatedAt: fixedNow}
|
|
}
|
|
objects := &fixtureObjects{items: map[string]port.Object{key: {Key: key, ETag: "original", Modified: fixedNow.Add(-tc.age)}}, changed: tc.changed}
|
|
report, e := NewReconciler(repo, objects, func() time.Time { return fixedNow }).Run(context.Background(), uuid.Nil, "", time.Minute, tc.apply)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if objects.removed != tc.wantDeleted {
|
|
t.Fatalf("deleted %d, want %d; report=%+v", objects.removed, tc.wantDeleted, report)
|
|
}
|
|
if tc.wantDeleted > 0 {
|
|
_, e = NewReconciler(repo, objects, func() time.Time { return fixedNow }).Run(context.Background(), uuid.Nil, "", time.Minute, true)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if objects.removed != 1 {
|
|
t.Fatal("second run deleted something twice")
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
func TestMissingAndPendingMetadataRepair(t *testing.T) {
|
|
for _, status := range []string{"ready", "uploading", "missing"} {
|
|
t.Run(status, func(t *testing.T) {
|
|
id := uuid.New()
|
|
raw := []byte("original bytes")
|
|
sum := sha256.Sum256(raw)
|
|
repo := &fixtureRepo{rows: map[uuid.UUID]model.Attachment{id: {ID: id, Status: status, CreatedAt: fixedNow.Add(-2 * time.Minute), UpdatedAt: fixedNow.Add(-2 * time.Minute), SizeBytes: int64(len(raw)), SHA256: hex.EncodeToString(sum[:])}}}
|
|
objects := &fixtureObjects{items: map[string]port.Object{}, content: raw}
|
|
want := "missing"
|
|
if status != "ready" {
|
|
objects.items[ObjectKey(id)] = port.Object{Key: ObjectKey(id), Size: int64(len(raw)), Modified: fixedNow.Add(-2 * time.Minute)}
|
|
want = "ready"
|
|
}
|
|
_, e := NewReconciler(repo, objects, func() time.Time { return fixedNow }).Run(context.Background(), uuid.Nil, "", time.Minute, true)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if got := repo.rows[id].Status; got != want {
|
|
t.Fatalf("status %s, want %s", got, want)
|
|
}
|
|
if objects.removed != 0 {
|
|
t.Fatal("metadata repair removed original")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
func TestDependencyFailureNeverDeletes(t *testing.T) {
|
|
repo := &fixtureRepo{rows: map[uuid.UUID]model.Attachment{}, failure: model.ErrUnavailable}
|
|
objects := &fixtureObjects{items: map[string]port.Object{}}
|
|
_, e := NewReconciler(repo, objects, func() time.Time { return fixedNow }).Run(context.Background(), uuid.Nil, "", time.Minute, true)
|
|
if !errors.Is(e, model.ErrUnavailable) {
|
|
t.Fatalf("expected dependency error, got %v", e)
|
|
}
|
|
if objects.removed != 0 {
|
|
t.Fatal("deleted during failed inventory")
|
|
}
|
|
}
|
|
func TestFailureDoesNotReturnCursorPastFailedRecord(t *testing.T) {
|
|
id := uuid.New()
|
|
repo := &fixtureRepo{rows: map[uuid.UUID]model.Attachment{id: {ID: id, Status: "ready", CreatedAt: fixedNow.Add(-2 * time.Minute)}}, more: true}
|
|
objects := &fixtureObjects{failure: model.ErrUnavailable}
|
|
report, err := NewReconciler(repo, objects, func() time.Time { return fixedNow }).Run(context.Background(), uuid.Nil, "", time.Minute, true)
|
|
if !errors.Is(err, model.ErrUnavailable) {
|
|
t.Fatalf("expected failure, got %v", err)
|
|
}
|
|
if report.NextMetadata != "" || report.NextObject != "" {
|
|
t.Fatal("failed inventory must restart rather than skip the failed item")
|
|
}
|
|
}
|