forked from mtusi2026/mtusi2026lab6
feat: add bounded observability and verified SLO incident alerts
This commit is contained in:
@@ -0,0 +1,225 @@
|
||||
package media
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/model/dto"
|
||||
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/service"
|
||||
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/store"
|
||||
)
|
||||
|
||||
type mediaFixture struct {
|
||||
attachment dto.Attachment
|
||||
body []byte
|
||||
authorizeErr error
|
||||
reserveErr error
|
||||
publishErr error
|
||||
getErr error
|
||||
objectErr error
|
||||
}
|
||||
|
||||
// ChatsGetByID models a single authorization outcome for one scenario.
|
||||
func (f *mediaFixture) ChatsGetByID(_ context.Context, _, chat string) (dto.ChatDTO, error) {
|
||||
id, _ := uuid.Parse(chat)
|
||||
return dto.ChatDTO{ID: id}, f.authorizeErr
|
||||
}
|
||||
|
||||
// Reserve records durable intent before a test object can be written.
|
||||
func (f *mediaFixture) Reserve(_ context.Context, a dto.Attachment) error {
|
||||
f.attachment = a
|
||||
return f.reserveErr
|
||||
}
|
||||
|
||||
// QueueAttachment models an acknowledged or uncertain database commit.
|
||||
func (f *mediaFixture) QueueAttachment(_ context.Context, _ uuid.UUID) (dto.Attachment, error) {
|
||||
f.attachment.Status = "queued"
|
||||
return f.attachment, f.publishErr
|
||||
}
|
||||
|
||||
// Attachment returns fixture metadata without bypassing service authorization.
|
||||
func (f *mediaFixture) Attachment(_ context.Context, _ uuid.UUID) (dto.Attachment, error) {
|
||||
return f.attachment, f.getErr
|
||||
}
|
||||
|
||||
// Put captures exact original bytes or injects an object failure.
|
||||
func (f *mediaFixture) Put(_ context.Context, _, _ string, r io.Reader, _ int64) error {
|
||||
if f.objectErr != nil {
|
||||
return f.objectErr
|
||||
}
|
||||
var err error
|
||||
f.body, err = io.ReadAll(r)
|
||||
return err
|
||||
}
|
||||
|
||||
// Get returns a separate read cursor over the captured original.
|
||||
func (f *mediaFixture) Get(_ context.Context, _ string) (io.ReadCloser, error) {
|
||||
return io.NopCloser(bytes.NewReader(f.body)), f.objectErr
|
||||
}
|
||||
|
||||
// mediaService constructs an independent service with explicit fixture dependencies.
|
||||
func mediaService(t *testing.T, f *mediaFixture) *Service {
|
||||
t.Helper()
|
||||
s, err := New(f, f, f)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// TestUploadPublication verifies exact bytes and durable publication ordering.
|
||||
func TestUploadPublication(t *testing.T) {
|
||||
f := &mediaFixture{}
|
||||
s := mediaService(t, f)
|
||||
original := imageBytes(t)
|
||||
a, err := s.Upload(t.Context(), uuid.NewString(), uuid.NewString(), "a.png", "image/png", original)
|
||||
if err != nil || a.Status != "queued" || a.SizeBytes != int64(len(original)) || len(a.SHA256) != 64 || !bytes.Equal(f.body, original) {
|
||||
t.Fatalf("upload result: %+v %v", a, err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestUploadRejectsUnauthorized verifies authorization precedes object creation.
|
||||
func TestUploadRejectsUnauthorized(t *testing.T) {
|
||||
f := &mediaFixture{authorizeErr: service.ErrNotFound}
|
||||
s := mediaService(t, f)
|
||||
_, err := s.Upload(t.Context(), uuid.NewString(), uuid.NewString(), "a.png", "image/png", imageBytes(t))
|
||||
if !errors.Is(err, service.ErrNotFound) || f.body != nil {
|
||||
t.Fatalf("unauthorized upload: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestUploadDependencyFailures verifies failed operations are not acknowledged as ready.
|
||||
func TestUploadDependencyFailures(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
fixture mediaFixture
|
||||
}{
|
||||
{name: "reserve", fixture: mediaFixture{reserveErr: service.ErrUnavailable}},
|
||||
{name: "put", fixture: mediaFixture{objectErr: service.ErrUnavailable}},
|
||||
{name: "commit acknowledgement", fixture: mediaFixture{publishErr: service.ErrUnavailable}},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
f := tc.fixture
|
||||
s := mediaService(t, &f)
|
||||
_, err := s.Upload(t.Context(), uuid.NewString(), uuid.NewString(), "a.png", "image/png", imageBytes(t))
|
||||
if !errors.Is(err, service.ErrUnavailable) {
|
||||
t.Fatalf("error=%v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestUploadConcurrencyBound verifies decoding cannot create an unbounded queue.
|
||||
func TestUploadConcurrencyBound(t *testing.T) {
|
||||
s := mediaService(t, &mediaFixture{})
|
||||
s.slots <- struct{}{}
|
||||
s.slots <- struct{}{}
|
||||
_, err := s.Upload(t.Context(), uuid.NewString(), uuid.NewString(), "a.png", "image/png", imageBytes(t))
|
||||
if !errors.Is(err, service.ErrUnavailable) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestGetAttachment checks metadata authorization and intermediate state handling.
|
||||
func TestGetAttachment(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
fixture mediaFixture
|
||||
id string
|
||||
want error
|
||||
}{
|
||||
{name: "invalid id", id: "bad", want: service.ErrInvalid},
|
||||
{name: "missing", id: uuid.NewString(), fixture: mediaFixture{getErr: store.ErrNotFound}, want: service.ErrNotFound},
|
||||
{name: "database unavailable", id: uuid.NewString(), fixture: mediaFixture{getErr: service.ErrUnavailable}, want: service.ErrUnavailable},
|
||||
{name: "foreign", id: uuid.NewString(), fixture: mediaFixture{authorizeErr: service.ErrNotFound}, want: service.ErrNotFound},
|
||||
{name: "unfinished", id: uuid.NewString(), fixture: mediaFixture{attachment: dto.Attachment{Status: "queued"}}},
|
||||
{name: "ready", id: uuid.NewString(), fixture: mediaFixture{attachment: dto.Attachment{Status: "ready"}}},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
f := tc.fixture
|
||||
s := mediaService(t, &f)
|
||||
_, err := s.Get(t.Context(), uuid.NewString(), tc.id)
|
||||
if !errors.Is(err, tc.want) {
|
||||
t.Fatalf("got %v want %v", err, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestDownloadOriginal checks byte identity through an independently authorized read.
|
||||
func TestDownloadOriginal(t *testing.T) {
|
||||
f := &mediaFixture{attachment: dto.Attachment{Status: "ready"}, body: imageBytes(t)}
|
||||
s := mediaService(t, f)
|
||||
_, reader, err := s.Download(t.Context(), uuid.NewString(), uuid.NewString(), "original")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := io.ReadAll(reader)
|
||||
if closeErr := reader.Close(); closeErr != nil {
|
||||
t.Fatal(closeErr)
|
||||
}
|
||||
if err != nil || !bytes.Equal(got, f.body) {
|
||||
t.Fatalf("download: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestMediaRequiresDependencies rejects incomplete service composition.
|
||||
func TestMediaRequiresDependencies(t *testing.T) {
|
||||
if _, err := New(nil, nil, nil); err == nil {
|
||||
t.Fatal("missing dependencies accepted")
|
||||
}
|
||||
}
|
||||
|
||||
// TestUploadRejectsInvalidOriginal never reserves invalid file metadata.
|
||||
func TestUploadRejectsInvalidOriginal(t *testing.T) {
|
||||
f := &mediaFixture{}
|
||||
s := mediaService(t, f)
|
||||
_, err := s.Upload(t.Context(), uuid.NewString(), uuid.NewString(), "a.png", "image/png", []byte("invalid"))
|
||||
if !errors.Is(err, service.ErrMedia) || f.attachment.ID != uuid.Nil {
|
||||
t.Fatalf("invalid bytes persisted: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestUploadRejectsMalformedOwner does not persist an invalid authenticated identity.
|
||||
func TestUploadRejectsMalformedOwner(t *testing.T) {
|
||||
f := &mediaFixture{}
|
||||
s := mediaService(t, f)
|
||||
_, err := s.Upload(t.Context(), "bad", uuid.NewString(), "a.png", "image/png", imageBytes(t))
|
||||
if !errors.Is(err, service.ErrUnauthorized) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestDownloadFailures keeps authorization, variant and dependency failures distinct.
|
||||
func TestDownloadFailures(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
fixture mediaFixture
|
||||
variant string
|
||||
want error
|
||||
}{
|
||||
{name: "foreign", fixture: mediaFixture{authorizeErr: service.ErrNotFound}, want: service.ErrNotFound},
|
||||
{name: "variant missing", fixture: mediaFixture{attachment: dto.Attachment{Status: "ready"}}, variant: "thumbnail", want: service.ErrNotFound},
|
||||
{name: "S3 unavailable", fixture: mediaFixture{attachment: dto.Attachment{Status: "ready"}, objectErr: service.ErrUnavailable}, want: service.ErrUnavailable},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
f := tc.fixture
|
||||
s := mediaService(t, &f)
|
||||
_, reader, err := s.Download(t.Context(), uuid.NewString(), uuid.NewString(), tc.variant)
|
||||
if reader != nil {
|
||||
_ = reader.Close()
|
||||
}
|
||||
if !errors.Is(err, tc.want) {
|
||||
t.Fatalf("got %v want %v", err, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user