Skip to content

Commit 5ccbafc

Browse files
committed
Fix the new e2e2 test
1 parent 19cbe80 commit 5ccbafc

File tree

5 files changed

+55
-26
lines changed

5 files changed

+55
-26
lines changed

test/e2e2/flexcluster_test.go

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ var _ = Describe("FlexCluster CRUD", Ordered, Label("flexcluster-ctlr"), func()
5050
var kubeClient client.Client
5151
var ako operator.Operator
5252
var testNamespace *corev1.Namespace
53+
var sharedGroupNamespace *corev1.Namespace
5354
var testGroup *nextapiv1.Group
5455
var groupID string
5556
var orgID string
@@ -79,12 +80,28 @@ var _ = Describe("FlexCluster CRUD", Ordered, Label("flexcluster-ctlr"), func()
7980
},
8081
)).To(Succeed())
8182

83+
By("Create namespace and credentials for shared test Group", func() {
84+
sharedGroupNamespace = &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{
85+
Name: utils.RandomName("flex-shared-grp-ns"),
86+
}}
87+
Expect(kubeClient.Create(ctx, sharedGroupNamespace)).To(Succeed())
88+
89+
globalCredsKey := client.ObjectKey{
90+
Name: DefaultGlobalCredentials,
91+
Namespace: control.MustEnvVar("OPERATOR_NAMESPACE"),
92+
}
93+
credentialsSecret, err := copySecretToNamespace(ctx, kubeClient, globalCredsKey, sharedGroupNamespace.Name)
94+
Expect(err).NotTo(HaveOccurred())
95+
Expect(
96+
kubeClient.Patch(ctx, credentialsSecret, client.Apply, client.ForceOwnership, GinkGoFieldOwner),
97+
).To(Succeed())
98+
})
99+
82100
By("Create test Group", func() {
83-
operatorNamespace := control.MustEnvVar("OPERATOR_NAMESPACE")
84101
groupName := utils.RandomName("flexcluster-test-group")
85102
// Replace placeholders in the Group YAML template
86103
groupYAML := strings.ReplaceAll(string(flexsamples.TestGroup), "__GROUP_NAME__", groupName)
87-
groupYAML = strings.ReplaceAll(groupYAML, "__OPERATOR_NAMESPACE__", operatorNamespace)
104+
groupYAML = strings.ReplaceAll(groupYAML, "__OPERATOR_NAMESPACE__", sharedGroupNamespace.Name)
88105
groupYAML = strings.ReplaceAll(groupYAML, "__CREDENTIALS_SECRET_NAME__", DefaultGlobalCredentials)
89106
groupYAML = strings.ReplaceAll(groupYAML, "__ORG_ID__", orgID)
90107
objs := yml.MustParseObjects(strings.NewReader(groupYAML))
@@ -122,6 +139,14 @@ var _ = Describe("FlexCluster CRUD", Ordered, Label("flexcluster-ctlr"), func()
122139
}).WithTimeout(5 * time.Minute).WithPolling(5 * time.Second).NotTo(Succeed())
123140
})
124141
}
142+
if kubeClient != nil && sharedGroupNamespace != nil {
143+
By("Clean up shared group namespace", func() {
144+
Expect(kubeClient.Delete(ctx, sharedGroupNamespace)).To(Succeed())
145+
Eventually(func(g Gomega) bool {
146+
return kubeClient.Get(ctx, client.ObjectKeyFromObject(sharedGroupNamespace), sharedGroupNamespace) == nil
147+
}).WithTimeout(time.Minute).WithPolling(time.Second).To(BeFalse())
148+
})
149+
}
125150
if ako != nil {
126151
ako.Stop(GinkgoT())
127152
}
@@ -149,6 +174,12 @@ var _ = Describe("FlexCluster CRUD", Ordered, Label("flexcluster-ctlr"), func()
149174

150175
DescribeTable("FlexCluster CRUD lifecycle",
151176
func(createYAML, updateYAML []byte, clusterName string) {
177+
// Generate randomized group name for this test run (cluster names are unique per group)
178+
groupName := utils.RandomName("flex-grp")
179+
180+
// Track created objects for cleanup
181+
var createdObjects []client.Object
182+
152183
By("Copy credentials secret to test namespace", func() {
153184
globalCredsKey := client.ObjectKey{
154185
Name: DefaultGlobalCredentials,
@@ -165,18 +196,21 @@ var _ = Describe("FlexCluster CRUD", Ordered, Label("flexcluster-ctlr"), func()
165196
// Replace placeholders with actual values
166197
createYAMLStr := strings.ReplaceAll(string(createYAML), "__GROUP_ID__", groupID)
167198
createYAMLStr = strings.ReplaceAll(createYAMLStr, "__ORG_ID__", orgID)
199+
createYAMLStr = strings.ReplaceAll(createYAMLStr, "__GROUP_NAME__", groupName)
168200
objs := yml.MustParseObjects(strings.NewReader(createYAMLStr))
169201
for _, obj := range objs {
170-
objToApply := kube.WithRenamedNamespace(obj, testNamespace.Name)
202+
obj.SetNamespace(testNamespace.Name)
171203
Expect(
172-
kubeClient.Patch(ctx, objToApply, client.Apply, client.ForceOwnership, GinkGoFieldOwner),
204+
kubeClient.Patch(ctx, obj, client.Apply, client.ForceOwnership, GinkGoFieldOwner),
173205
).To(Succeed())
206+
createdObjects = append(createdObjects, obj)
174207
}
175208
})
176209

177210
By("Wait for Group to be Ready (if using groupRef)", func() {
178211
createYAMLStr := strings.ReplaceAll(string(createYAML), "__GROUP_ID__", groupID)
179212
createYAMLStr = strings.ReplaceAll(createYAMLStr, "__ORG_ID__", orgID)
213+
createYAMLStr = strings.ReplaceAll(createYAMLStr, "__GROUP_NAME__", groupName)
180214
objs := yml.MustParseObjects(strings.NewReader(createYAMLStr))
181215
for _, obj := range objs {
182216
if group, ok := obj.(*nextapiv1.Group); ok {
@@ -223,11 +257,12 @@ var _ = Describe("FlexCluster CRUD", Ordered, Label("flexcluster-ctlr"), func()
223257
// Replace placeholders with actual values
224258
updateYAMLStr := strings.ReplaceAll(string(updateYAML), "__GROUP_ID__", groupID)
225259
updateYAMLStr = strings.ReplaceAll(updateYAMLStr, "__ORG_ID__", orgID)
260+
updateYAMLStr = strings.ReplaceAll(updateYAMLStr, "__GROUP_NAME__", groupName)
226261
updateObjs := yml.MustParseObjects(strings.NewReader(updateYAMLStr))
227262
for _, obj := range updateObjs {
228-
objToPatch := kube.WithRenamedNamespace(obj, testNamespace.Name)
263+
obj.SetNamespace(testNamespace.Name)
229264
Expect(
230-
kubeClient.Patch(ctx, objToPatch, client.Apply, client.ForceOwnership, GinkGoFieldOwner),
265+
kubeClient.Patch(ctx, obj, client.Apply, client.ForceOwnership, GinkGoFieldOwner),
231266
).To(Succeed())
232267
}
233268
}
@@ -253,15 +288,18 @@ var _ = Describe("FlexCluster CRUD", Ordered, Label("flexcluster-ctlr"), func()
253288
}
254289
})
255290

256-
By("Delete FlexCluster", func() {
257-
Expect(kubeClient.Delete(ctx, &cluster)).To(Succeed())
291+
By("Delete all created resources", func() {
292+
for _, obj := range createdObjects {
293+
_ = kubeClient.Delete(ctx, obj)
294+
}
258295
})
259296

260-
By("Wait for FlexCluster to be deleted", func() {
261-
Eventually(func(g Gomega) error {
262-
err := kubeClient.Get(ctx, client.ObjectKeyFromObject(&cluster), &cluster)
263-
return err
264-
}).WithTimeout(5 * time.Minute).WithPolling(5 * time.Second).NotTo(Succeed())
297+
By("Wait for all resources to be deleted", func() {
298+
for _, obj := range createdObjects {
299+
Eventually(func(g Gomega) error {
300+
return kubeClient.Get(ctx, client.ObjectKeyFromObject(obj), obj)
301+
}).WithTimeout(5 * time.Minute).WithPolling(5 * time.Second).ShouldNot(Succeed())
302+
}
265303
})
266304
},
267305
Entry("With direct groupId",

test/e2e2/flexsamples/with_groupid_create.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ apiVersion: atlas.generated.mongodb.com/v1
22
kind: FlexCluster
33
metadata:
44
name: flexy
5-
namespace: mongodb-atlas-system
65
spec:
76
connectionSecretRef:
87
name: mongodb-atlas-operator-api-key
@@ -14,4 +13,3 @@ spec:
1413
providerSettings:
1514
backingProviderName: GCP
1615
regionName: CENTRAL_US
17-

test/e2e2/flexsamples/with_groupid_update.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ apiVersion: atlas.generated.mongodb.com/v1
22
kind: FlexCluster
33
metadata:
44
name: flexy
5-
namespace: mongodb-atlas-system
65
spec:
76
connectionSecretRef:
87
name: mongodb-atlas-operator-api-key
@@ -14,4 +13,3 @@ spec:
1413
providerSettings:
1514
backingProviderName: GCP
1615
regionName: CENTRAL_US
17-
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,28 @@
11
apiVersion: atlas.generated.mongodb.com/v1
22
kind: Group
33
metadata:
4-
name: test-flexy
5-
namespace: mongodb-atlas-system
4+
name: __GROUP_NAME__
65
spec:
76
connectionSecretRef:
87
name: mongodb-atlas-operator-api-key
98
v20250312:
109
entry:
1110
orgId: __ORG_ID__
12-
name: test-flexy
11+
name: __GROUP_NAME__
1312
---
1413
apiVersion: atlas.generated.mongodb.com/v1
1514
kind: FlexCluster
1615
metadata:
1716
name: flexy
18-
namespace: mongodb-atlas-system
1917
annotations:
2018
some-tag: tag
2119
spec:
2220
v20250312:
2321
groupRef:
24-
name: test-flexy
22+
name: __GROUP_NAME__
2523
entry:
2624
name: flexy
2725
terminationProtectionEnabled: true
2826
providerSettings:
2927
backingProviderName: GCP
3028
regionName: CENTRAL_US
31-

test/e2e2/flexsamples/with_groupref_update.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,17 @@ apiVersion: atlas.generated.mongodb.com/v1
22
kind: FlexCluster
33
metadata:
44
name: flexy
5-
namespace: mongodb-atlas-system
65
annotations:
76
some-tag: tag
87
spec:
98
connectionSecretRef:
109
name: mongodb-atlas-operator-api-key
1110
v20250312:
1211
groupRef:
13-
name: test-flexy
12+
name: __GROUP_NAME__
1413
entry:
1514
name: flexy
1615
terminationProtectionEnabled: false
1716
providerSettings:
1817
backingProviderName: GCP
1918
regionName: CENTRAL_US
20-

0 commit comments

Comments
 (0)