Skip to content

Commit 3ea89a2

Browse files
authored
CLOUDP-299865: Tests for Flex Connection Secrets (#2121)
1 parent 77aaffc commit 3ea89a2

File tree

4 files changed

+81
-30
lines changed

4 files changed

+81
-30
lines changed

api/v1/atlasdeployment_types.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,8 @@ func (c *AtlasDeployment) WithAtlasName(name string) *AtlasDeployment {
693693
c.Spec.DeploymentSpec.Name = name
694694
} else if c.Spec.ServerlessSpec != nil {
695695
c.Spec.ServerlessSpec.Name = name
696+
} else if c.Spec.FlexSpec != nil {
697+
c.Spec.FlexSpec.Name = name
696698
}
697699
return c
698700
}
@@ -835,13 +837,23 @@ func NewDefaultAWSServerlessInstance(namespace, projectName string) *AtlasDeploy
835837
func NewDefaultAWSFlexInstance(namespace, projectName string) *AtlasDeployment {
836838
return newFlexInstance(
837839
namespace,
838-
"test-flex-instance-k8s",
839-
"test-flex-instance",
840+
"test-flex-instance-aws-k8s",
841+
"test-flex-instance-aws",
840842
"AWS",
841843
"US_EAST_1",
842844
).WithProjectName(projectName)
843845
}
844846

