Skip to content

Commit 7032528

Browse files
committed
refactoring helper classes
1 parent 4f00574 commit 7032528

10 files changed

+240
-255
lines changed

docker/mongodb-kubernetes-tests/tests/authentication/replica_set_switch_project_helper.py renamed to docker/mongodb-kubernetes-tests/tests/authentication/helper_replica_set_switch_project.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from kubetester import (
22
create_or_update_configmap,
3+
read_configmap,
34
)
45
from kubetester.mongodb import MongoDB
56
from kubetester.mongotester import ReplicaSetTester
@@ -32,15 +33,16 @@ def test_ops_manager_state_with_expected_authentication(self, expected_users: in
3233
tester.assert_authentication_mechanism_enabled(self.authentication_mechanism, self.active_auth_mechanism)
3334
tester.assert_authentication_enabled(self.expected_num_deployment_auth_mechanisms)
3435
tester.assert_expected_users(expected_users)
35-
36+
3637
if self.authentication_mechanism == "MONGODB-X509":
3738
tester.assert_authoritative_set(True)
3839

39-
def test_switch_replica_set_project(self, original_configmap: dict, new_project_configmap_name: str):
40-
new_project_name = self.namespace + "-second"
40+
def test_switch_replica_set_project(self):
41+
original_configmap = read_configmap(namespace=self.namespace, name="my-project")
42+
new_project_name = f"{self.namespace}-second"
4143
new_project_configmap = create_or_update_configmap(
4244
namespace=self.namespace,
43-
name=new_project_configmap_name,
45+
name=new_project_name,
4446
data={
4547
"baseUrl": original_configmap["baseUrl"],
4648
"projectName": new_project_name,
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
from kubetester import (
2+
create_or_update_configmap,
3+
read_configmap,
4+
)
5+
from kubetester.mongodb import MongoDB
6+
from kubetester.mongotester import ShardedClusterTester
7+
from kubetester.phase import Phase
8+
9+
10+
class ShardedClusterCreationAndProjectSwitchTestHelper:
11+
def __init__(
12+
self,
13+
sharded_cluster: MongoDB,
14+
namespace: str,
15+
authentication_mechanism: str,
16+
expected_num_deployment_auth_mechanisms=2,
17+
active_auth_mechanism=True,
18+
):
19+
self.sharded_cluster = sharded_cluster
20+
self.namespace = namespace
21+
self.authentication_mechanism = authentication_mechanism
22+
self.expected_num_deployment_auth_mechanisms = expected_num_deployment_auth_mechanisms
23+
self.active_auth_mechanism = active_auth_mechanism
24+
25+
def test_create_sharded_cluster(self):
26+
self.sharded_cluster.assert_reaches_phase(Phase.Running, timeout=800)
27+
28+
def test_sharded_cluster_connectivity(self, shard_count):
29+
ShardedClusterTester(self.sharded_cluster.name, shard_count).assert_connectivity()
30+
31+
def test_ops_manager_state_with_expected_authentication(self, expected_users: int):
32+
tester = self.sharded_cluster.get_automation_config_tester()
33+
tester.assert_authentication_mechanism_enabled(self.authentication_mechanism, self.active_auth_mechanism)
34+
tester.assert_authentication_enabled(self.expected_num_deployment_auth_mechanisms)
35+
tester.assert_expected_users(expected_users)
36+
37+
if self.authentication_mechanism == "MONGODB-X509":
38+
tester.assert_authoritative_set(True)
39+
40+
def test_switch_sharded_cluster_project(self):
41+
original_configmap = read_configmap(namespace=self.namespace, name="my-project")
42+
new_project_name = f"{self.namespace}-second"
43+
44+
new_project_configmap = create_or_update_configmap(
45+
namespace=self.namespace,
46+
name=new_project_name,
47+
data={
48+
"baseUrl": original_configmap["baseUrl"],
49+
"projectName": new_project_name,
50+
"orgId": original_configmap["orgId"],
51+
},
52+
)
53+
54+
self.sharded_cluster["spec"]["opsManager"]["configMapRef"]["name"] = new_project_configmap
55+
self.sharded_cluster.update()
56+
self.sharded_cluster.assert_reaches_phase(Phase.Running, timeout=800)
57+
58+
def test_ops_manager_state_with_users(self, user_name: str, expected_roles: set, expected_users: int):
59+
tester = self.sharded_cluster.get_automation_config_tester()
60+
tester.assert_has_user(user_name)
61+
tester.assert_user_has_roles(user_name, expected_roles)
62+
tester.assert_expected_users(expected_users)

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

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import tempfile
21
from typing import List
32

43
import pytest
54
from kubetester import (
65
create_secret,
76
find_fixture,
8-
read_configmap,
97
try_load,
108
)
119
from kubetester.certs import create_mongodb_tls_certs
@@ -15,7 +13,7 @@
1513
from kubetester.mongodb_user import MongoDBUser, Role, generic_user
1614
from kubetester.phase import Phase
1715

18-
from .replica_set_switch_project_helper import (
16+
from .helper_replica_set_switch_project import (
1917
ReplicaSetCreationAndProjectSwitchTestHelper,
2018
)
2119

@@ -137,25 +135,11 @@ def test_new_mdb_users_are_created_and_can_authenticate(
137135
attempts=10,
138136
)
139137

140-
def test_switch_replica_set_project(
141-
self, namespace: str, test_helper: ReplicaSetCreationAndProjectSwitchTestHelper
142-
):
143-
original_configmap = read_configmap(namespace=namespace, name="my-project")
144-
test_helper.test_switch_replica_set_project(
145-
original_configmap, new_project_configmap_name=namespace + "-" + "second"
146-
)
138+
def test_switch_replica_set_project(self, test_helper: ReplicaSetCreationAndProjectSwitchTestHelper):
139+
test_helper.test_switch_replica_set_project()
147140

148-
def test_ops_manager_state_correctly_updated_in_moved_cluster(
141+
def test_ops_manager_state_with_users_correctly_updated_after_switch(
149142
self, test_helper: ReplicaSetCreationAndProjectSwitchTestHelper
150143
):
151144
test_helper.test_ops_manager_state_with_expected_authentication(expected_users=0)
152-
153-
# tester.assert_expected_users(1)
154-
155-
# tester = replica_set.tester()
156-
# tester.assert_ldap_authentication(
157-
# username=user_ldap["spec"]["username"],
158-
# password=user_ldap.password,
159-
# tls_ca_file=ca_path,
160-
# attempts=10,
161-
# )
145+
# There should be one user (the previously created user should still exist in the automation configuration). We need to investigate further to understand why the user is not being picked up.

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

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import pytest
22
from kubetester import (
33
create_or_update_secret,
4-
read_configmap,
54
try_load,
65
)
76
from kubetester.kubetester import KubernetesTester
@@ -10,21 +9,16 @@
109
from kubetester.mongodb_user import MongoDBUser
1110
from kubetester.phase import Phase
1211

13-
from .replica_set_switch_project_helper import (
12+
from .helper_replica_set_switch_project import (
1413
ReplicaSetCreationAndProjectSwitchTestHelper,
1514
)
1615

17-
# Constants
1816
MDB_RESOURCE_NAME = "replica-set-scram-sha-1-switch-project"
1917

2018

2119
@pytest.fixture(scope="module")
2220
def replica_set(namespace: str) -> MongoDB:
23-
"""
24-
Fixture to initialize the MongoDB resource for the replica set.
2521

26-
Dynamically updates the resource configuration based on the test context.
27-
"""
2822
resource = MongoDB.from_yaml(
2923
load_fixture("replica-set-explicit-scram-sha-1.yaml"), name=MDB_RESOURCE_NAME, namespace=namespace
3024
)
@@ -94,13 +88,8 @@ def test_ops_manager_state_with_users_correctly_updated(
9488
user_name=user_name, expected_roles=expected_roles, expected_users=1
9589
)
9690

97-
def test_switch_replica_set_project(
98-
self, namespace: str, test_helper: ReplicaSetCreationAndProjectSwitchTestHelper
99-
):
100-
original_configmap = read_configmap(namespace=namespace, name="my-project")
101-
test_helper.test_switch_replica_set_project(
102-
original_configmap, new_project_configmap_name=namespace + "-" + "second"
103-
)
91+
def test_switch_replica_set_project(self, test_helper: ReplicaSetCreationAndProjectSwitchTestHelper):
92+
test_helper.test_switch_replica_set_project()
10493

10594
def test_replica_set_connectivity_after_switch(self, test_helper: ReplicaSetCreationAndProjectSwitchTestHelper):
10695
test_helper.test_replica_set_connectivity(3)

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

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import pytest
22
from kubetester import (
33
create_or_update_secret,
4-
read_configmap,
54
try_load,
65
)
76
from kubetester.kubetester import KubernetesTester
@@ -10,21 +9,16 @@
109
from kubetester.mongodb_user import MongoDBUser
1110
from kubetester.phase import Phase
1211

13-
from .replica_set_switch_project_helper import (
12+
from .helper_replica_set_switch_project import (
1413
ReplicaSetCreationAndProjectSwitchTestHelper,
1514
)
1615

17-
# Constants
1816
MDB_RESOURCE_NAME = "replica-set-scram-sha-256-switch-project"
1917

2018

2119
@pytest.fixture(scope="module")
2220
def replica_set(namespace: str) -> MongoDB:
23-
"""
24-
Fixture to initialize the MongoDB resource for the replica set.
2521

26-
Dynamically updates the resource configuration based on the test context.
27-
"""
2822
resource = MongoDB.from_yaml(
2923
load_fixture("replica-set-scram-sha-256.yaml"), name=MDB_RESOURCE_NAME, namespace=namespace
3024
)
@@ -89,13 +83,8 @@ def test_ops_manager_state_with_users_correctly_updated(
8983
user_name=user_name, expected_roles=expected_roles, expected_users=1
9084
)
9185

92-
def test_switch_replica_set_project(
93-
self, test_helper: ReplicaSetCreationAndProjectSwitchTestHelper, namespace: str
94-
):
95-
original_configmap = read_configmap(namespace=namespace, name="my-project")
96-
test_helper.test_switch_replica_set_project(
97-
original_configmap, new_project_configmap_name=namespace + "-" + "second"
98-
)
86+
def test_switch_replica_set_project(self, test_helper: ReplicaSetCreationAndProjectSwitchTestHelper):
87+
test_helper.test_switch_replica_set_project()
9988

10089
def test_replica_set_connectivity_after_switch(self, test_helper: ReplicaSetCreationAndProjectSwitchTestHelper):
10190
test_helper.test_replica_set_connectivity(3)

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

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import pytest
22
from kubetester import (
3-
read_configmap,
43
try_load,
54
)
65
from kubetester.certs import (
@@ -12,21 +11,16 @@
1211
from kubetester.kubetester import fixture as load_fixture
1312
from kubetester.mongodb import MongoDB
1413

15-
from .replica_set_switch_project_helper import (
14+
from .helper_replica_set_switch_project import (
1615
ReplicaSetCreationAndProjectSwitchTestHelper,
1716
)
1817

19-
# Constants
2018
MDB_RESOURCE_NAME = "replica-set-x509-switch-project"
2119

2220

2321
@pytest.fixture(scope="module")
2422
def replica_set(namespace: str, server_certs: str, agent_certs: str, issuer_ca_configmap: str) -> MongoDB:
25-
"""
26-
Fixture to initialize the MongoDB resource for the replica set.
2723

28-
Dynamically updates the resource configuration based on the test context.
29-
"""
3024
resource = MongoDB.from_yaml(
3125
load_fixture("replica-set-x509-to-scram-256.yaml"), name=MDB_RESOURCE_NAME, namespace=namespace
3226
)
@@ -69,15 +63,10 @@ def test_ops_manager_state_correctly_updated_in_initial_replica_set(
6963
):
7064
test_helper.test_ops_manager_state_with_expected_authentication(expected_users=0)
7165

72-
def test_switch_replica_set_project(
73-
self, test_helper: ReplicaSetCreationAndProjectSwitchTestHelper, namespace: str
74-
):
75-
original_configmap = read_configmap(namespace=namespace, name="my-project")
76-
test_helper.test_switch_replica_set_project(
77-
original_configmap, new_project_configmap_name=namespace + "-" + "second"
78-
)
66+
def test_switch_replica_set_project(self, test_helper: ReplicaSetCreationAndProjectSwitchTestHelper):
67+
test_helper.test_switch_replica_set_project()
7968

80-
def test_ops_manager_state_correctly_updated_in_moved_replica_set(
69+
def test_ops_manager_state_with_users_correctly_updated_after_switch(
8170
self, test_helper: ReplicaSetCreationAndProjectSwitchTestHelper
8271
):
8372
test_helper.test_ops_manager_state_with_expected_authentication(expected_users=0)

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

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
1-
import time
21
from typing import Dict, List
32

43
import pytest
54
from kubetester import (
6-
create_or_update_configmap,
7-
create_secret,
85
find_fixture,
9-
read_configmap,
106
try_load,
117
wait_until,
128
)
139
from kubetester.kubetester import KubernetesTester
14-
from kubetester.kubetester import fixture as yaml_fixture
1510
from kubetester.ldap import LDAP_AUTHENTICATION_MECHANISM, LDAPUser, OpenLDAP
1611
from kubetester.mongodb import MongoDB
1712
from kubetester.mongodb_user import MongoDBUser, Role, generic_user
18-
from kubetester.mongotester import ShardedClusterTester
1913
from kubetester.phase import Phase
2014

15+
from .helper_sharded_cluster_switch_project import (
16+
ShardedClusterCreationAndProjectSwitchTestHelper,
17+
)
18+
2119
MDB_RESOURCE_NAME = "sharded-cluster-ldap-switch-project"
2220

2321

@@ -72,11 +70,21 @@ def ldap_user_mongodb(sharded_cluster: MongoDB, namespace: str, ldap_mongodb_use
7270
return user.create()
7371

7472

73+
@pytest.fixture(scope="module")
74+
def test_helper(sharded_cluster: MongoDB, namespace: str) -> ShardedClusterCreationAndProjectSwitchTestHelper:
75+
return ShardedClusterCreationAndProjectSwitchTestHelper(
76+
sharded_cluster=sharded_cluster,
77+
namespace=namespace,
78+
authentication_mechanism=LDAP_AUTHENTICATION_MECHANISM,
79+
expected_num_deployment_auth_mechanisms=2,
80+
active_auth_mechanism=False,
81+
)
82+
83+
7584
@pytest.mark.e2e_sharded_cluster_ldap_switch_project
7685
class TestShardedClusterLDAPProjectSwitch(KubernetesTester):
7786

78-
def test_create_sharded_cluster(self, sharded_cluster: MongoDB):
79-
sharded_cluster.update()
87+
def test_create_sharded_cluster(self, sharded_cluster):
8088
sharded_cluster.assert_reaches_phase(Phase.Pending, timeout=600)
8189

8290
def test_sharded_cluster_turn_tls_on_CLOUDP_229222(self, sharded_cluster: MongoDB):
@@ -114,33 +122,19 @@ def wait_for_ac_pushed() -> bool:
114122
def test_sharded_cluster_CLOUDP_229222(self, sharded_cluster: MongoDB, ldap_mongodb_users: List[LDAPUser]):
115123
sharded_cluster.assert_reaches_phase(Phase.Running, timeout=800)
116124

125+
def test_new_mdb_users_are_created(self, ldap_user_mongodb: MongoDBUser):
126+
ldap_user_mongodb.assert_reaches_phase(Phase.Updated)
127+
117128
def test_ops_manager_state_correctly_updated_in_initial_cluster(
118-
self, sharded_cluster: MongoDB, ldap_user_mongodb: MongoDBUser
129+
self, test_helper: ShardedClusterCreationAndProjectSwitchTestHelper
119130
):
131+
test_helper.test_ops_manager_state_with_expected_authentication(expected_users=1)
120132

121-
ldap_user_mongodb.assert_reaches_phase(Phase.Updated)
122-
ac_tester = sharded_cluster.get_automation_config_tester()
123-
ac_tester.assert_authentication_mechanism_enabled(LDAP_AUTHENTICATION_MECHANISM, active_auth_mechanism=False)
124-
ac_tester.assert_expected_users(1)
125-
126-
def test_switch_sharded_cluster_project(self, sharded_cluster: MongoDB, namespace: str):
127-
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-
},
137-
)
138-
139-
sharded_cluster["spec"]["opsManager"]["configMapRef"]["name"] = new_project_configmap
140-
sharded_cluster.update()
141-
sharded_cluster.assert_reaches_phase(Phase.Running, timeout=600)
133+
def test_switch_sharded_cluster_project(self, test_helper: ShardedClusterCreationAndProjectSwitchTestHelper):
134+
test_helper.test_switch_sharded_cluster_project()
142135

143-
def test_ops_manager_state_correctly_updated_in_moved_cluster(self, sharded_cluster: MongoDB):
144-
ac_tester = sharded_cluster.get_automation_config_tester()
145-
ac_tester.assert_authentication_mechanism_enabled(LDAP_AUTHENTICATION_MECHANISM, active_auth_mechanism=False)
146-
# ac_tester.assert_expected_users(1)
136+
def test_ops_manager_state_correctly_updated_after_switch(
137+
self, test_helper: ShardedClusterCreationAndProjectSwitchTestHelper
138+
):
139+
test_helper.test_ops_manager_state_with_expected_authentication(expected_users=0)
140+
# There should be one user (the previously created user should still exist in the automation configuration). We need to investigate further to understand why the user is not being picked up.

0 commit comments

Comments
 (0)