41 lines
1.1 KiB
Go
41 lines
1.1 KiB
Go
package health
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestHandler(t *testing.T) {
|
|
now := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
|
|
s := NewState(time.Minute)
|
|
s.now = func() time.Time { return now }
|
|
s.started = now
|
|
|
|
get := func() (int, string) {
|
|
rec := httptest.NewRecorder()
|
|
s.Handler().ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/healthz", nil))
|
|
return rec.Code, rec.Body.String()
|
|
}
|
|
|
|
if code, _ := get(); code != 200 {
|
|
t.Fatalf("сразу после старта: %d", code)
|
|
}
|
|
now = now.Add(2 * time.Minute)
|
|
if code, body := get(); code != 503 || !strings.Contains(body, "stalled") {
|
|
t.Fatalf("застрял: %d %s", code, body)
|
|
}
|
|
s.Record(errors.New("traefik down"))
|
|
code, body := get()
|
|
if code != 200 || !strings.Contains(body, "traefik down") {
|
|
t.Fatalf("ошибка внешней системы не должна делать unhealthy: %d %s", code, body)
|
|
}
|
|
s.Record(nil)
|
|
if _, body := get(); !strings.Contains(body, "lastSuccess") || strings.Contains(body, "lastError") {
|
|
t.Fatalf("body = %s", body)
|
|
}
|
|
}
|