diff --git a/hack/test-go.sh b/hack/test-go.sh index f7f631c5db..edb0018d9c 100755 --- a/hack/test-go.sh +++ b/hack/test-go.sh @@ -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 diff --git a/pkg/util/fake_handler.go b/pkg/util/fake_handler.go index 66017d2ac4..fa6a698e2f 100644 --- a/pkg/util/fake_handler.go +++ b/pkg/util/fake_handler.go @@ -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) } diff --git a/pkg/util/fake_handler_test.go b/pkg/util/fake_handler_test.go new file mode 100644 index 0000000000..c5c4d00d50 --- /dev/null +++ b/pkg/util/fake_handler_test.go @@ -0,0 +1,132 @@ +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) + } +} diff --git a/pkg/util/util_test.go b/pkg/util/util_test.go new file mode 100644 index 0000000000..3e8aeabac6 --- /dev/null +++ b/pkg/util/util_test.go @@ -0,0 +1,41 @@ +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) + } +}