forked from mtusi2026/mtusi2026lab4
160 lines
5.7 KiB
Go
160 lines
5.7 KiB
Go
package media
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/model/dto"
|
|
)
|
|
|
|
type reconciliationFixture struct {
|
|
records []dto.Attachment
|
|
entries []ObjectInfo
|
|
exists bool
|
|
referenced bool
|
|
claim bool
|
|
scanErr error
|
|
existsErr error
|
|
claimErr error
|
|
finishErr error
|
|
removeErr error
|
|
markErr error
|
|
listErr error
|
|
referenceErr error
|
|
removed int
|
|
}
|
|
|
|
// Scan returns an independent bounded fixture page.
|
|
func (f *reconciliationFixture) Scan(context.Context, uuid.UUID, int) ([]dto.Attachment, error) {
|
|
return f.records, f.scanErr
|
|
}
|
|
|
|
// Reclaim injects failures at the claim or finish boundary independently.
|
|
func (f *reconciliationFixture) Reclaim(_ context.Context, _ uuid.UUID, _ time.Time, finish bool) (bool, error) {
|
|
if finish {
|
|
return f.claim, f.finishErr
|
|
}
|
|
return f.claim, f.claimErr
|
|
}
|
|
|
|
// MarkMissing models availability-state persistence without deleting metadata.
|
|
func (f *reconciliationFixture) MarkMissing(context.Context, uuid.UUID, bool) error {
|
|
return f.markErr
|
|
}
|
|
|
|
// Exists returns an explicit existence or infrastructure-failure outcome.
|
|
func (f *reconciliationFixture) Exists(context.Context, string) (bool, error) {
|
|
return f.exists, f.existsErr
|
|
}
|
|
|
|
// Remove counts applied side effects for dry-run and referenced-object assertions.
|
|
func (f *reconciliationFixture) Remove(context.Context, string) error {
|
|
f.removed++
|
|
return f.removeErr
|
|
}
|
|
|
|
// List supplies object timestamps independently of database state.
|
|
func (f *reconciliationFixture) List(context.Context, string) ([]ObjectInfo, error) {
|
|
return f.entries, f.listErr
|
|
}
|
|
|
|
// HasObject includes reservations so active uploads remain protected.
|
|
func (f *reconciliationFixture) HasObject(context.Context, string) (bool, error) {
|
|
return f.referenced, f.referenceErr
|
|
}
|
|
|
|
// TestReconciliationSafety exercises interrupted cleanup, age protection and missing-object repair.
|
|
func TestReconciliationSafety(t *testing.T) {
|
|
fault := errors.New("dependency failure")
|
|
old := time.Now().Add(-2 * time.Hour)
|
|
cases := []struct {
|
|
name string
|
|
fixture reconciliationFixture
|
|
status string
|
|
created time.Time
|
|
apply bool
|
|
wantErr bool
|
|
changed int
|
|
}{
|
|
{name: "fresh", created: time.Now(), apply: true},
|
|
{name: "scan failure", fixture: reconciliationFixture{scanErr: fault}, wantErr: true},
|
|
{name: "head failure", fixture: reconciliationFixture{existsErr: fault}, wantErr: true},
|
|
{name: "dry abandoned", status: "uploading"},
|
|
{name: "abandoned applied", status: "uploading", apply: true, fixture: reconciliationFixture{claim: true}, changed: 1},
|
|
{name: "claimed by another", status: "uploading", apply: true},
|
|
{name: "claim failure", status: "uploading", apply: true, fixture: reconciliationFixture{claimErr: fault}, wantErr: true},
|
|
{name: "delete failure", status: "deleting", apply: true, fixture: reconciliationFixture{claim: true, removeErr: fault}, wantErr: true},
|
|
{name: "finish failure", status: "deleting", apply: true, fixture: reconciliationFixture{claim: true, finishErr: fault}, wantErr: true},
|
|
{name: "missing dry", status: "ready"},
|
|
{name: "missing applied", status: "ready", apply: true, changed: 1},
|
|
{name: "mark failure", status: "ready", apply: true, fixture: reconciliationFixture{markErr: fault}, wantErr: true},
|
|
{name: "missing repeat", status: "missing", apply: true},
|
|
{name: "restored", status: "missing", apply: true, fixture: reconciliationFixture{exists: true}, changed: 1},
|
|
{name: "restore failure", status: "missing", apply: true, fixture: reconciliationFixture{exists: true, markErr: fault}, wantErr: true},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
f := tc.fixture
|
|
created := tc.created
|
|
if created.IsZero() {
|
|
created = old
|
|
}
|
|
f.records = []dto.Attachment{{ID: uuid.New(), Status: tc.status, CreatedAt: created, ObjectKey: "original/test"}}
|
|
result, err := Reconcile(t.Context(), &f, &f, uuid.Nil, time.Hour, tc.apply)
|
|
if (err != nil) != tc.wantErr || result.Changed != tc.changed {
|
|
t.Fatalf("result=%+v error=%v", result, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestOrphanSafety rejects deleting fresh, reserved and unknown-age objects.
|
|
func TestOrphanSafety(t *testing.T) {
|
|
fault := errors.New("dependency failure")
|
|
cases := []struct {
|
|
name string
|
|
fixture reconciliationFixture
|
|
fresh bool
|
|
apply bool
|
|
wantErr bool
|
|
removed int
|
|
}{
|
|
{name: "fresh", fresh: true, apply: true},
|
|
{name: "referenced", fixture: reconciliationFixture{referenced: true}, apply: true},
|
|
{name: "dry run"},
|
|
{name: "remove", apply: true, removed: 1},
|
|
{name: "list failure", fixture: reconciliationFixture{listErr: fault}, wantErr: true},
|
|
{name: "metadata failure", fixture: reconciliationFixture{referenceErr: fault}, wantErr: true},
|
|
{name: "remove failure", fixture: reconciliationFixture{removeErr: fault}, apply: true, wantErr: true},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
f := tc.fixture
|
|
modified := time.Now().Add(-2 * time.Hour)
|
|
if tc.fresh {
|
|
modified = time.Now()
|
|
}
|
|
f.entries = []ObjectInfo{{Key: "original/test", ModifiedAt: modified}}
|
|
result, err := ReconcileOrphans(t.Context(), &f, &f, "", time.Hour, tc.apply)
|
|
if (err != nil) != tc.wantErr || result.Removed != tc.removed {
|
|
t.Fatalf("result=%+v error=%v", result, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestGraceCannotBeDisabled rejects unsafe cleanup windows at both scan entrypoints.
|
|
func TestGraceCannotBeDisabled(t *testing.T) {
|
|
f := &reconciliationFixture{}
|
|
if _, err := Reconcile(t.Context(), f, f, uuid.Nil, time.Second, true); err == nil {
|
|
t.Fatal("unsafe metadata grace accepted")
|
|
}
|
|
if _, err := ReconcileOrphans(t.Context(), f, f, "", time.Second, true); err == nil {
|
|
t.Fatal("unsafe object grace accepted")
|
|
}
|
|
}
|