forked from mtusi2026/mtusi2026lab4
test(attachments): verify download after uploader replica stops
This commit is contained in:
@@ -1,12 +1,14 @@
|
|||||||
> make failure-go
|
> make failure-go
|
||||||
go test -tags=failure ./student-tests/... -v -args -base-url=http://localhost:8080
|
go test -tags=failure ./student-tests/... -v -args -base-url=http://localhost:8080
|
||||||
|
=== RUN TestAttachmentUploaderFailover
|
||||||
|
--- PASS: TestAttachmentUploaderFailover (1.53s)
|
||||||
=== RUN TestRedisDown
|
=== RUN TestRedisDown
|
||||||
--- PASS: TestRedisDown (3.04s)
|
--- PASS: TestRedisDown (2.81s)
|
||||||
=== RUN TestDatabaseDown
|
=== RUN TestDatabaseDown
|
||||||
--- PASS: TestDatabaseDown (12.51s)
|
--- PASS: TestDatabaseDown (7.67s)
|
||||||
=== RUN TestS3Down
|
=== RUN TestS3Down
|
||||||
--- PASS: TestS3Down (55.01s)
|
--- PASS: TestS3Down (47.30s)
|
||||||
=== RUN TestSessionExpiry
|
=== RUN TestSessionExpiry
|
||||||
--- PASS: TestSessionExpiry (6.40s)
|
--- PASS: TestSessionExpiry (6.05s)
|
||||||
PASS
|
PASS
|
||||||
ok mtusi2026-messenger/student-tests 76.980s
|
ok mtusi2026-messenger/student-tests 65.376s
|
||||||
@@ -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")
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user