@@ -2,11 +2,17 @@ package mongodb
22
33import (
44 "context"
5+ "encoding/json"
6+ "fmt"
7+ "io/ioutil"
58 "os"
69 "reflect"
710 "testing"
811 "time"
912
13+ "k8s.io/apimachinery/pkg/runtime"
14+ "sigs.k8s.io/yaml"
15+
1016 "github.com/pkg/errors"
1117
1218 "github.com/stretchr/objx"
@@ -414,6 +420,76 @@ func AssertReplicaSetIsConfiguredWithScram(t *testing.T, mdb mdbv1.MongoDB) {
414420 })
415421}
416422
423+ func TestOpenshift_Configuration (t * testing.T ) {
424+ sts := performReconciliationAndGetStatefulSet (t , "openshift_mdb.yaml" )
425+ assert .Equal (t , "MANAGED_SECURITY_CONTEXT" , sts .Spec .Template .Spec .Containers [0 ].Env [1 ].Name )
426+ assert .Equal (t , "MANAGED_SECURITY_CONTEXT" , sts .Spec .Template .Spec .Containers [1 ].Env [1 ].Name )
427+ }
428+
429+ func TestVolumeClaimTemplates_Configuration (t * testing.T ) {
430+ sts := performReconciliationAndGetStatefulSet (t , "volume_claim_templates_mdb.yaml" )
431+
432+ assert .Len (t , sts .Spec .VolumeClaimTemplates , 2 )
433+
434+ pvcSpec := sts .Spec .VolumeClaimTemplates [1 ].Spec
435+
436+ storage := pvcSpec .Resources .Requests [corev1 .ResourceStorage ]
437+ storageRef := & storage
438+
439+ assert .Equal (t , "1Gi" , storageRef .String ())
440+ assert .Len (t , pvcSpec .AccessModes , 1 )
441+ assert .Contains (t , pvcSpec .AccessModes , corev1 .ReadWriteOnce )
442+ }
443+
444+ func TestChangeDataVolume_Configuration (t * testing.T ) {
445+ sts := performReconciliationAndGetStatefulSet (t , "change_data_volume.yaml" )
446+ assert .Len (t , sts .Spec .VolumeClaimTemplates , 1 )
447+
448+ dataVolume := sts .Spec .VolumeClaimTemplates [0 ]
449+
450+ storage := dataVolume .Spec .Resources .Requests [corev1 .ResourceStorage ]
451+ storageRef := & storage
452+
453+ assert .Equal (t , "data-volume" , dataVolume .Name )
454+ assert .Equal (t , "50Gi" , storageRef .String ())
455+ }
456+
457+ func performReconciliationAndGetStatefulSet (t * testing.T , filePath string ) appsv1.StatefulSet {
458+ mdb , err := loadTestFixture (filePath )
459+ assert .NoError (t , err )
460+ mgr := client .NewManager (& mdb )
461+ assert .NoError (t , generatePasswordsForAllUsers (mdb , mgr .Client ))
462+ r := newReconciler (mgr , mockManifestProvider (mdb .Spec .Version ))
463+ res , err := r .Reconcile (reconcile.Request {NamespacedName : mdb .NamespacedName ()})
464+ assertReconciliationSuccessful (t , res , err )
465+
466+ sts , err := mgr .Client .GetStatefulSet (mdb .NamespacedName ())
467+ assert .NoError (t , err )
468+ return sts
469+ }
470+
471+ func generatePasswordsForAllUsers (mdb mdbv1.MongoDB , c client.Client ) error {
472+ for _ , user := range mdb .Spec .Users {
473+
474+ key := "password"
475+ if user .PasswordSecretRef .Key != "" {
476+ key = user .PasswordSecretRef .Key
477+ }
478+
479+ passwordSecret := secret .Builder ().
480+ SetName (user .PasswordSecretRef .Name ).
481+ SetNamespace (mdb .Namespace ).
482+ SetField (key , "GAGTQK2ccRRaxJFudI5y" ).
483+ Build ()
484+
485+ if err := c .CreateSecret (passwordSecret ); err != nil {
486+ return err
487+ }
488+ }
489+
490+ return nil
491+ }
492+
417493func assertReconciliationSuccessful (t * testing.T , result reconcile.Result , err error ) {
418494 assert .NoError (t , err )
419495 assert .Equal (t , false , result .Requeue )
@@ -431,3 +507,29 @@ func makeStatefulSetReady(t *testing.T, c k8sClient.Client, mdb mdbv1.MongoDB) {
431507 err = c .Update (context .TODO (), & sts )
432508 assert .NoError (t , err )
433509}
510+
511+ // loadTestFixture will create a MongoDB resource from a given fixture
512+ func loadTestFixture (yamlFileName string ) (mdbv1.MongoDB , error ) {
513+ testPath := fmt .Sprintf ("testdata/%s" , yamlFileName )
514+ mdb := mdbv1.MongoDB {}
515+ data , err := ioutil .ReadFile (testPath )
516+ if err != nil {
517+ return mdb , errors .Errorf ("error reading file: %s" , err )
518+ }
519+
520+ if err := marshalRuntimeObjectFromYAMLBytes (data , & mdb ); err != nil {
521+ return mdb , errors .Errorf ("error converting yaml bytes to service account: %s" , err )
522+ }
523+
524+ return mdb , nil
525+ }
526+
527+ // marshalRuntimeObjectFromYAMLBytes accepts the bytes of a yaml resource
528+ // and unmarshals them into the provided runtime Object
529+ func marshalRuntimeObjectFromYAMLBytes (bytes []byte , obj runtime.Object ) error {
530+ jsonBytes , err := yaml .YAMLToJSON (bytes )
531+ if err != nil {
532+ return err
533+ }
534+ return json .Unmarshal (jsonBytes , & obj )
535+ }
0 commit comments