4
4
"context"
5
5
"encoding/json"
6
6
"fmt"
7
+ "sigs.k8s.io/controller-runtime/pkg/client"
8
+ "strings"
7
9
"testing"
8
10
"time"
9
11
@@ -217,6 +219,7 @@ func containsVolume(volumes []corev1.PersistentVolume, volumeName string) bool {
217
219
}
218
220
return false
219
221
}
222
+
220
223
func HasExpectedPersistentVolumes (volumes []corev1.PersistentVolume ) func (t * testing.T ) {
221
224
return func (t * testing.T ) {
222
225
volumeList , err := getPersistentVolumesList ()
@@ -230,6 +233,77 @@ func HasExpectedPersistentVolumes(volumes []corev1.PersistentVolume) func(t *tes
230
233
}
231
234
}
232
235
}
236
+ func HasExpectedMetadata (mdb * mdbv1.MongoDBCommunity , expectedLabels map [string ]string , expectedAnnotations map [string ]string ) func (t * testing.T ) {
237
+ return func (t * testing.T ) {
238
+ namespace := mdb .Namespace
239
+
240
+ statefulSetList := appsv1.StatefulSetList {}
241
+ err := e2eutil .TestClient .Client .List (context .TODO (), & statefulSetList , client .InNamespace (namespace ))
242
+ assert .NoError (t , err )
243
+ assert .NotEmpty (t , statefulSetList .Items )
244
+ for _ , s := range statefulSetList .Items {
245
+ containsMetadata (t , & s .ObjectMeta , expectedLabels , expectedAnnotations , "statefulset " + s .Name )
246
+ }
247
+
248
+ volumeList := corev1.PersistentVolumeList {}
249
+ err = e2eutil .TestClient .Client .List (context .TODO (), & volumeList , client .InNamespace (namespace ))
250
+ assert .NoError (t , err )
251
+ assert .NotEmpty (t , volumeList .Items )
252
+ for _ , s := range volumeList .Items {
253
+ volName := s .Name
254
+ if strings .HasPrefix (volName , "data-volume-" ) || strings .HasPrefix (volName , "logs-volume-" ) {
255
+ containsMetadata (t , & s .ObjectMeta , expectedLabels , expectedAnnotations , "volume " + volName )
256
+ }
257
+ }
258
+
259
+ podList := corev1.PodList {}
260
+ err = e2eutil .TestClient .Client .List (context .TODO (), & podList , client .InNamespace (namespace ))
261
+ assert .NoError (t , err )
262
+ assert .NotEmpty (t , podList .Items )
263
+
264
+ for _ , s := range podList .Items {
265
+ // only consider stateful-sets (as opposite to the controller replica set)
266
+ for _ , owner := range s .OwnerReferences {
267
+ if owner .Kind == "ReplicaSet" {
268
+ continue
269
+ }
270
+ }
271
+ // Ignore non-owned pods
272
+ if len (s .OwnerReferences ) == 0 {
273
+ continue
274
+ }
275
+
276
+ // Ensure we are considering pods owned by a stateful set
277
+ hasStatefulSetOwner := false
278
+ for _ , owner := range s .OwnerReferences {
279
+ if owner .Kind == "StatefulSet" {
280
+ hasStatefulSetOwner = true
281
+ }
282
+ }
283
+ if ! hasStatefulSetOwner {
284
+ continue
285
+ }
286
+
287
+ containsMetadata (t , & s .ObjectMeta , expectedLabels , expectedAnnotations , "pod " + s .Name )
288
+ }
289
+ }
290
+ }
291
+
292
+ func containsMetadata (t * testing.T , metadata * metav1.ObjectMeta , expectedLabels map [string ]string , expectedAnnotations map [string ]string , msg string ) {
293
+ labels := metadata .Labels
294
+ for k , v := range expectedLabels {
295
+ assert .Contains (t , labels , k , msg + " has label " + k )
296
+ value := labels [k ]
297
+ assert .Equal (t , v , value , msg + " has label " + k + " with value " + v )
298
+ }
299
+
300
+ annotations := metadata .Annotations
301
+ for k , v := range expectedAnnotations {
302
+ assert .Contains (t , annotations , k , msg + " has annotation " + k )
303
+ value := annotations [k ]
304
+ assert .Equal (t , v , value , msg + " has annotation " + k + " with value " + v )
305
+ }
306
+ }
233
307
234
308
// MongoDBReachesPendingPhase ensures the MongoDB resources gets to the Pending phase
235
309
func MongoDBReachesPendingPhase (mdb * mdbv1.MongoDBCommunity ) func (t * testing.T ) {
0 commit comments