This commit is contained in:
Brendan Burns 2014-06-11 12:50:01 -07:00
parent 9010ef954c
commit 9f76f13205
2 changed files with 22 additions and 25 deletions

View File

@ -4,7 +4,7 @@ import (
"bytes" "bytes"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"testing" "testing"
) )
func expectNoError(t *testing.T, err error) { func expectNoError(t *testing.T, err error) {
@ -19,13 +19,13 @@ func TestFakeHandlerPath(t *testing.T) {
method := "GET" method := "GET"
path := "/foo/bar" path := "/foo/bar"
body := "somebody" body := "somebody"
req, err := http.NewRequest(method, server.URL + path, bytes.NewBufferString(body)); req, err := http.NewRequest(method, server.URL+path, bytes.NewBufferString(body))
expectNoError(t, err) expectNoError(t, err)
client := http.Client{} client := http.Client{}
_, err = client.Do(req) _, err = client.Do(req)
expectNoError(t, err) expectNoError(t, err)
handler.ValidateRequest(t, path, method, &body) handler.ValidateRequest(t, path, method, &body)
} }
@ -34,18 +34,18 @@ func TestFakeHandlerPathNoBody(t *testing.T) {
server := httptest.NewServer(&handler) server := httptest.NewServer(&handler)
method := "GET" method := "GET"
path := "/foo/bar" path := "/foo/bar"
req, err := http.NewRequest(method, server.URL + path, nil); req, err := http.NewRequest(method, server.URL+path, nil)
expectNoError(t, err) expectNoError(t, err)
client := http.Client{} client := http.Client{}
_, err = client.Do(req) _, err = client.Do(req)
expectNoError(t, err) expectNoError(t, err)
handler.ValidateRequest(t, path, method, nil) handler.ValidateRequest(t, path, method, nil)
} }
type fakeError struct { type fakeError struct {
errors[] string errors []string
} }
func (f *fakeError) Errorf(format string, args ...interface{}) { func (f *fakeError) Errorf(format string, args ...interface{}) {
@ -59,12 +59,12 @@ func TestFakeHandlerWrongPath(t *testing.T) {
path := "/foo/bar" path := "/foo/bar"
fakeT := fakeError{} fakeT := fakeError{}
req, err := http.NewRequest(method, server.URL + "/foo/baz", nil); req, err := http.NewRequest(method, server.URL+"/foo/baz", nil)
expectNoError(t, err) expectNoError(t, err)
client := http.Client{} client := http.Client{}
_, err = client.Do(req) _, err = client.Do(req)
expectNoError(t, err) expectNoError(t, err)
handler.ValidateRequest(&fakeT, path, method, nil) handler.ValidateRequest(&fakeT, path, method, nil)
if len(fakeT.errors) != 1 { if len(fakeT.errors) != 1 {
t.Errorf("Unexpected error set: %#v", fakeT.errors) t.Errorf("Unexpected error set: %#v", fakeT.errors)
@ -78,12 +78,12 @@ func TestFakeHandlerWrongMethod(t *testing.T) {
path := "/foo/bar" path := "/foo/bar"
fakeT := fakeError{} fakeT := fakeError{}
req, err := http.NewRequest("PUT", server.URL + path, nil); req, err := http.NewRequest("PUT", server.URL+path, nil)
expectNoError(t, err) expectNoError(t, err)
client := http.Client{} client := http.Client{}
_, err = client.Do(req) _, err = client.Do(req)
expectNoError(t, err) expectNoError(t, err)
handler.ValidateRequest(&fakeT, path, method, nil) handler.ValidateRequest(&fakeT, path, method, nil)
if len(fakeT.errors) != 1 { if len(fakeT.errors) != 1 {
t.Errorf("Unexpected error set: %#v", fakeT.errors) t.Errorf("Unexpected error set: %#v", fakeT.errors)
@ -98,12 +98,12 @@ func TestFakeHandlerWrongBody(t *testing.T) {
body := "somebody" body := "somebody"
fakeT := fakeError{} fakeT := fakeError{}
req, err := http.NewRequest(method, server.URL + path, bytes.NewBufferString(body)); req, err := http.NewRequest(method, server.URL+path, bytes.NewBufferString(body))
expectNoError(t, err) expectNoError(t, err)
client := http.Client{} client := http.Client{}
_, err = client.Do(req) _, err = client.Do(req)
expectNoError(t, err) expectNoError(t, err)
otherbody := "otherbody" otherbody := "otherbody"
handler.ValidateRequest(&fakeT, path, method, &otherbody) handler.ValidateRequest(&fakeT, path, method, &otherbody)
if len(fakeT.errors) != 1 { if len(fakeT.errors) != 1 {
@ -119,7 +119,7 @@ func TestFakeHandlerNilBody(t *testing.T) {
body := "somebody" body := "somebody"
fakeT := fakeError{} fakeT := fakeError{}
req, err := http.NewRequest(method, server.URL + path, nil); req, err := http.NewRequest(method, server.URL+path, nil)
expectNoError(t, err) expectNoError(t, err)
client := http.Client{} client := http.Client{}
_, err = client.Do(req) _, err = client.Do(req)
@ -130,5 +130,3 @@ func TestFakeHandlerNilBody(t *testing.T) {
t.Errorf("Unexpected error set: %#v", fakeT.errors) t.Errorf("Unexpected error set: %#v", fakeT.errors)
} }
} }

View File

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