diff --git a/student-tests/dependency_failure_test.go b/student-tests/dependency_failure_test.go new file mode 100644 index 00000000..b0f23638 --- /dev/null +++ b/student-tests/dependency_failure_test.go @@ -0,0 +1,89 @@ +//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() +} diff --git a/student.mk b/student.mk index 8de59f03..012aef3c 100644 --- a/student.mk +++ b/student.mk @@ -2,10 +2,13 @@ LOAD_ARGS = $(if $(LOAD_WORKERS),-workers=$(LOAD_WORKERS),) \ $(if $(LOAD_DURATION),-duration=$(LOAD_DURATION),) \ $(if $(LOAD_RATE),-rate=$(LOAD_RATE),) -.PHONY: help-go contract-go race-go load-go +.PHONY: help-go contract-go race-go load-go failure-go help-go: - @echo "contract-go: student contract tests; race-go: concurrency test with -race; load-go: Go load test" + @echo "contract-go: contract tests" + @echo "race-go: concurrency tests (-race)" + @echo "load-go: load test" + @echo "failure-go: dependency failure tests (stops redis/postgres containers)" contract-go: go test -tags=contract ./student-tests/... -v -args -base-url=$(BASE_URL) @@ -15,3 +18,6 @@ race-go: load-go: go test -tags=load ./student-tests/... -v -args -base-url=$(BASE_URL) $(LOAD_ARGS) + +failure-go: + go test -tags=failure ./student-tests/... -v -args -base-url=$(BASE_URL)