|
| 1 | +import pytest |
| 2 | + |
| 3 | +from kubetester.kubetester import KubernetesTester |
| 4 | +from kubetester.kubetester import fixture as load_fixture |
| 5 | +from kubetester.mongodb import MongoDB |
| 6 | +from kubetester.mongotester import ReplicaSetTester |
| 7 | +from kubetester.phase import Phase |
| 8 | + |
| 9 | +from kubetester import ( |
| 10 | + random_k8s_name, |
| 11 | + create_or_update_configmap, |
| 12 | + read_configmap |
| 13 | +) |
| 14 | + |
| 15 | +# Constants |
| 16 | +MDB_RESOURCE_NAME = "replica-set-scram-sha-256-switch-project" |
| 17 | +MDB_FIXTURE_NAME = MDB_RESOURCE_NAME |
| 18 | + |
| 19 | +CONFIG_MAP_KEYS = { |
| 20 | + "BASE_URL": "baseUrl", |
| 21 | + "PROJECT_NAME": "projectName", |
| 22 | + "ORG_ID": "orgId", |
| 23 | +} |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | +@pytest.fixture(scope="module") |
| 28 | +def replica_set(namespace: str) -> MongoDB: |
| 29 | + """ |
| 30 | + Fixture to initialize the MongoDB resource for the replica set. |
| 31 | + |
| 32 | + Dynamically updates the resource configuration based on the test context. |
| 33 | + """ |
| 34 | + resource = MongoDB.from_yaml(load_fixture(f"{MDB_FIXTURE_NAME}.yaml"), namespace=namespace) |
| 35 | + return resource |
| 36 | + |
| 37 | + |
| 38 | +@pytest.fixture(scope="module") |
| 39 | +def project_name_prefix(namespace: str) -> str: |
| 40 | + """ |
| 41 | + Generates a random Kubernetes project name prefix based on the namespace. |
| 42 | + |
| 43 | + Ensures test isolation in a multi-namespace test environment. |
| 44 | + """ |
| 45 | + return random_k8s_name(f"{namespace}-project-") |
| 46 | + |
| 47 | + |
| 48 | +@pytest.mark.e2e_replica_set_scram_sha_256_switch_project |
| 49 | +class TestReplicaSetCreationAndProjectSwitch(KubernetesTester): |
| 50 | + """ |
| 51 | + E2E test suite for replica set creation and user connectivity with SCRAM-SHA-256 authentication. |
| 52 | + """ |
| 53 | + |
| 54 | + def test_create_replica_set(self, custom_mdb_version: str, replica_set: MongoDB): |
| 55 | + """ |
| 56 | + Test replica set creation ensuring resources are applied correctly and set reaches Running phase. |
| 57 | + """ |
| 58 | + replica_set.set_version(custom_mdb_version) |
| 59 | + replica_set.update() |
| 60 | + replica_set.assert_reaches_phase(Phase.Running, timeout=600) |
| 61 | + |
| 62 | + def test_replica_set_connectivity(self): |
| 63 | + """ |
| 64 | + Verify connectivity to the original replica set. |
| 65 | + """ |
| 66 | + ReplicaSetTester(MDB_RESOURCE_NAME, 3).assert_connectivity() # Adjust node count as appropriate |
| 67 | + |
| 68 | + def test_ops_manager_state_correctly_updated_in_initial_replica_set(self, replica_set: MongoDB): |
| 69 | + """ |
| 70 | + Ensure Ops Manager state is correctly updated in the original replica set. |
| 71 | + """ |
| 72 | + tester = replica_set.get_automation_config_tester() |
| 73 | + tester.assert_authentication_mechanism_enabled("SCRAM-SHA-256") |
| 74 | + tester.assert_authentication_enabled() |
| 75 | + |
| 76 | + def test_switch_replica_set_project( |
| 77 | + self, custom_mdb_version: str, replica_set: MongoDB, namespace: str, project_name_prefix: str |
| 78 | + ): |
| 79 | + """ |
| 80 | + Modify the replica set to switch its Ops Manager reference to a new project and verify lifecycle. |
| 81 | + """ |
| 82 | + original_configmap = read_configmap(namespace=namespace, name="my-project") |
| 83 | + new_project_name = f"{project_name_prefix}-second" |
| 84 | + new_project_configmap = create_or_update_configmap( |
| 85 | + namespace=namespace, |
| 86 | + name=new_project_name, |
| 87 | + data={ |
| 88 | + CONFIG_MAP_KEYS["BASE_URL"]: original_configmap[CONFIG_MAP_KEYS["BASE_URL"]], |
| 89 | + CONFIG_MAP_KEYS["PROJECT_NAME"]: new_project_name, |
| 90 | + CONFIG_MAP_KEYS["ORG_ID"]: original_configmap[CONFIG_MAP_KEYS["ORG_ID"]], |
| 91 | + }, |
| 92 | + ) |
| 93 | + |
| 94 | + replica_set.load() |
| 95 | + replica_set["spec"]["opsManager"]["configMapRef"]["name"] = new_project_configmap |
| 96 | + replica_set.set_version(custom_mdb_version) |
| 97 | + replica_set.update() |
| 98 | + |
| 99 | + replica_set.assert_reaches_phase(Phase.Running, timeout=600) |
| 100 | + |
| 101 | + def test_moved_replica_set_connectivity(self): |
| 102 | + """ |
| 103 | + Verify connectivity to the replica set after switching projects. |
| 104 | + """ |
| 105 | + ReplicaSetTester(MDB_RESOURCE_NAME, 3).assert_connectivity() # Adjust node count as appropriate |
| 106 | + |
| 107 | + def test_ops_manager_state_correctly_updated_in_moved_replica_set(self, replica_set: MongoDB): |
| 108 | + """ |
| 109 | + Ensure Ops Manager state is correctly updated in the moved replica set after the project switch. |
| 110 | + """ |
| 111 | + tester = replica_set.get_automation_config_tester() |
| 112 | + tester.assert_authentication_mechanism_enabled("SCRAM-SHA-256") |
| 113 | + tester.assert_authentication_enabled() |
0 commit comments