ananke/internal/metrics/exporter_test.go

51 lines
1.4 KiB
Go

package metrics
import (
"net/http/httptest"
"strings"
"testing"
"time"
)
func TestExporterEmitsCoreMetrics(t *testing.T) {
e := New()
e.UpdateBudget(321)
e.UpdateSample(Sample{
Name: "db-ups",
Target: "atlasups@localhost",
OnBattery: true,
LowBattery: false,
RuntimeSecond: 412,
BatteryCharge: 81.5,
LoadPercent: 27.4,
PowerNominalW: 510,
ThresholdSec: 354,
Trigger: true,
BreachCount: 2,
Status: "OB",
UpdatedAt: time.Unix(1710000000, 0).UTC(),
})
e.MarkShutdown("ups-threshold")
req := httptest.NewRequest("GET", "/metrics", nil)
rr := httptest.NewRecorder()
e.Handler("/metrics").ServeHTTP(rr, req)
body := rr.Body.String()
mustContain := []string{
"hecate_shutdown_budget_seconds 321",
"hecate_shutdown_triggers_total 1",
"hecate_ups_on_battery{source=\"db-ups\",target=\"atlasups@localhost\"",
"hecate_ups_runtime_seconds{source=\"db-ups\",target=\"atlasups@localhost\"",
"hecate_ups_battery_charge_percent{source=\"db-ups\",target=\"atlasups@localhost\"",
"hecate_ups_load_percent{source=\"db-ups\",target=\"atlasups@localhost\"",
"hecate_ups_power_nominal_watts{source=\"db-ups\",target=\"atlasups@localhost\"",
"hecate_ups_threshold_seconds{source=\"db-ups\",target=\"atlasups@localhost\"",
}
for _, m := range mustContain {
if !strings.Contains(body, m) {
t.Fatalf("missing metric fragment %q in output:\n%s", m, body)
}
}
}