@@ -19,19 +19,14 @@ import (
1919 akov2 "github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/api/v1"
2020 "github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/api/v1/common"
2121 "github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/api/v1/status"
22+ "github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/controller/customresource"
2223 "github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/controller/workflow"
2324)
2425
2526func TestUpdateTeamState (t * testing.T ) {
2627 t .Run ("should not duplicate projects listed" , func (t * testing.T ) {
2728 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 )
3530 secret := & corev1.Secret {
3631 ObjectMeta : metav1.ObjectMeta {
3732 Name : "my-secret" ,
@@ -74,38 +69,25 @@ func TestUpdateTeamState(t *testing.T) {
7469 return & mongodbatlas.Client {}, "0987654321" , nil
7570 },
7671 }
77- k8sClient := fake .NewClientBuilder ().
78- WithScheme (testScheme ).
79- WithObjects (secret , project , team ).
80- Build ()
72+ k8sClient := buildFakeKubernetesClient (secret , project , team )
8173 reconciler := & AtlasProjectReconciler {
8274 Client : k8sClient ,
8375 Log : logger ,
8476 AtlasProvider : atlasProvider ,
8577 }
86- teamRef := & common.ResourceRefNamespaced {
87- Name : team .Name ,
88- Namespace : "testNS" ,
89- }
9078 // check we have exactly 1 project in status
9179 assert .Equal (t , 1 , len (team .Status .Projects ))
9280
9381 // "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 )
9583 assert .NoError (t , err )
9684 k8sClient .Get (context .Background (), types.NamespacedName {Name : team .ObjectMeta .Name , Namespace : team .ObjectMeta .Namespace }, team )
9785 assert .Equal (t , 1 , len (team .Status .Projects ))
9886 })
9987
10088 t .Run ("must remove a team from Atlas is a team is unassigned" , func (t * testing.T ) {
10189 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 )
10991 secret := & corev1.Secret {
11092 ObjectMeta : metav1.ObjectMeta {
11193 Name : "my-secret" ,
@@ -151,23 +133,124 @@ func TestUpdateTeamState(t *testing.T) {
151133 }, "0987654321" , nil
152134 },
153135 }
154- k8sClient := fake .NewClientBuilder ().
155- WithScheme (testScheme ).
156- WithObjects (secret , project , team ).
157- Build ()
136+ k8sClient := buildFakeKubernetesClient (secret , project , team )
158137 reconciler := & AtlasProjectReconciler {
159138 Client : k8sClient ,
160139 Log : logger ,
161140 AtlasProvider : atlasProvider ,
162141 }
163- teamRef := & common.ResourceRefNamespaced {
164- Name : team .Name ,
165- Namespace : "testNS" ,
166- }
167142
168- err := reconciler .updateTeamState (workflowCtx , project , teamRef , true )
143+ err := reconciler .updateTeamState (workflowCtx , project , reference ( team ) , true )
169144 assert .NoError (t , err )
170145 k8sClient .Get (context .Background (), types.NamespacedName {Name : team .ObjectMeta .Name , Namespace : team .ObjectMeta .Namespace }, team )
171146 assert .Len (t , teamsMock .RemoveTeamFromOrganizationRequests , 1 )
172147 })
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+ }
173256}
0 commit comments