125 lines
3.0 KiB
Go
125 lines
3.0 KiB
Go
//go:build failure || worker || pipeline
|
|
|
|
package studenttests_test
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net/http"
|
|
"os/exec"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"mtusi2026-messenger/internal/dto"
|
|
)
|
|
|
|
func compose(t *testing.T, args ...string) {
|
|
t.Helper()
|
|
|
|
cmd := exec.Command("docker", append([]string{"compose"}, args...)...)
|
|
cmd.Dir = ".."
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
t.Fatalf("docker compose %v: %v\n%s", args, err, out)
|
|
}
|
|
}
|
|
|
|
func stopService(t *testing.T, name string) {
|
|
t.Helper()
|
|
|
|
compose(t, "stop", name)
|
|
t.Cleanup(func() {
|
|
compose(t, "start", name)
|
|
waitReady(t)
|
|
})
|
|
}
|
|
|
|
func waitReady(t *testing.T) {
|
|
t.Helper()
|
|
|
|
deadline := time.Now().Add(60 * time.Second)
|
|
for time.Now().Before(deadline) {
|
|
resp, err := httpClient.Get(*baseURL + "/health/ready")
|
|
if err == nil {
|
|
resp.Body.Close()
|
|
if resp.StatusCode == http.StatusOK {
|
|
return
|
|
}
|
|
}
|
|
time.Sleep(500 * time.Millisecond)
|
|
}
|
|
t.Fatal("service did not become ready in 60s")
|
|
}
|
|
|
|
func workerCommand(t *testing.T, args ...string) string {
|
|
t.Helper()
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
defer cancel()
|
|
cmd := exec.CommandContext(ctx, "docker", args...)
|
|
cmd.Dir = ".."
|
|
out, err := cmd.Output()
|
|
if err != nil {
|
|
t.Fatalf("Docker command failed: %v", err)
|
|
}
|
|
return strings.TrimSpace(string(out))
|
|
}
|
|
|
|
func workerSQL(t *testing.T, query string) string {
|
|
t.Helper()
|
|
return workerCommand(t, "compose", "exec", "-T", "postgres", "psql",
|
|
"-U", "messenger", "-d", "messenger", "-Atqc", query)
|
|
}
|
|
|
|
func workerWait(t *testing.T, description string, timeout time.Duration, condition func() bool) {
|
|
t.Helper()
|
|
deadline := time.Now().Add(timeout)
|
|
for time.Now().Before(deadline) {
|
|
if condition() {
|
|
return
|
|
}
|
|
time.Sleep(300 * time.Millisecond)
|
|
}
|
|
t.Fatalf("timeout waiting for %s", description)
|
|
}
|
|
|
|
func workerQueue(t *testing.T) string {
|
|
t.Helper()
|
|
out := workerCommand(t, "compose", "exec", "-T", "rabbitmq", "rabbitmqctl",
|
|
"list_queues", "name", "messages_ready", "messages_unacknowledged", "--silent")
|
|
for _, line := range strings.Split(out, "\n") {
|
|
fields := strings.Fields(line)
|
|
if len(fields) == 3 && fields[0] == "attachments.process.v1" {
|
|
return fields[1] + ":" + fields[2]
|
|
}
|
|
}
|
|
t.Fatal("attachment queue missing")
|
|
return ""
|
|
}
|
|
|
|
func workerUpload(t *testing.T, user testUser, chatID uuid.UUID) (dto.AttachmentResponse, []byte) {
|
|
t.Helper()
|
|
source := makePNG(t, 320, 200)
|
|
resp := uploadAttachment(t, user, chatID, "worker.png", "image/png", source)
|
|
requireStatus(t, resp, http.StatusAccepted)
|
|
var att dto.AttachmentResponse
|
|
mustDecode(t, resp, &att)
|
|
if att.Status != "queued" {
|
|
t.Fatalf("expected queued, got %s", att.Status)
|
|
}
|
|
return att, source
|
|
}
|
|
|
|
func workerContent(t *testing.T, user testUser, id uuid.UUID, variant string) []byte {
|
|
t.Helper()
|
|
resp := doRequest(t, http.MethodGet, "/api/v1/attachments/"+id.String()+"/content?variant="+variant, user.Token, nil)
|
|
requireStatus(t, resp, http.StatusOK)
|
|
defer resp.Body.Close()
|
|
data, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return data
|
|
}
|