Skip to content

Commit 889ebd1

Browse files
committed
Fix RemoveOwnerRef unit test to use fresh ownerRefs for each test case
Since RemoveOwnerRef may modify the underlying array, the same ownerRefs should not be used for different test cases.
1 parent 938359d commit 889ebd1

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

util/util_test.go

+17-18
Original file line numberDiff line numberDiff line change
@@ -840,17 +840,19 @@ func TestIsSupportedVersionSkew(t *testing.T) {
840840

841841
func TestRemoveOwnerRef(t *testing.T) {
842842
g := NewWithT(t)
843-
ownerRefs := []metav1.OwnerReference{
844-
{
845-
APIVersion: "dazzlings.info/v1",
846-
Kind: "Twilight",
847-
Name: "m4g1c",
848-
},
849-
{
850-
APIVersion: "bar.cluster.x-k8s.io/v1beta1",
851-
Kind: "TestCluster",
852-
Name: "bar-1",
853-
},
843+
makeOwnerRefs := func() []metav1.OwnerReference {
844+
return []metav1.OwnerReference{
845+
{
846+
APIVersion: "dazzlings.info/v1",
847+
Kind: "Twilight",
848+
Name: "m4g1c",
849+
},
850+
{
851+
APIVersion: "bar.cluster.x-k8s.io/v1beta1",
852+
Kind: "TestCluster",
853+
Name: "bar-1",
854+
},
855+
}
854856
}
855857

856858
tests := []struct {
@@ -874,15 +876,12 @@ func TestRemoveOwnerRef(t *testing.T) {
874876
},
875877
},
876878
}
877-
878-
originalOwnerRefs := append([]metav1.OwnerReference{}, ownerRefs...)
879879
for _, tt := range tests {
880880
t.Run(tt.name, func(t *testing.T) {
881-
result := RemoveOwnerRef(ownerRefs, tt.toBeRemoved)
882-
// RemoveOwnerRef should remove the owner ref, if it is found.
883-
g.Expect(HasOwnerRef(result, tt.toBeRemoved)).NotTo(BeTrue())
884-
// RemoveOwnerRef should not mutate its input.
885-
g.Expect(ownerRefs).To(Equal(originalOwnerRefs), "RemoveOwnerRef mutated its input")
881+
// Use a fresh ownerRefs slice for each test, because RemoveOwnerRef may modify the underlying array.
882+
ownerRefs := makeOwnerRefs()
883+
ownerRefs = RemoveOwnerRef(ownerRefs, tt.toBeRemoved)
884+
g.Expect(HasOwnerRef(ownerRefs, tt.toBeRemoved)).NotTo(BeTrue())
886885
})
887886
}
888887
}

0 commit comments

Comments
 (0)