1
- from typing import List
1
+ from typing import List
2
2
3
3
import kubernetes
4
4
import pytest
5
+ from kubetester import create_or_update_configmap , read_configmap , try_load
5
6
from kubetester .automation_config_tester import AutomationConfigTester
6
7
from kubetester .certs_mongodb_multi import create_multi_cluster_mongodb_tls_certs
7
8
from kubetester .kubetester import fixture as yaml_fixture
17
18
BUNDLE_SECRET_NAME = f"prefix-{ RESOURCE_NAME } -cert"
18
19
19
20
21
+ @pytest .fixture (scope = "module" )
22
+ def new_project_configmap (
23
+ namespace : str ,
24
+ ) -> str :
25
+ cm = read_configmap (namespace = namespace , name = "my-project" )
26
+ project_name = "new-project"
27
+ return create_or_update_configmap (
28
+ namespace = namespace ,
29
+ name = project_name ,
30
+ data = {
31
+ "baseUrl" : cm ["baseUrl" ],
32
+ "projectName" : project_name ,
33
+ "orgId" : cm ["orgId" ],
34
+ },
35
+ )
36
+
37
+
20
38
@pytest .fixture (scope = "module" )
21
39
def mongodb_multi_unmarshalled (
22
40
namespace : str ,
@@ -28,7 +46,7 @@ def mongodb_multi_unmarshalled(
28
46
resource = MongoDBMulti .from_yaml (yaml_fixture ("mongodb-multi.yaml" ), RESOURCE_NAME , namespace )
29
47
resource .set_version (custom_mdb_version )
30
48
# ensure certs are created for the members during scale up
31
- resource ["spec" ]["clusterSpecList" ] = cluster_spec_list (member_cluster_names , [2 , 1 , 2 ])
49
+ resource ["spec" ]["clusterSpecList" ] = cluster_spec_list (member_cluster_names , [3 , 1 , 2 ])
32
50
resource ["spec" ]["security" ] = {
33
51
"certsSecretPrefix" : "prefix" ,
34
52
"tls" : {
@@ -58,9 +76,14 @@ def server_certs(
58
76
59
77
@pytest .fixture (scope = "module" )
60
78
def mongodb_multi (mongodb_multi_unmarshalled : MongoDBMulti , server_certs : str ) -> MongoDBMulti :
79
+ if try_load (mongodb_multi_unmarshalled ):
80
+ return mongodb_multi_unmarshalled
81
+
61
82
# remove the last element, we are only starting with 2 clusters we will scale up the 3rd one later.
62
83
mongodb_multi_unmarshalled ["spec" ]["clusterSpecList" ].pop ()
63
- return mongodb_multi_unmarshalled .update ()
84
+ # remove one member from the first cluster to start with 2 members
85
+ mongodb_multi_unmarshalled ["spec" ]["clusterSpecList" ][0 ]["members" ] = 2
86
+ return mongodb_multi_unmarshalled
64
87
65
88
66
89
@pytest .mark .e2e_multi_cluster_scale_up_cluster
@@ -70,6 +93,7 @@ def test_deploy_operator(multi_cluster_operator: Operator):
70
93
71
94
@pytest .mark .e2e_multi_cluster_scale_up_cluster
72
95
def test_create_mongodb_multi (mongodb_multi : MongoDBMulti ):
96
+ mongodb_multi .update ()
73
97
mongodb_multi .assert_reaches_phase (Phase .Running , timeout = 600 )
74
98
75
99
@@ -97,7 +121,6 @@ def test_ops_manager_has_been_updated_correctly_before_scaling():
97
121
98
122
@pytest .mark .e2e_multi_cluster_scale_up_cluster
99
123
def test_scale_mongodb_multi (mongodb_multi : MongoDBMulti , member_cluster_clients : List [MultiClusterClient ]):
100
- mongodb_multi .load ()
101
124
mongodb_multi ["spec" ]["clusterSpecList" ].append (
102
125
{"members" : 2 , "clusterName" : member_cluster_clients [2 ].cluster_name }
103
126
)
@@ -139,3 +162,42 @@ def test_ops_manager_has_been_updated_correctly_after_scaling():
139
162
def test_replica_set_is_reachable (mongodb_multi : MongoDBMulti , ca_path : str ):
140
163
tester = mongodb_multi .tester ()
141
164
tester .assert_connectivity (opts = [with_tls (use_tls = True , ca_path = ca_path )])
165
+
166
+
167
+ # From here on, the tests are for verifying that we can change the project of the MongoDBMulti resource even with
168
+ # non-sequential member ids in the replicaset.
169
+
170
+
171
+ @pytest .mark .e2e_multi_cluster_scale_up_cluster
172
+ def test_scale_up_first_cluster (
173
+ mongodb_multi : MongoDBMulti ,
174
+ member_cluster_clients : List [MultiClusterClient ],
175
+ ):
176
+ # Scale up the first cluster to 3 members. This will lead to non-sequential member ids in the replicaset.
177
+ # multi-replica-set-0-0 : 0
178
+ # multi-replica-set-0-1 : 1
179
+ # multi-replica-set-0-2 : 5
180
+ # multi-replica-set-1-0 : 2
181
+ # multi-replica-set-2-0 : 3
182
+ # multi-replica-set-2-1 : 4
183
+
184
+ mongodb_multi ["spec" ]["clusterSpecList" ][0 ]["members" ] = 3
185
+ mongodb_multi .update ()
186
+ mongodb_multi .assert_abandons_phase (Phase .Running , timeout = 120 )
187
+ mongodb_multi .assert_reaches_phase (Phase .Running , timeout = 1800 )
188
+
189
+
190
+ @pytest .mark .e2e_multi_cluster_scale_up_cluster
191
+ def test_change_project (mongodb_multi : MongoDBMulti , new_project_configmap : str ):
192
+ oldRsMembers = mongodb_multi .get_automation_config_tester ().get_replica_set_members (mongodb_multi .name )
193
+
194
+ mongodb_multi ["spec" ]["opsManager" ]["configMapRef" ]["name" ] = new_project_configmap
195
+ mongodb_multi .update ()
196
+
197
+ mongodb_multi .assert_abandons_phase (phase = Phase .Running , timeout = 300 )
198
+ mongodb_multi .assert_reaches_phase (phase = Phase .Running , timeout = 600 )
199
+
200
+ newRsMembers = mongodb_multi .get_automation_config_tester ().get_replica_set_members (mongodb_multi .name )
201
+
202
+ # Assert that the replica set member ids have not changed after changing the project.
203
+ assert oldRsMembers == newRsMembers
0 commit comments