@@ -17,7 +17,6 @@ package e2e2_test
1717import (
1818 "context"
1919 "os"
20- "strings"
2120 "time"
2221
2322 . "github.com/onsi/ginkgo/v2"
@@ -26,23 +25,29 @@ import (
2625 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2726 "sigs.k8s.io/controller-runtime/pkg/client"
2827
28+ k8s "github.com/crd2go/crd2go/k8s"
2929 nextapiv1 "github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/nextapi/generated/v1"
3030 "github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/version"
31- "github.com/mongodb/mongodb-atlas-kubernetes/v2/test/e2e2/flexsamples"
3231 "github.com/mongodb/mongodb-atlas-kubernetes/v2/test/helper/control"
3332 "github.com/mongodb/mongodb-atlas-kubernetes/v2/test/helper/e2e/utils"
3433 "github.com/mongodb/mongodb-atlas-kubernetes/v2/test/helper/e2e2/kube"
3534 "github.com/mongodb/mongodb-atlas-kubernetes/v2/test/helper/e2e2/operator"
3635 "github.com/mongodb/mongodb-atlas-kubernetes/v2/test/helper/e2e2/resources"
36+ "github.com/mongodb/mongodb-atlas-kubernetes/v2/test/helper/e2e2/samples"
3737 "github.com/mongodb/mongodb-atlas-kubernetes/v2/test/helper/e2e2/testparams"
38- "github.com/mongodb/mongodb-atlas-kubernetes/v2/test/helper/e2e2/yml"
3938)
4039
4140const (
4241 FlexClusterCRDName = "flexclusters.atlas.generated.mongodb.com"
4342 GroupCRDName = "groups.atlas.generated.mongodb.com"
4443)
4544
45+ // mutationFunc is a function type for mutating objects during test setup.
46+ type mutationFunc func (objs []client.Object , params * testparams.TestParams ) * nextapiv1.FlexCluster
47+
48+ // updateMutationFunc is a function type for mutating objects during test updates.
49+ type updateMutationFunc func (cluster * nextapiv1.FlexCluster )
50+
4651var _ = Describe ("FlexCluster CRUD" , Ordered , Label ("flexcluster-ctlr" ), func () {
4752 var ctx context.Context
4853 var kubeClient client.Client
@@ -82,14 +87,15 @@ var _ = Describe("FlexCluster CRUD", Ordered, Label("flexcluster-ctlr"), func()
8287
8388 By ("Create test Group" , func () {
8489 groupName := utils .RandomName ("flexcluster-test-group" )
85- // Set up shared test params for Group YAML template
90+ // Set up shared test params
8691 sharedTestParams = testparams .New (orgID , sharedGroupNamespace .Name , DefaultGlobalCredentials ).
8792 WithGroupName (groupName )
88- // Replace placeholders in the Group YAML template
89- groupYAML := sharedTestParams . ReplaceYAML ( string ( flexsamples . TestGroup ))
90- objs := yml . MustParseObjects ( strings . NewReader ( groupYAML ) )
93+
94+ // Load sample Group YAML and apply mutations
95+ objs := samples . MustLoadSampleObjects ( "atlas_generated_v1_group.yaml" )
9196 Expect (len (objs )).To (Equal (1 ))
9297 testGroup = objs [0 ].(* nextapiv1.Group )
98+ sharedTestParams .WithNamespace (sharedGroupNamespace .Name ).ApplyToGroup (testGroup )
9399 Expect (kubeClient .Create (ctx , testGroup )).To (Succeed ())
94100 })
95101
@@ -150,30 +156,45 @@ var _ = Describe("FlexCluster CRUD", Ordered, Label("flexcluster-ctlr"), func()
150156 })
151157
152158 DescribeTable ("FlexCluster CRUD lifecycle" ,
153- func (createYAML , updateYAML [] byte , clusterName string ) {
159+ func (sampleFile string , createMutation mutationFunc , updateMutation updateMutationFunc , clusterName string ) {
154160 // Generate randomized group name for this test run (cluster names are unique per group)
155161 groupName := utils .RandomName ("flex-grp" )
156162
157- // Set up test params for this test case (reuse shared values, override groupName)
158- testParams := sharedTestParams .WithGroupName (groupName )
163+ // Set up test params for this test case (reuse shared values, override groupName and namespace )
164+ testParams := sharedTestParams .WithGroupName (groupName ). WithNamespace ( testNamespace . Name )
159165
160166 // Track created objects for cleanup
161167 var createdObjects []client.Object
168+ var cluster * nextapiv1.FlexCluster
162169
163170 By ("Copy credentials secret to test namespace" , func () {
164171 Expect (resources .CopyCredentialsToNamespace (ctx , kubeClient , DefaultGlobalCredentials , control .MustEnvVar ("OPERATOR_NAMESPACE" ), testNamespace .Name , GinkGoFieldOwner )).To (Succeed ())
165172 })
166173
167- By ("Create resources from YAML" , func () {
168- objs , err := resources .ApplyYAMLToNamespace (ctx , kubeClient , createYAML , testParams , testNamespace .Name , GinkGoFieldOwner )
174+ By ("Load sample YAML and apply mutations for create" , func () {
175+ objs := samples .MustLoadSampleObjects (sampleFile )
176+
177+ // Apply create mutation function
178+ cluster = createMutation (objs , testParams )
179+
180+ // Apply all objects to namespace
181+ createdObjects , err := resources .ApplyObjectsToNamespace (ctx , kubeClient , objs , testNamespace .Name , GinkGoFieldOwner )
169182 Expect (err ).NotTo (HaveOccurred ())
170- createdObjects = append (createdObjects , objs ... )
183+
184+ // Find cluster object for later use if not returned by mutation
185+ if cluster == nil {
186+ for _ , obj := range createdObjects {
187+ if fc , ok := obj .(* nextapiv1.FlexCluster ); ok {
188+ cluster = fc
189+ break
190+ }
191+ }
192+ }
171193 })
172194
173195 By ("Wait for Group to be Ready (if using groupRef)" , func () {
174- createYAMLStr := testParams .ReplaceYAML (string (createYAML ))
175- objs := yml .MustParseObjects (strings .NewReader (createYAMLStr ))
176- for _ , obj := range objs {
196+ // Check if any Group objects were created
197+ for _ , obj := range createdObjects {
177198 if group , ok := obj .(* nextapiv1.Group ); ok {
178199 groupObj := & nextapiv1.Group {
179200 ObjectMeta : metav1.ObjectMeta {Name : group .Name , Namespace : testNamespace .Name },
@@ -185,13 +206,9 @@ var _ = Describe("FlexCluster CRUD", Ordered, Label("flexcluster-ctlr"), func()
185206 }
186207 })
187208
188- cluster := nextapiv1.FlexCluster {
189- ObjectMeta : metav1.ObjectMeta {Name : clusterName , Namespace : testNamespace .Name },
190- }
191-
192209 By ("Wait for FlexCluster to be Ready" , func () {
193210 Eventually (func (g Gomega ) {
194- g .Expect (resources .CheckResourceReady (ctx , kubeClient , & cluster )).To (Succeed ())
211+ g .Expect (resources .CheckResourceReady (ctx , kubeClient , cluster )).To (Succeed ())
195212 }).WithTimeout (5 * time .Minute ).WithPolling (5 * time .Second ).Should (Succeed ())
196213 })
197214
@@ -202,18 +219,15 @@ var _ = Describe("FlexCluster CRUD", Ordered, Label("flexcluster-ctlr"), func()
202219 })
203220
204221 By ("Update FlexCluster" , func () {
205- if len (updateYAML ) > 0 {
206- _ , err := resources .ApplyYAMLToNamespace (ctx , kubeClient , updateYAML , testParams , testNamespace .Name , GinkGoFieldOwner )
207- Expect (err ).NotTo (HaveOccurred ())
208- }
222+ // Apply update mutation to the same cluster object
223+ updateMutation (cluster )
224+ Expect (kubeClient .Patch (ctx , cluster , client .Apply , client .ForceOwnership , GinkGoFieldOwner )).To (Succeed ())
209225 })
210226
211227 By ("Wait for FlexCluster to be Ready & updated" , func () {
212- if len (updateYAML ) > 0 {
213- Eventually (func (g Gomega ) {
214- g .Expect (resources .CheckResourceUpdated (ctx , kubeClient , & cluster )).To (Succeed ())
215- }).WithTimeout (5 * time .Minute ).WithPolling (5 * time .Second ).Should (Succeed ())
216- }
228+ Eventually (func (g Gomega ) {
229+ g .Expect (resources .CheckResourceUpdated (ctx , kubeClient , cluster )).To (Succeed ())
230+ }).WithTimeout (5 * time .Minute ).WithPolling (5 * time .Second ).Should (Succeed ())
217231 })
218232
219233 By ("Delete all created resources" , func () {
@@ -231,14 +245,87 @@ var _ = Describe("FlexCluster CRUD", Ordered, Label("flexcluster-ctlr"), func()
231245 })
232246 },
233247 Entry ("With direct groupId" ,
234- flexsamples .WithGroupIdCreate ,
235- flexsamples .WithGroupIdUpdate ,
248+ "atlas_generated_v1_flexcluster.yaml" ,
249+ mutateFlexClusterWithGroupId ,
250+ updateFlexClusterTerminationProtection ,
236251 "flexy" ,
237252 ),
238253 Entry ("With groupRef" ,
239- flexsamples .WithGroupRefCreate ,
240- flexsamples .WithGroupRefUpdate ,
254+ "atlas_generated_v1_flexcluster_with_groupref.yaml" ,
255+ mutateFlexClusterWithGroupRef ,
256+ updateFlexClusterTerminationProtection ,
241257 "flexy" ,
242258 ),
243259 )
244260})
261+
262+ // mutateFlexClusterWithGroupId mutates a FlexCluster object to use direct groupId.
263+ // Returns the mutated FlexCluster if found, nil otherwise.
264+ func mutateFlexClusterWithGroupId (objs []client.Object , params * testparams.TestParams ) * nextapiv1.FlexCluster {
265+ for _ , obj := range objs {
266+ if cluster , ok := obj .(* nextapiv1.FlexCluster ); ok {
267+ cluster .SetNamespace (params .Namespace )
268+
269+ if cluster .Spec .ConnectionSecretRef == nil {
270+ cluster .Spec .ConnectionSecretRef = & k8s.LocalReference {}
271+ }
272+ cluster .Spec .ConnectionSecretRef .Name = params .CredentialsSecretName
273+
274+ if cluster .Spec .V20250312 == nil {
275+ cluster .Spec .V20250312 = & nextapiv1.FlexClusterSpecV20250312 {}
276+ }
277+ if params .GroupID != "" {
278+ cluster .Spec .V20250312 .GroupId = & params .GroupID
279+ // Clear groupRef if groupId is set
280+ cluster .Spec .V20250312 .GroupRef = nil
281+ }
282+ return cluster
283+ }
284+ }
285+ return nil
286+ }
287+
288+ // mutateFlexClusterWithGroupRef mutates a FlexCluster object to use groupRef.
289+ // This also mutates any Group objects in the same list to use test params.
290+ // Returns the mutated FlexCluster if found, nil otherwise.
291+ func mutateFlexClusterWithGroupRef (objs []client.Object , params * testparams.TestParams ) * nextapiv1.FlexCluster {
292+ var cluster * nextapiv1.FlexCluster
293+ for _ , obj := range objs {
294+ switch o := obj .(type ) {
295+ case * nextapiv1.Group :
296+ params .ApplyToGroup (o )
297+ case * nextapiv1.FlexCluster :
298+ o .SetNamespace (params .Namespace )
299+
300+ if o .Spec .ConnectionSecretRef == nil {
301+ o .Spec .ConnectionSecretRef = & k8s.LocalReference {}
302+ }
303+ o .Spec .ConnectionSecretRef .Name = params .CredentialsSecretName
304+
305+ if o .Spec .V20250312 == nil {
306+ o .Spec .V20250312 = & nextapiv1.FlexClusterSpecV20250312 {}
307+ }
308+ if o .Spec .V20250312 .GroupRef == nil {
309+ o .Spec .V20250312 .GroupRef = & k8s.LocalReference {}
310+ }
311+ o .Spec .V20250312 .GroupRef .Name = params .GroupName
312+ // Clear groupId if groupRef is set
313+ o .Spec .V20250312 .GroupId = nil
314+ cluster = o
315+ }
316+ }
317+ return cluster
318+ }
319+
320+ // updateFlexClusterTerminationProtection mutates a FlexCluster for the update scenario.
321+ // This changes terminationProtectionEnabled from true to false.
322+ func updateFlexClusterTerminationProtection (cluster * nextapiv1.FlexCluster ) {
323+ if cluster .Spec .V20250312 == nil {
324+ cluster .Spec .V20250312 = & nextapiv1.FlexClusterSpecV20250312 {}
325+ }
326+ if cluster .Spec .V20250312 .Entry == nil {
327+ cluster .Spec .V20250312 .Entry = & nextapiv1.FlexClusterSpecV20250312Entry {}
328+ }
329+ terminationProtectionEnabled := false
330+ cluster .Spec .V20250312 .Entry .TerminationProtectionEnabled = & terminationProtectionEnabled
331+ }
0 commit comments