From 2cb560988b111ee736c4ab22588d2091cb04075e Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Fri, 4 Apr 2014 00:02:44 +0000 Subject: [PATCH] api/server: convert TestGetInfo from an integration test to a unit test. Docker-DCO-1.1-Signed-off-by: Solomon Hykes (github: shykes) --- api/server/server_unit_test.go | 67 ++++++++++++++++++++++++++++++++++ integration/api_test.go | 41 --------------------- 2 files changed, 67 insertions(+), 41 deletions(-) diff --git a/api/server/server_unit_test.go b/api/server/server_unit_test.go index c14fd8ba9e..3fc1cea064 100644 --- a/api/server/server_unit_test.go +++ b/api/server/server_unit_test.go @@ -111,3 +111,70 @@ func TestGetVersion(t *testing.T) { t.Errorf("Expected Content-Type %s, %s found", expected, result) } } + +func TestGetInfo(t *testing.T) { + tmp, err := utils.TestDirectory("") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(tmp) + eng, err := engine.New(tmp) + if err != nil { + t.Fatal(err) + } + + var called bool + eng.Register("info", func(job *engine.Job) engine.Status { + called = true + v := &engine.Env{} + v.SetInt("Containers", 1) + v.SetInt("Images", 42000) + if _, err := v.WriteTo(job.Stdout); err != nil { + return job.Error(err) + } + return engine.StatusOK + }) + + r := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/info", nil) + if err != nil { + t.Fatal(err) + } + // FIXME getting the version should require an actual running Server + if err := ServeRequest(eng, api.APIVERSION, r, req); err != nil { + t.Fatal(err) + } + if !called { + t.Fatalf("handler was not called") + } + + out := engine.NewOutput() + i, err := out.AddEnv() + if err != nil { + t.Fatal(err) + } + if _, err := io.Copy(out, r.Body); err != nil { + t.Fatal(err) + } + out.Close() + { + expected := 42000 + result := i.GetInt("Images") + if expected != result { + t.Fatalf("%#v\n", result) + } + } + { + expected := 1 + result := i.GetInt("Containers") + if expected != result { + t.Fatalf("%#v\n", result) + } + } + { + expected := "application/json" + if result := r.HeaderMap.Get("Content-Type"); result != expected { + t.Fatalf("%#v\n", result) + } + } +} diff --git a/integration/api_test.go b/integration/api_test.go index 61697af8a1..26441a2668 100644 --- a/integration/api_test.go +++ b/integration/api_test.go @@ -24,47 +24,6 @@ import ( "github.com/dotcloud/docker/vendor/src/code.google.com/p/go/src/pkg/archive/tar" ) -func TestGetInfo(t *testing.T) { - eng := NewTestEngine(t) - defer mkRuntimeFromEngine(eng, t).Nuke() - - job := eng.Job("images") - initialImages, err := job.Stdout.AddListTable() - if err != nil { - t.Fatal(err) - } - if err := job.Run(); err != nil { - t.Fatal(err) - } - req, err := http.NewRequest("GET", "/info", nil) - if err != nil { - t.Fatal(err) - } - r := httptest.NewRecorder() - - if err := server.ServeRequest(eng, api.APIVERSION, r, req); err != nil { - t.Fatal(err) - } - assertHttpNotError(r, t) - - out := engine.NewOutput() - i, err := out.AddEnv() - if err != nil { - t.Fatal(err) - } - if _, err := io.Copy(out, r.Body); err != nil { - t.Fatal(err) - } - out.Close() - if images := i.GetInt("Images"); images != initialImages.Len() { - t.Errorf("Expected images: %d, %d found", initialImages.Len(), images) - } - expected := "application/json" - if result := r.HeaderMap.Get("Content-Type"); result != expected { - t.Errorf("Expected Content-Type %s, %s found", expected, result) - } -} - func TestGetEvents(t *testing.T) { eng := NewTestEngine(t) srv := mkServerFromEngine(eng, t)