90 lines
2.2 KiB
Go
90 lines
2.2 KiB
Go
//go:build failure
|
|
|
|
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")
|
|
}
|
|
|
|
func TestRedisDown(t *testing.T) {
|
|
user := createUser(t)
|
|
|
|
stopService(t, "redis")
|
|
|
|
// настоящий токен
|
|
resp := doRequest(t, http.MethodGet, "/api/v1/users/me", user.Token, nil)
|
|
requireStatus(t, resp, http.StatusOK)
|
|
resp.Body.Close()
|
|
|
|
// выдуманный токен
|
|
resp = doRequest(t, http.MethodGet, "/api/v1/users/me", "forged-"+user.Token, nil)
|
|
requireStatus(t, resp, http.StatusUnauthorized)
|
|
resp.Body.Close()
|
|
|
|
// без токена
|
|
resp = doRequest(t, http.MethodGet, "/api/v1/users/me", "", nil)
|
|
requireStatus(t, resp, http.StatusUnauthorized)
|
|
resp.Body.Close()
|
|
}
|
|
|
|
func TestDatabaseDown(t *testing.T) {
|
|
user := createUser(t) // токен ещё не использован, ключа в Redis нет
|
|
|
|
stopService(t, "postgres")
|
|
|
|
// ключа в кэше нет, проверить сессию негде
|
|
resp := doRequest(t, http.MethodGet, "/api/v1/users/me", user.Token, nil)
|
|
requireStatus(t, resp, http.StatusServiceUnavailable)
|
|
resp.Body.Close()
|
|
|
|
// выдуманный токен
|
|
resp = doRequest(t, http.MethodGet, "/api/v1/users/me", "forged-"+user.Token, nil)
|
|
requireStatus(t, resp, http.StatusServiceUnavailable)
|
|
resp.Body.Close()
|
|
|
|
// без токена
|
|
resp = doRequest(t, http.MethodGet, "/api/v1/users/me", "", nil)
|
|
requireStatus(t, resp, http.StatusUnauthorized)
|
|
resp.Body.Close()
|
|
}
|