Skip to content

Commit 6e82d97

Browse files
committed
fix: Ensure testForceDetachMetric works on the delta of ForceDetachMetricCounter
Test_Run_OneVolumeDetachOnOutOfServiceTaintedNode and Test_Run_Positive_OneDesiredVolumeAttachThenDetachWithMountedVolume call testForceDetachMetric with a default of 1. However, depending how the functions are run the value of the ForceDetachMetricCounter may not be 1. This commit changes the testForceDetachMetric invocation in these two functions to fetch the value of ForceDetachMetricCounter at the start of the function and then use that in the call to testForceDetachMetric, similar to Test_Run_OneVolumeDetachOnUnhealthyNodeWithForceDetachOnUnmountDisabled.
1 parent 3dedb8e commit 6e82d97

File tree

1 file changed

+42
-7
lines changed

1 file changed

+42
-7
lines changed

pkg/controller/volume/attachdetach/reconciler/reconciler_test.go

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,18 @@ func Test_Run_Positive_OneDesiredVolumeAttachThenDetachWithMountedVolume(t *test
227227
registerMetrics.Do(func() {
228228
legacyregistry.MustRegister(metrics.ForceDetachMetricCounter)
229229
})
230+
231+
// NOTE: This value is being pulled from a global variable, so it won't necessarily be 0 at the start of the test
232+
initialForceDetachCountTimeout, err := metricstestutil.GetCounterMetricValue(metrics.ForceDetachMetricCounter.WithLabelValues(metrics.ForceDetachReasonTimeout))
233+
if err != nil {
234+
t.Errorf("Error getting initialForceDetachCountTimeout")
235+
}
236+
237+
initialForceDetachCountOutOfService, err := metricstestutil.GetCounterMetricValue(metrics.ForceDetachMetricCounter.WithLabelValues(metrics.ForceDetachReasonOutOfService))
238+
if err != nil {
239+
t.Errorf("Error getting initialForceDetachCountOutOfService")
240+
}
241+
230242
// Arrange
231243
volumePluginMgr, fakePlugin := volumetesting.GetTestVolumePluginMgr(t)
232244
dsw := cache.NewDesiredStateOfWorld(volumePluginMgr)
@@ -295,7 +307,9 @@ func Test_Run_Positive_OneDesiredVolumeAttachThenDetachWithMountedVolume(t *test
295307
waitForDetachCallCount(t, 1 /* expectedDetachCallCount */, fakePlugin)
296308

297309
// Force detach metric due to timeout
298-
testForceDetachMetric(t, 1, metrics.ForceDetachReasonTimeout)
310+
testForceDetachMetric(t, int(initialForceDetachCountTimeout)+1, metrics.ForceDetachReasonTimeout)
311+
// We shouldn't see any additional force detaches, so only consider the initial count
312+
testForceDetachMetric(t, int(initialForceDetachCountOutOfService), metrics.ForceDetachReasonOutOfService)
299313
}
300314

301315
// Populates desiredStateOfWorld cache with one node/volume/pod tuple.
@@ -852,6 +866,18 @@ func Test_Run_OneVolumeDetachOnOutOfServiceTaintedNode(t *testing.T) {
852866
registerMetrics.Do(func() {
853867
legacyregistry.MustRegister(metrics.ForceDetachMetricCounter)
854868
})
869+
870+
// NOTE: This value is being pulled from a global variable, so it won't necessarily be 0 at the start of the test
871+
initialForceDetachCountOutOfService, err := metricstestutil.GetCounterMetricValue(metrics.ForceDetachMetricCounter.WithLabelValues(metrics.ForceDetachReasonOutOfService))
872+
if err != nil {
873+
t.Errorf("Error getting initialForceDetachCountOutOfService")
874+
}
875+
876+
initialForceDetachCountTimeout, err := metricstestutil.GetCounterMetricValue(metrics.ForceDetachMetricCounter.WithLabelValues(metrics.ForceDetachReasonTimeout))
877+
if err != nil {
878+
t.Errorf("Error getting initialForceDetachCountTimeout")
879+
}
880+
855881
// Arrange
856882
volumePluginMgr, fakePlugin := volumetesting.GetTestVolumePluginMgr(t)
857883
dsw := cache.NewDesiredStateOfWorld(volumePluginMgr)
@@ -922,7 +948,9 @@ func Test_Run_OneVolumeDetachOnOutOfServiceTaintedNode(t *testing.T) {
922948
waitForDetachCallCount(t, 1 /* expectedDetachCallCount */, fakePlugin)
923949

924950
// Force detach metric due to out-of-service taint
925-
testForceDetachMetric(t, 1, metrics.ForceDetachReasonOutOfService)
951+
testForceDetachMetric(t, int(initialForceDetachCountOutOfService)+1, metrics.ForceDetachReasonOutOfService)
952+
// We shouldn't see any additional force detaches, so only consider the initial count
953+
testForceDetachMetric(t, int(initialForceDetachCountTimeout), metrics.ForceDetachReasonTimeout)
926954
}
927955

928956
// Populates desiredStateOfWorld cache with one node/volume/pod tuple.
@@ -1119,10 +1147,14 @@ func Test_Run_OneVolumeDetachOnUnhealthyNodeWithForceDetachOnUnmountDisabled(t *
11191147
legacyregistry.MustRegister(metrics.ForceDetachMetricCounter)
11201148
})
11211149
// NOTE: This value is being pulled from a global variable, so it won't necessarily be 0 at the start of the test
1122-
// For example, if Test_Run_OneVolumeDetachOnOutOfServiceTaintedNode runs before this test, then it will be 1
1123-
initialForceDetachCount, err := metricstestutil.GetCounterMetricValue(metrics.ForceDetachMetricCounter.WithLabelValues(metrics.ForceDetachReasonOutOfService))
1150+
initialForceDetachCountOutOfService, err := metricstestutil.GetCounterMetricValue(metrics.ForceDetachMetricCounter.WithLabelValues(metrics.ForceDetachReasonOutOfService))
11241151
if err != nil {
1125-
t.Errorf("Error getting initialForceDetachCount")
1152+
t.Errorf("Error getting initialForceDetachCountOutOfService")
1153+
}
1154+
1155+
initialForceDetachCountTimeout, err := metricstestutil.GetCounterMetricValue(metrics.ForceDetachMetricCounter.WithLabelValues(metrics.ForceDetachReasonTimeout))
1156+
if err != nil {
1157+
t.Errorf("Error getting initialForceDetachCountTimeout")
11261158
}
11271159

11281160
// Arrange
@@ -1219,7 +1251,8 @@ func Test_Run_OneVolumeDetachOnUnhealthyNodeWithForceDetachOnUnmountDisabled(t *
12191251

12201252
// Force detach metric due to out-of-service taint
12211253
// We shouldn't see any additional force detaches, so only consider the initial count
1222-
testForceDetachMetric(t, int(initialForceDetachCount), metrics.ForceDetachReasonOutOfService)
1254+
testForceDetachMetric(t, int(initialForceDetachCountOutOfService), metrics.ForceDetachReasonOutOfService)
1255+
testForceDetachMetric(t, int(initialForceDetachCountTimeout), metrics.ForceDetachReasonTimeout)
12231256

12241257
// Act
12251258
// Taint the node
@@ -1238,7 +1271,9 @@ func Test_Run_OneVolumeDetachOnUnhealthyNodeWithForceDetachOnUnmountDisabled(t *
12381271

12391272
// Force detach metric due to out-of-service taint
12401273
// We should see one more force detach, so consider the initial count + 1
1241-
testForceDetachMetric(t, int(initialForceDetachCount)+1, metrics.ForceDetachReasonOutOfService)
1274+
testForceDetachMetric(t, int(initialForceDetachCountOutOfService)+1, metrics.ForceDetachReasonOutOfService)
1275+
// We shouldn't see any additional force detaches, so only consider the initial count
1276+
testForceDetachMetric(t, int(initialForceDetachCountTimeout), metrics.ForceDetachReasonTimeout)
12421277
}
12431278

12441279
func Test_ReportMultiAttachError(t *testing.T) {

0 commit comments

Comments
 (0)