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