mirror of https://github.com/k3s-io/k3s
Merge pull request #36 from brendandburns/tests
Expand testing of the util package. Now 70%pull/6/head
commit
b6521a3f1b
|
@ -28,6 +28,8 @@ find_test_dirs() {
|
||||||
-wholename './third_party' \
|
-wholename './third_party' \
|
||||||
-o -wholename './release' \
|
-o -wholename './release' \
|
||||||
-o -wholename './target' \
|
-o -wholename './target' \
|
||||||
|
-o -wholename '*/third_party/*' \
|
||||||
|
-o -wholename '*/output/*' \
|
||||||
\) -prune \
|
\) -prune \
|
||||||
\) -name '*_test.go' -print0 | xargs -0n1 dirname | sort -u
|
\) -name '*_test.go' -print0 | xargs -0n1 dirname | sort -u
|
||||||
)
|
)
|
||||||
|
@ -35,6 +37,12 @@ find_test_dirs() {
|
||||||
|
|
||||||
|
|
||||||
cd "${KUBE_TARGET}"
|
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
|
for package in $(find_test_dirs); do
|
||||||
go test -cover -coverprofile="tmp.out" "${KUBE_GO_PACKAGE}/${package}"
|
go test -cover -coverprofile="tmp.out" "${KUBE_GO_PACKAGE}/${package}"
|
||||||
done
|
done
|
||||||
|
|
|
@ -19,9 +19,14 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"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.
|
// FakeHandler is to assist in testing HTTP requests.
|
||||||
type FakeHandler struct {
|
type FakeHandler struct {
|
||||||
RequestReceived *http.Request
|
RequestReceived *http.Request
|
||||||
|
@ -41,7 +46,7 @@ func (f *FakeHandler) ServeHTTP(response http.ResponseWriter, request *http.Requ
|
||||||
f.ResponseBody = string(bodyReceived)
|
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 {
|
if f.RequestReceived.URL.Path != expectedPath {
|
||||||
t.Errorf("Unexpected request path: %s", f.RequestReceived.URL.Path)
|
t.Errorf("Unexpected request path: %s", f.RequestReceived.URL.Path)
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue