Skip to content

Commit fe01392

Browse files
authored
CLOUDP-122569: e2e aftereach global key deletion (#554)
1 parent 27113c6 commit fe01392

File tree

8 files changed

+54
-22
lines changed

8 files changed

+54
-22
lines changed

.github/actions/cleanup/entrypoint.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,12 @@ delete_all() {
116116
BASE_URL="https://cloud-qa.mongodb.com/api/atlas/v1.0"
117117

118118
get_api_keys() {
119-
curl -s -u "${INPUT_ATLAS_PUBLIC_KEY}:${INPUT_ATLAS_PRIVATE_KEY}" --digest "${BASE_URL}/orgs/${MCLI_ORG_ID}/apiKeys"
119+
curl -s -u "${MCLI_PUBLIC_API_KEY}:${MCLI_PRIVATE_API_KEY}" --digest "${BASE_URL}/orgs/${MCLI_ORG_ID}/apiKeys"
120120
}
121121

122122
delete_test_apikeys() {
123123
API_KEY_ID=$1
124-
curl -s -u "${INPUT_ATLAS_PUBLIC_KEY}:${INPUT_ATLAS_PRIVATE_KEY}" --digest --request DELETE "${BASE_URL}/orgs/${MCLI_ORG_ID}/apiKeys/${API_KEY_ID}"
124+
curl -s -u "${MCLI_PUBLIC_API_KEY}:${MCLI_PRIVATE_API_KEY}" --digest --request DELETE "${BASE_URL}/orgs/${MCLI_ORG_ID}/apiKeys/${API_KEY_ID}"
125125
}
126126

127127
# ------------------------------------------------------------------------------
@@ -160,7 +160,7 @@ if [[ "${INPUT_CLEAN_ALL:-}" == "true" ]]; then
160160
echo "Please, remember running tests will fail (run CLEAN_ALL = false, if need soft deletion)"
161161
test_description="created from the AO test"
162162
all_keys=$(get_api_keys)
163-
for key in $(echo "$all_keys" | jq '.results | keys | .[]'); do
163+
for key in $(echo "$all_keys" | jq 'select(.results | length > 0) | .results | keys | .[]'); do
164164
element=$(echo "$all_keys" | jq ".results[$key]")
165165
desc=$(echo "$element" | jq -r '.desc')
166166
if [[ "${desc}" == "${test_description}" ]]; then

test/e2e/actions/steps.go

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func CheckIfUserExist(username, projecID string) func() bool {
115115
}
116116
}
117117

118-
func CompareClustersSpec(requested model.ClusterSpec, created mongodbatlas.Cluster) { // TODO
118+
func CompareClustersSpec(requested model.ClusterSpec, created mongodbatlas.Cluster) {
119119
ExpectWithOffset(1, created).To(MatchFields(IgnoreExtras, Fields{
120120
"MongoURI": Not(BeEmpty()),
121121
"MongoURIWithOptions": Not(BeEmpty()),
@@ -350,12 +350,12 @@ func CreateConnectionAtlasKey(data *model.TestDataProvider) {
350350
})
351351
}
352352

353-
func createConnectionAtlasKeyFrom(data *model.TestDataProvider, public, private string) {
353+
func createConnectionAtlasKeyFrom(data *model.TestDataProvider, key *mongodbatlas.APIKey) {
354354
By("Change resources depends on AtlasKey and create key", func() {
355355
if data.Resources.AtlasKeyAccessType.GlobalLevelKey {
356-
kubecli.CreateApiKeySecretFrom(config.DefaultOperatorGlobalKey, data.Resources.Namespace, os.Getenv("MCLI_ORG_ID"), public, private)
356+
kubecli.CreateApiKeySecretFrom(config.DefaultOperatorGlobalKey, data.Resources.Namespace, os.Getenv("MCLI_ORG_ID"), key.PublicKey, key.PrivateKey)
357357
} else {
358-
kubecli.CreateApiKeySecretFrom(data.Resources.KeyName, data.Resources.Namespace, os.Getenv("MCLI_ORG_ID"), public, private)
358+
kubecli.CreateApiKeySecretFrom(data.Resources.KeyName, data.Resources.Namespace, os.Getenv("MCLI_ORG_ID"), key.PublicKey, key.PrivateKey)
359359
}
360360
})
361361
}
@@ -364,13 +364,14 @@ func recreateAtlasKeyIfNeed(data *model.TestDataProvider) {
364364
if !data.Resources.AtlasKeyAccessType.IsFullAccess() {
365365
aClient, err := atlas.AClient()
366366
Expect(err).ShouldNot(HaveOccurred())
367-
public, private, err := aClient.AddKeyWithAccessList(data.Resources.ProjectID, data.Resources.AtlasKeyAccessType.Roles, data.Resources.AtlasKeyAccessType.Whitelist)
367+
globalKey, err := aClient.AddKeyWithAccessList(data.Resources.ProjectID, data.Resources.AtlasKeyAccessType.Roles, data.Resources.AtlasKeyAccessType.Whitelist)
368368
Expect(err).ShouldNot(HaveOccurred())
369-
Expect(public).ShouldNot(BeEmpty())
370-
Expect(private).ShouldNot(BeEmpty())
369+
Expect(globalKey.PublicKey).ShouldNot(BeEmpty())
370+
Expect(globalKey.PrivateKey).ShouldNot(BeEmpty())
371+
data.Resources.AtlasKeyAccessType.GlobalKeyAttached = globalKey
371372

372373
kubecli.DeleteApiKeySecret(data.Resources.KeyName, data.Resources.Namespace)
373-
createConnectionAtlasKeyFrom(data, public, private)
374+
createConnectionAtlasKeyFrom(data, globalKey)
374375
}
375376
}
376377

@@ -436,7 +437,7 @@ func DeployUserResourcesAction(data *model.TestDataProvider) {
436437
DeployUsers(data)
437438
}
438439

439-
func DeleteDBUsersApps(data *model.TestDataProvider) {
440+
func DeleteDBUsersApps(data model.TestDataProvider) {
440441
By("Delete dbusers applications", func() {
441442
for _, user := range data.Resources.Users {
442443
helm.Uninstall("test-app-"+user.Spec.Username, data.Resources.Namespace)
@@ -471,11 +472,23 @@ func DeleteUserResourcesProject(data *model.TestDataProvider) {
471472
})
472473
}
473474

475+
func DeleteGlobalKeyIfExist(data model.TestDataProvider) {
476+
if data.Resources.AtlasKeyAccessType.GlobalLevelKey {
477+
By("Delete Global API key for test", func() {
478+
client, err := atlas.AClient()
479+
Expect(err).ShouldNot(HaveOccurred())
480+
err = client.DeleteGlobalKey(*data.Resources.AtlasKeyAccessType.GlobalKeyAttached)
481+
Expect(err).ShouldNot(HaveOccurred())
482+
})
483+
}
484+
}
485+
474486
func AfterEachFinalCleanup(datas []model.TestDataProvider) {
475-
for i := range datas {
487+
for _, data := range datas {
476488
GinkgoWriter.Write([]byte("AfterEach. Final cleanup...\n"))
477-
DeleteDBUsersApps(&datas[i])
478-
Expect(kubecli.DeleteNamespace(datas[i].Resources.Namespace)).Should(Say("deleted"), "Cant delete namespace after testing")
489+
DeleteDBUsersApps(data)
490+
Expect(kubecli.DeleteNamespace(data.Resources.Namespace)).Should(Say("deleted"), "Cant delete namespace after testing")
491+
DeleteGlobalKeyIfExist(data)
479492
GinkgoWriter.Write([]byte("AfterEach. Cleanup finished\n"))
480493
}
481494
}

test/e2e/api/atlas/atlas.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ type Atlas struct {
2424
Client *mongodbatlas.Client
2525
}
2626

27+
const (
28+
keyDescription = "created from the AO test"
29+
)
30+
2731
func AClient() (Atlas, error) {
2832
var A Atlas
2933
A.OrgID = os.Getenv("MCLI_ORG_ID")
@@ -40,21 +44,21 @@ func AClient() (Atlas, error) {
4044
return A, nil
4145
}
4246

43-
func (a *Atlas) AddKeyWithAccessList(projectID string, roles, access []string) (public string, private string, err error) {
47+
func (a *Atlas) AddKeyWithAccessList(projectID string, roles, access []string) (*mongodbatlas.APIKey, error) {
4448
createKeyRequest := &mongodbatlas.APIKeyInput{
45-
Desc: "created from the AO test",
49+
Desc: keyDescription,
4650
Roles: roles,
4751
}
4852
newKey, _, err := a.Client.ProjectAPIKeys.Create(context.Background(), projectID, createKeyRequest)
4953
if err != nil {
50-
return "", "", err
54+
return nil, err
5155
}
5256
createAccessRequest := formAccessRequest(access)
5357
_, _, err = a.Client.WhitelistAPIKeys.Create(context.Background(), a.OrgID, newKey.ID, createAccessRequest)
5458
if err != nil {
55-
return "", "", err
59+
return nil, err
5660
}
57-
return newKey.PublicKey, newKey.PrivateKey, nil
61+
return newKey, nil
5862
}
5963

6064
func formAccessRequest(access []string) []*mongodbatlas.WhitelistAPIKeysReq {
@@ -117,6 +121,13 @@ func (a *Atlas) GetUserByName(database, projectID, username string) (*mongodbatl
117121
if err != nil {
118122
return nil, err
119123
}
120-
121124
return dbUser, nil
122125
}
126+
127+
func (a *Atlas) DeleteGlobalKey(key mongodbatlas.APIKey) error {
128+
_, err := a.Client.APIKeys.Delete(context.Background(), a.OrgID, key.ID)
129+
if err != nil {
130+
return err
131+
}
132+
return nil
133+
}

test/e2e/helm_chart_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ var _ = Describe("HELM charts", func() {
5757
data.Resources.Namespace,
5858
)
5959
actions.SaveTestAppLogs(data.Resources)
60+
actions.DeleteGlobalKeyIfExist(data)
6061
}
6162
})
6263
})

test/e2e/integration_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ var _ = Describe("Configuration namespaced. Deploy cluster", Label("integration-
5151
data.Resources.Namespace,
5252
)
5353
actions.DeleteUserResourcesProject(&data)
54+
actions.DeleteGlobalKeyIfExist(data)
5455
}
5556
})
5657

test/e2e/model/atlas_key_type.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package model
22

3+
import "go.mongodb.org/atlas/mongodbatlas"
4+
35
type AtlasRoles = string
46

57
const (
@@ -18,10 +20,12 @@ const (
1820
)
1921

2022
type AtlasKeyType struct {
21-
GlobalLevelKey bool // if true, tests create "<operator-deployment-name>-api-key"
2223
DefaultFullAccessKey bool // use full access key provided with github secrets
2324
Roles []AtlasRoles // specify role for non default
2425
Whitelist []string
26+
27+
GlobalLevelKey bool // if true, tests create "<operator-deployment-name>-api-key"
28+
GlobalKeyAttached *mongodbatlas.APIKey
2529
}
2630

2731
func NewAtlasKeyType(r []AtlasRoles, wl []string) *AtlasKeyType {

test/e2e/multinamespace_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ var _ = Describe("Users can use clusterwide configuration with limitation to wat
4141
data.Resources.Namespace,
4242
)
4343
}
44+
actions.AfterEachFinalCleanup(listData)
4445
}
4546
})
4647
})

test/e2e/private_link_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ var _ = Describe("UserLogin", Label("privatelink"), func() {
7171
})
7272
By("Delete Resources, Project with PEService", func() {
7373
actions.DeleteUserResourcesProject(&data)
74+
actions.DeleteGlobalKeyIfExist(data)
7475
})
7576
})
7677

0 commit comments

Comments
 (0)