forked from mtusi2026/mtusi2026lab5
93 lines
2.6 KiB
Go
93 lines
2.6 KiB
Go
//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.StatusAccepted)
|
|
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.StatusConflict && 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")
|
|
}
|