forked from zalando/skipper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskipper_test.go
312 lines (260 loc) · 7.95 KB
/
skipper_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
307
308
309
310
311
312
package skipper
import (
"crypto/tls"
"io"
"net"
"net/http"
"os"
"syscall"
"testing"
"time"
log "github.com/sirupsen/logrus"
"github.com/zalando/skipper/dataclients/routestring"
"github.com/zalando/skipper/filters"
"github.com/zalando/skipper/filters/builtin"
"github.com/zalando/skipper/proxy"
"github.com/zalando/skipper/ratelimit"
"github.com/zalando/skipper/routing"
"github.com/stretchr/testify/require"
)
const (
listenDelay = 15 * time.Millisecond
listenTimeout = 9 * listenDelay
)
func testListener() bool {
for _, a := range os.Args {
if a == "listener" {
return true
}
}
return false
}
func waitConn(req func() (*http.Response, error)) (*http.Response, error) {
to := time.After(listenTimeout)
for {
rsp, err := req()
if err == nil {
return rsp, nil
}
select {
case <-to:
return nil, err
default:
time.Sleep(listenDelay)
}
}
}
func waitConnGet(url string) (*http.Response, error) {
return waitConn(func() (*http.Response, error) {
return (&http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true}}}).Get(url)
})
}
func findAddress() (string, error) {
l, err := net.ListenTCP("tcp6", &net.TCPAddr{})
if err != nil {
return "", err
}
defer l.Close()
return l.Addr().String(), nil
}
func TestOptionsTLSConfig(t *testing.T) {
cert, err := tls.LoadX509KeyPair("fixtures/test.crt", "fixtures/test.key")
require.NoError(t, err)
cert2, err := tls.LoadX509KeyPair("fixtures/test2.crt", "fixtures/test2.key")
require.NoError(t, err)
// empty
o := &Options{}
c, err := o.tlsConfig()
require.NoError(t, err)
require.Nil(t, c)
// proxy tls config
o = &Options{ProxyTLS: &tls.Config{}}
c, err = o.tlsConfig()
require.NoError(t, err)
require.Equal(t, &tls.Config{}, c)
// proxy tls config priority
o = &Options{ProxyTLS: &tls.Config{}, CertPathTLS: "fixtures/test.crt", KeyPathTLS: "fixtures/test.key"}
c, err = o.tlsConfig()
require.NoError(t, err)
require.Equal(t, &tls.Config{}, c)
// cert key path
o = &Options{TLSMinVersion: tls.VersionTLS12, CertPathTLS: "fixtures/test.crt", KeyPathTLS: "fixtures/test.key"}
c, err = o.tlsConfig()
require.NoError(t, err)
require.Equal(t, uint16(tls.VersionTLS12), c.MinVersion)
require.Equal(t, []tls.Certificate{cert}, c.Certificates)
// multiple cert key paths
o = &Options{TLSMinVersion: tls.VersionTLS13, CertPathTLS: "fixtures/test.crt,fixtures/test2.crt", KeyPathTLS: "fixtures/test.key,fixtures/test2.key"}
c, err = o.tlsConfig()
require.NoError(t, err)
require.Equal(t, uint16(tls.VersionTLS13), c.MinVersion)
require.Equal(t, []tls.Certificate{cert, cert2}, c.Certificates)
}
func TestOptionsTLSConfigInvalidPaths(t *testing.T) {
for _, tt := range []struct {
name string
options *Options
}{
{"missing cert path", &Options{KeyPathTLS: "fixtures/test.key"}},
{"missing key path", &Options{CertPathTLS: "fixtures/test.crt"}},
{"wrong cert path", &Options{CertPathTLS: "fixtures/notFound.crt", KeyPathTLS: "fixtures/test.key"}},
{"wrong key path", &Options{CertPathTLS: "fixtures/test.crt", KeyPathTLS: "fixtures/notFound.key"}},
{"cert key mismatch", &Options{CertPathTLS: "fixtures/test.crt", KeyPathTLS: "fixtures/test2.key"}},
{"multiple cert key count mismatch", &Options{CertPathTLS: "fixtures/test.crt,fixtures/test2.crt", KeyPathTLS: "fixtures/test.key"}},
{"multiple cert key mismatch", &Options{CertPathTLS: "fixtures/test.crt,fixtures/test2.crt", KeyPathTLS: "fixtures/test2.key,fixtures/test.key"}},
} {
t.Run(tt.name, func(t *testing.T) {
_, err := tt.options.tlsConfig()
t.Logf("tlsConfig error: %v", err)
require.Error(t, err)
})
}
}
// to run this test, set `-args listener` for the test command
func TestHTTPSServer(t *testing.T) {
// TODO: figure why sometimes cannot connect
if !testListener() {
t.Skip()
}
a, err := findAddress()
if err != nil {
t.Fatal(err)
}
o := Options{
Address: a,
CertPathTLS: "fixtures/test.crt",
KeyPathTLS: "fixtures/test.key",
}
rt := routing.New(routing.Options{
FilterRegistry: builtin.MakeRegistry(),
DataClients: []routing.DataClient{}})
defer rt.Close()
proxy := proxy.New(rt, proxy.OptionsNone)
defer proxy.Close()
go listenAndServe(proxy, &o)
r, err := waitConnGet("https://" + o.Address)
if r != nil {
defer r.Body.Close()
}
if err != nil {
t.Fatalf("Cannot connect to the local server for testing: %s ", err.Error())
}
if r.StatusCode != 404 {
t.Fatalf("Status code should be 404, instead got: %d\n", r.StatusCode)
}
_, err = io.ReadAll(r.Body)
if err != nil {
t.Fatalf("Failed to stream response body: %v", err)
}
}
// to run this test, set `-args listener` for the test command
func TestHTTPServer(t *testing.T) {
// TODO: figure why sometimes cannot connect
if !testListener() {
t.Skip()
}
a, err := findAddress()
if err != nil {
t.Fatal(err)
}
o := Options{Address: a}
rt := routing.New(routing.Options{
FilterRegistry: builtin.MakeRegistry(),
DataClients: []routing.DataClient{}})
defer rt.Close()
proxy := proxy.New(rt, proxy.OptionsNone)
defer proxy.Close()
go listenAndServe(proxy, &o)
r, err := waitConnGet("http://" + o.Address)
if r != nil {
defer r.Body.Close()
}
if err != nil {
t.Fatalf("Cannot connect to the local server for testing: %s ", err.Error())
}
if r.StatusCode != 404 {
t.Fatalf("Status code should be 404, instead got: %d\n", r.StatusCode)
}
_, err = io.ReadAll(r.Body)
if err != nil {
t.Fatalf("Failed to stream response body: %v", err)
}
}
func TestHTTPServerShutdown(t *testing.T) {
o := &Options{}
testServerShutdown(t, o, "http")
}
func TestHTTPSServerShutdown(t *testing.T) {
o := &Options{
CertPathTLS: "fixtures/test.crt",
KeyPathTLS: "fixtures/test.key",
}
testServerShutdown(t, o, "https")
}
func testServerShutdown(t *testing.T, o *Options, scheme string) {
const shutdownDelay = 1 * time.Second
address, err := findAddress()
require.NoError(t, err)
o.Address, o.WaitForHealthcheckInterval = address, shutdownDelay
testUrl := scheme + "://" + address
// simulate a backend that got a request and should be handled correctly
dc, err := routestring.New(`r0: * -> latency("3s") -> inlineContent("OK") -> status(200) -> <shunt>`)
require.NoError(t, err)
rt := routing.New(routing.Options{
FilterRegistry: builtin.MakeRegistry(),
DataClients: []routing.DataClient{dc},
})
defer rt.Close()
proxy := proxy.New(rt, proxy.OptionsNone)
defer proxy.Close()
sigs := make(chan os.Signal, 1)
go func() {
err := listenAndServeQuit(proxy, o, sigs, nil, nil)
require.NoError(t, err)
}()
// initiate shutdown
sigs <- syscall.SIGTERM
time.Sleep(shutdownDelay / 2)
t.Logf("ongoing request passing in before shutdown")
r, err := waitConnGet(testUrl)
require.NoError(t, err)
require.Equal(t, 200, r.StatusCode)
defer r.Body.Close()
body, err := io.ReadAll(r.Body)
require.NoError(t, err)
require.Equal(t, "OK", string(body))
time.Sleep(shutdownDelay / 2)
t.Logf("request after shutdown should fail")
_, err = waitConnGet(testUrl)
require.Error(t, err)
}
type (
customRatelimitSpec struct{ registry *ratelimit.Registry }
customRatelimitFilter struct{}
)
func (s *customRatelimitSpec) Name() string { return "customRatelimit" }
func (s *customRatelimitSpec) CreateFilter(config []interface{}) (filters.Filter, error) {
log.Infof("Registry: %v", s.registry)
return &customRatelimitFilter{}, nil
}
func (f *customRatelimitFilter) Request(ctx filters.FilterContext) {}
func (f *customRatelimitFilter) Response(ctx filters.FilterContext) {}
func Example_ratelimitRegistryBinding() {
s := &customRatelimitSpec{}
o := Options{
Address: ":9090",
InlineRoutes: `* -> customRatelimit() -> <shunt>`,
EnableRatelimiters: true,
EnableSwarm: true,
SwarmRedisURLs: []string{":6379"},
CustomFilters: []filters.Spec{s},
SwarmRegistry: func(registry *ratelimit.Registry) {
s.registry = registry
},
}
log.Fatal(Run(o))
// Example functions without output comments are compiled but not executed
}