Skip to content

Commit

Permalink
CLOUDP-265086: Add PAK deletion to cleanup (#3211)
Browse files Browse the repository at this point in the history
  • Loading branch information
blva authored Aug 23, 2024
1 parent a911d78 commit 1d6150a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 31 deletions.
50 changes: 50 additions & 0 deletions test/e2e/atlas/atlas_e2e_test_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,56 @@ func deleteProjectWithRetry(t *testing.T, projectID string) {
}
}

// list of keys to delete as clean up.
func getKeysToDelete() map[string]struct{} {
return map[string]struct{}{
"mongodb-atlas-operator-api-key": {},
"e2e-test-helper": {},
"e2e-test-atlas-org": {},
}
}

func deleteKeys(t *testing.T, cliPath string, toDelete map[string]struct{}) {
t.Helper()

cmd := exec.Command(cliPath,
orgEntity,
"apiKeys",
"ls",
"-o=json")

cmd.Env = os.Environ()
resp, err := e2e.RunAndGetStdOut(cmd)
require.NoError(t, err, string(resp))

var keys atlasv2.PaginatedApiApiUser
err = json.Unmarshal(resp, &keys)
require.NoError(t, err)
for _, key := range keys.GetResults() {
keyID := key.GetId()
desc := key.GetDesc()

errors := []error{}
if _, ok := toDelete[desc]; ok {
t.Logf("Deleting key with ID: %s", keyID)
cmd = exec.Command(cliPath,
orgEntity,
"apiKeys",
"rm",
keyID,
"--force")
cmd.Env = os.Environ()
_, err = e2e.RunAndGetStdOut(cmd)
if err != nil {
errors = append(errors, err)
}
}
if len(errors) > 0 {
t.Errorf("unexpected errors while deleting keys: %v", errors)
}
}
}

func deleteOrgInvitations(t *testing.T, cliPath string) {
t.Helper()
cmd := exec.Command(cliPath,
Expand Down
4 changes: 4 additions & 0 deletions test/e2e/atlas/cleanup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ func TestCleanup(t *testing.T) {
t.Parallel()
deleteAllDataFederations(t, cliPath, projectID)
})
t.Cleanup(func() {
deleteKeys(t, cliPath, getKeysToDelete())
})

t.Log("skip deleting default project", projectID)
continue
Expand All @@ -74,6 +77,7 @@ func TestCleanup(t *testing.T) {
t.Parallel()
t.Cleanup(func() {
deleteProjectWithRetry(t, projectID)
deleteKeys(t, cliPath, getKeysToDelete())
})
for _, provider := range []string{"aws", "gcp", "azure"} {
p := provider
Expand Down
32 changes: 1 addition & 31 deletions test/e2e/atlas/kubernetes_operator_install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package atlas_test

import (
"encoding/json"
"fmt"
"os"
"os/exec"
Expand All @@ -29,7 +28,6 @@ import (
akov2 "github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/api/v1"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
atlasv2 "go.mongodb.org/atlas-sdk/v20240530005/admin"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -444,33 +442,5 @@ func cleanUpKeys(t *testing.T, operator *operatorHelper, namespace string, cliPa
toDelete[secret.Name] = struct{}{}
}

cmd := exec.Command(cliPath,
orgEntity,
"apiKeys",
"ls",
"-o=json")
cmd.Env = os.Environ()
resp, err := e2e.RunAndGetStdOut(cmd)
require.NoError(t, err, string(resp))

var keys atlasv2.PaginatedApiApiUser
err = json.Unmarshal(resp, &keys)
require.NoError(t, err)

for _, key := range keys.GetResults() {
keyID := *key.Id
desc := *key.Desc

if _, ok := toDelete[desc]; ok {
cmd = exec.Command(cliPath,
orgEntity,
"apiKeys",
"rm",
keyID,
"--force")
cmd.Env = os.Environ()
_, err = e2e.RunAndGetStdOut(cmd)
require.NoError(t, err)
}
}
deleteKeys(t, cliPath, toDelete)
}

0 comments on commit 1d6150a

Please sign in to comment.