Skip to content

Commit b6521a3

Browse files
committed
Merge pull request kubernetes#36 from brendandburns/tests
Expand testing of the util package. Now 70%
2 parents 845f776 + 9f76f13 commit b6521a3

File tree

4 files changed

+188
-2
lines changed

4 files changed

+188
-2
lines changed

hack/test-go.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,21 @@ find_test_dirs() {
2828
-wholename './third_party' \
2929
-o -wholename './release' \
3030
-o -wholename './target' \
31+
-o -wholename '*/third_party/*' \
32+
-o -wholename '*/output/*' \
3133
\) -prune \
3234
\) -name '*_test.go' -print0 | xargs -0n1 dirname | sort -u
3335
)
3436
}
3537

3638

3739
cd "${KUBE_TARGET}"
40+
41+
if [ "$1" != "" ]; then
42+
go test -cover -coverprofile="tmp.out" "$KUBE_GO_PACKAGE/$1"
43+
exit 0
44+
fi
45+
3846
for package in $(find_test_dirs); do
3947
go test -cover -coverprofile="tmp.out" "${KUBE_GO_PACKAGE}/${package}"
4048
done

pkg/util/fake_handler.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,14 @@ import (
1919
"io/ioutil"
2020
"log"
2121
"net/http"
22-
"testing"
2322
)
2423

