Skip to content

Commit 70069a0

Browse files
Use GenerateName in controller tests to avoid name collisions
Replace hardcoded resource names with GenerateName-based dynamic names in all envtest controller tests. This eliminates occasional 409 conflict errors.
1 parent 43348c0 commit 70069a0

26 files changed

Lines changed: 1744 additions & 1745 deletions

internal/controller/cisco/nx/bordergateway_controller_test.go

Lines changed: 43 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
. "github.com/onsi/ginkgo/v2"
1010
. "github.com/onsi/gomega"
11-
"k8s.io/apimachinery/pkg/api/errors"
1211
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1312
"sigs.k8s.io/controller-runtime/pkg/client"
1413
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
@@ -19,66 +18,61 @@ import (
1918

2019
var _ = Describe("BorderGateway Controller", func() {
2120
Context("When reconciling a resource", func() {
22-
const name = "test-bgw"
23-
key := client.ObjectKey{Name: name, Namespace: metav1.NamespaceDefault}
21+
var (
22+
name string
23+
key client.ObjectKey
24+
)
2425

2526
BeforeEach(func() {
2627
By("Creating the custom resource for the Kind Device")
27-
device := &v1alpha1.Device{}
28-
if err := k8sClient.Get(ctx, key, device); errors.IsNotFound(err) {
29-
resource := &v1alpha1.Device{
30-
ObjectMeta: metav1.ObjectMeta{
31-
Name: name,
32-
Namespace: metav1.NamespaceDefault,
28+
device := &v1alpha1.Device{
29+
ObjectMeta: metav1.ObjectMeta{
30+
GenerateName: "test-bgw-",
31+
Namespace: metav1.NamespaceDefault,
32+
},
33+
Spec: v1alpha1.DeviceSpec{
34+
Endpoint: v1alpha1.Endpoint{
35+
Address: "192.168.10.2:9339",
3336
},
34-
Spec: v1alpha1.DeviceSpec{
35-
Endpoint: v1alpha1.Endpoint{
36-
Address: "192.168.10.2:9339",
37-
},
38-
},
39-
}
40-
Expect(k8sClient.Create(ctx, resource)).To(Succeed())
37+
},
4138
}
39+
Expect(k8sClient.Create(ctx, device)).To(Succeed())
40+
name = device.Name
41+
key = client.ObjectKey{Name: name, Namespace: metav1.NamespaceDefault}
4242

4343
By("Creating the custom resource for the Kind Interface")
44-
intf := &v1alpha1.Interface{}
45-
if err := k8sClient.Get(ctx, key, intf); errors.IsNotFound(err) {
46-
resource := &v1alpha1.Interface{
47-
ObjectMeta: metav1.ObjectMeta{
48-
Name: name,
49-
Namespace: metav1.NamespaceDefault,
50-
},
51-
Spec: v1alpha1.InterfaceSpec{
52-
DeviceRef: v1alpha1.LocalObjectReference{Name: name},
53-
Name: "lo100",
54-
AdminState: v1alpha1.AdminStateUp,
55-
Description: "Test",
56-
MTU: 1500,
57-
Type: v1alpha1.InterfaceTypeLoopback,
58-
IPv4: &v1alpha1.InterfaceIPv4{
59-
Addresses: []v1alpha1.IPPrefix{{Prefix: netip.MustParsePrefix("10.0.0.1/32")}},
60-
},
44+
intf := &v1alpha1.Interface{
45+
ObjectMeta: metav1.ObjectMeta{
46+
Name: name,
47+
Namespace: metav1.NamespaceDefault,
48+
},
49+
Spec: v1alpha1.InterfaceSpec{
50+
DeviceRef: v1alpha1.LocalObjectReference{Name: name},
51+
Name: "lo100",
52+
AdminState: v1alpha1.AdminStateUp,
53+
Description: "Test",
54+
MTU: 1500,
55+
Type: v1alpha1.InterfaceTypeLoopback,
56+
IPv4: &v1alpha1.InterfaceIPv4{
57+
Addresses: []v1alpha1.IPPrefix{{Prefix: netip.MustParsePrefix("10.0.0.1/32")}},
6158
},
62-
}
63-
Expect(k8sClient.Create(ctx, resource)).To(Succeed())
59+
},
6460
}
61+
Expect(k8sClient.Create(ctx, intf)).To(Succeed())
6562

6663
By("Creating the custom resource for the Kind BorderGateway")
67-
bgw := &nxv1alpha1.BorderGateway{}
68-
if err := k8sClient.Get(ctx, key, bgw); errors.IsNotFound(err) {
69-
resource := &nxv1alpha1.BorderGateway{
70-
ObjectMeta: metav1.ObjectMeta{
71-
Name: name,
72-
Namespace: metav1.NamespaceDefault,
73-
},
74-
Spec: nxv1alpha1.BorderGatewaySpec{
75-
DeviceRef: v1alpha1.LocalObjectReference{Name: name},
76-
MultisiteID: 123,
77-
SourceInterfaceRef: v1alpha1.LocalObjectReference{Name: name},
78-
},
79-
}
80-
Expect(k8sClient.Create(ctx, resource)).To(Succeed())
64+
bgw := &nxv1alpha1.BorderGateway{
65+
ObjectMeta: metav1.ObjectMeta{
66+
Name: name,
67+
Namespace: metav1.NamespaceDefault,
68+
},
69+
Spec: nxv1alpha1.BorderGatewaySpec{
70+
DeviceRef: v1alpha1.LocalObjectReference{Name: name},
71+
MultisiteID: 123,
72+
SourceInterfaceRef: v1alpha1.LocalObjectReference{Name: name},
73+
},
8174
}
75+
Expect(k8sClient.Create(ctx, bgw)).To(Succeed())
8276
})
8377

8478
AfterEach(func() {

internal/controller/cisco/nx/system_controller_test.go

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package nx
66
import (
77
. "github.com/onsi/ginkgo/v2"
88
. "github.com/onsi/gomega"
9-
"k8s.io/apimachinery/pkg/api/errors"
109
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1110
"sigs.k8s.io/controller-runtime/pkg/client"
1211
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
@@ -17,44 +16,42 @@ import (
1716

1817
var _ = Describe("System Controller", func() {
1918
Context("When reconciling a resource", func() {
20-
const name = "test-system"
21-
key := client.ObjectKey{Name: name, Namespace: metav1.NamespaceDefault}
19+
var (
20+
name string
21+
key client.ObjectKey
22+
)
2223

2324
BeforeEach(func() {
2425
By("Creating the custom resource for the Kind Device")
25-
device := &v1alpha1.Device{}
26-
if err := k8sClient.Get(ctx, key, device); errors.IsNotFound(err) {
27-
resource := &v1alpha1.Device{
28-
ObjectMeta: metav1.ObjectMeta{
29-
Name: name,
30-
Namespace: metav1.NamespaceDefault,
26+
device := &v1alpha1.Device{
27+
ObjectMeta: metav1.ObjectMeta{
28+
GenerateName: "test-system-",
29+
Namespace: metav1.NamespaceDefault,
30+
},
31+
Spec: v1alpha1.DeviceSpec{
32+
Endpoint: v1alpha1.Endpoint{
33+
Address: "192.168.10.2:9339",
3134
},
32-
Spec: v1alpha1.DeviceSpec{
33-
Endpoint: v1alpha1.Endpoint{
34-
Address: "192.168.10.2:9339",
35-
},
36-
},
37-
}
38-
Expect(k8sClient.Create(ctx, resource)).To(Succeed())
35+
},
3936
}
37+
Expect(k8sClient.Create(ctx, device)).To(Succeed())
38+
name = device.Name
39+
key = client.ObjectKey{Name: name, Namespace: metav1.NamespaceDefault}
4040

4141
By("Creating the custom resource for the Kind System")
42-
system := &nxv1alpha1.System{}
43-
if err := k8sClient.Get(ctx, key, system); errors.IsNotFound(err) {
44-
resource := &nxv1alpha1.System{
45-
ObjectMeta: metav1.ObjectMeta{
46-
Name: name,
47-
Namespace: metav1.NamespaceDefault,
48-
},
49-
Spec: nxv1alpha1.SystemSpec{
50-
DeviceRef: v1alpha1.LocalObjectReference{Name: name},
51-
JumboMTU: 9216,
52-
ReservedVlan: 3986,
53-
VlanLongName: true,
54-
},
55-
}
56-
Expect(k8sClient.Create(ctx, resource)).To(Succeed())
42+
resource := &nxv1alpha1.System{
43+
ObjectMeta: metav1.ObjectMeta{
44+
Name: name,
45+
Namespace: metav1.NamespaceDefault,
46+
},
47+
Spec: nxv1alpha1.SystemSpec{
48+
DeviceRef: v1alpha1.LocalObjectReference{Name: name},
49+
JumboMTU: 9216,
50+
ReservedVlan: 3986,
51+
VlanLongName: true,
52+
},
5753
}
54+
Expect(k8sClient.Create(ctx, resource)).To(Succeed())
5855
})
5956

6057
AfterEach(func() {

0 commit comments

Comments
 (0)