66 "testing"
77 "time"
88
9+ "k8s.io/apimachinery/pkg/types"
10+
911 "k8s.io/apimachinery/pkg/util/wait"
1012
1113 mdbv1 "github.com/mongodb/mongodb-kubernetes-operator/pkg/apis/mongodb/v1"
@@ -32,6 +34,17 @@ func StatefulSetIsReady(mdb *mdbv1.MongoDB) func(t *testing.T) {
3234 }
3335}
3436
37+ // MongoDBReachesRunningPhase ensure the MongoDB resource reaches the Running phase
38+ func MongoDBReachesRunningPhase (mdb * mdbv1.MongoDB ) func (t * testing.T ) {
39+ return func (t * testing.T ) {
40+ err := e2eutil .WaitForMongoDBToReachPhase (t , mdb , mdbv1 .Running , time .Second * 15 , time .Minute * 5 )
41+ if err != nil {
42+ t .Fatal (err )
43+ }
44+ t .Logf ("MongoDB %s/%s is Running!" , mdb .Namespace , mdb .Name )
45+ }
46+ }
47+
3548func AutomationConfigConfigMapExists (mdb * mdbv1.MongoDB ) func (t * testing.T ) {
3649 return func (t * testing.T ) {
3750 cm , err := e2eutil .WaitForConfigMapToExist (mdb .ConfigMapName (), time .Second * 5 , time .Minute * 1 )
@@ -44,10 +57,10 @@ func AutomationConfigConfigMapExists(mdb *mdbv1.MongoDB) func(t *testing.T) {
4457 }
4558}
4659
47- // CreateOrUpdateResource creates the MongoDB resource if it doesn't exist, or updates it otherwise
48- func CreateOrUpdateResource (mdb * mdbv1.MongoDB , ctx * f.TestCtx ) func (* testing.T ) {
60+ // CreateMongoDBResource creates the MongoDB resource
61+ func CreateMongoDBResource (mdb * mdbv1.MongoDB , ctx * f.TestCtx ) func (* testing.T ) {
4962 return func (t * testing.T ) {
50- if err := e2eutil . CreateOrUpdateMongoDB ( mdb , ctx ); err != nil {
63+ if err := f . Global . Client . Create ( context . TODO (), mdb , & f. CleanupOptions { TestContext : ctx } ); err != nil {
5164 t .Fatal (err )
5265 }
5366 t .Logf ("Created MongoDB resource %s/%s" , mdb .Name , mdb .Namespace )
@@ -78,7 +91,29 @@ func BasicConnectivity(mdb *mdbv1.MongoDB) func(t *testing.T) {
7891 if err := Connect (mdb ); err != nil {
7992 t .Fatal (fmt .Sprintf ("Error connecting to MongoDB deployment: %+v" , err ))
8093 }
81- t .Logf ("successfully connected to MongoDB deployment" )
94+ }
95+ }
96+
97+ // Status compares the given status to the actual status of the MongoDB resource
98+ func Status (mdb * mdbv1.MongoDB , expectedStatus mdbv1.MongoDBStatus ) func (t * testing.T ) {
99+ return func (t * testing.T ) {
100+ if err := f .Global .Client .Get (context .TODO (), types.NamespacedName {Name : mdb .Name , Namespace : mdb .Namespace }, mdb ); err != nil {
101+ t .Fatal (fmt .Errorf ("error getting MongoDB resource: %+v" , err ))
102+ }
103+ assert .Equal (t , expectedStatus , mdb .Status )
104+ }
105+ }
106+
107+ // Scale update the MongoDB with a new number of members and updates the resource
108+ func Scale (mdb * mdbv1.MongoDB , newMembers int ) func (* testing.T ) {
109+ return func (t * testing.T ) {
110+ t .Logf ("Scaling Mongodb %s, to %d members" , mdb .Name , newMembers )
111+ err := e2eutil .UpdateMongoDBResource (mdb , func (db * mdbv1.MongoDB ) {
112+ db .Spec .Members = newMembers
113+ })
114+ if err != nil {
115+ t .Fatal (err )
116+ }
82117 }
83118}
84119
@@ -91,7 +126,7 @@ func Connect(mdb *mdbv1.MongoDB) error {
91126 return err
92127 }
93128
94- return wait .Poll (time .Second * 5 , time .Minute * 2 , func () (done bool , err error ) {
129+ return wait .Poll (time .Second * 1 , time .Second * 30 , func () (done bool , err error ) {
95130 collection := mongoClient .Database ("testing" ).Collection ("numbers" )
96131 _ , err = collection .InsertOne (ctx , bson.M {"name" : "pi" , "value" : 3.14159 })
97132 if err != nil {
@@ -101,17 +136,6 @@ func Connect(mdb *mdbv1.MongoDB) error {
101136 })
102137}
103138
104- // Scale update the MongoDB with a new number of members and updates the resource
105- func Scale (mdb * mdbv1.MongoDB , newMembers int , ctx * f.TestCtx ) func (* testing.T ) {
106- return func (t * testing.T ) {
107- mdb .Spec .Members = newMembers
108- t .Logf ("Scaling Mongodb %s, to %d members" , mdb .Name , mdb .Spec .Members )
109- if err := e2eutil .CreateOrUpdateMongoDB (mdb , ctx ); err != nil {
110- t .Fatal (err )
111- }
112- }
113- }
114-
115139// IsReachableDuring periodically tests connectivity to the provided MongoDB resource
116140// during execution of the provided functions. This function can be used to ensure
117141// The MongoDB is up throughout the test.
0 commit comments