Skip to content

Commit 4f00574

Browse files
committed
add replicaset helper class
1 parent a913281 commit 4f00574

File tree

5 files changed

+209
-175
lines changed

5 files changed

+209
-175
lines changed

docker/mongodb-kubernetes-tests/tests/authentication/replica_set_ldap_switch_project.py

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,22 @@
33

44
import pytest
55
from kubetester import (
6-
create_or_update_configmap,
76
create_secret,
87
find_fixture,
98
read_configmap,
109
try_load,
1110
)
12-
from kubetester.certs import create_mongodb_tls_certs, create_x509_user_cert
11+
from kubetester.certs import create_mongodb_tls_certs
1312
from kubetester.kubetester import KubernetesTester
1413
from kubetester.ldap import LDAP_AUTHENTICATION_MECHANISM, LDAPUser, OpenLDAP
1514
from kubetester.mongodb import MongoDB
1615
from kubetester.mongodb_user import MongoDBUser, Role, generic_user
1716
from kubetester.phase import Phase
1817

18+
from .replica_set_switch_project_helper import (
19+
ReplicaSetCreationAndProjectSwitchTestHelper,
20+
)
21+
1922
MDB_RESOURCE_NAME = "replica-set-ldap-switch-project"
2023

2124

@@ -98,18 +101,29 @@ def user_ldap(replica_set: MongoDB, namespace: str, ldap_mongodb_users: List[LDA
98101
return user.create()
99102

100103

104+
@pytest.fixture(scope="module")
105+
def test_helper(replica_set: MongoDB, namespace: str) -> ReplicaSetCreationAndProjectSwitchTestHelper:
106+
return ReplicaSetCreationAndProjectSwitchTestHelper(
107+
replica_set=replica_set,
108+
namespace=namespace,
109+
authentication_mechanism=LDAP_AUTHENTICATION_MECHANISM,
110+
expected_num_deployment_auth_mechanisms=3,
111+
)
112+
113+
101114
@pytest.mark.e2e_replica_set_ldap_switch_project
102115
class TestReplicaSetLDAPProjectSwitch(KubernetesTester):
103116

104-
def test_create_replica_set(self, replica_set: MongoDB, ldap_mongodb_users: List[LDAPUser]):
105-
replica_set.assert_reaches_phase(Phase.Running, timeout=600)
117+
def test_create_replica_set(self, test_helper: ReplicaSetCreationAndProjectSwitchTestHelper):
118+
test_helper.test_create_replica_set()
106119

107-
def test_create_ldap_user(self, replica_set: MongoDB, user_ldap: MongoDBUser):
120+
def test_create_ldap_user(self, user_ldap: MongoDBUser):
108121
user_ldap.assert_reaches_phase(Phase.Updated)
109122

110-
tester = replica_set.get_automation_config_tester()
111-
tester.assert_authentication_mechanism_enabled(LDAP_AUTHENTICATION_MECHANISM, active_auth_mechanism=True)
112-
tester.assert_expected_users(1)
123+
def test_ops_manager_state_correctly_updated_in_initial_replica_set(
124+
self, test_helper: ReplicaSetCreationAndProjectSwitchTestHelper
125+
):
126+
test_helper.test_ops_manager_state_with_expected_authentication(expected_users=1)
113127

114128
def test_new_mdb_users_are_created_and_can_authenticate(
115129
self, replica_set: MongoDB, user_ldap: MongoDBUser, ca_path: str
@@ -123,29 +137,19 @@ def test_new_mdb_users_are_created_and_can_authenticate(
123137
attempts=10,
124138
)
125139

126-
def test_switch_replica_set_project(self, replica_set: MongoDB, namespace: str, user_ldap: MongoDBUser):
140+
def test_switch_replica_set_project(
141+
self, namespace: str, test_helper: ReplicaSetCreationAndProjectSwitchTestHelper
142+
):
127143
original_configmap = read_configmap(namespace=namespace, name="my-project")
128-
new_project_name = namespace + "-" + "second"
129-
new_project_configmap = create_or_update_configmap(
130-
namespace=namespace,
131-
name=new_project_name,
132-
data={
133-
"baseUrl": original_configmap["baseUrl"],
134-
"projectName": new_project_name,
135-
"orgId": original_configmap["orgId"],
136-
},
144+
test_helper.test_switch_replica_set_project(
145+
original_configmap, new_project_configmap_name=namespace + "-" + "second"
137146
)
138147

139-
replica_set["spec"]["opsManager"]["configMapRef"]["name"] = new_project_configmap
140-
replica_set.update()
141-
142-
replica_set.assert_reaches_phase(Phase.Running, timeout=600)
143-
144148
def test_ops_manager_state_correctly_updated_in_moved_cluster(
145-
self, replica_set: MongoDB, user_ldap: MongoDBUser, ca_path: str
149+
self, test_helper: ReplicaSetCreationAndProjectSwitchTestHelper
146150
):
147-
tester = replica_set.get_automation_config_tester()
148-
tester.assert_authentication_mechanism_enabled(LDAP_AUTHENTICATION_MECHANISM, active_auth_mechanism=True)
151+
test_helper.test_ops_manager_state_with_expected_authentication(expected_users=0)
152+
149153
# tester.assert_expected_users(1)
150154

151155
# tester = replica_set.tester()
Lines changed: 49 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import pytest
22
from kubetester import (
3-
create_or_update_configmap,
43
create_or_update_secret,
54
read_configmap,
65
try_load,
@@ -9,9 +8,12 @@
98
from kubetester.kubetester import fixture as load_fixture
109
from kubetester.mongodb import MongoDB
1110
from kubetester.mongodb_user import MongoDBUser
12-
from kubetester.mongotester import ReplicaSetTester
1311
from kubetester.phase import Phase
1412

13+
from .replica_set_switch_project_helper import (
14+
ReplicaSetCreationAndProjectSwitchTestHelper,
15+
)
16+
1517
# Constants
1618
MDB_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
3749
class 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

Comments
 (0)