1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

api/server: convert TestGetInfo from an integration test to a unit test.

Docker-DCO-1.1-Signed-off-by: Solomon Hykes <solomon@docker.com> (github: shykes)
This commit is contained in:
Solomon Hykes 2014-04-04 00:02:44 +00:00
parent 76057addb2
commit 2cb560988b
2 changed files with 67 additions and 41 deletions

View file

@ -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)
}
}
}

View file

@ -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)