Skip to content

Commit 16ffc23

Browse files
adding e2e test replicating pause and unpause
1 parent 73d97e7 commit 16ffc23

File tree

3 files changed

+258
-1
lines changed

3 files changed

+258
-1
lines changed

.github/workflows/e2e-testing.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ jobs:
6363
MONGODB_ATLAS_SECRET_PROFILE: cfn-cloud-dev-github-action
6464
run: |
6565
cd cfn-resources/test/e2e/cluster
66-
go test -timeout 90m -v cluster_test.go
66+
go test -timeout 90m -v cluster_test.go cluster_pause_test.go
6767
6868

6969
flex-cluster:
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"AWSTemplateFormatVersion": "2010-09-09",
3+
"Description": "Minimal template to exercise pausing/unpausing a single REPLICASET cluster",
4+
"Resources": {
5+
"Cluster": {
6+
"Type": "{{ .ResourceTypeName }}",
7+
"Properties": {
8+
"Name": "{{ .Name }}",
9+
"ProjectId": "{{ .ProjectID }}",
10+
"Profile": "{{ .Profile }}",
11+
"ClusterType": "REPLICASET",
12+
"Paused": "{{ .Paused }}",
13+
"ReplicationSpecs": [
14+
{
15+
"NumShards": 1,
16+
"AdvancedRegionConfigs": [
17+
{
18+
"RegionName": "US_EAST_1",
19+
"Priority": 7,
20+
"ProviderName": "AWS",
21+
"ElectableSpecs": {
22+
"EbsVolumeType": "STANDARD",
23+
"InstanceSize": "M10",
24+
"NodeCount": 3
25+
}
26+
}
27+
]
28+
}
29+
]
30+
}
31+
}
32+
},
33+
"Outputs": {
34+
"MongoDBAtlasClusterID": {
35+
"Description": "Cluster Id",
36+
"Value": { "Fn::GetAtt": ["Cluster", "Id"] }
37+
}
38+
}
39+
}
40+
41+
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
// Copyright 2024 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+
package cluster_test
15+
16+
import (
17+
ctx "context"
18+
"os"
19+
"testing"
20+
21+
"github.com/aws/aws-sdk-go-v2/service/cloudformation"
22+
"github.com/mongodb/mongodbatlas-cloudformation-resources/test/e2e/utility"
23+
"github.com/stretchr/testify/assert"
24+
admin20231115014 "go.mongodb.org/atlas-sdk/v20231115014/admin"
25+
)
26+
27+
type pauseTestContext struct {
28+
cfnClient *cloudformation.Client
29+
atlasClient20231115014 *admin20231115014.APIClient
30+
resourceCtx utility.ResourceContext
31+
template string
32+
clusterTmplObj pauseTestCluster
33+
}
34+
35+
type pauseTestCluster struct {
36+
ResourceTypeName string
37+
Name string
38+
Profile string
39+
ProjectID string
40+
Paused string
41+
}
42+
43+
const (
44+
pauseResourceDirectory = "cluster"
45+
pauseCfnTemplatePath = "cluster_pause.json.template"
46+
)
47+
48+
// Replication specs are hardcoded in the CFN template for this test.
49+
50+
var (
51+
pauseProfile = os.Getenv("MONGODB_ATLAS_SECRET_PROFILE")
52+
pauseOrgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
53+
pauseRandSuffix = utility.GetRandNum().String()
54+
pauseProjectName = "cfn-e2e-cluster-pause" + pauseRandSuffix
55+
pauseClusterName = "cfn-e2e-cluster-pause" + pauseRandSuffix
56+
pauseStackName = "stack-cluster-pause-e2e-" + pauseRandSuffix
57+
)
58+
59+
func TestClusterPauseCFN(t *testing.T) {
60+
testCtx := setupPauseSuite(t)
61+
62+
t.Run("Validate Template", func(t *testing.T) {
63+
utility.TestIsTemplateValid(t, testCtx.cfnClient, testCtx.template)
64+
})
65+
66+
t.Run("Create Stack", func(t *testing.T) {
67+
testCreatePauseStack(t, testCtx)
68+
})
69+
70+
t.Run("Pause Cluster", func(t *testing.T) {
71+
testUpdatePauseState(t, testCtx, true)
72+
})
73+
74+
t.Run("Unpause Cluster", func(t *testing.T) {
75+
testUpdatePauseState(t, testCtx, false)
76+
})
77+
78+
t.Run("Delete Stack", func(t *testing.T) {
79+
testDeletePauseStack(t, testCtx)
80+
})
81+
}
82+
83+
func setupPauseSuite(t *testing.T) *pauseTestContext {
84+
t.Helper()
85+
t.Log("Setting up pause suite")
86+
testCtx := new(pauseTestContext)
87+
testCtx.setUp(t)
88+
return testCtx
89+
}
90+
91+
func (c *pauseTestContext) setUp(t *testing.T) {
92+
t.Helper()
93+
c.resourceCtx = utility.InitResourceCtx(pauseStackName, pauseRandSuffix, os.Getenv("RESOURCE_TYPE_NAME_FOR_E2E"), pauseResourceDirectory)
94+
c.cfnClient, _ = utility.NewClients(t)
95+
_, c.atlasClient20231115014 = utility.NewClients20231115014(t)
96+
97+
utility.PublishToPrivateRegistry(t, c.resourceCtx)
98+
c.setupPrerequisites(t)
99+
}
100+
101+
func (c *pauseTestContext) setupPrerequisites(t *testing.T) {
102+
t.Helper()
103+
t.Cleanup(func() {
104+
cleanupPausePrerequisites(t, c)
105+
cleanupPauseResources(t, c)
106+
})
107+
t.Log("Setting up prerequisites for pause test")
108+
109+
var projectID string
110+
if projectIDEnvVar := os.Getenv("MONGODB_ATLAS_PROJECT_ID"); projectIDEnvVar != "" {
111+
t.Logf("using projectID from env var %s", projectIDEnvVar)
112+
projectID = projectIDEnvVar
113+
} else {
114+
projectID = utility.CreateProject(t, c.atlasClient20231115014, pauseOrgID, pauseProjectName)
115+
}
116+
117+
c.clusterTmplObj = pauseTestCluster{
118+
Name: pauseClusterName,
119+
ProjectID: projectID,
120+
Profile: pauseProfile,
121+
Paused: "false",
122+
ResourceTypeName: os.Getenv("RESOURCE_TYPE_NAME_FOR_E2E"),
123+
}
124+
125+
var err error
126+
c.template, err = newPauseCFNTemplate(c.clusterTmplObj)
127+
utility.FailNowIfError(t, "Error while reading pause CFN Template: %v", err)
128+
t.Logf("Pause test setup complete. ProjectID: %s, ClusterName: %s", c.clusterTmplObj.ProjectID, c.clusterTmplObj.Name)
129+
}
130+
131+
func newPauseCFNTemplate(tmpl pauseTestCluster) (string, error) {
132+
return utility.ExecuteGoTemplate(pauseCfnTemplatePath, tmpl)
133+
}
134+
135+
func testCreatePauseStack(t *testing.T, c *pauseTestContext) {
136+
t.Helper()
137+
t.Logf("Creating pause stack with template:\n%s", c.template)
138+
139+
output := utility.CreateStack(t, c.cfnClient, pauseStackName, c.template)
140+
clusterID := getPauseClusterIDFromStack(output)
141+
142+
cluster := readPauseClusterFromAtlas(t, c)
143+
144+
a := assert.New(t)
145+
a.Equal(cluster.GetId(), clusterID)
146+
a.False(cluster.GetPaused())
147+
}
148+
149+
func testUpdatePauseState(t *testing.T, c *pauseTestContext, pause bool) {
150+
t.Helper()
151+
152+
if pause {
153+
c.clusterTmplObj.Paused = "true"
154+
} else {
155+
c.clusterTmplObj.Paused = "false"
156+
}
157+
158+
var err error
159+
c.template, err = newPauseCFNTemplate(c.clusterTmplObj)
160+
utility.FailNowIfError(t, "Error while reading pause CFN Template: %v", err)
161+
162+
output := utility.UpdateStack(t, c.cfnClient, pauseStackName, c.template)
163+
_ = getPauseClusterIDFromStack(output)
164+
165+
cluster := readPauseClusterFromAtlas(t, c)
166+
167+
a := assert.New(t)
168+
if pause {
169+
a.True(cluster.GetPaused())
170+
} else {
171+
a.False(cluster.GetPaused())
172+
}
173+
}
174+
175+
func testDeletePauseStack(t *testing.T, c *pauseTestContext) {
176+
t.Helper()
177+
utility.DeleteStack(t, c.cfnClient, pauseStackName)
178+
a := assert.New(t)
179+
_, resp, _ := c.atlasClient20231115014.ClustersApi.GetCluster(ctx.Background(), c.clusterTmplObj.ProjectID, c.clusterTmplObj.Name).Execute()
180+
a.Equal(404, resp.StatusCode)
181+
}
182+
183+
func cleanupPauseResources(t *testing.T, c *pauseTestContext) {
184+
t.Helper()
185+
utility.DeleteStackForCleanup(t, c.cfnClient, pauseStackName)
186+
}
187+
188+
func cleanupPausePrerequisites(t *testing.T, c *pauseTestContext) {
189+
t.Helper()
190+
t.Log("Cleaning up pause test prerequisites")
191+
if os.Getenv("MONGODB_ATLAS_PROJECT_ID") == "" {
192+
utility.DeleteProject(t, c.atlasClient20231115014, c.clusterTmplObj.ProjectID)
193+
} else {
194+
t.Log("skipping project deletion (project managed outside of test)")
195+
}
196+
}
197+
198+
func readPauseClusterFromAtlas(t *testing.T, c *pauseTestContext) *admin20231115014.AdvancedClusterDescription {
199+
t.Helper()
200+
context := ctx.Background()
201+
projectID := c.clusterTmplObj.ProjectID
202+
cluster, resp, err := c.atlasClient20231115014.ClustersApi.GetCluster(context, projectID, c.clusterTmplObj.Name).Execute()
203+
utility.FailNowIfError(t, "Err while retrieving Cluster from Atlas: %v", err)
204+
assert.Equal(t, 200, resp.StatusCode)
205+
return cluster
206+
}
207+
208+
func getPauseClusterIDFromStack(output *cloudformation.DescribeStacksOutput) string {
209+
stackOutputs := output.Stacks[0].Outputs
210+
for i := 0; i < len(stackOutputs); i++ {
211+
if *stackOutputs[i].OutputKey == "MongoDBAtlasClusterID" {
212+
return *stackOutputs[i].OutputValue
213+
}
214+
}
215+
return ""
216+
}

0 commit comments

Comments
 (0)