@@ -2,17 +2,20 @@ package int
2
2
3
3
import (
4
4
"context"
5
+ "errors"
5
6
"fmt"
6
7
"net/http"
7
8
"time"
8
9
9
10
mdbv1 "github.com/mongodb/mongodb-atlas-kubernetes/pkg/api/v1"
10
11
"github.com/mongodb/mongodb-atlas-kubernetes/pkg/api/v1/status"
12
+ "github.com/mongodb/mongodb-atlas-kubernetes/pkg/controller/atlas"
11
13
"github.com/mongodb/mongodb-atlas-kubernetes/pkg/controller/workflow"
12
14
"github.com/mongodb/mongodb-atlas-kubernetes/pkg/util/kube"
13
15
"github.com/mongodb/mongodb-atlas-kubernetes/pkg/util/testutil"
14
16
. "github.com/onsi/ginkgo"
15
17
. "github.com/onsi/gomega"
18
+ "go.mongodb.org/atlas/mongodbatlas"
16
19
corev1 "k8s.io/api/core/v1"
17
20
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
18
21
)
@@ -59,21 +62,24 @@ var _ = Describe("AtlasCluster", func() {
59
62
AfterEach (func () {
60
63
if createdProject != nil && createdProject .Status .ID != "" {
61
64
if createdCluster != nil {
62
- By ("Removing Atlas Cluster " + createdCluster .Spec .Name )
63
- _ , _ = atlasClient .Clusters .Delete (context .Background (), createdProject .Status .ID , createdCluster .Spec .Name )
65
+ By ("Removing Atlas Cluster " + createdCluster .Name )
66
+ Expect (k8sClient .Delete (context .Background (), createdCluster )).To (Succeed ())
67
+
68
+ Eventually (checkAtlasClusterRemoved (createdProject .Status .ID , createdCluster .Name ), 600 , interval ).Should (BeTrue ())
64
69
}
65
- // TODO need to wait for the cluster to get removed
66
- // By("Removing Atlas Project " + createdProject.Status.ID)
67
- // _, err := atlasClient.Projects.Delete(context.Background(), createdProject.Status.ID)
68
- // Expect(err).ToNot(HaveOccurred())
70
+ By ("Removing Atlas Project " + createdProject .Status .ID )
71
+ // This is a bit strange but the delete request right after the cluster is removed may fail with "Still active cluster" error
72
+ // UI shows the cluster being deleted though. Seems to be the issue only if removal is done using API,
73
+ // if the cluster is terminated using UI - it stays in "Deleting" state
74
+ Eventually (removeAtlasProject (createdProject .Status .ID ), 600 , interval ).Should (BeTrue ())
69
75
}
70
76
71
77
By ("Removing the namespace " + namespace .Name )
72
78
err := k8sClient .Delete (context .Background (), & namespace )
73
79
Expect (err ).ToNot (HaveOccurred ())
74
80
})
75
81
76
- Describe ("Create/Update/Delete the cluster" , func () {
82
+ Describe ("Create/Update the cluster" , func () {
77
83
It ("Should Succeed" , func () {
78
84
expectedCluster := testAtlasCluster (namespace .Name , "test-cluster" , createdProject .Name )
79
85
@@ -137,16 +143,12 @@ var _ = Describe("AtlasCluster", func() {
137
143
Expect (err ).ToNot (HaveOccurred ())
138
144
139
145
Expect (atlasCluster .Name ).To (Equal (createdAtlasCluster .Name ))
146
+ print (createdCluster .Labels )
147
+ print (createdAtlasCluster .Labels )
140
148
Expect (atlasCluster .Labels ).To (Equal (createdAtlasCluster .Labels ))
141
149
Expect (atlasCluster .ProviderSettings .InstanceSizeName ).To (Equal (createdAtlasCluster .ProviderSettings .InstanceSizeName ))
142
150
Expect (atlasCluster .ProviderSettings .ProviderName ).To (Equal (createdAtlasCluster .ProviderSettings .ProviderName ))
143
151
Expect (atlasCluster .ProviderSettings .RegionName ).To (Equal (createdAtlasCluster .ProviderSettings .RegionName ))
144
-
145
- By ("Deleting the cluster" )
146
- err = k8sClient .Delete (context .Background (), createdCluster )
147
- Expect (err ).ToNot (HaveOccurred ())
148
-
149
- Eventually (checkAtlasDeleteStarted (createdProject .Status .ID , createdCluster .Name ), 1200 , interval ).Should (BeTrue ())
150
152
})
151
153
})
152
154
})
@@ -182,7 +184,7 @@ func testAtlasCluster(namespace, name, projectName string) *mdbv1.AtlasCluster {
182
184
Namespace : namespace ,
183
185
},
184
186
Spec : mdbv1.AtlasClusterSpec {
185
- Name : "test-cluster" ,
187
+ Name : "test-atlas- cluster" ,
186
188
Project : mdbv1.ResourceRef {Name : projectName },
187
189
ProviderSettings : & mdbv1.ProviderSettingsSpec {
188
190
InstanceSizeName : "M10" ,
@@ -192,18 +194,31 @@ func testAtlasCluster(namespace, name, projectName string) *mdbv1.AtlasCluster {
192
194
},
193
195
}
194
196
}
195
- func checkAtlasDeleteStarted (projectID string , clusterName string ) func () bool {
197
+
198
+ // checkAtlasClusterRemoved returns true if the Atlas Cluster is removed from Atlas. Note the behavior: the cluster
199
+ // is removed from Atlas as soon as the DELETE API call has been made. This is different from the case when the
200
+ // cluster is terminated from UI (in this case GET request succeeds while the cluster is being terminated)
201
+ func checkAtlasClusterRemoved (projectID string , clusterName string ) func () bool {
196
202
return func () bool {
197
- c , r , err := atlasClient .Clusters .Get (context .Background (), projectID , clusterName )
203
+ _ , r , err := atlasClient .Clusters .Get (context .Background (), projectID , clusterName )
198
204
if err != nil {
199
- // cluster already deleted - that's fine for us
200
205
if r != nil && r .StatusCode == http .StatusNotFound {
201
206
return true
202
207
}
208
+ }
209
+ return false
210
+ }
211
+ }
203
212
213
+ func removeAtlasProject (projectID string ) func () bool {
214
+ return func () bool {
215
+ _ , err := atlasClient .Projects .Delete (context .Background (), projectID )
216
+ if err != nil {
217
+ var apiError * mongodbatlas.ErrorResponse
218
+ Expect (errors .As (err , & apiError )).To (BeTrue ())
219
+ Expect (apiError .ErrorCode ).To (Equal (atlas .CannotCloseGroupActiveAtlasCluster ))
204
220
return false
205
221
}
206
-
207
- return c .StateName == "DELETING" || c .StateName == "DELETED"
222
+ return true
208
223
}
209
224
}
0 commit comments