Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 40 additions & 25 deletions test/e2e/backuprestores/backup_restores_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,35 @@ func TestRestores(t *testing.T) {
g.GenerateProjectAndCluster("backupRestores")
require.NotEmpty(t, g.ClusterName)

g2 := internal.NewAtlasE2ETestGenerator(t, internal.WithSnapshot(), internal.WithBackup())
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reverts 2951dd7

g2.GenerateProjectAndCluster("backupRestores2")
require.NotEmpty(t, g2.ClusterName)
sourceProjectID := g.ProjectID
sourceClusterName := g.ClusterName

g.ProjectID = ""
g.ClusterName = ""

g.GenerateProjectAndCluster("backupRestores2")
require.NotEmpty(t, g.ClusterName)

targetProjectID := g.ProjectID
targetClusterName := g.ClusterName

t.Cleanup(func() {
require.NoError(t, internal.DeleteClusterForProject(sourceProjectID, sourceClusterName))
internal.DeleteProjectWithRetry(t, sourceProjectID)
require.NoError(t, internal.DeleteClusterForProject(targetProjectID, targetClusterName))
internal.DeleteProjectWithRetry(t, targetProjectID)
})

g.Run("Create snapshot", func(t *testing.T) { //nolint:thelper // g.Run replaces t.Run
cmd := exec.Command(cliPath,
backupsEntity,
snapshotsEntity,
"create",
g.ClusterName,
sourceClusterName,
"--desc",
"test-snapshot",
"--projectId",
g.ProjectID,
sourceProjectID,
"-o=json")
cmd.Env = os.Environ()
resp, err := internal.RunAndGetStdOut(cmd)
Expand All @@ -77,9 +92,9 @@ func TestRestores(t *testing.T) {
"watch",
snapshotID,
"--clusterName",
g.ClusterName,
sourceClusterName,
"--projectId",
g.ProjectID)
sourceProjectID)
cmd.Env = os.Environ()
resp, _ := internal.RunAndGetStdOut(cmd)
t.Log(string(resp))
Expand All @@ -92,15 +107,15 @@ func TestRestores(t *testing.T) {
"start",
"automated",
"--clusterName",
g.ClusterName,
sourceClusterName,
"--snapshotId",
snapshotID,
"--projectId",
g.ProjectID,
sourceProjectID,
"--targetProjectId",
g2.ProjectID,
targetProjectID,
"--targetClusterName",
g2.ClusterName,
targetClusterName,
"-o=json")
cmd.Env = os.Environ()
resp, err := internal.RunAndGetStdOut(cmd)
Expand All @@ -118,9 +133,9 @@ func TestRestores(t *testing.T) {
"watch",
restoreJobID,
"--clusterName",
g.ClusterName,
sourceClusterName,
"--projectId",
g.ProjectID,
sourceProjectID,
"-o=json")
cmd.Env = os.Environ()
resp, err := internal.RunAndGetStdOut(cmd)
Expand All @@ -133,9 +148,9 @@ func TestRestores(t *testing.T) {
backupsEntity,
restoresEntity,
"list",
g.ClusterName,
sourceClusterName,
"--projectId",
g.ProjectID,
sourceProjectID,
"-o=json")
cmd.Env = os.Environ()
resp, err := internal.RunAndGetStdOut(cmd)
Expand All @@ -153,9 +168,9 @@ func TestRestores(t *testing.T) {
"describe",
restoreJobID,
"--clusterName",
g.ClusterName,
sourceClusterName,
"--projectId",
g.ProjectID,
sourceProjectID,
"-o=json")
cmd.Env = os.Environ()
resp, err := internal.RunAndGetStdOut(cmd)
Expand All @@ -174,11 +189,11 @@ func TestRestores(t *testing.T) {
"start",
"download",
"--clusterName",
g.ClusterName,
sourceClusterName,
"--snapshotId",
snapshotID,
"--projectId",
g.ProjectID,
sourceProjectID,
"-o=json")
cmd.Env = os.Environ()
resp, err := internal.RunAndGetStdOut(cmd)
Expand All @@ -196,9 +211,9 @@ func TestRestores(t *testing.T) {
"watch",
restoreJobID,
"--clusterName",
g.ClusterName,
sourceClusterName,
"--projectId",
g.ProjectID,
sourceProjectID,
"-o=json")
cmd.Env = os.Environ()
resp, err := internal.RunAndGetStdOut(cmd)
Expand All @@ -213,9 +228,9 @@ func TestRestores(t *testing.T) {
"delete",
snapshotID,
"--clusterName",
g.ClusterName,
sourceClusterName,
"--projectId",
g.ProjectID,
sourceProjectID,
"--force")
cmd.Env = os.Environ()
resp, err := internal.RunAndGetStdOut(cmd)
Expand All @@ -233,9 +248,9 @@ func TestRestores(t *testing.T) {
"watch",
snapshotID,
"--clusterName",
g.ClusterName,
sourceClusterName,
"--projectId",
g.ProjectID)
sourceProjectID)
cmd.Env = os.Environ()
resp, _ := internal.RunAndGetStdOut(cmd)
t.Log(string(resp))
Expand Down
18 changes: 14 additions & 4 deletions test/internal/atlas_e2e_test_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,8 @@ func (g *AtlasE2ETestGenerator) GenerateProject(prefix string) {
}

g.t.Cleanup(func() {
deleteProjectWithRetry(g.t, g.ProjectID)
g.Logf("Project cleanup %q", g.ProjectID)
DeleteProjectWithRetry(g.t, g.ProjectID)
})
}

