Skip to content

Commit

Permalink
refactor: append test flag to shared test description
Browse files Browse the repository at this point in the history
- so that the failed test is easy to identify
- shallow copy tests and add comments so that others do the same
Signed-off-by: vadasambar <[email protected]>
  • Loading branch information
vadasambar committed Mar 22, 2023
1 parent d3c0b55 commit dee5eae
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions cluster-autoscaler/utils/drain/drain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package drain

import (
"fmt"
"testing"
"time"

Expand Down Expand Up @@ -645,13 +646,26 @@ func TestDrain(t *testing.T) {
}

allTests := []testOpts{}
// Note: be careful about modifying the underlying reference values for sharedTest
// since they are shared (changing it once will change it for all shallow copies of sharedTest)
for _, sharedTest := range sharedTests {
// make sure you shallow copy the test like this
// before you modify it
// (so that modifying one test doesn't affect another)
enabledTest := sharedTest
disabledTest := sharedTest

// to execute the same shared tests for when the skipNodesWithCustomControllerPods flag is true
// and when the flag is false
sharedTest.skipNodesWithCustomControllerPods = true
allTests = append(allTests, sharedTest)
sharedTest.skipNodesWithCustomControllerPods = false
allTests = append(allTests, sharedTest)
enabledTest.skipNodesWithCustomControllerPods = true
enabledTest.description = fmt.Sprintf("%s with skipNodesWithCustomControllerPods:%v",
enabledTest.description, enabledTest.skipNodesWithCustomControllerPods)
allTests = append(allTests, enabledTest)

disabledTest.skipNodesWithCustomControllerPods = false
disabledTest.description = fmt.Sprintf("%s with skipNodesWithCustomControllerPods:%v",
disabledTest.description, disabledTest.skipNodesWithCustomControllerPods)
allTests = append(allTests, disabledTest)
}

allTests = append(allTests, testOpts{
Expand Down Expand Up @@ -703,19 +717,19 @@ func TestDrain(t *testing.T) {
if test.expectFatal {
assert.Equal(t, test.expectBlockingPod, blockingPod)
if err == nil {
t.Fatalf("%s: unexpected non-error, skipNodesWithCustomControllerPods: %v", test.description, test.skipNodesWithCustomControllerPods)
t.Fatalf("%s: unexpected non-error", test.description)
}
}

if !test.expectFatal {
assert.Nil(t, blockingPod)
if err != nil {
t.Fatalf("%s: error occurred: %v, skipNodesWithCustomControllerPods: %v", test.description, err, test.skipNodesWithCustomControllerPods)
t.Fatalf("%s: error occurred: %v", test.description, err)
}
}

if len(pods) != len(test.expectPods) {
t.Fatalf("Wrong pod list content: %v, skipNodesWithCustomControllerPods: %v", test.description, test.skipNodesWithCustomControllerPods)
t.Fatalf("Wrong pod list content: %v", test.description)
}

assert.ElementsMatch(t, test.expectDaemonSetPods, daemonSetPods)
Expand Down

0 comments on commit dee5eae

Please sign in to comment.