Files
traefik-selectel/internal/health/health_test.go
T
vasyanskandClaude Sonnet 5 273cd54415
Build / Tests (push) Successful in 13s
Build / Build image (push) Failing after 15s
Build / Notify on failure (push) Failing after 0s
feat: traefik-selectel-dns — A-записи Selectel по роутам Traefik + CI сборка образа
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-21 21:50:07 +07:00

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)
}
}