forked from mtusi2026/mtusi2026lab5
48 lines
898 B
Go
48 lines
898 B
Go
//go:build failure || worker
|
|
|
|
package studenttests_test
|
|
|
|
import (
|
|
"net/http"
|
|
"os/exec"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
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")
|
|
}
|