diff --git a/pkg/custompluginmonitor/plugin/plugin_scheduler_test.go b/pkg/custompluginmonitor/plugin/plugin_scheduler_test.go index 64c2e0b3..452b47b1 100644 --- a/pkg/custompluginmonitor/plugin/plugin_scheduler_test.go +++ b/pkg/custompluginmonitor/plugin/plugin_scheduler_test.go @@ -130,7 +130,14 @@ func (r *executionRecorder) run(rule cpmtypes.CustomRule) (cpmtypes.Status, stri return cpmtypes.OK, rule.Path } -func (r *executionRecorder) snapshot() (map[string]int, map[string]int, int, int) { +type executionSnapshot struct { + counts map[string]int + maxActive map[string]int + activeTotal int + highWater int +} + +func (r *executionRecorder) snapshot() executionSnapshot { r.mu.Lock() defer r.mu.Unlock() counts := make(map[string]int, len(r.counts)) @@ -141,7 +148,12 @@ func (r *executionRecorder) snapshot() (map[string]int, map[string]int, int, int for rule, count := range r.maxActive { maxActive[rule] = count } - return counts, maxActive, r.activeTotal, r.highWater + return executionSnapshot{ + counts: counts, + maxActive: maxActive, + activeTotal: r.activeTotal, + highWater: r.highWater, + } } func schedulerRule(name string, interval *time.Duration) *cpmtypes.CustomRule { @@ -247,7 +259,7 @@ func stepClock(t *testing.T, fakeClock *recordingClock, duration time.Duration) func assertCounts(t *testing.T, recorder *executionRecorder, wanted map[string]int) { t.Helper() - got, _, _, _ := recorder.snapshot() + got := recorder.snapshot().counts if !reflect.DeepEqual(got, wanted) { t.Fatalf("Invocation counts differ: got %v, wanted %v", got, wanted) } @@ -432,15 +444,14 @@ func TestPluginSchedulerConcurrencyReachesLimit(t *testing.T) { } stepClock(t, fakeClock, 10*time.Second) waitInvocations(t, recorder, 2) - _, _, active, highWater := recorder.snapshot() - if active != 2 || highWater != 2 { - t.Fatalf("Concurrency state is active=%d high-water=%d; wanted 2 and 2", active, highWater) + snapshot := recorder.snapshot() + if snapshot.activeTotal != 2 || snapshot.highWater != 2 { + t.Fatalf("Concurrency state is active=%d high-water=%d; wanted 2 and 2", snapshot.activeTotal, snapshot.highWater) } close(release) waitInvocations(t, recorder, 2) waitResults(t, p, 4) - _, _, _, highWater = recorder.snapshot() - if highWater != 2 { + if highWater := recorder.snapshot().highWater; highWater != 2 { t.Fatalf("Concurrency high-water is %d; wanted 2", highWater) } stopPlugin(t, p) @@ -468,7 +479,7 @@ func TestPluginSchedulerRuleNeverOverlapsAndOverrunCatchesUpOnce(t *testing.T) { waitInvocations(t, recorder, 1) waitResults(t, p, 1) assertCounts(t, recorder, map[string]int{"rule": 3}) - _, maxActive, _, _ := recorder.snapshot() + maxActive := recorder.snapshot().maxActive if maxActive["rule"] != 1 { t.Fatalf("Rule concurrency high-water is %d; wanted 1", maxActive["rule"]) } diff --git a/pkg/custompluginmonitor/types/config_test.go b/pkg/custompluginmonitor/types/config_test.go index e3862da6..44a12161 100644 --- a/pkg/custompluginmonitor/types/config_test.go +++ b/pkg/custompluginmonitor/types/config_test.go @@ -26,7 +26,13 @@ import ( "k8s.io/node-problem-detector/pkg/types" ) -func TestCustomPluginConfigApplyConfiguration(t *testing.T) { +type applyConfigurationTestCase struct { + Orig CustomPluginConfig + Wanted CustomPluginConfig + ErrorMessageStart string +} + +func applyConfigurationTestCases() map[string]applyConfigurationTestCase { globalTimeout := 6 * time.Second globalTimeoutString := globalTimeout.String() invokeInterval := 31 * time.Second @@ -43,11 +49,7 @@ func TestCustomPluginConfigApplyConfiguration(t *testing.T) { ruleInvokeIntervalString := ruleInvokeInterval.String() invalidRuleInvokeIntervalString := "invalid" - utMetas := map[string]struct { - Orig CustomPluginConfig - Wanted CustomPluginConfig - ErrorMessageStart string - }{ + return map[string]applyConfigurationTestCase{ "global default settings": { Orig: CustomPluginConfig{ Rules: []*CustomRule{ @@ -268,8 +270,10 @@ func TestCustomPluginConfigApplyConfiguration(t *testing.T) { }, }, } +} - for desp, utMeta := range utMetas { +func TestCustomPluginConfigApplyConfiguration(t *testing.T) { + for desp, utMeta := range applyConfigurationTestCases() { err := (&utMeta.Orig).ApplyConfiguration() if utMeta.ErrorMessageStart != "" { if err == nil {