Skip to content

Commit 396c812

Browse files
committed
Update test to check for more complex behavior
1 parent 83decf6 commit 396c812

File tree

3 files changed

+79
-35
lines changed

3 files changed

+79
-35
lines changed

controllers/mongodb_cleanup_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestReplicaSetReconcilerCleanupScramSecrets(t *testing.T) {
2222
t.Run("no change same resource", func(t *testing.T) {
2323
actual := getScramSecretsToDelete(lastApplied.Spec, lastApplied.Spec)
2424

25-
assert.Equal(t, []string{}, actual)
25+
assert.Equal(t, []string(nil), actual)
2626
})
2727

2828
t.Run("new user new secret", func(t *testing.T) {
@@ -45,7 +45,7 @@ func TestReplicaSetReconcilerCleanupScramSecrets(t *testing.T) {
4545

4646
actual := getScramSecretsToDelete(current.Spec, lastApplied.Spec)
4747

48-
assert.Equal(t, []string{}, actual)
48+
assert.Equal(t, []string(nil), actual)
4949
})
5050

5151
t.Run("old user new secret", func(t *testing.T) {
@@ -165,7 +165,7 @@ func TestReplicaSetReconcilerCleanupConnectionStringSecrets(t *testing.T) {
165165
t.Run("no change same resource", func(t *testing.T) {
166166
actual := getConnectionStringSecretsToDelete(lastApplied.Spec, lastApplied.Spec, "my-rs")
167167

168-
assert.Equal(t, []string{}, actual)
168+
assert.Equal(t, []string(nil), actual)
169169
})
170170

171171
t.Run("new user does not require existing user cleanup", func(t *testing.T) {
@@ -188,7 +188,7 @@ func TestReplicaSetReconcilerCleanupConnectionStringSecrets(t *testing.T) {
188188

189189
actual := getConnectionStringSecretsToDelete(current.Spec, lastApplied.Spec, "my-rs")
190190

191-
assert.Equal(t, []string{}, actual)
191+
assert.Equal(t, []string(nil), actual)
192192
})
193193

194194
t.Run("old user new secret", func(t *testing.T) {

test/e2e/mongodbtests/mongodbtests.go

+26-3
Original file line numberDiff line numberDiff line change
@@ -772,10 +772,22 @@ func assertEqualOwnerReference(t *testing.T, resourceType string, resourceNamesp
772772
assert.Equal(t, expectedOwnerReference.UID, ownerReferences[0].UID)
773773
}
774774

775-
func RemoveAllUsersFromResource(ctx context.Context, mdb *mdbv1.MongoDBCommunity) func(*testing.T) {
775+
func RemoveLastUserFromMongoDBCommunity(ctx context.Context, mdb *mdbv1.MongoDBCommunity) func(*testing.T) {
776776
return func(t *testing.T) {
777777
err := e2eutil.UpdateMongoDBResource(ctx, mdb, func(db *mdbv1.MongoDBCommunity) {
778-
db.Spec.Users = []mdbv1.MongoDBUser{}
778+
db.Spec.Users = db.Spec.Users[:len(db.Spec.Users)-1]
779+
})
780+
781+
if err != nil {
782+
t.Fatal(err)
783+
}
784+
}
785+
}
786+
787+
func EditConnectionStringSecretNameOfLastUser(ctx context.Context, mdb *mdbv1.MongoDBCommunity, newSecretName string) func(*testing.T) {
788+
return func(t *testing.T) {
789+
err := e2eutil.UpdateMongoDBResource(ctx, mdb, func(db *mdbv1.MongoDBCommunity) {
790+
db.Spec.Users[len(db.Spec.Users)-1].ConnectionStringSecretName = newSecretName
779791
})
780792

781793
if err != nil {
@@ -790,7 +802,7 @@ func ConnectionStringSecretIsCleanedUp(ctx context.Context, mdb *mdbv1.MongoDBCo
790802
newErr := e2eutil.TestClient.Get(ctx, types.NamespacedName{Name: removedConnectionString, Namespace: mdb.Namespace}, &connectionStringSecret)
791803

792804
fmt.Println(newErr)
793-
assert.Error(t, newErr)
805+
assert.EqualError(t, newErr, fmt.Sprintf("secrets \"%s\" not found", removedConnectionString))
794806
}
795807
}
796808

@@ -803,3 +815,14 @@ func AuthUsersDeletedIsUpdated(ctx context.Context, mdb *mdbv1.MongoDBCommunity,
803815
assert.Contains(t, currentAc.Auth.UsersDeleted, deletedUser)
804816
}
805817
}
818+
819+
func AddUserToMongoDBCommunity(ctx context.Context, mdb *mdbv1.MongoDBCommunity, newUser mdbv1.MongoDBUser) func(t *testing.T) {
820+
return func(t *testing.T) {
821+
err := e2eutil.UpdateMongoDBResource(ctx, mdb, func(db *mdbv1.MongoDBCommunity) {
822+
db.Spec.Users = append(db.Spec.Users, newUser)
823+
})
824+
if err != nil {
825+
t.Fatal(err)
826+
}
827+
}
828+
}

test/e2e/replica_set_remove_user/replica_set_remove_user_test.go

+49-28
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package replica_set_remove_user
33
import (
44
"context"
55
"fmt"
6-
v1 "github.com/mongodb/mongodb-kubernetes-operator/api/v1"
6+
mdbv1 "github.com/mongodb/mongodb-kubernetes-operator/api/v1"
77
"github.com/mongodb/mongodb-kubernetes-operator/pkg/automationconfig"
88
e2eutil "github.com/mongodb/mongodb-kubernetes-operator/test/e2e"
99
"github.com/mongodb/mongodb-kubernetes-operator/test/e2e/mongodbtests"
@@ -36,28 +36,6 @@ func TestCleanupUsers(t *testing.T) {
3636
t.Fatal(err)
3737
}
3838

39-
lcr := automationconfig.CrdLogRotate{
40-
// fractional values are supported
41-
SizeThresholdMB: "0.1",
42-
LogRotate: automationconfig.LogRotate{
43-
TimeThresholdHrs: 1,
44-
NumUncompressed: 10,
45-
NumTotal: 10,
46-
IncludeAuditLogsWithMongoDBLogs: false,
47-
},
48-
PercentOfDiskspace: "1",
49-
}
50-
51-
systemLog := automationconfig.SystemLog{
52-
Destination: automationconfig.File,
53-
Path: "/tmp/mongod.log",
54-
LogAppend: false,
55-
}
56-
57-
// logRotate can only be configured if systemLog to file has been configured
58-
mdb.Spec.AgentConfiguration.LogRotate = &lcr
59-
mdb.Spec.AgentConfiguration.SystemLog = &systemLog
60-
6139
// config member options
6240
memberOptions := []automationconfig.MemberOptions{
6341
{
@@ -81,8 +59,46 @@ func TestCleanupUsers(t *testing.T) {
8159
settings := map[string]interface{}{
8260
"electionTimeoutMillis": float64(20),
8361
}
84-
mdb.Spec.AutomationConfigOverride = &v1.AutomationConfigOverride{
85-
ReplicaSet: v1.OverrideReplicaSet{Settings: v1.MapWrapper{Object: settings}},
62+
mdb.Spec.AutomationConfigOverride = &mdbv1.AutomationConfigOverride{
63+
ReplicaSet: mdbv1.OverrideReplicaSet{Settings: mdbv1.MapWrapper{Object: settings}},
64+
}
65+
66+
newUser := mdbv1.MongoDBUser{
67+
Name: fmt.Sprintf("%s-user-2", "mdb-0"),
68+
PasswordSecretRef: mdbv1.SecretKeyReference{
69+
Key: fmt.Sprintf("%s-password-2", "mdb-0"),
70+
Name: fmt.Sprintf("%s-%s-password-secret-2", "mdb-0", testCtx.ExecutionId),
71+
},
72+
Roles: []mdbv1.Role{
73+
// roles on testing db for general connectivity
74+
{
75+
DB: "testing",
76+
Name: "readWrite",
77+
},
78+
{
79+
DB: "testing",
80+
Name: "clusterAdmin",
81+
},
82+
// admin roles for reading FCV
83+
{
84+
DB: "admin",
85+
Name: "readWrite",
86+
},
87+
{
88+
DB: "admin",
89+
Name: "clusterAdmin",
90+
},
91+
{
92+
DB: "admin",
93+
Name: "userAdmin",
94+
},
95+
},
96+
ScramCredentialsSecretName: fmt.Sprintf("%s-my-scram-2", "mdb-0"),
97+
}
98+
99+
_, err = setup.GeneratePasswordForUser(testCtx, newUser, "")
100+
if err != nil {
101+
t.Fatal(err)
86102
}
87103

88104
tester, err := FromResource(ctx, t, mdb)
@@ -93,11 +109,16 @@ func TestCleanupUsers(t *testing.T) {
93109
t.Run("Create MongoDB Resource", mongodbtests.CreateMongoDBResource(&mdb, testCtx))
94110
t.Run("Basic tests", mongodbtests.BasicFunctionality(ctx, &mdb))
95111
t.Run("Keyfile authentication is configured", tester.HasKeyfileAuth(3))
96-
t.Run("AutomationConfig has the correct logRotateConfig", mongodbtests.AutomationConfigHasLogRotationConfig(ctx, &mdb, &lcr))
97112
t.Run("Test Basic Connectivity", tester.ConnectivitySucceeds())
98113
t.Run("Test SRV Connectivity", tester.ConnectivitySucceeds(WithURI(mdb.MongoSRVURI("")), WithoutTls(), WithReplicaSet(mdb.Name)))
99-
deletedUser := mdb.Spec.Users[0]
100-
t.Run("Delete user from MongoDB Resource", mongodbtests.RemoveAllUsersFromResource(ctx, &mdb))
114+
t.Run("Add new user to MongoDB Resource", mongodbtests.AddUserToMongoDBCommunity(ctx, &mdb, newUser))
115+
t.Run("MongoDB reaches Running phase", mongodbtests.MongoDBReachesRunningPhase(ctx, &mdb))
116+
editedUser := mdb.Spec.Users[1]
117+
t.Run("Edit connection string secret name of the added user", mongodbtests.EditConnectionStringSecretNameOfLastUser(ctx, &mdb, "other-secret-name"))
118+
t.Run("MongoDB reaches Running phase", mongodbtests.MongoDBReachesRunningPhase(ctx, &mdb))
119+
t.Run("Old connection string secret is cleaned up", mongodbtests.ConnectionStringSecretIsCleanedUp(ctx, &mdb, editedUser.GetConnectionStringSecretName(mdb.Name)))
120+
deletedUser := mdb.Spec.Users[1]
121+
t.Run("Remove last user from MongoDB Resource", mongodbtests.RemoveLastUserFromMongoDBCommunity(ctx, &mdb))
101122
t.Run("MongoDB reaches Pending phase", mongodbtests.MongoDBReachesPendingPhase(ctx, &mdb))
102123
t.Run("Removed users are added to automation config", mongodbtests.AuthUsersDeletedIsUpdated(ctx, &mdb, deletedUser))
103124
t.Run("MongoDB reaches Running phase", mongodbtests.MongoDBReachesRunningPhase(ctx, &mdb))

0 commit comments

Comments
 (0)