1
0
Fork 0
mirror of https://github.com/imjasonh/git-k8s synced 2026-07-16 12:33:02 +00:00
git-k8s/pkg/metrics/metrics_test.go
Claude 1d7073009f
Add Prometheus metrics, health probes, Git timeouts, and unit tests
Critical production-readiness improvements:

- Prometheus metrics: reconcile count/latency per controller, Git operation
  duration (clone/push/ls-remote) via pkg/metrics with /metrics on :9090
- Health probes: leverage Knative's built-in health server on :8080, add
  readiness/liveness probes to all four deployment manifests
- Git operation timeouts: 5-minute context deadline on all git.CloneContext
  and PushContext calls to prevent indefinite blocking on large repos
- Unit tests: new ReconcileKind-level tests for push (pending→failed on
  missing repo, auth error paths), sync (branch-not-found, LastSyncTime),
  resolver (missing repo, partial hashes, empty phase), and repowatcher
  (ls-remote errors, empty remote, unchanged branches)

https://claude.ai/code/session_01PaXbaSqhVEqj97kpY4v6rt
2026-03-17 14:32:02 +00:00

44 lines
1.3 KiB
Go

package metrics
import (
"testing"
"github.com/prometheus/client_golang/prometheus"
)
func TestMetricsRegistered(t *testing.T) {
// Record some data so counters/histograms appear in Gather output.
ReconcileCount.WithLabelValues("test", "success").Inc()
ReconcileLatency.WithLabelValues("test").Observe(0.1)
GitOperationDuration.WithLabelValues("clone").Observe(0.5)
metrics, err := prometheus.DefaultGatherer.Gather()
if err != nil {
t.Fatalf("Gather() error = %v", err)
}
found := make(map[string]bool)
for _, mf := range metrics {
found[mf.GetName()] = true
}
for _, name := range []string{
"gitkube_reconcile_count_total",
"gitkube_reconcile_duration_seconds",
"gitkube_git_operation_duration_seconds",
} {
if !found[name] {
t.Errorf("metric %q not found in registry", name)
}
}
}
func TestMetricsIncrement(t *testing.T) {
// Verify we can record metrics without panicking.
ReconcileCount.WithLabelValues("test-controller", "success").Inc()
ReconcileCount.WithLabelValues("test-controller", "error").Inc()
ReconcileLatency.WithLabelValues("test-controller").Observe(0.5)
GitOperationDuration.WithLabelValues("clone").Observe(1.2)
GitOperationDuration.WithLabelValues("push").Observe(0.3)
GitOperationDuration.WithLabelValues("ls-remote").Observe(0.1)
}