lab7: безопасность, mTLS, OpenBao и PKI lifecycle
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
package attachment_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/mock"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"mtusi-messenger/internal/domain"
|
||||
"mtusi-messenger/internal/service/attachment"
|
||||
"mtusi-messenger/internal/service/attachment/mocks"
|
||||
)
|
||||
|
||||
func TestSweepAppliesStatusAwareRetention(t *testing.T) {
|
||||
repo := mocks.NewMockSweepRepository(t)
|
||||
locker := mocks.NewMockLockStore(t)
|
||||
store := newFakeStore()
|
||||
|
||||
stale := time.Now().UTC().Add(-2 * time.Hour)
|
||||
|
||||
presign := domain.Attachment{
|
||||
ID: uuid.New(), StagingKey: "staging/presign/chat/" + uuid.NewString(),
|
||||
Status: domain.AttachmentPending,
|
||||
}
|
||||
staleStaging := domain.StaleStaging{
|
||||
AttachmentID: uuid.New(), StagingKey: "staging/uploads/chat/ready", Status: domain.AttachmentReady,
|
||||
}
|
||||
activeStaging := "staging/uploads/chat/active"
|
||||
readyKey := "attachments/chat/ready/original"
|
||||
variantKey := "attachments/chat/ready/variants/v1/thumbnail.jpg"
|
||||
|
||||
for _, key := range []string{
|
||||
presign.StagingKey, staleStaging.StagingKey, activeStaging, readyKey, variantKey,
|
||||
"attachments/chat/orphan", "staging/orphan",
|
||||
} {
|
||||
store.objects[key] = storedObject{data: []byte("x"), lastModified: stale}
|
||||
}
|
||||
store.objects["staging/fresh"] = storedObject{data: []byte("x"), lastModified: time.Now().UTC()}
|
||||
|
||||
repo.EXPECT().ListPendingPresign(mock.Anything, mock.Anything, 100).
|
||||
Return([]domain.Attachment{presign}, nil).Once()
|
||||
repo.EXPECT().DeletePending(mock.Anything, presign.ID).Return(nil).Once()
|
||||
repo.EXPECT().ListStaleStaging(mock.Anything, mock.Anything, mock.Anything, 100).
|
||||
Return([]domain.StaleStaging{staleStaging}, nil).Once()
|
||||
repo.EXPECT().MarkStagingDeleted(mock.Anything, staleStaging.AttachmentID, staleStaging.StagingKey).
|
||||
Return(nil).Once()
|
||||
repo.EXPECT().ListReferencedKeys(mock.Anything, "", 1000).
|
||||
Return([]string{activeStaging, readyKey, variantKey}, nil).Once()
|
||||
locker.EXPECT().AcquireSweepLock(mock.Anything).Return(func() {}, true, nil).Once()
|
||||
|
||||
sweeper := attachment.NewSweeper(
|
||||
repo, store, locker,
|
||||
10*time.Minute, time.Hour, 24*time.Hour, time.Minute, slog.Default(),
|
||||
)
|
||||
|
||||
stats, err := sweeper.SweepOnce(t.Context())
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 1, stats.RowsDeleted)
|
||||
require.Equal(t, 1, stats.StagingCleared)
|
||||
require.Equal(t, 4, stats.ObjectsDeleted)
|
||||
|
||||
require.NotContains(t, store.objects, presign.StagingKey)
|
||||
require.NotContains(t, store.objects, staleStaging.StagingKey)
|
||||
require.NotContains(t, store.objects, "attachments/chat/orphan")
|
||||
require.NotContains(t, store.objects, "staging/orphan")
|
||||
require.Contains(t, store.objects, activeStaging)
|
||||
require.Contains(t, store.objects, readyKey)
|
||||
require.Contains(t, store.objects, variantKey)
|
||||
require.Contains(t, store.objects, "staging/fresh")
|
||||
}
|
||||
|
||||
func TestSweepSkipsWhenAnotherReplicaHoldsTheLock(t *testing.T) {
|
||||
repo := mocks.NewMockSweepRepository(t)
|
||||
locker := mocks.NewMockLockStore(t)
|
||||
sweeper := attachment.NewSweeper(
|
||||
repo, newFakeStore(), locker,
|
||||
time.Minute, time.Hour, 24*time.Hour, time.Minute, slog.Default(),
|
||||
)
|
||||
|
||||
locker.EXPECT().AcquireSweepLock(mock.Anything).Return(nil, false, nil).Once()
|
||||
|
||||
stats, err := sweeper.SweepOnce(t.Context())
|
||||
require.NoError(t, err)
|
||||
require.Zero(t, stats)
|
||||
}
|
||||
|
||||
func TestSweepAbortsObjectCollectionWhenKeysCannotBeRead(t *testing.T) {
|
||||
repo := mocks.NewMockSweepRepository(t)
|
||||
locker := mocks.NewMockLockStore(t)
|
||||
store := newFakeStore()
|
||||
|
||||
orphan := "attachments/chat/orphan"
|
||||
store.objects[orphan] = storedObject{data: []byte("x"), lastModified: time.Now().UTC().Add(-2 * time.Hour)}
|
||||
|
||||
repo.EXPECT().ListPendingPresign(mock.Anything, mock.Anything, 100).Return(nil, nil).Once()
|
||||
repo.EXPECT().ListStaleStaging(mock.Anything, mock.Anything, mock.Anything, 100).Return(nil, nil).Once()
|
||||
repo.EXPECT().ListReferencedKeys(mock.Anything, "", 1000).
|
||||
Return(nil, context.DeadlineExceeded).Once()
|
||||
locker.EXPECT().AcquireSweepLock(mock.Anything).Return(func() {}, true, nil).Once()
|
||||
|
||||
sweeper := attachment.NewSweeper(
|
||||
repo, store, locker,
|
||||
time.Minute, time.Hour, 24*time.Hour, time.Minute, slog.Default(),
|
||||
)
|
||||
|
||||
_, err := sweeper.SweepOnce(t.Context())
|
||||
require.ErrorIs(t, err, context.DeadlineExceeded)
|
||||
require.Contains(t, store.objects, orphan, "fail-closed: no deletion when the key set is unknown")
|
||||
}
|
||||
Reference in New Issue
Block a user