diff --git a/test/e2e/no_user_identity_test.go b/test/e2e/no_user_identity_test.go index c78881986..319a14154 100644 --- a/test/e2e/no_user_identity_test.go +++ b/test/e2e/no_user_identity_test.go @@ -83,10 +83,11 @@ func TestCreationOfUserAndIdentityIsSkipped(t *testing.T) { t.Run("user and identity stay there when user is deactivated", func(t *testing.T) { // when - userSignup, err := hostAwait.UpdateUserSignup(t, signup.Name, - func(us *toolchainv1alpha1.UserSignup) { - states.SetDeactivated(us, true) - }) + userSignup, err := wait.For(t, hostAwait.Awaitility, &toolchainv1alpha1.UserSignup{}). + Update(signup.Name, + func(us *toolchainv1alpha1.UserSignup) { + states.SetDeactivated(us, true) + }) require.NoError(t, err) // Wait until the UserSignup is deactivated diff --git a/test/e2e/parallel/e2e_test.go b/test/e2e/parallel/e2e_test.go index aa5a707c8..07a8031ea 100644 --- a/test/e2e/parallel/e2e_test.go +++ b/test/e2e/parallel/e2e_test.go @@ -160,8 +160,10 @@ func TestE2EFlow(t *testing.T) { require.NoError(t, err) // when - user.Identities = []string{} - err = memberAwait.Client.Update(context.TODO(), user) + _, err = wait.For(t, memberAwait.Awaitility, &userv1.User{}). + Update(user.Name, func(u *userv1.User) { + u.Identities = []string{} + }) // then require.NoError(t, err) @@ -175,10 +177,12 @@ func TestE2EFlow(t *testing.T) { err := memberAwait.Client.Get(context.TODO(), types.NamespacedName{Name: identitypkg.NewIdentityNamingStandard( johnSignup.Spec.IdentityClaims.Sub, "rhd").IdentityName()}, identity) require.NoError(t, err) - identity.User = corev1.ObjectReference{Name: "", UID: ""} // when - err = memberAwait.Client.Update(context.TODO(), identity) + _, err = wait.For(t, memberAwait.Awaitility, &userv1.Identity{}). + Update(identity.Name, func(i *userv1.Identity) { + i.User = corev1.ObjectReference{Name: "", UID: ""} + }) // then require.NoError(t, err) @@ -483,10 +487,11 @@ func TestE2EFlow(t *testing.T) { require.Len(t, userList.Items, 1) // Now deactivate the UserSignup - userSignup, err := hostAwait.UpdateUserSignup(t, originalSubJohnSignup.Name, - func(us *toolchainv1alpha1.UserSignup) { - states.SetDeactivated(us, true) - }) + userSignup, err := wait.For(t, hostAwait.Awaitility, &toolchainv1alpha1.UserSignup{}). + Update(originalSubJohnSignup.Name, + func(us *toolchainv1alpha1.UserSignup) { + states.SetDeactivated(us, true) + }) require.NoError(t, err) // Wait until the UserSignup is deactivated diff --git a/test/e2e/parallel/nstemplatetier_test.go b/test/e2e/parallel/nstemplatetier_test.go index c2a3fc983..b3d205fc9 100644 --- a/test/e2e/parallel/nstemplatetier_test.go +++ b/test/e2e/parallel/nstemplatetier_test.go @@ -169,10 +169,11 @@ func TestResetDeactivatingStateWhenPromotingUser(t *testing.T) { RequireConditions(wait.ConditionSet(wait.Default(), wait.ApprovedByAdmin())...). Execute(t) // Set the deactivating state on the UserSignup - updatedUserSignup, err := hostAwait.UpdateUserSignup(t, user.UserSignup.Name, - func(us *toolchainv1alpha1.UserSignup) { - states.SetDeactivating(us, true) - }) + updatedUserSignup, err := wait.For(t, hostAwait.Awaitility, &toolchainv1alpha1.UserSignup{}). + Update(user.UserSignup.Name, + func(us *toolchainv1alpha1.UserSignup) { + states.SetDeactivating(us, true) + }) require.NoError(t, err) // Move the MUR to the user tier with longer deactivation time diff --git a/test/e2e/parallel/proxy_test.go b/test/e2e/parallel/proxy_test.go index a849ef7af..744cca0ea 100644 --- a/test/e2e/parallel/proxy_test.go +++ b/test/e2e/parallel/proxy_test.go @@ -8,7 +8,6 @@ import ( "errors" "fmt" "io" - rbacv1 "k8s.io/api/rbac/v1" "net/http" "net/url" "os" @@ -18,6 +17,8 @@ import ( "testing" "time" + rbacv1 "k8s.io/api/rbac/v1" + toolchainv1alpha1 "github.com/codeready-toolchain/api/api/v1alpha1" commonproxy "github.com/codeready-toolchain/toolchain-common/pkg/proxy" "github.com/codeready-toolchain/toolchain-common/pkg/test" diff --git a/test/e2e/parallel/registration_service_test.go b/test/e2e/parallel/registration_service_test.go index b0b17bb00..52d3f1376 100644 --- a/test/e2e/parallel/registration_service_test.go +++ b/test/e2e/parallel/registration_service_test.go @@ -412,12 +412,13 @@ func TestSignupOK(t *testing.T) { identity.ID, identity.Username), mp["message"]) assert.Equal(t, "error creating UserSignup resource", mp["details"]) - userSignup, err = hostAwait.UpdateUserSignup(t, userSignup.Name, - func(instance *toolchainv1alpha1.UserSignup) { - // Approve usersignup. - states.SetApprovedManually(instance, true) - instance.Spec.TargetCluster = memberAwait.ClusterName - }) + userSignup, err = wait.For(t, hostAwait.Awaitility, &toolchainv1alpha1.UserSignup{}). + Update(userSignup.Name, + func(instance *toolchainv1alpha1.UserSignup) { + // Approve usersignup. + states.SetApprovedManually(instance, true) + instance.Spec.TargetCluster = memberAwait.ClusterName + }) require.NoError(t, err) // Wait for the resources to be provisioned @@ -444,10 +445,11 @@ func TestSignupOK(t *testing.T) { t.Logf("Signed up new user %+v", userSignup) // Deactivate the usersignup - userSignup, err = hostAwait.UpdateUserSignup(t, userSignup.Name, - func(us *toolchainv1alpha1.UserSignup) { - states.SetDeactivated(us, true) - }) + userSignup, err = wait.For(t, hostAwait.Awaitility, &toolchainv1alpha1.UserSignup{}). + Update(userSignup.Name, + func(us *toolchainv1alpha1.UserSignup) { + states.SetDeactivated(us, true) + }) require.NoError(t, err) _, err = hostAwait.WaitForUserSignup(t, userSignup.Name, wait.UntilUserSignupHasConditions(wait.ConditionSet(wait.Default(), wait.DeactivatedWithoutPreDeactivation())...), @@ -609,11 +611,12 @@ func TestPhoneVerification(t *testing.T) { assert.Equal(t, "PendingApproval", mpStatus["reason"]) require.False(t, mpStatus["verificationRequired"].(bool)) - userSignup, err = hostAwait.UpdateUserSignup(t, userSignup.Name, - func(instance *toolchainv1alpha1.UserSignup) { - // Now approve the usersignup. - states.SetApprovedManually(instance, true) - }) + userSignup, err = wait.For(t, hostAwait.Awaitility, &toolchainv1alpha1.UserSignup{}). + Update(userSignup.Name, + func(instance *toolchainv1alpha1.UserSignup) { + // Now approve the usersignup. + states.SetApprovedManually(instance, true) + }) require.NoError(t, err) transformedUsername := commonsignup.TransformUsername(userSignup.Spec.IdentityClaims.PreferredUsername, []string{"openshift", "kube", "default", "redhat", "sandbox"}, []string{"admin"}) // Confirm the MasterUserRecord is provisioned @@ -666,11 +669,12 @@ func TestPhoneVerification(t *testing.T) { userSignup, err = hostAwait.WaitForUserSignup(t, userSignup.Name) require.NoError(t, err) - userSignup, err = hostAwait.UpdateUserSignup(t, userSignup.Name, - func(instance *toolchainv1alpha1.UserSignup) { - // Now mark the original UserSignup as deactivated - states.SetDeactivated(instance, true) - }) + userSignup, err = wait.For(t, hostAwait.Awaitility, &toolchainv1alpha1.UserSignup{}). + Update(userSignup.Name, + func(instance *toolchainv1alpha1.UserSignup) { + // Now mark the original UserSignup as deactivated + states.SetDeactivated(instance, true) + }) require.NoError(t, err) // Ensure the UserSignup is deactivated @@ -720,10 +724,11 @@ func TestActivationCodeVerification(t *testing.T) { wait.UntilUserSignupHasConditions(wait.ConditionSet(wait.Default(), wait.PendingApproval())...)) require.NoError(t, err) // explicitly approve the usersignup (see above, config for parallel test has automatic approval disabled) - userSignup, err = hostAwait.UpdateUserSignup(t, userSignup.Name, - func(us *toolchainv1alpha1.UserSignup) { - states.SetApprovedManually(us, true) - }) + userSignup, err = wait.For(t, hostAwait.Awaitility, &toolchainv1alpha1.UserSignup{}). + Update(userSignup.Name, + func(us *toolchainv1alpha1.UserSignup) { + states.SetApprovedManually(us, true) + }) require.NoError(t, err) t.Logf("user signup '%s' approved", userSignup.Name) @@ -793,8 +798,11 @@ func TestActivationCodeVerification(t *testing.T) { Status: corev1.ConditionTrue, })) // need to reload event require.NoError(t, err) - event.Status.ActivationCount = event.Spec.MaxAttendees // activation count identical to `MaxAttendees` - err = hostAwait.Client.Status().Update(context.TODO(), event) + event, err = wait.For(t, hostAwait.Awaitility, &toolchainv1alpha1.SocialEvent{}). + UpdateStatus(event.Name, + func(ev *toolchainv1alpha1.SocialEvent) { + ev.Status.ActivationCount = event.Spec.MaxAttendees // activation count identical to `MaxAttendees` + }) require.NoError(t, err) userSignup, token := signup(t, hostAwait) diff --git a/test/e2e/parallel/socialevent_test.go b/test/e2e/parallel/socialevent_test.go index 3bba42be3..c2b197863 100644 --- a/test/e2e/parallel/socialevent_test.go +++ b/test/e2e/parallel/socialevent_test.go @@ -1,7 +1,6 @@ package parallel import ( - "context" "testing" "time" @@ -75,11 +74,12 @@ func TestCreateSocialEvent(t *testing.T) { require.NoError(t, err) t.Run("update with valid tier name", func(t *testing.T) { - // given - event.Spec.UserTier = "deactivate30" - // when - err := hostAwait.Client.Update(context.TODO(), event) + event, err = For(t, hostAwait.Awaitility, &toolchainv1alpha1.SocialEvent{}). + Update(event.Name, + func(ev *toolchainv1alpha1.SocialEvent) { + ev.Spec.UserTier = "deactivate30" + }) // then require.NoError(t, err) @@ -111,11 +111,12 @@ func TestCreateSocialEvent(t *testing.T) { require.NoError(t, err) t.Run("update with valid tier name", func(t *testing.T) { - // given - event.Spec.SpaceTier = "base" - // when - err := hostAwait.Client.Update(context.TODO(), event) + event, err = For(t, hostAwait.Awaitility, &toolchainv1alpha1.SocialEvent{}). + Update(event.Name, + func(ev *toolchainv1alpha1.SocialEvent) { + ev.Spec.SpaceTier = "base" + }) // then require.NoError(t, err) diff --git a/test/e2e/parallel/space_cleanup_test.go b/test/e2e/parallel/space_cleanup_test.go index 5de3063f2..79ac00064 100644 --- a/test/e2e/parallel/space_cleanup_test.go +++ b/test/e2e/parallel/space_cleanup_test.go @@ -49,10 +49,11 @@ func TestSpaceAndSpaceBindingCleanup(t *testing.T) { // when // deactivate the UserSignup so that the MUR will be deleted - userSignup, err := hostAwait.UpdateUserSignup(t, userSignup.Name, - func(us *toolchainv1alpha1.UserSignup) { - states.SetDeactivated(us, true) - }) + userSignup, err := wait.For(t, hostAwait.Awaitility, &toolchainv1alpha1.UserSignup{}). + Update(userSignup.Name, + func(us *toolchainv1alpha1.UserSignup) { + states.SetDeactivated(us, true) + }) require.NoError(t, err) t.Logf("user signup '%s' set to deactivated", userSignup.Name) @@ -74,10 +75,11 @@ func TestSpaceAndSpaceBindingCleanup(t *testing.T) { // when // we deactivate the UserSignup so that the MUR will be deleted - userSignup, err = hostAwait.UpdateUserSignup(t, userSignup.Name, - func(us *toolchainv1alpha1.UserSignup) { - states.SetDeactivated(us, true) - }) + userSignup, err = wait.For(t, hostAwait.Awaitility, &toolchainv1alpha1.UserSignup{}). + Update(userSignup.Name, + func(us *toolchainv1alpha1.UserSignup) { + states.SetDeactivated(us, true) + }) require.NoError(t, err) t.Logf("user signup '%s' set to deactivated", userSignup.Name) diff --git a/test/e2e/parallel/space_test.go b/test/e2e/parallel/space_test.go index bd6d3d361..477e45a9a 100644 --- a/test/e2e/parallel/space_test.go +++ b/test/e2e/parallel/space_test.go @@ -203,11 +203,11 @@ func TestSpaceRoles(t *testing.T) { }) t.Run("set owner user as maintainer instead", func(t *testing.T) { - // given an appstudio space with `owner` user as an admin of it - ownerBinding.Spec.SpaceRole = "maintainer" - // when - err = hostAwait.Client.Update(context.TODO(), ownerBinding) + ownerBinding, err = hostAwait.UpdateSpaceBinding(t, ownerBinding.Name, func(sb *toolchainv1alpha1.SpaceBinding) { + // given an appstudio space with `owner` user as an admin of it + sb.Spec.SpaceRole = "maintainer" + }) require.NoError(t, err) // then @@ -222,11 +222,11 @@ func TestSpaceRoles(t *testing.T) { }) t.Run("set owner user as contributor instead", func(t *testing.T) { - // given an appstudio space with `owner` user as an admin of it - ownerBinding.Spec.SpaceRole = "contributor" - // when - err := hostAwait.Client.Update(context.TODO(), ownerBinding) + ownerBinding, err = hostAwait.UpdateSpaceBinding(t, ownerBinding.Name, func(sb *toolchainv1alpha1.SpaceBinding) { + // given an appstudio space with `owner` user as an admin of it + sb.Spec.SpaceRole = "contributor" + }) // then require.NoError(t, err) @@ -335,13 +335,12 @@ func TestSubSpaces(t *testing.T) { require.NoError(t, err) t.Run("we update role in parentSpaceBinding and expect change to be reflected in subSpaces", func(t *testing.T) { - // given - // the parentSpace role was "downgraded" to maintainer - parentSpaceBindings.Spec.SpaceRole = "maintainer" - // when // we update the parentSpace bindings - err := hostAwait.Client.Update(context.TODO(), parentSpaceBindings) + parentSpaceBindings, err = hostAwait.UpdateSpaceBinding(t, parentSpaceBindings.Name, func(sb *toolchainv1alpha1.SpaceBinding) { + // the parentSpace role was "downgraded" to maintainer + sb.Spec.SpaceRole = "maintainer" + }) // then // downgrade of the usernames is done in parentSpace diff --git a/test/e2e/parallel/usersignup_test.go b/test/e2e/parallel/usersignup_test.go index b0e2f24d5..cfe27211a 100644 --- a/test/e2e/parallel/usersignup_test.go +++ b/test/e2e/parallel/usersignup_test.go @@ -66,9 +66,10 @@ func TestTransformUsernameWithSpaceConflict(t *testing.T) { }() // now deactivate the usersignup - _, err = hostAwait.UpdateUserSignup(t, userSignup.Name, func(us *toolchainv1alpha1.UserSignup) { - states.SetDeactivated(us, true) - }) + _, err = wait.For(t, hostAwait.Awaitility, &toolchainv1alpha1.UserSignup{}). + Update(userSignup.Name, func(us *toolchainv1alpha1.UserSignup) { + states.SetDeactivated(us, true) + }) require.NoError(t, err) // wait until it is deactivated, SpaceBinding is gone, and Space is in terminating state @@ -81,9 +82,10 @@ func TestTransformUsernameWithSpaceConflict(t *testing.T) { require.NoError(t, err) // when - userSignup, err = hostAwait.UpdateUserSignup(t, userSignup.Name, func(us *toolchainv1alpha1.UserSignup) { - states.SetApprovedManually(us, true) - }) + userSignup, err = wait.For(t, hostAwait.Awaitility, &toolchainv1alpha1.UserSignup{}). + Update(userSignup.Name, func(us *toolchainv1alpha1.UserSignup) { + states.SetApprovedManually(us, true) + }) require.NoError(t, err) // then diff --git a/test/e2e/user_management_test.go b/test/e2e/user_management_test.go index a306cd55c..65d772c12 100644 --- a/test/e2e/user_management_test.go +++ b/test/e2e/user_management_test.go @@ -156,11 +156,12 @@ func (s *userManagementTestSuite) TestUserDeactivation() { Execute(t) // Delete the user's email and set them to deactivated - userSignup, err := hostAwait.UpdateUserSignup(t, uNoEmail.UserSignup.Name, - func(us *toolchainv1alpha1.UserSignup) { - us.Spec.IdentityClaims.Email = "" - states.SetDeactivated(us, true) - }) + userSignup, err := wait.For(t, hostAwait.Awaitility, &toolchainv1alpha1.UserSignup{}). + Update(uNoEmail.UserSignup.Name, + func(us *toolchainv1alpha1.UserSignup) { + us.Spec.IdentityClaims.Email = "" + states.SetDeactivated(us, true) + }) require.NoError(t, err) t.Logf("user signup '%s' set to deactivated", userSignup.Name) @@ -499,10 +500,11 @@ func (s *userManagementTestSuite) TestUserDeactivation() { require.NoError(t, err) // Now deactivate the user - userSignup, err = hostAwait.UpdateUserSignup(t, userSignup.Name, - func(us *toolchainv1alpha1.UserSignup) { - states.SetDeactivated(us, true) - }) + userSignup, err = wait.For(t, hostAwait.Awaitility, &toolchainv1alpha1.UserSignup{}). + Update(userSignup.Name, + func(us *toolchainv1alpha1.UserSignup) { + states.SetDeactivated(us, true) + }) require.NoError(t, err) userSignup, err = hostAwait.WaitForUserSignup(t, userSignup.Name, @@ -516,13 +518,14 @@ func (s *userManagementTestSuite) TestUserDeactivation() { testconfig.Deactivation().UserSignupUnverifiedRetentionDays(0)) // Reactivate the user - userSignup, err = hostAwait.UpdateUserSignup(t, userSignup.Name, - func(us *toolchainv1alpha1.UserSignup) { - states.SetDeactivating(us, false) - states.SetDeactivated(us, false) - states.SetApprovedManually(us, true) - states.SetVerificationRequired(us, true) - }) + userSignup, err = wait.For(t, hostAwait.Awaitility, &toolchainv1alpha1.UserSignup{}). + Update(userSignup.Name, + func(us *toolchainv1alpha1.UserSignup) { + states.SetDeactivating(us, false) + states.SetDeactivated(us, false) + states.SetApprovedManually(us, true) + states.SetVerificationRequired(us, true) + }) require.NoError(t, err) t.Logf("user signup '%s' reactivated", userSignup.Name) @@ -781,10 +784,11 @@ func (s *userManagementTestSuite) TestReturningUsersProvisionedToLastCluster() { // when DeactivateAndCheckUser(t, s.Awaitilities, userSignup) // If TargetCluster is set it will override the last cluster annotation so remove TargetCluster - userSignup, err := s.Host().UpdateUserSignup(t, userSignup.Name, - func(us *toolchainv1alpha1.UserSignup) { - us.Spec.TargetCluster = "" - }) + userSignup, err := wait.For(t, hostAwait.Awaitility, &toolchainv1alpha1.UserSignup{}). + Update(userSignup.Name, + func(us *toolchainv1alpha1.UserSignup) { + us.Spec.TargetCluster = "" + }) require.NoError(t, err) ReactivateAndCheckUser(t, s.Awaitilities, userSignup) diff --git a/test/e2e/usersignup_test.go b/test/e2e/usersignup_test.go index eba3abe89..2936aeb6e 100644 --- a/test/e2e/usersignup_test.go +++ b/test/e2e/usersignup_test.go @@ -405,10 +405,11 @@ func (s *userSignupIntegrationTest) TestUserResourcesUpdatedWhenPropagatedClaims VerifyResourcesProvisionedForSignup(s.T(), s.Awaitilities, userSignup) // Update the UserSignup - userSignup, err := hostAwait.UpdateUserSignup(s.T(), userSignup.Name, func(us *toolchainv1alpha1.UserSignup) { - // Modify the user's AccountID - us.Spec.IdentityClaims.AccountID = "nnnbbb111234" - }) + userSignup, err := wait.For(s.T(), hostAwait.Awaitility, &toolchainv1alpha1.UserSignup{}). + Update(userSignup.Name, func(us *toolchainv1alpha1.UserSignup) { + // Modify the user's AccountID + us.Spec.IdentityClaims.AccountID = "nnnbbb111234" + }) require.NoError(s.T(), err) // Confirm the AccountID is updated diff --git a/test/metrics/metrics_test.go b/test/metrics/metrics_test.go index 8e4dbc587..108567c16 100644 --- a/test/metrics/metrics_test.go +++ b/test/metrics/metrics_test.go @@ -147,10 +147,11 @@ func TestMetricsWhenUsersManuallyApprovedAndThenDeactivated(t *testing.T) { // when deactivating the users for username, usersignup := range signupsMember2 { - _, err := hostAwait.UpdateUserSignup(t, usersignup.Name, - func(usersignup *toolchainv1alpha1.UserSignup) { - states.SetDeactivated(usersignup, true) - }) + _, err := wait.For(t, hostAwait.Awaitility, &toolchainv1alpha1.UserSignup{}). + Update(usersignup.Name, + func(usersignup *toolchainv1alpha1.UserSignup) { + states.SetDeactivated(usersignup, true) + }) require.NoError(t, err) err = hostAwait.WaitUntilMasterUserRecordAndSpaceBindingsDeleted(t, username) @@ -221,10 +222,11 @@ func TestMetricsWhenUsersAutomaticallyApprovedAndThenDeactivated(t *testing.T) { // when deactivating the users for username, usersignup := range usersignups { - _, err := hostAwait.UpdateUserSignup(t, usersignup.Name, - func(usersignup *toolchainv1alpha1.UserSignup) { - states.SetDeactivated(usersignup, true) - }) + _, err := wait.For(t, hostAwait.Awaitility, &toolchainv1alpha1.UserSignup{}). + Update(usersignup.Name, + func(usersignup *toolchainv1alpha1.UserSignup) { + states.SetDeactivated(usersignup, true) + }) require.NoError(t, err) err = hostAwait.WaitUntilMasterUserRecordAndSpaceBindingsDeleted(t, username) @@ -310,10 +312,11 @@ func TestVerificationRequiredMetric(t *testing.T) { t.Run("no change to metric when user deactivated", func(t *testing.T) { // when deactivating the user - _, err = hostAwait.UpdateUserSignup(t, userSignup.Name, - func(usersignup *toolchainv1alpha1.UserSignup) { - states.SetDeactivated(usersignup, true) - }) + _, err := wait.For(t, hostAwait.Awaitility, &toolchainv1alpha1.UserSignup{}). + Update(userSignup.Name, + func(usersignup *toolchainv1alpha1.UserSignup) { + states.SetDeactivated(usersignup, true) + }) // then require.NoError(t, err) @@ -371,10 +374,11 @@ func TestMetricsWhenUsersDeactivatedAndReactivated(t *testing.T) { for j := 1; j < i; j++ { // deactivate and reactivate as many times as necessary (based on its "number") // deactivate the user - _, err := hostAwait.UpdateUserSignup(t, usersignups[username].Name, - func(usersignup *toolchainv1alpha1.UserSignup) { - states.SetDeactivated(usersignup, true) - }) + _, err := wait.For(t, hostAwait.Awaitility, &toolchainv1alpha1.UserSignup{}). + Update(usersignups[username].Name, + func(usersignup *toolchainv1alpha1.UserSignup) { + states.SetDeactivated(usersignup, true) + }) require.NoError(t, err) err = hostAwait.WaitUntilMasterUserRecordAndSpaceBindingsDeleted(t, username) diff --git a/test/migration/setup_runner.go b/test/migration/setup_runner.go index 68fd2c326..17aefd71b 100644 --- a/test/migration/setup_runner.go +++ b/test/migration/setup_runner.go @@ -153,10 +153,11 @@ func (r *SetupMigrationRunner) prepareDeactivatedUser(t *testing.T) { hostAwait := r.Awaitilities.Host() // deactivate the UserSignup - userSignup, err := hostAwait.UpdateUserSignup(t, userSignup.Name, - func(us *toolchainv1alpha1.UserSignup) { - states.SetDeactivated(us, true) - }) + userSignup, err := wait.For(t, hostAwait.Awaitility, &toolchainv1alpha1.UserSignup{}). + Update(userSignup.Name, + func(us *toolchainv1alpha1.UserSignup) { + states.SetDeactivated(us, true) + }) require.NoError(t, err) t.Logf("user signup '%s' set to deactivated", userSignup.Name) diff --git a/testsupport/deactivation.go b/testsupport/deactivation.go index 591c201c7..fd003bf61 100644 --- a/testsupport/deactivation.go +++ b/testsupport/deactivation.go @@ -16,10 +16,11 @@ import ( // DeactivateAndCheckUser deactivates the given UserSignups and checks that the MUR is deprovisioned func DeactivateAndCheckUser(t *testing.T, awaitilities wait.Awaitilities, userSignup *toolchainv1alpha1.UserSignup) *toolchainv1alpha1.UserSignup { hostAwait := awaitilities.Host() - userSignup, err := hostAwait.UpdateUserSignup(t, userSignup.Name, - func(us *toolchainv1alpha1.UserSignup) { - states.SetDeactivated(us, true) - }) + userSignup, err := wait.For(t, hostAwait.Awaitility, &toolchainv1alpha1.UserSignup{}). + Update(userSignup.Name, + func(us *toolchainv1alpha1.UserSignup) { + states.SetDeactivated(us, true) + }) require.NoError(t, err) t.Logf("user signup '%s' set to deactivated", userSignup.Name) @@ -62,10 +63,11 @@ func ReactivateAndCheckUser(t *testing.T, awaitilities wait.Awaitilities, userSi }, userSignup) require.NoError(t, err) - userSignup, err = hostAwait.UpdateUserSignup(t, userSignup.Name, - func(us *toolchainv1alpha1.UserSignup) { - states.SetApprovedManually(us, true) - }) + userSignup, err = wait.For(t, hostAwait.Awaitility, &toolchainv1alpha1.UserSignup{}). + Update(userSignup.Name, + func(us *toolchainv1alpha1.UserSignup) { + states.SetApprovedManually(us, true) + }) require.NoError(t, err) t.Logf("user signup '%s' reactivated", userSignup.Name) diff --git a/testsupport/signup_request.go b/testsupport/signup_request.go index aa6b71053..4d3ff7fb3 100644 --- a/testsupport/signup_request.go +++ b/testsupport/signup_request.go @@ -269,7 +269,8 @@ func (r *SignupRequest) Execute(t *testing.T) *SignupResult { } } - userSignup, err = hostAwait.UpdateUserSignup(t, userSignup.Name, doUpdate) + userSignup, err = wait.For(t, hostAwait.Awaitility, &toolchainv1alpha1.UserSignup{}). + Update(userSignup.Name, doUpdate) require.NoError(t, err) } diff --git a/testsupport/spaceprovisionerconfig/spaceprovisionerconfig.go b/testsupport/spaceprovisionerconfig/spaceprovisionerconfig.go index f5c7c6d0c..55abdbcc7 100644 --- a/testsupport/spaceprovisionerconfig/spaceprovisionerconfig.go +++ b/testsupport/spaceprovisionerconfig/spaceprovisionerconfig.go @@ -34,11 +34,15 @@ func UpdateForCluster(t *testing.T, await *wait.Awaitility, referencedClusterNam originalSpc := spc.DeepCopy() - for _, opt := range opts { - opt(spc) - } + spc, err = wait.For(t, await, &toolchainv1alpha1.SpaceProvisionerConfig{}). + Update(spc.Name, + func(spcfg *toolchainv1alpha1.SpaceProvisionerConfig) { + for _, opt := range opts { + opt(spcfg) + } + }) + require.NoError(t, err) - require.NoError(t, await.Client.Update(context.TODO(), spc)) // log spc values needed for debugging a problem with random capacity manager e2e test failures t.Logf("SPC values before the update %+v", originalSpc) t.Logf("SPC values after the update %+v", spc) @@ -54,12 +58,13 @@ func UpdateForCluster(t *testing.T, await *wait.Awaitility, referencedClusterNam require.Fail(t, err.Error()) } - // make the originalSpc look like we freshly obtained it from the server and updated its fields - // to look like the original. - originalSpc.Generation = currentSpc.Generation - originalSpc.ResourceVersion = currentSpc.ResourceVersion + _, err = wait.For(t, await, &toolchainv1alpha1.SpaceProvisionerConfig{}). + Update(originalSpc.Name, + func(spcfg *toolchainv1alpha1.SpaceProvisionerConfig) { + spcfg.Spec = originalSpc.Spec + }) - require.NoError(t, await.Client.Update(context.TODO(), originalSpc)) + require.NoError(t, err) }) } diff --git a/testsupport/tiers/tier_setup.go b/testsupport/tiers/tier_setup.go index e56676104..28b7f19dc 100644 --- a/testsupport/tiers/tier_setup.go +++ b/testsupport/tiers/tier_setup.go @@ -128,7 +128,10 @@ func UpdateCustomNSTemplateTier(t *testing.T, hostAwait *HostAwaitility, tier *C err := modify(hostAwait, tier) require.NoError(t, err) } - err = hostAwait.Client.Update(context.TODO(), tier.NSTemplateTier) + _, err = For(t, hostAwait.Awaitility, &toolchainv1alpha1.NSTemplateTier{}). + Update(tier.NSTemplateTier.Name, func(nstt *toolchainv1alpha1.NSTemplateTier) { + nstt.Spec = tier.NSTemplateTier.Spec + }) require.NoError(t, err) return tier } diff --git a/testsupport/wait/awaitility.go b/testsupport/wait/awaitility.go index bb3ae98f1..c19bd498d 100644 --- a/testsupport/wait/awaitility.go +++ b/testsupport/wait/awaitility.go @@ -892,3 +892,52 @@ func For[T client.Object](t *testing.T, a *Awaitility, obj T) *Waiter[T] { gvk: gvks[0], } } + +// doUpdate tries to update the Status or Spec of the given object +// If it fails with an error (for example if the object has been modified) then it retrieves the latest version and tries again +// Returns the updated object +func (w *Waiter[T]) doUpdate(status bool, objectName string, modify func(T)) (T, error) { + var objectToReturn T + err := wait.PollUntilContextTimeout(context.TODO(), w.await.RetryInterval, w.await.Timeout, true, func(ctx context.Context) (done bool, err error) { + obj := &unstructured.Unstructured{} + obj.SetGroupVersionKind(w.gvk) + if err := w.await.Client.Get(context.TODO(), types.NamespacedName{Namespace: w.await.Namespace, Name: objectName}, obj); err != nil { + return true, err + } + object, err := w.cast(obj) + if err != nil { + return false, fmt.Errorf("failed to cast the object to GVK %v: %w", w.gvk, err) + } + modify(object) + if status { + // Update the Status + if err := w.await.Client.Status().Update(context.TODO(), object); err != nil { + w.t.Logf("error updating '%v' Status '%s': %s. Will retry again...", w.gvk, objectName, err.Error()) + return false, nil + } + } else { + // Update the Spec + if err := w.await.Client.Update(context.TODO(), object); err != nil { + w.t.Logf("Error updating '%v' Spec '%s': %s. Will retry again...", w.gvk, objectName, err.Error()) + return false, nil + } + } + objectToReturn = object + return true, nil + }) + return objectToReturn, err +} + +// Update tries to update the given object +// If it fails with an error (for example if the object has been modified) then it retrieves the latest version and tries again +// Returns the updated object +func (w *Waiter[T]) Update(objectName string, modify func(T)) (T, error) { + return w.doUpdate(false, objectName, modify) +} + +// UpdateStatus tries to update the Status of the given object +// If it fails with an error (for example if the object has been modified) then it retrieves the latest version and tries again +// Returns the updated object +func (w *Waiter[T]) UpdateStatus(objectName string, modify func(T)) (T, error) { + return w.doUpdate(true, objectName, modify) +} diff --git a/testsupport/wait/host.go b/testsupport/wait/host.go index fdd09019e..9852429d3 100644 --- a/testsupport/wait/host.go +++ b/testsupport/wait/host.go @@ -214,28 +214,6 @@ func (a *HostAwaitility) UpdateMasterUserRecord(t *testing.T, status bool, murNa return m, err } -// UpdateUserSignup tries to update the Spec of the given UserSignup -// If it fails with an error (for example if the object has been modified) then it retrieves the latest version and tries again -// Returns the updated UserSignup -func (a *HostAwaitility) UpdateUserSignup(t *testing.T, userSignupName string, modifyUserSignup func(us *toolchainv1alpha1.UserSignup)) (*toolchainv1alpha1.UserSignup, error) { - var userSignup *toolchainv1alpha1.UserSignup - err := wait.PollUntilContextTimeout(context.TODO(), a.RetryInterval, a.Timeout, true, func(ctx context.Context) (done bool, err error) { - freshUserSignup := &toolchainv1alpha1.UserSignup{} - if err := a.Client.Get(context.TODO(), types.NamespacedName{Namespace: a.Namespace, Name: userSignupName}, freshUserSignup); err != nil { - return true, err - } - - modifyUserSignup(freshUserSignup) - if err := a.Client.Update(context.TODO(), freshUserSignup); err != nil { - t.Logf("error updating UserSignup '%s': %s. Will retry again...", userSignupName, err.Error()) - return false, nil - } - userSignup = freshUserSignup - return true, nil - }) - return userSignup, err -} - // UpdateSpace tries to update the Spec of the given Space // If it fails with an error (for example if the object has been modified) then it retrieves the latest version and tries again // Returns the updated Space