|
| 1 | +package metrics |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/sirupsen/logrus" |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + |
| 10 | + "github.com/prometheus/client_golang/prometheus" |
| 11 | + dto "github.com/prometheus/client_model/go" |
| 12 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 13 | +) |
| 14 | + |
| 15 | +var fakeK8sClient = fake.NewFakeClient() |
| 16 | + |
| 17 | +func TestRegisterKubeVersion(t *testing.T) { |
| 18 | + tests := []struct { |
| 19 | + name string |
| 20 | + isLatest bool |
| 21 | + currentVersion string |
| 22 | + latestVersion string |
| 23 | + channel string |
| 24 | + expectedValue float64 |
| 25 | + }{ |
| 26 | + { |
| 27 | + name: "cluster is up to date", |
| 28 | + isLatest: true, |
| 29 | + currentVersion: "1.28.2", |
| 30 | + latestVersion: "1.28.2", |
| 31 | + channel: "stable", |
| 32 | + expectedValue: 1.0, |
| 33 | + }, |
| 34 | + { |
| 35 | + name: "cluster needs update", |
| 36 | + isLatest: false, |
| 37 | + currentVersion: "1.27.1", |
| 38 | + latestVersion: "1.28.2", |
| 39 | + channel: "stable", |
| 40 | + expectedValue: 0.0, |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "cluster is ahead of stable", |
| 44 | + isLatest: true, |
| 45 | + currentVersion: "1.29.0", |
| 46 | + latestVersion: "1.28.2", |
| 47 | + channel: "stable", |
| 48 | + expectedValue: 1.0, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "latest channel with pre-release", |
| 52 | + isLatest: false, |
| 53 | + currentVersion: "1.28.1", |
| 54 | + latestVersion: "1.29.0-alpha.1", |
| 55 | + channel: "latest", |
| 56 | + expectedValue: 0.0, |
| 57 | + }, |
| 58 | + } |
| 59 | + |
| 60 | + for _, tt := range tests { |
| 61 | + t.Run(tt.name, func(t *testing.T) { |
| 62 | + // Create a new metrics instance for each test to avoid interference |
| 63 | + registry := prometheus.NewRegistry() |
| 64 | + m := New(logrus.NewEntry(logrus.New()), registry, fakeK8sClient) |
| 65 | + |
| 66 | + // Register the Kubernetes version |
| 67 | + m.RegisterKubeVersion(tt.isLatest, tt.currentVersion, tt.latestVersion, tt.channel) |
| 68 | + |
| 69 | + // Gather metrics |
| 70 | + metricFamilies, err := registry.Gather() |
| 71 | + require.NoError(t, err) |
| 72 | + |
| 73 | + // Find the kubernetes version metric |
| 74 | + var kubeMetric *dto.MetricFamily |
| 75 | + for _, mf := range metricFamilies { |
| 76 | + if mf.GetName() == "version_checker_is_latest_kube_version" { |
| 77 | + kubeMetric = mf |
| 78 | + break |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + require.NotNil(t, kubeMetric, "Kubernetes version metric should be present") |
| 83 | + require.Len(t, kubeMetric.GetMetric(), 1, "Should have exactly one metric value") |
| 84 | + |
| 85 | + metric := kubeMetric.GetMetric()[0] |
| 86 | + assert.Equal(t, tt.expectedValue, metric.GetGauge().GetValue()) |
| 87 | + |
| 88 | + // Check labels |
| 89 | + labels := metric.GetLabel() |
| 90 | + assert.Len(t, labels, 3, "Should have 3 labels: current_version, latest_version, channel") |
| 91 | + |
| 92 | + labelMap := make(map[string]string) |
| 93 | + for _, label := range labels { |
| 94 | + labelMap[label.GetName()] = label.GetValue() |
| 95 | + } |
| 96 | + |
| 97 | + assert.Equal(t, tt.currentVersion, labelMap["current_version"]) |
| 98 | + assert.Equal(t, tt.latestVersion, labelMap["latest_version"]) |
| 99 | + assert.Equal(t, tt.channel, labelMap["channel"]) |
| 100 | + }) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func TestRegisterKubeVersion_MultipleChannels(t *testing.T) { |
| 105 | + registry := prometheus.NewRegistry() |
| 106 | + m := New(logrus.NewEntry(logrus.New()), registry, fakeK8sClient) |
| 107 | + |
| 108 | + // Register metrics for different channels |
| 109 | + m.RegisterKubeVersion(true, "1.28.2", "1.28.2", "stable") |
| 110 | + m.RegisterKubeVersion(false, "1.28.2", "1.29.0-alpha.1", "latest") |
| 111 | + |
| 112 | + // Gather metrics |
| 113 | + metricFamilies, err := registry.Gather() |
| 114 | + require.NoError(t, err) |
| 115 | + |
| 116 | + // Find the kubernetes version metric |
| 117 | + var kubeMetric *dto.MetricFamily |
| 118 | + for _, mf := range metricFamilies { |
| 119 | + if mf.GetName() == "version_checker_is_latest_kube_version" { |
| 120 | + kubeMetric = mf |
| 121 | + break |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + require.NotNil(t, kubeMetric, "Kubernetes version metric should be present") |
| 126 | + require.Len(t, kubeMetric.GetMetric(), 2, "Should have exactly two metric values for different channels") |
| 127 | + |
| 128 | + // Check that both metrics are present |
| 129 | + channels := make(map[string]float64) |
| 130 | + for _, metric := range kubeMetric.GetMetric() { |
| 131 | + labelMap := make(map[string]string) |
| 132 | + for _, label := range metric.GetLabel() { |
| 133 | + labelMap[label.GetName()] = label.GetValue() |
| 134 | + } |
| 135 | + channels[labelMap["channel"]] = metric.GetGauge().GetValue() |
| 136 | + } |
| 137 | + |
| 138 | + assert.Equal(t, 1.0, channels["stable"], "Stable channel should be up to date") |
| 139 | + assert.Equal(t, 0.0, channels["latest"], "Latest channel should need update") |
| 140 | +} |
0 commit comments