Expand Down Expand Up @@ -335,10 +336,19 @@ func (g *AtlasE2ETestGenerator) generateClusterWithPrefix(prefix string) {
}

g.t.Cleanup(func() {
g.Logf("Cluster cleanup %q\n", g.ProjectID)
if e := DeleteClusterForProject(g.ProjectID, g.ClusterName); e != nil {
g.t.Errorf("unexpected error deleting cluster: %v", e)
g.Logf("Cluster cleanup for project %q\n", g.ProjectID)
err := DeleteClusterForProject(g.ProjectID, g.ClusterName)
if err == nil {
g.t.Logf("Cluster %q was deleted", g.ClusterName)
return
}

if strings.Contains(err.Error(), "CLUSTER_NOT_FOUND") || strings.Contains(err.Error(), "GROUP_NOT_FOUND") {
g.t.Logf("Cluster %q was already deleted", g.ClusterName)
return
}

g.t.Errorf("unexpected error deleting cluster: %v", err)
})
}

Expand Down
2 changes: 1 addition & 1 deletion test/internal/cleanup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func TestCleanup(t *testing.T) {
t.Run("trying to delete project "+projectID, func(t *testing.T) {
t.Parallel()
t.Cleanup(func() {
deleteProjectWithRetry(t, projectID)
DeleteProjectWithRetry(t, projectID)
deleteKeys(t, cliPath, getKeysToDelete())
})
for _, provider := range []string{"aws", "gcp", "azure"} {
Expand Down
14 changes: 7 additions & 7 deletions test/internal/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ func RandInt(maximum int64) (*big.Int, error) {
return rand.Int(rand.Reader, big.NewInt(maximum))
}

func deleteProjectWithRetry(t *testing.T, projectID string) {
// DeleteProjectWithRetry deletes a project with a retry backoff strategy.
func DeleteProjectWithRetry(t *testing.T, projectID string) {
t.Helper()
deleted := false
backoff := 1
Expand All @@ -165,6 +166,7 @@ func deleteProjectWithRetry(t *testing.T, projectID string) {
deleted = true
break
}

t.Logf("%d/%d attempts - trying again in %d seconds: unexpected error while deleting the project %q: %v", attempts, maxRetryAttempts, backoff, projectID, e)
time.Sleep(time.Duration(backoff) * time.Second)
backoff *= 2
Expand Down Expand Up @@ -321,19 +323,16 @@ func internalDeleteClusterForProject(projectID, clusterName string) error {
"delete",
clusterName,
"--force",
"--watch",
}
if projectID != "" {
args = append(args, "--projectId", projectID)
}
deleteCmd := exec.Command(cliPath, args...)
deleteCmd.Env = os.Environ()
if resp, err := RunAndGetStdOut(deleteCmd); err != nil {
if resp, err := RunAndGetStdOutAndErr(deleteCmd); err != nil {
return fmt.Errorf("error deleting cluster %w: %s", err, string(resp))
}

// this command will fail with 404 once the cluster is deleted
// we just need to wait for this to close the project
_ = WatchCluster(projectID, clusterName)
return nil
}

Expand Down Expand Up @@ -386,6 +385,7 @@ func DeleteClusterForProject(projectID, clusterName string) error {
if !strings.Contains(err.Error(), "CANNOT_TERMINATE_CLUSTER_WHEN_TERMINATION_PROTECTION_ENABLED") {
return err
}

if err := removeTerminationProtectionFromCluster(projectID, clusterName); err != nil {
return err
}
Expand Down Expand Up @@ -878,7 +878,7 @@ func deleteProject(projectID string) error {
projectID,
"--force")
cmd.Env = os.Environ()
resp, err := RunAndGetStdOut(cmd)
resp, err := RunAndGetStdOutAndErr(cmd)
if err != nil {
return fmt.Errorf("%s (%w)", string(resp), err)
}
Expand Down
Loading