11package construct
22
33import (
4- "os"
54 "reflect"
65 "testing"
76
@@ -21,10 +20,6 @@ import (
2120 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2221)
2322
24- func init () {
25- os .Setenv (VersionUpgradeHookImageEnv , "version-upgrade-hook-image" )
26- }
27-
2823func newTestReplicaSet () mdbv1.MongoDBCommunity {
2924 return mdbv1.MongoDBCommunity {
3025 ObjectMeta : metav1.ObjectMeta {
@@ -40,12 +35,8 @@ func newTestReplicaSet() mdbv1.MongoDBCommunity {
4035}
4136
4237func TestMultipleCalls_DoNotCauseSideEffects (t * testing.T ) {
43- t .Setenv (MongodbRepoUrl , "docker.io/mongodb" )
44- t .Setenv (MongodbImageEnv , "mongodb-community-server" )
45- t .Setenv (AgentImageEnv , "agent-image" )
46-
4738 mdb := newTestReplicaSet ()
48- stsFunc := BuildMongoDBReplicaSetStatefulSetModificationFunction (& mdb , & mdb , os . Getenv ( AgentImageEnv ) , true )
39+ stsFunc := BuildMongoDBReplicaSetStatefulSetModificationFunction (& mdb , & mdb , "fake-mongodbImage" , "fake-agentImage" , "fake-versionUpgradeHookImage" , "fake-readinessProbeImage" , true )
4940 sts := & appsv1.StatefulSet {}
5041
5142 t .Run ("1st Call" , func (t * testing.T ) {
@@ -63,103 +54,20 @@ func TestMultipleCalls_DoNotCauseSideEffects(t *testing.T) {
6354}
6455
6556func TestManagedSecurityContext (t * testing.T ) {
66- t .Setenv (MongodbRepoUrl , "docker.io/mongodb" )
67- t .Setenv (MongodbImageEnv , "mongodb-community-server" )
68- t .Setenv (AgentImageEnv , "agent-image" )
6957 t .Setenv (podtemplatespec .ManagedSecurityContextEnv , "true" )
7058
7159 mdb := newTestReplicaSet ()
72- stsFunc := BuildMongoDBReplicaSetStatefulSetModificationFunction (& mdb , & mdb , os . Getenv ( AgentImageEnv ) , true )
60+ stsFunc := BuildMongoDBReplicaSetStatefulSetModificationFunction (& mdb , & mdb , "fake-mongodbImage" , "fake-agentImage" , "fake-versionUpgradeHookImage" , "fake-readinessProbeImage" , true )
7361
7462 sts := & appsv1.StatefulSet {}
7563 stsFunc (sts )
7664
7765 assertStatefulSetIsBuiltCorrectly (t , mdb , sts )
7866}
7967
80- func TestGetMongoDBImage (t * testing.T ) {
81- type testConfig struct {
82- setArgs func (t * testing.T )
83- version string
84- expectedImage string
85- }
86- tests := map [string ]testConfig {
87- "Default UBI8 Community image" : {
88- setArgs : func (t * testing.T ) {
89- t .Setenv (MongodbRepoUrl , "docker.io/mongodb" )
90- t .Setenv (MongodbImageEnv , "mongodb-community-server" )
91- },
92- version : "6.0.5" ,
93- expectedImage : "docker.io/mongodb/mongodb-community-server:6.0.5-ubi8" ,
94- },
95- "Overridden UBI8 Enterprise image" : {
96- setArgs : func (t * testing.T ) {
97- t .Setenv (MongodbRepoUrl , "docker.io/mongodb" )
98- t .Setenv (MongodbImageEnv , "mongodb-enterprise-server" )
99- },
100- version : "6.0.5" ,
101- expectedImage : "docker.io/mongodb/mongodb-enterprise-server:6.0.5-ubi8" ,
102- },
103- "Overridden UBI8 Enterprise image from Quay" : {
104- setArgs : func (t * testing.T ) {
105- t .Setenv (MongodbRepoUrl , "quay.io/mongodb" )
106- t .Setenv (MongodbImageEnv , "mongodb-enterprise-server" )
107- },
108- version : "6.0.5" ,
109- expectedImage : "quay.io/mongodb/mongodb-enterprise-server:6.0.5-ubi8" ,
110- },
111- "Overridden Ubuntu Community image" : {
112- setArgs : func (t * testing.T ) {
113- t .Setenv (MongodbRepoUrl , "docker.io/mongodb" )
114- t .Setenv (MongodbImageEnv , "mongodb-community-server" )
115- t .Setenv (MongoDBImageType , "ubuntu2204" )
116- },
117- version : "6.0.5" ,
118- expectedImage : "docker.io/mongodb/mongodb-community-server:6.0.5-ubuntu2204" ,
119- },
120- "Overridden UBI Community image" : {
121- setArgs : func (t * testing.T ) {
122- t .Setenv (MongodbRepoUrl , "docker.io/mongodb" )
123- t .Setenv (MongodbImageEnv , "mongodb-community-server" )
124- t .Setenv (MongoDBImageType , "ubi8" )
125- },
126- version : "6.0.5" ,
127- expectedImage : "docker.io/mongodb/mongodb-community-server:6.0.5-ubi8" ,
128- },
129- "Docker Inc images" : {
130- setArgs : func (t * testing.T ) {
131- t .Setenv (MongodbRepoUrl , "docker.io" )
132- t .Setenv (MongodbImageEnv , "mongo" )
133- },
134- version : "6.0.5" ,
135- expectedImage : "docker.io/mongo:6.0.5" ,
136- },
137- "Deprecated AppDB images defined the old way" : {
138- setArgs : func (t * testing.T ) {
139- t .Setenv (MongodbRepoUrl , "quay.io" )
140- t .Setenv (MongodbImageEnv , "mongodb/mongodb-enterprise-appdb-database-ubi" )
141- // In this example, we intentionally don't use the suffix from the env. variable and let users
142- // define it in the version instead. There are some known customers who do this.
143- // This is a backwards compatibility case.
144- t .Setenv (MongoDBImageType , "will-be-ignored" )
145- },
146-
147- version : "5.0.14-ent" ,
148- expectedImage : "quay.io/mongodb/mongodb-enterprise-appdb-database-ubi:5.0.14-ent" ,
149- },
150- }
151- for testName := range tests {
152- t .Run (testName , func (t * testing.T ) {
153- testConfig := tests [testName ]
154- testConfig .setArgs (t )
155- image := getMongoDBImage (testConfig .version )
156- assert .Equal (t , testConfig .expectedImage , image )
157- })
158- }
159- }
160-
16168func TestMongod_Container (t * testing.T ) {
162- c := container .New (mongodbContainer ("4.2" , []corev1.VolumeMount {}, mdbv1 .NewMongodConfiguration ()))
69+ const mongodbImageMock = "fake-mongodbImage"
70+ c := container .New (mongodbContainer (mongodbImageMock , []corev1.VolumeMount {}, mdbv1 .NewMongodConfiguration ()))
16371
16472 t .Run ("Has correct Env vars" , func (t * testing.T ) {
16573 assert .Len (t , c .Env , 1 )
@@ -168,7 +76,7 @@ func TestMongod_Container(t *testing.T) {
16876 })
16977
17078 t .Run ("Image is correct" , func (t * testing.T ) {
171- assert .Equal (t , getMongoDBImage ( "4.2" ) , c .Image )
79+ assert .Equal (t , mongodbImageMock , c .Image )
17280 })
17381
17482 t .Run ("Resource requirements are correct" , func (t * testing.T ) {
@@ -210,7 +118,7 @@ func assertStatefulSetIsBuiltCorrectly(t *testing.T, mdb mdbv1.MongoDBCommunity,
210118 }
211119
212120 agentContainer := sts .Spec .Template .Spec .Containers [0 ]
213- assert .Equal (t , "agent-image " , agentContainer .Image )
121+ assert .Equal (t , "fake-agentImage " , agentContainer .Image )
214122 probe := agentContainer .ReadinessProbe
215123 assert .True (t , reflect .DeepEqual (probes .New (DefaultReadiness ()), * probe ))
216124 assert .Equal (t , probes .New (DefaultReadiness ()).FailureThreshold , probe .FailureThreshold )
@@ -231,7 +139,7 @@ func assertStatefulSetIsBuiltCorrectly(t *testing.T, mdb mdbv1.MongoDBCommunity,
231139 assertContainsVolumeMountWithName (t , agentContainer .VolumeMounts , "my-rs-keyfile" )
232140
233141 mongodContainer := sts .Spec .Template .Spec .Containers [1 ]
234- assert .Equal (t , "docker.io/mongodb/mongodb-community-server:6.0.5-ubi8 " , mongodContainer .Image )
142+ assert .Equal (t , "fake-mongodbImage " , mongodContainer .Image )
235143 assert .Len (t , mongodContainer .VolumeMounts , 6 )
236144 if ! managedSecurityContext {
237145 assert .NotNil (t , sts .Spec .Template .Spec .Containers [1 ].SecurityContext )
@@ -248,7 +156,7 @@ func assertStatefulSetIsBuiltCorrectly(t *testing.T, mdb mdbv1.MongoDBCommunity,
248156
249157 initContainer := sts .Spec .Template .Spec .InitContainers [0 ]
250158 assert .Equal (t , versionUpgradeHookName , initContainer .Name )
251- assert .Equal (t , "version-upgrade-hook-image " , initContainer .Image )
159+ assert .Equal (t , "fake-versionUpgradeHookImage " , initContainer .Image )
252160 assert .Len (t , initContainer .VolumeMounts , 1 )
253161 if ! managedSecurityContext {
254162 assert .NotNil (t , sts .Spec .Template .Spec .InitContainers [0 ].SecurityContext )
0 commit comments