Skip to content

Commit ba1b023

Browse files
authored
CLOUDP-301141: test/int/databaseuser_unprotected_test.go: fix concurrent updates (#2136)
* test/int/databaseuser_unprotected_test.go: fix concurrent updates * respect error * further update fixes
1 parent a58964c commit ba1b023

File tree

1 file changed

+42
-28
lines changed

1 file changed

+42
-28
lines changed

test/int/databaseuser_unprotected_test.go

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,10 @@ var _ = Describe("Atlas Database User", Label("int", "AtlasDatabaseUser", "prote
243243

244244
By("Giving user readWrite permissions", func() {
245245
// Adding the role allowing read/write
246-
testDBUser1 = testDBUser1.WithRole("readWriteAnyDatabase", "admin", "")
247-
248-
Expect(k8sClient.Update(context.Background(), testDBUser1)).To(Succeed())
246+
_, err := retry.RetryUpdateOnConflict(context.Background(), k8sClient, client.ObjectKeyFromObject(testDBUser1), func(user *akov2.AtlasDatabaseUser) {
247+
user.WithRole("readWriteAnyDatabase", "admin", "")
248+
})
249+
Expect(err).NotTo(HaveOccurred())
249250

250251
Eventually(func() bool {
251252
return resources.CheckCondition(k8sClient, testDBUser1, api.TrueCondition(api.ReadyType))
@@ -317,10 +318,10 @@ var _ = Describe("Atlas Database User", Label("int", "AtlasDatabaseUser", "prote
317318
})
318319

319320
By("Removing database user scope for first deployment", func() {
320-
Expect(k8sClient.Get(context.Background(), client.ObjectKeyFromObject(testDBUser1), testDBUser1)).Should(Succeed())
321-
testDBUser1.Spec.Scopes = nil
322-
323-
Expect(k8sClient.Update(context.Background(), testDBUser1)).To(Succeed())
321+
_, err := retry.RetryUpdateOnConflict(context.Background(), k8sClient, client.ObjectKeyFromObject(testDBUser1), func(user *akov2.AtlasDatabaseUser) {
322+
user.Spec.Scopes = nil
323+
})
324+
Expect(err).NotTo(HaveOccurred())
324325

325326
Eventually(func() bool {
326327
return resources.CheckCondition(k8sClient, testDBUser1, api.TrueCondition(api.ReadyType))
@@ -400,8 +401,12 @@ var _ = Describe("Atlas Database User", Label("int", "AtlasDatabaseUser", "prote
400401
})
401402

402403
By("Breaking the password secret", func() {
403-
passwordSecret := buildPasswordSecret(testNamespace.Name, UserPasswordSecret, "")
404-
Expect(k8sClient.Update(context.Background(), &passwordSecret)).To(Succeed())
404+
_, err := retry.RetryUpdateOnConflict(context.Background(), k8sClient, client.ObjectKey{Namespace: testNamespace.Name, Name: UserPasswordSecret}, func(secret *corev1.Secret) {
405+
empty := buildPasswordSecret(secret.GetNamespace(), secret.GetName(), "")
406+
secret.Labels = empty.Labels
407+
secret.StringData = empty.StringData
408+
})
409+
Expect(err).NotTo(HaveOccurred())
405410

406411
expectedCondition := api.FalseCondition(api.DatabaseUserReadyType).WithReason(string(workflow.Internal)).WithMessageRegexp("the 'password' field is empty")
407412
Eventually(func() bool {
@@ -412,8 +417,12 @@ var _ = Describe("Atlas Database User", Label("int", "AtlasDatabaseUser", "prote
412417
})
413418

414419
By("Fixing the password secret", func() {
415-
passwordSecret := buildPasswordSecret(testNamespace.Name, UserPasswordSecret, "someNewPassw00rd")
416-
Expect(k8sClient.Update(context.Background(), &passwordSecret)).To(Succeed())
420+
_, err := retry.RetryUpdateOnConflict(context.Background(), k8sClient, client.ObjectKey{Namespace: testNamespace.Name, Name: UserPasswordSecret}, func(secret *corev1.Secret) {
421+
somePassword := buildPasswordSecret(secret.GetNamespace(), secret.GetName(), "someNewPassw00rd")
422+
secret.Labels = somePassword.Labels
423+
secret.StringData = somePassword.StringData
424+
})
425+
Expect(err).NotTo(HaveOccurred())
417426

418427
Eventually(func() bool {
419428
return resources.CheckCondition(k8sClient, testDBUser1, api.TrueCondition(api.ReadyType))
@@ -466,14 +475,17 @@ var _ = Describe("Atlas Database User", Label("int", "AtlasDatabaseUser", "prote
466475
By("Renaming username, new user is added and stale secrets are removed", func() {
467476
Expect(k8sClient.Get(context.Background(), client.ObjectKeyFromObject(testDBUser1), testDBUser1)).To(Succeed())
468477
oldName := testDBUser1.Spec.Username
469-
testDBUser1 = testDBUser1.WithAtlasUserName("new-user")
470-
Expect(k8sClient.Update(context.Background(), testDBUser1)).To(Succeed())
478+
479+
_, err := retry.RetryUpdateOnConflict(context.Background(), k8sClient, client.ObjectKeyFromObject(testDBUser1), func(user *akov2.AtlasDatabaseUser) {
480+
user.WithAtlasUserName("new-user")
481+
})
482+
Expect(err).NotTo(HaveOccurred())
471483

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

476-
_, _, err := atlasClient.DatabaseUsersApi.
488+
_, _, err = atlasClient.DatabaseUsersApi.
477489
GetDatabaseUser(context.Background(), testProject.ID(), testDBUser1.Spec.DatabaseName, oldName).
478490
Execute()
479491
Expect(err).To(HaveOccurred())
@@ -495,8 +507,10 @@ var _ = Describe("Atlas Database User", Label("int", "AtlasDatabaseUser", "prote
495507
})
496508

497509
By("Scoping user to one cluster, a stale secret is removed", func() {
498-
testDBUser1 = testDBUser1.ClearScopes().WithScope(akov2.DeploymentScopeType, testDeployment.GetDeploymentName())
499-
Expect(k8sClient.Update(context.Background(), testDBUser1)).To(Succeed())
510+
_, err := retry.RetryUpdateOnConflict(context.Background(), k8sClient, client.ObjectKeyFromObject(testDBUser1), func(user *akov2.AtlasDatabaseUser) {
511+
user.ClearScopes().WithScope(akov2.DeploymentScopeType, testDeployment.GetDeploymentName())
512+
})
513+
Expect(err).NotTo(HaveOccurred())
500514

501515
Eventually(func() bool {
502516
return resources.CheckCondition(k8sClient, testDBUser1, api.TrueCondition(api.ReadyType))
@@ -556,10 +570,10 @@ var _ = Describe("Atlas Database User", Label("int", "AtlasDatabaseUser", "prote
556570
By("Fixing the user date expiration", func() {
557571
after := time.Now().UTC().Add(time.Hour * 10).Format("2006-01-02T15:04:05")
558572

559-
Expect(k8sClient.Update(context.Background(), testDBUser1)).To(Succeed())
560-
retry.RetryUpdateOnConflict(context.Background(), k8sClient, client.ObjectKeyFromObject(testDBUser1), func(user *akov2.AtlasDatabaseUser) {
573+
_, err := retry.RetryUpdateOnConflict(context.Background(), k8sClient, client.ObjectKeyFromObject(testDBUser1), func(user *akov2.AtlasDatabaseUser) {
561574
user.Spec.DeleteAfterDate = after
562575
})
576+
Expect(err).NotTo(HaveOccurred())
563577
Eventually(func() bool {
564578
return resources.CheckCondition(k8sClient, testDBUser1, api.TrueCondition(api.ReadyType))
565579
}).WithTimeout(databaseUserTimeout).WithPolling(PollingInterval).Should(BeTrue())
@@ -571,10 +585,10 @@ var _ = Describe("Atlas Database User", Label("int", "AtlasDatabaseUser", "prote
571585
By("Expiring the User", func() {
572586
before := time.Now().UTC().Add(time.Minute * -5).Format("2006-01-02T15:04:05")
573587

574-
Expect(k8sClient.Update(context.Background(), testDBUser1)).To(Succeed())
575-
retry.RetryUpdateOnConflict(context.Background(), k8sClient, client.ObjectKeyFromObject(testDBUser1), func(user *akov2.AtlasDatabaseUser) {
588+
_, err := retry.RetryUpdateOnConflict(context.Background(), k8sClient, client.ObjectKeyFromObject(testDBUser1), func(user *akov2.AtlasDatabaseUser) {
576589
user.Spec.DeleteAfterDate = before
577590
})
591+
Expect(err).NotTo(HaveOccurred())
578592
Eventually(func() bool {
579593
return resources.CheckCondition(k8sClient, testDBUser1, api.FalseCondition(api.DatabaseUserReadyType).WithReason(string(workflow.DatabaseUserExpired)))
580594
}).WithTimeout(databaseUserTimeout).WithPolling(PollingInterval).Should(BeTrue())
@@ -611,15 +625,15 @@ var _ = Describe("Atlas Database User", Label("int", "AtlasDatabaseUser", "prote
611625
})
612626

613627
By("Skipping reconciliation", func() {
614-
Expect(k8sClient.Get(context.Background(), client.ObjectKeyFromObject(testDBUser1), testDBUser1)).To(Succeed())
615-
testDBUser1.ObjectMeta.Annotations = map[string]string{customresource.ReconciliationPolicyAnnotation: customresource.ReconciliationPolicySkip}
616-
testDBUser1.Spec.Roles = append(testDBUser1.Spec.Roles, akov2.RoleSpec{
617-
RoleName: "new-role",
618-
DatabaseName: "new-database",
619-
CollectionName: "new-collection",
628+
_, err := retry.RetryUpdateOnConflict(context.Background(), k8sClient, client.ObjectKeyFromObject(testDBUser1), func(user *akov2.AtlasDatabaseUser) {
629+
user.ObjectMeta.Annotations = map[string]string{customresource.ReconciliationPolicyAnnotation: customresource.ReconciliationPolicySkip}
630+
user.Spec.Roles = append(testDBUser1.Spec.Roles, akov2.RoleSpec{
631+
RoleName: "new-role",
632+
DatabaseName: "new-database",
633+
CollectionName: "new-collection",
634+
})
620635
})
621-
622-
Expect(k8sClient.Update(context.Background(), testDBUser1)).To(Succeed())
636+
Expect(err).NotTo(HaveOccurred())
623637

624638
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*2)
625639
defer cancel()

0 commit comments

Comments
 (0)