|
1 | 1 | package searchcontroller |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "fmt" |
| 6 | + "github.com/stretchr/testify/require" |
| 7 | + corev1 "k8s.io/api/core/v1" |
5 | 8 | "strings" |
6 | 9 | "testing" |
7 | 10 |
|
8 | 11 | "github.com/stretchr/testify/assert" |
| 12 | + "go.uber.org/zap" |
9 | 13 | "sigs.k8s.io/controller-runtime/pkg/client" |
10 | 14 |
|
11 | 15 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
12 | 16 |
|
13 | 17 | searchv1 "github.com/mongodb/mongodb-kubernetes/api/v1/search" |
14 | 18 | userv1 "github.com/mongodb/mongodb-kubernetes/api/v1/user" |
15 | 19 | "github.com/mongodb/mongodb-kubernetes/controllers/operator/mock" |
| 20 | + "github.com/mongodb/mongodb-kubernetes/controllers/operator/workflow" |
16 | 21 | mdbcv1 "github.com/mongodb/mongodb-kubernetes/mongodb-community-operator/api/v1" |
17 | 22 | kubernetesClient "github.com/mongodb/mongodb-kubernetes/mongodb-community-operator/pkg/kube/client" |
18 | 23 | ) |
19 | 24 |
|
| 25 | +func init() { |
| 26 | + logger, _ := zap.NewDevelopment() |
| 27 | + zap.ReplaceGlobals(logger) |
| 28 | +} |
| 29 | + |
| 30 | +func newTestMongoDBSearch(name, namespace string, modifications ...func(*searchv1.MongoDBSearch)) *searchv1.MongoDBSearch { |
| 31 | + mdbSearch := &searchv1.MongoDBSearch{ |
| 32 | + ObjectMeta: metav1.ObjectMeta{ |
| 33 | + Name: name, |
| 34 | + Namespace: namespace, |
| 35 | + }, |
| 36 | + Spec: searchv1.MongoDBSearchSpec{ |
| 37 | + Source: &searchv1.MongoDBSource{ |
| 38 | + MongoDBResourceRef: &userv1.MongoDBResourceRef{ |
| 39 | + Name: "test-mongodb", |
| 40 | + }, |
| 41 | + }, |
| 42 | + }, |
| 43 | + } |
| 44 | + |
| 45 | + for _, modify := range modifications { |
| 46 | + modify(mdbSearch) |
| 47 | + } |
| 48 | + |
| 49 | + return mdbSearch |
| 50 | +} |
| 51 | + |
| 52 | +func newTestMongoDBCommunity(name, namespace string, modifications ...func(*mdbcv1.MongoDBCommunity)) *mdbcv1.MongoDBCommunity { |
| 53 | + mdbc := &mdbcv1.MongoDBCommunity{ |
| 54 | + ObjectMeta: metav1.ObjectMeta{ |
| 55 | + Name: name, |
| 56 | + Namespace: namespace, |
| 57 | + }, |
| 58 | + Spec: mdbcv1.MongoDBCommunitySpec{ |
| 59 | + Version: "8.2.0", |
| 60 | + Members: 3, |
| 61 | + }, |
| 62 | + } |
| 63 | + |
| 64 | + for _, modify := range modifications { |
| 65 | + modify(mdbc) |
| 66 | + } |
| 67 | + |
| 68 | + return mdbc |
| 69 | +} |
| 70 | + |
| 71 | +func newTestOperatorSearchConfig() OperatorSearchConfig { |
| 72 | + config := OperatorSearchConfig{ |
| 73 | + SearchRepo: "test-repo", |
| 74 | + SearchName: "mongot", |
| 75 | + SearchVersion: "0.0.0", |
| 76 | + } |
| 77 | + |
| 78 | + return config |
| 79 | +} |
| 80 | + |
| 81 | +func newTestFakeClient(objects ...client.Object) kubernetesClient.Client { |
| 82 | + clientBuilder := mock.NewEmptyFakeClientBuilder() |
| 83 | + clientBuilder.WithIndex(&searchv1.MongoDBSearch{}, MongoDBSearchIndexFieldName, func(obj client.Object) []string { |
| 84 | + mdbResource := obj.(*searchv1.MongoDBSearch).GetMongoDBResourceRef() |
| 85 | + return []string{mdbResource.Namespace + "/" + mdbResource.Name} |
| 86 | + }) |
| 87 | + clientBuilder.WithObjects(objects...) |
| 88 | + return kubernetesClient.NewClient(clientBuilder.Build()) |
| 89 | +} |
| 90 | + |
| 91 | +func reconcileMongoDBSearch(ctx context.Context, fakeClient kubernetesClient.Client, mdbSearch *searchv1.MongoDBSearch, mdbc *mdbcv1.MongoDBCommunity, operatorConfig OperatorSearchConfig) workflow.Status { |
| 92 | + helper := NewMongoDBSearchReconcileHelper( |
| 93 | + fakeClient, |
| 94 | + mdbSearch, |
| 95 | + NewCommunityResourceSearchSource(mdbc), |
| 96 | + operatorConfig, |
| 97 | + ) |
| 98 | + |
| 99 | + return helper.Reconcile(ctx, zap.S()) |
| 100 | +} |
| 101 | + |
20 | 102 | func TestMongoDBSearchReconcileHelper_ValidateSingleMongoDBSearchForSearchSource(t *testing.T) { |
21 | 103 | mdbSearchSpec := searchv1.MongoDBSearchSpec{ |
22 | 104 | Source: &searchv1.MongoDBSource{ |
@@ -152,3 +234,96 @@ func TestGetMongodConfigParameters_TransportAndPorts(t *testing.T) { |
152 | 234 | }) |
153 | 235 | } |
154 | 236 | } |
| 237 | + |
| 238 | +func assertServiceBasicProperties(t *testing.T, svc corev1.Service, mdbSearch *searchv1.MongoDBSearch) { |
| 239 | + t.Helper() |
| 240 | + svcName := mdbSearch.SearchServiceNamespacedName() |
| 241 | + |
| 242 | + assert.Equal(t, svcName.Name, svc.Name) |
| 243 | + assert.Equal(t, svcName.Namespace, svc.Namespace) |
| 244 | + assert.Equal(t, "ClusterIP", string(svc.Spec.Type)) |
| 245 | + assert.Equal(t, "None", svc.Spec.ClusterIP) |
| 246 | + assert.False(t, svc.Spec.PublishNotReadyAddresses) |
| 247 | + |
| 248 | + expectedAppLabel := svcName.Name |
| 249 | + assert.Equal(t, expectedAppLabel, svc.Labels["app"]) |
| 250 | + assert.Equal(t, expectedAppLabel, svc.Spec.Selector["app"]) |
| 251 | +} |
| 252 | + |
| 253 | +func assertServicePorts(t *testing.T, svc corev1.Service, expectedPorts map[string]int32) { |
| 254 | + t.Helper() |
| 255 | + |
| 256 | + portMap := make(map[string]int32) |
| 257 | + for _, port := range svc.Spec.Ports { |
| 258 | + portMap[port.Name] = port.Port |
| 259 | + } |
| 260 | + |
| 261 | + assert.Len(t, svc.Spec.Ports, len(expectedPorts), "Expected %d ports but got %d", len(expectedPorts), len(svc.Spec.Ports)) |
| 262 | + |
| 263 | + for portName, expectedPort := range expectedPorts { |
| 264 | + actualPort, exists := portMap[portName] |
| 265 | + assert.True(t, exists, "Expected port %s to exist", portName) |
| 266 | + assert.Equal(t, expectedPort, actualPort, "Port %s has wrong value", portName) |
| 267 | + } |
| 268 | +} |
| 269 | + |
| 270 | +func TestMongoDBSearchReconcileHelper_ServiceCreation(t *testing.T) { |
| 271 | + cases := []struct { |
| 272 | + name string |
| 273 | + modifySearch func(*searchv1.MongoDBSearch) |
| 274 | + expectedPorts map[string]int32 |
| 275 | + }{ |
| 276 | + { |
| 277 | + name: "Default configuration with prometheus enabled", |
| 278 | + modifySearch: func(search *searchv1.MongoDBSearch) { |
| 279 | + search.Spec.Prometheus = &searchv1.Prometheus{} |
| 280 | + }, |
| 281 | + expectedPorts: map[string]int32{ |
| 282 | + "mongot-grpc": searchv1.MongotDefaultGrpcPort, |
| 283 | + "prometheus": searchv1.MongotDefaultPrometheusPort, |
| 284 | + "healthcheck": searchv1.MongotDefautHealthCheckPort, |
| 285 | + }, |
| 286 | + }, |
| 287 | + { |
| 288 | + name: "Prometheus enabled with custom port", |
| 289 | + modifySearch: func(search *searchv1.MongoDBSearch) { |
| 290 | + search.Spec.Prometheus = &searchv1.Prometheus{ |
| 291 | + Port: 9999, |
| 292 | + } |
| 293 | + }, |
| 294 | + expectedPorts: map[string]int32{ |
| 295 | + "mongot-grpc": searchv1.MongotDefaultGrpcPort, |
| 296 | + "prometheus": 9999, |
| 297 | + "healthcheck": searchv1.MongotDefautHealthCheckPort, |
| 298 | + }, |
| 299 | + }, |
| 300 | + { |
| 301 | + name: "Prometheus disabled", |
| 302 | + modifySearch: func(search *searchv1.MongoDBSearch) { |
| 303 | + search.Spec.Prometheus = nil |
| 304 | + }, |
| 305 | + expectedPorts: map[string]int32{ |
| 306 | + "mongot-grpc": searchv1.MongotDefaultGrpcPort, |
| 307 | + "healthcheck": searchv1.MongotDefautHealthCheckPort, |
| 308 | + }, |
| 309 | + }, |
| 310 | + } |
| 311 | + |
| 312 | + for _, tc := range cases { |
| 313 | + t.Run(tc.name, func(t *testing.T) { |
| 314 | + mdbSearch := newTestMongoDBSearch("test-mongodb-search", "test", tc.modifySearch) |
| 315 | + mdbc := newTestMongoDBCommunity("test-mongodb", "test") |
| 316 | + fakeClient := newTestFakeClient(mdbSearch, mdbc) |
| 317 | + |
| 318 | + reconcileMongoDBSearch(t.Context(), fakeClient, mdbSearch, mdbc, newTestOperatorSearchConfig()) |
| 319 | + |
| 320 | + svcName := mdbSearch.SearchServiceNamespacedName() |
| 321 | + svc, err := fakeClient.GetService(t.Context(), svcName) |
| 322 | + require.NoError(t, err) |
| 323 | + require.NotNil(t, svc) |
| 324 | + |
| 325 | + assertServiceBasicProperties(t, svc, mdbSearch) |
| 326 | + assertServicePorts(t, svc, tc.expectedPorts) |
| 327 | + }) |
| 328 | + } |
| 329 | +} |
0 commit comments