forked from kubernetes/kubernetes
/
http_test.go
316 lines (294 loc) · 8.57 KB
/
http_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
313
314
315
316
/*
Copyright 2015 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package http
import (
"fmt"
"net"
"net/http"
"net/http/httptest"
"net/url"
"os"
"strconv"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/kubernetes/pkg/probe"
)
const FailureCode int = -1
func setEnv(key, value string) func() {
originalValue := os.Getenv(key)
os.Setenv(key, value)
if len(originalValue) > 0 {
return func() {
os.Setenv(key, originalValue)
}
}
return func() {}
}
func unsetEnv(key string) func() {
originalValue := os.Getenv(key)
os.Unsetenv(key)
if len(originalValue) > 0 {
return func() {
os.Setenv(key, originalValue)
}
}
return func() {}
}
func TestHTTPProbeProxy(t *testing.T) {
res := "welcome to http probe proxy"
localProxy := "http://127.0.0.1:9098/"
defer setEnv("http_proxy", localProxy)()
defer setEnv("HTTP_PROXY", localProxy)()
defer unsetEnv("no_proxy")()
defer unsetEnv("NO_PROXY")()
prober := New(true)
go func() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, res)
})
err := http.ListenAndServe(":9098", nil)
if err != nil {
t.Errorf("Failed to start foo server: localhost:9098")
}
}()
// take some time to wait server boot
time.Sleep(2 * time.Second)
url, err := url.Parse("http://example.com")
if err != nil {
t.Errorf("proxy test unexpected error: %v", err)
}
_, response, _ := prober.Probe(url, http.Header{}, time.Second*3)
if response == res {
t.Errorf("proxy test unexpected error: the probe is using proxy")
}
}
func TestHTTPProbeChecker(t *testing.T) {
handleReq := func(s int, body string) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(s)
w.Write([]byte(body))
}
}
// Echo handler that returns the contents of request headers in the body
headerEchoHandler := func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
output := ""
for k, arr := range r.Header {
for _, v := range arr {
output += fmt.Sprintf("%s: %s\n", k, v)
}
}
w.Write([]byte(output))
}
redirectHandler := func(s int, bad bool) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
http.Redirect(w, r, "/new", s)
} else if bad && r.URL.Path == "/new" {
http.Error(w, "", http.StatusInternalServerError)
}
}
}
prober := New(true)
testCases := []struct {
handler func(w http.ResponseWriter, r *http.Request)
reqHeaders http.Header
health probe.Result
accBody string
notBody string
}{
// The probe will be filled in below. This is primarily testing that an HTTP GET happens.
{
handler: handleReq(http.StatusOK, "ok body"),
health: probe.Success,
accBody: "ok body",
},
{
handler: headerEchoHandler,
reqHeaders: http.Header{
"X-Muffins-Or-Cupcakes": {"muffins"},
},
health: probe.Success,
accBody: "X-Muffins-Or-Cupcakes: muffins",
},
{
handler: headerEchoHandler,
reqHeaders: http.Header{
"User-Agent": {"foo/1.0"},
},
health: probe.Success,
accBody: "User-Agent: foo/1.0",
},
{
handler: headerEchoHandler,
reqHeaders: http.Header{
"User-Agent": {""},
},
health: probe.Success,
notBody: "User-Agent",
},
{
handler: headerEchoHandler,
reqHeaders: http.Header{},
health: probe.Success,
accBody: "User-Agent: kube-probe/",
},
{
// Echo handler that returns the contents of Host in the body
handler: func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Write([]byte(r.Host))
},
reqHeaders: http.Header{
"Host": {"muffins.cupcakes.org"},
},
health: probe.Success,
accBody: "muffins.cupcakes.org",
},
{
handler: handleReq(FailureCode, "fail body"),
health: probe.Failure,
},
{
handler: handleReq(http.StatusInternalServerError, "fail body"),
health: probe.Failure,
},
{
handler: func(w http.ResponseWriter, r *http.Request) {
time.Sleep(3 * time.Second)
},
health: probe.Failure,
},
{
handler: redirectHandler(http.StatusMovedPermanently, false), // 301
health: probe.Success,
},
{
handler: redirectHandler(http.StatusMovedPermanently, true), // 301
health: probe.Failure,
},
{
handler: redirectHandler(http.StatusFound, false), // 302
health: probe.Success,
},
{
handler: redirectHandler(http.StatusFound, true), // 302
health: probe.Failure,
},
{
handler: redirectHandler(http.StatusTemporaryRedirect, false), // 307
health: probe.Success,
},
{
handler: redirectHandler(http.StatusTemporaryRedirect, true), // 307
health: probe.Failure,
},
{
handler: redirectHandler(http.StatusPermanentRedirect, false), // 308
health: probe.Success,
},
{
handler: redirectHandler(http.StatusPermanentRedirect, true), // 308
health: probe.Failure,
},
}
for i, test := range testCases {
t.Run(fmt.Sprintf("case-%2d", i), func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
test.handler(w, r)
}))
defer server.Close()
u, err := url.Parse(server.URL)
if err != nil {
t.Errorf("case %d: unexpected error: %v", i, err)
}
_, port, err := net.SplitHostPort(u.Host)
if err != nil {
t.Errorf("case %d: unexpected error: %v", i, err)
}
_, err = strconv.Atoi(port)
if err != nil {
t.Errorf("case %d: unexpected error: %v", i, err)
}
health, output, err := prober.Probe(u, test.reqHeaders, 1*time.Second)
if test.health == probe.Unknown && err == nil {
t.Errorf("case %d: expected error", i)
}
if test.health != probe.Unknown && err != nil {
t.Errorf("case %d: unexpected error: %v", i, err)
}
if health != test.health {
t.Errorf("case %d: expected %v, got %v", i, test.health, health)
}
if health != probe.Failure && test.health != probe.Failure {
if !strings.Contains(output, test.accBody) {
t.Errorf("Expected response body to contain %v, got %v", test.accBody, output)
}
if test.notBody != "" && strings.Contains(output, test.notBody) {
t.Errorf("Expected response not to contain %v, got %v", test.notBody, output)
}
}
})
}
}
func TestHTTPProbeChecker_NonLocalRedirects(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/redirect":
loc, _ := url.QueryUnescape(r.URL.Query().Get("loc"))
http.Redirect(w, r, loc, http.StatusFound)
case "/loop":
http.Redirect(w, r, "/loop", http.StatusFound)
case "/success":
w.WriteHeader(http.StatusOK)
default:
http.Error(w, "", http.StatusInternalServerError)
}
})
server := httptest.NewServer(handler)
defer server.Close()
newportServer := httptest.NewServer(handler)
defer newportServer.Close()
testCases := map[string]struct {
redirect string
expectLocalResult probe.Result
expectNonLocalResult probe.Result
}{
"local success": {"/success", probe.Success, probe.Success},
"local fail": {"/fail", probe.Failure, probe.Failure},
"newport success": {newportServer.URL + "/success", probe.Success, probe.Success},
"newport fail": {newportServer.URL + "/fail", probe.Failure, probe.Failure},
"bogus nonlocal": {"http://0.0.0.0/fail", probe.Warning, probe.Failure},
"redirect loop": {"/loop", probe.Failure, probe.Failure},
}
for desc, test := range testCases {
t.Run(desc+"-local", func(t *testing.T) {
prober := New(false)
target, err := url.Parse(server.URL + "/redirect?loc=" + url.QueryEscape(test.redirect))
require.NoError(t, err)
result, _, _ := prober.Probe(target, nil, wait.ForeverTestTimeout)
assert.Equal(t, test.expectLocalResult, result)
})
t.Run(desc+"-nonlocal", func(t *testing.T) {
prober := New(true)
target, err := url.Parse(server.URL + "/redirect?loc=" + url.QueryEscape(test.redirect))
require.NoError(t, err)
result, _, _ := prober.Probe(target, nil, wait.ForeverTestTimeout)
assert.Equal(t, test.expectNonLocalResult, result)
})
}
}