Skip to content

Commit 7bfeb24

Browse files
authored
CLOUDP-131043: Delete atlas projects on a cron job (#1391)
* CLOUDP-131043: Delete atlas projects on a cron job * Keep deleting on defer * Add missing continue
1 parent 6bd50a1 commit 7bfeb24

File tree

4 files changed

+120
-12
lines changed

4 files changed

+120
-12
lines changed

build/ci/evergreen.yml

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,7 @@ tasks:
349349
- command: shell.exec
350350
type: test
351351
params:
352-
env:
353-
XDG_CONFIG_HOME: ${go_base_path}${workdir}
352+
<<: *go_options
354353
script: |
355354
set -Eeou pipefail
356355
mkdir "$XDG_CONFIG_HOME/mongocli"
@@ -1270,6 +1269,44 @@ tasks:
12701269
E2E_TAGS: atlas,backup
12711270
E2E_CLOUD_ROLE_ID: ${e2e_cloud_role_id}
12721271
E2E_TEST_BUCKET: ${e2e_test_bucket}
1272+
- name: atlas_cleanup_e2e
1273+
tags: [ "e2e","cleanup" ]
1274+
must_have_test_results: true
1275+
depends_on:
1276+
- name: compile
1277+
variant: "code_health"
1278+
patch_optional: true
1279+
commands:
1280+
- func: "clone"
1281+
- func: "install gotestsum"
1282+
- func: "e2e test"
1283+
vars:
1284+
MCLI_ORG_ID: ${atlas_org_id}
1285+
MCLI_PROJECT_ID: ${atlas_project_id}
1286+
MCLI_PRIVATE_API_KEY: ${atlas_private_api_key}
1287+
MCLI_PUBLIC_API_KEY: ${atlas_public_api_key}
1288+
MCLI_OPS_MANAGER_URL: ${mcli_ops_manager_url}
1289+
MCLI_SERVICE: cloud
1290+
E2E_TAGS: atlas,cleanup
1291+
- name: atlas_gov_cleanup_e2e
1292+
tags: [ "e2e","cleanup" ]
1293+
must_have_test_results: true
1294+
depends_on:
1295+
- name: compile
1296+
variant: "code_health"
1297+
patch_optional: true
1298+
commands:
1299+
- func: "clone"
1300+
- func: "install gotestsum"
1301+
- func: "e2e test"
1302+
vars:
1303+
MCLI_ORG_ID: ${atlas_gov_org_id}
1304+
MCLI_PROJECT_ID: ${atlas_gov_project_id}
1305+
MCLI_PRIVATE_API_KEY: ${atlas_gov_private_api_key}
1306+
MCLI_PUBLIC_API_KEY: ${atlas_gov_public_api_key}
1307+
MCLI_OPS_MANAGER_URL: ${mcli_cloud_gov_ops_manager_url}
1308+
MCLI_SERVICE: cloudgov
1309+
E2E_TAGS: atlas,cleanup
12731310
buildvariants:
12741311
- name: code_health
12751312
display_name: "Code Health"
@@ -1391,6 +1428,17 @@ buildvariants:
13911428
go_base_path: ""
13921429
tasks:
13931430
- name: ".e2e .clusters .cloudmanager"
1431+
- name: e2e_cleanup
1432+
display_name: "E2E Cleanup Resources"
1433+
run_on:
1434+
- rhel80-small
1435+
expansions:
1436+
go_root: "/opt/golang/go1.18"
1437+
go_bin: "/opt/golang/go1.18/bin"
1438+
go_base_path: ""
1439+
tasks:
1440+
- name: ".e2e .cleanup"
1441+
cron: "0 2 * * *" # 2am daily
13941442
- name: e2e_ops_manager_44
13951443
display_name: "E2E Ops Manager 4.4 Tests"
13961444
run_on:

internal/store/store.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ response:
289289
%v %v
290290
%v
291291
%v
292-
`, resp.Request.Method, resp.Request.URL.Path, resp.Proto, resp.Status, respHeaders, string(resp.Raw))
292+
`, resp.Request.Method, resp.Request.URL.String(), resp.Proto, resp.Status, respHeaders, string(resp.Raw))
293293
})
294294
s.client = c
295295
return nil

test/e2e/atlas/atlas_e2e_test_generator_test.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,24 +76,26 @@ func (g *atlasE2ETestGenerator) generateProject(prefix string) {
7676
}
7777

7878
g.t.Cleanup(func() {
79-
g.deleteProjectWithRetry()
79+
deleteProjectWithRetry(g.t, g.projectID)
8080
})
8181
}
8282

83-
func (g *atlasE2ETestGenerator) deleteProjectWithRetry() {
84-
var attempts int
85-
for attempts = 1; attempts <= maxRetryAttempts; attempts++ {
86-
if e := deleteProject(g.projectID); e != nil {
87-
g.t.Logf("%d/%d attempts - trying again in %d seconds: unexpected error while deleting the project '%s': %v", attempts, maxRetryAttempts, sleepTimeInSeconds, g.projectID, e)
83+
func deleteProjectWithRetry(t *testing.T, projectID string) {
84+
t.Helper()
85+
deleted := false
86+
for attempts := 1; attempts <= maxRetryAttempts; attempts++ {
87+
if e := deleteProject(projectID); e != nil {
88+
t.Logf("%d/%d attempts - trying again in %d seconds: unexpected error while deleting the project '%s': %v", attempts, maxRetryAttempts, sleepTimeInSeconds, projectID, e)
8889
time.Sleep(sleepTimeInSeconds * time.Second)
8990
} else {
90-
g.t.Logf("project '%s' successfully deleted", g.projectID)
91+
t.Logf("project '%s' successfully deleted", projectID)
92+
deleted = true
9193
break
9294
}
9395
}
9496

95-
if attempts > maxRetryAttempts {
96-
g.t.Fatalf("we could not delete the project '%s' (projectId: '%s')", g.projectName, g.projectID)
97+
if !deleted {
98+
t.Errorf("we could not delete the project '%s'", projectID)
9799
}
98100
}
99101

test/e2e/atlas/cleanup_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2020 MongoDB Inc
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//go:build e2e || (atlas && cleanup)
15+
16+
package atlas_test
17+
18+
import (
19+
"encoding/json"
20+
"fmt"
21+
"os"
22+
"testing"
23+
24+
"github.com/mongodb/mongodb-atlas-cli/test/e2e"
25+
"github.com/stretchr/testify/require"
26+
"go.mongodb.org/atlas/mongodbatlas"
27+
exec "golang.org/x/sys/execabs"
28+
)
29+
30+
func TestCleanup(t *testing.T) {
31+
req := require.New(t)
32+
cliPath, err := e2e.AtlasCLIBin()
33+
req.NoError(err)
34+
35+
cmd := exec.Command(cliPath,
36+
projectEntity,
37+
"list",
38+
"-o=json")
39+
cmd.Env = os.Environ()
40+
resp, err := cmd.CombinedOutput()
41+
req.NoError(err, resp)
42+
43+
var projects mongodbatlas.Projects
44+
err = json.Unmarshal(resp, &projects)
45+
req.NoError(err)
46+
47+
for _, project := range projects.Results {
48+
if project.ID == os.Getenv("MCLI_PROJECT_ID") {
49+
fmt.Println("skipping project", project.ID)
50+
continue
51+
}
52+
t.Run("Project "+project.ID, func(t *testing.T) {
53+
deleteProjectWithRetry(t, project.ID)
54+
})
55+
}
56+
57+
fmt.Println(projects)
58+
}

0 commit comments

Comments
 (0)