Skip to content

CLOUDP-299865: Tests for Flex Connection Secrets #2121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions api/v1/atlasdeployment_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,8 @@ func (c *AtlasDeployment) WithAtlasName(name string) *AtlasDeployment {
c.Spec.DeploymentSpec.Name = name
} else if c.Spec.ServerlessSpec != nil {
c.Spec.ServerlessSpec.Name = name
} else if c.Spec.FlexSpec != nil {
c.Spec.FlexSpec.Name = name
}
return c
}
Expand Down Expand Up @@ -835,13 +837,23 @@ func NewDefaultAWSServerlessInstance(namespace, projectName string) *AtlasDeploy
func NewDefaultAWSFlexInstance(namespace, projectName string) *AtlasDeployment {
return newFlexInstance(
namespace,
"test-flex-instance-k8s",
"test-flex-instance",
"test-flex-instance-aws-k8s",
"test-flex-instance-aws",
"AWS",
"US_EAST_1",
).WithProjectName(projectName)
}

func NewDefaultAzureFlexInstance(namespace, projectName string) *AtlasDeployment {
return newFlexInstance(
namespace,
"test-flex-instance-az-k8s",
"test-flex-instance-az",
"AZURE",
"US_EAST_2",
).WithProjectName(projectName)
}

func (c *AtlasDeployment) AtlasName() string {
if c.Spec.DeploymentSpec != nil {
return c.Spec.DeploymentSpec.Name
Expand Down
1 change: 1 addition & 0 deletions internal/translation/deployment/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,7 @@ func flexFromAtlas(instance *adminv20241113001.FlexClusterDescription20241113) *
State: instance.GetStateName(),
MongoDBVersion: instance.GetMongoDBVersion(),
Connection: &status.ConnectionStrings{
Standard: connectionStrings.GetStandard(),
StandardSrv: connectionStrings.GetStandardSrv(),
},
}
Expand Down
6 changes: 3 additions & 3 deletions test/int/databaseuser_protected_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ var _ = Describe("Atlas Database User", Label("int", "AtlasDatabaseUser", "prote
})

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

Eventually(func() bool {
_, r, err := atlasClient.ClustersApi.
GetCluster(context.Background(), testProject.ID(), deploymentName).
_, r, err := atlasClientv20241113001.FlexClustersApi.
GetFlexCluster(context.Background(), testProject.ID(), deploymentName).
Execute()
if err != nil {
if r != nil && r.StatusCode == http.StatusNotFound {
Expand Down
88 changes: 63 additions & 25 deletions test/int/databaseuser_unprotected_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/mongodb/mongodb-atlas-kubernetes/v2/test/helper/conditions"
"github.com/mongodb/mongodb-atlas-kubernetes/v2/test/helper/events"
"github.com/mongodb/mongodb-atlas-kubernetes/v2/test/helper/resources"
"github.com/mongodb/mongodb-atlas-kubernetes/v2/test/helper/retry"
)

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

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

Eventually(func() bool {
Expand Down Expand Up @@ -276,7 +278,7 @@ var _ = Describe("Atlas Database User", Label("int", "AtlasDatabaseUser", "prote
})
})

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

By("Creating a database user", func() {
Expand All @@ -297,13 +299,13 @@ var _ = Describe("Atlas Database User", Label("int", "AtlasDatabaseUser", "prote
Expect(tryConnect(testProject.ID(), *testDeployment, *testDBUser1)).Should(Succeed())
})

By("Creating a second deployment", func() {
secondDeployment = akov2.DefaultAzureDeployment(testNamespace.Name, projectName).Lightweight()
By("Creating a second deployment", func() {
secondDeployment = akov2.NewDefaultAzureFlexInstance(testNamespace.Name, projectName)
Expect(k8sClient.Create(context.Background(), secondDeployment)).To(Succeed())

Eventually(func() bool {
return resources.CheckCondition(k8sClient, secondDeployment, api.TrueCondition(api.ReadyType))
}).WithTimeout(20 * time.Minute).WithPolling(PollingInterval).Should(BeTrue())
}).WithTimeout(5 * time.Minute).WithPolling(PollingInterval).Should(BeTrue())
})

By("Validating connection secrets were created", func() {
Expand All @@ -319,8 +321,8 @@ var _ = Describe("Atlas Database User", Label("int", "AtlasDatabaseUser", "prote
Expect(k8sClient.Delete(context.Background(), secondDeployment)).To(Succeed())

Eventually(func() bool {
_, r, err := atlasClient.ClustersApi.
GetCluster(context.Background(), testProject.ID(), deploymentName).
_, r, err := atlasClientv20241113001.FlexClustersApi.
GetFlexCluster(context.Background(), testProject.ID(), deploymentName).
Execute()
if err != nil {
if r != nil && r.StatusCode == http.StatusNotFound {
Expand All @@ -329,7 +331,33 @@ var _ = Describe("Atlas Database User", Label("int", "AtlasDatabaseUser", "prote
}

return false
}).WithTimeout(20 * time.Minute).WithPolling(PollingInterval).Should(BeTrue())
}).WithTimeout(5 * time.Minute).WithPolling(PollingInterval).Should(BeTrue())
})
})

It("Adds connection secret when new user is created with an existing deployment", Label("user-add-secret"), func() {
By("Creating a database user", func() {
passwordSecret := buildPasswordSecret(testNamespace.Name, UserPasswordSecret, DBUserPassword)
Expect(k8sClient.Create(context.Background(), &passwordSecret)).To(Succeed())

testDBUser1 = akov2.NewDBUser(testNamespace.Name, dbUserName1, dbUserName1, projectName).
WithPasswordSecret(UserPasswordSecret).
WithRole("readWriteAnyDatabase", "admin", "")
Expect(k8sClient.Create(context.Background(), testDBUser1)).To(Succeed())

Eventually(func() bool {
return resources.CheckCondition(k8sClient, testDBUser1, api.TrueCondition(api.ReadyType))
}).WithTimeout(databaseUserTimeout).WithPolling(PollingInterval).Should(BeTrue())

validateSecret(k8sClient, *testProject, *testDeployment, *testDBUser1)

Expect(tryConnect(testProject.ID(), *testDeployment, *testDBUser1)).Should(Succeed())
})

By("Validating connection secrets were created", func() {
validateSecret(k8sClient, *testProject, *testDeployment, *testDBUser1)

Expect(tryConnect(testProject.ID(), *testDeployment, *testDBUser1)).Should(Succeed())
})
})

Expand Down Expand Up @@ -388,7 +416,7 @@ var _ = Describe("Atlas Database User", Label("int", "AtlasDatabaseUser", "prote
secondTestDeployment := &akov2.AtlasDeployment{}

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

Eventually(func() bool {
Expand Down Expand Up @@ -432,9 +460,15 @@ var _ = Describe("Atlas Database User", Label("int", "AtlasDatabaseUser", "prote

checkNumberOfConnectionSecrets(k8sClient, *testProject, testNamespace.Name, 2)
secret := validateSecret(k8sClient, *testProject, *testDeployment, *testDBUser1)
Expect(secret.Name).To(Equal(fmt.Sprintf("%s-test-deployment-aws-new-user", kube.NormalizeIdentifier(testProject.Spec.Name))))
Expect(secret.Name).To(Equal(fmt.Sprintf("%s-%s-new-user",
kube.NormalizeIdentifier(testProject.Spec.Name),
kube.NormalizeIdentifier(testDeployment.GetDeploymentName()),
)))
secret = validateSecret(k8sClient, *testProject, *secondTestDeployment, *testDBUser1)
Expect(secret.Name).To(Equal(fmt.Sprintf("%s-test-deployment-azure-new-user", kube.NormalizeIdentifier(testProject.Spec.Name))))
Expect(secret.Name).To(Equal(fmt.Sprintf("%s-%s-new-user",
kube.NormalizeIdentifier(testProject.Spec.Name),
kube.NormalizeIdentifier(secondTestDeployment.GetDeploymentName()),
)))

Expect(tryConnect(testProject.ID(), *testDeployment, *testDBUser1)).Should(Succeed())
Expect(tryConnect(testProject.ID(), *secondTestDeployment, *testDBUser1)).Should(Succeed())
Expand All @@ -460,8 +494,8 @@ var _ = Describe("Atlas Database User", Label("int", "AtlasDatabaseUser", "prote
Expect(k8sClient.Delete(context.Background(), secondTestDeployment)).To(Succeed())

Eventually(func() bool {
_, r, err := atlasClient.ClustersApi.
GetCluster(context.Background(), testProject.ID(), deploymentName).
_, r, err := atlasClientv20241113001.FlexClustersApi.
GetFlexCluster(context.Background(), testProject.ID(), deploymentName).
Execute()
if err != nil {
if r != nil && r.StatusCode == http.StatusNotFound {
Expand Down Expand Up @@ -501,9 +535,11 @@ var _ = Describe("Atlas Database User", Label("int", "AtlasDatabaseUser", "prote

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

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

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

Expect(k8sClient.Update(context.Background(), testDBUser1)).To(Succeed())
retry.RetryUpdateOnConflict(context.Background(), k8sClient, client.ObjectKeyFromObject(testDBUser1), func(user *akov2.AtlasDatabaseUser) {
user.Spec.DeleteAfterDate = before
})
Eventually(func() bool {
return resources.CheckCondition(k8sClient, testDBUser1, api.FalseCondition(api.DatabaseUserReadyType).WithReason(string(workflow.DatabaseUserExpired)))
}).WithTimeout(databaseUserTimeout).WithPolling(PollingInterval).Should(BeTrue())
Expand Down Expand Up @@ -585,8 +623,8 @@ var _ = Describe("Atlas Database User", Label("int", "AtlasDatabaseUser", "prote
Expect(k8sClient.Delete(context.Background(), testDeployment)).To(Succeed())

Eventually(func() bool {
_, r, err := atlasClient.ClustersApi.
GetCluster(context.Background(), testProject.ID(), deploymentName).
_, r, err := atlasClientv20241113001.FlexClustersApi.
GetFlexCluster(context.Background(), testProject.ID(), deploymentName).
Execute()
if err != nil {
if r != nil && r.StatusCode == http.StatusNotFound {
Expand All @@ -595,7 +633,7 @@ var _ = Describe("Atlas Database User", Label("int", "AtlasDatabaseUser", "prote
}

return false
}).WithTimeout(20 * time.Minute).WithPolling(PollingInterval).Should(BeTrue())
}).WithTimeout(10 * time.Minute).WithPolling(PollingInterval).Should(BeTrue())
})

By("Deleting the project", func() {
Expand All @@ -611,7 +649,7 @@ var _ = Describe("Atlas Database User", Label("int", "AtlasDatabaseUser", "prote
}

return false
}).WithTimeout(15 * time.Minute).WithPolling(PollingInterval).Should(BeTrue())
}).WithTimeout(5 * time.Minute).WithPolling(PollingInterval).Should(BeTrue())
})

By("Stopping the operator", func() {
Expand Down Expand Up @@ -646,8 +684,8 @@ func validateSecret(k8sClient client.Client, project akov2.AtlasProject, deploym
password, err := user.ReadPassword(context.Background(), k8sClient)
Expect(err).NotTo(HaveOccurred())

c, _, err := atlasClient.ClustersApi.
GetCluster(context.Background(), project.ID(), deployment.GetDeploymentName()).
c, _, err := atlasClientv20241113001.FlexClustersApi.
GetFlexCluster(context.Background(), project.ID(), deployment.GetDeploymentName()).
Execute()
Expect(err).NotTo(HaveOccurred())

Expand All @@ -656,8 +694,8 @@ func validateSecret(k8sClient client.Client, project akov2.AtlasProject, deploym
expectedData := map[string][]byte{
"connectionStringStandard": []byte(buildConnectionURL(connectionStrings.GetStandard(), username, password)),
"connectionStringStandardSrv": []byte(buildConnectionURL(connectionStrings.GetStandardSrv(), username, password)),
"connectionStringPrivate": []byte(buildConnectionURL(connectionStrings.GetPrivate(), username, password)),
"connectionStringPrivateSrv": []byte(buildConnectionURL(connectionStrings.GetPrivateSrv(), username, password)),
"connectionStringPrivate": []byte(""),
"connectionStringPrivateSrv": []byte(""),
"username": []byte(username),
"password": []byte(password),
}
Expand Down Expand Up @@ -698,8 +736,8 @@ func buildConnectionURL(connURL, userName, password string) string {
func mongoClient(projectID string, deployment akov2.AtlasDeployment, user akov2.AtlasDatabaseUser) (*mongo.Client, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
c, _, err := atlasClient.ClustersApi.
GetCluster(context.Background(), projectID, deployment.GetDeploymentName()).
c, _, err := atlasClientv20241113001.FlexClustersApi.
GetFlexCluster(context.Background(), projectID, deployment.GetDeploymentName()).
Execute()
Expect(err).NotTo(HaveOccurred())

Expand Down
Loading