forked from mtusi2026/mtusi2026lab4
fix(s3): handle storage outages and check bucket readiness
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
//go:build contract || reconcile
|
||||
//go:build contract || reconcile || failure
|
||||
|
||||
package studenttests_test
|
||||
|
||||
|
||||
@@ -3,10 +3,14 @@
|
||||
package studenttests_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"net/http"
|
||||
"os/exec"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"mtusi2026-messenger/internal/dto"
|
||||
)
|
||||
|
||||
func compose(t *testing.T, args ...string) {
|
||||
@@ -87,3 +91,66 @@ func TestDatabaseDown(t *testing.T) {
|
||||
requireStatus(t, resp, http.StatusUnauthorized)
|
||||
resp.Body.Close()
|
||||
}
|
||||
|
||||
func TestS3Down(t *testing.T) {
|
||||
user := createUser(t)
|
||||
chat := createChat(t, user, user.ID)
|
||||
original := makePNG(t, 32, 32)
|
||||
att := uploadPNG(t, user, chat.ID)
|
||||
|
||||
stopService(t, "s3")
|
||||
for _, path := range []string{"/health/ready", "/api/v1/attachments/" + att.ID.String() + "/content"} {
|
||||
resp := doRequest(t, http.MethodGet, path, user.Token, nil)
|
||||
requireStatus(t, resp, http.StatusServiceUnavailable)
|
||||
requireErrorCode(t, resp, dto.ErrorCodeUnavailable)
|
||||
}
|
||||
resp := uploadAttachment(t, user, chat.ID, "outage.png", "image/png", original)
|
||||
requireStatus(t, resp, http.StatusServiceUnavailable)
|
||||
requireErrorCode(t, resp, dto.ErrorCodeUnavailable)
|
||||
resp = doRequest(t, http.MethodGet, "/health/live", "", nil)
|
||||
requireStatus(t, resp, http.StatusOK)
|
||||
resp.Body.Close()
|
||||
|
||||
compose(t, "start", "s3")
|
||||
waitReady(t)
|
||||
deadline := time.Now().Add(60 * time.Second)
|
||||
downloaded, uploaded := false, false
|
||||
for time.Now().Before(deadline) && (!downloaded || !uploaded) {
|
||||
if !downloaded {
|
||||
req, err := http.NewRequest(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 && readErr == nil {
|
||||
if !bytes.Equal(data, original) {
|
||||
t.Fatal("original changed across S3 outage")
|
||||
}
|
||||
downloaded = true
|
||||
} else if response.StatusCode != http.StatusServiceUnavailable && response.StatusCode != http.StatusBadGateway && response.StatusCode != http.StatusGatewayTimeout {
|
||||
t.Fatalf("unexpected download recovery status: %d", response.StatusCode)
|
||||
}
|
||||
}
|
||||
}
|
||||
if !uploaded {
|
||||
response := uploadAttachment(t, user, chat.ID, "recovery.png", "image/png", original)
|
||||
status := response.StatusCode
|
||||
response.Body.Close()
|
||||
if status == http.StatusCreated {
|
||||
uploaded = true
|
||||
} else if status != http.StatusServiceUnavailable {
|
||||
t.Fatalf("unexpected upload recovery status: %d", status)
|
||||
}
|
||||
}
|
||||
if !downloaded || !uploaded {
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
if !downloaded || !uploaded {
|
||||
t.Fatalf("S3 did not recover in 60s: download=%t upload=%t", downloaded, uploaded)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user