2026-04-03 14:46:03 -03:00
|
|
|
package metrics
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http/httptest"
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
2026-04-09 01:38:06 -03:00
|
|
|
// TestExporterEmitsCoreMetrics runs one orchestration or CLI step.
|
|
|
|
|
// Signature: TestExporterEmitsCoreMetrics(t *testing.T).
|
|
|
|
|
// Why: keeps behavior explicit so startup/shutdown workflows remain maintainable as services evolve.
|
2026-04-03 14:46:03 -03:00
|
|
|
func TestExporterEmitsCoreMetrics(t *testing.T) {
|
|
|
|
|
e := New()
|
|
|
|
|
e.UpdateBudget(321)
|
|
|
|
|
e.UpdateSample(Sample{
|
2026-04-04 05:50:38 -03:00
|
|
|
Name: "Pyrphoros",
|
|
|
|
|
Target: "pyrphoros@localhost",
|
2026-04-03 14:46:03 -03:00
|
|
|
OnBattery: true,
|
|
|
|
|
LowBattery: false,
|
|
|
|
|
RuntimeSecond: 412,
|
2026-04-03 17:50:05 -03:00
|
|
|
BatteryCharge: 81.5,
|
|
|
|
|
LoadPercent: 27.4,
|
2026-04-03 20:43:27 -03:00
|
|
|
PowerNominalW: 510,
|
2026-04-03 14:46:03 -03:00
|
|
|
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{
|
2026-04-07 13:13:58 -03:00
|
|
|
"ananke_shutdown_budget_seconds 321",
|
|
|
|
|
"ananke_shutdown_triggers_total 1",
|
|
|
|
|
"ananke_ups_on_battery{source=\"Pyrphoros\",target=\"pyrphoros@localhost\"",
|
|
|
|
|
"ananke_ups_runtime_seconds{source=\"Pyrphoros\",target=\"pyrphoros@localhost\"",
|
|
|
|
|
"ananke_ups_battery_charge_percent{source=\"Pyrphoros\",target=\"pyrphoros@localhost\"",
|
|
|
|
|
"ananke_ups_load_percent{source=\"Pyrphoros\",target=\"pyrphoros@localhost\"",
|
|
|
|
|
"ananke_ups_power_nominal_watts{source=\"Pyrphoros\",target=\"pyrphoros@localhost\"",
|
|
|
|
|
"ananke_ups_threshold_seconds{source=\"Pyrphoros\",target=\"pyrphoros@localhost\"",
|
2026-04-03 14:46:03 -03:00
|
|
|
}
|
|
|
|
|
for _, m := range mustContain {
|
|
|
|
|
if !strings.Contains(body, m) {
|
|
|
|
|
t.Fatalf("missing metric fragment %q in output:\n%s", m, body)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|