Update tests

pull/6/head
Brendan Burns 2014-06-09 07:16:43 -07:00
parent 0b6702e80b
commit 9010ef954c
4 changed files with 191 additions and 2 deletions

View File

@ -28,6 +28,8 @@ find_test_dirs() {
-wholename './third_party' \
-o -wholename './release' \
-o -wholename './target' \
-o -wholename '*/third_party/*' \
-o -wholename '*/output/*' \
\) -prune \
\) -name '*_test.go' -print0 | xargs -0n1 dirname | sort -u
)
@ -35,6 +37,12 @@ find_test_dirs() {
cd "${KUBE_TARGET}"
if [ "$1" != "" ]; then
go test -cover -coverprofile="tmp.out" "$KUBE_GO_PACKAGE/$1"
exit 0
fi
for package in $(find_test_dirs); do
go test -cover -coverprofile="tmp.out" "${KUBE_GO_PACKAGE}/${package}"
done

View File

@ -19,9 +19,14 @@ import (
"io/ioutil"
"log"
"net/http"
"testing"
)
// TestInterface is a simple interface providing Errorf, to make injection for
// testing easier (insert 'yo dawg' meme here)
type TestInterface interface {
Errorf(format string, args ...interface{})
}
// FakeHandler is to assist in testing HTTP requests.
type FakeHandler struct {
RequestReceived *http.Request
@ -41,7 +46,7 @@ func (f *FakeHandler) ServeHTTP(response http.ResponseWriter, request *http.Requ
f.ResponseBody = string(bodyReceived)
}
func (f FakeHandler) ValidateRequest(t *testing.T, expectedPath, expectedMethod string, body *string) {
func (f FakeHandler) ValidateRequest(t TestInterface, expectedPath, expectedMethod string, body *string) {
if f.RequestReceived.URL.Path != expectedPath {
t.Errorf("Unexpected request path: %s", f.RequestReceived.URL.Path)
}

View File

@ -0,0 +1,134 @@
package util
import (
"bytes"
"net/http"
"net/http/httptest"
"testing"
)
func expectNoError(t *testing.T, err error) {
if err != nil {
t.Errorf("Unexpected error: %#v", err)
}
}
func TestFakeHandlerPath(t *testing.T) {
handler := FakeHandler{}
server := httptest.NewServer(&handler)
method := "GET"
path := "/foo/bar"
body := "somebody"
req, err := http.NewRequest(method, server.URL + path, bytes.NewBufferString(body));
expectNoError(t, err)
client := http.Client{}
_, err = client.Do(req)
expectNoError(t, err)
handler.ValidateRequest(t, path, method, &body)
}
func TestFakeHandlerPathNoBody(t *testing.T) {
handler := FakeHandler{}
server := httptest.NewServer(&handler)
method := "GET"
path := "/foo/bar"
req, err := http.NewRequest(method, server.URL + path, nil);
expectNoError(t, err)
client := http.Client{}
_, err = client.Do(req)
expectNoError(t, err)
handler.ValidateRequest(t, path, method, nil)
}
type fakeError struct {
errors[] string
}
func (f *fakeError) Errorf(format string, args ...interface{}) {
f.errors = append(f.errors, format)
}
func TestFakeHandlerWrongPath(t *testing.T) {
handler := FakeHandler{}
server := httptest.NewServer(&handler)
method := "GET"
path := "/foo/bar"
fakeT := fakeError{}
req, err := http.NewRequest(method, server.URL + "/foo/baz", nil);
expectNoError(t, err)
client := http.Client{}
_, err = client.Do(req)
expectNoError(t, err)
handler.ValidateRequest(&fakeT, path, method, nil)
if len(fakeT.errors) != 1 {
t.Errorf("Unexpected error set: %#v", fakeT.errors)
}
}
func TestFakeHandlerWrongMethod(t *testing.T) {
handler := FakeHandler{}
server := httptest.NewServer(&handler)
method := "GET"
path := "/foo/bar"
fakeT := fakeError{}
req, err := http.NewRequest("PUT", server.URL + path, nil);
expectNoError(t, err)
client := http.Client{}
_, err = client.Do(req)
expectNoError(t, err)
handler.ValidateRequest(&fakeT, path, method, nil)
if len(fakeT.errors) != 1 {
t.Errorf("Unexpected error set: %#v", fakeT.errors)
}
}
func TestFakeHandlerWrongBody(t *testing.T) {
handler := FakeHandler{}
server := httptest.NewServer(&handler)
method := "GET"
path := "/foo/bar"
body := "somebody"
fakeT := fakeError{}
req, err := http.NewRequest(method, server.URL + path, bytes.NewBufferString(body));
expectNoError(t, err)
client := http.Client{}
_, err = client.Do(req)
expectNoError(t, err)
otherbody := "otherbody"
handler.ValidateRequest(&fakeT, path, method, &otherbody)
if len(fakeT.errors) != 1 {
t.Errorf("Unexpected error set: %#v", fakeT.errors)
}
}
func TestFakeHandlerNilBody(t *testing.T) {
handler := FakeHandler{}
server := httptest.NewServer(&handler)
method := "GET"
path := "/foo/bar"
body := "somebody"
fakeT := fakeError{}
req, err := http.NewRequest(method, server.URL + path, nil);
expectNoError(t, err)
client := http.Client{}
_, err = client.Do(req)
expectNoError(t, err)
handler.ValidateRequest(&fakeT, path, method, &body)
if len(fakeT.errors) != 1 {
t.Errorf("Unexpected error set: %#v", fakeT.errors)
}
}

42
pkg/util/util_test.go Normal file
View File

@ -0,0 +1,42 @@
package util
import (
"encoding/json"
"testing"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
)
func TestMakeJSONString(t *testing.T) {
pod := api.Pod{
JSONBase: api.JSONBase{ ID: "foo" },
Labels: map[string]string{
"foo": "bar",
"baz": "blah",
},
}
body := MakeJSONString(pod)
expectedBody, err := json.Marshal(pod)
expectNoError(t, err)
if string(expectedBody) != body {
t.Errorf("JSON doesn't match. Expected %s, saw %s", expectedBody, body)
}
}
func TestHandleCrash(t *testing.T) {
count := 0
expect := 10
for i := 0; i < expect; i = i + 1 {
defer HandleCrash()
if i % 2 == 0 {
panic("Test Panic")
}
count = count + 1
}
if count != expect {
t.Errorf("Expected %d iterations, found %d", expect, count)
}
}