Files
mtusi2026lab4/internal/reconcile/reconciler_test.go
T

210 lines
7.0 KiB
Go

package reconcile
import (
"context"
"errors"
"testing"
"time"
"mtusi2026lab4/internal/adapter/objectstore"
"mtusi2026lab4/internal/domain"
)
type fakeMetadataStore struct {
records []AttachmentRecord
markError error
skipMark bool
}
func (store *fakeMetadataStore) ListAttachmentInventory(
context.Context,
) ([]AttachmentRecord, error) {
return append([]AttachmentRecord(nil), store.records...), nil
}
func (store *fakeMetadataStore) MarkAttachmentFailedIfUnreferenced(
_ context.Context,
attachmentID string,
expectedStatus domain.AttachmentStatus,
updatedBefore time.Time,
errorCode string,
updatedAt time.Time,
) (bool, error) {
if store.markError != nil {
return false, store.markError
}
if store.skipMark {
return false, nil
}
for index := range store.records {
record := &store.records[index]
if record.ID != attachmentID || record.Status != expectedStatus ||
record.UpdatedAt.After(updatedBefore) || record.Referenced {
continue
}
record.Status = domain.AttachmentStatusFailed
record.UpdatedAt = updatedAt
_ = errorCode
return true, nil
}
return false, nil
}
func TestReconcilerDoesNotDeleteAfterConcurrentMetadataChange(t *testing.T) {
old := time.Now().UTC().Add(-2 * time.Hour)
metadata := &fakeMetadataStore{
records: []AttachmentRecord{{
ID: "pending-race", ObjectKey: "attachments/pending-race",
Status: domain.AttachmentStatusPending, UpdatedAt: old,
}},
skipMark: true,
}
objects := &fakeObjectInventory{objects: map[string]objectstore.StoredObject{
"attachments/pending-race": {Key: "attachments/pending-race", LastModified: old},
}}
reconciler, err := New(metadata, objects)
if err != nil {
t.Fatal(err)
}
result, err := reconciler.Run(context.Background(), time.Hour, true)
if err != nil {
t.Fatal(err)
}
if result.DeletedObjects != 0 || result.FailedAttachments != 0 {
t.Fatalf("concurrent metadata change must suppress deletion: %+v", result)
}
if _, exists := objects.objects["attachments/pending-race"]; !exists {
t.Fatal("reconciler deleted an object after losing the metadata race")
}
if len(result.Issues) != 1 || result.Issues[0].Action != "skipped_concurrent_change" {
t.Fatalf("unexpected race result: %+v", result.Issues)
}
}
type fakeObjectInventory struct {
objects map[string]objectstore.StoredObject
deleteError error
}
func (store *fakeObjectInventory) List(
context.Context,
string,
) ([]objectstore.StoredObject, error) {
objects := make([]objectstore.StoredObject, 0, len(store.objects))
for _, object := range store.objects {
objects = append(objects, object)
}
return objects, nil
}
func (store *fakeObjectInventory) Delete(_ context.Context, key string) error {
if store.deleteError != nil {
return store.deleteError
}
delete(store.objects, key)
return nil
}
func TestReconcilerDryRunApplyAndRepeat(t *testing.T) {
now := time.Now().UTC()
old := now.Add(-2 * time.Hour)
fresh := now.Add(-5 * time.Minute)
metadata := &fakeMetadataStore{records: []AttachmentRecord{
{ID: "ready-present", ObjectKey: "attachments/ready-present", Status: domain.AttachmentStatusReady, UpdatedAt: old},
{ID: "ready-missing", ObjectKey: "attachments/ready-missing", Status: domain.AttachmentStatusReady, UpdatedAt: old},
{ID: "ready-used-missing", ObjectKey: "attachments/ready-used-missing", Status: domain.AttachmentStatusReady, UpdatedAt: old, Referenced: true},
{ID: "pending-old", ObjectKey: "attachments/pending-old", Status: domain.AttachmentStatusPending, UpdatedAt: old},
{ID: "failed-old", ObjectKey: "attachments/failed-old", Status: domain.AttachmentStatusFailed, UpdatedAt: old},
{ID: "pending-fresh", ObjectKey: "attachments/pending-fresh", Status: domain.AttachmentStatusPending, UpdatedAt: fresh},
}}
objects := &fakeObjectInventory{objects: map[string]objectstore.StoredObject{
"attachments/ready-present": {Key: "attachments/ready-present", LastModified: old},
"attachments/pending-old": {Key: "attachments/pending-old", LastModified: old},
"attachments/failed-old": {Key: "attachments/failed-old", LastModified: old},
"attachments/orphan-old": {Key: "attachments/orphan-old", LastModified: old},
"attachments/orphan-fresh": {Key: "attachments/orphan-fresh", LastModified: fresh},
}}
reconciler, err := New(metadata, objects)
if err != nil {
t.Fatal(err)
}
dryRun, err := reconciler.Run(context.Background(), time.Hour, false)
if err != nil {
t.Fatal(err)
}
if !dryRun.DryRun || len(dryRun.Issues) != 5 ||
dryRun.DeletedObjects != 0 || dryRun.FailedAttachments != 0 {
t.Fatalf("unexpected dry-run result: %+v", dryRun)
}
if len(objects.objects) != 5 {
t.Fatal("dry-run changed object storage")
}
for _, record := range metadata.records {
if record.ID == "ready-missing" && record.Status != domain.AttachmentStatusReady {
t.Fatal("dry-run changed metadata")
}
}
applied, err := reconciler.Run(context.Background(), time.Hour, true)
if err != nil {
t.Fatal(err)
}
if applied.DeletedObjects != 3 || applied.FailedAttachments != 2 {
t.Fatalf("unexpected apply result: %+v", applied)
}
if _, exists := objects.objects["attachments/orphan-old"]; exists {
t.Fatal("old orphan was not deleted")
}
if _, exists := objects.objects["attachments/orphan-fresh"]; !exists {
t.Fatal("fresh orphan must be protected by grace period")
}
if _, exists := objects.objects["attachments/ready-present"]; !exists {
t.Fatal("healthy object was deleted")
}
for _, issue := range applied.Issues {
if issue.AttachmentID == "ready-used-missing" &&
(!issue.Protected || issue.Action != "manual_repair_required") {
t.Fatalf("referenced attachment was not protected: %+v", issue)
}
}
repeated, err := reconciler.Run(context.Background(), time.Hour, true)
if err != nil {
t.Fatal(err)
}
if repeated.DeletedObjects != 0 || repeated.FailedAttachments != 0 {
t.Fatalf("repeat must be mutation-idempotent: %+v", repeated)
}
if len(repeated.Issues) != 1 || !repeated.Issues[0].Protected {
t.Fatalf("repeat should retain only protected manual issue: %+v", repeated)
}
}
func TestReconcilerReportsMutationFailures(t *testing.T) {
old := time.Now().UTC().Add(-2 * time.Hour)
metadata := &fakeMetadataStore{records: []AttachmentRecord{
{ID: "missing", ObjectKey: "attachments/missing", Status: domain.AttachmentStatusReady, UpdatedAt: old},
}}
metadata.markError = errors.New("injected database failure")
objects := &fakeObjectInventory{objects: make(map[string]objectstore.StoredObject)}
reconciler, err := New(metadata, objects)
if err != nil {
t.Fatal(err)
}
if _, err = reconciler.Run(context.Background(), time.Hour, true); err == nil {
t.Fatal("injected database mutation failure was hidden")
}
metadata.records = nil
metadata.markError = nil
objects.objects["attachments/orphan"] = objectstore.StoredObject{
Key: "attachments/orphan", LastModified: old,
}
objects.deleteError = errors.New("injected object-store failure")
if _, err = reconciler.Run(context.Background(), time.Hour, true); err == nil {
t.Fatal("injected object-store mutation failure was hidden")
}
}