24+
// TestInterface is a simple interface providing Errorf, to make injection for
25+
// testing easier (insert 'yo dawg' meme here)
26+
type TestInterface interface {
27+
Errorf(format string, args ...interface{})
28+
}
29+
2530
// FakeHandler is to assist in testing HTTP requests.
2631
type FakeHandler struct {
2732
RequestReceived *http.Request
@@ -41,7 +46,7 @@ func (f *FakeHandler) ServeHTTP(response http.ResponseWriter, request *http.Requ
4146
f.ResponseBody = string(bodyReceived)
4247
}
4348

44-
func (f FakeHandler) ValidateRequest(t *testing.T, expectedPath, expectedMethod string, body *string) {
49+
func (f FakeHandler) ValidateRequest(t TestInterface, expectedPath, expectedMethod string, body *string) {
4550
if f.RequestReceived.URL.Path != expectedPath {
4651
t.Errorf("Unexpected request path: %s", f.RequestReceived.URL.Path)
4752
}

pkg/util/fake_handler_test.go

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package util
2+
3+
import (
4+
"bytes"
5+
"net/http"
6+
"net/http/httptest"
7+
"testing"
8+
)
9+
10+
func expectNoError(t *testing.T, err error) {
11+
if err != nil {
12+
t.Errorf("Unexpected error: %#v", err)
13+
}
14+
}
15+
16+
func TestFakeHandlerPath(t *testing.T) {
17+
handler := FakeHandler{}
18+
server := httptest.NewServer(&handler)
19+
method := "GET"
20+
path := "/foo/bar"
21+
body := "somebody"
22+
23+
req, err := http.NewRequest(method, server.URL+path, bytes.NewBufferString(body))
24+
expectNoError(t, err)
25+
client := http.Client{}
26+
_, err = client.Do(req)
27+
expectNoError(t, err)
28+
29+
handler.ValidateRequest(t, path, method, &body)
30+
}
31+
32+
func TestFakeHandlerPathNoBody(t *testing.T) {
33+
handler := FakeHandler{}
34+
server := httptest.NewServer(&handler)
35+
method := "GET"
36+
path := "/foo/bar"
37+
38+
req, err := http.NewRequest(method, server.URL+path, nil)
39+
expectNoError(t, err)
40+
client := http.Client{}
41+
_, err = client.Do(req)
42+
expectNoError(t, err)
43+
44+
handler.ValidateRequest(t, path, method, nil)
45+
}
46+
47+
type fakeError struct {
48+
errors []string
49+
}
50+
51+
func (f *fakeError) Errorf(format string, args ...interface{}) {
52+
f.errors = append(f.errors, format)
53+
}
54+
55+
func TestFakeHandlerWrongPath(t *testing.T) {
56+
handler := FakeHandler{}
57+
server := httptest.NewServer(&handler)
58+
method := "GET"
59+
path := "/foo/bar"
60+
fakeT := fakeError{}
61+
62+
req, err := http.NewRequest(method, server.URL+"/foo/baz", nil)
63+
expectNoError(t, err)
64+
client := http.Client{}
65+
_, err = client.Do(req)
66+
expectNoError(t, err)
67+
68+
handler.ValidateRequest(&fakeT, path, method, nil)
69+
if len(fakeT.errors) != 1 {
70+
t.Errorf("Unexpected error set: %#v", fakeT.errors)
71+
}
72+
}
73+
74+
func TestFakeHandlerWrongMethod(t *testing.T) {
75+
handler := FakeHandler{}
76+
server := httptest.NewServer(&handler)
77+
method := "GET"
78+
path := "/foo/bar"
79+
fakeT := fakeError{}
80+
81+
req, err := http.NewRequest("PUT", server.URL+path, nil)
82+
expectNoError(t, err)
83+
client := http.Client{}
84+
_, err = client.Do(req)
85+
expectNoError(t, err)
86+
87+
handler.ValidateRequest(&fakeT, path, method, nil)
88+
if len(fakeT.errors) != 1 {
89+
t.Errorf("Unexpected error set: %#v", fakeT.errors)
90+
}
91+
}
92+
93+
func TestFakeHandlerWrongBody(t *testing.T) {
94+
handler := FakeHandler{}
95+
server := httptest.NewServer(&handler)
96+
method := "GET"
97+
path := "/foo/bar"
98+
body := "somebody"
99+
fakeT := fakeError{}
100+
101+
req, err := http.NewRequest(method, server.URL+path, bytes.NewBufferString(body))
102+
expectNoError(t, err)
103+
client := http.Client{}
104+
_, err = client.Do(req)
105+
expectNoError(t, err)
106+
107+
otherbody := "otherbody"
108+
handler.ValidateRequest(&fakeT, path, method, &otherbody)
109+
if len(fakeT.errors) != 1 {
110+
t.Errorf("Unexpected error set: %#v", fakeT.errors)
111+
}
112+
}
113+
114+
func TestFakeHandlerNilBody(t *testing.T) {
115+
handler := FakeHandler{}
116+
server := httptest.NewServer(&handler)
117+
method := "GET"
118+
path := "/foo/bar"
119+
body := "somebody"
120+
fakeT := fakeError{}
121+
122+
req, err := http.NewRequest(method, server.URL+path, nil)
123+
expectNoError(t, err)
124+
client := http.Client{}
125+
_, err = client.Do(req)
126+
expectNoError(t, err)
127+
128+
handler.ValidateRequest(&fakeT, path, method, &body)
129+
if len(fakeT.errors) != 1 {
130+
t.Errorf("Unexpected error set: %#v", fakeT.errors)
131+
}
132+
}

pkg/util/util_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package util
2+
3+
import (
4+
"encoding/json"
5+
"testing"
6+
7+
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
8+
)
9+
10+
func TestMakeJSONString(t *testing.T) {
11+
pod := api.Pod{
12+
JSONBase: api.JSONBase{ID: "foo"},
13+
Labels: map[string]string{
14+
"foo": "bar",
15+
"baz": "blah",
16+
},
17+
}
18+
19+
body := MakeJSONString(pod)
20+
21+
expectedBody, err := json.Marshal(pod)
22+
expectNoError(t, err)
23+
if string(expectedBody) != body {
24+
t.Errorf("JSON doesn't match. Expected %s, saw %s", expectedBody, body)
25+
}
26+
}
27+
28+
func TestHandleCrash(t *testing.T) {
29+
count := 0
30+
expect := 10
31+
for i := 0; i < expect; i = i + 1 {
32+
defer HandleCrash()
33+
if i%2 == 0 {
34+
panic("Test Panic")
35+
}
36+
count = count + 1
37+
}
38+
if count != expect {
39+
t.Errorf("Expected %d iterations, found %d", expect, count)
40+
}
41+
}

0 commit comments

Comments
 (0)