-
Notifications
You must be signed in to change notification settings - Fork 2
/
config_test.go
237 lines (231 loc) · 5.78 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
package main
// nolint: lll
import (
"io/ioutil"
"testing"
"github.com/brigadecore/brigade-dockerhub-gateway/internal/webhooks"
"github.com/brigadecore/brigade-foundations/http"
"github.com/brigadecore/brigade/sdk/v3/restmachinery"
"github.com/stretchr/testify/require"
)
// Note that unit testing in Go does NOT clear environment variables between
// tests, which can sometimes be a pain, but it's fine here-- so each of these
// test functions uses a series of test cases that cumulatively build upon one
// another.
func TestAPIClientConfig(t *testing.T) {
testCases := []struct {
name string
setup func()
assertions func(
address string,
token string,
opts restmachinery.APIClientOptions,
err error,
)
}{
{
name: "API_ADDRESS not set",
setup: func() {},
assertions: func(
_ string,
_ string,
_ restmachinery.APIClientOptions,
err error,
) {
require.Error(t, err)
require.Contains(t, err.Error(), "value not found for")
require.Contains(t, err.Error(), "API_ADDRESS")
},
},
{
name: "API_TOKEN not set",
setup: func() {
t.Setenv("API_ADDRESS", "foo")
},
assertions: func(
_ string,
_ string,
_ restmachinery.APIClientOptions,
err error,
) {
require.Error(t, err)
require.Contains(t, err.Error(), "value not found for")
require.Contains(t, err.Error(), "API_TOKEN")
},
},
{
name: "success",
setup: func() {
t.Setenv("API_TOKEN", "bar")
t.Setenv("API_IGNORE_CERT_WARNINGS", "true")
},
assertions: func(
address string,
token string,
opts restmachinery.APIClientOptions,
err error,
) {
require.NoError(t, err)
require.Equal(t, "foo", address)
require.Equal(t, "bar", token)
require.True(t, opts.AllowInsecureConnections)
},
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
testCase.setup()
address, token, opts, err := apiClientConfig()
testCase.assertions(address, token, opts, err)
})
}
}
func TestTokenFilterConfig(t *testing.T) {
testCases := []struct {
name string
setup func()
assertions func(webhooks.TokenFilterConfig, error)
}{
{
name: "TOKENS_PATH not set",
assertions: func(_ webhooks.TokenFilterConfig, err error) {
require.Error(t, err)
require.Contains(t, err.Error(), "value not found for")
require.Contains(t, err.Error(), "TOKENS_PATH")
},
},
{
name: "TOKENS_PATH path does not exist",
setup: func() {
t.Setenv("TOKENS_PATH", "/completely/bogus/path")
},
assertions: func(_ webhooks.TokenFilterConfig, err error) {
require.Error(t, err)
require.Contains(
t,
err.Error(),
"file /completely/bogus/path does not exist",
)
},
},
{
name: "TOKENS_PATH does not contain valid json",
setup: func() {
tokensFile, err := ioutil.TempFile("", "tokens.json")
require.NoError(t, err)
defer tokensFile.Close()
_, err = tokensFile.Write([]byte("this is not json"))
require.NoError(t, err)
t.Setenv("TOKENS_PATH", tokensFile.Name())
},
assertions: func(_ webhooks.TokenFilterConfig, err error) {
require.Error(t, err)
require.Contains(
t, err.Error(), "invalid character",
)
},
},
{
name: "success",
setup: func() {
tokensFile, err := ioutil.TempFile("", "tokens.json")
require.NoError(t, err)
defer tokensFile.Close()
_, err = tokensFile.Write([]byte(`{"foo": "bar"}`))
require.NoError(t, err)
t.Setenv("TOKENS_PATH", tokensFile.Name())
},
assertions: func(config webhooks.TokenFilterConfig, err error) {
require.NoError(t, err)
},
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
if testCase.setup != nil {
testCase.setup()
}
config, err := tokenFilterConfig()
testCase.assertions(config, err)
})
}
}
func TestServerConfig(t *testing.T) {
testCases := []struct {
name string
setup func()
assertions func(http.ServerConfig, error)
}{
{
name: "PORT not an int",
setup: func() {
t.Setenv("PORT", "foo")
},
assertions: func(_ http.ServerConfig, err error) {
require.Error(t, err)
require.Contains(t, err.Error(), "was not parsable as an int")
require.Contains(t, err.Error(), "PORT")
},
},
{
name: "TLS_ENABLED not a bool",
setup: func() {
t.Setenv("PORT", "8080")
t.Setenv("TLS_ENABLED", "nope")
},
assertions: func(_ http.ServerConfig, err error) {
require.Error(t, err)
require.Contains(t, err.Error(), "was not parsable as a bool")
require.Contains(t, err.Error(), "TLS_ENABLED")
},
},
{
name: "TLS_CERT_PATH required but not set",
setup: func() {
t.Setenv("TLS_ENABLED", "true")
},
assertions: func(_ http.ServerConfig, err error) {
require.Error(t, err)
require.Contains(t, err.Error(), "value not found for")
require.Contains(t, err.Error(), "TLS_CERT_PATH")
},
},
{
name: "TLS_KEY_PATH required but not set",
setup: func() {
t.Setenv("TLS_CERT_PATH", "/var/ssl/cert")
},
assertions: func(_ http.ServerConfig, err error) {
require.Error(t, err)
require.Contains(t, err.Error(), "value not found for")
require.Contains(t, err.Error(), "TLS_KEY_PATH")
},
},
{
name: "success",
setup: func() {
t.Setenv("TLS_KEY_PATH", "/var/ssl/key")
},
assertions: func(config http.ServerConfig, err error) {
require.NoError(t, err)
require.Equal(
t,
http.ServerConfig{
Port: 8080,
TLSEnabled: true,
TLSCertPath: "/var/ssl/cert",
TLSKeyPath: "/var/ssl/key",
},
config,
)
},
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
testCase.setup()
config, err := serverConfig()
testCase.assertions(config, err)
})
}
}