@@ -4,23 +4,15 @@ import (
44 "context"
55 "fmt"
66 "reflect"
7- "testing"
8- "time"
97
108 "github.com/mongodb/mongodb-kubernetes-operator/pkg/util/envvar"
119
12- "github.com/pkg/errors"
13-
1410 mdbv1 "github.com/mongodb/mongodb-kubernetes-operator/api/v1"
15- "github.com/mongodb/mongodb-kubernetes-operator/pkg/kube/statefulset"
16- appsv1 "k8s.io/api/apps/v1"
1711 corev1 "k8s.io/api/core/v1"
1812 rbacv1 "k8s.io/api/rbac/v1"
1913 apiErrors "k8s.io/apimachinery/pkg/api/errors"
2014 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2115 "k8s.io/apimachinery/pkg/types"
22- "k8s.io/apimachinery/pkg/util/wait"
23- "sigs.k8s.io/controller-runtime/pkg/client"
2416 k8sClient "sigs.k8s.io/controller-runtime/pkg/client"
2517)
2618
@@ -47,127 +39,6 @@ func UpdateMongoDBResource(original *mdbv1.MongoDBCommunity, updateFunc func(*md
4739 return TestClient .Update (context .TODO (), original )
4840}
4941
50- // WaitForConfigMapToExist waits until a ConfigMap of the given name exists
51- // using the provided retryInterval and timeout
52- func WaitForConfigMapToExist (cmName string , retryInterval , timeout time.Duration ) (corev1.ConfigMap , error ) {
53- cm := corev1.ConfigMap {}
54- return cm , waitForRuntimeObjectToExist (cmName , retryInterval , timeout , & cm , OperatorNamespace )
55- }
56-
57- // WaitForSecretToExist waits until a Secret of the given name exists
58- // using the provided retryInterval and timeout
59- func WaitForSecretToExist (cmName string , retryInterval , timeout time.Duration , namespace string ) (corev1.Secret , error ) {
60- s := corev1.Secret {}
61- return s , waitForRuntimeObjectToExist (cmName , retryInterval , timeout , & s , namespace )
62- }
63-
64- // WaitForMongoDBToReachPhase waits until the given MongoDB resource reaches the expected phase
65- func WaitForMongoDBToReachPhase (t * testing.T , mdb * mdbv1.MongoDBCommunity , phase mdbv1.Phase , retryInterval , timeout time.Duration ) error {
66- return waitForMongoDBCondition (mdb , retryInterval , timeout , func (db mdbv1.MongoDBCommunity ) bool {
67- t .Logf ("current phase: %s, waiting for phase: %s" , db .Status .Phase , phase )
68- return db .Status .Phase == phase
69- })
70- }
71-
72- // waitForMongoDBCondition polls and waits for a given condition to be true
73- func waitForMongoDBCondition (mdb * mdbv1.MongoDBCommunity , retryInterval , timeout time.Duration , condition func (mdbv1.MongoDBCommunity ) bool ) error {
74- mdbNew := mdbv1.MongoDBCommunity {}
75- return wait .Poll (retryInterval , timeout , func () (done bool , err error ) {
76- err = TestClient .Get (context .TODO (), mdb .NamespacedName (), & mdbNew )
77- if err != nil {
78- return false , err
79- }
80- ready := condition (mdbNew )
81- return ready , nil
82- })
83- }
84-
85- // WaitForStatefulSetToExist waits until a StatefulSet of the given name exists
86- // using the provided retryInterval and timeout
87- func WaitForStatefulSetToExist (stsName string , retryInterval , timeout time.Duration , namespace string ) (appsv1.StatefulSet , error ) {
88- sts := appsv1.StatefulSet {}
89- return sts , waitForRuntimeObjectToExist (stsName , retryInterval , timeout , & sts , namespace )
90- }
91-
92- // WaitForStatefulSetToHaveUpdateStrategy waits until all replicas of the StatefulSet with the given name
93- // have reached the ready status
94- func WaitForStatefulSetToHaveUpdateStrategy (t * testing.T , mdb * mdbv1.MongoDBCommunity , strategy appsv1.StatefulSetUpdateStrategyType , retryInterval , timeout time.Duration ) error {
95- return waitForStatefulSetCondition (t , mdb , retryInterval , timeout , func (sts appsv1.StatefulSet ) bool {
96- return sts .Spec .UpdateStrategy .Type == strategy
97- })
98- }
99-
100- // WaitForStatefulSetToBeReady waits until all replicas of the StatefulSet with the given name
101- // have reached the ready status
102- func WaitForStatefulSetToBeReady (t * testing.T , mdb * mdbv1.MongoDBCommunity , retryInterval , timeout time.Duration ) error {
103- return waitForStatefulSetCondition (t , mdb , retryInterval , timeout , func (sts appsv1.StatefulSet ) bool {
104- return statefulset .IsReady (sts , mdb .Spec .Members )
105- })
106- }
107-
108- // waitForStatefulSetCondition waits until all replicas of the StatefulSet with the given name
109- // is not ready.
110- func WaitForStatefulSetToBeUnready (t * testing.T , mdb * mdbv1.MongoDBCommunity , retryInterval , timeout time.Duration ) error {
111- return waitForStatefulSetCondition (t , mdb , retryInterval , timeout , func (sts appsv1.StatefulSet ) bool {
112- return ! statefulset .IsReady (sts , mdb .Spec .Members )
113- })
114- }
115-
116- // WaitForStatefulSetToBeReadyAfterScaleDown waits for just the ready replicas to be correct
117- // and does not account for the updated replicas
118- func WaitForStatefulSetToBeReadyAfterScaleDown (t * testing.T , mdb * mdbv1.MongoDBCommunity , retryInterval , timeout time.Duration ) error {
119- return waitForStatefulSetCondition (t , mdb , retryInterval , timeout , func (sts appsv1.StatefulSet ) bool {
120- return int32 (mdb .Spec .Members ) == sts .Status .ReadyReplicas
121- })
122- }
123-
124- func waitForStatefulSetCondition (t * testing.T , mdb * mdbv1.MongoDBCommunity , retryInterval , timeout time.Duration , condition func (set appsv1.StatefulSet ) bool ) error {
125- _ , err := WaitForStatefulSetToExist (mdb .Name , retryInterval , timeout , mdb .Namespace )
126- if err != nil {
127- return errors .Errorf ("error waiting for stateful set to be created: %s" , err )
128- }
129-
130- sts := appsv1.StatefulSet {}
131- return wait .Poll (retryInterval , timeout , func () (done bool , err error ) {
132- err = TestClient .Get (context .TODO (), mdb .NamespacedName (), & sts )
133- if err != nil {
134- return false , err
135- }
136- t .Logf ("Waiting for %s to have %d replicas. Current ready replicas: %d, Current updated replicas: %d, Current generation: %d, Observed Generation: %d\n " ,
137- mdb .Name , mdb .Spec .Members , sts .Status .ReadyReplicas , sts .Status .UpdatedReplicas , sts .Generation , sts .Status .ObservedGeneration )
138- ready := condition (sts )
139- return ready , nil
140- })
141- }
142-
143- func WaitForPodReadiness (t * testing.T , isReady bool , containerName string , timeout time.Duration , pod corev1.Pod ) error {
144- return wait .Poll (time .Second * 3 , timeout , func () (done bool , err error ) {
145- err = TestClient .Get (context .TODO (), types.NamespacedName {Name : pod .Name , Namespace : pod .Namespace }, & pod )
146- if err != nil {
147- return false , err
148- }
149- for _ , status := range pod .Status .ContainerStatuses {
150- t .Logf ("%s (%s), ready: %v\n " , pod .Name , status .Name , status .Ready )
151- if status .Name == containerName && status .Ready == isReady {
152- return true , nil
153- }
154- }
155- return false , nil
156- })
157- }
158-
159- // waitForRuntimeObjectToExist waits until a runtime.Object of the given name exists
160- // using the provided retryInterval and timeout provided.
161- func waitForRuntimeObjectToExist (name string , retryInterval , timeout time.Duration , obj client.Object , namespace string ) error {
162- return wait .Poll (retryInterval , timeout , func () (done bool , err error ) {
163- err = TestClient .Get (context .TODO (), types.NamespacedName {Name : name , Namespace : namespace }, obj )
164- if err != nil {
165- return false , client .IgnoreNotFound (err )
166- }
167- return true , nil
168- })
169- }
170-
17142func NewTestMongoDB (ctx * Context , name string , namespace string ) (mdbv1.MongoDBCommunity , mdbv1.MongoDBUser ) {
17243 mongodbNamespace := namespace
17344 if mongodbNamespace == "" {
0 commit comments