Skip to content

Commit a6c9afa

Browse files
author
Gustavo Bazan
authored
CLOUDP-191092: Fix cli clean up (#2099)
1 parent a4aeb2a commit a6c9afa

File tree

7 files changed

+91
-45
lines changed

7 files changed

+91
-45
lines changed

Makefile

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,10 @@ UNIT_TAGS?=unit
2929
INTEGRATION_TAGS?=integration
3030
E2E_TAGS?=e2e
3131
E2E_TIMEOUT?=60m
32+
E2E_PARALLEL?=1
3233

3334
export PATH := $(shell go env GOPATH)/bin:$(PATH)
3435
export PATH := ./bin:$(PATH)
35-
ifneq ($(OS),Windows_NT)
36-
export SHELL := env PATH=$(PATH) /bin/bash
37-
endif
3836
export TERM := linux-m
3937
export GO111MODULE := on
4038
export MCLI_E2E_BINARY
@@ -151,7 +149,7 @@ build-atlascli-debug: ## Generate a binary in ./bin for debugging atlascli
151149
e2e-test: build-all ## Run E2E tests
152150
@echo "==> Running E2E tests..."
153151
# the target assumes the MCLI_* environment variables are exported
154-
$(TEST_CMD) -v -p 1 -parallel 1 -timeout $(E2E_TIMEOUT) -tags="$(E2E_TAGS)" ./test/e2e...
152+
$(TEST_CMD) -v -p 1 -parallel $(E2E_PARALLEL) -timeout $(E2E_TIMEOUT) -tags="$(E2E_TAGS)" ./test/e2e...
155153

156154
.PHONY: integration-test
157155
integration-test: ## Run integration tests

build/ci/evergreen.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ functions:
9595
- AZURE_CLIENT_SECRET
9696
- E2E_TIMEOUT
9797
- E2E_SERVERLESS_INSTANCE_NAME
98+
- E2E_PARALLEL
9899
- revision
99100
env:
100101
<<: *go_env
@@ -1365,6 +1366,7 @@ tasks:
13651366
MCLI_OPS_MANAGER_URL: ${mcli_ops_manager_url}
13661367
MCLI_SERVICE: cloud
13671368
E2E_TAGS: atlas,cleanup
1369+
E2E_PARALLEL: 16
13681370
- name: atlas_gov_cleanup_e2e
13691371
tags: [ "e2e","cleanup", "foliage_check_task_only" ]
13701372
must_have_test_results: true
@@ -1384,6 +1386,7 @@ tasks:
13841386
MCLI_OPS_MANAGER_URL: ${mcli_cloud_gov_ops_manager_url}
13851387
MCLI_SERVICE: cloudgov
13861388
E2E_TAGS: atlas,cleanup
1389+
E2E_PARALLEL: 16
13871390
buildvariants:
13881391
- name: code_health
13891392
display_name: "Code Health"

internal/store/peering_connections.go

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ func (s *Store) PeeringConnections(projectID string, opts *atlas.ContainersListO
7373
ItemsPerPage(opts.ItemsPerPage).
7474
PageNum(opts.PageNum).
7575
ProviderName(opts.ProviderName).Execute()
76-
return result.Results, err
76+
if err != nil {
77+
return nil, err
78+
}
79+
return result.Results, nil
7780
default:
7881
return nil, fmt.Errorf("%w: %s", errUnsupportedService, s.service)
7982
}
@@ -121,7 +124,10 @@ func (s *Store) ContainersByProvider(projectID string, opts *atlas.ContainersLis
121124
res = res.PageNum(opts.PageNum).ItemsPerPage(opts.ItemsPerPage).ProviderName(opts.ProviderName)
122125
}
123126
result, _, err := res.Execute()
124-
return result.Results, err
127+
if err != nil {
128+
return nil, err
129+
}
130+
return result.Results, nil
125131
default:
126132
return nil, fmt.Errorf("%w: %s", errUnsupportedService, s.service)
127133
}
@@ -138,8 +144,10 @@ func (s *Store) AzureContainers(projectID string) ([]atlasv2.CloudProviderContai
138144
ItemsPerPage(maxPerPage).
139145
ProviderName("Azure").
140146
Execute()
141-
142-
return result.Results, err
147+
if err != nil {
148+
return nil, err
149+
}
150+
return result.Results, nil
143151
default:
144152
return nil, fmt.Errorf("%w: %s", errUnsupportedService, s.service)
145153
}
@@ -155,7 +163,10 @@ func (s *Store) AWSContainers(projectID string) ([]atlasv2.CloudProviderContaine
155163
ProviderName("AWS").
156164
Execute()
157165

158-
return result.Results, err
166+
if err != nil {
167+
return nil, err
168+
}
169+
return result.Results, nil
159170
default:
160171
return nil, fmt.Errorf("%w: %s", errUnsupportedService, s.service)
161172
}
@@ -170,8 +181,10 @@ func (s *Store) GCPContainers(projectID string) ([]atlasv2.CloudProviderContaine
170181
ItemsPerPage(maxPerPage).
171182
ProviderName("GCP").
172183
Execute()
173-
174-
return result.Results, err
184+
if err != nil {
185+
return nil, err
186+
}
187+
return result.Results, nil
175188
default:
176189
return nil, fmt.Errorf("%w: %s", errUnsupportedService, s.service)
177190
}
@@ -186,7 +199,10 @@ func (s *Store) AllContainers(projectID string, opts *atlas.ListOptions) ([]atla
186199
res = res.PageNum(opts.PageNum).ItemsPerPage(opts.ItemsPerPage)
187200
}
188201
result, _, err := res.Execute()
189-
return result.Results, err
202+
if err != nil {
203+
return nil, err
204+
}
205+
return result.Results, nil
190206
default:
191207
return nil, fmt.Errorf("%w: %s", errUnsupportedService, s.service)
192208
}

