-
Notifications
You must be signed in to change notification settings - Fork 17
/
config_test.go
306 lines (237 loc) · 11.2 KB
/
config_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
Copyright (C) 2018 Expedia Group.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"github.com/stretchr/testify/assert"
"testing"
)
type envVars map[string]string
func (e envVars) lookupEnv(key string) (string, bool) {
val, contains := e[key]
return val, contains
}
func newflyteEnvVars() envVars {
return envVars{
portEnvName: "80",
tlsCertPathEnvName: "/path/to/tls/cert",
tlsKeyPathEnvName: "/path/to/tls/key",
mgoHostEnvName: "mongo:27017",
authPolicyPathEnvName: "/path/to/authpolicy",
oidcIssuerURLName: "dex:5559",
oidcIssuerClientIDName: "example-app",
flyteTTLEnvName: "86400",
shouldDeleteDeadPacksEnvName: "false",
deleteDeadPacksTimeEnvName: "10:00",
packGracePeriodUntilDeadInSecondsEnvName: "500000",
}
}
func TestConfigShouldReadFlyteEnvVars(t *testing.T) {
defer func(oldFileExists func(string) bool) { fileExists = oldFileExists }(fileExists)
fileExists = func(string) bool { return true }
defer func(oldGetEnv func(string) (string, bool)) { lookupEnv = oldGetEnv }(lookupEnv)
lookupEnv = newflyteEnvVars().lookupEnv
c := NewConfig()
assert.Equal(t, "80", c.Port)
assert.Equal(t, "/path/to/tls/cert", c.TLSCertPath)
assert.Equal(t, "/path/to/tls/key", c.TLSKeyPath)
assert.Equal(t, "mongo:27017", c.MongoHost)
assert.Equal(t, "/path/to/authpolicy", c.AuthPolicyPath)
assert.Equal(t, "dex:5559", c.OidcIssuerURL)
assert.Equal(t, "example-app", c.OidcIssuerClientID)
assert.Equal(t, 86400, c.FlyteTTL)
assert.Equal(t, false, c.ShouldDeleteDeadPacks)
assert.Equal(t, "10:00", c.DeleteDeadPacksTime)
assert.Equal(t, 500000, c.PackGracePeriodUntilDeadInSeconds)
}
func TestConfigShouldDefaultMongoHostIfNotSetAsEnvVar(t *testing.T) {
defer func(oldFileExists func(string) bool) { fileExists = oldFileExists }(fileExists)
fileExists = func(string) bool { return true }
// remove mongo host from env vars
flyteEnvVars := newflyteEnvVars()
delete(flyteEnvVars, mgoHostEnvName)
defer func(oldGetEnv func(string) (string, bool)) { lookupEnv = oldGetEnv }(lookupEnv)
lookupEnv = flyteEnvVars.lookupEnv
c := NewConfig()
assert.Equal(t, "localhost:27017", c.MongoHost)
}
func TestConfigShouldDefaultPortBasedOnTLSEnabled(t *testing.T) {
defer func(oldFileExists func(string) bool) { fileExists = oldFileExists }(fileExists)
fileExists = func(string) bool { return true }
// remove port from env vars
flyteEnvVars := newflyteEnvVars()
delete(flyteEnvVars, portEnvName)
defer func(oldGetEnv func(string) (string, bool)) { lookupEnv = oldGetEnv }(lookupEnv)
lookupEnv = flyteEnvVars.lookupEnv
c := NewConfig()
// secure port default
assert.Equal(t, "8443", c.Port)
}
func TestConfigShouldDefaultPortBasedOnTLSDisabled(t *testing.T) {
defer func(oldFileExists func(string) bool) { fileExists = oldFileExists }(fileExists)
fileExists = func(string) bool { return true }
// remove port from env vars & tls vars (turns off tls)
flyteEnvVars := newflyteEnvVars()
delete(flyteEnvVars, portEnvName)
delete(flyteEnvVars, tlsCertPathEnvName)
delete(flyteEnvVars, tlsKeyPathEnvName)
defer func(oldGetEnv func(string) (string, bool)) { lookupEnv = oldGetEnv }(lookupEnv)
lookupEnv = flyteEnvVars.lookupEnv
c := NewConfig()
// standard http port default
assert.Equal(t, "8080", c.Port)
}
func TestConfigShouldSetDefaultTTL(t *testing.T) {
defer func(oldFileExists func(string) bool) { fileExists = oldFileExists }(fileExists)
fileExists = func(string) bool { return true }
// remove default flyte data ttl from env vars
flyteEnvVars := newflyteEnvVars()
delete(flyteEnvVars, flyteTTLEnvName)
defer func(oldGetEnv func(string) (string, bool)) { lookupEnv = oldGetEnv }(lookupEnv)
lookupEnv = flyteEnvVars.lookupEnv
c := NewConfig()
// default flyte data ttl
assert.Equal(t, oneYearInSeconds, c.FlyteTTL)
}
func TestConfigShouldSetDefaultTTLAndLogOnStringToIntConversionError(t *testing.T) {
defer func(oldFileExists func(string) bool) { fileExists = oldFileExists }(fileExists)
fileExists = func(string) bool { return true }
flyteEnvVars := newflyteEnvVars()
flyteEnvVars[flyteTTLEnvName] = "this-string-cannot-be-converted-to-int!!!"
defer func(oldGetEnv func(string) (string, bool)) { lookupEnv = oldGetEnv }(lookupEnv)
lookupEnv = flyteEnvVars.lookupEnv
c := NewConfig()
// default flyte data ttl
assert.Equal(t, 31557600, c.FlyteTTL)
}
func TestConfigShouldEnableTLSOnlyIfBothKeyAndCertProvided(t *testing.T) {
defer func(oldFileExists func(string) bool) { fileExists = oldFileExists }(fileExists)
fileExists = func(string) bool { return true }
// remove tls key env var
flyteEnvVars := newflyteEnvVars()
delete(flyteEnvVars, tlsKeyPathEnvName)
defer func(oldGetEnv func(string) (string, bool)) { lookupEnv = oldGetEnv }(lookupEnv)
lookupEnv = flyteEnvVars.lookupEnv
c := NewConfig()
assert.False(t, c.requireTLS())
}
func TestConfigShouldEnableAuthIfOIDCIssuerIsProvidedAndPolicyPathIsProvided(t *testing.T) {
defer func(oldFileExists func(string) bool) { fileExists = oldFileExists }(fileExists)
fileExists = func(string) bool { return true }
// policy path, oidc issuer uri/config id is set
flyteEnvVars := newflyteEnvVars()
defer func(oldGetEnv func(string) (string, bool)) { lookupEnv = oldGetEnv }(lookupEnv)
lookupEnv = flyteEnvVars.lookupEnv
c := NewConfig()
assert.True(t, c.requireAuth())
}
func TestConfigShouldNotEnableAuthIfPolicyPathNotProvided(t *testing.T) {
defer func(oldFileExists func(string) bool) { fileExists = oldFileExists }(fileExists)
fileExists = func(string) bool { return true }
// remove policy path env var
flyteEnvVars := newflyteEnvVars()
delete(flyteEnvVars, authPolicyPathEnvName)
defer func(oldGetEnv func(string) (string, bool)) { lookupEnv = oldGetEnv }(lookupEnv)
lookupEnv = flyteEnvVars.lookupEnv
c := NewConfig()
assert.False(t, c.requireAuth())
}
func TestConfigShouldNotEnableAuthIfOIDCIssuerUriNotProvided(t *testing.T) {
defer func(oldFileExists func(string) bool) { fileExists = oldFileExists }(fileExists)
fileExists = func(string) bool { return true }
// remove oidc issuer uri env var
flyteEnvVars := newflyteEnvVars()
delete(flyteEnvVars, oidcIssuerURLName)
defer func(oldGetEnv func(string) (string, bool)) { lookupEnv = oldGetEnv }(lookupEnv)
lookupEnv = flyteEnvVars.lookupEnv
c := NewConfig()
assert.False(t, c.requireAuth())
}
func TestConfigShouldNotEnableAuthIfOIDCIssuerConfigIdNotProvided(t *testing.T) {
defer func(oldFileExists func(string) bool) { fileExists = oldFileExists }(fileExists)
fileExists = func(string) bool { return true }
// remove oidc issuer config id env var
flyteEnvVars := newflyteEnvVars()
delete(flyteEnvVars, oidcIssuerClientIDName)
defer func(oldGetEnv func(string) (string, bool)) { lookupEnv = oldGetEnv }(lookupEnv)
lookupEnv = flyteEnvVars.lookupEnv
c := NewConfig()
assert.False(t, c.requireAuth())
}
func TestConfigShouldSetDefaultForShouldDeleteDeadPacksIfNotSetAsEnvVar(t *testing.T) {
defer func(oldFileExists func(string) bool) { fileExists = oldFileExists }(fileExists)
fileExists = func(string) bool { return true }
// remove shouldDeleteDeadPacksEnvName from env vars
flyteEnvVars := newflyteEnvVars()
delete(flyteEnvVars, shouldDeleteDeadPacksEnvName)
defer func(oldGetEnv func(string) (string, bool)) { lookupEnv = oldGetEnv }(lookupEnv)
lookupEnv = flyteEnvVars.lookupEnv
c := NewConfig()
assert.Equal(t, false, c.ShouldDeleteDeadPacks)
}
func TestConfigShouldSetDefaultAndLogErrorForShouldDeleteDeadPacksIfEnvVarValueSetIsNotBoolean(t *testing.T) {
defer func(oldFileExists func(string) bool) { fileExists = oldFileExists }(fileExists)
fileExists = func(string) bool { return true }
// change shouldDeleteDeadPacksEnvName in env vars to non boolean value
flyteEnvVars := newflyteEnvVars()
flyteEnvVars[shouldDeleteDeadPacksEnvName] = "6372gyd"
defer func(oldGetEnv func(string) (string, bool)) { lookupEnv = oldGetEnv }(lookupEnv)
lookupEnv = flyteEnvVars.lookupEnv
c := NewConfig()
assert.Equal(t, false, c.ShouldDeleteDeadPacks)
}
func TestConfigShouldSetDefaultDeleteDeadPacksTimeIfNotSetAsEnvVar(t *testing.T) {
defer func(oldFileExists func(string) bool) { fileExists = oldFileExists }(fileExists)
fileExists = func(string) bool { return true }
// remove deleteDeadPacksTimeEnvName from env vars
flyteEnvVars := newflyteEnvVars()
delete(flyteEnvVars, deleteDeadPacksTimeEnvName)
defer func(oldGetEnv func(string) (string, bool)) { lookupEnv = oldGetEnv }(lookupEnv)
lookupEnv = flyteEnvVars.lookupEnv
c := NewConfig()
assert.Equal(t, defaultDeleteDeadPacksTime, c.DeleteDeadPacksTime)
}
var invalidTimes = []struct {
invalidValue string
errorMsg string
}{
{"1100", "FLYTE_DELETE_DEAD_PACKS_AT_HH_COLON_MM env is invalid, using default 23:00, error: parsing time \"1100\" as \"15:04\": cannot parse \"00\" as \":\""},
{"AA:00", "FLYTE_DELETE_DEAD_PACKS_AT_HH_COLON_MM env is invalid, using default 23:00, error: parsing time \"AA:00\" as \"15:04\": cannot parse \"AA:00\" as \"15\""},
{"23:AA", "FLYTE_DELETE_DEAD_PACKS_AT_HH_COLON_MM env is invalid, using default 23:00, error: parsing time \"23:AA\" as \"15:04\": cannot parse \"AA\" as \"04\""},
{"24:00", "FLYTE_DELETE_DEAD_PACKS_AT_HH_COLON_MM env is invalid, using default 23:00, error: parsing time \"24:00\": hour out of range"},
{"23:60", "FLYTE_DELETE_DEAD_PACKS_AT_HH_COLON_MM env is invalid, using default 23:00, error: parsing time \"23:60\": minute out of range"},
}
func TestConfigShouldSetDefaultDeleteDeadPacksTimeAndLogErrorIfTimeIsInInvalidFormat(t *testing.T) {
defer func(oldFileExists func(string) bool) { fileExists = oldFileExists }(fileExists)
fileExists = func(string) bool { return true }
defer func(oldGetEnv func(string) (string, bool)) { lookupEnv = oldGetEnv }(lookupEnv)
for _, tt := range invalidTimes {
// change deleteDeadPacksTimeEnvName in env vars
flyteEnvVars := newflyteEnvVars()
flyteEnvVars[deleteDeadPacksTimeEnvName] = tt.invalidValue
lookupEnv = flyteEnvVars.lookupEnv
c := NewConfig()
assert.Equal(t, defaultDeleteDeadPacksTime, c.DeleteDeadPacksTime)
}
}
func TestConfigShouldSetDefaultPackGracePeriodUntilDeadInSeconds(t *testing.T) {
defer func(oldFileExists func(string) bool) { fileExists = oldFileExists }(fileExists)
fileExists = func(string) bool { return true }
// remove default flyte pack grace period from env vars
flyteEnvVars := newflyteEnvVars()
delete(flyteEnvVars, packGracePeriodUntilDeadInSecondsEnvName)
defer func(oldGetEnv func(string) (string, bool)) { lookupEnv = oldGetEnv }(lookupEnv)
lookupEnv = flyteEnvVars.lookupEnv
c := NewConfig()
// default flyte pack grace period in seconds
assert.Equal(t, oneWeekInSeconds, c.PackGracePeriodUntilDeadInSeconds)
}