-
Notifications
You must be signed in to change notification settings - Fork 277
Expand file tree
/
Copy pathmetrics_test.go
More file actions
70 lines (55 loc) · 1.98 KB
/
metrics_test.go
File metadata and controls
70 lines (55 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package metrics
import (
"runtime"
"testing"
"github.com/microsoft/retina/pkg/log"
dto "github.com/prometheus/client_model/go"
)
func TestInitialization_FirstInit(t *testing.T) {
log.SetupZapLogger(log.GetDefaultLogOpts())
InitializeMetrics()
// All metrics should be initialized.
objs := []interface{}{DropPacketsGauge, DropBytesGauge, ForwardBytesGauge, ForwardPacketsGauge, NodeConnectivityStatusGauge, NodeConnectivityLatencyGauge, PluginManagerFailedToReconcileCounter, BuildInfo}
for _, obj := range objs {
if obj == nil {
t.Fatalf("Expected all metrics to be initialized")
}
}
}
func TestInitialization_MultipleInit(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Fatalf("Did not expect InitializeMetrics to panic on reinitialization")
}
}()
log.SetupZapLogger(log.GetDefaultLogOpts())
InitializeMetrics()
// Should not panic when reinitializing.
InitializeMetrics()
}
func TestBuildInfo(t *testing.T) {
log.SetupZapLogger(log.GetDefaultLogOpts())
InitializeMetrics()
if BuildInfo == nil {
t.Fatalf("Expected BuildInfo to be initialized")
}
// Verify that the build info gauge has been set with runtime information
// Note: buildinfo.Version is populated via ldflags during build, so in tests it will
// use the "unknown" fallback. The actual version label is tested in integration tests
// or when running a built binary.
metric := &dto.Metric{}
// Use the actual runtime values that were used to set the metric
err := BuildInfo.WithLabelValues("unknown", runtime.GOARCH, runtime.GOOS).Write(metric)
if err != nil {
t.Fatalf("Failed to write metric: %v", err)
}
if metric.Gauge == nil {
t.Fatalf("Expected gauge metric, got nil")
}
if *metric.Gauge.Value != 1 {
t.Errorf("Expected gauge value to be 1, got %f", *metric.Gauge.Value)
}
t.Logf("✓ BuildInfo metric correctly set to 1 with labels: version=unknown, arch=%s, os=%s", runtime.GOARCH, runtime.GOOS)
}