1010from kubetester .mongodb_multi import MongoDBMulti
1111from kubetester .mongodb_role import ClusterMongoDBRole , ClusterMongoDBRoleKind
1212from pytest import fixture , mark
13+ from tests .authentication .shared import custom_roles as testhelper
1314from tests .multicluster .conftest import cluster_spec_list
1415
1516
16- # fmt: off
17- def get_expected_role (role_name : str ) -> dict :
18- return {
19- "role" : role_name ,
20- "db" : "admin" ,
21- "roles" : [
22- {
23- "db" : "admin" ,
24- "role" : "read"
25- }
26- ],
27- "privileges" : [
28- {
29- "resource" : {
30- "db" : "config" ,
31- "collection" : ""
32- },
33- "actions" : [
34- "find" ,
35- "update" ,
36- "insert" ,
37- "remove"
38- ]
39- },
40- {
41- "resource" : {
42- "db" : "users" ,
43- "collection" : "usersCollection"
44- },
45- "actions" : [
46- "update" ,
47- "insert" ,
48- "remove"
49- ]
50- },
51- {
52- "resource" : {
53- "db" : "" ,
54- "collection" : ""
55- },
56- "actions" : [
57- "find"
58- ]
59- },
60- {
61- "resource" : {
62- "cluster" : True
63- },
64- "actions" : [
65- "bypassWriteBlockingMode"
66- ]
67- }
68- ],
69- "authenticationRestrictions" : [
70- {
71- "clientSource" : ["127.0.0.0/8" ],
72- "serverAddress" : ["10.0.0.0/8" ]
73- }
74- ],
75- }
76- # fmt: on
77-
78-
7917@fixture (scope = "function" )
8018def first_project (namespace : str ) -> str :
8119 cm = read_configmap (namespace = namespace , name = "my-project" )
@@ -242,16 +180,13 @@ def test_create_resources(
242180 sharded_cluster : MongoDB ,
243181 mc_replica_set : MongoDBMulti ,
244182):
245- mongodb_role_with_empty_strings .update ()
246- mongodb_role_without_empty_strings .update ()
247-
248- replica_set .update ()
249- sharded_cluster .update ()
250- mc_replica_set .update ()
251-
252- replica_set .assert_reaches_phase (Phase .Running , timeout = 400 )
253- sharded_cluster .assert_reaches_phase (Phase .Running , timeout = 400 )
254- mc_replica_set .assert_reaches_phase (Phase .Running , timeout = 400 )
183+ testhelper .test_create_resources (
184+ mongodb_role_with_empty_strings ,
185+ mongodb_role_without_empty_strings ,
186+ replica_set ,
187+ sharded_cluster ,
188+ mc_replica_set ,
189+ )
255190
256191
257192@mark .e2e_mongodbmulticluster_custom_roles
@@ -262,10 +197,10 @@ def test_automation_config_has_roles(
262197 mongodb_role_with_empty_strings : ClusterMongoDBRole ,
263198 mongodb_role_without_empty_strings : ClusterMongoDBRole ,
264199):
265- assert_expected_roles (
266- mc_replica_set ,
200+ testhelper .test_automation_config_has_roles (
267201 replica_set ,
268202 sharded_cluster ,
203+ mc_replica_set ,
269204 mongodb_role_with_empty_strings ,
270205 mongodb_role_without_empty_strings ,
271206 )
@@ -278,31 +213,12 @@ def assert_expected_roles(
278213 mongodb_role_with_empty_strings : ClusterMongoDBRole ,
279214 mongodb_role_without_empty_strings : ClusterMongoDBRole ,
280215):
281- rs_tester = replica_set .get_automation_config_tester ()
282- sc_tester = sharded_cluster .get_automation_config_tester ()
283- mcrs_tester = mc_replica_set .get_automation_config_tester ()
284- mcrs_tester .assert_has_expected_number_of_roles (expected_roles = 2 )
285- rs_tester .assert_has_expected_number_of_roles (expected_roles = 2 )
286- sc_tester .assert_has_expected_number_of_roles (expected_roles = 2 )
287-
288- rs_tester .assert_expected_role (
289- role_index = 0 , expected_value = get_expected_role (mongodb_role_with_empty_strings ["spec" ]["role" ])
290- )
291- # the second role created without specifying fields with "" should result in identical role to the one with explicitly specified db: "", collection: "".
292- rs_tester .assert_expected_role (
293- role_index = 1 , expected_value = get_expected_role (mongodb_role_without_empty_strings ["spec" ]["role" ])
294- )
295- sc_tester .assert_expected_role (
296- role_index = 0 , expected_value = get_expected_role (mongodb_role_with_empty_strings ["spec" ]["role" ])
297- )
298- sc_tester .assert_expected_role (
299- role_index = 1 , expected_value = get_expected_role (mongodb_role_without_empty_strings ["spec" ]["role" ])
300- )
301- mcrs_tester .assert_expected_role (
302- role_index = 0 , expected_value = get_expected_role (mongodb_role_with_empty_strings ["spec" ]["role" ])
303- )
304- mcrs_tester .assert_expected_role (
305- role_index = 1 , expected_value = get_expected_role (mongodb_role_without_empty_strings ["spec" ]["role" ])
216+ testhelper .assert_expected_roles (
217+ mc_replica_set ,
218+ replica_set ,
219+ sharded_cluster ,
220+ mongodb_role_with_empty_strings ,
221+ mongodb_role_without_empty_strings ,
306222 )
307223
308224
@@ -314,18 +230,13 @@ def test_change_inherited_role(
314230 mongodb_role_with_empty_strings : ClusterMongoDBRole ,
315231 mongodb_role_without_empty_strings : ClusterMongoDBRole ,
316232):
317- mongodb_role_with_empty_strings ["spec" ]["roles" ][0 ]["role" ] = "readWrite"
318- mongodb_role_with_empty_strings .update ()
319-
320- def is_role_changed (ac_tester : AutomationConfigTester ):
321- return (
322- ac_tester .get_role_at_index (0 )["roles" ][0 ]["role" ] == "readWrite"
323- and ac_tester .get_role_at_index (1 )["roles" ][0 ]["role" ] == "read"
324- )
325-
326- wait_until (lambda : is_role_changed (replica_set .get_automation_config_tester ()))
327- wait_until (lambda : is_role_changed (sharded_cluster .get_automation_config_tester ()))
328- wait_until (lambda : is_role_changed (mc_replica_set .get_automation_config_tester ()))
233+ testhelper .test_change_inherited_role (
234+ replica_set ,
235+ sharded_cluster ,
236+ mc_replica_set ,
237+ mongodb_role_with_empty_strings ,
238+ mongodb_role_without_empty_strings ,
239+ )
329240
330241
331242@mark .e2e_mongodbmulticluster_custom_roles
@@ -335,55 +246,26 @@ def test_deleting_role_does_not_remove_access(
335246 mc_replica_set : MongoDBMulti ,
336247 mongodb_role_with_empty_strings : ClusterMongoDBRole ,
337248):
338- mongodb_role_with_empty_strings .delete ()
339-
340- assert try_load (mongodb_role_with_empty_strings ) == False
341-
342- replica_set .assert_reaches_phase (
343- phase = Phase .Failed , msg_regexp = f"ClusterMongoDBRole '{ mongodb_role_with_empty_strings .get_name ()} ' not found"
344- )
345- sharded_cluster .assert_reaches_phase (
346- phase = Phase .Failed , msg_regexp = f"ClusterMongoDBRole '{ mongodb_role_with_empty_strings .get_name ()} ' not found"
249+ testhelper .test_deleting_role_does_not_remove_access (
250+ replica_set , sharded_cluster , mc_replica_set , mongodb_role_with_empty_strings
347251 )
348- mc_replica_set .assert_reaches_phase (
349- phase = Phase .Failed , msg_regexp = f"ClusterMongoDBRole '{ mongodb_role_with_empty_strings .get_name ()} ' not found"
350- )
351-
352- # The role should still exist in the automation config
353- replica_set .get_automation_config_tester ().assert_has_expected_number_of_roles (expected_roles = 2 )
354- sharded_cluster .get_automation_config_tester ().assert_has_expected_number_of_roles (expected_roles = 2 )
355- mc_replica_set .get_automation_config_tester ().assert_has_expected_number_of_roles (expected_roles = 2 )
356252
357253
358254@mark .e2e_mongodbmulticluster_custom_roles
359255def test_removing_role_from_resources (replica_set : MongoDB , sharded_cluster : MongoDB , mc_replica_set : MongoDBMulti ):
360- sharded_cluster ["spec" ]["security" ]["roleRefs" ] = None
361- sharded_cluster .update ()
362-
363- mc_replica_set ["spec" ]["security" ]["roleRefs" ] = None
364- mc_replica_set .update ()
365-
366- wait_until (lambda : len (sharded_cluster .get_automation_config_tester ().automation_config ["roles" ]) == 0 , timeout = 120 )
367- wait_until (lambda : len (mc_replica_set .get_automation_config_tester ().automation_config ["roles" ]) == 0 , timeout = 120 )
256+ testhelper .test_removing_role_from_resources (replica_set , sharded_cluster , mc_replica_set )
368257
369258
370259@mark .e2e_mongodbmulticluster_custom_roles
371260def test_install_operator_with_clustermongodbroles_disabled (multi_cluster_operator_no_cluster_mongodb_roles ):
372- multi_cluster_operator_no_cluster_mongodb_roles . assert_is_running ( )
261+ testhelper . test_install_operator_with_clustermongodbroles_disabled ( multi_cluster_operator_no_cluster_mongodb_roles )
373262
374263
375264@mark .e2e_mongodbmulticluster_custom_roles
376265def test_replicaset_is_failed (replica_set : MongoDB ):
377- replica_set .assert_reaches_phase (
378- Phase .Failed ,
379- msg_regexp = "RoleRefs are not supported when ClusterMongoDBRoles are disabled. Please enable ClusterMongoDBRoles in the operator configuration." ,
380- )
266+ testhelper .test_replicaset_is_failed (replica_set )
381267
382268
383269@mark .e2e_mongodbmulticluster_custom_roles
384270def test_replicaset_is_reconciled_without_rolerefs (replica_set : MongoDB ):
385- replica_set ["spec" ]["security" ]["roleRefs" ] = None
386- replica_set .update ()
387-
388- replica_set .assert_reaches_phase (Phase .Running )
389- replica_set .get_automation_config_tester ().assert_has_expected_number_of_roles (expected_roles = 0 )
271+ testhelper .test_replicaset_is_reconciled_without_rolerefs (replica_set )
0 commit comments