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
|
|
|
"io"
|
2014-03-28 19:36:33 -04:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2014-07-02 04:01:19 -04:00
|
|
|
"reflect"
|
2014-06-18 15:11:04 -04:00
|
|
|
"strings"
|
2014-03-28 19:36:33 -04:00
|
|
|
"testing"
|
2014-06-18 15:11:04 -04:00
|
|
|
|
2014-07-24 18:19:50 -04:00
|
|
|
"github.com/docker/docker/api"
|
|
|
|
"github.com/docker/docker/engine"
|
|
|
|
"github.com/docker/docker/pkg/version"
|
2014-03-28 19:36:33 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
2015-03-25 03:44:12 -04:00
|
|
|
eng.Register("version", func(job *engine.Job) error {
|
2014-04-03 19:14:51 -04:00
|
|
|
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 {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-04-03 19:14:51 -04:00
|
|
|
}
|
2015-03-25 03:44:12 -04:00
|
|
|
return nil
|
2014-04-03 19:14:51 -04:00
|
|
|
})
|
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
|
2015-03-25 03:44:12 -04:00
|
|
|
eng.Register("info", func(job *engine.Job) error {
|
2014-04-03 20:02:44 -04:00
|
|
|
called = true
|
|
|
|
v := &engine.Env{}
|
|
|
|
v.SetInt("Containers", 1)
|
|
|
|
v.SetInt("Images", 42000)
|
|
|
|
if _, err := v.WriteTo(job.Stdout); err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-04-03 20:02:44 -04:00
|
|
|
}
|
2015-03-25 03:44:12 -04:00
|
|
|
return nil
|
2014-04-03 20:02:44 -04:00
|
|
|
})
|
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)
|
|
|
|
}
|
2014-07-02 04:01:19 -04:00
|
|
|
assertContentType(r, "application/json", t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetImagesJSON(t *testing.T) {
|
|
|
|
eng := engine.New()
|
|
|
|
var called bool
|
2015-03-25 03:44:12 -04:00
|
|
|
eng.Register("images", func(job *engine.Job) error {
|
2014-07-02 04:01:19 -04:00
|
|
|
called = true
|
|
|
|
v := createEnvFromGetImagesJSONStruct(sampleImage)
|
|
|
|
if _, err := v.WriteTo(job.Stdout); err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-07-02 04:01:19 -04:00
|
|
|
}
|
2015-03-25 03:44:12 -04:00
|
|
|
return nil
|
2014-07-02 04:01:19 -04:00
|
|
|
})
|
|
|
|
r := serveRequest("GET", "/images/json", nil, eng, t)
|
|
|
|
if !called {
|
|
|
|
t.Fatal("handler was not called")
|
|
|
|
}
|
|
|
|
assertHttpNotError(r, t)
|
|
|
|
assertContentType(r, "application/json", t)
|
|
|
|
var observed getImagesJSONStruct
|
|
|
|
if err := json.Unmarshal(r.Body.Bytes(), &observed); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(observed, sampleImage) {
|
|
|
|
t.Errorf("Expected %#v but got %#v", sampleImage, observed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetImagesJSONFilter(t *testing.T) {
|
|
|
|
eng := engine.New()
|
|
|
|
filter := "nothing"
|
2015-03-25 03:44:12 -04:00
|
|
|
eng.Register("images", func(job *engine.Job) error {
|
2014-07-02 04:01:19 -04:00
|
|
|
filter = job.Getenv("filter")
|
2015-03-25 03:44:12 -04:00
|
|
|
return nil
|
2014-07-02 04:01:19 -04:00
|
|
|
})
|
|
|
|
serveRequest("GET", "/images/json?filter=aaaa", nil, eng, t)
|
|
|
|
if filter != "aaaa" {
|
|
|
|
t.Errorf("%#v", filter)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetImagesJSONFilters(t *testing.T) {
|
|
|
|
eng := engine.New()
|
|
|
|
filter := "nothing"
|
2015-03-25 03:44:12 -04:00
|
|
|
eng.Register("images", func(job *engine.Job) error {
|
2014-07-02 04:01:19 -04:00
|
|
|
filter = job.Getenv("filters")
|
2015-03-25 03:44:12 -04:00
|
|
|
return nil
|
2014-07-02 04:01:19 -04:00
|
|
|
})
|
|
|
|
serveRequest("GET", "/images/json?filters=nnnn", nil, eng, t)
|
|
|
|
if filter != "nnnn" {
|
|
|
|
t.Errorf("%#v", filter)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetImagesJSONAll(t *testing.T) {
|
|
|
|
eng := engine.New()
|
|
|
|
allFilter := "-1"
|
2015-03-25 03:44:12 -04:00
|
|
|
eng.Register("images", func(job *engine.Job) error {
|
2014-07-02 04:01:19 -04:00
|
|
|
allFilter = job.Getenv("all")
|
2015-03-25 03:44:12 -04:00
|
|
|
return nil
|
2014-07-02 04:01:19 -04:00
|
|
|
})
|
|
|
|
serveRequest("GET", "/images/json?all=1", nil, eng, t)
|
|
|
|
if allFilter != "1" {
|
|
|
|
t.Errorf("%#v", allFilter)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetImagesJSONLegacyFormat(t *testing.T) {
|
|
|
|
eng := engine.New()
|
|
|
|
var called bool
|
2015-03-25 03:44:12 -04:00
|
|
|
eng.Register("images", func(job *engine.Job) error {
|
2014-07-02 04:01:19 -04:00
|
|
|
called = true
|
|
|
|
outsLegacy := engine.NewTable("Created", 0)
|
|
|
|
outsLegacy.Add(createEnvFromGetImagesJSONStruct(sampleImage))
|
|
|
|
if _, err := outsLegacy.WriteListTo(job.Stdout); err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-07-02 04:01:19 -04:00
|
|
|
}
|
2015-03-25 03:44:12 -04:00
|
|
|
return nil
|
2014-07-02 04:01:19 -04:00
|
|
|
})
|
|
|
|
r := serveRequestUsingVersion("GET", "/images/json", "1.6", nil, eng, t)
|
|
|
|
if !called {
|
|
|
|
t.Fatal("handler was not called")
|
|
|
|
}
|
|
|
|
assertHttpNotError(r, t)
|
|
|
|
assertContentType(r, "application/json", t)
|
|
|
|
images := engine.NewTable("Created", 0)
|
|
|
|
if _, err := images.ReadListFrom(r.Body.Bytes()); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if images.Len() != 1 {
|
|
|
|
t.Fatalf("Expected 1 image, %d found", images.Len())
|
|
|
|
}
|
|
|
|
image := images.Data[0]
|
|
|
|
if image.Get("Tag") != "test-tag" {
|
|
|
|
t.Errorf("Expected tag 'test-tag', found '%s'", image.Get("Tag"))
|
|
|
|
}
|
|
|
|
if image.Get("Repository") != "test-name" {
|
|
|
|
t.Errorf("Expected repository 'test-name', found '%s'", image.Get("Repository"))
|
2014-04-08 21:08:20 -04:00
|
|
|
}
|
|
|
|
}
|
2014-04-03 20:02:44 -04:00
|
|
|
|
2014-06-11 11:54:43 -04:00
|
|
|
func TestGetContainersByName(t *testing.T) {
|
|
|
|
eng := engine.New()
|
|
|
|
name := "container_name"
|
|
|
|
var called bool
|
2015-03-25 03:44:12 -04:00
|
|
|
eng.Register("container_inspect", func(job *engine.Job) error {
|
2014-06-11 11:54:43 -04:00
|
|
|
called = true
|
|
|
|
if job.Args[0] != name {
|
2014-07-02 04:01:19 -04:00
|
|
|
t.Errorf("name != '%s': %#v", name, job.Args[0])
|
2014-06-11 11:54:43 -04:00
|
|
|
}
|
|
|
|
if api.APIVERSION.LessThan("1.12") && !job.GetenvBool("dirty") {
|
2014-07-02 04:01:19 -04:00
|
|
|
t.Errorf("dirty env variable not set")
|
2014-06-11 11:54:43 -04:00
|
|
|
} else if api.APIVERSION.GreaterThanOrEqualTo("1.12") && job.GetenvBool("dirty") {
|
2014-07-02 04:01:19 -04:00
|
|
|
t.Errorf("dirty env variable set when it shouldn't")
|
2014-06-11 11:54:43 -04:00
|
|
|
}
|
|
|
|
v := &engine.Env{}
|
|
|
|
v.SetBool("dirty", true)
|
|
|
|
if _, err := v.WriteTo(job.Stdout); err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-06-11 11:54:43 -04:00
|
|
|
}
|
2015-03-25 03:44:12 -04:00
|
|
|
return nil
|
2014-06-11 11:54:43 -04:00
|
|
|
})
|
|
|
|
r := serveRequest("GET", "/containers/"+name+"/json", nil, eng, t)
|
|
|
|
if !called {
|
|
|
|
t.Fatal("handler was not called")
|
|
|
|
}
|
2014-07-02 04:01:19 -04:00
|
|
|
assertContentType(r, "application/json", t)
|
2014-06-11 11:54:43 -04:00
|
|
|
var stdoutJson interface{}
|
|
|
|
if err := json.Unmarshal(r.Body.Bytes(), &stdoutJson); err != nil {
|
|
|
|
t.Fatalf("%#v", err)
|
|
|
|
}
|
|
|
|
if stdoutJson.(map[string]interface{})["dirty"].(float64) != 1 {
|
|
|
|
t.Fatalf("%#v", stdoutJson)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-05 06:32:19 -04:00
|
|
|
func TestGetEvents(t *testing.T) {
|
|
|
|
eng := engine.New()
|
|
|
|
var called bool
|
2015-03-25 03:44:12 -04:00
|
|
|
eng.Register("events", func(job *engine.Job) error {
|
2014-06-05 06:32:19 -04:00
|
|
|
called = true
|
|
|
|
since := job.Getenv("since")
|
|
|
|
if since != "1" {
|
|
|
|
t.Fatalf("'since' should be 1, found %#v instead", since)
|
|
|
|
}
|
|
|
|
until := job.Getenv("until")
|
|
|
|
if until != "0" {
|
|
|
|
t.Fatalf("'until' should be 0, found %#v instead", until)
|
|
|
|
}
|
|
|
|
v := &engine.Env{}
|
|
|
|
v.Set("since", since)
|
|
|
|
v.Set("until", until)
|
|
|
|
if _, err := v.WriteTo(job.Stdout); err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-06-05 06:32:19 -04:00
|
|
|
}
|
2015-03-25 03:44:12 -04:00
|
|
|
return nil
|
2014-06-05 06:32:19 -04:00
|
|
|
})
|
|
|
|
r := serveRequest("GET", "/events?since=1&until=0", nil, eng, t)
|
|
|
|
if !called {
|
|
|
|
t.Fatal("handler was not called")
|
|
|
|
}
|
2014-10-09 16:17:47 -04:00
|
|
|
assertContentType(r, "application/json", t)
|
2015-03-25 21:40:23 -04:00
|
|
|
var stdoutJSON struct {
|
2014-06-05 06:32:19 -04:00
|
|
|
Since int
|
|
|
|
Until int
|
|
|
|
}
|
2015-03-25 21:40:23 -04:00
|
|
|
if err := json.Unmarshal(r.Body.Bytes(), &stdoutJSON); err != nil {
|
2014-07-02 04:01:19 -04:00
|
|
|
t.Fatal(err)
|
2014-06-05 06:32:19 -04:00
|
|
|
}
|
2015-03-25 21:40:23 -04:00
|
|
|
if stdoutJSON.Since != 1 {
|
|
|
|
t.Errorf("since != 1: %#v", stdoutJSON.Since)
|
2014-06-05 06:32:19 -04:00
|
|
|
}
|
2015-03-25 21:40:23 -04:00
|
|
|
if stdoutJSON.Until != 0 {
|
|
|
|
t.Errorf("until != 0: %#v", stdoutJSON.Until)
|
2014-06-05 06:32:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-18 15:11:04 -04:00
|
|
|
func TestLogs(t *testing.T) {
|
|
|
|
eng := engine.New()
|
|
|
|
var inspect bool
|
|
|
|
var logs bool
|
2015-03-25 03:44:12 -04:00
|
|
|
eng.Register("container_inspect", func(job *engine.Job) error {
|
2014-06-18 15:11:04 -04:00
|
|
|
inspect = true
|
|
|
|
if len(job.Args) == 0 {
|
|
|
|
t.Fatal("Job arguments is empty")
|
|
|
|
}
|
|
|
|
if job.Args[0] != "test" {
|
|
|
|
t.Fatalf("Container name %s, must be test", job.Args[0])
|
|
|
|
}
|
2015-03-25 03:44:12 -04:00
|
|
|
return nil
|
2014-06-18 15:11:04 -04:00
|
|
|
})
|
|
|
|
expected := "logs"
|
2015-03-25 03:44:12 -04:00
|
|
|
eng.Register("logs", func(job *engine.Job) error {
|
2014-06-18 15:11:04 -04:00
|
|
|
logs = true
|
|
|
|
if len(job.Args) == 0 {
|
|
|
|
t.Fatal("Job arguments is empty")
|
|
|
|
}
|
|
|
|
if job.Args[0] != "test" {
|
|
|
|
t.Fatalf("Container name %s, must be test", job.Args[0])
|
|
|
|
}
|
|
|
|
follow := job.Getenv("follow")
|
|
|
|
if follow != "1" {
|
|
|
|
t.Fatalf("follow: %s, must be 1", follow)
|
|
|
|
}
|
|
|
|
stdout := job.Getenv("stdout")
|
|
|
|
if stdout != "1" {
|
|
|
|
t.Fatalf("stdout %s, must be 1", stdout)
|
|
|
|
}
|
|
|
|
stderr := job.Getenv("stderr")
|
|
|
|
if stderr != "" {
|
|
|
|
t.Fatalf("stderr %s, must be empty", stderr)
|
|
|
|
}
|
|
|
|
timestamps := job.Getenv("timestamps")
|
|
|
|
if timestamps != "1" {
|
|
|
|
t.Fatalf("timestamps %s, must be 1", timestamps)
|
|
|
|
}
|
|
|
|
job.Stdout.Write([]byte(expected))
|
2015-03-25 03:44:12 -04:00
|
|
|
return nil
|
2014-06-18 15:11:04 -04:00
|
|
|
})
|
|
|
|
r := serveRequest("GET", "/containers/test/logs?follow=1&stdout=1×tamps=1", nil, eng, t)
|
|
|
|
if r.Code != http.StatusOK {
|
|
|
|
t.Fatalf("Got status %d, expected %d", r.Code, http.StatusOK)
|
|
|
|
}
|
|
|
|
if !inspect {
|
|
|
|
t.Fatal("container_inspect job was not called")
|
|
|
|
}
|
|
|
|
if !logs {
|
|
|
|
t.Fatal("logs job was not called")
|
|
|
|
}
|
|
|
|
res := r.Body.String()
|
|
|
|
if res != expected {
|
|
|
|
t.Fatalf("Output %s, expected %s", res, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLogsNoStreams(t *testing.T) {
|
|
|
|
eng := engine.New()
|
|
|
|
var inspect bool
|
|
|
|
var logs bool
|
2015-03-25 03:44:12 -04:00
|
|
|
eng.Register("container_inspect", func(job *engine.Job) error {
|
2014-06-18 15:11:04 -04:00
|
|
|
inspect = true
|
|
|
|
if len(job.Args) == 0 {
|
|
|
|
t.Fatal("Job arguments is empty")
|
|
|
|
}
|
|
|
|
if job.Args[0] != "test" {
|
|
|
|
t.Fatalf("Container name %s, must be test", job.Args[0])
|
|
|
|
}
|
2015-03-25 03:44:12 -04:00
|
|
|
return nil
|
2014-06-18 15:11:04 -04:00
|
|
|
})
|
2015-03-25 03:44:12 -04:00
|
|
|
eng.Register("logs", func(job *engine.Job) error {
|
2014-06-18 15:11:04 -04:00
|
|
|
logs = true
|
2015-03-25 03:44:12 -04:00
|
|
|
return nil
|
2014-06-18 15:11:04 -04:00
|
|
|
})
|
|
|
|
r := serveRequest("GET", "/containers/test/logs", nil, eng, t)
|
|
|
|
if r.Code != http.StatusBadRequest {
|
|
|
|
t.Fatalf("Got status %d, expected %d", r.Code, http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
if inspect {
|
|
|
|
t.Fatal("container_inspect job was called, but it shouldn't")
|
|
|
|
}
|
|
|
|
if logs {
|
|
|
|
t.Fatal("logs job was called, but it shouldn't")
|
|
|
|
}
|
|
|
|
res := strings.TrimSpace(r.Body.String())
|
|
|
|
expected := "Bad parameters: you must choose at least one stream"
|
|
|
|
if !strings.Contains(res, expected) {
|
|
|
|
t.Fatalf("Output %s, expected %s in it", res, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-29 19:31:16 -04:00
|
|
|
func TestGetImagesHistory(t *testing.T) {
|
|
|
|
eng := engine.New()
|
|
|
|
imageName := "docker-test-image"
|
|
|
|
var called bool
|
2015-03-25 03:44:12 -04:00
|
|
|
eng.Register("history", func(job *engine.Job) error {
|
2014-06-29 19:31:16 -04:00
|
|
|
called = true
|
2014-06-30 05:36:46 -04:00
|
|
|
if len(job.Args) == 0 {
|
|
|
|
t.Fatal("Job arguments is empty")
|
|
|
|
}
|
2014-06-29 19:31:16 -04:00
|
|
|
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 {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-06-29 19:31:16 -04:00
|
|
|
}
|
2015-03-25 03:44:12 -04:00
|
|
|
return nil
|
2014-06-29 19:31:16 -04:00
|
|
|
})
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-01 21:52:59 -04:00
|
|
|
func TestGetImagesByName(t *testing.T) {
|
|
|
|
eng := engine.New()
|
|
|
|
name := "image_name"
|
|
|
|
var called bool
|
2015-03-25 03:44:12 -04:00
|
|
|
eng.Register("image_inspect", func(job *engine.Job) error {
|
2014-07-01 21:52:59 -04:00
|
|
|
called = true
|
|
|
|
if job.Args[0] != name {
|
|
|
|
t.Fatalf("name != '%s': %#v", name, job.Args[0])
|
|
|
|
}
|
|
|
|
if api.APIVERSION.LessThan("1.12") && !job.GetenvBool("dirty") {
|
|
|
|
t.Fatal("dirty env variable not set")
|
|
|
|
} else if api.APIVERSION.GreaterThanOrEqualTo("1.12") && job.GetenvBool("dirty") {
|
|
|
|
t.Fatal("dirty env variable set when it shouldn't")
|
|
|
|
}
|
|
|
|
v := &engine.Env{}
|
|
|
|
v.SetBool("dirty", true)
|
|
|
|
if _, err := v.WriteTo(job.Stdout); err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-07-01 21:52:59 -04:00
|
|
|
}
|
2015-03-25 03:44:12 -04:00
|
|
|
return nil
|
2014-07-01 21:52:59 -04:00
|
|
|
})
|
|
|
|
r := serveRequest("GET", "/images/"+name+"/json", nil, eng, t)
|
|
|
|
if !called {
|
|
|
|
t.Fatal("handler was not called")
|
|
|
|
}
|
|
|
|
if r.HeaderMap.Get("Content-Type") != "application/json" {
|
|
|
|
t.Fatalf("%#v\n", r)
|
|
|
|
}
|
|
|
|
var stdoutJson interface{}
|
|
|
|
if err := json.Unmarshal(r.Body.Bytes(), &stdoutJson); err != nil {
|
|
|
|
t.Fatalf("%#v", err)
|
|
|
|
}
|
|
|
|
if stdoutJson.(map[string]interface{})["dirty"].(float64) != 1 {
|
|
|
|
t.Fatalf("%#v", stdoutJson)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-06 16:44:56 -04:00
|
|
|
func TestDeleteContainers(t *testing.T) {
|
|
|
|
eng := engine.New()
|
|
|
|
name := "foo"
|
|
|
|
var called bool
|
2015-03-25 03:44:12 -04:00
|
|
|
eng.Register("rm", func(job *engine.Job) error {
|
2014-07-06 16:44:56 -04:00
|
|
|
called = true
|
|
|
|
if len(job.Args) == 0 {
|
|
|
|
t.Fatalf("Job arguments is empty")
|
|
|
|
}
|
|
|
|
if job.Args[0] != name {
|
|
|
|
t.Fatalf("name != '%s': %#v", name, job.Args[0])
|
|
|
|
}
|
2015-03-25 03:44:12 -04:00
|
|
|
return nil
|
2014-07-06 16:44:56 -04:00
|
|
|
})
|
|
|
|
r := serveRequest("DELETE", "/containers/"+name, nil, eng, t)
|
|
|
|
if !called {
|
|
|
|
t.Fatalf("handler was not called")
|
|
|
|
}
|
|
|
|
if r.Code != http.StatusNoContent {
|
|
|
|
t.Fatalf("Got status %d, expected %d", r.Code, http.StatusNoContent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-07-02 04:01:19 -04:00
|
|
|
return serveRequestUsingVersion(method, target, api.APIVERSION, body, eng, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func serveRequestUsingVersion(method, target string, version version.Version, 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)
|
|
|
|
}
|
2015-01-13 16:41:34 -05:00
|
|
|
ServeRequest(eng, version, r, req)
|
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
|
|
|
}
|
2014-07-02 04:01:19 -04:00
|
|
|
|
2015-03-25 21:40:23 -04:00
|
|
|
func assertContentType(recorder *httptest.ResponseRecorder, contentType string, t *testing.T) {
|
|
|
|
if recorder.HeaderMap.Get("Content-Type") != contentType {
|
2014-07-02 04:01:19 -04:00
|
|
|
t.Fatalf("%#v\n", recorder)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: Duplicated from integration/utils_test.go, but maybe that's OK as that
|
|
|
|
// should die as soon as we converted all integration tests?
|
|
|
|
// assertHttpNotError expect the given response to not have an error.
|
|
|
|
// Otherwise the it causes the test to fail.
|
|
|
|
func assertHttpNotError(r *httptest.ResponseRecorder, t *testing.T) {
|
|
|
|
// Non-error http status are [200, 400)
|
|
|
|
if r.Code < http.StatusOK || r.Code >= http.StatusBadRequest {
|
|
|
|
t.Fatal(fmt.Errorf("Unexpected http error: %v", r.Code))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createEnvFromGetImagesJSONStruct(data getImagesJSONStruct) *engine.Env {
|
|
|
|
v := &engine.Env{}
|
|
|
|
v.SetList("RepoTags", data.RepoTags)
|
|
|
|
v.Set("Id", data.Id)
|
|
|
|
v.SetInt64("Created", data.Created)
|
|
|
|
v.SetInt64("Size", data.Size)
|
|
|
|
v.SetInt64("VirtualSize", data.VirtualSize)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
type getImagesJSONStruct struct {
|
|
|
|
RepoTags []string
|
|
|
|
Id string
|
|
|
|
Created int64
|
|
|
|
Size int64
|
|
|
|
VirtualSize int64
|
|
|
|
}
|
|
|
|
|
|
|
|
var sampleImage getImagesJSONStruct = getImagesJSONStruct{
|
|
|
|
RepoTags: []string{"test-name:test-tag"},
|
|
|
|
Id: "ID",
|
|
|
|
Created: 999,
|
|
|
|
Size: 777,
|
|
|
|
VirtualSize: 666,
|
|
|
|
}
|