44 "context"
55 "encoding/json"
66 "fmt"
7+ "sigs.k8s.io/controller-runtime/pkg/client"
8+ "strings"
79 "testing"
810 "time"
911
@@ -217,6 +219,7 @@ func containsVolume(volumes []corev1.PersistentVolume, volumeName string) bool {
217219 }
218220 return false
219221}
222+
220223func HasExpectedPersistentVolumes (volumes []corev1.PersistentVolume ) func (t * testing.T ) {
221224 return func (t * testing.T ) {
222225 volumeList , err := getPersistentVolumesList ()
@@ -230,6 +233,77 @@ func HasExpectedPersistentVolumes(volumes []corev1.PersistentVolume) func(t *tes
230233 }
231234 }
232235}
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+ }
233307
234308// MongoDBReachesPendingPhase ensures the MongoDB resources gets to the Pending phase
235309func MongoDBReachesPendingPhase (mdb * mdbv1.MongoDBCommunity ) func (t * testing.T ) {
0 commit comments