Update api_test.go to reflect new api.go

This commit is contained in:
Guillaume J. Charmes 2013-05-10 15:24:07 -07:00
parent f7beba3acc
commit b99446831f
1 changed files with 71 additions and 121 deletions

View File

@ -43,13 +43,9 @@ func TestGetAuth(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
body, err := postAuth(srv, r, req, nil) if err := postAuth(srv, r, req, nil); err != nil {
if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if body == nil {
t.Fatalf("No body received\n")
}
if r.Code != http.StatusOK && r.Code != 0 { if r.Code != http.StatusOK && r.Code != 0 {
t.Fatalf("%d OK or 0 expected, received %d\n", http.StatusOK, r.Code) t.Fatalf("%d OK or 0 expected, received %d\n", http.StatusOK, r.Code)
} }
@ -70,15 +66,14 @@ func TestGetVersion(t *testing.T) {
srv := &Server{runtime: runtime} srv := &Server{runtime: runtime}
body, err := getVersion(srv, nil, nil, nil) r := httptest.NewRecorder()
if err != nil {
if err := getVersion(srv, r, nil, nil); err != nil {
t.Fatal(err) t.Fatal(err)
} }
v := &ApiVersion{} v := &ApiVersion{}
if err = json.Unmarshal(r.Body.Bytes(), v); err != nil {
err = json.Unmarshal(body, v)
if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if v.Version != VERSION { if v.Version != VERSION {
@ -95,12 +90,14 @@ func TestGetInfo(t *testing.T) {
srv := &Server{runtime: runtime} srv := &Server{runtime: runtime}
body, err := getInfo(srv, nil, nil, nil) r := httptest.NewRecorder()
if err != nil {
if err := getInfo(srv, r, nil, nil); err != nil {
t.Fatal(err) t.Fatal(err)
} }
infos := &ApiInfo{} infos := &ApiInfo{}
err = json.Unmarshal(body, infos) err = json.Unmarshal(r.Body.Bytes(), infos)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -124,14 +121,14 @@ func TestGetImagesJson(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
body, err := getImagesJson(srv, nil, req, nil) r := httptest.NewRecorder()
if err != nil {
if err := getImagesJson(srv, r, req, nil); err != nil {
t.Fatal(err) t.Fatal(err)
} }
images := []ApiImages{} images := []ApiImages{}
err = json.Unmarshal(body, &images) if err := json.Unmarshal(r.Body.Bytes(), &images); err != nil {
if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -143,20 +140,20 @@ func TestGetImagesJson(t *testing.T) {
t.Errorf("Excepted image %s, %s found", unitTestImageName, images[0].Repository) t.Errorf("Excepted image %s, %s found", unitTestImageName, images[0].Repository)
} }
r2 := httptest.NewRecorder()
// only_ids=1&all=1 // only_ids=1&all=1
req2, err := http.NewRequest("GET", "/images/json?only_ids=1&all=1", nil) req2, err := http.NewRequest("GET", "/images/json?only_ids=1&all=1", nil)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
body2, err := getImagesJson(srv, nil, req2, nil) if err := getImagesJson(srv, r2, req2, nil); err != nil {
if err != nil {
t.Fatal(err) t.Fatal(err)
} }
images2 := []ApiImages{} images2 := []ApiImages{}
err = json.Unmarshal(body2, &images2) if err := json.Unmarshal(r2.Body.Bytes(), &images2); err != nil {
if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -172,20 +169,20 @@ func TestGetImagesJson(t *testing.T) {
t.Errorf("Retrieved image Id differs, expected %s, received %s", GetTestImage(runtime).ShortId(), images2[0].Id) t.Errorf("Retrieved image Id differs, expected %s, received %s", GetTestImage(runtime).ShortId(), images2[0].Id)
} }
r3 := httptest.NewRecorder()
// filter=a // filter=a
req3, err := http.NewRequest("GET", "/images/json?filter=a", nil) req3, err := http.NewRequest("GET", "/images/json?filter=a", nil)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
body3, err := getImagesJson(srv, nil, req3, nil) if err := getImagesJson(srv, r3, req3, nil); err != nil {
if err != nil {
t.Fatal(err) t.Fatal(err)
} }
images3 := []ApiImages{} images3 := []ApiImages{}
err = json.Unmarshal(body3, &images3) if err := json.Unmarshal(r3.Body.Bytes(), &images3); err != nil {
if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -204,9 +201,7 @@ func TestGetImagesViz(t *testing.T) {
srv := &Server{runtime: runtime} srv := &Server{runtime: runtime}
r := httptest.NewRecorder() r := httptest.NewRecorder()
if err := getImagesViz(srv, r, nil, nil); err != nil {
_, err = getImagesViz(srv, r, nil, nil)
if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -233,19 +228,19 @@ func TestGetImagesSearch(t *testing.T) {
srv := &Server{runtime: runtime} srv := &Server{runtime: runtime}
r := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/images/search?term=redis", nil) req, err := http.NewRequest("GET", "/images/search?term=redis", nil)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
body, err := getImagesSearch(srv, nil, req, nil) if err := getImagesSearch(srv, r, req, nil); err != nil {
if err != nil {
t.Fatal(err) t.Fatal(err)
} }
results := []ApiSearch{} results := []ApiSearch{}
err = json.Unmarshal(body, &results) if err := json.Unmarshal(r.Body.Bytes(), &results); err != nil {
if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if len(results) < 2 { if len(results) < 2 {
@ -262,14 +257,14 @@ func TestGetImagesHistory(t *testing.T) {
srv := &Server{runtime: runtime} srv := &Server{runtime: runtime}
body, err := getImagesHistory(srv, nil, nil, map[string]string{"name": unitTestImageName}) r := httptest.NewRecorder()
if err != nil {
if err := getImagesHistory(srv, r, nil, map[string]string{"name": unitTestImageName}); err != nil {
t.Fatal(err) t.Fatal(err)
} }
history := []ApiHistory{} history := []ApiHistory{}
err = json.Unmarshal(body, &history) if err := json.Unmarshal(r.Body.Bytes(), &history); err != nil {
if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if len(history) != 1 { if len(history) != 1 {
@ -286,15 +281,13 @@ func TestGetImagesByName(t *testing.T) {
srv := &Server{runtime: runtime} srv := &Server{runtime: runtime}
body, err := getImagesByName(srv, nil, nil, map[string]string{"name": unitTestImageName}) r := httptest.NewRecorder()
if err != nil { if err := getImagesByName(srv, r, nil, map[string]string{"name": unitTestImageName}); err != nil {
t.Fatal(err) t.Fatal(err)
} }
img := &Image{} img := &Image{}
if err := json.Unmarshal(r.Body.Bytes(), img); err != nil {
err = json.Unmarshal(body, img)
if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if img.Id != GetTestImage(runtime).Id || img.Comment != "Imported from http://get.docker.io/images/busybox" { if img.Id != GetTestImage(runtime).Id || img.Comment != "Imported from http://get.docker.io/images/busybox" {
@ -325,13 +318,12 @@ func TestGetContainersPs(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
body, err := getContainersPs(srv, nil, req, nil) r := httptest.NewRecorder()
if err != nil { if err := getContainersPs(srv, r, req, nil); err != nil {
t.Fatal(err) t.Fatal(err)
} }
containers := []ApiContainers{} containers := []ApiContainers{}
err = json.Unmarshal(body, &containers) if err := json.Unmarshal(r.Body.Bytes(), &containers); err != nil {
if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if len(containers) != 1 { if len(containers) != 1 {
@ -370,9 +362,7 @@ func TestGetContainersExport(t *testing.T) {
} }
r := httptest.NewRecorder() r := httptest.NewRecorder()
if err = getContainersExport(srv, r, nil, map[string]string{"name": container.Id}); err != nil {
_, err = getContainersExport(srv, r, nil, map[string]string{"name": container.Id})
if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -426,12 +416,12 @@ func TestGetContainersChanges(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
body, err := getContainersChanges(srv, nil, nil, map[string]string{"name": container.Id}) r := httptest.NewRecorder()
if err != nil { if err := getContainersChanges(srv, r, nil, map[string]string{"name": container.Id}); err != nil {
t.Fatal(err) t.Fatal(err)
} }
changes := []Change{} changes := []Change{}
if err := json.Unmarshal(body, &changes); err != nil { if err := json.Unmarshal(r.Body.Bytes(), &changes); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -470,12 +460,12 @@ func TestGetContainersByName(t *testing.T) {
} }
defer runtime.Destroy(container) defer runtime.Destroy(container)
body, err := getContainersByName(srv, nil, nil, map[string]string{"name": container.Id}) r := httptest.NewRecorder()
if err != nil { if err := getContainersByName(srv, r, nil, map[string]string{"name": container.Id}); err != nil {
t.Fatal(err) t.Fatal(err)
} }
outContainer := &Container{} outContainer := &Container{}
if err := json.Unmarshal(body, outContainer); err != nil { if err := json.Unmarshal(r.Body.Bytes(), outContainer); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if outContainer.Id != container.Id { if outContainer.Id != container.Id {
@ -498,14 +488,13 @@ func TestPostAuth(t *testing.T) {
} }
runtime.authConfig = authConfigOrig runtime.authConfig = authConfigOrig
body, err := getAuth(srv, nil, nil, nil) r := httptest.NewRecorder()
if err != nil { if err := getAuth(srv, r, nil, nil); err != nil {
t.Fatal(err) t.Fatal(err)
} }
authConfig := &auth.AuthConfig{} authConfig := &auth.AuthConfig{}
err = json.Unmarshal(body, authConfig) if err := json.Unmarshal(r.Body.Bytes(), authConfig); err != nil {
if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -523,8 +512,6 @@ func TestPostCommit(t *testing.T) {
srv := &Server{runtime: runtime} srv := &Server{runtime: runtime}
r := httptest.NewRecorder()
builder := NewBuilder(runtime) builder := NewBuilder(runtime)
// Create a container and remove a file // Create a container and remove a file
@ -548,8 +535,8 @@ func TestPostCommit(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
body, err := postCommit(srv, r, req, nil) r := httptest.NewRecorder()
if err != nil { if err := postCommit(srv, r, req, nil); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if r.Code != http.StatusCreated { if r.Code != http.StatusCreated {
@ -557,7 +544,7 @@ func TestPostCommit(t *testing.T) {
} }
apiId := &ApiId{} apiId := &ApiId{}
if err := json.Unmarshal(body, apiId); err != nil { if err := json.Unmarshal(r.Body.Bytes(), apiId); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if _, err := runtime.graph.Get(apiId.Id); err != nil { if _, err := runtime.graph.Get(apiId.Id); err != nil {
@ -579,20 +566,16 @@ func TestPostBuild(t *testing.T) {
c1 := make(chan struct{}) c1 := make(chan struct{})
go func() { go func() {
defer close(c1)
r := &hijackTester{ r := &hijackTester{
ResponseRecorder: httptest.NewRecorder(), ResponseRecorder: httptest.NewRecorder(),
in: stdin, in: stdin,
out: stdoutPipe, out: stdoutPipe,
} }
body, err := postBuild(srv, r, nil, nil) if err := postBuild(srv, r, nil, nil); err != nil {
close(c1)
if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if body != nil {
t.Fatalf("No body expected, received: %s\n", body)
}
}() }()
// Acknowledge hijack // Acknowledge hijack
@ -794,8 +777,6 @@ func TestPostContainersCreate(t *testing.T) {
srv := &Server{runtime: runtime} srv := &Server{runtime: runtime}
r := httptest.NewRecorder()
configJson, err := json.Marshal(&Config{ configJson, err := json.Marshal(&Config{
Image: GetTestImage(runtime).Id, Image: GetTestImage(runtime).Id,
Memory: 33554432, Memory: 33554432,
@ -810,8 +791,8 @@ func TestPostContainersCreate(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
body, err := postContainersCreate(srv, r, req, nil) r := httptest.NewRecorder()
if err != nil { if err := postContainersCreate(srv, r, req, nil); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if r.Code != http.StatusCreated { if r.Code != http.StatusCreated {
@ -819,7 +800,7 @@ func TestPostContainersCreate(t *testing.T) {
} }
apiRun := &ApiRun{} apiRun := &ApiRun{}
if err := json.Unmarshal(body, apiRun); err != nil { if err := json.Unmarshal(r.Body.Bytes(), apiRun); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -874,14 +855,9 @@ func TestPostContainersKill(t *testing.T) {
} }
r := httptest.NewRecorder() r := httptest.NewRecorder()
if err := postContainersKill(srv, r, nil, map[string]string{"name": container.Id}); err != nil {
body, err := postContainersKill(srv, r, nil, map[string]string{"name": container.Id})
if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if body != nil {
t.Fatalf("No body expected, received: %s\n", body)
}
if r.Code != http.StatusNoContent { if r.Code != http.StatusNoContent {
t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code) t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
} }
@ -922,19 +898,14 @@ func TestPostContainersRestart(t *testing.T) {
t.Errorf("Container should be running") t.Errorf("Container should be running")
} }
r := httptest.NewRecorder()
req, err := http.NewRequest("POST", "/containers/"+container.Id+"/restart?t=1", bytes.NewReader([]byte{})) req, err := http.NewRequest("POST", "/containers/"+container.Id+"/restart?t=1", bytes.NewReader([]byte{}))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
body, err := postContainersRestart(srv, r, req, map[string]string{"name": container.Id}) r := httptest.NewRecorder()
if err != nil { if err := postContainersRestart(srv, r, req, map[string]string{"name": container.Id}); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if body != nil {
t.Fatalf("No body expected, received: %s\n", body)
}
if r.Code != http.StatusNoContent { if r.Code != http.StatusNoContent {
t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code) t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
} }
@ -973,14 +944,9 @@ func TestPostContainersStart(t *testing.T) {
defer runtime.Destroy(container) defer runtime.Destroy(container)
r := httptest.NewRecorder() r := httptest.NewRecorder()
if err := postContainersStart(srv, r, nil, map[string]string{"name": container.Id}); err != nil {
body, err := postContainersStart(srv, r, nil, map[string]string{"name": container.Id})
if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if body != nil {
t.Fatalf("No body expected, received: %s\n", body)
}
if r.Code != http.StatusNoContent { if r.Code != http.StatusNoContent {
t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code) t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
} }
@ -992,7 +958,8 @@ func TestPostContainersStart(t *testing.T) {
t.Errorf("Container should be running") t.Errorf("Container should be running")
} }
if _, err = postContainersStart(srv, r, nil, map[string]string{"name": container.Id}); err == nil { r = httptest.NewRecorder()
if err = postContainersStart(srv, r, nil, map[string]string{"name": container.Id}); err == nil {
t.Fatalf("A running containter should be able to be started") t.Fatalf("A running containter should be able to be started")
} }
@ -1033,20 +1000,15 @@ func TestPostContainersStop(t *testing.T) {
t.Errorf("Container should be running") t.Errorf("Container should be running")
} }
r := httptest.NewRecorder()
// Note: as it is a POST request, it requires a body. // Note: as it is a POST request, it requires a body.
req, err := http.NewRequest("POST", "/containers/"+container.Id+"/stop?t=1", bytes.NewReader([]byte{})) req, err := http.NewRequest("POST", "/containers/"+container.Id+"/stop?t=1", bytes.NewReader([]byte{}))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
body, err := postContainersStop(srv, r, req, map[string]string{"name": container.Id}) r := httptest.NewRecorder()
if err != nil { if err := postContainersStop(srv, r, req, map[string]string{"name": container.Id}); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if body != nil {
t.Fatalf("No body expected, received: %s\n", body)
}
if r.Code != http.StatusNoContent { if r.Code != http.StatusNoContent {
t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code) t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
} }
@ -1081,12 +1043,12 @@ func TestPostContainersWait(t *testing.T) {
} }
setTimeout(t, "Wait timed out", 3*time.Second, func() { setTimeout(t, "Wait timed out", 3*time.Second, func() {
body, err := postContainersWait(srv, nil, nil, map[string]string{"name": container.Id}) r := httptest.NewRecorder()
if err != nil { if err := postContainersWait(srv, r, nil, map[string]string{"name": container.Id}); err != nil {
t.Fatal(err) t.Fatal(err)
} }
apiWait := &ApiWait{} apiWait := &ApiWait{}
if err := json.Unmarshal(body, apiWait); err != nil { if err := json.Unmarshal(r.Body.Bytes(), apiWait); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if apiWait.StatusCode != 0 { if apiWait.StatusCode != 0 {
@ -1131,8 +1093,7 @@ func TestPostContainersAttach(t *testing.T) {
// Attach to it // Attach to it
c1 := make(chan struct{}) c1 := make(chan struct{})
go func() { go func() {
// We're simulating a disconnect so the return value doesn't matter. What matters is the defer close(c1)
// fact that CmdAttach returns.
r := &hijackTester{ r := &hijackTester{
ResponseRecorder: httptest.NewRecorder(), ResponseRecorder: httptest.NewRecorder(),
@ -1145,14 +1106,9 @@ func TestPostContainersAttach(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
body, err := postContainersAttach(srv, r, req, map[string]string{"name": container.Id}) if err := postContainersAttach(srv, r, req, map[string]string{"name": container.Id}); err != nil {
close(c1)
if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if body != nil {
t.Fatalf("No body expected, received: %s\n", body)
}
}() }()
// Acknowledge hijack // Acknowledge hijack
@ -1215,20 +1171,14 @@ func TestDeleteContainers(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
r := httptest.NewRecorder()
req, err := http.NewRequest("DELETE", "/containers/"+container.Id, nil) req, err := http.NewRequest("DELETE", "/containers/"+container.Id, nil)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
r := httptest.NewRecorder()
body, err := deleteContainers(srv, r, req, map[string]string{"name": container.Id}) if err := deleteContainers(srv, r, req, map[string]string{"name": container.Id}); err != nil {
if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if body != nil {
t.Fatalf("No body expected, received: %s\n", body)
}
if r.Code != http.StatusNoContent { if r.Code != http.StatusNoContent {
t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code) t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
} }
@ -1244,7 +1194,7 @@ func TestDeleteContainers(t *testing.T) {
func TestDeleteImages(t *testing.T) { func TestDeleteImages(t *testing.T) {
//FIXME: Implement this test //FIXME: Implement this test
t.Log("Test not implemented") t.Skip("Test not implemented")
} }
// Mocked types for tests // Mocked types for tests