test/e2e/atlas/atlas_e2e_test_generator_test.go

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"time"
2626

2727
"github.com/mongodb/mongodb-atlas-cli/test/e2e"
28+
"github.com/stretchr/testify/require"
2829
atlasv2 "go.mongodb.org/atlas-sdk/v20230201002/admin"
2930
)
3031

@@ -241,9 +242,7 @@ func deleteProjectWithRetry(t *testing.T, projectID string) {
241242
t.Logf("%d/%d attempts - trying again in %d seconds: unexpected error while deleting the project %q: %v", attempts, maxRetryAttempts, backoff, projectID, e)
242243
if strings.Contains(e.Error(), "CANNOT_CLOSE_GROUP_ACTIVE_ATLAS_CLUSTERS") {
243244
cliPath, err := e2e.AtlasCLIBin()
244-
if err != nil {
245-
t.Errorf("%s: invalid bin", err)
246-
}
245+
require.NoError(t, err)
247246
deleteClustersForProject(t, cliPath, projectID)
248247
}
249248
time.Sleep(time.Duration(backoff) * time.Second)
@@ -254,6 +253,39 @@ func deleteProjectWithRetry(t *testing.T, projectID string) {
254253
}
255254
}
256255

256+
func deleteOrgInvitations(t *testing.T) {
257+
t.Helper()
258+
cliPath, err := e2e.AtlasCLIBin()
259+
require.NoError(t, err)
260+
cmd := exec.Command(cliPath,
261+
orgEntity,
262+
invitationsEntity,
263+
"ls",
264+
"-o=json")
265+
cmd.Env = os.Environ()
266+
resp, err := cmd.CombinedOutput()
267+
require.NoError(t, err, string(resp))
268+
var invitations []atlasv2.OrganizationInvitation
269+
require.NoError(t, json.Unmarshal(resp, &invitations), string(resp))
270+
t.Logf("%s\n", resp)
271+
for _, i := range invitations {
272+
deleteOrgInvitation(t, cliPath, *i.Id)
273+
}
274+
}
275+
276+
func deleteOrgInvitation(t *testing.T, cliPath string, id string) {
277+
t.Helper()
278+
cmd := exec.Command(cliPath,
279+
orgEntity,
280+
invitationsEntity,
281+
"delete",
282+
id,
283+
"--force")
284+
cmd.Env = os.Environ()
285+
resp, err := cmd.CombinedOutput()
286+
require.NoError(t, err, string(resp))
287+
}
288+
257289
func (g *atlasE2ETestGenerator) generateServerlessCluster() {
258290
g.t.Helper()
259291

test/e2e/atlas/cleanup_test.go

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package atlas_test
1818

1919
import (
2020
"encoding/json"
21+
"fmt"
2122
"os"
2223
"os/exec"
2324
"testing"
@@ -35,26 +36,33 @@ func TestCleanup(t *testing.T) {
3536
cmd := exec.Command(cliPath,
3637
projectEntity,
3738
"list",
39+
"--limit=500",
3840
"-o=json")
3941
cmd.Env = os.Environ()
4042
resp, err := cmd.CombinedOutput()
4143
req.NoError(err, string(resp))
4244

4345
var projects mongodbatlas.Projects
4446
err = json.Unmarshal(resp, &projects)
45-
req.NoError(err)
46-
t.Log(projects)
47+
req.NoError(err, string(resp))
48+
t.Logf("%s\n", resp)
49+
deleteOrgInvitations(t)
4750
for _, project := range projects.Results {
48-
if project.ID == os.Getenv("MCLI_PROJECT_ID") {
49-
t.Skip("skipping project", project.ID)
51+
projectID := project.ID
52+
if projectID == os.Getenv("MCLI_PROJECT_ID") {
53+
t.Log("skipping project", projectID)
54+
continue
5055
}
51-
deleteAllNetworkPeers(t, cliPath, project.ID, "aws")
52-
deleteAllNetworkPeers(t, cliPath, project.ID, "gcp")
53-
deleteAllNetworkPeers(t, cliPath, project.ID, "azure")
54-
deleteAllPrivateEndpoints(t, cliPath, project.ID, "aws")
55-
deleteAllPrivateEndpoints(t, cliPath, project.ID, "gcp")
56-
deleteAllPrivateEndpoints(t, cliPath, project.ID, "azure")
57-
deleteClustersForProject(t, cliPath, project.ID)
58-
deleteProjectWithRetry(t, project.ID)
56+
t.Run(fmt.Sprintf("trying to delete project %s\n", project.ID), func(t *testing.T) {
57+
t.Parallel()
58+
deleteAllNetworkPeers(t, cliPath, projectID, "aws")
59+
deleteAllNetworkPeers(t, cliPath, projectID, "gcp")
60+
deleteAllNetworkPeers(t, cliPath, projectID, "azure")
61+
deleteAllPrivateEndpoints(t, cliPath, projectID, "aws")
62+
deleteAllPrivateEndpoints(t, cliPath, projectID, "gcp")
63+
deleteAllPrivateEndpoints(t, cliPath, projectID, "azure")
64+
deleteClustersForProject(t, cliPath, projectID)
65+
deleteProjectWithRetry(t, projectID)
66+
})
5967
}
6068
}

test/e2e/atlas/helper_test.go

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ const (
5252
onlineArchiveEntity = "onlineArchives"
5353
projectEntity = "project"
5454
orgEntity = "org"
55+
invitationsEntity = "invitations"
5556
maintenanceEntity = "maintenanceWindows"
5657
integrationsEntity = "integrations"
5758
securityEntity = "security"
@@ -615,23 +616,14 @@ func deleteAllNetworkPeers(t *testing.T, cliPath, projectID, provider string) {
615616
}
616617
}
617618

619+
const sleep = 10 * time.Second
620+
618621
func deleteAllPrivateEndpoints(t *testing.T, cliPath, projectID, provider string) {
619622
t.Helper()
620623

621624
privateEndpoints := listPrivateEndpointsByProject(t, cliPath, projectID, provider)
622625
for _, endpoint := range privateEndpoints {
623-
var endpointID string
624-
625-
switch endpoint.CloudProvider {
626-
case "AWS":
627-
endpointID = endpoint.GetId()
628-
case "AZURE":
629-
endpointID = endpoint.GetId()
630-
case "GCP":
631-
endpointID = endpoint.GetId()
632-
}
633-
require.NotEmpty(t, endpointID)
634-
deletePrivateEndpoint(t, cliPath, projectID, provider, endpointID)
626+
deletePrivateEndpoint(t, cliPath, projectID, provider, endpoint.GetId())
635627
}
636628

637629
clear := false
@@ -642,11 +634,10 @@ func deleteAllPrivateEndpoints(t *testing.T, cliPath, projectID, provider string
642634
clear = true
643635
break
644636
}
645-
646-
time.Sleep(10 * time.Second)
637+
time.Sleep(sleep)
647638
}
648639

649-
require.True(t, clear)
640+
require.True(t, clear, "failed to clean all private endpoints")
650641
}
651642

652643
func listPrivateEndpointsByProject(t *testing.T, cliPath, projectID, provider string) []atlasv2.EndpointService {

test/e2e/iam/atlas_org_invitations_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ import (
3030

3131
func TestAtlasOrgInvitations(t *testing.T) {
3232
cliPath, err := e2e.AtlasCLIBin()
33-
if err != nil {
34-
t.Fatalf("unexpected error: %v", err)
35-
}
33+
require.NoError(t, err)
3634

3735
n, err := e2e.RandInt(1000)
3836
require.NoError(t, err)

0 commit comments

Comments
 (0)