Add unit tests for the custom plugin monitor - #1330
Conversation
This change adds tests for generateStatus, the monitor lifecycle and the metrics path. Package coverage increases from 0.8% to 95.3% of statements. A table test replays sequences of plugin results against one monitor. The table covers the five condition scenarios in generateStatus. Other tests cover the temporary rule path, the problem counter, the problem gauge and the constructor. This change adds no production code.
|
Hi @DigitalVeer. Thanks for your PR. I'm waiting for a kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with Tip We noticed you've done this a few times! Consider joining the org to skip this step and gain Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: DigitalVeer The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
hakman
left a comment
There was a problem hiding this comment.
Thanks @DigitalVeer, I added a few suggestions.
| continue | ||
| } | ||
| require.Len(t, got.Events, 1, "step %d", i) | ||
| event := got.Events[0] |
There was a problem hiding this comment.
The expected event is built with the same helper production uses, so this doesn't independently check severity. If that helper always returned Warn, resolution events would become warnings and the whole suite would still pass.
| event := got.Events[0] | |
| event := got.Events[0] | |
| // Assert severity literally: `want` below is built with the same helper | |
| // generateStatus uses, so it cannot catch a bad severity mapping. | |
| wantSeverity := types.Info | |
| if s.wantStatus == types.True { | |
| wantSeverity = types.Warn | |
| } | |
| assert.Equal(t, wantSeverity, event.Severity, "step %d", i) |
| require.Len(t, status.Events, 1) | ||
| assert.Equal(t, types.Warn, status.Events[0].Severity) | ||
|
|
||
| requireStop(t, c) |
There was a problem hiding this comment.
requireStop shows that Stop() returned, but not that the plugin it owns actually stopped. Delete c.plugin.Stop() at custom_plugin_monitor.go:148 and this package still passes under -race, with the plugin's ticker and rule goroutines left running.
| requireStop(t, c) | |
| requireStop(t, c) | |
| // Stop must also shut down the plugin it owns. | |
| select { | |
| case _, open := <-c.plugin.GetResultChan(): | |
| assert.False(t, open, "Stop did not close the plugin result channel") | |
| case <-time.After(testWait): | |
| t.Fatal("Stop did not close the plugin result channel") | |
| } |
| statusChan, err := c.Start() | ||
| require.NoError(t, err) | ||
|
|
||
| status := receiveStatus(t, statusChan) |
There was a problem hiding this comment.
This doesn't observe whether monitorLoop returns. With no rules, no result-derived status can follow the initial one, so requireNoStatus at the end passes either way — change the closed-channel return at custom_plugin_monitor.go:141 to break and the loop spins forever while this test stays green. Running the loop directly makes its completion observable; Start is still covered by TestMonitorLoopReportsPluginResults.
| statusChan, err := c.Start() | |
| require.NoError(t, err) | |
| status := receiveStatus(t, statusChan) | |
| startedAt := time.Now() | |
| // Start is not used here: monitorLoop runs directly so its return is observable. | |
| // Stop would deadlock, because the closed-channel path never calls tomb.Done. | |
| go c.plugin.Run() | |
| returned := make(chan struct{}) | |
| go func() { | |
| c.monitorLoop() | |
| close(returned) | |
| }() | |
| status := receiveStatus(t, c.statusChan) |
| assert.Empty(t, status.Events) | ||
| require.Len(t, status.Conditions, 2) | ||
| for _, cond := range status.Conditions { | ||
| assert.Equal(t, types.False, cond.Status) |
There was a problem hiding this comment.
These are the only conditions in the suite with a real initialization time — everywhere else newTestMonitor overwrites Transition with testEpoch. Drop the assignment at custom_plugin_monitor.go:321 and every condition ships a zero LastTransitionTime, with the suite still passing. Uses the startedAt captured above.
| assert.Equal(t, types.False, cond.Status) | |
| assert.Equal(t, types.False, cond.Status) | |
| assert.WithinRange(t, cond.Transition, startedAt, time.Now()) |
| c.plugin.Stop() | ||
| requireNoStatus(t, statusChan) |
There was a problem hiding this comment.
This is what makes the test check what its name says. The deadline keeps a regression here a focused failure instead of a hang that times out the whole run.
| c.plugin.Stop() | |
| requireNoStatus(t, statusChan) | |
| c.plugin.Stop() | |
| requireNoStatus(t, c.statusChan) | |
| select { | |
| case <-returned: | |
| case <-time.After(testWait): | |
| t.Fatal("monitorLoop did not return after the result channel closed") | |
| } |
This PR bumps the unit test coverage for custom plugin monitor from 0.8% to 95.3%
This PR adds tests for
generateStatus, the monitor lifecycle and the metrics path.This is the first item of #1328.
pkg/custompluginmonitorWhat the tests check
TestGenerateStatusForConditionsreplays plugin resultsagainst one monitor and checks the condition and the event after each step for five scenarios.
TestGenerateStatusForTemporaryProblemchecks that a temporary rule creates an event andmoves no condition.
TestGenerateStatusMetricschecks the problem counter and the problem gauge through theexisting metrics stub. The counter increments for new problems only.
TestMonitorLoopReportsPluginResultsandTestMonitorLoopExitsWhenResultChannelClosescheck the goroutine lifecycle.
Stopmust return. A closed result channel must end theloop.
TestNewCustomPluginMonitorOrDiewrites a config file and checks that the constructorapplies the defaults.
The lifecycle tests use the plugin scripts in
pkg/custompluginmonitor/plugin/test-data.Validation
go test -cover ./pkg/custompluginmonitor/go test -timeout=1m -race -short -count=5golangci-lint run --config .golangci.yml