Skip to content

Commit 8e180bf

Browse files
authored
Merge pull request #7309 from dlipovetsky/fix-removeownerref
🐛 Fix RemoveOwnerRef unit test to use fresh ownerRefs for each test case
2 parents 438cf49 + 889ebd1 commit 8e180bf

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

util/util_test.go

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import (
4141
func TestMachineToInfrastructureMapFunc(t *testing.T) {
4242
g := NewWithT(t)
4343

44-
var testcases = []struct {
44+
testcases := []struct {
4545
name string
4646
input schema.GroupVersionKind
4747
request client.Object
@@ -110,7 +110,7 @@ func TestMachineToInfrastructureMapFunc(t *testing.T) {
110110
}
111111

112112
func TestClusterToInfrastructureMapFunc(t *testing.T) {
113-
var testcases = []struct {
113+
testcases := []struct {
114114
name string
115115
input schema.GroupVersionKind
116116
request *clusterv1.Cluster
@@ -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 {
@@ -876,8 +878,10 @@ func TestRemoveOwnerRef(t *testing.T) {
876878
}
877879
for _, tt := range tests {
878880
t.Run(tt.name, func(t *testing.T) {
879-
newOwnerRefs := RemoveOwnerRef(ownerRefs, tt.toBeRemoved)
880-
g.Expect(HasOwnerRef(newOwnerRefs, tt.toBeRemoved)).NotTo(BeTrue())
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())
881885
})
882886
}
883887
}

0 commit comments

Comments
 (0)