Skip to content

Commit 7dedfee

Browse files
committed
fix tests
1 parent 5ec3baa commit 7dedfee

7 files changed

+516
-66
lines changed

controllers/operator/mongodbsearch_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (r *MongoDBSearchReconciler) getSourceMongoDBForSearch(ctx context.Context,
7474
log.Infof("Looking up Search source %s", sourceName)
7575

7676
mdb := &mdbv1.MongoDB{}
77-
if err := kubeClient.Get(ctx, sourceName, mdb); err != nil && !apierrors.IsNotFound(err) {
77+
if err := kubeClient.Get(ctx, sourceName, mdb); err != nil {
7878
if !apierrors.IsNotFound(err) {
7979
return nil, xerrors.Errorf("error getting MongoDB %s: %w", sourceName, err)
8080
}

controllers/operator/mongodbsearch_controller_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
mdbcv1 "github.com/mongodb/mongodb-kubernetes/mongodb-community-operator/api/v1"
3030
"github.com/mongodb/mongodb-kubernetes/mongodb-community-operator/api/v1/common"
3131
"github.com/mongodb/mongodb-kubernetes/mongodb-community-operator/pkg/mongot"
32+
"github.com/mongodb/mongodb-kubernetes/mongodb-community-operator/pkg/util/constants"
3233
)
3334

3435
func newMongoDBCommunity(name, namespace string) *mdbcv1.MongoDBCommunity {
@@ -62,7 +63,16 @@ func newSearchReconcilerWithOperatorConfig(
6263
builder.WithIndex(&searchv1.MongoDBSearch{}, search_controller.MongoDBSearchIndexFieldName, mdbcSearchIndexBuilder)
6364

6465
if mdbc != nil {
65-
builder.WithObjects(mdbc)
66+
keyfileSecret := &corev1.Secret{
67+
ObjectMeta: metav1.ObjectMeta{
68+
Name: mdbc.GetAgentKeyfileSecretNamespacedName().Name,
69+
Namespace: mdbc.Namespace,
70+
},
71+
StringData: map[string]string{
72+
constants.AgentKeyfileKey: "keyfile",
73+
},
74+
}
75+
builder.WithObjects(mdbc, keyfileSecret)
6676
}
6777

6878
for _, search := range searches {

controllers/search_controller/community_search_source.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,13 @@ func (r *CommunitySearchSource) Validate() error {
6666
foundScram := false
6767
for _, authMode := range r.Spec.Security.Authentication.Modes {
6868
// Check for SCRAM, SCRAM-SHA-1, or SCRAM-SHA-256
69-
if strings.HasPrefix(string(authMode), util.SCRAM) {
69+
if strings.HasPrefix(strings.ToUpper(string(authMode)), util.SCRAM) {
7070
foundScram = true
7171
break
7272
}
7373
}
7474

75-
if !foundScram {
75+
if !foundScram && len(r.Spec.Security.Authentication.Modes) > 0 {
7676
return xerrors.New("MongoDBSearch requires SCRAM authentication to be enabled")
7777
}
7878

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
package search_controller
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
8+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9+
10+
mdbcv1 "github.com/mongodb/mongodb-kubernetes/mongodb-community-operator/api/v1"
11+
)
12+
13+
func newCommunitySearchSource(version string, authModes []mdbcv1.AuthMode) *CommunitySearchSource {
14+
return &CommunitySearchSource{
15+
MongoDBCommunity: &mdbcv1.MongoDBCommunity{
16+
ObjectMeta: metav1.ObjectMeta{
17+
Name: "test-mongodb",
18+
Namespace: "test-namespace",
19+
},
20+
Spec: mdbcv1.MongoDBCommunitySpec{
21+
Version: version,
22+
Security: mdbcv1.Security{
23+
Authentication: mdbcv1.Authentication{
24+
Modes: authModes,
25+
},
26+
},
27+
},
28+
},
29+
}
30+
}
31+
32+
func TestCommunitySearchSource_Validate(t *testing.T) {
33+
cases := []struct {
34+
name string
35+
version string
36+
authModes []mdbcv1.AuthMode
37+
expectError bool
38+
expectedErrMsg string
39+
}{
40+
// Version validation tests
41+
{
42+
name: "Invalid version",
43+
version: "invalid.version",
44+
authModes: []mdbcv1.AuthMode{"SCRAM-SHA-256"},
45+
expectError: true,
46+
expectedErrMsg: "error parsing MongoDB version",
47+
},
48+
{
49+
name: "Version too old",
50+
version: "7.0.0",
51+
authModes: []mdbcv1.AuthMode{"SCRAM-SHA-256"},
52+
expectError: true,
53+
expectedErrMsg: "MongoDB version must be 8.0.10 or higher",
54+
},
55+
{
56+
name: "Version just below minimum",
57+
version: "8.0.9",
58+
authModes: []mdbcv1.AuthMode{"SCRAM-SHA-256"},
59+
expectError: true,
60+
expectedErrMsg: "MongoDB version must be 8.0.10 or higher",
61+
},
62+
{
63+
name: "Valid minimum version",
64+
version: "8.0.10",
65+
authModes: []mdbcv1.AuthMode{"SCRAM-SHA-256"},
66+
expectError: false,
67+
},
68+
{
69+
name: "Version above minimum",
70+
version: "8.1.0",
71+
authModes: []mdbcv1.AuthMode{"SCRAM-SHA-256"},
72+
expectError: false,
73+
},
74+
{
75+
name: "Version with build number",
76+
version: "8.1.0-rc1",
77+
authModes: []mdbcv1.AuthMode{"SCRAM-SHA-256"},
78+
expectError: false,
79+
},
80+
// Authentication mode tests - empty/nil cases
81+
{
82+
name: "Empty auth modes",
83+
version: "8.0.10",
84+
authModes: []mdbcv1.AuthMode{},
85+
expectError: false,
86+
},
87+
{
88+
name: "Nil auth modes",
89+
version: "8.0.10",
90+
authModes: nil,
91+
expectError: false,
92+
},
93+
{
94+
name: "X509 mode only",
95+
version: "8.0.10",
96+
authModes: []mdbcv1.AuthMode{"X509"},
97+
expectError: true,
98+
expectedErrMsg: "MongoDBSearch requires SCRAM authentication to be enabled",
99+
},
100+
{
101+
name: "X509 and SCRAM",
102+
version: "8.0.10",
103+
authModes: []mdbcv1.AuthMode{"X509", "SCRAM-SHA-256"},
104+
expectError: false,
105+
},
106+
{
107+
name: "Multiple auth modes with SCRAM first",
108+
version: "8.0.10",
109+
authModes: []mdbcv1.AuthMode{"SCRAM-SHA-1", "X509"},
110+
expectError: false,
111+
},
112+
{
113+
name: "Multiple auth modes with SCRAM last",
114+
version: "8.0.10",
115+
authModes: []mdbcv1.AuthMode{"PLAIN", "X509", "SCRAM-SHA-256"},
116+
expectError: false,
117+
},
118+
{
119+
name: "Multiple non-SCRAM modes",
120+
version: "8.0.10",
121+
authModes: []mdbcv1.AuthMode{"PLAIN", "X509"},
122+
expectError: true,
123+
expectedErrMsg: "MongoDBSearch requires SCRAM authentication to be enabled",
124+
},
125+
// SCRAM variant tests
126+
{
127+
name: "SCRAM only",
128+
version: "8.0.10",
129+
authModes: []mdbcv1.AuthMode{"SCRAM"},
130+
expectError: false,
131+
},
132+
{
133+
name: "SCRAM-SHA-1 only",
134+
version: "8.0.10",
135+
authModes: []mdbcv1.AuthMode{"SCRAM-SHA-1"},
136+
expectError: false,
137+
},
138+
{
139+
name: "SCRAM-SHA-256 only",
140+
version: "8.0.10",
141+
authModes: []mdbcv1.AuthMode{"SCRAM-SHA-256"},
142+
expectError: false,
143+
},
144+
{
145+
name: "All SCRAM variants",
146+
version: "8.0.10",
147+
authModes: []mdbcv1.AuthMode{"SCRAM", "SCRAM-SHA-1", "SCRAM-SHA-256"},
148+
expectError: false,
149+
},
150+
// Case-insensitive tests (now supported with ToUpper)
151+
{
152+
name: "Lowercase SCRAM",
153+
version: "8.0.10",
154+
authModes: []mdbcv1.AuthMode{"scram-sha-256"},
155+
expectError: false,
156+
},
157+
{
158+
name: "Mixed case SCRAM",
159+
version: "8.0.10",
160+
authModes: []mdbcv1.AuthMode{"Scram-Sha-256"},
161+
expectError: false,
162+
},
163+
// Edge case tests
164+
{
165+
name: "PLAIN only",
166+
version: "8.0.10",
167+
authModes: []mdbcv1.AuthMode{"PLAIN"},
168+
expectError: true,
169+
expectedErrMsg: "MongoDBSearch requires SCRAM authentication to be enabled",
170+
},
171+
// Combined validation tests
172+
{
173+
name: "Invalid version with valid auth",
174+
version: "7.0.0",
175+
authModes: []mdbcv1.AuthMode{"SCRAM-SHA-256"},
176+
expectError: true,
177+
expectedErrMsg: "MongoDB version must be 8.0.10 or higher",
178+
},
179+
{
180+
name: "Valid version with invalid auth",
181+
version: "8.0.10",
182+
authModes: []mdbcv1.AuthMode{"X509"},
183+
expectError: true,
184+
expectedErrMsg: "MongoDBSearch requires SCRAM authentication to be enabled",
185+
},
186+
{
187+
name: "Invalid version with invalid auth",
188+
version: "7.0.0",
189+
authModes: []mdbcv1.AuthMode{"X509"},
190+
expectError: true,
191+
expectedErrMsg: "MongoDB version must be 8.0.10 or higher", // Should fail on version first
192+
},
193+
}
194+
195+
for _, c := range cases {
196+
t.Run(c.name, func(t *testing.T) {
197+
src := newCommunitySearchSource(c.version, c.authModes)
198+
err := src.Validate()
199+
200+
if c.expectError {
201+
assert.Error(t, err)
202+
assert.Contains(t, err.Error(), c.expectedErrMsg)
203+
} else {
204+
assert.NoError(t, err)
205+
}
206+
})
207+
}
208+
}

controllers/search_controller/enterprise_search_source.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,17 @@ func (r EnterpriseResourceSearchSource) Validate() error {
7171
return xerrors.Errorf("MongoDBSearch is only supported for %s resources", mdbv1.ReplicaSet)
7272
}
7373

74+
authModes := r.Spec.GetSecurityAuthenticationModes()
7475
foundScram := false
75-
for _, authMode := range r.Spec.GetSecurityAuthenticationModes() {
76+
for _, authMode := range authModes {
7677
// Check for SCRAM, SCRAM-SHA-1, or SCRAM-SHA-256
77-
if strings.HasPrefix(authMode, util.SCRAM) {
78+
if strings.HasPrefix(strings.ToUpper(authMode), util.SCRAM) {
7879
foundScram = true
7980
break
8081
}
8182
}
8283

83-
if !foundScram {
84+
if !foundScram && len(authModes) > 0 {
8485
return xerrors.New("MongoDBSearch requires SCRAM authentication to be enabled")
8586
}
8687

0 commit comments

Comments
 (0)