Skip to content

Commit

Permalink
CLOUDP-191092: Fix cli clean up (#2099)
Browse files Browse the repository at this point in the history
  • Loading branch information
gssbzn authored Jul 20, 2023
1 parent a4aeb2a commit a6c9afa
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 45 deletions.
6 changes: 2 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@ UNIT_TAGS?=unit
INTEGRATION_TAGS?=integration
E2E_TAGS?=e2e
E2E_TIMEOUT?=60m
E2E_PARALLEL?=1

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

.PHONY: integration-test
integration-test: ## Run integration tests
Expand Down
3 changes: 3 additions & 0 deletions build/ci/evergreen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ functions:
- AZURE_CLIENT_SECRET
- E2E_TIMEOUT
- E2E_SERVERLESS_INSTANCE_NAME
- E2E_PARALLEL
- revision
env:
<<: *go_env
Expand Down Expand Up @@ -1365,6 +1366,7 @@ tasks:
MCLI_OPS_MANAGER_URL: ${mcli_ops_manager_url}
MCLI_SERVICE: cloud
E2E_TAGS: atlas,cleanup
E2E_PARALLEL: 16
- name: atlas_gov_cleanup_e2e
tags: [ "e2e","cleanup", "foliage_check_task_only" ]
must_have_test_results: true
Expand All @@ -1384,6 +1386,7 @@ tasks:
MCLI_OPS_MANAGER_URL: ${mcli_cloud_gov_ops_manager_url}
MCLI_SERVICE: cloudgov
E2E_TAGS: atlas,cleanup
E2E_PARALLEL: 16
buildvariants:
- name: code_health
display_name: "Code Health"
Expand Down
32 changes: 24 additions & 8 deletions internal/store/peering_connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ func (s *Store) PeeringConnections(projectID string, opts *atlas.ContainersListO
ItemsPerPage(opts.ItemsPerPage).
PageNum(opts.PageNum).
ProviderName(opts.ProviderName).Execute()
return result.Results, err
if err != nil {
return nil, err
}
return result.Results, nil
default:
return nil, fmt.Errorf("%w: %s", errUnsupportedService, s.service)
}
Expand Down Expand Up @@ -121,7 +124,10 @@ func (s *Store) ContainersByProvider(projectID string, opts *atlas.ContainersLis
res = res.PageNum(opts.PageNum).ItemsPerPage(opts.ItemsPerPage).ProviderName(opts.ProviderName)
}
result, _, err := res.Execute()
return result.Results, err
if err != nil {
return nil, err
}
return result.Results, nil
default:
return nil, fmt.Errorf("%w: %s", errUnsupportedService, s.service)
}
Expand All @@ -138,8 +144,10 @@ func (s *Store) AzureContainers(projectID string) ([]atlasv2.CloudProviderContai
ItemsPerPage(maxPerPage).
ProviderName("Azure").
Execute()

return result.Results, err
if err != nil {
return nil, err
}
return result.Results, nil
default:
return nil, fmt.Errorf("%w: %s", errUnsupportedService, s.service)
}
Expand All @@ -155,7 +163,10 @@ func (s *Store) AWSContainers(projectID string) ([]atlasv2.CloudProviderContaine
ProviderName("AWS").
Execute()

return result.Results, err
if err != nil {
return nil, err
}
return result.Results, nil
default:
return nil, fmt.Errorf("%w: %s", errUnsupportedService, s.service)
}
Expand All @@ -170,8 +181,10 @@ func (s *Store) GCPContainers(projectID string) ([]atlasv2.CloudProviderContaine
ItemsPerPage(maxPerPage).
ProviderName("GCP").
Execute()

return result.Results, err
if err != nil {
return nil, err
}
return result.Results, nil
default:
return nil, fmt.Errorf("%w: %s", errUnsupportedService, s.service)
}
Expand All @@ -186,7 +199,10 @@ func (s *Store) AllContainers(projectID string, opts *atlas.ListOptions) ([]atla
res = res.PageNum(opts.PageNum).ItemsPerPage(opts.ItemsPerPage)
}
result, _, err := res.Execute()
return result.Results, err
if err != nil {
return nil, err
}
return result.Results, nil
default:
return nil, fmt.Errorf("%w: %s", errUnsupportedService, s.service)
}
Expand Down
38 changes: 35 additions & 3 deletions test/e2e/atlas/atlas_e2e_test_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"time"

"github.com/mongodb/mongodb-atlas-cli/test/e2e"
"github.com/stretchr/testify/require"
atlasv2 "go.mongodb.org/atlas-sdk/v20230201002/admin"
)

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

func deleteOrgInvitations(t *testing.T) {
t.Helper()
cliPath, err := e2e.AtlasCLIBin()
require.NoError(t, err)
cmd := exec.Command(cliPath,
orgEntity,
invitationsEntity,
"ls",
"-o=json")
cmd.Env = os.Environ()
resp, err := cmd.CombinedOutput()
require.NoError(t, err, string(resp))
var invitations []atlasv2.OrganizationInvitation
require.NoError(t, json.Unmarshal(resp, &invitations), string(resp))
t.Logf("%s\n", resp)
for _, i := range invitations {
deleteOrgInvitation(t, cliPath, *i.Id)
}
}

