forked from zalando/skipper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
skipper_test.go
229 lines (195 loc) · 4.57 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
package skipper
import (
"crypto/tls"
"io/ioutil"
"net"
"net/http"
"os"
"testing"
"time"
"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 TestLoadPlugins(t *testing.T) {
o := Options{
PluginDirs: []string{"./_test_plugins"},
FilterPlugins: [][]string{{"filter_noop"}},
}
if err := o.findAndLoadPlugins(); err != nil {
t.Fatalf("Failed to load plugins: %s", err)
}
}
func TestLoadPluginsFail(t *testing.T) {
o := Options{
PluginDirs: []string{"./_test_plugins_fail"},
}
if err := o.findAndLoadPlugins(); err == nil {
t.Fatalf("did not fail to load plugins: %s", err)
}
}