2014-03-28 19:36:33 -04:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2014-04-08 21:08:20 -04:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2014-03-28 19:36:33 -04:00
|
|
|
"fmt"
|
2014-04-03 19:14:51 -04:00
|
|
|
"github.com/dotcloud/docker/api"
|
|
|
|
"github.com/dotcloud/docker/engine"
|
|
|
|
"io"
|
2014-03-28 19:36:33 -04:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetBoolParam(t *testing.T) {
|
|
|
|
if ret, err := getBoolParam("true"); err != nil || !ret {
|
|
|
|
t.Fatalf("true -> true, nil | got %t %s", ret, err)
|
|
|
|
}
|
|
|
|
if ret, err := getBoolParam("True"); err != nil || !ret {
|
|
|
|
t.Fatalf("True -> true, nil | got %t %s", ret, err)
|
|
|
|
}
|
|
|
|
if ret, err := getBoolParam("1"); err != nil || !ret {
|
|
|
|
t.Fatalf("1 -> true, nil | got %t %s", ret, err)
|
|
|
|
}
|
|
|
|
if ret, err := getBoolParam(""); err != nil || ret {
|
|
|
|
t.Fatalf("\"\" -> false, nil | got %t %s", ret, err)
|
|
|
|
}
|
|
|
|
if ret, err := getBoolParam("false"); err != nil || ret {
|
|
|
|
t.Fatalf("false -> false, nil | got %t %s", ret, err)
|
|
|
|
}
|
|
|
|
if ret, err := getBoolParam("0"); err != nil || ret {
|
|
|
|
t.Fatalf("0 -> false, nil | got %t %s", ret, err)
|
|
|
|
}
|
|
|
|
if ret, err := getBoolParam("faux"); err == nil || ret {
|
|
|
|
t.Fatalf("faux -> false, err | got %t %s", ret, err)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TesthttpError(t *testing.T) {
|
|
|
|
r := httptest.NewRecorder()
|
|
|
|
|
|
|
|
httpError(r, fmt.Errorf("No such method"))
|
|
|
|
if r.Code != http.StatusNotFound {
|
|
|
|
t.Fatalf("Expected %d, got %d", http.StatusNotFound, r.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
httpError(r, fmt.Errorf("This accound hasn't been activated"))
|
|
|
|
if r.Code != http.StatusForbidden {
|
|
|
|
t.Fatalf("Expected %d, got %d", http.StatusForbidden, r.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
httpError(r, fmt.Errorf("Some error"))
|
|
|
|
if r.Code != http.StatusInternalServerError {
|
|
|
|
t.Fatalf("Expected %d, got %d", http.StatusInternalServerError, r.Code)
|
|
|
|
}
|
|
|
|
}
|
2014-04-03 19:14:51 -04:00
|
|
|
|
|
|
|
func TestGetVersion(t *testing.T) {
|
2014-04-23 14:54:35 -04:00
|
|
|
eng := engine.New()
|
2014-04-03 19:14:51 -04:00
|
|
|
var called bool
|
|
|
|
eng.Register("version", func(job *engine.Job) engine.Status {
|
|
|
|
called = true
|
|
|
|
v := &engine.Env{}
|
2014-04-04 14:35:07 -04:00
|
|
|
v.SetJson("Version", "42.1")
|
2014-04-03 19:14:51 -04:00
|
|
|
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
|
|
|
|
})
|
2014-04-08 21:08:20 -04:00
|
|
|
r := serveRequest("GET", "/version", nil, eng, t)
|
2014-04-03 19:14:51 -04:00
|
|
|
if !called {
|
|
|
|
t.Fatalf("handler was not called")
|
|
|
|
}
|
2014-04-08 21:08:20 -04:00
|
|
|
v := readEnv(r.Body, t)
|
|
|
|
if v.Get("Version") != "42.1" {
|
|
|
|
t.Fatalf("%#v\n", v)
|
2014-04-03 19:14:51 -04:00
|
|
|
}
|
2014-04-08 21:08:20 -04:00
|
|
|
if r.HeaderMap.Get("Content-Type") != "application/json" {
|
|
|
|
t.Fatalf("%#v\n", r)
|
2014-04-03 19:14:51 -04:00
|
|
|
}
|
|
|
|
}
|
2014-04-03 20:02:44 -04:00
|
|
|
|
|
|
|
func TestGetInfo(t *testing.T) {
|
2014-04-23 14:54:35 -04:00
|
|
|
eng := engine.New()
|
2014-04-03 20:02:44 -04:00
|
|
|
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
|
|
|
|
})
|
2014-04-08 21:08:20 -04:00
|
|
|
r := serveRequest("GET", "/info", nil, eng, t)
|
|
|
|
if !called {
|
|
|
|
t.Fatalf("handler was not called")
|
|
|
|
}
|
|
|
|
v := readEnv(r.Body, t)
|
|
|
|
if v.GetInt("Images") != 42000 {
|
|
|
|
t.Fatalf("%#v\n", v)
|
|
|
|
}
|
|
|
|
if v.GetInt("Containers") != 1 {
|
|
|
|
t.Fatalf("%#v\n", v)
|
|
|
|
}
|
|
|
|
if r.HeaderMap.Get("Content-Type") != "application/json" {
|
|
|
|
t.Fatalf("%#v\n", r)
|
|
|
|
}
|
|
|
|
}
|
2014-04-03 20:02:44 -04:00
|
|
|
|
2014-04-08 21:08:20 -04:00
|
|
|
func serveRequest(method, target string, body io.Reader, eng *engine.Engine, t *testing.T) *httptest.ResponseRecorder {
|
2014-04-03 20:02:44 -04:00
|
|
|
r := httptest.NewRecorder()
|
2014-04-08 21:08:20 -04:00
|
|
|
req, err := http.NewRequest(method, target, body)
|
2014-04-03 20:02:44 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := ServeRequest(eng, api.APIVERSION, r, req); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-04-08 21:08:20 -04:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func readEnv(src io.Reader, t *testing.T) *engine.Env {
|
2014-04-03 20:02:44 -04:00
|
|
|
out := engine.NewOutput()
|
2014-04-08 21:08:20 -04:00
|
|
|
v, err := out.AddEnv()
|
2014-04-03 20:02:44 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-04-08 21:08:20 -04:00
|
|
|
if _, err := io.Copy(out, src); err != nil {
|
2014-04-03 20:02:44 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
out.Close()
|
2014-04-08 21:08:20 -04:00
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
func toJson(data interface{}, t *testing.T) io.Reader {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
if err := json.NewEncoder(&buf).Encode(data); err != nil {
|
|
|
|
t.Fatal(err)
|
2014-04-03 20:02:44 -04:00
|
|
|
}
|
2014-04-08 21:08:20 -04:00
|
|
|
return &buf
|
2014-04-03 20:02:44 -04:00
|
|
|
}
|