61 lines
2.3 KiB
Go
61 lines
2.3 KiB
Go
package metricsquality
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"scm.bstein.dev/bstein/ananke/internal/metrics"
|
|
)
|
|
|
|
// TestExporterAppendsQualityGateMetrics runs one orchestration or CLI step.
|
|
// Signature: TestExporterAppendsQualityGateMetrics(t *testing.T).
|
|
// Why: verifies exporter output still includes host quality-gate metrics after
|
|
// the split-module migration.
|
|
func TestExporterAppendsQualityGateMetrics(t *testing.T) {
|
|
tmp := t.TempDir()
|
|
metricsPath := filepath.Join(tmp, "quality-gate.prom")
|
|
content := strings.Join([]string{
|
|
`# HELP ananke_quality_gate_runs_total Total quality gate runs by status.`,
|
|
`# TYPE ananke_quality_gate_runs_total counter`,
|
|
`ananke_quality_gate_runs_total{suite="ananke",status="ok"} 10`,
|
|
`ananke_quality_gate_runs_total{suite="ananke",status="failed"} 2`,
|
|
"",
|
|
}, "\n")
|
|
if err := os.WriteFile(metricsPath, []byte(content), 0o600); err != nil {
|
|
t.Fatalf("write quality metrics file: %v", err)
|
|
}
|
|
t.Setenv("ANANKE_QUALITY_METRICS_FILE", metricsPath)
|
|
|
|
exporter := metrics.New()
|
|
req := httptest.NewRequest(http.MethodGet, "/metrics", nil)
|
|
rr := httptest.NewRecorder()
|
|
exporter.Handler("/metrics").ServeHTTP(rr, req)
|
|
body := rr.Body.String()
|
|
if !strings.Contains(body, `ananke_quality_gate_runs_total{suite="ananke",status="ok"} 10`) {
|
|
t.Fatalf("expected quality gate metrics appended to exporter output, got:\n%s", body)
|
|
}
|
|
}
|
|
|
|
// TestExporterDefaultsQualityGateMetrics runs one orchestration or CLI step.
|
|
// Signature: TestExporterDefaultsQualityGateMetrics(t *testing.T).
|
|
// Why: keeps zero-value quality-gate series visible before the host writes its
|
|
// first metrics file.
|
|
func TestExporterDefaultsQualityGateMetrics(t *testing.T) {
|
|
t.Setenv("ANANKE_QUALITY_METRICS_FILE", filepath.Join(t.TempDir(), "missing.prom"))
|
|
exporter := metrics.New()
|
|
req := httptest.NewRequest(http.MethodGet, "/metrics", nil)
|
|
rr := httptest.NewRecorder()
|
|
exporter.Handler("/metrics").ServeHTTP(rr, req)
|
|
body := rr.Body.String()
|
|
if !strings.Contains(body, `ananke_quality_gate_runs_total{suite="ananke",status="ok"} 0`) {
|
|
t.Fatalf("expected default ok=0 quality gate metric, got:\n%s", body)
|
|
}
|
|
if !strings.Contains(body, `ananke_quality_gate_runs_total{suite="ananke",status="failed"} 0`) {
|
|
t.Fatalf("expected default failed=0 quality gate metric, got:\n%s", body)
|
|
}
|
|
}
|