Revert "test: use maps.Clone in TestPrepareMountArgs to prevent mutating shar…"#1401
Revert "test: use maps.Clone in TestPrepareMountArgs to prevent mutating shar…"#1401chrisThePattyEater wants to merge 2 commits into
Conversation
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: chrisThePattyEater 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 |
There was a problem hiding this comment.
Code Review
This pull request refactors test setups. In injection_test.go, it switches from directly adding objects to the informer's store to creating them via a fake client and waiting for cache synchronization. In sidecar_mounter_config_test.go, it removes the use of maps.Clone and directly mutates tc.expectedArgs. However, feedback points out that this direct mutation re-introduces a test pollution bug because tc.expectedArgs references a shared global map, and suggests manually copying the map instead.
| found := slices.Contains(tc.mc.Options, util.DisableMetricsForGKE+":true") | ||
| if !found { | ||
| expectedArgs["prometheus-port"] = strconv.Itoa(testPrometheusPort) | ||
| tc.expectedArgs["prometheus-port"] = strconv.Itoa(testPrometheusPort) | ||
| } | ||
| // Increase port value to match behavior of prepareMountArgs() | ||
| testPrometheusPort++ | ||
|
|
||
| tc.mc.prepareMountArgs() | ||
| if !reflect.DeepEqual(tc.mc.FlagMap, expectedArgs) { | ||
| t.Errorf("Got args %v, but expected %v", tc.mc.FlagMap, expectedArgs) | ||
| if !reflect.DeepEqual(tc.mc.FlagMap, tc.expectedArgs) { | ||
| t.Errorf("Got args %v, but expected %v", tc.mc.FlagMap, tc.expectedArgs) | ||
| } |
There was a problem hiding this comment.
Reverting the use of maps.Clone re-introduces a test pollution bug. tc.expectedArgs points to the shared global map defaultFlagMap for multiple test cases. Mutating tc.expectedArgs directly via tc.expectedArgs["prometheus-port"] = ... mutates defaultFlagMap for all subsequent test cases that reference it.
To prevent this test pollution without relying on the maps package (if that was the reason for the revert), we can manually copy the map for each test run.
| found := slices.Contains(tc.mc.Options, util.DisableMetricsForGKE+":true") | |
| if !found { | |
| expectedArgs["prometheus-port"] = strconv.Itoa(testPrometheusPort) | |
| tc.expectedArgs["prometheus-port"] = strconv.Itoa(testPrometheusPort) | |
| } | |
| // Increase port value to match behavior of prepareMountArgs() | |
| testPrometheusPort++ | |
| tc.mc.prepareMountArgs() | |
| if !reflect.DeepEqual(tc.mc.FlagMap, expectedArgs) { | |
| t.Errorf("Got args %v, but expected %v", tc.mc.FlagMap, expectedArgs) | |
| if !reflect.DeepEqual(tc.mc.FlagMap, tc.expectedArgs) { | |
| t.Errorf("Got args %v, but expected %v", tc.mc.FlagMap, tc.expectedArgs) | |
| } | |
| expectedArgs := make(map[string]string, len(tc.expectedArgs)) | |
| for k, v := range tc.expectedArgs { | |
| expectedArgs[k] = v | |
| } | |
| found := slices.Contains(tc.mc.Options, util.DisableMetricsForGKE+":true") | |
| if !found { | |
| expectedArgs["prometheus-port"] = strconv.Itoa(testPrometheusPort) | |
| } | |
| // Increase port value to match behavior of prepareMountArgs() | |
| testPrometheusPort++ | |
| tc.mc.prepareMountArgs() | |
| if !reflect.DeepEqual(tc.mc.FlagMap, expectedArgs) { | |
| t.Errorf("Got args %v, but expected %v", tc.mc.FlagMap, expectedArgs) | |
| } |
Reverts #1398