@@ -19,19 +19,14 @@ import (
19
19
akov2 "github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/api/v1"
20
20
"github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/api/v1/common"
21
21
"github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/api/v1/status"
22
+ "github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/controller/customresource"
22
23
"github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/controller/workflow"
23
24
)
24
25
25
26
func TestUpdateTeamState (t * testing.T ) {
26
27
t .Run ("should not duplicate projects listed" , func (t * testing.T ) {
27
28
logger := zaptest .NewLogger (t ).Sugar ()
28
- workflowCtx := & workflow.Context {
29
- Context : context .Background (),
30
- Log : logger ,
31
- }
32
- testScheme := runtime .NewScheme ()
33
- akov2 .AddToScheme (testScheme )
34
- corev1 .AddToScheme (testScheme )
29
+ workflowCtx := defaultTestWorkflow (logger )
35
30
secret := & corev1.Secret {
36
31
ObjectMeta : metav1.ObjectMeta {
37
32
Name : "my-secret" ,
@@ -74,38 +69,25 @@ func TestUpdateTeamState(t *testing.T) {
74
69
return & mongodbatlas.Client {}, "0987654321" , nil
75
70
},
76
71
}
77
- k8sClient := fake .NewClientBuilder ().
78
- WithScheme (testScheme ).
79
- WithObjects (secret , project , team ).
80
- Build ()
72
+ k8sClient := buildFakeKubernetesClient (secret , project , team )
81
73
reconciler := & AtlasProjectReconciler {
82
74
Client : k8sClient ,
83
75
Log : logger ,
84
76
AtlasProvider : atlasProvider ,
85
77
}
86
- teamRef := & common.ResourceRefNamespaced {
87
- Name : team .Name ,
88
- Namespace : "testNS" ,
89
- }
90
78
// check we have exactly 1 project in status
91
79
assert .Equal (t , 1 , len (team .Status .Projects ))
92
80
93
81
// "reconcile" the team state and check we still have 1 project in status
94
- err := reconciler .updateTeamState (workflowCtx , project , teamRef , false )
82
+ err := reconciler .updateTeamState (workflowCtx , project , reference ( team ) , false )
95
83
assert .NoError (t , err )
96
84
k8sClient .Get (context .Background (), types.NamespacedName {Name : team .ObjectMeta .Name , Namespace : team .ObjectMeta .Namespace }, team )
97
85
assert .Equal (t , 1 , len (team .Status .Projects ))
98
86
})
99
87
100
88
t .Run ("must remove a team from Atlas is a team is unassigned" , func (t * testing.T ) {
101
89
logger := zaptest .NewLogger (t ).Sugar ()
102
- workflowCtx := & workflow.Context {
103
- Context : context .Background (),
104
- Log : logger ,
105
- }
106
- testScheme := runtime .NewScheme ()
107
- akov2 .AddToScheme (testScheme )
108
- corev1 .AddToScheme (testScheme )
90
+ workflowCtx := defaultTestWorkflow (logger )
109
91
secret := & corev1.Secret {
110
92
ObjectMeta : metav1.ObjectMeta {
111
93
Name : "my-secret" ,
@@ -151,23 +133,124 @@ func TestUpdateTeamState(t *testing.T) {
151
133
}, "0987654321" , nil
152
134
},
153
135
}
154
- k8sClient := fake .NewClientBuilder ().
155
- WithScheme (testScheme ).
156
- WithObjects (secret , project , team ).
157
- Build ()
136
+ k8sClient := buildFakeKubernetesClient (secret , project , team )
158
137
reconciler := & AtlasProjectReconciler {
159
138
Client : k8sClient ,
160
139
Log : logger ,
161
140
AtlasProvider : atlasProvider ,
162
141
}
163
- teamRef := & common.ResourceRefNamespaced {
164
- Name : team .Name ,
165
- Namespace : "testNS" ,
166
- }
167
142
168
- err := reconciler .updateTeamState (workflowCtx , project , teamRef , true )
143
+ err := reconciler .updateTeamState (workflowCtx , project , reference ( team ) , true )
169
144
assert .NoError (t , err )
170
145
k8sClient .Get (context .Background (), types.NamespacedName {Name : team .ObjectMeta .Name , Namespace : team .ObjectMeta .Namespace }, team )
171
146
assert .Len (t , teamsMock .RemoveTeamFromOrganizationRequests , 1 )
172
147
})
148
+
149
+ t .Run ("must honor deletion protection flag for Teams" , func (t * testing.T ) {
150
+ for _ , tc := range []struct {
151
+ title string
152
+ deletionProtection bool
153
+ keepFlag bool
154
+ expectRemoval bool
155
+ }{
156
+ {
157
+ title : "with deletion protection unassigned teams are not removed" ,
158
+ deletionProtection : true ,
159
+ keepFlag : false ,
160
+ expectRemoval : false ,
161
+ },
162
+ {
163
+ title : "without deletion protection unassigned teams are removed" ,
164
+ deletionProtection : false ,
165
+ keepFlag : false ,
166
+ expectRemoval : true ,
167
+ },
168
+ {
169
+ title : "with deletion protection & keep flag teams are not removed" ,
170
+ deletionProtection : false ,
171
+ keepFlag : true ,
172
+ expectRemoval : false ,
173
+ },
174
+ {
175
+ title : "without deletion protection but keep flag teams are not removed" ,
176
+ deletionProtection : true ,
177
+ keepFlag : true ,
178
+ expectRemoval : false ,
179
+ },
180
+ } {
181
+ t .Run (tc .title , func (t * testing.T ) {
182
+ logger := zaptest .NewLogger (t ).Sugar ()
183
+ workflowCtx := defaultTestWorkflow (logger )
184
+ project := & akov2.AtlasProject {
185
+ Spec : akov2.AtlasProjectSpec {
186
+ Name : "projectName" ,
187
+ },
188
+ }
189
+ team := & akov2.AtlasTeam {
190
+ ObjectMeta : metav1.ObjectMeta {
191
+ Name : "testTeam" ,
192
+ Namespace : "testNS" ,
193
+ },
194
+ }
195
+ teamsMock := & atlas.TeamsClientMock {
196
+ RemoveTeamFromOrganizationFunc : func (orgID string , teamID string ) (* mongodbatlas.Response , error ) {
197
+ return nil , nil
198
+ },
199
+ RemoveTeamFromOrganizationRequests : map [string ]struct {}{},
200
+ }
201
+ atlasProvider := & atlas.TestProvider {
202
+ ClientFunc : func (secretRef * client.ObjectKey , log * zap.SugaredLogger ) (* mongodbatlas.Client , string , error ) {
203
+ return & mongodbatlas.Client {
204
+ Teams : teamsMock ,
205
+ }, "0987654321" , nil
206
+ },
207
+ }
208
+ reconciler := & AtlasProjectReconciler {
209
+ Client : buildFakeKubernetesClient (project , team ),
210
+ Log : logger ,
211
+ AtlasProvider : atlasProvider ,
212
+ ObjectDeletionProtection : tc .deletionProtection ,
213
+ }
214
+ if tc .keepFlag {
215
+ customresource .SetAnnotation (project ,
216
+ customresource .ResourcePolicyAnnotation , customresource .ResourcePolicyKeep )
217
+ }
218
+ err := reconciler .updateTeamState (workflowCtx , project , reference (team ), true )
219
+ assert .NoError (t , err )
220
+ expectedRemovals := 0
221
+ if tc .expectRemoval {
222
+ expectedRemovals = 1
223
+ }
224
+ assert .Len (t , teamsMock .RemoveTeamFromOrganizationRequests , expectedRemovals )
225
+ })
226
+ }
227
+ })
228
+ }
229
+
230
+ func defaultTestWorkflow (logger * zap.SugaredLogger ) * workflow.Context {
231
+ return & workflow.Context {
232
+ Context : context .Background (),
233
+ Log : logger ,
234
+ }
235
+ }
236
+
237
+ func defaultTestScheme () * runtime.Scheme {
238
+ scheme := runtime .NewScheme ()
239
+ akov2 .AddToScheme (scheme )
240
+ corev1 .AddToScheme (scheme )
241
+ return scheme
242
+ }
243
+
244
+ func buildFakeKubernetesClient (objects ... client.Object ) client.WithWatch {
245
+ return fake .NewClientBuilder ().
246
+ WithScheme (defaultTestScheme ()).
247
+ WithObjects (objects ... ).
248
+ Build ()
249
+ }
250
+
251
+ func reference (obj client.Object ) * common.ResourceRefNamespaced {
252
+ return & common.ResourceRefNamespaced {
253
+ Name : obj .GetName (),
254
+ Namespace : obj .GetNamespace (),
255
+ }
173
256
}
0 commit comments