-
Notifications
You must be signed in to change notification settings - Fork 10
/
config_test.go
207 lines (201 loc) · 4.88 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
package main
// nolint: lll
import (
"net"
"testing"
"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 TestIPFilterConfig(t *testing.T) {
testCases := []struct {
name string
setup func()
assertions func(http.IPFilterConfig, error)
}{
{
name: "ALLOWED_CLIENT_IPS not defined",
assertions: func(config http.IPFilterConfig, err error) {
require.NoError(t, err)
require.Equal(
t,
http.IPFilterConfig{
AllowedRanges: []net.IPNet{},
},
config,
)
},
},
{
name: "ALLOWED_CLIENT_IPS defined",
setup: func() {
t.Setenv("ALLOWED_CLIENT_IPS", "192.168.1.0/24,0.0.0.0/0")
},
assertions: func(config http.IPFilterConfig, err error) {
require.NoError(t, err)
require.Len(t, config.AllowedRanges, 2)
require.Equal(t, "192.168.1.0/24", config.AllowedRanges[0].String())
require.Equal(t, "0.0.0.0/0", config.AllowedRanges[1].String())
},
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
if testCase.setup != nil {
testCase.setup()
}
testCase.assertions(ipFilterConfig())
})
}
}
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)
})
}
}