From 91f9308991c9dbb48751cbfcca517f7f33623731 Mon Sep 17 00:00:00 2001 From: kk10 Date: Thu, 5 Nov 2026 11:18:04 +0300 Subject: [PATCH] test(attachments): verify download after uploader replica stops --- evidence/failure-go.txt | 14 ++-- student-tests/attachment_failover_test.go | 92 +++++++++++++++++++++++ 2 files changed, 100 insertions(+), 6 deletions(-) create mode 100644 student-tests/attachment_failover_test.go diff --git a/evidence/failure-go.txt b/evidence/failure-go.txt index f6fc73a9..a64fc650 100644 --- a/evidence/failure-go.txt +++ b/evidence/failure-go.txt @@ -1,12 +1,14 @@ -> make failure-go +> make failure-go go test -tags=failure ./student-tests/... -v -args -base-url=http://localhost:8080 +=== RUN TestAttachmentUploaderFailover +--- PASS: TestAttachmentUploaderFailover (1.53s) === RUN TestRedisDown ---- PASS: TestRedisDown (3.04s) +--- PASS: TestRedisDown (2.81s) === RUN TestDatabaseDown ---- PASS: TestDatabaseDown (12.51s) +--- PASS: TestDatabaseDown (7.67s) === RUN TestS3Down ---- PASS: TestS3Down (55.01s) +--- PASS: TestS3Down (47.30s) === RUN TestSessionExpiry ---- PASS: TestSessionExpiry (6.40s) +--- PASS: TestSessionExpiry (6.05s) PASS -ok mtusi2026-messenger/student-tests 76.980s \ No newline at end of file +ok mtusi2026-messenger/student-tests 65.376s \ No newline at end of file diff --git a/student-tests/attachment_failover_test.go b/student-tests/attachment_failover_test.go new file mode 100644 index 00000000..e37e1440 --- /dev/null +++ b/student-tests/attachment_failover_test.go @@ -0,0 +1,92 @@ +//go:build failure + +package studenttests_test + +import ( + "bytes" + "context" + "io" + "net/http" + "os/exec" + "strings" + "testing" + "time" + + "mtusi2026-messenger/internal/dto" +) + +func TestAttachmentUploaderFailover(t *testing.T) { + user := createUser(t) + chat := createChat(t, user, user.ID) + original := makePNG(t, 32, 32) + resp := uploadAttachment(t, user, chat.ID, "failover.png", "image/png", original) + requireStatus(t, resp, http.StatusCreated) + uploader := resp.Header.Get("X-Instance-Id") + var att dto.AttachmentResponse + mustDecode(t, resp, &att) + if uploader == "" { + t.Fatal("upload response has no X-Instance-Id") + } + + cmd := exec.Command("docker", "compose", "ps", "-q", "api") + cmd.Dir = ".." + out, err := cmd.Output() + if err != nil { + t.Fatalf("list API replicas: %v", err) + } + replicas := strings.Fields(string(out)) + if len(replicas) < 2 { + t.Fatal("at least two running API replicas required") + } + var target string + for _, id := range replicas { + if strings.HasPrefix(id, uploader) { + target = id + break + } + } + if target == "" { + t.Fatal("upload instance is not an API container in this Compose project") + } + t.Cleanup(func() { + if err := exec.Command("docker", "start", target).Run(); err != nil { + t.Errorf("restart uploader replica: %v", err) + } + }) + if err := exec.Command("docker", "stop", "-t", "5", target).Run(); err != nil { + t.Fatalf("stop uploader replica: %v", err) + } + + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + for ctx.Err() == nil { + req, err := http.NewRequestWithContext(ctx, http.MethodGet, *baseURL+"/api/v1/attachments/"+att.ID.String()+"/content", nil) + if err != nil { + t.Fatal(err) + } + req.Header.Set("Authorization", "Bearer "+user.Token) + response, err := httpClient.Do(req) + if err == nil { + data, readErr := io.ReadAll(response.Body) + response.Body.Close() + if response.StatusCode == http.StatusOK { + downloader := response.Header.Get("X-Instance-Id") + if downloader == "" || downloader == uploader { + t.Fatal("download was not served by another replica") + } + if readErr != nil { + t.Fatal(readErr) + } + if !bytes.Equal(data, original) { + t.Fatal("downloaded original differs from uploaded file") + } + return + } + if response.StatusCode != http.StatusBadGateway && response.StatusCode != http.StatusServiceUnavailable && response.StatusCode != http.StatusGatewayTimeout { + t.Fatalf("unexpected download status: %d", response.StatusCode) + } + } + time.Sleep(200 * time.Millisecond) + } + t.Fatal("download through another replica did not recover within 10s") +}