Compare commits

3 Commits
12 changed files with 476 additions and 75 deletions
+1 -1
View File
@@ -134,7 +134,7 @@ services:
resources:
limits:
cpus: "0.5"
memory: 256M
memory: 512M
restart: unless-stopped
load_balancer:
+36
View File
@@ -0,0 +1,36 @@
> make contract-go
go test -tags=contract ./student-tests/... -v -args -base-url=http://localhost:8080
=== RUN TestAttachmentJPEG
--- PASS: TestAttachmentJPEG (0.23s)
=== RUN TestAttachmentTooManyPixels
--- PASS: TestAttachmentTooManyPixels (1.83s)
=== RUN TestAttachmentUnsafeFilename
--- PASS: TestAttachmentUnsafeFilename (0.12s)
=== RUN TestAttachmentTooLarge
--- PASS: TestAttachmentTooLarge (0.22s)
=== RUN TestAttachmentCorruptedImage
--- PASS: TestAttachmentCorruptedImage (0.14s)
=== RUN TestAttachmentMimeSubstitution
--- PASS: TestAttachmentMimeSubstitution (0.12s)
=== RUN TestAttachmentForeignID
--- PASS: TestAttachmentForeignID (0.26s)
=== RUN TestAttachmentFromAnotherChat
--- PASS: TestAttachmentFromAnotherChat (0.41s)
=== RUN TestCreateChatRollback
--- PASS: TestCreateChatRollback (0.16s)
=== RUN TestInvalidJSON
--- PASS: TestInvalidJSON (0.00s)
=== RUN TestForeignChat
--- PASS: TestForeignChat (0.57s)
=== RUN TestEmptyMessage
--- PASS: TestEmptyMessage (0.21s)
=== RUN TestDuplicateUsername
--- PASS: TestDuplicateUsername (0.28s)
=== RUN TestPagination
--- PASS: TestPagination (0.36s)
=== RUN TestLoginWrongPassword
--- PASS: TestLoginWrongPassword (0.34s)
=== RUN TestLogout
--- PASS: TestLogout (0.17s)
PASS
ok mtusi2026-messenger/student-tests 5.442s
+27
View File
@@ -0,0 +1,27 @@
> make load-go LOAD_WORKERS=10 LOAD_DURATION=30s
go test -count=1 -tags=load ./student-tests/... -v -args -base-url=http://localhost:8080 -workers=10 -duration=30s
=== RUN TestConcurrentUploads
attachment_load_test.go:141: workers=10 duration=30.0s elapsed=30.3s
attachment_load_test.go:142: total uploads=5096 transport_errors=0
attachment_load_test.go:144: HTTP 201: 5096
attachment_load_test.go:148: successful RPS: 168.33
attachment_load_test.go:152: latency p50=55.701325ms p95=99.180675ms p99=137.654783ms
attachment_load_test.go:176: memory per container (idle -> peak):
attachment_load_test.go:178: mtusi2026lab4-api-1: 13.0MiB -> 15.5MiB
attachment_load_test.go:178: mtusi2026lab4-api-2: 13.0MiB -> 16.4MiB
--- PASS: TestConcurrentUploads (32.60s)
=== RUN TestLoad
load_test.go:109: duration: 30.001s
load_test.go:110: workers: 10
load_test.go:111: rate per worker: 20 req/s
load_test.go:112: target rate: 200 req/s
load_test.go:113: total requests: 5990
load_test.go:116: HTTP 201: 5990
load_test.go:119: request errors: 0
load_test.go:123: successful RPS: 199.66
load_test.go:132: successful latency p50: 8.402766ms
load_test.go:133: successful latency p95: 11.110029ms
load_test.go:134: successful latency p99: 14.64398ms
--- PASS: TestLoad (30.37s)
PASS
ok mtusi2026-messenger/student-tests 62.967s
+3
View File
@@ -0,0 +1,3 @@
> make test
python3 tests/smoke.py --base-url "http://localhost:8080"
PASS: lab4 public API scenario; infrastructure and grading evidence remain separate.
+52
View File
@@ -0,0 +1,52 @@
> make reconcile-test-go
docker compose --env-file .env build reconcile
[+] Building 13.8s (18/18) FINISHED
=> [internal] load local bake definitions 0.0s
=> => reading from stdin 600B 0.0s
=> [internal] load build definition from Dockerfile.reconcile 0.0s
=> => transferring dockerfile: 342B 0.0s
=> [internal] load metadata for gcr.io/distroless/static-debian12:nonroot 1.4s
=> [internal] load metadata for docker.io/library/golang:1.27.1 1.3s
=> [auth] library/golang:pull token for registry-1.docker.io 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 203B 0.0s
=> [build 1/6] FROM docker.io/library/golang:1.27.1@sha256:162be5298a40ed317005c8339c6de4d10d3eef336d66dc8e9259b03ab9d3a6d2 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 11.25kB 0.0s
=> [stage-1 1/3] FROM gcr.io/distroless/static-debian12:nonroot@sha256:afa5c872c891853ca7fcf1f12c3edb23f7eeef36189728842dd51042ff57f7ab 0.0s
=> CACHED [build 2/6] WORKDIR /src 0.0s
=> CACHED [build 3/6] COPY go.mod go.sum ./ 0.0s
=> CACHED [build 4/6] RUN go mod download 0.0s
=> [build 5/6] COPY . . 0.1s
=> [build 6/6] RUN CGO_ENABLED=0 GOOS=linux go build -o /out/reconcile ./cmd/reconcile 11.9s
=> CACHED [stage-1 2/3] WORKDIR /app 0.0s
=> CACHED [stage-1 3/3] COPY --from=build /out/reconcile . 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:0a06bc3c0229755a92090161a0913af4d053556c4934087af6539b95c1d9b1cd 0.0s
=> => naming to docker.io/library/mtusi-messenger-reconcile:lab4 0.0s
=> resolving provenance for metadata file 0.0s
[+] build 1/1
✔ Image mtusi-messenger-reconcile:lab4 Built 13.9s
go test -count=1 -tags=reconcile ./student-tests/... -v -args -base-url=http://localhost:8080
=== RUN TestReconcileRepair
reconcile_test.go:85: dry_run=true objects=14606 rows=14612 list_pages=15 duration_ms=1813 orphan=1 missing=1 stale=0 skipped_fresh=0 skipped_referenced=0 deleted_objects=0 deleted_rows=0 marked_failed=0 errors=[]
reconcile_test.go:99: dry_run=false objects=14606 rows=14612 list_pages=15 duration_ms=1557 orphan=1 missing=1 stale=0 skipped_fresh=0 skipped_referenced=0 deleted_objects=1 deleted_rows=0 marked_failed=1 errors=[]
reconcile_test.go:109: dry_run=false objects=14605 rows=14612 list_pages=15 duration_ms=1836 orphan=0 missing=0 stale=0 skipped_fresh=0 skipped_referenced=0 deleted_objects=0 deleted_rows=0 marked_failed=0 errors=[]
reconcile_test.go:116: PASS: dry-run preserved data; orphan deleted; missing marked failed; repeat made no changes
--- PASS: TestReconcileRepair (8.81s)
=== RUN TestReconcileSafety
reconcile_test.go:137: dry_run=true objects=14607 rows=14614 list_pages=15 duration_ms=1630 orphan=0 missing=0 stale=0 skipped_fresh=1 skipped_referenced=1 deleted_objects=0 deleted_rows=0 marked_failed=0 errors=[]
reconcile_test.go:137: dry_run=false objects=14607 rows=14614 list_pages=15 duration_ms=1560 orphan=0 missing=0 stale=0 skipped_fresh=1 skipped_referenced=1 deleted_objects=0 deleted_rows=0 marked_failed=0 errors=[]
reconcile_test.go:161: PASS: fresh and old referenced uploads preserved, originals unchanged
--- PASS: TestReconcileSafety (5.03s)
=== RUN TestReconcileCost
reconcile_test.go:166: scan 1/3
reconcile_test.go:167: dry_run=true objects=14607 rows=14614 list_pages=15 duration_ms=1609 orphan=0 missing=0 stale=0 skipped_fresh=0 skipped_referenced=0 deleted_objects=0 deleted_rows=0 marked_failed=0 errors=[]
reconcile_test.go:166: scan 2/3
reconcile_test.go:167: dry_run=true objects=14607 rows=14614 list_pages=15 duration_ms=1721 orphan=0 missing=0 stale=0 skipped_fresh=0 skipped_referenced=0 deleted_objects=0 deleted_rows=0 marked_failed=0 errors=[]
reconcile_test.go:166: scan 3/3
reconcile_test.go:167: dry_run=true objects=14607 rows=14614 list_pages=15 duration_ms=1587 orphan=0 missing=0 stale=0 skipped_fresh=0 skipped_referenced=0 deleted_objects=0 deleted_rows=0 marked_failed=0 errors=[]
--- PASS: TestReconcileCost (6.92s)
PASS
ok mtusi2026-messenger/student-tests 20.784s
+7
View File
@@ -0,0 +1,7 @@
> OBJECT_KEY=$(docker compose exec -T postgres psql -U messenger -d messenger -Atc "SELECT s3_key FROM attachments WHERE status = 'ready' ORDER BY created_at DESC LIMIT 1;")
> echo "$OBJECT_KEY"
chats/01a1a6df-35b7-786f-aa90-f7155218d5b0/attachments/01a1a6df-35ca-7a29-9fa7-fb30a52fbed0
> docker compose exec -T s3 wget -S -O /dev/null "http://127.0.0.1:9000/messenger-attachments/${OBJECT_KEY}"
Connecting to 127.0.0.1:9000 (127.0.0.1:9000)
HTTP/1.1 403 Forbidden
wget: server returned error: HTTP/1.1 403 Forbidden
+5
View File
@@ -0,0 +1,5 @@
> make test-failover ACTION_SCRIPT=scripts/stop-one.sh
python3 tests/resilience.py --mode failover --action-script "scripts/stop-one.sh" --base-url "http://localhost:8080"
Executing explicit failover hook; hook output suppressed to avoid leaking secrets.
{"observed_instances_before": 2, "successful_writes": 122, "failed_attempts": 0, "first_recovery_seconds": 2.441}
PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence.
+24
View File
@@ -0,0 +1,24 @@
> docker compose ps -a
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
mtusi2026lab4-api-1 mtusi-messenger:lab4 "/app/server" api 2 hours ago Up 2 hours (healthy) 8080/tcp
mtusi2026lab4-api-2 mtusi-messenger:lab4 "/app/server" api 2 hours ago Up 21 seconds (healthy) 8080/tcp
mtusi2026lab4-load_balancer-1 nginx:1.31.7-alpine3.24 "/docker-entrypoint.…" load_balancer 2 hours ago Up 2 hours 127.0.0.1:8080->8080/tcp
mtusi2026lab4-migrate-1 mtusi2026lab4-migrate "sh -c 'goose -dir /…" migrate 26 hours ago Exited (0) 2 hours ago
mtusi2026lab4-postgres-1 postgres:18.6 "docker-entrypoint.s…" postgres 26 hours ago Up 2 hours (healthy) 127.0.0.1:5432->5432/tcp
mtusi2026lab4-redis-1 redis:8.8.3-alpine3.23 "docker-entrypoint.s…" redis 26 hours ago Up 2 hours (healthy) 6379/tcp
mtusi2026lab4-s3-1 chrislusf/seaweedfs:4.51 "/entrypoint.sh serv…" s3 26 hours ago Up 2 hours (healthy) 7333/tcp, 8080/tcp, 8333/tcp, 8888/tcp, 9333/tcp, 18080/tcp, 18888/tcp, 19333/tcp
mtusi2026lab4-s3-config-init-1 mtusi-messenger-s3config:lab4 "/usr/local/bin/s3co…" s3-config-init 2 hours ago Exited (0) 2 hours ago
> make test-persistence ACTION_SCRIPT=scripts/restart.sh
python3 tests/resilience.py --mode persistence --action-script "scripts/restart.sh" --base-url "http://localhost:8080"
Executing explicit persistence hook; hook output suppressed to avoid leaking secrets.
PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence.
> docker compose ps -a
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
mtusi2026lab4-api-1 mtusi-messenger:lab4 "/app/server" api 31 seconds ago Up 9 seconds (healthy) 8080/tcp
mtusi2026lab4-api-2 mtusi-messenger:lab4 "/app/server" api 31 seconds ago Up 9 seconds (healthy) 8080/tcp
mtusi2026lab4-load_balancer-1 nginx:1.31.7-alpine3.24 "/docker-entrypoint.…" load_balancer 31 seconds ago Up 3 seconds 127.0.0.1:8080->8080/tcp
mtusi2026lab4-migrate-1 mtusi2026lab4-migrate "sh -c 'goose -dir /…" migrate 31 seconds ago Exited (0) 24 seconds ago
mtusi2026lab4-postgres-1 postgres:18.6 "docker-entrypoint.s…" postgres 31 seconds ago Up 30 seconds (healthy) 127.0.0.1:5432->5432/tcp
mtusi2026lab4-redis-1 redis:8.8.3-alpine3.23 "docker-entrypoint.s…" redis 31 seconds ago Up 29 seconds (healthy) 6379/tcp
mtusi2026lab4-s3-1 chrislusf/seaweedfs:4.51 "/entrypoint.sh serv…" s3 31 seconds ago Up 29 seconds (healthy) 7333/tcp, 8080/tcp, 8333/tcp, 8888/tcp, 9333/tcp, 18080/tcp, 18888/tcp, 19333/tcp
mtusi2026lab4-s3-config-init-1 mtusi-messenger-s3config:lab4 "/usr/local/bin/s3co…" s3-config-init 31 seconds ago Exited (0) 30 seconds ago
+106
View File
@@ -0,0 +1,106 @@
//go:build contract || reconcile
package studenttests_test
import (
"bytes"
"encoding/json"
"net/http"
"net/url"
"testing"
"github.com/google/uuid"
"mtusi2026-messenger/internal/dto"
)
const maxUploadBytes = 10 * 1024 * 1024
func uploadAttachment(
t *testing.T,
actor testUser,
chatID uuid.UUID,
filename string,
contentType string,
body []byte,
) *http.Response {
t.Helper()
req, err := http.NewRequest(
http.MethodPost,
*baseURL+"/api/v1/chats/"+chatID.String()+"/attachments?filename="+url.QueryEscape(filename),
bytes.NewReader(body),
)
if err != nil {
t.Fatalf("build upload request: %v", err)
}
req.Header.Set("Content-Type", contentType)
req.Header.Set("Authorization", "Bearer "+actor.Token)
resp, err := httpClient.Do(req)
if err != nil {
t.Fatalf("upload request failed: %v", err)
}
return resp
}
func uploadPNG(t *testing.T, actor testUser, chatID uuid.UUID) dto.AttachmentResponse {
t.Helper()
resp := uploadAttachment(t, actor, chatID, "sample.png", "image/png", makePNG(t, 32, 32))
requireStatus(t, resp, http.StatusCreated)
var att dto.AttachmentResponse
mustDecode(t, resp, &att)
return att
}
func postMessage(
t *testing.T,
actor testUser,
chatID uuid.UUID,
text string,
attachmentIDs []uuid.UUID,
) *http.Response {
t.Helper()
body, err := json.Marshal(map[string]any{
"text": text,
"attachment_ids": attachmentIDs,
})
if err != nil {
t.Fatalf("marshal message: %v", err)
}
req, err := http.NewRequest(
http.MethodPost,
*baseURL+"/api/v1/chats/"+chatID.String()+"/messages",
bytes.NewReader(body),
)
if err != nil {
t.Fatalf("build message request: %v", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+actor.Token)
req.Header.Set("Idempotency-Key", uuid.NewString())
resp, err := httpClient.Do(req)
if err != nil {
t.Fatalf("message request failed: %v", err)
}
return resp
}
func requireErrorCode(t *testing.T, resp *http.Response, code dto.ErrorCode) {
t.Helper()
var apiErr dto.ErrorResponse
mustDecode(t, resp, &apiErr)
if apiErr.Error.Code != code {
t.Errorf("expected error code %q, got %q", code, apiErr.Error.Code)
}
}
+41 -73
View File
@@ -4,9 +4,10 @@ package studenttests_test
import (
"bytes"
"encoding/json"
"image/jpeg"
"image/png"
"io"
"net/http"
"net/url"
"testing"
"github.com/google/uuid"
@@ -14,95 +15,62 @@ import (
"mtusi2026-messenger/internal/dto"
)
const maxUploadBytes = 10 * 1024 * 1024
func TestAttachmentJPEG(t *testing.T) {
user := createUser(t)
chat := createChat(t, user, user.ID)
func uploadAttachment(
t *testing.T,
actor testUser,
chatID uuid.UUID,
filename string,
contentType string,
body []byte,
) *http.Response {
t.Helper()
req, err := http.NewRequest(
http.MethodPost,
*baseURL+"/api/v1/chats/"+chatID.String()+"/attachments?filename="+url.QueryEscape(filename),
bytes.NewReader(body),
)
img, err := png.Decode(bytes.NewReader(makePNG(t, 32, 32)))
if err != nil {
t.Fatalf("build upload request: %v", err)
t.Fatalf("decode png: %v", err)
}
req.Header.Set("Content-Type", contentType)
req.Header.Set("Authorization", "Bearer "+actor.Token)
resp, err := httpClient.Do(req)
if err != nil {
t.Fatalf("upload request failed: %v", err)
var body bytes.Buffer
if err := jpeg.Encode(&body, img, nil); err != nil {
t.Fatalf("encode jpeg: %v", err)
}
return resp
}
func uploadPNG(t *testing.T, actor testUser, chatID uuid.UUID) dto.AttachmentResponse {
t.Helper()
resp := uploadAttachment(t, actor, chatID, "sample.png", "image/png", makePNG(t, 32, 32))
resp := uploadAttachment(t, user, chat.ID, "sample.jpg", "image/jpeg", body.Bytes())
requireStatus(t, resp, http.StatusCreated)
var att dto.AttachmentResponse
mustDecode(t, resp, &att)
if att.Status != "ready" {
t.Fatalf("expected ready attachment, got %q", att.Status)
}
return att
resp = doRequest(t, http.MethodGet, "/api/v1/attachments/"+att.ID.String()+"/content", user.Token, nil)
requireStatus(t, resp, http.StatusOK)
defer resp.Body.Close()
original, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("read original: %v", err)
}
if !bytes.Equal(original, body.Bytes()) {
t.Error("downloaded JPEG differs from uploaded original")
}
}
func postMessage(
t *testing.T,
actor testUser,
chatID uuid.UUID,
text string,
attachmentIDs []uuid.UUID,
) *http.Response {
t.Helper()
func TestAttachmentTooManyPixels(t *testing.T) {
user := createUser(t)
chat := createChat(t, user, user.ID)
body, err := json.Marshal(map[string]any{
"text": text,
"attachment_ids": attachmentIDs,
})
if err != nil {
t.Fatalf("marshal message: %v", err)
body := makePNG(t, 8000, 5001)
if len(body) > maxUploadBytes {
t.Fatal("pixel-limit fixture exceeds upload byte limit")
}
req, err := http.NewRequest(
http.MethodPost,
*baseURL+"/api/v1/chats/"+chatID.String()+"/messages",
bytes.NewReader(body),
)
if err != nil {
t.Fatalf("build message request: %v", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+actor.Token)
req.Header.Set("Idempotency-Key", uuid.NewString())
resp, err := httpClient.Do(req)
if err != nil {
t.Fatalf("message request failed: %v", err)
}
return resp
resp := uploadAttachment(t, user, chat.ID, "large.png", "image/png", body)
requireStatus(t, resp, http.StatusRequestEntityTooLarge)
requireErrorCode(t, resp, dto.ErrorCodePayloadTooLarge)
}
func requireErrorCode(t *testing.T, resp *http.Response, code dto.ErrorCode) {
t.Helper()
func TestAttachmentUnsafeFilename(t *testing.T) {
user := createUser(t)
chat := createChat(t, user, user.ID)
var apiErr dto.ErrorResponse
mustDecode(t, resp, &apiErr)
if apiErr.Error.Code != code {
t.Errorf("expected error code %q, got %q", code, apiErr.Error.Code)
}
resp := uploadAttachment(t, user, chat.ID, "../image.png", "image/png", makePNG(t, 32, 32))
requireStatus(t, resp, http.StatusBadRequest)
requireErrorCode(t, resp, dto.ErrorCodeInvalidRequest)
}
func TestAttachmentTooLarge(t *testing.T) {
+169
View File
@@ -0,0 +1,169 @@
//go:build reconcile
package studenttests_test
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"os"
"os/exec"
"slices"
"strings"
"testing"
"time"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"mtusi2026-messenger/internal/reconcile"
)
func reconcileDB(t *testing.T) *pgx.Conn {
t.Helper()
secret, err := os.ReadFile("../secrets/test_database_url.txt")
if err != nil {
t.Fatalf("read test database URL: %v", err)
}
conn, err := pgx.Connect(context.Background(), strings.TrimSpace(string(secret)))
if err != nil {
t.Fatalf("connect to test database: %v", err)
}
t.Cleanup(func() { conn.Close(context.Background()) })
return conn
}
func reconcileSQL(t *testing.T, conn *pgx.Conn, query string, args ...any) {
t.Helper()
if _, err := conn.Exec(context.Background(), query, args...); err != nil {
t.Fatalf("prepare/check reconcile fixture: %v", err)
}
}
func runReconcile(t *testing.T, dryRun bool, grace string) reconcile.Report {
t.Helper()
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
dry := "false"
if dryRun {
dry = "true"
}
cmd := exec.CommandContext(ctx, "docker", "compose", "--env-file", ".env", "run", "--rm", "--no-deps", "reconcile", "-json", "-dry-run="+dry, "-grace="+grace)
cmd.Dir = ".."
var stderr bytes.Buffer
cmd.Stderr = &stderr
output, err := cmd.Output()
if err != nil {
t.Fatalf("run reconciler: %v\n%s", err, stderr.String())
}
var report reconcile.Report
if err := json.Unmarshal(output, &report); err != nil {
t.Fatalf("decode reconcile report: %v\n%s", err, output)
}
t.Logf("dry_run=%t objects=%d rows=%d list_pages=%d duration_ms=%d orphan=%d missing=%d stale=%d skipped_fresh=%d skipped_referenced=%d deleted_objects=%d deleted_rows=%d marked_failed=%d errors=%v", report.DryRun, report.ScannedObjects, report.ScannedRows, report.ListPages, report.DurationMS, len(report.OrphanObjects), len(report.MissingAttachments), len(report.StaleUploads), report.SkippedFresh, report.SkippedReferenced, report.DeletedObjects, report.DeletedRows, report.MarkedFailed, report.Errors)
if len(report.Errors) != 0 {
t.Fatal("reconciler reported errors")
}
return report
}
func TestReconcileRepair(t *testing.T) {
conn := reconcileDB(t)
user := createUser(t)
chat := createChat(t, user, user.ID)
att := uploadPNG(t, user, chat.ID)
var originalKey string
if err := conn.QueryRow(context.Background(), "SELECT s3_key FROM attachments WHERE id=$1", att.ID).Scan(&originalKey); err != nil {
t.Fatal(err)
}
// Simulate lost metadata for the original object and metadata pointing at a missing object.
reconcileSQL(t, conn, "UPDATE attachments SET s3_key=$2 WHERE id=$1", att.ID, "chats/reconcile-missing/"+uuid.NewString())
// Only this experiment uses a shortened grace period, to avoid waiting an hour.
time.Sleep(1100 * time.Millisecond)
dry := runReconcile(t, true, "1s")
if !slices.Contains(dry.OrphanObjects, originalKey) || !slices.Contains(dry.MissingAttachments, att.ID) {
t.Fatal("dry-run did not detect both injected inconsistencies")
}
if dry.DeletedObjects != 0 || dry.DeletedRows != 0 || dry.MarkedFailed != 0 {
t.Fatal("dry-run changed data")
}
var status, code string
if err := conn.QueryRow(context.Background(), "SELECT status::text FROM attachments WHERE id=$1", att.ID).Scan(&status); err != nil {
t.Fatal(err)
}
if status != "ready" {
t.Fatalf("dry-run changed status: %s", status)
}
repair := runReconcile(t, false, "1s")
if repair.DeletedObjects < 1 || repair.MarkedFailed < 1 {
t.Fatal("repair did not apply fixes")
}
if err := conn.QueryRow(context.Background(), "SELECT status::text, error_code FROM attachments WHERE id=$1", att.ID).Scan(&status, &code); err != nil {
t.Fatal(err)
}
if status != "failed" || code != "object_missing" {
t.Fatalf("unexpected repaired state: %s %s", status, code)
}
again := runReconcile(t, false, "1s")
if slices.Contains(again.OrphanObjects, originalKey) || slices.Contains(again.MissingAttachments, att.ID) {
t.Fatal("fixture still requires repair on repeat")
}
if again.DeletedObjects != 0 || again.DeletedRows != 0 || again.MarkedFailed != 0 {
t.Fatal("repeat repair changed data; run on an idle test stand")
}
t.Log("PASS: dry-run preserved data; orphan deleted; missing marked failed; repeat made no changes")
}
func TestReconcileSafety(t *testing.T) {
conn := reconcileDB(t)
user := createUser(t)
chat := createChat(t, user, user.ID)
fresh := uploadPNG(t, user, chat.ID)
used := uploadPNG(t, user, chat.ID)
resp := postMessage(t, user, chat.ID, "", []uuid.UUID{used.ID})
requireStatus(t, resp, http.StatusCreated)
resp.Body.Close()
// Exercise both grace-period protection and protection of referenced queued uploads.
reconcileSQL(t, conn, "UPDATE attachments SET status='queued' WHERE id=$1", fresh.ID)
reconcileSQL(t, conn, "UPDATE attachments SET status='queued', created_at=now()-interval '2 hours' WHERE id=$1", used.ID)
t.Cleanup(func() {
if _, err := conn.Exec(context.Background(), "UPDATE attachments SET status='ready' WHERE id=ANY($1::uuid[])", []uuid.UUID{fresh.ID, used.ID}); err != nil {
t.Errorf("restore fixture status: %v", err)
}
})
for _, dryRun := range []bool{true, false} {
report := runReconcile(t, dryRun, "1h")
if report.SkippedFresh < 1 || report.SkippedReferenced < 1 {
t.Fatal("fresh/referenced protection was not exercised")
}
for _, att := range []uuid.UUID{fresh.ID, used.ID} {
var status string
if err := conn.QueryRow(context.Background(), "SELECT status::text FROM attachments WHERE id=$1", att).Scan(&status); err != nil {
t.Fatal(err)
}
if status != "queued" {
t.Fatalf("protected fixture changed status: %s", status)
}
resp := doRequest(t, http.MethodGet, "/api/v1/attachments/"+att.String()+"/content", user.Token, nil)
requireStatus(t, resp, http.StatusOK)
data, err := io.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(data, makePNG(t, 32, 32)) {
t.Fatal("protected original changed")
}
}
}
t.Log("PASS: fresh and old referenced uploads preserved, originals unchanged")
}
func TestReconcileCost(t *testing.T) {
for i := 1; i <= 3; i++ {
t.Logf("scan %d/3", i)
runReconcile(t, true, "1h")
}
}
+5 -1
View File
@@ -8,7 +8,7 @@ ACTION ?= stop-one.sh
ACTION_AT ?= 10s
DRY_RUN ?= true
.PHONY: help-go contract-go race-go load-go failure-go load-action-go reconcile
.PHONY: help-go contract-go race-go load-go failure-go load-action-go reconcile reconcile-test-go
help-go:
@echo "contract-go: contract tests"
@@ -38,3 +38,7 @@ load-action-go:
reconcile:
docker compose --env-file .env run --rm --no-deps reconcile -dry-run=$(DRY_RUN)
reconcile-test-go:
docker compose --env-file .env build reconcile
go test -count=1 -tags=reconcile ./student-tests/... -v -args -base-url=$(BASE_URL)