-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathmetrics_test.go
More file actions
111 lines (88 loc) · 3.99 KB
/
Copy pathmetrics_test.go
File metadata and controls
111 lines (88 loc) · 3.99 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
Copyright 2025 Valkey Contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package controller
import (
"testing"
"github.com/prometheus/client_golang/prometheus/testutil"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
valkeyiov1alpha1 "github.com/valkey-io/valkey-operator/api/v1alpha1"
)
// testNamespace is the namespace used by all metrics tests.
const testNamespace = "default"
func conditionGaugeValue(t *testing.T, name, condType, status string) float64 {
t.Helper()
return testutil.ToFloat64(clusterCondition.WithLabelValues(name, testNamespace, condType, status))
}
// expectConditionSeries asserts the three status series for a condition type.
func expectConditionSeries(t *testing.T, name, condType string, wantTrue, wantFalse, wantUnknown float64) {
t.Helper()
if got := conditionGaugeValue(t, name, condType, "true"); got != wantTrue {
t.Errorf("%s{status=true} = %v, want %v", condType, got, wantTrue)
}
if got := conditionGaugeValue(t, name, condType, "false"); got != wantFalse {
t.Errorf("%s{status=false} = %v, want %v", condType, got, wantFalse)
}
if got := conditionGaugeValue(t, name, condType, "unknown"); got != wantUnknown {
t.Errorf("%s{status=unknown} = %v, want %v", condType, got, wantUnknown)
}
}
func TestInitClusterMetrics_PreCreatesConditionSeries(t *testing.T) {
const name, ns = "metrics-cond-init-test", "default"
before := testutil.CollectAndCount(clusterCondition)
initClusterMetrics(name, ns)
want := before + len(valkeyiov1alpha1.ClusterConditionTypes)*3
if got := testutil.CollectAndCount(clusterCondition); got != want {
t.Fatalf("expected %d condition series after init, got %d", want, got)
}
deleteClusterMetrics(name, ns)
if got := testutil.CollectAndCount(clusterCondition); got != before {
t.Fatalf("expected %d condition series after delete, got %d", before, got)
}
}
func TestUpdateClusterMetrics_ClusterCondition(t *testing.T) {
const name, ns = "metrics-cond-test", "default"
const condType = valkeyiov1alpha1.ConditionSchedulingSatisfied
initClusterMetrics(name, ns)
defer deleteClusterMetrics(name, ns)
cluster := &valkeyiov1alpha1.ValkeyCluster{}
cluster.Name = name
cluster.Namespace = ns
// Condition absent -> all three status series read 0.
updateClusterMetrics(cluster)
expectConditionSeries(t, name, condType, 0, 0, 0)
setCondition(cluster, condType, valkeyiov1alpha1.ReasonAllPodsScheduled, "ok", metav1.ConditionTrue)
updateClusterMetrics(cluster)
expectConditionSeries(t, name, condType, 1, 0, 0)
setCondition(cluster, condType, valkeyiov1alpha1.ReasonPodsPendingScheduling, "pending", metav1.ConditionFalse)
updateClusterMetrics(cluster)
expectConditionSeries(t, name, condType, 0, 1, 0)
setCondition(cluster, condType, valkeyiov1alpha1.ReasonPodsPendingScheduling, "unknown", metav1.ConditionUnknown)
updateClusterMetrics(cluster)
expectConditionSeries(t, name, condType, 0, 0, 1)
// Condition removed again -> back to all zeros.
cluster.Status.Conditions = nil
updateClusterMetrics(cluster)
expectConditionSeries(t, name, condType, 0, 0, 0)
}
func TestUpdateClusterMetrics_UnregisteredConditionType(t *testing.T) {
const name, ns = "metrics-cond-unregistered-test", "default"
const condType = "NotInClusterConditionTypes"
initClusterMetrics(name, ns)
defer deleteClusterMetrics(name, ns)
cluster := &valkeyiov1alpha1.ValkeyCluster{}
cluster.Name = name
cluster.Namespace = ns
setCondition(cluster, condType, "SomeReason", "some message", metav1.ConditionTrue)
updateClusterMetrics(cluster)
expectConditionSeries(t, name, condType, 1, 0, 0)
}