Skip to content

Commit c0a80c1

Browse files
committed
tests + lint
1 parent 219e880 commit c0a80c1

15 files changed

+821
-240
lines changed

.evergreen-tasks.yml

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -585,16 +585,6 @@ tasks:
585585
commands:
586586
- func: "e2e_test"
587587

588-
- name: e2e_sharded_cluster_scram_sha_256_switch_project
589-
tags: [ "patch-run" ]
590-
commands:
591-
- func: "e2e_test"
592-
593-
- name: e2e_replica_set_scram_sha_256_switch_project
594-
tags: [ "patch-run" ]
595-
commands:
596-
- func: "e2e_test"
597-
598588
- name: e2e_sharded_cluster_scram_sha_1_user_connectivity
599589
tags: [ "patch-run" ]
600590
commands:
@@ -700,6 +690,36 @@ tasks:
700690
commands:
701691
- func: "e2e_test"
702692

693+
- name: e2e_sharded_cluster_scram_sha_256_switch_project
694+
tags: [ "patch-run" ]
695+
commands:
696+
- func: "e2e_test"
697+
698+
- name: e2e_sharded_cluster_scram_sha_1_switch_project
699+
tags: [ "patch-run" ]
700+
commands:
701+
- func: "e2e_test"
702+
703+
- name: e2e_sharded_cluster_x509_switch_project
704+
tags: [ "patch-run" ]
705+
commands:
706+
- func: "e2e_test"
707+
708+
- name: e2e_replica_set_scram_sha_256_switch_project
709+
tags: [ "patch-run" ]
710+
commands:
711+
- func: "e2e_test"
712+
713+
- name: e2e_replica_set_scram_sha_1_switch_project
714+
tags: [ "patch-run" ]
715+
commands:
716+
- func: "e2e_test"
717+
718+
- name: e2e_replica_set_x509_switch_project
719+
tags: [ "patch-run" ]
720+
commands:
721+
- func: "e2e_test"
722+
703723
# TODO: not used in any variant
704724
- name: e2e_replica_set_scram_x509_internal_cluster
705725
tags: [ "patch-run" ]

.evergreen.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -710,11 +710,15 @@ task_groups:
710710
- e2e_replica_set_scram_x509_ic_manual_certs
711711
- e2e_sharded_cluster_scram_sha_1_upgrade
712712
- e2e_sharded_cluster_scram_sha_256_user_connectivity
713-
- e2e_sharded_cluster_scram_sha_256_switch_project
714-
- e2e_replica_set_scram_sha_256_switch_project
715713
- e2e_sharded_cluster_scram_sha_1_user_connectivity
716714
- e2e_sharded_cluster_scram_x509_ic_manual_certs
717715
- e2e_sharded_cluster_external_access
716+
- e2e_sharded_cluster_scram_sha_256_switch_project
717+
- e2e_sharded_cluster_scram_sha_1_switch_project
718+
- e2e_sharded_cluster_x509_switch_project
719+
- e2e_replica_set_scram_sha_256_switch_project
720+
- e2e_replica_set_scram_sha_1_switch_project
721+
- e2e_replica_set_x509_switch_project
718722
# e2e_auth_transitions_task_group
719723
- e2e_replica_set_scram_sha_and_x509
720724
- e2e_replica_set_x509_to_scram_transition

controllers/om/automation_config.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ import (
2020
)
2121

2222
// The constants for the authentication secret
23-
const agentAuthenticationSecretSuffix = "-agent-auth-secret"
24-
const autoPwdSecretKey = "automation-agent-password"
23+
const (
24+
autoPwdSecretKey = "automation-agent-password"
25+
)
2526

2627
// AutomationConfig maintains the raw map in the Deployment field
2728
// and constructs structs to make use of go's type safety
@@ -435,15 +436,21 @@ func (ac *AutomationConfig) EnsureKeyFileContents() error {
435436
return nil
436437
}
437438

