|
| 1 | +package e2e |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + commonauth "github.com/codeready-toolchain/toolchain-common/pkg/test/auth" |
| 9 | + "github.com/codeready-toolchain/toolchain-e2e/testsupport" |
| 10 | + authsupport "github.com/codeready-toolchain/toolchain-e2e/testsupport/auth" |
| 11 | + "github.com/codeready-toolchain/toolchain-e2e/testsupport/space" |
| 12 | + "github.com/codeready-toolchain/toolchain-e2e/testsupport/wait" |
| 13 | + "github.com/google/uuid" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | + "github.com/stretchr/testify/suite" |
| 16 | +) |
| 17 | + |
| 18 | +type namespaceResetFeatureIntegrationSuite struct { |
| 19 | + suite.Suite |
| 20 | + wait.Awaitilities |
| 21 | +} |
| 22 | + |
| 23 | +func TestNamespacesResetE2E(t *testing.T) { |
| 24 | + suite.Run(t, &namespaceResetFeatureIntegrationSuite{}) |
| 25 | +} |
| 26 | + |
| 27 | +// SetupSuite sets up the required preconditions for the tests to run. |
| 28 | +func (s *namespaceResetFeatureIntegrationSuite) SetupSuite() { |
| 29 | + s.Awaitilities = testsupport.WaitForDeployments(s.T()) |
| 30 | +} |
| 31 | + |
| 32 | +// TestResetNamespaces tests that the "namespace reset" endpoint works as |
| 33 | +// expected. |
| 34 | +func (s *namespaceResetFeatureIntegrationSuite) TestResetNamespaces() { |
| 35 | + // given |
| 36 | + memberAwaitily := s.Member1() |
| 37 | + |
| 38 | + // Create a new user signup for the test. |
| 39 | + userUuid, err := uuid.NewUUID() |
| 40 | + require.NoError(s.T(), err, "unable to generate user uuid") |
| 41 | + |
| 42 | + userOne := testsupport.NewSignupRequest(s.Awaitilities). |
| 43 | + IdentityID(userUuid). |
| 44 | + Username("userone"). |
| 45 | + Email("userone@redhat.com"). |
| 46 | + ManuallyApprove(). |
| 47 | + EnsureMUR(). |
| 48 | + TargetCluster(s.Member1()). |
| 49 | + RequireConditions(wait.ConditionSet(wait.Default(), wait.ApprovedByAdmin())...). |
| 50 | + Execute(s.T()) |
| 51 | + |
| 52 | + // Get the user's "NSTemplateSet" and its provisioned namespaces for the |
| 53 | + // later checks. We are storing the creation timestamp to make sure that |
| 54 | + // it is different from the one of the freshly created namespaces. |
| 55 | + _, nsTemplateSet := space.VerifyResourcesProvisionedForSpace(s.T(), s.Awaitilities, userOne.Space.Name) |
| 56 | + provisionedNamespaces := nsTemplateSet.Status.ProvisionedNamespaces |
| 57 | + |
| 58 | + namespacesCreationTimestamp := make(map[string]time.Time) |
| 59 | + for _, ns := range provisionedNamespaces { |
| 60 | + namespace, err := memberAwaitily.WaitForNamespaceWithName(s.T(), ns.Name) |
| 61 | + require.NoError(s.T(), err, "unable to fetch namespace") |
| 62 | + |
| 63 | + namespacesCreationTimestamp[ns.Name] = namespace.CreationTimestamp.Time |
| 64 | + } |
| 65 | + |
| 66 | + // Create a token and identity to invoke the "reset-namespace" with. |
| 67 | + userIdentity := &commonauth.Identity{ |
| 68 | + ID: userUuid, |
| 69 | + Username: userOne.UserSignup.Status.CompliantUsername, |
| 70 | + } |
| 71 | + claims := []commonauth.ExtraClaim{commonauth.WithEmailClaim(userOne.UserSignup.Spec.IdentityClaims.Email)} |
| 72 | + claims = append(claims, commonauth.WithUserIDClaim(userIdentity.ID.String())) |
| 73 | + claims = append(claims, commonauth.WithAccountIDClaim("999111")) |
| 74 | + claims = append(claims, commonauth.WithGivenNameClaim("John")) |
| 75 | + claims = append(claims, commonauth.WithFamilyNameClaim("Doe")) |
| 76 | + claims = append(claims, commonauth.WithCompanyClaim("Red Hat")) |
| 77 | + claims = append(claims, commonauth.WithAccountNumberClaim("2222")) |
| 78 | + |
| 79 | + token, err := authsupport.NewTokenFromIdentity(userIdentity, claims...) |
| 80 | + require.NoError(s.T(), err, `ùnable to create the token for the "namespace reset" request`) |
| 81 | + |
| 82 | + // when |
| 83 | + // Call the endpoint under test. |
| 84 | + testsupport.NewHTTPRequest(s.T()). |
| 85 | + InvokeEndpoint(http.MethodPost, s.Host().RegistrationServiceURL+"/api/v1/reset-namespaces", token, "", http.StatusAccepted) |
| 86 | + |
| 87 | + // then |
| 88 | + // Wait for the user's namespaces to reach the "terminating" status. |
| 89 | + for _, pns := range provisionedNamespaces { |
| 90 | + _, err := memberAwaitily.WaitForNamespaceInTerminating(s.T(), pns.Name) |
| 91 | + require.NoError(s.T(), err, `unexpected error when waiting for the namespace "%s" to be terminated`, pns.Name) |
| 92 | + } |
| 93 | + |
| 94 | + // Verify that the namespaces are provisioned again... |
| 95 | + nsTmplSet, err := memberAwaitily.WaitForNSTmplSet(s.T(), userOne.UserSignup.Status.CompliantUsername, wait.UntilNSTemplateSetHasConditions(wait.Provisioned())) |
| 96 | + require.NoError(s.T(), err, `unexpected error when waiting for the "NSTemplateSet" to become "provisioned"`) |
| 97 | + |
| 98 | + // ... and double check that the namespaces are active. |
| 99 | + for _, namespace := range nsTmplSet.Spec.Namespaces { |
| 100 | + fetchedNamespace, err := memberAwaitily.WaitForNamespace(s.T(), userOne.UserSignup.Status.CompliantUsername, namespace.TemplateRef, nsTmplSet.Spec.TierName, wait.UntilNamespaceIsActive()) |
| 101 | + require.NoError(s.T(), err, `unexpected error when waiting for the namespace "%s" to become active`, namespace.TemplateRef) |
| 102 | + |
| 103 | + timestamp, ok := namespacesCreationTimestamp[fetchedNamespace.Name] |
| 104 | + if !ok { |
| 105 | + require.FailNow(s.T(), "mismatch in the namespace provisioning", `the recreated namespace "%s" was not part of the original provisioned namespaces' list: %#v'`, fetchedNamespace.Name, provisionedNamespaces) |
| 106 | + } |
| 107 | + |
| 108 | + require.NotEqual(s.T(), timestamp, fetchedNamespace.CreationTimestamp.Time, `the namespace "%s" appears to not have been recreated due to having the same creation timestamp as before`, fetchedNamespace.Name) |
| 109 | + } |
| 110 | +} |
0 commit comments