forked from zalando/skipper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
skipper_test.go
303 lines (258 loc) · 6.26 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
package skipper
import (
"crypto/tls"
"io/ioutil"
"net"
"net/http"
"os"
"os/signal"
"sync"
"syscall"
"testing"
"time"
"github.com/zalando/skipper/dataclients/routestring"
"github.com/zalando/skipper/filters/builtin"
"github.com/zalando/skipper/proxy"
"github.com/zalando/skipper/routing"
)
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 TestOptionsDefaultsToHTTP(t *testing.T) {
o := Options{}
if o.isHTTPS() {
t.FailNow()
}
}
func TestOptionsWithCertUsesHTTPS(t *testing.T) {
o := Options{CertPathTLS: "foo", KeyPathTLS: "bar"}
if !o.isHTTPS() {
t.FailNow()
}
}
func TestWithWrongCertPathFails(t *testing.T) {
a, err := findAddress()
if err != nil {
t.Fatal(err)
}
o := Options{Address: a,
CertPathTLS: "fixtures/notFound.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()
err = listenAndServe(proxy, &o)
if err == nil {
t.Fatal(err)
}
}
func TestWithWrongKeyPathFails(t *testing.T) {
a, err := findAddress()
if err != nil {
t.Fatal(err)
}
o := Options{Address: a,
CertPathTLS: "fixtures/test.crt",
KeyPathTLS: "fixtures/notFound.key",
}
rt := routing.New(routing.Options{
FilterRegistry: builtin.MakeRegistry(),
DataClients: []routing.DataClient{}})
defer rt.Close()
proxy := proxy.New(rt, proxy.OptionsNone)
defer proxy.Close()
err = listenAndServe(proxy, &o)
if err == nil {
t.Fatal(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 = ioutil.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 = ioutil.ReadAll(r.Body)
if err != nil {
t.Fatalf("Failed to stream response body: %v", err)
}
}
func TestHTTPServerShutdown(t *testing.T) {
d := 1 * time.Second
o := Options{
Address: ":19999",
WaitForHealthcheckInterval: d,
}
// simulate a backend that got a request and should be handled correctly
dc, err := routestring.New(`r0: * -> latency("3s") -> inlineContent("OK") -> status(200) -> <shunt>`)
if err != nil {
t.Errorf("Failed to create dataclient: %v", 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()
go func() {
if errLas := listenAndServe(proxy, &o); errLas != nil {
t.Logf("Failed to liste and serve: %v", errLas)
}
}()
pid := syscall.Getpid()
p, err := os.FindProcess(pid)
if err != nil {
t.Errorf("Failed to find current process: %v", err)
}
var wg sync.WaitGroup
installSigHandler := make(chan struct{}, 1)
wg.Add(1)
go func() {
defer wg.Done()
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGTERM)
installSigHandler <- struct{}{}
<-sigs
// ongoing requests passing in before shutdown
time.Sleep(d / 2)
r, err2 := waitConnGet("http://" + o.Address)
if r != nil {
defer r.Body.Close()
}
if err2 != nil {
t.Errorf("Cannot connect to the local server for testing: %v ", err2)
}
if r.StatusCode != 200 {
t.Errorf("Status code should be 200, instead got: %d\n", r.StatusCode)
}
body, err2 := ioutil.ReadAll(r.Body)
if err2 != nil {
t.Errorf("Failed to stream response body: %v", err2)
}
if s := string(body); s != "OK" {
t.Errorf("Failed to get the right content: %s", s)
}
// requests on closed listener should fail
time.Sleep(d / 2)
r2, err2 := waitConnGet("http://" + o.Address)
if r2 != nil {
defer r2.Body.Close()
}
if err2 == nil {
t.Error("Can connect to a closed server for testing")
}
}()
<-installSigHandler
time.Sleep(d / 2)
if err = p.Signal(syscall.SIGTERM); err != nil {
t.Errorf("Failed to signal process: %v", err)
}
wg.Wait()
time.Sleep(d)
}