439+
// AuthSecretName for a given mdbName (`mdbName`) returns the name of
440+
// the secret associated with it.
441+
func AuthSecretName(mdbName string) string {
442+
return fmt.Sprintf("%s-agent-auth-secre", mdbName)
443+
}
444+
438445
// EnsurePassword makes sure that there is an Automation Agent password
439446
// that the agents will use to communicate with the deployments. The password
440447
// is returned, so it can be provided to the other agents
441448
// EnsurePassword makes sure that there is an Automation Agent password
442449
// that the agents will use to communicate with the deployments. The password
443450
// is returned, so it can be provided to the other agents.
444451
func (ac *AutomationConfig) EnsurePassword(k8sClient secret.GetUpdateCreator, ctx context.Context, mdbNamespacedName *types.NamespacedName) (string, error) {
445-
secretNamespacedName := client.ObjectKey{Name: mdbNamespacedName.Name + agentAuthenticationSecretSuffix, Namespace: mdbNamespacedName.Namespace}
446-
452+
secretName := AuthSecretName(mdbNamespacedName.Name)
453+
secretNamespacedName := client.ObjectKey{Name: secretName, Namespace: mdbNamespacedName.Namespace}
447454
var password string
448455

449456
data, err := secret.ReadStringData(ctx, k8sClient, secretNamespacedName)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
apiVersion: mongodb.com/v1
3+
kind: MongoDB
4+
metadata:
5+
name: replica-set-scram-sha-1-switch-project
6+
spec:
7+
members: 3
8+
version: 5.0.5
9+
type: ReplicaSet
10+
opsManager:
11+
configMapRef:
12+
name: my-project
13+
credentials: my-credentials
14+
logLevel: DEBUG
15+
persistent: false
16+
security:
17+
authentication:
18+
agents:
19+
# This may look weird, but without it we'll get this from OpsManager:
20+
# Cannot configure SCRAM-SHA-1 without using MONGODB-CR in te Agent Mode","reason":"Cannot configure SCRAM-SHA-1 without using MONGODB-CR in te Agent Mode
21+
mode: MONGODB-CR
22+
enabled: true
23+
modes: ["SCRAM-SHA-1", "MONGODB-CR"]

docker/mongodb-kubernetes-tests/tests/authentication/fixtures/replica-set-scram-sha-256-switch-project.yaml renamed to docker/mongodb-kubernetes-tests/tests/authentication/fixtures/switch-project/replica-set-scram-sha-256-switch-project.yaml

File renamed without changes.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
apiVersion: mongodb.com/v1
3+
kind: MongoDB
4+
metadata:
5+
name: replica-set-x509-switch-project
6+
spec:
7+
members: 3
8+
version: 4.4.0-ent
9+
type: ReplicaSet
10+
opsManager:
11+
configMapRef:
12+
name: my-project
13+
credentials: my-credentials
14+
logLevel: DEBUG
15+
persistent: false
16+
security:
17+
tls:
18+
enabled: true
19+
authentication:
20+
agents:
21+
mode: X509
22+
enabled: true
23+
modes: ["X509"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
apiVersion: mongodb.com/v1
3+
kind: MongoDB
4+
metadata:
5+
name: sharded-cluster-scram-sha-1-switch-project
6+
spec:
7+
shardCount: 1
8+
type: ShardedCluster
9+
mongodsPerShardCount: 1
10+
mongosCount: 1
11+
configServerCount: 1
12+
version: 5.0.5
13+
opsManager:
14+
configMapRef:
15+
name: my-project
16+
credentials: my-credentials
17+
logLevel: DEBUG
18+
persistent: true
19+
security:
20+
authentication:
21+
agents:
22+
# This may look weird, but without it we'll get this from OpsManager:
23+
# Cannot configure SCRAM-SHA-1 without using MONGODB-CR in te Agent Mode","reason":"Cannot configure SCRAM-SHA-1 without using MONGODB-CR in te Agent Mode
24+
mode: MONGODB-CR
25+
enabled: true
26+
modes: ["SCRAM-SHA-1", "MONGODB-CR"]

docker/mongodb-kubernetes-tests/tests/authentication/fixtures/sharded-cluster-scram-sha-256-switch-project.yaml renamed to docker/mongodb-kubernetes-tests/tests/authentication/fixtures/switch-project/sharded-cluster-scram-sha-256-switch-project.yaml

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

0 commit comments

Comments
 (0)