forked from mtusi2026/mtusi2026lab4
Prepare verified laboratory 4 implementation and evidence
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
package usecase
|
||||
|
||||
import (
|
||||
"backend/internal/domain/model"
|
||||
"backend/internal/domain/port"
|
||||
"backend/internal/domain/repository"
|
||||
"context"
|
||||
"github.com/google/uuid"
|
||||
"io"
|
||||
)
|
||||
|
||||
type Attachments struct {
|
||||
repo repository.AttachmentRepository
|
||||
messenger *Messenger
|
||||
objects port.Objects
|
||||
}
|
||||
|
||||
func NewAttachments(r repository.AttachmentRepository, m *Messenger, o port.Objects) *Attachments {
|
||||
return &Attachments{r, m, o}
|
||||
}
|
||||
func ObjectKey(id uuid.UUID) string { return "originals/" + id.String() }
|
||||
func (a *Attachments) Upload(ctx context.Context, caller, chat uuid.UUID, name, mime, sha string, size int64, body io.Reader) (model.Attachment, error) {
|
||||
if _, e := a.messenger.GetChat(ctx, caller, chat); e != nil {
|
||||
return model.Attachment{}, e
|
||||
}
|
||||
row, e := a.repo.ReserveAttachment(ctx, model.Attachment{ID: uuid.New(), ChatID: chat, OwnerID: caller, Filename: name, ContentType: mime, SHA256: sha, SizeBytes: size})
|
||||
if e != nil {
|
||||
return model.Attachment{}, e
|
||||
}
|
||||
if e = a.objects.Put(ctx, ObjectKey(row.ID), body, size, mime); e != nil {
|
||||
return model.Attachment{}, model.ErrUnavailable
|
||||
}
|
||||
ok, e := a.repo.SetAttachmentStatus(ctx, row.ID, "uploading", "ready")
|
||||
if e != nil {
|
||||
return model.Attachment{}, e
|
||||
}
|
||||
if !ok {
|
||||
return model.Attachment{}, model.ErrUnavailable
|
||||
}
|
||||
return a.repo.GetAttachment(ctx, row.ID)
|
||||
}
|
||||
func (a *Attachments) Get(ctx context.Context, caller, id uuid.UUID) (model.Attachment, error) {
|
||||
row, e := a.repo.GetAttachment(ctx, id)
|
||||
if e != nil {
|
||||
return model.Attachment{}, e
|
||||
}
|
||||
if _, e = a.messenger.GetChat(ctx, caller, row.ChatID); e != nil {
|
||||
return model.Attachment{}, e
|
||||
}
|
||||
if row.Status != "ready" {
|
||||
return model.Attachment{}, model.ErrUnavailable
|
||||
}
|
||||
return row, nil
|
||||
}
|
||||
func (a *Attachments) Content(ctx context.Context, caller, id uuid.UUID) (model.Attachment, io.ReadCloser, error) {
|
||||
row, e := a.Get(ctx, caller, id)
|
||||
if e != nil {
|
||||
return row, nil, e
|
||||
}
|
||||
info, e := a.objects.Stat(ctx, ObjectKey(id))
|
||||
if e != nil || info.Size != row.SizeBytes {
|
||||
return row, nil, model.ErrUnavailable
|
||||
}
|
||||
r, e := a.objects.Get(ctx, ObjectKey(id))
|
||||
if e != nil {
|
||||
return row, nil, model.ErrUnavailable
|
||||
}
|
||||
return row, r, nil
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package usecase
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"backend/internal/domain/model"
|
||||
"backend/internal/domain/port"
|
||||
)
|
||||
|
||||
type Auth struct {
|
||||
sessions port.Sessions
|
||||
passwords port.Passwords
|
||||
messenger *Messenger
|
||||
ttl time.Duration
|
||||
now func() time.Time
|
||||
}
|
||||
type Session struct {
|
||||
Token string
|
||||
ExpiresAt time.Time
|
||||
User model.User
|
||||
}
|
||||
|
||||
func NewAuth(s port.Sessions, p port.Passwords, m *Messenger, ttl time.Duration, now func() time.Time) *Auth {
|
||||
return &Auth{s, p, m, ttl, now}
|
||||
}
|
||||
func (a *Auth) Register(ctx context.Context, username, name, password string) (model.User, error) {
|
||||
if n := utf8.RuneCountInString(password); n < 12 || n > 128 || !utf8.ValidString(password) {
|
||||
return model.User{}, &model.ValidationError{Message: "password: 12–128 Unicode characters"}
|
||||
}
|
||||
draft, err := model.NewUserDraft(username, name)
|
||||
if err != nil {
|
||||
return model.User{}, err
|
||||
}
|
||||
hash, err := a.passwords.Hash(password)
|
||||
if err != nil {
|
||||
return model.User{}, err
|
||||
}
|
||||
draft.PasswordHash = hash
|
||||
return a.messenger.repo.CreateUser(ctx, draft)
|
||||
}
|
||||
func (a *Auth) Login(ctx context.Context, username, password string) (Session, error) {
|
||||
id, hash, err := a.sessions.Credentials(ctx, username)
|
||||
if err != nil {
|
||||
return Session{}, err
|
||||
}
|
||||
if !a.passwords.Verify(password, hash) {
|
||||
return Session{}, model.ErrUnauthorized
|
||||
}
|
||||
user, err := a.messenger.repo.GetUser(ctx, id)
|
||||
if err != nil {
|
||||
return Session{}, err
|
||||
}
|
||||
raw := make([]byte, 32)
|
||||
if _, err = rand.Read(raw); err != nil {
|
||||
return Session{}, err
|
||||
}
|
||||
token := base64.RawURLEncoding.EncodeToString(raw)
|
||||
expires := a.now().UTC().Add(a.ttl)
|
||||
if err = a.sessions.CreateSession(ctx, TokenHash(token), id, expires); err != nil {
|
||||
return Session{}, err
|
||||
}
|
||||
return Session{token, expires, user}, nil
|
||||
}
|
||||
func TokenHash(token string) string {
|
||||
sum := sha256.Sum256([]byte(token))
|
||||
return hex.EncodeToString(sum[:])
|
||||
}
|
||||
func (a *Auth) Authenticate(ctx context.Context, token string) (model.User, error) {
|
||||
if len(token) != 43 {
|
||||
return model.User{}, model.ErrUnauthorized
|
||||
}
|
||||
id, err := a.sessions.AuthenticateSession(ctx, TokenHash(token))
|
||||
if err != nil {
|
||||
return model.User{}, err
|
||||
}
|
||||
return a.messenger.repo.GetUser(ctx, id)
|
||||
}
|
||||
func (a *Auth) Logout(ctx context.Context, token string) error {
|
||||
return a.sessions.RevokeSession(ctx, TokenHash(token))
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package usecase
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"backend/internal/domain/model"
|
||||
"backend/internal/domain/repository"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const chatsCursorScope = "chats"
|
||||
|
||||
// Page — страница списка; NextCursor == nil, если следующей записи нет.
|
||||
type Page[T any] struct {
|
||||
Items []T
|
||||
NextCursor *string
|
||||
}
|
||||
|
||||
type Messenger struct {
|
||||
repo repository.Repository
|
||||
}
|
||||
|
||||
func NewMessenger(repo repository.Repository) *Messenger {
|
||||
return &Messenger{repo: repo}
|
||||
}
|
||||
|
||||
func (m *Messenger) CreateChat(ctx context.Context, caller uuid.UUID, title string, memberIDs []uuid.UUID) (model.Chat, error) {
|
||||
draft, err := model.NewChatDraft(caller, title, memberIDs)
|
||||
if err != nil {
|
||||
return model.Chat{}, err
|
||||
}
|
||||
return m.repo.CreateChat(ctx, draft)
|
||||
}
|
||||
|
||||
func (m *Messenger) ListChats(ctx context.Context, caller uuid.UUID, limit *int, cursor *string) (Page[model.Chat], error) {
|
||||
q, err := model.NewPageQuery(limit, cursor, chatsCursorScope)
|
||||
if err != nil {
|
||||
return Page[model.Chat]{}, err
|
||||
}
|
||||
chats, more, err := m.repo.ListChats(ctx, caller, q)
|
||||
if err != nil {
|
||||
return Page[model.Chat]{}, err
|
||||
}
|
||||
return newPage(chats, more, chatsCursorScope, model.Chat.Position), nil
|
||||
}
|
||||
|
||||
// GetChat отдаёт чат только участнику; постороннему — ErrNotFound, чтобы не
|
||||
// раскрывать существование чужого чата.
|
||||
func (m *Messenger) GetChat(ctx context.Context, caller, chatID uuid.UUID) (model.Chat, error) {
|
||||
chat, err := m.repo.GetChat(ctx, chatID)
|
||||
if err != nil {
|
||||
return model.Chat{}, err
|
||||
}
|
||||
if !chat.HasMember(caller) {
|
||||
return model.Chat{}, model.ErrNotFound
|
||||
}
|
||||
return chat, nil
|
||||
}
|
||||
|
||||
// SendMessage сначала проверяет доступ, затем тело: посторонний получает 404
|
||||
// даже на невалидное сообщение. Проверка членства и запись не атомарны, но
|
||||
// участники чата неизменяемы, а чаты не удаляются, поэтому гонки здесь нет.
|
||||
func (m *Messenger) SendMessage(ctx context.Context, caller, chatID uuid.UUID, text, key string, attachments []uuid.UUID) (model.Message, error) {
|
||||
if _, err := m.GetChat(ctx, caller, chatID); err != nil {
|
||||
return model.Message{}, err
|
||||
}
|
||||
draft, err := model.NewMessageDraft(chatID, caller, text, attachments)
|
||||
if err != nil {
|
||||
return model.Message{}, err
|
||||
}
|
||||
draft, err = model.WithKey(draft, key)
|
||||
if err != nil {
|
||||
return model.Message{}, err
|
||||
}
|
||||
return m.repo.CreateMessage(ctx, draft)
|
||||
}
|
||||
|
||||
func (m *Messenger) ListMessages(ctx context.Context, caller, chatID uuid.UUID, limit *int, cursor *string) (Page[model.Message], error) {
|
||||
if _, err := m.GetChat(ctx, caller, chatID); err != nil {
|
||||
return Page[model.Message]{}, err
|
||||
}
|
||||
scope := "messages:" + chatID.String()
|
||||
q, err := model.NewPageQuery(limit, cursor, scope)
|
||||
if err != nil {
|
||||
return Page[model.Message]{}, err
|
||||
}
|
||||
messages, more, err := m.repo.ListMessages(ctx, chatID, q)
|
||||
if err != nil {
|
||||
return Page[model.Message]{}, err
|
||||
}
|
||||
return newPage(messages, more, scope, model.Message.Position), nil
|
||||
}
|
||||
|
||||
func newPage[T any](items []T, more bool, scope string, position func(T) model.Position) Page[T] {
|
||||
page := Page[T]{Items: items}
|
||||
if more && len(items) > 0 {
|
||||
next := model.EncodeCursor(scope, position(items[len(items)-1]))
|
||||
page.NextCursor = &next
|
||||
}
|
||||
return page
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
package usecase
|
||||
|
||||
import (
|
||||
"backend/internal/domain/model"
|
||||
"backend/internal/domain/port"
|
||||
"backend/internal/domain/repository"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"github.com/google/uuid"
|
||||
"io"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ReconcileReport struct {
|
||||
Metadata, Objects, Orphans, Missing, Recovered, Fresh, Deleted, Changed int
|
||||
NextMetadata, NextObject string
|
||||
}
|
||||
type Reconciler struct {
|
||||
repo repository.AttachmentRepository
|
||||
objects port.Objects
|
||||
now func() time.Time
|
||||
}
|
||||
|
||||
func NewReconciler(repo repository.AttachmentRepository, objects port.Objects, now func() time.Time) *Reconciler {
|
||||
return &Reconciler{repo, objects, now}
|
||||
}
|
||||
func (r *Reconciler) Run(ctx context.Context, after uuid.UUID, objectAfter string, grace time.Duration, apply bool) (report ReconcileReport, runErr error) {
|
||||
report = ReconcileReport{}
|
||||
defer func() {
|
||||
if runErr != nil {
|
||||
report.NextMetadata = ""
|
||||
report.NextObject = ""
|
||||
}
|
||||
}()
|
||||
if grace < time.Minute {
|
||||
return report, &model.ValidationError{Message: "reconciliation grace must be at least 60 seconds"}
|
||||
}
|
||||
now := r.now().UTC()
|
||||
old := func(t time.Time) bool { age := now.Sub(t); return !t.IsZero() && age >= grace && age >= 0 }
|
||||
rows, more, e := r.repo.ReconcileAttachments(ctx, after)
|
||||
if e != nil {
|
||||
return report, e
|
||||
}
|
||||
for _, a := range rows {
|
||||
report.Metadata++
|
||||
if more {
|
||||
report.NextMetadata = a.ID.String()
|
||||
}
|
||||
if !old(a.CreatedAt) {
|
||||
report.Fresh++
|
||||
continue
|
||||
}
|
||||
info, e := r.objects.Stat(ctx, ObjectKey(a.ID))
|
||||
target := a.Status
|
||||
if errors.Is(e, model.ErrNotFound) {
|
||||
switch a.Status {
|
||||
case "ready":
|
||||
report.Missing++
|
||||
target = "missing"
|
||||
case "uploading":
|
||||
report.Missing++
|
||||
target = "abandoned"
|
||||
}
|
||||
} else if e != nil {
|
||||
return report, e
|
||||
} else if a.Status != "ready" {
|
||||
valid := false
|
||||
if info.Size == a.SizeBytes {
|
||||
body, err := r.objects.Get(ctx, ObjectKey(a.ID))
|
||||
if err != nil {
|
||||
return report, err
|
||||
}
|
||||
hash := sha256.New()
|
||||
n, err := io.Copy(hash, io.LimitReader(body, a.SizeBytes+1))
|
||||
_ = body.Close()
|
||||
if err != nil {
|
||||
return report, model.ErrUnavailable
|
||||
}
|
||||
valid = n == a.SizeBytes && hex.EncodeToString(hash.Sum(nil)) == a.SHA256
|
||||
}
|
||||
if valid {
|
||||
report.Recovered++
|
||||
target = "ready"
|
||||
} else {
|
||||
report.Missing++
|
||||
target = "missing"
|
||||
}
|
||||
} else if info.Size != a.SizeBytes {
|
||||
report.Missing++
|
||||
target = "missing"
|
||||
}
|
||||
if apply && target != a.Status {
|
||||
changed, e := r.repo.SetAttachmentStatus(ctx, a.ID, a.Status, target)
|
||||
if e != nil {
|
||||
return report, e
|
||||
}
|
||||
if changed {
|
||||
report.Changed++
|
||||
}
|
||||
}
|
||||
}
|
||||
objects, more, e := r.objects.List(ctx, objectAfter)
|
||||
if e != nil {
|
||||
return report, e
|
||||
}
|
||||
for _, o := range objects {
|
||||
report.Objects++
|
||||
if more {
|
||||
report.NextObject = o.Key
|
||||
}
|
||||
if !old(o.Modified) {
|
||||
report.Fresh++
|
||||
continue
|
||||
}
|
||||
raw, ok := strings.CutPrefix(o.Key, "originals/")
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
id, e := uuid.Parse(raw)
|
||||
if e != nil || ObjectKey(id) != o.Key {
|
||||
continue
|
||||
}
|
||||
if _, e = r.repo.GetAttachment(ctx, id); e == nil {
|
||||
continue
|
||||
} else if !errors.Is(e, model.ErrNotFound) {
|
||||
return report, e
|
||||
}
|
||||
report.Orphans++
|
||||
if !apply {
|
||||
continue
|
||||
}
|
||||
fresh, e := r.objects.Stat(ctx, o.Key)
|
||||
if errors.Is(e, model.ErrNotFound) {
|
||||
continue
|
||||
}
|
||||
if e != nil {
|
||||
return report, e
|
||||
}
|
||||
if fresh.ETag != o.ETag || fresh.Size != o.Size || !fresh.Modified.Equal(o.Modified) || !old(fresh.Modified) {
|
||||
report.Fresh++
|
||||
continue
|
||||
}
|
||||
if _, e = r.repo.GetAttachment(ctx, id); e == nil {
|
||||
continue
|
||||
} else if !errors.Is(e, model.ErrNotFound) {
|
||||
return report, e
|
||||
}
|
||||
// Object keys are random UUIDs, immutable and never reused by the upload API.
|
||||
// An absent metadata row cannot later adopt an old inventory key.
|
||||
if e = r.objects.Remove(ctx, o.Key); e != nil {
|
||||
return report, e
|
||||
}
|
||||
report.Deleted++
|
||||
}
|
||||
return report, nil
|
||||
}
|
||||
@@ -0,0 +1,184 @@
|
||||
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")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user