55 "os"
66 "reflect"
77 "testing"
8+ "time"
9+
10+ k8sClient "sigs.k8s.io/controller-runtime/pkg/client"
811
912 "github.com/mongodb/mongodb-kubernetes-operator/pkg/kube/probes"
1013
@@ -111,11 +114,11 @@ func TestChangingVersion_ResultsInRollingUpdateStrategyType(t *testing.T) {
111114 mgr := client .NewManager (& mdb )
112115 mgrClient := mgr .GetClient ()
113116 r := newReconciler (mgr , mockManifestProvider (mdb .Spec .Version ))
114- res , err := r .Reconcile (reconcile.Request {NamespacedName : types. NamespacedName { Namespace : mdb .Namespace , Name : mdb . Name } })
117+ res , err := r .Reconcile (reconcile.Request {NamespacedName : mdb .NamespacedName () })
115118 assertReconciliationSuccessful (t , res , err )
116119
117120 // fetch updated resource after first reconciliation
118- _ = mgrClient .Get (context .TODO (), types. NamespacedName { Name : mdb .Name , Namespace : mdb . Namespace } , & mdb )
121+ _ = mgrClient .Get (context .TODO (), mdb .NamespacedName () , & mdb )
119122
120123 sts := appsv1.StatefulSet {}
121124 err = mgrClient .Get (context .TODO (), types.NamespacedName {Name : mdb .Name , Namespace : mdb .Namespace }, & sts )
@@ -127,6 +130,26 @@ func TestChangingVersion_ResultsInRollingUpdateStrategyType(t *testing.T) {
127130
128131 _ = mgrClient .Update (context .TODO (), & mdb )
129132
133+ // agents start the upgrade, they are not all ready
134+ sts .Status .UpdatedReplicas = 1
135+ sts .Status .ReadyReplicas = 2
136+ err = mgrClient .Update (context .TODO (), & sts )
137+
138+ _ = mgrClient .Get (context .TODO (), mdb .NamespacedName (), & sts )
139+
140+ // the request is requeued as the agents are still doing the upgrade
141+ res , err = r .Reconcile (reconcile.Request {NamespacedName : types.NamespacedName {Namespace : mdb .Namespace , Name : mdb .Name }})
142+ assert .NoError (t , err )
143+ assert .Equal (t , res .RequeueAfter , time .Second * 10 )
144+
145+ _ = mgrClient .Get (context .TODO (), mdb .NamespacedName (), & sts )
146+ assert .Equal (t , appsv1 .OnDeleteStatefulSetStrategyType , sts .Spec .UpdateStrategy .Type )
147+ // upgrade is now complete
148+ sts .Status .UpdatedReplicas = 3
149+ sts .Status .ReadyReplicas = 3
150+ err = mgrClient .Update (context .TODO (), & sts )
151+
152+ // reconcilliation is successful
130153 res , err = r .Reconcile (reconcile.Request {NamespacedName : types.NamespacedName {Namespace : mdb .Namespace , Name : mdb .Name }})
131154 assertReconciliationSuccessful (t , res , err )
132155
@@ -142,23 +165,23 @@ func TestBuildStatefulSet_ConfiguresUpdateStrategyCorrectly(t *testing.T) {
142165 t .Run ("On No Version Change, Same Version" , func (t * testing.T ) {
143166 mdb := newTestReplicaSet ()
144167 mdb .Spec .Version = "4.0.0"
145- mdb .Annotations [mdbv1 . LastVersionAnnotationKey ] = "4.0.0"
168+ mdb .Annotations [lastVersionAnnotationKey ] = "4.0.0"
146169 sts , err := buildStatefulSet (mdb )
147170 assert .NoError (t , err )
148171 assert .Equal (t , appsv1 .RollingUpdateStatefulSetStrategyType , sts .Spec .UpdateStrategy .Type )
149172 })
150173 t .Run ("On No Version Change, First Version" , func (t * testing.T ) {
151174 mdb := newTestReplicaSet ()
152175 mdb .Spec .Version = "4.0.0"
153- delete (mdb .Annotations , mdbv1 . LastVersionAnnotationKey )
176+ delete (mdb .Annotations , lastVersionAnnotationKey )
154177 sts , err := buildStatefulSet (mdb )
155178 assert .NoError (t , err )
156179 assert .Equal (t , appsv1 .RollingUpdateStatefulSetStrategyType , sts .Spec .UpdateStrategy .Type )
157180 })
158181 t .Run ("On Version Change" , func (t * testing.T ) {
159182 mdb := newTestReplicaSet ()
160183 mdb .Spec .Version = "4.0.0"
161- mdb .Annotations [mdbv1 . LastVersionAnnotationKey ] = "4.2.0"
184+ mdb .Annotations [lastVersionAnnotationKey ] = "4.2.0"
162185 sts , err := buildStatefulSet (mdb )
163186 assert .NoError (t , err )
164187 assert .Equal (t , appsv1 .OnDeleteStatefulSetStrategyType , sts .Spec .UpdateStrategy .Type )
@@ -198,6 +221,7 @@ func TestAutomationConfig_versionIsBumpedOnChange(t *testing.T) {
198221 assert .Equal (t , 1 , currentAc .Version )
199222
200223 mdb .Spec .Members += 1
224+ makeStatefulSetReady (mgr .GetClient (), mdb )
201225
202226 _ = mgr .GetClient ().Update (context .TODO (), & mdb )
203227 res , err = r .Reconcile (reconcile.Request {NamespacedName : types.NamespacedName {Namespace : mdb .Namespace , Name : mdb .Name }})
@@ -228,3 +252,13 @@ func TestAutomationConfig_versionIsNotBumpedWithNoChanges(t *testing.T) {
228252 assert .NoError (t , err )
229253 assert .Equal (t , currentAc .Version , 1 )
230254}
255+
256+ // makeStatefulSetReady updates the StatefulSet corresponding to the
257+ // provided MongoDB resource to mark it as ready for the case of `statefulset.IsReady`
258+ func makeStatefulSetReady (c k8sClient.Client , mdb mdbv1.MongoDB ) {
259+ sts := appsv1.StatefulSet {}
260+ _ = c .Get (context .TODO (), mdb .NamespacedName (), & sts )
261+ sts .Status .ReadyReplicas = int32 (mdb .Spec .Members )
262+ sts .Status .UpdatedReplicas = int32 (mdb .Spec .Members )
263+ _ = c .Update (context .TODO (), & sts )
264+ }
0 commit comments