|
19 | 19 | package rollout
|
20 | 20 |
|
21 | 21 | import (
|
22 |
| - "testing" |
23 |
| - |
| 22 | + autoupdatepb "github.com/gravitational/teleport/api/gen/proto/go/teleport/autoupdate/v1" |
| 23 | + "github.com/jonboulle/clockwork" |
24 | 24 | "github.com/prometheus/client_golang/prometheus"
|
| 25 | + dto "github.com/prometheus/client_model/go" |
| 26 | + "github.com/stretchr/testify/require" |
| 27 | + "testing" |
| 28 | + "time" |
25 | 29 | )
|
26 | 30 |
|
27 | 31 | func newMetricsForTest(t *testing.T) *metrics {
|
28 | 32 | reg := prometheus.NewRegistry()
|
29 | 33 | return newMetrics(reg)
|
30 | 34 | }
|
| 35 | + |
| 36 | +func Test_setVersionMetric(t *testing.T) { |
| 37 | + now := clockwork.NewFakeClock().Now() |
| 38 | + aMinuteAgo := now.Add(-time.Minute) |
| 39 | + aWeekAgo := now.Add(-time.Hour * 24 * 7) |
| 40 | + testVersion := "1.2.3-alpha.1" |
| 41 | + previousVersion := "1.2.1" |
| 42 | + testMetricLabels := []string{metricsVersionLabelName} |
| 43 | + tests := []struct { |
| 44 | + name string |
| 45 | + previousVersions map[string]time.Time |
| 46 | + previousMetrics map[string]float64 |
| 47 | + expectedVersions map[string]time.Time |
| 48 | + expectedMetrics map[string]float64 |
| 49 | + }{ |
| 50 | + { |
| 51 | + name: "no versions", |
| 52 | + previousVersions: map[string]time.Time{}, |
| 53 | + previousMetrics: map[string]float64{}, |
| 54 | + expectedVersions: map[string]time.Time{ |
| 55 | + testVersion: now, |
| 56 | + }, |
| 57 | + expectedMetrics: map[string]float64{ |
| 58 | + testVersion: 1, |
| 59 | + }, |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "same version, not expired", |
| 63 | + previousVersions: map[string]time.Time{ |
| 64 | + testVersion: aMinuteAgo, |
| 65 | + }, |
| 66 | + previousMetrics: map[string]float64{ |
| 67 | + testVersion: 1, |
| 68 | + }, |
| 69 | + expectedVersions: map[string]time.Time{ |
| 70 | + testVersion: now, |
| 71 | + }, |
| 72 | + expectedMetrics: map[string]float64{ |
| 73 | + testVersion: 1, |
| 74 | + }, |
| 75 | + }, |
| 76 | + { |
| 77 | + name: "same version, expired", |
| 78 | + previousVersions: map[string]time.Time{ |
| 79 | + testVersion: aWeekAgo, |
| 80 | + }, |
| 81 | + previousMetrics: map[string]float64{ |
| 82 | + testVersion: 1, |
| 83 | + }, |
| 84 | + expectedVersions: map[string]time.Time{ |
| 85 | + testVersion: now, |
| 86 | + }, |
| 87 | + expectedMetrics: map[string]float64{ |
| 88 | + testVersion: 1, |
| 89 | + }, |
| 90 | + }, |
| 91 | + { |
| 92 | + name: "old non-expired versions", |
| 93 | + previousVersions: map[string]time.Time{ |
| 94 | + previousVersion: aMinuteAgo, |
| 95 | + }, |
| 96 | + previousMetrics: map[string]float64{ |
| 97 | + previousVersion: 1, |
| 98 | + }, |
| 99 | + expectedVersions: map[string]time.Time{ |
| 100 | + previousVersion: aMinuteAgo, |
| 101 | + testVersion: now, |
| 102 | + }, |
| 103 | + expectedMetrics: map[string]float64{ |
| 104 | + previousVersion: 0, |
| 105 | + testVersion: 1, |
| 106 | + }, |
| 107 | + }, |
| 108 | + { |
| 109 | + name: "old expired versions", |
| 110 | + previousVersions: map[string]time.Time{ |
| 111 | + previousVersion: aWeekAgo, |
| 112 | + }, |
| 113 | + previousMetrics: map[string]float64{ |
| 114 | + previousVersion: 1, |
| 115 | + }, |
| 116 | + expectedVersions: map[string]time.Time{ |
| 117 | + testVersion: now, |
| 118 | + }, |
| 119 | + expectedMetrics: map[string]float64{ |
| 120 | + testVersion: 1, |
| 121 | + }, |
| 122 | + }, |
| 123 | + } |
| 124 | + for _, test := range tests { |
| 125 | + t.Run(test.name, func(t *testing.T) { |
| 126 | + t.Parallel() |
| 127 | + // Test setup: create metrics and load previous metrics. |
| 128 | + m := metrics{ |
| 129 | + previousVersions: test.previousVersions, |
| 130 | + } |
| 131 | + |
| 132 | + testGauge := prometheus.NewGaugeVec(prometheus.GaugeOpts{}, testMetricLabels) |
| 133 | + for k, v := range test.previousMetrics { |
| 134 | + testGauge.With(prometheus.Labels{testMetricLabels[0]: k}).Set(v) |
| 135 | + } |
| 136 | + |
| 137 | + // Test execution: set the version metric. |
| 138 | + m.setVersionMetric(testVersion, testGauge, now) |
| 139 | + |
| 140 | + // Test validation: collect the metrics and check that the state match what we expect. |
| 141 | + require.Equal(t, test.expectedVersions, m.previousVersions) |
| 142 | + metricsChan := make(chan prometheus.Metric, 100) |
| 143 | + testGauge.Collect(metricsChan) |
| 144 | + close(metricsChan) |
| 145 | + metricsResult := collectMetricsByLabel(t, metricsChan, testMetricLabels[0]) |
| 146 | + require.Equal(t, test.expectedMetrics, metricsResult) |
| 147 | + }) |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +func Test_setGroupStates(t *testing.T) { |
| 152 | + testMetricLabels := []string{metricsGroupNumberLabelName} |
| 153 | + testGroups := []*autoupdatepb.AutoUpdateAgentRolloutStatusGroup{ |
| 154 | + {State: autoupdatepb.AutoUpdateAgentGroupState_AUTO_UPDATE_AGENT_GROUP_STATE_DONE}, |
| 155 | + {State: autoupdatepb.AutoUpdateAgentGroupState_AUTO_UPDATE_AGENT_GROUP_STATE_ACTIVE}, |
| 156 | + {State: autoupdatepb.AutoUpdateAgentGroupState_AUTO_UPDATE_AGENT_GROUP_STATE_UNSTARTED}, |
| 157 | + } |
| 158 | + tests := []struct { |
| 159 | + name string |
| 160 | + previousGroupCount int |
| 161 | + previousMetrics map[string]float64 |
| 162 | + expectedGroupCount int |
| 163 | + expectedMetrics map[string]float64 |
| 164 | + }{ |
| 165 | + { |
| 166 | + name: "no groups", |
| 167 | + previousGroupCount: 0, |
| 168 | + previousMetrics: map[string]float64{}, |
| 169 | + expectedGroupCount: len(testGroups), |
| 170 | + expectedMetrics: map[string]float64{ |
| 171 | + "0": float64(autoupdatepb.AutoUpdateAgentGroupState_AUTO_UPDATE_AGENT_GROUP_STATE_DONE), |
| 172 | + "1": float64(autoupdatepb.AutoUpdateAgentGroupState_AUTO_UPDATE_AGENT_GROUP_STATE_ACTIVE), |
| 173 | + "2": float64(autoupdatepb.AutoUpdateAgentGroupState_AUTO_UPDATE_AGENT_GROUP_STATE_UNSTARTED), |
| 174 | + }, |
| 175 | + }, |
| 176 | + { |
| 177 | + name: "same groups, same states", |
| 178 | + previousGroupCount: len(testGroups), |
| 179 | + previousMetrics: map[string]float64{ |
| 180 | + "0": float64(autoupdatepb.AutoUpdateAgentGroupState_AUTO_UPDATE_AGENT_GROUP_STATE_DONE), |
| 181 | + "1": float64(autoupdatepb.AutoUpdateAgentGroupState_AUTO_UPDATE_AGENT_GROUP_STATE_ACTIVE), |
| 182 | + "2": float64(autoupdatepb.AutoUpdateAgentGroupState_AUTO_UPDATE_AGENT_GROUP_STATE_UNSTARTED), |
| 183 | + }, |
| 184 | + expectedGroupCount: len(testGroups), |
| 185 | + expectedMetrics: map[string]float64{ |
| 186 | + "0": float64(autoupdatepb.AutoUpdateAgentGroupState_AUTO_UPDATE_AGENT_GROUP_STATE_DONE), |
| 187 | + "1": float64(autoupdatepb.AutoUpdateAgentGroupState_AUTO_UPDATE_AGENT_GROUP_STATE_ACTIVE), |
| 188 | + "2": float64(autoupdatepb.AutoUpdateAgentGroupState_AUTO_UPDATE_AGENT_GROUP_STATE_UNSTARTED), |
| 189 | + }, |
| 190 | + }, |
| 191 | + { |
| 192 | + name: "same groups, different states", |
| 193 | + previousGroupCount: len(testGroups), |
| 194 | + previousMetrics: map[string]float64{ |
| 195 | + "0": float64(autoupdatepb.AutoUpdateAgentGroupState_AUTO_UPDATE_AGENT_GROUP_STATE_ACTIVE), |
| 196 | + "1": float64(autoupdatepb.AutoUpdateAgentGroupState_AUTO_UPDATE_AGENT_GROUP_STATE_UNSTARTED), |
| 197 | + "2": float64(autoupdatepb.AutoUpdateAgentGroupState_AUTO_UPDATE_AGENT_GROUP_STATE_UNSTARTED), |
| 198 | + }, |
| 199 | + expectedGroupCount: len(testGroups), |
| 200 | + expectedMetrics: map[string]float64{ |
| 201 | + "0": float64(autoupdatepb.AutoUpdateAgentGroupState_AUTO_UPDATE_AGENT_GROUP_STATE_DONE), |
| 202 | + "1": float64(autoupdatepb.AutoUpdateAgentGroupState_AUTO_UPDATE_AGENT_GROUP_STATE_ACTIVE), |
| 203 | + "2": float64(autoupdatepb.AutoUpdateAgentGroupState_AUTO_UPDATE_AGENT_GROUP_STATE_UNSTARTED), |
| 204 | + }, |
| 205 | + }, |
| 206 | + { |
| 207 | + name: "less groups", |
| 208 | + previousGroupCount: 1, |
| 209 | + previousMetrics: map[string]float64{ |
| 210 | + "0": float64(autoupdatepb.AutoUpdateAgentGroupState_AUTO_UPDATE_AGENT_GROUP_STATE_DONE), |
| 211 | + }, |
| 212 | + expectedGroupCount: len(testGroups), |
| 213 | + expectedMetrics: map[string]float64{ |
| 214 | + "0": float64(autoupdatepb.AutoUpdateAgentGroupState_AUTO_UPDATE_AGENT_GROUP_STATE_DONE), |
| 215 | + "1": float64(autoupdatepb.AutoUpdateAgentGroupState_AUTO_UPDATE_AGENT_GROUP_STATE_ACTIVE), |
| 216 | + "2": float64(autoupdatepb.AutoUpdateAgentGroupState_AUTO_UPDATE_AGENT_GROUP_STATE_UNSTARTED), |
| 217 | + }, |
| 218 | + }, |
| 219 | + { |
| 220 | + name: "more groups", |
| 221 | + previousGroupCount: 5, |
| 222 | + previousMetrics: map[string]float64{ |
| 223 | + "0": float64(autoupdatepb.AutoUpdateAgentGroupState_AUTO_UPDATE_AGENT_GROUP_STATE_ACTIVE), |
| 224 | + "1": float64(autoupdatepb.AutoUpdateAgentGroupState_AUTO_UPDATE_AGENT_GROUP_STATE_UNSTARTED), |
| 225 | + "2": float64(autoupdatepb.AutoUpdateAgentGroupState_AUTO_UPDATE_AGENT_GROUP_STATE_UNSTARTED), |
| 226 | + "3": float64(autoupdatepb.AutoUpdateAgentGroupState_AUTO_UPDATE_AGENT_GROUP_STATE_UNSTARTED), |
| 227 | + "4": float64(autoupdatepb.AutoUpdateAgentGroupState_AUTO_UPDATE_AGENT_GROUP_STATE_UNSTARTED), |
| 228 | + }, |
| 229 | + expectedGroupCount: len(testGroups), |
| 230 | + expectedMetrics: map[string]float64{ |
| 231 | + "0": float64(autoupdatepb.AutoUpdateAgentGroupState_AUTO_UPDATE_AGENT_GROUP_STATE_DONE), |
| 232 | + "1": float64(autoupdatepb.AutoUpdateAgentGroupState_AUTO_UPDATE_AGENT_GROUP_STATE_ACTIVE), |
| 233 | + "2": float64(autoupdatepb.AutoUpdateAgentGroupState_AUTO_UPDATE_AGENT_GROUP_STATE_UNSTARTED), |
| 234 | + "3": float64(autoupdatepb.AutoUpdateAgentGroupState_AUTO_UPDATE_AGENT_GROUP_STATE_UNSPECIFIED), |
| 235 | + "4": float64(autoupdatepb.AutoUpdateAgentGroupState_AUTO_UPDATE_AGENT_GROUP_STATE_UNSPECIFIED), |
| 236 | + }, |
| 237 | + }, |
| 238 | + } |
| 239 | + for _, test := range tests { |
| 240 | + t.Run(test.name, func(t *testing.T) { |
| 241 | + t.Parallel() |
| 242 | + |
| 243 | + testGauge := prometheus.NewGaugeVec(prometheus.GaugeOpts{}, testMetricLabels) |
| 244 | + for k, v := range test.previousMetrics { |
| 245 | + testGauge.With(prometheus.Labels{testMetricLabels[0]: k}).Set(v) |
| 246 | + } |
| 247 | + |
| 248 | + // Test setup: create metrics and load previous metrics. |
| 249 | + m := metrics{ |
| 250 | + groupCount: test.previousGroupCount, |
| 251 | + rolloutGroupState: testGauge, |
| 252 | + } |
| 253 | + |
| 254 | + // Test execution: set the version metric. |
| 255 | + m.setGroupStates(testGroups) |
| 256 | + |
| 257 | + // Test validation: collect the metrics and check that the state match what we expect. |
| 258 | + require.Equal(t, test.expectedGroupCount, m.groupCount) |
| 259 | + metricsChan := make(chan prometheus.Metric, 100) |
| 260 | + m.rolloutGroupState.Collect(metricsChan) |
| 261 | + close(metricsChan) |
| 262 | + metricsResult := collectMetricsByLabel(t, metricsChan, testMetricLabels[0]) |
| 263 | + require.Equal(t, test.expectedMetrics, metricsResult) |
| 264 | + |
| 265 | + }) |
| 266 | + } |
| 267 | +} |
| 268 | + |
| 269 | +func collectMetricsByLabel(t *testing.T, ch <-chan prometheus.Metric, labelName string) map[string]float64 { |
| 270 | + t.Helper() |
| 271 | + result := make(map[string]float64) |
| 272 | + |
| 273 | + var protoMetric dto.Metric |
| 274 | + for { |
| 275 | + m, ok := <-ch |
| 276 | + if !ok { |
| 277 | + return result |
| 278 | + } |
| 279 | + require.NoError(t, m.Write(&protoMetric)) |
| 280 | + ll := protoMetric.GetLabel() |
| 281 | + require.Len(t, ll, 1) |
| 282 | + require.Equal(t, labelName, ll[0].GetName()) |
| 283 | + gg := protoMetric.GetGauge() |
| 284 | + require.NotNil(t, gg) |
| 285 | + result[ll[0].GetValue()] = gg.GetValue() |
| 286 | + } |
| 287 | +} |
0 commit comments