11import pytest
22from kubetester import (
3- create_or_update_configmap ,
43 create_or_update_secret ,
54 read_configmap ,
65 try_load ,
98from kubetester .kubetester import fixture as load_fixture
109from kubetester .mongodb import MongoDB
1110from kubetester .mongodb_user import MongoDBUser
12- from kubetester .mongotester import ReplicaSetTester
1311from kubetester .phase import Phase
1412
13+ from .replica_set_switch_project_helper import (
14+ ReplicaSetCreationAndProjectSwitchTestHelper ,
15+ )
16+
1517# Constants
1618MDB_RESOURCE_NAME = "replica-set-scram-sha-1-switch-project"
1719
@@ -33,35 +35,39 @@ def replica_set(namespace: str) -> MongoDB:
3335 return resource .update ()
3436
3537
38+ @pytest .fixture (scope = "module" )
39+ def test_helper (replica_set : MongoDB , namespace : str ) -> ReplicaSetCreationAndProjectSwitchTestHelper :
40+ return ReplicaSetCreationAndProjectSwitchTestHelper (
41+ replica_set = replica_set ,
42+ namespace = namespace ,
43+ authentication_mechanism = "MONGODB-CR" ,
44+ expected_num_deployment_auth_mechanisms = 2 ,
45+ )
46+
47+
3648@pytest .mark .e2e_replica_set_scram_sha_1_switch_project
3749class TestReplicaSetCreationAndProjectSwitch (KubernetesTester ):
3850 """
3951 E2E test suite for replica set creation, user connectivity with SCRAM-SHA-1 authentication and switching Ops Manager project reference.
4052 """
4153
42- PASSWORD_SECRET_NAME = "mms-user-1-password"
43- USER_PASSWORD = "my-password"
44- USER_NAME = "mms-user-1"
54+ def test_create_replica_set (self , test_helper : ReplicaSetCreationAndProjectSwitchTestHelper ):
55+ test_helper .test_create_replica_set ()
4556
46- def test_create_replica_set (self , replica_set : MongoDB ):
47- replica_set . assert_reaches_phase ( Phase . Running , timeout = 600 )
57+ def test_replica_set_connectivity (self , test_helper : ReplicaSetCreationAndProjectSwitchTestHelper ):
58+ test_helper . test_replica_set_connectivity ( 3 )
4859
49- def test_replica_set_connectivity (self ):
50- ReplicaSetTester (MDB_RESOURCE_NAME , 3 ).assert_connectivity ()
51-
52- def test_ops_manager_state_correctly_updated_in_initial_replica_set (self , replica_set : MongoDB ):
53- tester = replica_set .get_automation_config_tester ()
54- tester .assert_authentication_mechanism_enabled ("MONGODB-CR" )
55- tester .assert_authoritative_set (True )
56- tester .assert_authentication_enabled (2 )
57- tester .assert_expected_users (0 )
60+ def test_ops_manager_state_correctly_updated_in_initial_replica_set (
61+ self , test_helper : ReplicaSetCreationAndProjectSwitchTestHelper
62+ ):
63+ test_helper .test_ops_manager_state_with_expected_authentication (expected_users = 0 )
5864
5965 def test_create_secret (self ):
6066 create_or_update_secret (
6167 KubernetesTester .get_namespace (),
62- self . PASSWORD_SECRET_NAME ,
68+ "mms-user-1-password" ,
6369 {
64- "password" : self . USER_PASSWORD ,
70+ "password" : "my-password" ,
6571 },
6672 )
6773
@@ -71,59 +77,49 @@ def test_create_user(self, namespace: str):
7177 namespace = namespace ,
7278 )
7379 mdb ["spec" ]["mongodbResourceRef" ]["name" ] = MDB_RESOURCE_NAME
74-
7580 mdb .update ()
7681 mdb .assert_reaches_phase (Phase .Updated , timeout = 150 )
7782
78- def test_ops_manager_state_with_users_correctly_updated (self , replica_set : MongoDB ):
83+ def test_ops_manager_state_with_users_correctly_updated (
84+ self , test_helper : ReplicaSetCreationAndProjectSwitchTestHelper
85+ ):
86+ user_name = "mms-user-1"
7987 expected_roles = {
8088 ("admin" , "clusterAdmin" ),
8189 ("admin" , "userAdminAnyDatabase" ),
8290 ("admin" , "readWrite" ),
8391 ("admin" , "userAdminAnyDatabase" ),
8492 }
93+ test_helper .test_ops_manager_state_with_users (
94+ user_name = user_name , expected_roles = expected_roles , expected_users = 1
95+ )
8596
86- tester = replica_set .get_automation_config_tester ()
87- tester .assert_has_user (self .USER_NAME )
88- tester .assert_user_has_roles (self .USER_NAME , expected_roles )
89- tester .assert_expected_users (1 )
90-
91- def test_switch_replica_set_project (self , replica_set : MongoDB , namespace : str ):
97+ def test_switch_replica_set_project (
98+ self , namespace : str , test_helper : ReplicaSetCreationAndProjectSwitchTestHelper
99+ ):
92100 original_configmap = read_configmap (namespace = namespace , name = "my-project" )
93- new_project_name = namespace + "-" + "second"
94- new_project_configmap = create_or_update_configmap (
95- namespace = namespace ,
96- name = new_project_name ,
97- data = {
98- "baseUrl" : original_configmap ["baseUrl" ],
99- "projectName" : new_project_name ,
100- "orgId" : original_configmap ["orgId" ],
101- },
101+ test_helper .test_switch_replica_set_project (
102+ original_configmap , new_project_configmap_name = namespace + "-" + "second"
102103 )
103104
104- replica_set [ "spec" ][ "opsManager" ][ "configMapRef" ][ "name" ] = new_project_configmap
105- replica_set . update ( )
105+ def test_replica_set_connectivity_after_switch ( self , test_helper : ReplicaSetCreationAndProjectSwitchTestHelper ):
106+ test_helper . test_replica_set_connectivity ( 3 )
106107
107- replica_set .assert_reaches_phase (Phase .Running , timeout = 600 )
108+ def test_ops_manager_state_correctly_updated_after_switch (
109+ self , test_helper : ReplicaSetCreationAndProjectSwitchTestHelper
110+ ):
111+ test_helper .test_ops_manager_state_with_expected_authentication (expected_users = 1 )
108112
109- def test_moved_replica_set_connectivity (self ):
110- ReplicaSetTester (MDB_RESOURCE_NAME , 3 ).assert_connectivity ()
111-
112- def test_ops_manager_state_correctly_updated_in_moved_replica_set (self , replica_set : MongoDB ):
113- tester = replica_set .get_automation_config_tester ()
114- tester .assert_authentication_mechanism_enabled ("MONGODB-CR" )
115- tester .assert_authoritative_set (True )
116- tester .assert_authentication_enabled (2 )
117-
118- def test_ops_manager_state_with_users_correctly_updated_after_switch (self , replica_set : MongoDB ):
113+ def test_ops_manager_state_with_users_correctly_updated_after_switch (
114+ self , test_helper : ReplicaSetCreationAndProjectSwitchTestHelper
115+ ):
116+ user_name = "mms-user-1"
119117 expected_roles = {
120118 ("admin" , "clusterAdmin" ),
121119 ("admin" , "userAdminAnyDatabase" ),
122120 ("admin" , "readWrite" ),
123121 ("admin" , "userAdminAnyDatabase" ),
124122 }
125-
126- tester = replica_set .get_automation_config_tester ()
127- tester .assert_has_user (self .USER_NAME )
128- tester .assert_user_has_roles (self .USER_NAME , expected_roles )
129- tester .assert_expected_users (1 )
123+ test_helper .test_ops_manager_state_with_users (
124+ user_name = user_name , expected_roles = expected_roles , expected_users = 1
125+ )
0 commit comments