Skip to content

Commit cdc55d4

Browse files
committed
update unit tests for wireproto annotation
1 parent 53e9334 commit cdc55d4

File tree

3 files changed

+139
-32
lines changed

3 files changed

+139
-32
lines changed

controllers/operator/mongodbsearch_controller_test.go

Lines changed: 74 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,19 @@ func buildExpectedMongotConfig(search *searchv1.MongoDBSearch, mdbc *mdbcv1.Mong
100100
if search.Spec.LogLevel != "" {
101101
logLevel = string(search.Spec.LogLevel)
102102
}
103+
104+
var wireprotoServer *mongot.ConfigWireproto
105+
if search.IsWireprotoForced() {
106+
wireprotoServer = &mongot.ConfigWireproto{
107+
Address: fmt.Sprintf("0.0.0.0:%d", search.GetMongotWireprotoPort()),
108+
Authentication: &mongot.ConfigAuthentication{
109+
Mode: "keyfile",
110+
KeyFile: searchcontroller.TempKeyfilePath,
111+
},
112+
TLS: &mongot.ConfigWireprotoTLS{Mode: mongot.ConfigTLSModeDisabled},
113+
}
114+
}
115+
103116
return mongot.Config{
104117
SyncSource: mongot.ConfigSyncSource{
105118
ReplicaSet: mongot.ConfigReplicaSet{
@@ -119,6 +132,7 @@ func buildExpectedMongotConfig(search *searchv1.MongoDBSearch, mdbc *mdbcv1.Mong
119132
Address: fmt.Sprintf("0.0.0.0:%d", search.GetMongotGrpcPort()),
120133
TLS: &mongot.ConfigGrpcTLS{Mode: mongot.ConfigTLSModeDisabled},
121134
},
135+
Wireproto: wireprotoServer,
122136
},
123137
Metrics: mongot.ConfigMetrics{
124138
Enabled: true,
@@ -164,35 +178,69 @@ func TestMongoDBSearchReconcile_MissingSource(t *testing.T) {
164178

165179
func TestMongoDBSearchReconcile_Success(t *testing.T) {
166180
ctx := context.Background()
167-
search := newMongoDBSearch("search", mock.TestNamespace, "mdb")
168-
search.Spec.LogLevel = "WARN"
169-
170-
mdbc := newMongoDBCommunity("mdb", mock.TestNamespace)
171-
reconciler, c := newSearchReconciler(mdbc, search)
172181

173-
res, err := reconciler.Reconcile(
174-
ctx,
175-
reconcile.Request{NamespacedName: types.NamespacedName{Name: search.Name, Namespace: search.Namespace}},
176-
)
177-
expected, _ := workflow.OK().ReconcileResult()
178-
assert.NoError(t, err)
179-
assert.Equal(t, expected, res)
180-
181-
svc := &corev1.Service{}
182-
err = c.Get(ctx, search.SearchServiceNamespacedName(), svc)
183-
assert.NoError(t, err)
182+
tests := []struct {
183+
name string
184+
withWireproto bool
185+
}{
186+
{
187+
name: "grpc only (default)",
188+
withWireproto: false,
189+
},
190+
{
191+
name: "grpc + wireproto via annotation",
192+
withWireproto: true,
193+
},
194+
}
184195

185-
cm := &corev1.ConfigMap{}
186-
err = c.Get(ctx, search.MongotConfigConfigMapNamespacedName(), cm)
187-
assert.NoError(t, err)
188-
expectedConfig := buildExpectedMongotConfig(search, mdbc)
189-
configYaml, err := yaml.Marshal(expectedConfig)
190-
assert.NoError(t, err)
191-
assert.Equal(t, string(configYaml), cm.Data[searchcontroller.MongotConfigFilename])
196+
for _, tc := range tests {
197+
t.Run(tc.name, func(t *testing.T) {
198+
search := newMongoDBSearch("search", mock.TestNamespace, "mdb")
199+
search.Spec.LogLevel = "WARN"
200+
if tc.withWireproto {
201+
if search.Annotations == nil {
202+
search.Annotations = map[string]string{}
203+
}
204+
search.Annotations[searchv1.ForceWireprotoTransportAnnotation] = "true"
205+
}
192206

193-
sts := &appsv1.StatefulSet{}
194-
err = c.Get(ctx, search.StatefulSetNamespacedName(), sts)
195-
assert.NoError(t, err)
207+
mdbc := newMongoDBCommunity("mdb", mock.TestNamespace)
208+
reconciler, c := newSearchReconciler(mdbc, search)
209+
210+
res, err := reconciler.Reconcile(
211+
ctx,
212+
reconcile.Request{NamespacedName: types.NamespacedName{Name: search.Name, Namespace: search.Namespace}},
213+
)
214+
expected, _ := workflow.OK().ReconcileResult()
215+
assert.NoError(t, err)
216+
assert.Equal(t, expected, res)
217+
218+
svc := &corev1.Service{}
219+
err = c.Get(ctx, search.SearchServiceNamespacedName(), svc)
220+
assert.NoError(t, err)
221+
servicePortNames := []string{}
222+
for _, port := range svc.Spec.Ports {
223+
servicePortNames = append(servicePortNames, port.Name)
224+
}
225+
expectedPortNames := []string{"mongot-grpc", "metrics", "healthcheck"}
226+
if tc.withWireproto {
227+
expectedPortNames = append(expectedPortNames, "mongot-wireproto")
228+
}
229+
assert.ElementsMatch(t, expectedPortNames, servicePortNames)
230+
231+
cm := &corev1.ConfigMap{}
232+
err = c.Get(ctx, search.MongotConfigConfigMapNamespacedName(), cm)
233+
assert.NoError(t, err)
234+
expectedConfig := buildExpectedMongotConfig(search, mdbc)
235+
configYaml, err := yaml.Marshal(expectedConfig)
236+
assert.NoError(t, err)
237+
assert.Equal(t, string(configYaml), cm.Data[searchcontroller.MongotConfigFilename])
238+
239+
sts := &appsv1.StatefulSet{}
240+
err = c.Get(ctx, search.StatefulSetNamespacedName(), sts)
241+
assert.NoError(t, err)
242+
})
243+
}
196244
}
197245

198246
func checkSearchReconcileFailed(

controllers/searchcontroller/mongodbsearch_reconcile_helper.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -317,12 +317,14 @@ func buildSearchHeadlessService(search *searchv1.MongoDBSearch) corev1.Service {
317317
SetPublishNotReadyAddresses(true).
318318
SetOwnerReferences(search.GetOwnerReferences())
319319

320-
serviceBuilder.AddPort(&corev1.ServicePort{
321-
Name: "mongot-wireproto",
322-
Protocol: corev1.ProtocolTCP,
323-
Port: search.GetMongotWireprotoPort(),
324-
TargetPort: intstr.FromInt32(search.GetMongotWireprotoPort()),
325-
})
320+
if search.IsWireprotoForced() {
321+
serviceBuilder.AddPort(&corev1.ServicePort{
322+
Name: "mongot-wireproto",
323+
Protocol: corev1.ProtocolTCP,
324+
Port: search.GetMongotWireprotoPort(),
325+
TargetPort: intstr.FromInt32(search.GetMongotWireprotoPort()),
326+
})
327+
}
326328

327329
serviceBuilder.AddPort(&corev1.ServicePort{
328330
Name: "mongot-grpc",

controllers/searchcontroller/mongodbsearch_reconcile_helper_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package searchcontroller
22

33
import (
4+
"fmt"
5+
"strings"
46
"testing"
57

68
"github.com/stretchr/testify/assert"
@@ -146,3 +148,58 @@ func TestNeedsSearchCoordinatorRolePolyfill(t *testing.T) {
146148
})
147149
}
148150
}
151+
152+
func TestGetMongodConfigParameters_TransportAndPorts(t *testing.T) {
153+
cases := []struct {
154+
name string
155+
withWireproto bool
156+
expectedUseGrpc bool
157+
}{
158+
{
159+
name: "grpc only (default)",
160+
withWireproto: false,
161+
expectedUseGrpc: true,
162+
},
163+
{
164+
name: "grpc + wireproto via annotation",
165+
withWireproto: true,
166+
expectedUseGrpc: false,
167+
},
168+
}
169+
170+
for _, tc := range cases {
171+
t.Run(tc.name, func(t *testing.T) {
172+
search := &searchv1.MongoDBSearch{
173+
ObjectMeta: metav1.ObjectMeta{
174+
Name: "test-mongodb-search",
175+
Namespace: "test",
176+
},
177+
}
178+
if tc.withWireproto {
179+
search.Annotations = map[string]string{searchv1.ForceWireprotoTransportAnnotation: "true"}
180+
}
181+
182+
clusterDomain := "cluster.local"
183+
params := GetMongodConfigParameters(search, clusterDomain)
184+
185+
setParams := params["setParameter"].(map[string]any)
186+
187+
useGrpc := setParams["useGrpcForSearch"].(bool)
188+
assert.Equal(t, tc.expectedUseGrpc, useGrpc)
189+
190+
expectedPort := search.GetMongotGrpcPort()
191+
if tc.withWireproto {
192+
expectedPort = search.GetMongotWireprotoPort()
193+
}
194+
expectedPrefix := fmt.Sprintf("%s.%s.svc.%s", search.Name+"-search-svc", search.Namespace, clusterDomain)
195+
expectedSuffix := fmt.Sprintf(":%d", expectedPort)
196+
197+
for _, key := range []string{"mongotHost", "searchIndexManagementHostAndPort"} {
198+
value := setParams[key].(string)
199+
if !strings.HasPrefix(value, expectedPrefix) || !strings.HasSuffix(value, expectedSuffix) {
200+
t.Fatalf("%s mismatch: expected prefix %q and suffix %q, got %q", key, expectedPrefix, expectedSuffix, value)
201+
}
202+
}
203+
})
204+
}
205+
}

0 commit comments

Comments
 (0)