1
1
package construct
2
2
3
3
import (
4
- "os"
5
4
"reflect"
6
5
"testing"
7
6
@@ -21,10 +20,6 @@ import (
21
20
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
22
21
)
23
22
24
- func init () {
25
- os .Setenv (VersionUpgradeHookImageEnv , "version-upgrade-hook-image" )
26
- }
27
-
28
23
func newTestReplicaSet () mdbv1.MongoDBCommunity {
29
24
return mdbv1.MongoDBCommunity {
30
25
ObjectMeta : metav1.ObjectMeta {
@@ -40,12 +35,8 @@ func newTestReplicaSet() mdbv1.MongoDBCommunity {
40
35
}
41
36
42
37
func 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
-
47
38
mdb := newTestReplicaSet ()
48
- stsFunc := BuildMongoDBReplicaSetStatefulSetModificationFunction (& mdb , & mdb , os . Getenv ( AgentImageEnv ) , true )
39
+ stsFunc := BuildMongoDBReplicaSetStatefulSetModificationFunction (& mdb , & mdb , "docker.io/mongodb" , "mongodb-community-server" , "ubi8" , "agent-image" , "version-upgrade-hook-image" , "fake-readinessProbeImage" , true )
49
40
sts := & appsv1.StatefulSet {}
50
41
51
42
t .Run ("1st Call" , func (t * testing.T ) {
@@ -63,13 +54,10 @@ func TestMultipleCalls_DoNotCauseSideEffects(t *testing.T) {
63
54
}
64
55
65
56
func TestManagedSecurityContext (t * testing.T ) {
66
- t .Setenv (MongodbRepoUrl , "docker.io/mongodb" )
67
- t .Setenv (MongodbImageEnv , "mongodb-community-server" )
68
- t .Setenv (AgentImageEnv , "agent-image" )
69
57
t .Setenv (podtemplatespec .ManagedSecurityContextEnv , "true" )
70
58
71
59
mdb := newTestReplicaSet ()
72
- stsFunc := BuildMongoDBReplicaSetStatefulSetModificationFunction (& mdb , & mdb , os . Getenv ( AgentImageEnv ) , true )
60
+ stsFunc := BuildMongoDBReplicaSetStatefulSetModificationFunction (& mdb , & mdb , "docker.io/mongodb" , "mongodb-community-server" , "ubi8" , "agent-image" , "version-upgrade-hook-image" , "fake-readinessProbeImage" , true )
73
61
74
62
sts := & appsv1.StatefulSet {}
75
63
stsFunc (sts )
@@ -79,87 +67,77 @@ func TestManagedSecurityContext(t *testing.T) {
79
67
80
68
func TestGetMongoDBImage (t * testing.T ) {
81
69
type testConfig struct {
82
- setArgs func (t * testing.T )
83
- version string
84
- expectedImage string
70
+ mongodbRepoUrl string
71
+ mongodbImage string
72
+ mongodbImageType string
73
+ version string
74
+ expectedImage string
85
75
}
86
76
tests := map [string ]testConfig {
87
77
"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" ,
78
+ mongodbRepoUrl : "docker.io/mongodb" ,
79
+ mongodbImage : "mongodb-community-server" ,
80
+ mongodbImageType : "ubi8" ,
81
+ version : "6.0.5" ,
82
+ expectedImage : "docker.io/mongodb/mongodb-community-server:6.0.5-ubi8" ,
94
83
},
95
84
"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" ,
85
+ mongodbRepoUrl : "docker.io/mongodb" ,
86
+ mongodbImage : "mongodb-enterprise-server" ,
87
+ mongodbImageType : "ubi8" ,
88
+ version : "6.0.5" ,
89
+ expectedImage : "docker.io/mongodb/mongodb-enterprise-server:6.0.5-ubi8" ,
102
90
},
103
91
"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" ,
92
+ mongodbRepoUrl : "quay.io/mongodb" ,
93
+ mongodbImage : "mongodb-enterprise-server" ,
94
+ mongodbImageType : "ubi8" ,
95
+ version : "6.0.5" ,
96
+ expectedImage : "quay.io/mongodb/mongodb-enterprise-server:6.0.5-ubi8" ,
110
97
},
111
98
"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" ,
99
+ mongodbRepoUrl : "docker.io/mongodb" ,
100
+ mongodbImage : "mongodb-community-server" ,
101
+ mongodbImageType : "ubuntu2204" ,
102
+ version : "6.0.5" ,
103
+ expectedImage : "docker.io/mongodb/mongodb-community-server:6.0.5-ubuntu2204" ,
119
104
},
120
105
"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" ,
106
+ mongodbRepoUrl : "docker.io/mongodb" ,
107
+ mongodbImage : "mongodb-community-server" ,
108
+ mongodbImageType : "ubi8" ,
109
+ version : "6.0.5" ,
110
+ expectedImage : "docker.io/mongodb/mongodb-community-server:6.0.5-ubi8" ,
128
111
},
129
112
"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" ,
113
+ mongodbRepoUrl : "docker.io" ,
114
+ mongodbImage : "mongo" ,
115
+ mongodbImageType : "ubi8" ,
116
+ version : "6.0.5" ,
117
+ expectedImage : "docker.io/mongo:6.0.5" ,
136
118
},
137
119
"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" ,
120
+ mongodbRepoUrl : "quay.io" ,
121
+ mongodbImage : "mongodb/mongodb-enterprise-appdb-database-ubi" ,
122
+ // In this example, we intentionally don't use the suffix from the env. variable and let users
123
+ // define it in the version instead. There are some known customers who do this.
124
+ // This is a backwards compatibility case.
125
+ mongodbImageType : "will-be-ignored" ,
126
+ version : "5.0.14-ent" ,
127
+ expectedImage : "quay.io/mongodb/mongodb-enterprise-appdb-database-ubi:5.0.14-ent" ,
149
128
},
150
129
}
151
130
for testName := range tests {
152
131
t .Run (testName , func (t * testing.T ) {
153
132
testConfig := tests [testName ]
154
- testConfig .setArgs (t )
155
- image := getMongoDBImage (testConfig .version )
133
+ image := getMongoDBImage (testConfig .mongodbRepoUrl , testConfig .mongodbImage , testConfig .mongodbImageType , testConfig .version )
156
134
assert .Equal (t , testConfig .expectedImage , image )
157
135
})
158
136
}
159
137
}
160
138
161
139
func TestMongod_Container (t * testing.T ) {
162
- c := container .New (mongodbContainer ("4.2" , []corev1.VolumeMount {}, mdbv1 .NewMongodConfiguration ()))
140
+ c := container .New (mongodbContainer ("fake-repo-url" , "fake-image" , "ubi8" , " 4.2" , []corev1.VolumeMount {}, mdbv1 .NewMongodConfiguration ()))
163
141
164
142
t .Run ("Has correct Env vars" , func (t * testing.T ) {
165
143
assert .Len (t , c .Env , 1 )
@@ -168,7 +146,7 @@ func TestMongod_Container(t *testing.T) {
168
146
})
169
147
170
148
t .Run ("Image is correct" , func (t * testing.T ) {
171
- assert .Equal (t , getMongoDBImage ("4.2" ), c .Image )
149
+ assert .Equal (t , getMongoDBImage ("fake-repo-url" , "fake-image" , "ubi8" , " 4.2" ), c .Image )
172
150
})
173
151
174
152
t .Run ("Resource requirements are correct" , func (t * testing.T ) {
0 commit comments