forked from mtusi2026/mtusi2026lab6
341 lines
12 KiB
Go
341 lines
12 KiB
Go
package reconcile
|
|
|
|
import (
|
|
"context"
|
|
"sort"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"gitea.privcloud.tech/dizi/mtusi2026lab6/internal/model"
|
|
)
|
|
|
|
func TestReconcilerLifecycleAndRepeat(t *testing.T) {
|
|
now := time.Date(2026, 10, 9, 12, 0, 0, 0, time.UTC)
|
|
old := now.Add(-48 * time.Hour)
|
|
fresh := now.Add(-10 * time.Minute)
|
|
metadata := &metadataStub{items: map[string]model.Attachment{
|
|
"attachments/a/original": attachment("a", "attachments/a/original", model.AttachmentReady, old, false),
|
|
"attachments/b/original": attachment("b", "attachments/b/original", model.AttachmentReady, old, true),
|
|
"attachments/c/original": attachment("c", "attachments/c/original", model.AttachmentPending, old, false),
|
|
"attachments/d/original": attachment("d", "attachments/d/original", model.AttachmentReady, fresh, false),
|
|
"attachments/missing/original": attachment("missing", "attachments/missing/original", model.AttachmentReady, old, false),
|
|
"staging/pending/original": attachment("pending", "staging/pending/original", model.AttachmentPending, old, false),
|
|
}}
|
|
objects := &objectStub{items: map[string]model.StoredObject{
|
|
"attachments/a/original": {Key: "attachments/a/original", LastModified: old},
|
|
"attachments/b/original": {Key: "attachments/b/original", LastModified: old},
|
|
"attachments/c/original": {Key: "attachments/c/original", LastModified: old},
|
|
"attachments/d/original": {Key: "attachments/d/original", LastModified: fresh},
|
|
"attachments/orphan/original": {Key: "attachments/orphan/original", LastModified: old},
|
|
"staging/orphan/original": {Key: "staging/orphan/original", LastModified: old},
|
|
"staging/pending/original": {Key: "staging/pending/original", LastModified: old},
|
|
}}
|
|
reconciler, err := New(metadata, objects, Config{
|
|
Prefix: "attachments/", PageSize: 2, GraceTime: time.Hour,
|
|
OrphanPrefixes: []string{"staging/"},
|
|
UnreferencedRetention: 24 * time.Hour, Apply: true, Now: func() time.Time { return now },
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
report, err := reconciler.Run(context.Background())
|
|
require.NoError(t, err)
|
|
require.Equal(t, 2, report.OldPendingAttachments)
|
|
require.Equal(t, 1, report.OldUnreferencedAttachments)
|
|
require.Equal(t, 1, report.FreshUnreferencedAttachments)
|
|
require.Equal(t, 2, report.DeletedOrphanObjects)
|
|
require.Equal(t, 3, report.DeletedAttachmentObjects)
|
|
require.Equal(t, 4, report.DeletedUnreferencedRows)
|
|
require.Contains(t, metadata.items, "attachments/b/original")
|
|
require.Contains(t, metadata.items, "attachments/d/original")
|
|
require.NotContains(t, metadata.items, "attachments/a/original")
|
|
require.NotContains(t, metadata.items, "attachments/c/original")
|
|
require.NotContains(t, metadata.items, "attachments/missing/original")
|
|
require.NotContains(t, metadata.items, "staging/pending/original")
|
|
require.NotContains(t, objects.items, "attachments/orphan/original")
|
|
require.NotContains(t, objects.items, "staging/orphan/original")
|
|
require.NotContains(t, objects.items, "staging/pending/original")
|
|
|
|
repeated, err := reconciler.Run(context.Background())
|
|
require.NoError(t, err)
|
|
require.Zero(t, repeated.DeletedOrphanObjects)
|
|
require.Zero(t, repeated.DeletedAttachmentObjects)
|
|
require.Zero(t, repeated.DeletedUnreferencedRows)
|
|
}
|
|
|
|
func TestReconcilerRechecksOrphanBeforeDelete(t *testing.T) {
|
|
now := time.Now().UTC()
|
|
metadata := &metadataStub{items: map[string]model.Attachment{}, appeared: map[string]bool{"attachments/a/original": true}}
|
|
objects := &objectStub{items: map[string]model.StoredObject{
|
|
"attachments/a/original": {Key: "attachments/a/original", LastModified: now.Add(-time.Hour)},
|
|
}}
|
|
reconciler, err := New(metadata, objects, Config{
|
|
Prefix: "attachments/", PageSize: 1, GraceTime: time.Minute,
|
|
UnreferencedRetention: time.Hour, Apply: true, Now: func() time.Time { return now },
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
report, err := reconciler.Run(context.Background())
|
|
require.NoError(t, err)
|
|
require.Zero(t, report.DeletedOrphanObjects)
|
|
require.Contains(t, objects.items, "attachments/a/original")
|
|
}
|
|
|
|
func TestReconcilerDeletingAttachmentRespectsApplyMode(t *testing.T) {
|
|
now := time.Now().UTC()
|
|
key := "attachments/a/original"
|
|
tests := []struct {
|
|
name string
|
|
apply bool
|
|
objectPresent bool
|
|
}{
|
|
{name: "dry run with object", objectPresent: true},
|
|
{name: "dry run without object"},
|
|
{name: "apply with object", apply: true, objectPresent: true},
|
|
{name: "apply without object", apply: true},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
metadata := &metadataStub{items: map[string]model.Attachment{
|
|
key: attachment("a", key, model.AttachmentDeleting, now.Add(-time.Hour), false),
|
|
}}
|
|
objects := &objectStub{items: make(map[string]model.StoredObject)}
|
|
if test.objectPresent {
|
|
objects.items[key] = model.StoredObject{Key: key, LastModified: now.Add(-time.Hour)}
|
|
}
|
|
reconciler, err := New(metadata, objects, Config{
|
|
Prefix: "attachments/", PageSize: 1, GraceTime: time.Minute,
|
|
UnreferencedRetention: time.Hour, Apply: test.apply, Now: func() time.Time { return now },
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
report, err := reconciler.Run(context.Background())
|
|
require.NoError(t, err)
|
|
require.Equal(t, 1, report.DeletingAttachments)
|
|
var expectedDeletedRows, expectedDeletedObjects int
|
|
if test.apply {
|
|
expectedDeletedRows = 1
|
|
}
|
|
if test.apply && test.objectPresent {
|
|
expectedDeletedObjects = 1
|
|
}
|
|
require.Equal(t, expectedDeletedRows, report.DeletedUnreferencedRows)
|
|
require.Equal(t, expectedDeletedObjects, report.DeletedAttachmentObjects)
|
|
_, metadataExists := metadata.items[key]
|
|
require.Equal(t, !test.apply, metadataExists)
|
|
_, objectExists := objects.items[key]
|
|
require.Equal(t, test.objectPresent && !test.apply, objectExists)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestReconcilerDoesNotReportTrackedStagingObjectAsOrphan(t *testing.T) {
|
|
now := time.Now().UTC()
|
|
key := "staging/a/original"
|
|
metadata := &metadataStub{items: map[string]model.Attachment{
|
|
key: attachment("a", key, model.AttachmentQueued, now.Add(-time.Hour), false),
|
|
}}
|
|
objects := &objectStub{items: map[string]model.StoredObject{
|
|
key: {Key: key, LastModified: now.Add(-time.Hour)},
|
|
}}
|
|
reconciler, err := New(metadata, objects, Config{
|
|
Prefix: "attachments/", OrphanPrefixes: []string{"staging/"}, PageSize: 1,
|
|
GraceTime: time.Minute, UnreferencedRetention: time.Hour, Now: func() time.Time { return now },
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
report, err := reconciler.Run(context.Background())
|
|
require.NoError(t, err)
|
|
require.Zero(t, report.OldOrphanObjects)
|
|
require.Zero(t, report.FreshOrphanObjects)
|
|
require.Contains(t, objects.items, key)
|
|
}
|
|
|
|
func TestReconcilerPreservesAttachmentDuringReprocessing(t *testing.T) {
|
|
now := time.Date(2026, 10, 9, 12, 0, 0, 0, time.UTC)
|
|
for _, status := range []string{
|
|
model.AttachmentQueued,
|
|
model.AttachmentProcessing,
|
|
model.AttachmentFailed,
|
|
} {
|
|
t.Run(status, func(t *testing.T) {
|
|
original := attachment("a", "attachments/a/v1/original.png", status, now.Add(-48*time.Hour), false)
|
|
original.PipelineVersion = 2
|
|
missing := attachment("a", "attachments/a/v1/optimized.webp", status, now.Add(-48*time.Hour), false)
|
|
missing.PipelineVersion = 2
|
|
metadata := &metadataStub{items: map[string]model.Attachment{
|
|
original.ObjectKey: original,
|
|
missing.ObjectKey: missing,
|
|
}}
|
|
objects := &objectStub{items: map[string]model.StoredObject{
|
|
original.ObjectKey: {Key: original.ObjectKey, LastModified: now.Add(-48 * time.Hour)},
|
|
}}
|
|
reconciler, err := New(metadata, objects, Config{
|
|
Prefix: "attachments/", PageSize: 10, GraceTime: time.Hour,
|
|
UnreferencedRetention: 24 * time.Hour, Apply: true, Now: func() time.Time { return now },
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
report, err := reconciler.Run(context.Background())
|
|
require.NoError(t, err)
|
|
require.Zero(t, report.DeletedAttachmentObjects)
|
|
require.Zero(t, report.DeletedUnreferencedRows)
|
|
require.Contains(t, metadata.items, original.ObjectKey)
|
|
require.Contains(t, metadata.items, missing.ObjectKey)
|
|
require.Contains(t, objects.items, original.ObjectKey)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestReconcilerDeletesAllAttachmentVariantsOnce(t *testing.T) {
|
|
now := time.Date(2026, 10, 9, 12, 0, 0, 0, time.UTC)
|
|
old := now.Add(-48 * time.Hour)
|
|
keys := []string{
|
|
"attachments/a/v1/crop.jpg",
|
|
"attachments/a/v1/optimized.webp",
|
|
"attachments/a/v1/original.png",
|
|
"attachments/a/v1/thumbnail.jpg",
|
|
}
|
|
metadata := &metadataStub{items: make(map[string]model.Attachment, len(keys))}
|
|
objects := &objectStub{items: make(map[string]model.StoredObject, len(keys))}
|
|
for _, key := range keys {
|
|
metadata.items[key] = attachment("a", key, model.AttachmentReady, old, false)
|
|
objects.items[key] = model.StoredObject{Key: key, LastModified: old}
|
|
}
|
|
reconciler, err := New(metadata, objects, Config{
|
|
Prefix: "attachments/", PageSize: 10, GraceTime: time.Hour,
|
|
UnreferencedRetention: 24 * time.Hour, Apply: true, Now: func() time.Time { return now },
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
report, err := reconciler.Run(context.Background())
|
|
require.NoError(t, err)
|
|
require.Equal(t, len(keys), report.DeletedAttachmentObjects)
|
|
require.Equal(t, 1, report.DeletedUnreferencedRows)
|
|
require.Empty(t, metadata.items)
|
|
require.Empty(t, objects.items)
|
|
|
|
repeated, err := reconciler.Run(context.Background())
|
|
require.NoError(t, err)
|
|
require.Zero(t, repeated.DeletedAttachmentObjects)
|
|
require.Zero(t, repeated.DeletedUnreferencedRows)
|
|
}
|
|
|
|
func TestNewRejectsInvalidRetention(t *testing.T) {
|
|
_, err := New(&metadataStub{}, &objectStub{}, Config{PageSize: 1})
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func attachment(id, key, status string, createdAt time.Time, referenced bool) model.Attachment {
|
|
readyAt := createdAt
|
|
return model.Attachment{
|
|
ID: id, ObjectKey: key, Status: status, CreatedAt: createdAt,
|
|
ReadyAt: &readyAt, Referenced: referenced,
|
|
}
|
|
}
|
|
|
|
type metadataStub struct {
|
|
items map[string]model.Attachment
|
|
appeared map[string]bool
|
|
}
|
|
|
|
func (s *metadataStub) AttachmentObjectKeys(_ context.Context, attachmentID string) ([]string, error) {
|
|
var keys []string
|
|
for key, item := range s.items {
|
|
if item.ID == attachmentID {
|
|
keys = append(keys, key)
|
|
}
|
|
}
|
|
sort.Strings(keys)
|
|
return keys, nil
|
|
}
|
|
|
|
func (s *metadataStub) ListAttachments(_ context.Context, after string, limit int) ([]model.Attachment, string, error) {
|
|
keys := make([]string, 0, len(s.items))
|
|
for key := range s.items {
|
|
if key > after {
|
|
keys = append(keys, key)
|
|
}
|
|
}
|
|
sort.Strings(keys)
|
|
var next string
|
|
if len(keys) > limit {
|
|
keys = keys[:limit]
|
|
next = keys[len(keys)-1]
|
|
}
|
|
items := make([]model.Attachment, 0, len(keys))
|
|
for _, key := range keys {
|
|
items = append(items, s.items[key])
|
|
}
|
|
return items, next, nil
|
|
}
|
|
|
|
func (s *metadataStub) AttachmentExistsByObjectKey(_ context.Context, key string) (bool, error) {
|
|
if s.appeared[key] {
|
|
return true, nil
|
|
}
|
|
_, exists := s.items[key]
|
|
return exists, nil
|
|
}
|
|
|
|
func (s *metadataStub) ClaimAttachmentForDeletion(_ context.Context, candidate model.Attachment, cutoff time.Time) (bool, error) {
|
|
claim := false
|
|
for key, item := range s.items {
|
|
if item.ID != candidate.ID || item.Status != candidate.Status || item.Referenced {
|
|
continue
|
|
}
|
|
age := item.CreatedAt
|
|
if item.Status == model.AttachmentReady && item.ReadyAt != nil {
|
|
age = *item.ReadyAt
|
|
}
|
|
if age.After(cutoff) {
|
|
return false, nil
|
|
}
|
|
item.Status = model.AttachmentDeleting
|
|
s.items[key] = item
|
|
claim = true
|
|
}
|
|
return claim, nil
|
|
}
|
|
|
|
func (s *metadataStub) DeleteAttachmentIfDeleting(_ context.Context, id string) (bool, error) {
|
|
deleted := false
|
|
for key, item := range s.items {
|
|
if item.ID == id && item.Status == model.AttachmentDeleting && !item.Referenced {
|
|
delete(s.items, key)
|
|
deleted = true
|
|
}
|
|
}
|
|
return deleted, nil
|
|
}
|
|
|
|
type objectStub struct {
|
|
items map[string]model.StoredObject
|
|
}
|
|
|
|
func (s *objectStub) List(_ context.Context, prefix, after string, limit int) ([]model.StoredObject, string, error) {
|
|
keys := make([]string, 0, len(s.items))
|
|
for key := range s.items {
|
|
if key > after && len(key) >= len(prefix) && key[:len(prefix)] == prefix {
|
|
keys = append(keys, key)
|
|
}
|
|
}
|
|
sort.Strings(keys)
|
|
var next string
|
|
if len(keys) > limit {
|
|
keys = keys[:limit]
|
|
next = keys[len(keys)-1]
|
|
}
|
|
items := make([]model.StoredObject, 0, len(keys))
|
|
for _, key := range keys {
|
|
items = append(items, s.items[key])
|
|
}
|
|
return items, next, nil
|
|
}
|
|
|
|
func (s *objectStub) Delete(_ context.Context, key string) error {
|
|
delete(s.items, key)
|
|
return nil
|
|
}
|