From 9a2771eac22950fce43702896052f5187944de6d Mon Sep 17 00:00:00 2001 From: Adrien Folie Date: Mon, 30 Jun 2014 01:31:16 +0200 Subject: [PATCH 1/2] Migrate TestGetImagesHistory into unit and CLI test Docker-DCO-1.1-Signed-off-by: Adrien Folie (github: folieadrien) --- api/server/server_unit_test.go | 27 ++++++++++++++++++++++ integration-cli/docker_cli_history_test.go | 18 +++++++++++++++ integration/api_test.go | 25 -------------------- 3 files changed, 45 insertions(+), 25 deletions(-) diff --git a/api/server/server_unit_test.go b/api/server/server_unit_test.go index 914cb7c0dc..4840a2526c 100644 --- a/api/server/server_unit_test.go +++ b/api/server/server_unit_test.go @@ -289,6 +289,33 @@ func TestLogsNoStreams(t *testing.T) { } } +func TestGetImagesHistory(t *testing.T) { + eng := engine.New() + imageName := "docker-test-image" + var called bool + eng.Register("history", func(job *engine.Job) engine.Status { + called = true + if job.Args[0] != imageName { + t.Fatalf("name != '%s': %#v", imageName, job.Args[0]) + } + v := &engine.Env{} + if _, err := v.WriteTo(job.Stdout); err != nil { + return job.Error(err) + } + return engine.StatusOK + }) + r := serveRequest("GET", "/images/"+imageName+"/history", nil, eng, t) + if !called { + t.Fatalf("handler was not called") + } + if r.Code != http.StatusOK { + t.Fatalf("Got status %d, expected %d", r.Code, http.StatusOK) + } + if r.HeaderMap.Get("Content-Type") != "application/json" { + t.Fatalf("%#v\n", r) + } +} + func serveRequest(method, target string, body io.Reader, eng *engine.Engine, t *testing.T) *httptest.ResponseRecorder { r := httptest.NewRecorder() req, err := http.NewRequest(method, target, body) diff --git a/integration-cli/docker_cli_history_test.go b/integration-cli/docker_cli_history_test.go index 42edec2fd6..59ad117128 100644 --- a/integration-cli/docker_cli_history_test.go +++ b/integration-cli/docker_cli_history_test.go @@ -41,3 +41,21 @@ func TestBuildHistory(t *testing.T) { deleteImages("testbuildhistory") } + +func TestHistoryExistentImage(t *testing.T) { + historyCmd := exec.Command(dockerBinary, "history", "busybox") + _, exitCode, err := runCommandWithOutput(historyCmd) + if err != nil || exitCode != 0 { + t.Fatal("failed to get image history") + } + logDone("history - history on existent image must not fail") +} + +func TestHistoryNonExistentImage(t *testing.T) { + historyCmd := exec.Command(dockerBinary, "history", "testHistoryNonExistentImage") + _, exitCode, err := runCommandWithOutput(historyCmd) + if err == nil || exitCode == 0 { + t.Fatal("history on a non-existent image didn't result in a non-zero exit status") + } + logDone("history - history on non-existent image must fail") +} diff --git a/integration/api_test.go b/integration/api_test.go index 2d1b803682..b2e44717a5 100644 --- a/integration/api_test.go +++ b/integration/api_test.go @@ -4,7 +4,6 @@ import ( "bufio" "bytes" "encoding/json" - "fmt" "io" "io/ioutil" "net" @@ -125,30 +124,6 @@ func TestGetImagesJSON(t *testing.T) { } } -func TestGetImagesHistory(t *testing.T) { - eng := NewTestEngine(t) - defer mkDaemonFromEngine(eng, t).Nuke() - - r := httptest.NewRecorder() - - req, err := http.NewRequest("GET", fmt.Sprintf("/images/%s/history", unitTestImageName), nil) - if err != nil { - t.Fatal(err) - } - if err := server.ServeRequest(eng, api.APIVERSION, r, req); err != nil { - t.Fatal(err) - } - assertHttpNotError(r, t) - - outs := engine.NewTable("Created", 0) - if _, err := outs.ReadListFrom(r.Body.Bytes()); err != nil { - t.Fatal(err) - } - if len(outs.Data) != 1 { - t.Errorf("Expected 1 line, %d found", len(outs.Data)) - } -} - func TestGetImagesByName(t *testing.T) { eng := NewTestEngine(t) defer mkDaemonFromEngine(eng, t).Nuke() From c2699d8d896d592dc02276d76dfdf544fd26d0dd Mon Sep 17 00:00:00 2001 From: Adrien Folie Date: Mon, 30 Jun 2014 09:36:46 +0000 Subject: [PATCH 2/2] Add missing test for full coverage Docker-DCO-1.1-Signed-off-by: Adrien Folie (github: folieadrien) --- api/server/server_unit_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/api/server/server_unit_test.go b/api/server/server_unit_test.go index 4840a2526c..2d14f89551 100644 --- a/api/server/server_unit_test.go +++ b/api/server/server_unit_test.go @@ -295,6 +295,9 @@ func TestGetImagesHistory(t *testing.T) { var called bool eng.Register("history", func(job *engine.Job) engine.Status { called = true + if len(job.Args) == 0 { + t.Fatal("Job arguments is empty") + } if job.Args[0] != imageName { t.Fatalf("name != '%s': %#v", imageName, job.Args[0]) }