mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
api/server: replace an integration test with a unit test using engine mocking.
Docker-DCO-1.1-Signed-off-by: Solomon Hykes <solomon@docker.com> (github: shykes)
This commit is contained in:
parent
a278656330
commit
c8f437aee0
2 changed files with 61 additions and 37 deletions
|
@ -2,8 +2,13 @@ package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/dotcloud/docker/api"
|
||||||
|
"github.com/dotcloud/docker/engine"
|
||||||
|
"github.com/dotcloud/docker/utils"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -50,3 +55,59 @@ func TesthttpError(t *testing.T) {
|
||||||
t.Fatalf("Expected %d, got %d", http.StatusInternalServerError, r.Code)
|
t.Fatalf("Expected %d, got %d", http.StatusInternalServerError, r.Code)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetVersion(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("version", func(job *engine.Job) engine.Status {
|
||||||
|
called = true
|
||||||
|
v := &engine.Env{}
|
||||||
|
v.Set("Version", "42.1")
|
||||||
|
v.Set("ApiVersion", "1.1.1.1.1")
|
||||||
|
v.Set("GoVersion", "2.42")
|
||||||
|
v.Set("Os", "Linux")
|
||||||
|
v.Set("Arch", "x86_64")
|
||||||
|
if _, err := v.WriteTo(job.Stdout); err != nil {
|
||||||
|
return job.Error(err)
|
||||||
|
}
|
||||||
|
return engine.StatusOK
|
||||||
|
})
|
||||||
|
|
||||||
|
r := httptest.NewRecorder()
|
||||||
|
req, err := http.NewRequest("GET", "/version", 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()
|
||||||
|
v, err := out.AddEnv()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if _, err := io.Copy(out, r.Body); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
out.Close()
|
||||||
|
expected := "42.1"
|
||||||
|
if result := v.Get("Version"); result != expected {
|
||||||
|
t.Errorf("Expected version %s, %s found", expected, result)
|
||||||
|
}
|
||||||
|
expected = "application/json"
|
||||||
|
if result := r.HeaderMap.Get("Content-Type"); result != expected {
|
||||||
|
t.Errorf("Expected Content-Type %s, %s found", expected, result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -16,7 +16,6 @@ import (
|
||||||
|
|
||||||
"github.com/dotcloud/docker/api"
|
"github.com/dotcloud/docker/api"
|
||||||
"github.com/dotcloud/docker/api/server"
|
"github.com/dotcloud/docker/api/server"
|
||||||
"github.com/dotcloud/docker/dockerversion"
|
|
||||||
"github.com/dotcloud/docker/engine"
|
"github.com/dotcloud/docker/engine"
|
||||||
"github.com/dotcloud/docker/image"
|
"github.com/dotcloud/docker/image"
|
||||||
"github.com/dotcloud/docker/runconfig"
|
"github.com/dotcloud/docker/runconfig"
|
||||||
|
@ -25,42 +24,6 @@ import (
|
||||||
"github.com/dotcloud/docker/vendor/src/code.google.com/p/go/src/pkg/archive/tar"
|
"github.com/dotcloud/docker/vendor/src/code.google.com/p/go/src/pkg/archive/tar"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGetVersion(t *testing.T) {
|
|
||||||
eng := NewTestEngine(t)
|
|
||||||
defer mkRuntimeFromEngine(eng, t).Nuke()
|
|
||||||
|
|
||||||
var err error
|
|
||||||
r := httptest.NewRecorder()
|
|
||||||
|
|
||||||
req, err := http.NewRequest("GET", "/version", nil)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
// FIXME getting the version should require an actual running Server
|
|
||||||
if err := server.ServeRequest(eng, api.APIVERSION, r, req); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
assertHttpNotError(r, t)
|
|
||||||
|
|
||||||
out := engine.NewOutput()
|
|
||||||
v, err := out.AddEnv()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if _, err := io.Copy(out, r.Body); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
out.Close()
|
|
||||||
expected := dockerversion.VERSION
|
|
||||||
if result := v.Get("Version"); result != expected {
|
|
||||||
t.Errorf("Expected version %s, %s found", expected, result)
|
|
||||||
}
|
|
||||||
expected = "application/json"
|
|
||||||
if result := r.HeaderMap.Get("Content-Type"); result != expected {
|
|
||||||
t.Errorf("Expected Content-Type %s, %s found", expected, result)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetInfo(t *testing.T) {
|
func TestGetInfo(t *testing.T) {
|
||||||
eng := NewTestEngine(t)
|
eng := NewTestEngine(t)
|
||||||
defer mkRuntimeFromEngine(eng, t).Nuke()
|
defer mkRuntimeFromEngine(eng, t).Nuke()
|
||||||
|
|
Loading…
Add table
Reference in a new issue