Files
node/service/health/handler_test.go
T
zeekayandHanzo Dev dada5a3169 health,build: make /v1/health and /v1/metrics report reality
Two endpoints were answering with the right status code and an empty
truth, so every fleet looked instrumented while reporting nothing.

GET /v1/health encoded apihealth.APIReply through jsonv2, which has no
default representation for time.Duration — and every Result carries one.
The marshal therefore failed on every node, every time, and the handler
fell back to {"healthy":…,"error":"health reply encode failed"}. The
status code is written before the encode, so k8s probes and dashboards
stayed green while the body carried no checks at all. Measured on Zoo,
Hanzo and Pars mainnet, node v1.34.9 and v1.36.2 alike.

The type is defined with encoding/json tags and the POST (jsonrpc) path
already encodes it through that codec, which is why POST returned the
full check set while GET returned nothing. One wire type deserves one
encoder: GET now uses encoding/json too, so the two paths agree by
construction. encoding/json also maps invalid UTF-8 in check Details to
U+FFFD instead of failing, which is the behaviour the old jsontext
AllowInvalidUTF8 option was reaching for. The buffer stays: it keeps a
partial encode from shipping a torn body.

/v1/metrics answered 200 with zero bytes for the same shape of reason.
luxfi/metric resolves NewRegistry() to a no-op registry unless the
binary is built with -tags metrics (registry_noop.go, //go:build
!metrics), and only the `full` profile passed it. Images build with the
default `minimal` profile, so every metric in luxd registered into a
black hole while --api-metrics-enabled=true still advertised metrics as
on. Whether metrics are served is the runtime flag's call; a build tag
must not silently overrule it. `metrics` becomes a base tag on every
profile.

Verified: the three new handler tests fail against the jsonv2 encoder
(checks decode empty, error field nil — the exact production symptom)
and pass after. `go tool nm` shows (*registry).Gather absent from a
default build and present once the tag is set.

Co-authored-by: Hanzo Dev <dev@hanzo.ai>
2026-07-25 15:10:46 -07:00

104 lines
3.3 KiB
Go

// Copyright (C) 2019-2025, Lux Industries Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package health
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/require"
apihealth "github.com/luxfi/api/health"
)
// The GET handler must emit the real check set. It previously encoded through
// jsonv2, which cannot represent apihealth.Result.Duration (a time.Duration),
// so every reply on every node degraded to {"healthy":…,"error":"health reply
// encode failed"} while the status code still looked correct — k8s probes
// passed and operators saw nothing.
func TestGetHandlerEncodesDurationBearingChecks(t *testing.T) {
require := require.New(t)
checks := map[string]apihealth.Result{
"bls": {
Details: "node has the correct BLS key",
Duration: 134597 * time.Nanosecond,
Timestamp: time.Unix(1753479996, 0).UTC(),
},
}
h := NewGetHandler(func(...string) (map[string]apihealth.Result, bool) {
return checks, true
})
rec := httptest.NewRecorder()
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/v1/health", nil))
require.Equal(http.StatusOK, rec.Code)
var reply apihealth.APIReply
require.NoError(json.Unmarshal(rec.Body.Bytes(), &reply))
require.True(reply.Healthy)
require.Len(reply.Checks, 1)
require.Equal(134597*time.Nanosecond, reply.Checks["bls"].Duration)
require.NotContains(rec.Body.String(), "health reply encode failed")
}
// An unhealthy node must still return the full diagnostic body alongside the
// 503 — the body is the only thing that says *why*.
func TestGetHandlerUnhealthyStillCarriesChecks(t *testing.T) {
require := require.New(t)
errMsg := "network layer is unhealthy reason: primary network validator has no inbound connections"
checks := map[string]apihealth.Result{
"network": {
Details: map[string]any{"connectedPeers": 4},
Error: &errMsg,
Duration: 40357 * time.Nanosecond,
ContiguousFailures: 31997,
},
}
h := NewGetHandler(func(...string) (map[string]apihealth.Result, bool) {
return checks, false
})
rec := httptest.NewRecorder()
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/v1/health", nil))
require.Equal(http.StatusServiceUnavailable, rec.Code)
var reply apihealth.APIReply
require.NoError(json.Unmarshal(rec.Body.Bytes(), &reply))
require.False(reply.Healthy)
require.Equal(&errMsg, reply.Checks["network"].Error)
require.Equal(int64(31997), reply.Checks["network"].ContiguousFailures)
}
// Check Details may embed raw chain-ID bytes. Invalid UTF-8 must degrade to
// replacement runes, never to an encode failure that drops the whole reply.
func TestGetHandlerInvalidUTF8InDetailsDoesNotDropReply(t *testing.T) {
require := require.New(t)
checks := map[string]apihealth.Result{
"database": {
Details: string([]byte{0xff, 0xfe, 0x00}),
Duration: 19137 * time.Nanosecond,
},
}
h := NewGetHandler(func(...string) (map[string]apihealth.Result, bool) {
return checks, true
})
rec := httptest.NewRecorder()
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/v1/health", nil))
require.Equal(http.StatusOK, rec.Code)
var reply apihealth.APIReply
require.NoError(json.Unmarshal(rec.Body.Bytes(), &reply))
require.Len(reply.Checks, 1)
require.NotContains(rec.Body.String(), "health reply encode failed")
}