847+
func NewDefaultAzureFlexInstance(namespace, projectName string) *AtlasDeployment {
848+
return newFlexInstance(
849+
namespace,
850+
"test-flex-instance-az-k8s",
851+
"test-flex-instance-az",
852+
"AZURE",
853+
"US_EAST_2",
854+
).WithProjectName(projectName)
855+
}
856+
845857
func (c *AtlasDeployment) AtlasName() string {
846858
if c.Spec.DeploymentSpec != nil {
847859
return c.Spec.DeploymentSpec.Name

internal/translation/deployment/conversion.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,6 +1167,7 @@ func flexFromAtlas(instance *adminv20241113001.FlexClusterDescription20241113) *
11671167
State: instance.GetStateName(),
11681168
MongoDBVersion: instance.GetMongoDBVersion(),
11691169
Connection: &status.ConnectionStrings{
1170+
Standard: connectionStrings.GetStandard(),
11701171
StandardSrv: connectionStrings.GetStandardSrv(),
11711172
},
11721173
}

test/int/databaseuser_protected_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ var _ = Describe("Atlas Database User", Label("int", "AtlasDatabaseUser", "prote
5454
})
5555

5656
By("Creating a deployment", func() {
57-
testDeployment = akov2.DefaultAWSDeployment(testNamespace.Name, projectName).Lightweight()
57+
testDeployment = akov2.NewDefaultAWSFlexInstance(testNamespace.Name, projectName)
5858
customresource.SetAnnotation( // this test deployment must be deleted
5959
testDeployment,
6060
customresource.ResourcePolicyAnnotation,
@@ -305,8 +305,8 @@ var _ = Describe("Atlas Database User", Label("int", "AtlasDatabaseUser", "prote
305305
Expect(k8sClient.Delete(context.Background(), testDeployment)).To(Succeed())
306306

307307
Eventually(func() bool {
308-
_, r, err := atlasClient.ClustersApi.
309-
GetCluster(context.Background(), testProject.ID(), deploymentName).
308+
_, r, err := atlasClientv20241113001.FlexClustersApi.
309+
GetFlexCluster(context.Background(), testProject.ID(), deploymentName).
310310
Execute()
311311
if err != nil {
312312
if r != nil && r.StatusCode == http.StatusNotFound {

test/int/databaseuser_unprotected_test.go

Lines changed: 63 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/mongodb/mongodb-atlas-kubernetes/v2/test/helper/conditions"
3232
"github.com/mongodb/mongodb-atlas-kubernetes/v2/test/helper/events"
3333
"github.com/mongodb/mongodb-atlas-kubernetes/v2/test/helper/resources"
34+
"github.com/mongodb/mongodb-atlas-kubernetes/v2/test/helper/retry"
3435
)
3536

3637
const (
@@ -74,7 +75,8 @@ var _ = Describe("Atlas Database User", Label("int", "AtlasDatabaseUser", "prote
7475
})
7576

7677
By("Creating a deployment", func() {
77-
testDeployment = akov2.DefaultAWSDeployment(testNamespace.Name, projectName).Lightweight()
78+
testDeployment = akov2.NewDefaultAWSFlexInstance(testNamespace.Name, projectName).
79+
WithName("test-flex-deployment").WithAtlasName("test-flex-deployment")
7880
Expect(k8sClient.Create(context.Background(), testDeployment)).To(Succeed())
7981

8082
Eventually(func() bool {
@@ -276,7 +278,7 @@ var _ = Describe("Atlas Database User", Label("int", "AtlasDatabaseUser", "prote
276278
})
277279
})
278280

279-
It("Adds connection secret when new deployment is created", Label("user-add-secret"), func() {
281+
It("Adds connection secret when new deployment is created with an existing user", Label("user-add-secret"), func() {
280282
secondDeployment := &akov2.AtlasDeployment{}
281283

282284
By("Creating a database user for existing deployment only", func() {
@@ -298,13 +300,13 @@ var _ = Describe("Atlas Database User", Label("int", "AtlasDatabaseUser", "prote
298300
Expect(tryConnect(testProject.ID(), *testDeployment, *testDBUser1)).Should(Succeed())
299301
})
300302

301-
By("Creating a second deployment", func() {
302-
secondDeployment = akov2.DefaultAzureDeployment(testNamespace.Name, projectName).Lightweight()
303+
By("Creating a second deployment", func() {
304+
secondDeployment = akov2.NewDefaultAzureFlexInstance(testNamespace.Name, projectName)
303305
Expect(k8sClient.Create(context.Background(), secondDeployment)).To(Succeed())
304306

305307
Eventually(func() bool {
306308
return resources.CheckCondition(k8sClient, secondDeployment, api.TrueCondition(api.ReadyType))
307-
}).WithTimeout(20 * time.Minute).WithPolling(PollingInterval).Should(BeTrue())
309+
}).WithTimeout(5 * time.Minute).WithPolling(PollingInterval).Should(BeTrue())
308310
})
309311

310312
By("Validating connection secrets for second deployment were not created", func() {
@@ -338,8 +340,8 @@ var _ = Describe("Atlas Database User", Label("int", "AtlasDatabaseUser", "prote
338340
Expect(k8sClient.Delete(context.Background(), secondDeployment)).To(Succeed())
339341

340342
Eventually(func() bool {
341-
_, r, err := atlasClient.ClustersApi.
342-
GetCluster(context.Background(), testProject.ID(), deploymentName).
343+
_, r, err := atlasClientv20241113001.FlexClustersApi.
344+
GetFlexCluster(context.Background(), testProject.ID(), deploymentName).
343345
Execute()
344346
if err != nil {
345347
if r != nil && r.StatusCode == http.StatusNotFound {
@@ -348,7 +350,33 @@ var _ = Describe("Atlas Database User", Label("int", "AtlasDatabaseUser", "prote
348350
}
349351

350352
return false
351-
}).WithTimeout(20 * time.Minute).WithPolling(PollingInterval).Should(BeTrue())
353+
}).WithTimeout(5 * time.Minute).WithPolling(PollingInterval).Should(BeTrue())
354+
})
355+
})
356+
357+
It("Adds connection secret when new user is created with an existing deployment", Label("user-add-secret"), func() {
358+
By("Creating a database user", func() {
359+
passwordSecret := buildPasswordSecret(testNamespace.Name, UserPasswordSecret, DBUserPassword)
360+
Expect(k8sClient.Create(context.Background(), &passwordSecret)).To(Succeed())
361+
362+
testDBUser1 = akov2.NewDBUser(testNamespace.Name, dbUserName1, dbUserName1, projectName).
363+
WithPasswordSecret(UserPasswordSecret).
364+
WithRole("readWriteAnyDatabase", "admin", "")
365+
Expect(k8sClient.Create(context.Background(), testDBUser1)).To(Succeed())
366+
367+
Eventually(func() bool {
368+
return resources.CheckCondition(k8sClient, testDBUser1, api.TrueCondition(api.ReadyType))
369+
}).WithTimeout(databaseUserTimeout).WithPolling(PollingInterval).Should(BeTrue())
370+
371+
validateSecret(k8sClient, *testProject, *testDeployment, *testDBUser1)
372+
373+
Expect(tryConnect(testProject.ID(), *testDeployment, *testDBUser1)).Should(Succeed())
374+
})
375+
376+
By("Validating connection secrets were created", func() {
377+
validateSecret(k8sClient, *testProject, *testDeployment, *testDBUser1)
378+
379+
Expect(tryConnect(testProject.ID(), *testDeployment, *testDBUser1)).Should(Succeed())
352380
})
353381
})
354382

@@ -407,7 +435,7 @@ var _ = Describe("Atlas Database User", Label("int", "AtlasDatabaseUser", "prote
407435
secondTestDeployment := &akov2.AtlasDeployment{}
408436

409437
By("Creating a second deployment", func() {
410-
secondTestDeployment = akov2.DefaultAzureDeployment(testNamespace.Name, projectName).Lightweight()
438+
secondTestDeployment = akov2.NewDefaultAzureFlexInstance(testNamespace.Name, projectName)
411439
Expect(k8sClient.Create(context.Background(), secondTestDeployment)).To(Succeed())
412440

413441
Eventually(func() bool {
@@ -451,9 +479,15 @@ var _ = Describe("Atlas Database User", Label("int", "AtlasDatabaseUser", "prote
451479

452480
checkNumberOfConnectionSecrets(k8sClient, *testProject, testNamespace.Name, 2)
453481
secret := validateSecret(k8sClient, *testProject, *testDeployment, *testDBUser1)
454-
Expect(secret.Name).To(Equal(fmt.Sprintf("%s-test-deployment-aws-new-user", kube.NormalizeIdentifier(testProject.Spec.Name))))
482+
Expect(secret.Name).To(Equal(fmt.Sprintf("%s-%s-new-user",
483+
kube.NormalizeIdentifier(testProject.Spec.Name),
484+
kube.NormalizeIdentifier(testDeployment.GetDeploymentName()),
485+
)))
455486
secret = validateSecret(k8sClient, *testProject, *secondTestDeployment, *testDBUser1)
456-
Expect(secret.Name).To(Equal(fmt.Sprintf("%s-test-deployment-azure-new-user", kube.NormalizeIdentifier(testProject.Spec.Name))))
487+
Expect(secret.Name).To(Equal(fmt.Sprintf("%s-%s-new-user",
488+
kube.NormalizeIdentifier(testProject.Spec.Name),
489+
kube.NormalizeIdentifier(secondTestDeployment.GetDeploymentName()),
490+
)))
457491

458492
Expect(tryConnect(testProject.ID(), *testDeployment, *testDBUser1)).Should(Succeed())
459493
Expect(tryConnect(testProject.ID(), *secondTestDeployment, *testDBUser1)).Should(Succeed())
@@ -479,8 +513,8 @@ var _ = Describe("Atlas Database User", Label("int", "AtlasDatabaseUser", "prote
479513
Expect(k8sClient.Delete(context.Background(), secondTestDeployment)).To(Succeed())
480514

481515
Eventually(func() bool {
482-
_, r, err := atlasClient.ClustersApi.
483-
GetCluster(context.Background(), testProject.ID(), deploymentName).
516+
_, r, err := atlasClientv20241113001.FlexClustersApi.
517+
GetFlexCluster(context.Background(), testProject.ID(), deploymentName).
484518
Execute()
485519
if err != nil {
486520
if r != nil && r.StatusCode == http.StatusNotFound {
@@ -520,9 +554,11 @@ var _ = Describe("Atlas Database User", Label("int", "AtlasDatabaseUser", "prote
520554

521555
By("Fixing the user date expiration", func() {
522556
after := time.Now().UTC().Add(time.Hour * 10).Format("2006-01-02T15:04:05")
523-
testDBUser1 = testDBUser1.WithDeleteAfterDate(after)
524557

525558
Expect(k8sClient.Update(context.Background(), testDBUser1)).To(Succeed())
559+
retry.RetryUpdateOnConflict(context.Background(), k8sClient, client.ObjectKeyFromObject(testDBUser1), func(user *akov2.AtlasDatabaseUser) {
560+
user.Spec.DeleteAfterDate = after
561+
})
526562
Eventually(func() bool {
527563
return resources.CheckCondition(k8sClient, testDBUser1, api.TrueCondition(api.ReadyType))
528564
}).WithTimeout(databaseUserTimeout).WithPolling(PollingInterval).Should(BeTrue())
@@ -533,9 +569,11 @@ var _ = Describe("Atlas Database User", Label("int", "AtlasDatabaseUser", "prote
533569

534570
By("Expiring the User", func() {
535571
before := time.Now().UTC().Add(time.Minute * -5).Format("2006-01-02T15:04:05")
536-
testDBUser1 = testDBUser1.WithDeleteAfterDate(before)
537572

538573
Expect(k8sClient.Update(context.Background(), testDBUser1)).To(Succeed())
574+
retry.RetryUpdateOnConflict(context.Background(), k8sClient, client.ObjectKeyFromObject(testDBUser1), func(user *akov2.AtlasDatabaseUser) {
575+
user.Spec.DeleteAfterDate = before
576+
})
539577
Eventually(func() bool {
540578
return resources.CheckCondition(k8sClient, testDBUser1, api.FalseCondition(api.DatabaseUserReadyType).WithReason(string(workflow.DatabaseUserExpired)))
541579
}).WithTimeout(databaseUserTimeout).WithPolling(PollingInterval).Should(BeTrue())
@@ -604,8 +642,8 @@ var _ = Describe("Atlas Database User", Label("int", "AtlasDatabaseUser", "prote
604642
Expect(k8sClient.Delete(context.Background(), testDeployment)).To(Succeed())
605643

606644
Eventually(func() bool {
607-
_, r, err := atlasClient.ClustersApi.
608-
GetCluster(context.Background(), testProject.ID(), deploymentName).
645+
_, r, err := atlasClientv20241113001.FlexClustersApi.
646+
GetFlexCluster(context.Background(), testProject.ID(), deploymentName).
609647
Execute()
610648
if err != nil {
611649
if r != nil && r.StatusCode == http.StatusNotFound {
@@ -614,7 +652,7 @@ var _ = Describe("Atlas Database User", Label("int", "AtlasDatabaseUser", "prote
614652
}
615653

616654
return false
617-
}).WithTimeout(20 * time.Minute).WithPolling(PollingInterval).Should(BeTrue())
655+
}).WithTimeout(10 * time.Minute).WithPolling(PollingInterval).Should(BeTrue())
618656
})
619657

620658
By("Deleting the project", func() {
@@ -630,7 +668,7 @@ var _ = Describe("Atlas Database User", Label("int", "AtlasDatabaseUser", "prote
630668
}
631669

632670
return false
633-
}).WithTimeout(15 * time.Minute).WithPolling(PollingInterval).Should(BeTrue())
671+
}).WithTimeout(5 * time.Minute).WithPolling(PollingInterval).Should(BeTrue())
634672
})
635673

636674
By("Stopping the operator", func() {
@@ -665,8 +703,8 @@ func validateSecret(k8sClient client.Client, project akov2.AtlasProject, deploym
665703
password, err := user.ReadPassword(context.Background(), k8sClient)
666704
Expect(err).NotTo(HaveOccurred())
667705

668-
c, _, err := atlasClient.ClustersApi.
669-
GetCluster(context.Background(), project.ID(), deployment.GetDeploymentName()).
706+
c, _, err := atlasClientv20241113001.FlexClustersApi.
707+
GetFlexCluster(context.Background(), project.ID(), deployment.GetDeploymentName()).
670708
Execute()
671709
Expect(err).NotTo(HaveOccurred())
672710

@@ -675,8 +713,8 @@ func validateSecret(k8sClient client.Client, project akov2.AtlasProject, deploym
675713
expectedData := map[string][]byte{
676714
"connectionStringStandard": []byte(buildConnectionURL(connectionStrings.GetStandard(), username, password)),
677715
"connectionStringStandardSrv": []byte(buildConnectionURL(connectionStrings.GetStandardSrv(), username, password)),
678-
"connectionStringPrivate": []byte(buildConnectionURL(connectionStrings.GetPrivate(), username, password)),
679-
"connectionStringPrivateSrv": []byte(buildConnectionURL(connectionStrings.GetPrivateSrv(), username, password)),
716+
"connectionStringPrivate": []byte(""),
717+
"connectionStringPrivateSrv": []byte(""),
680718
"username": []byte(username),
681719
"password": []byte(password),
682720
}
@@ -717,8 +755,8 @@ func buildConnectionURL(connURL, userName, password string) string {
717755
func mongoClient(projectID string, deployment akov2.AtlasDeployment, user akov2.AtlasDatabaseUser) (*mongo.Client, error) {
718756
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
719757
defer cancel()
720-
c, _, err := atlasClient.ClustersApi.
721-
GetCluster(context.Background(), projectID, deployment.GetDeploymentName()).
758+
c, _, err := atlasClientv20241113001.FlexClustersApi.
759+
GetFlexCluster(context.Background(), projectID, deployment.GetDeploymentName()).
722760
Execute()
723761
Expect(err).NotTo(HaveOccurred())
724762

0 commit comments

Comments
 (0)