88
99 "github.com/ghodss/yaml"
1010 "github.com/stretchr/testify/assert"
11+ "github.com/stretchr/testify/require"
1112 "k8s.io/apimachinery/pkg/types"
1213 "k8s.io/utils/ptr"
1314 "sigs.k8s.io/controller-runtime/pkg/client"
@@ -22,12 +23,12 @@ import (
2223 "github.com/mongodb/mongodb-kubernetes/api/v1/status"
2324 userv1 "github.com/mongodb/mongodb-kubernetes/api/v1/user"
2425 "github.com/mongodb/mongodb-kubernetes/controllers/operator/mock"
25- "github.com/mongodb/mongodb-kubernetes/controllers/operator/workflow"
2626 "github.com/mongodb/mongodb-kubernetes/controllers/searchcontroller"
2727 mdbcv1 "github.com/mongodb/mongodb-kubernetes/mongodb-community-operator/api/v1"
2828 "github.com/mongodb/mongodb-kubernetes/mongodb-community-operator/api/v1/common"
2929 "github.com/mongodb/mongodb-kubernetes/mongodb-community-operator/pkg/mongot"
3030 "github.com/mongodb/mongodb-kubernetes/mongodb-community-operator/pkg/util/constants"
31+ "github.com/mongodb/mongodb-kubernetes/pkg/util"
3132)
3233
3334func newMongoDBCommunity (name , namespace string ) * mdbcv1.MongoDBCommunity {
@@ -135,10 +136,6 @@ func buildExpectedMongotConfig(search *searchv1.MongoDBSearch, mdbc *mdbcv1.Mong
135136 },
136137 Wireproto : wireprotoServer ,
137138 },
138- Metrics : mongot.ConfigMetrics {
139- Enabled : true ,
140- Address : fmt .Sprintf ("0.0.0.0:%d" , search .GetMongotMetricsPort ()),
141- },
142139 HealthCheck : mongot.ConfigHealthCheck {
143140 Address : fmt .Sprintf ("0.0.0.0:%d" , search .GetMongotHealthCheckPort ()),
144141 },
@@ -205,22 +202,16 @@ func TestMongoDBSearchReconcile_Success(t *testing.T) {
205202 mdbc := newMongoDBCommunity ("mdb" , mock .TestNamespace )
206203 reconciler , c := newSearchReconciler (mdbc , search )
207204
208- res , err := reconciler .Reconcile (
209- ctx ,
210- reconcile.Request {NamespacedName : types.NamespacedName {Name : search .Name , Namespace : search .Namespace }},
211- )
212- expected , _ := workflow .OK ().ReconcileResult ()
213- assert .NoError (t , err )
214- assert .Equal (t , expected , res )
205+ checkSearchReconcileSuccessful (ctx , t , reconciler , c , search )
215206
216207 svc := & corev1.Service {}
217- err = c .Get (ctx , search .SearchServiceNamespacedName (), svc )
208+ err : = c .Get (ctx , search .SearchServiceNamespacedName (), svc )
218209 assert .NoError (t , err )
219210 servicePortNames := []string {}
220211 for _ , port := range svc .Spec .Ports {
221212 servicePortNames = append (servicePortNames , port .Name )
222213 }
223- expectedPortNames := []string {"mongot-grpc" , "metrics" , " healthcheck" }
214+ expectedPortNames := []string {"mongot-grpc" , "healthcheck" }
224215 if tc .withWireproto {
225216 expectedPortNames = append (expectedPortNames , "mongot-wireproto" )
226217 }
@@ -254,14 +245,42 @@ func checkSearchReconcileFailed(
254245 reconcile.Request {NamespacedName : types.NamespacedName {Name : search .Name , Namespace : search .Namespace }},
255246 )
256247 assert .NoError (t , err )
257- assert .True (t , res .RequeueAfter > 0 )
248+ assert .Less (t , res .RequeueAfter , util . TWENTY_FOUR_HOURS )
258249
259250 updated := & searchv1.MongoDBSearch {}
260251 assert .NoError (t , c .Get (ctx , types.NamespacedName {Name : search .Name , Namespace : search .Namespace }, updated ))
261252 assert .Equal (t , status .PhaseFailed , updated .Status .Phase )
262253 assert .Contains (t , updated .Status .Message , expectedMsg )
263254}
264255
256+ // checkSearchReconcileSuccessful performs reconcile to check if it gets to a Running state.
257+ // In case it's a first reconcile and still Pending it's retried with mocked sts simulated as ready.
258+ func checkSearchReconcileSuccessful (
259+ ctx context.Context ,
260+ t * testing.T ,
261+ reconciler * MongoDBSearchReconciler ,
262+ c client.Client ,
263+ search * searchv1.MongoDBSearch ,
264+ ) {
265+ namespacedName := types.NamespacedName {Name : search .Name , Namespace : search .Namespace }
266+ res , err := reconciler .Reconcile (ctx , reconcile.Request {NamespacedName : namespacedName })
267+ require .NoError (t , err )
268+ mdbs := & searchv1.MongoDBSearch {}
269+ require .NoError (t , c .Get (ctx , namespacedName , mdbs ))
270+ if mdbs .Status .Phase == status .PhasePending {
271+ // mark mocked search statefulset as ready to not return Pending this time
272+ require .NoError (t , mock .MarkAllStatefulSetsAsReady (ctx , search .Namespace , c ))
273+
274+ res , err = reconciler .Reconcile (ctx , reconcile.Request {NamespacedName : namespacedName })
275+ require .NoError (t , err )
276+ mdbs = & searchv1.MongoDBSearch {}
277+ require .NoError (t , c .Get (ctx , namespacedName , mdbs ))
278+ }
279+
280+ require .Equal (t , util .TWENTY_FOUR_HOURS , res .RequeueAfter )
281+ require .Equal (t , status .PhaseRunning , mdbs .Status .Phase )
282+ }
283+
265284func TestMongoDBSearchReconcile_InvalidVersion (t * testing.T ) {
266285 ctx := context .Background ()
267286 search := newMongoDBSearch ("search" , mock .TestNamespace , "mdb" )
0 commit comments