54 lines
1.8 KiB
Go
54 lines
1.8 KiB
Go
package healthimp
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"git.sendico.io/tech/sxchnge/bff/pkg/api/routers/health"
|
|
"github.com/go-chi/chi/v5"
|
|
"go.uber.org/zap"
|
|
"go.uber.org/zap/zaptest/observer"
|
|
)
|
|
|
|
func TestSetStatusLogsOnlyTransitionsWithReason(t *testing.T) {
|
|
core, observed := observer.New(zap.InfoLevel)
|
|
router := NewRouter(zap.New(core), chi.NewRouter(), "")
|
|
|
|
router.SetStatusWithReason(health.SSStarting, "observability endpoint initialized")
|
|
router.SetStatusWithReason(health.SSStarting, "duplicate readiness report")
|
|
router.SetStatusWithReason(health.SSRunning, "all configured ingestion drivers are ready")
|
|
router.SetStatus(health.SSRunning)
|
|
|
|
entries := observed.FilterMessage("Health status changed").All()
|
|
if len(entries) != 2 {
|
|
t.Fatalf("health transition log count = %d, want 2", len(entries))
|
|
}
|
|
assertTransition(t, entries[0].ContextMap(), "created", "starting", "observability endpoint initialized")
|
|
assertTransition(t, entries[1].ContextMap(), "starting", "ok", "all configured ingestion drivers are ready")
|
|
}
|
|
|
|
func TestSetStatusUsesNonEmptyDefaultReason(t *testing.T) {
|
|
core, observed := observer.New(zap.InfoLevel)
|
|
router := NewRouter(zap.New(core), chi.NewRouter(), "")
|
|
|
|
router.SetStatusWithReason(health.SSTerminating, " ")
|
|
|
|
entries := observed.FilterMessage("Health status changed").All()
|
|
if len(entries) != 1 {
|
|
t.Fatalf("health transition log count = %d, want 1", len(entries))
|
|
}
|
|
assertTransition(t, entries[0].ContextMap(), "created", "deactivating", "service is shutting down")
|
|
}
|
|
|
|
func assertTransition(t *testing.T, fields map[string]any, previous, status, reason string) {
|
|
t.Helper()
|
|
for key, want := range map[string]string{
|
|
"previous_status": previous,
|
|
"status": status,
|
|
"reason": reason,
|
|
} {
|
|
if got, ok := fields[key].(string); !ok || got != want {
|
|
t.Fatalf("%s = %v, want %q", key, fields[key], want)
|
|
}
|
|
}
|
|
}
|