@@ -22,17 +22,17 @@ import (
2222
2323// StatefulSetIsReady ensures that the underlying stateful set
2424// reaches the running state
25- func StatefulSetIsReady (mdb mdbv1.MongoDB ) func (t * testing.T ) {
25+ func StatefulSetIsReady (mdb * mdbv1.MongoDB ) func (t * testing.T ) {
2626 return func (t * testing.T ) {
27- err := e2eutil .WaitForStatefulSetToBeReady (t , mdb . Name , time .Second * 15 , time .Minute * 5 )
27+ err := e2eutil .WaitForStatefulSetToBeReady (t , mdb , time .Second * 15 , time .Minute * 5 )
2828 if err != nil {
2929 t .Fatal (err )
3030 }
3131 t .Logf ("StatefulSet %s/%s is ready!" , mdb .Namespace , mdb .Name )
3232 }
3333}
3434
35- func AutomationConfigConfigMapExists (mdb mdbv1.MongoDB ) func (t * testing.T ) {
35+ func AutomationConfigConfigMapExists (mdb * mdbv1.MongoDB ) func (t * testing.T ) {
3636 return func (t * testing.T ) {
3737 cm , err := e2eutil .WaitForConfigMapToExist (mdb .ConfigMapName (), time .Second * 5 , time .Minute * 1 )
3838 assert .NoError (t , err )
@@ -44,15 +44,18 @@ func AutomationConfigConfigMapExists(mdb mdbv1.MongoDB) func(t *testing.T) {
4444 }
4545}
4646
47- func CreateResource (mdb mdbv1.MongoDB , ctx * f.TestCtx ) func (* testing.T ) {
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 ) {
4849 return func (t * testing.T ) {
49- err := e2eutil .CreateRuntimeObject (& mdb , ctx )
50- assert .NoError (t , err )
50+ if err := e2eutil .CreateOrUpdateMongoDB (mdb , ctx ); err != nil {
51+ t .Fatal (err )
52+ }
5153 t .Logf ("Created MongoDB resource %s/%s" , mdb .Name , mdb .Namespace )
5254 }
5355}
5456
55- func DeletePod (mdb mdbv1.MongoDB , podNum int ) func (* testing.T ) {
57+ // DeletePod will delete a pod that belongs to this MongoDB resource's StatefulSet
58+ func DeletePod (mdb * mdbv1.MongoDB , podNum int ) func (* testing.T ) {
5659 return func (t * testing.T ) {
5760 pod := corev1.Pod {
5861 ObjectMeta : metav1.ObjectMeta {
@@ -68,32 +71,72 @@ func DeletePod(mdb mdbv1.MongoDB, podNum int) func(*testing.T) {
6871 }
6972}
7073
71- // BasicConnectivity performs a check by initializing a mongo client
72- // and inserting a document into the MongoDB resource
73- func BasicConnectivity (mdb mdbv1.MongoDB ) func (t * testing.T ) {
74+ // BasicConnectivity returns a test function which performs
75+ // a basic MongoDB connectivity test
76+ func BasicConnectivity (mdb * mdbv1.MongoDB ) func (t * testing.T ) {
7477 return func (t * testing.T ) {
75- ctx , _ := context .WithTimeout (context .Background (), 10 * time .Minute )
76- mongoClient , err := mongo .Connect (ctx , options .Client ().ApplyURI (mdb .MongoURI ()))
77- if err != nil {
78- t .Fatal (err )
78+ if err := Connect (mdb ); err != nil {
79+ t .Fatal (fmt .Sprintf ("Error connecting to MongoDB deployment: %+v" , err ))
7980 }
81+ t .Logf ("successfully connected to MongoDB deployment" )
82+ }
83+ }
8084
81- t .Logf ("Created mongo client!" )
82-
83- var res * mongo.InsertOneResult
84- err = wait .Poll (time .Second * 5 , time .Minute * 1 , func () (done bool , err error ) {
85- collection := mongoClient .Database ("testing" ).Collection ("numbers" )
86- res , err = collection .InsertOne (ctx , bson.M {"name" : "pi" , "value" : 3.14159 })
87- if err != nil {
88- t .Logf ("error inserting document: %+v" , err )
89- return false , err
90- }
91- return true , nil
92- })
85+ // Connect performs a connectivity check by initializing a mongo client
86+ // and inserting a document into the MongoDB resource
87+ func Connect (mdb * mdbv1.MongoDB ) error {
88+ ctx , _ := context .WithTimeout (context .Background (), 10 * time .Minute )
89+ mongoClient , err := mongo .Connect (ctx , options .Client ().ApplyURI (mdb .MongoURI ()))
90+ if err != nil {
91+ return err
92+ }
9393
94+ return wait .Poll (time .Second * 5 , time .Minute * 2 , func () (done bool , err error ) {
95+ collection := mongoClient .Database ("testing" ).Collection ("numbers" )
96+ _ , err = collection .InsertOne (ctx , bson.M {"name" : "pi" , "value" : 3.14159 })
9497 if err != nil {
98+ return false , nil
99+ }
100+ return true , nil
101+ })
102+ }
103+
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 {
95110 t .Fatal (err )
96111 }
97- t .Logf ("inserted ID: %+v" , res .InsertedID )
112+ }
113+ }
114+
115+ // IsReachableDuring periodically tests connectivity to the provided MongoDB resource
116+ // during execution of the provided functions. This function can be used to ensure
117+ // The MongoDB is up throughout the test.
118+ func IsReachableDuring (mdb * mdbv1.MongoDB , interval time.Duration , testFunc func ()) func (* testing.T ) {
119+ return func (t * testing.T ) {
120+ ctx , cancelFunc := context .WithCancel (context .Background ())
121+ defer cancelFunc ()
122+
123+ // start a go routine which will periodically check basic MongoDB connectivity
124+ // once all the test functions have been executed, the go routine will be cancelled
125+ go func () {
126+ for {
127+ select {
128+ case <- ctx .Done ():
129+ t .Logf ("context cancelled, no longer checking connectivity" )
130+ return
131+ case <- time .After (interval ):
132+ if err := Connect (mdb ); err != nil {
133+ t .Fatal (fmt .Sprintf ("error reaching MongoDB deployment: %+v" , err ))
134+ } else {
135+ t .Logf ("Successfully connected to %s" , mdb .Name )
136+ }
137+ }
138+ }
139+ }()
140+ testFunc ()
98141 }
99142}
0 commit comments