func deleteOrgInvitation(t *testing.T, cliPath string, id string) {
t.Helper()
cmd := exec.Command(cliPath,
orgEntity,
invitationsEntity,
"delete",
id,
"--force")
cmd.Env = os.Environ()
resp, err := cmd.CombinedOutput()
require.NoError(t, err, string(resp))
}

func (g *atlasE2ETestGenerator) generateServerlessCluster() {
g.t.Helper()

Expand Down
32 changes: 20 additions & 12 deletions test/e2e/atlas/cleanup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package atlas_test

import (
"encoding/json"
"fmt"
"os"
"os/exec"
"testing"
Expand All @@ -35,26 +36,33 @@ func TestCleanup(t *testing.T) {
cmd := exec.Command(cliPath,
projectEntity,
"list",
"--limit=500",
"-o=json")
cmd.Env = os.Environ()
resp, err := cmd.CombinedOutput()
req.NoError(err, string(resp))

var projects mongodbatlas.Projects
err = json.Unmarshal(resp, &projects)
req.NoError(err)
t.Log(projects)
req.NoError(err, string(resp))
t.Logf("%s\n", resp)
deleteOrgInvitations(t)
for _, project := range projects.Results {
if project.ID == os.Getenv("MCLI_PROJECT_ID") {
t.Skip("skipping project", project.ID)
projectID := project.ID
if projectID == os.Getenv("MCLI_PROJECT_ID") {
t.Log("skipping project", projectID)
continue
}
deleteAllNetworkPeers(t, cliPath, project.ID, "aws")
deleteAllNetworkPeers(t, cliPath, project.ID, "gcp")
deleteAllNetworkPeers(t, cliPath, project.ID, "azure")
deleteAllPrivateEndpoints(t, cliPath, project.ID, "aws")
deleteAllPrivateEndpoints(t, cliPath, project.ID, "gcp")
deleteAllPrivateEndpoints(t, cliPath, project.ID, "azure")
deleteClustersForProject(t, cliPath, project.ID)
deleteProjectWithRetry(t, project.ID)
t.Run(fmt.Sprintf("trying to delete project %s\n", project.ID), func(t *testing.T) {
t.Parallel()
deleteAllNetworkPeers(t, cliPath, projectID, "aws")
deleteAllNetworkPeers(t, cliPath, projectID, "gcp")
deleteAllNetworkPeers(t, cliPath, projectID, "azure")
deleteAllPrivateEndpoints(t, cliPath, projectID, "aws")
deleteAllPrivateEndpoints(t, cliPath, projectID, "gcp")
deleteAllPrivateEndpoints(t, cliPath, projectID, "azure")
deleteClustersForProject(t, cliPath, projectID)
deleteProjectWithRetry(t, projectID)
})
}
}
21 changes: 6 additions & 15 deletions test/e2e/atlas/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const (
onlineArchiveEntity = "onlineArchives"
projectEntity = "project"
orgEntity = "org"
invitationsEntity = "invitations"
maintenanceEntity = "maintenanceWindows"
integrationsEntity = "integrations"
securityEntity = "security"
Expand Down Expand Up @@ -615,23 +616,14 @@ func deleteAllNetworkPeers(t *testing.T, cliPath, projectID, provider string) {
}
}

const sleep = 10 * time.Second

func deleteAllPrivateEndpoints(t *testing.T, cliPath, projectID, provider string) {
t.Helper()

privateEndpoints := listPrivateEndpointsByProject(t, cliPath, projectID, provider)
for _, endpoint := range privateEndpoints {
var endpointID string

switch endpoint.CloudProvider {
case "AWS":
endpointID = endpoint.GetId()
case "AZURE":
endpointID = endpoint.GetId()
case "GCP":
endpointID = endpoint.GetId()
}
require.NotEmpty(t, endpointID)
deletePrivateEndpoint(t, cliPath, projectID, provider, endpointID)
deletePrivateEndpoint(t, cliPath, projectID, provider, endpoint.GetId())
}

clear := false
Expand All @@ -642,11 +634,10 @@ func deleteAllPrivateEndpoints(t *testing.T, cliPath, projectID, provider string
clear = true
break
}

time.Sleep(10 * time.Second)
time.Sleep(sleep)
}

require.True(t, clear)
require.True(t, clear, "failed to clean all private endpoints")
}

func listPrivateEndpointsByProject(t *testing.T, cliPath, projectID, provider string) []atlasv2.EndpointService {
Expand Down
4 changes: 1 addition & 3 deletions test/e2e/iam/atlas_org_invitations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ import (

func TestAtlasOrgInvitations(t *testing.T) {
cliPath, err := e2e.AtlasCLIBin()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
require.NoError(t, err)

n, err := e2e.RandInt(1000)
require.NoError(t, err)
Expand Down

0 comments on commit a6c9afa

Please sign in to comment.