moby/api_test.go

1271 lines
29 KiB
Go
Raw Normal View History

package docker
import (
2013-05-10 19:28:07 +00:00
"archive/tar"
2013-05-10 04:54:43 +00:00
"bufio"
2013-05-09 22:47:06 +00:00
"bytes"
"encoding/json"
"github.com/dotcloud/docker/utils"
2013-05-10 04:54:43 +00:00
"io"
"net"
2013-05-09 22:47:06 +00:00
"net/http"
"net/http/httptest"
2013-05-10 02:19:24 +00:00
"os"
"path"
"testing"
2013-05-10 02:19:24 +00:00
"time"
)
2013-06-20 14:34:58 +00:00
func TestGetBoolParam(t *testing.T) {
if ret, err := getBoolParam("true"); err != nil || !ret {
2013-06-27 04:33:55 +00:00
t.Fatalf("true -> true, nil | got %t %s", ret, err)
2013-06-20 14:34:58 +00:00
}
if ret, err := getBoolParam("True"); err != nil || !ret {
2013-06-27 04:33:55 +00:00
t.Fatalf("True -> true, nil | got %t %s", ret, err)
2013-06-20 14:34:58 +00:00
}
if ret, err := getBoolParam("1"); err != nil || !ret {
2013-06-27 04:33:55 +00:00
t.Fatalf("1 -> true, nil | got %t %s", ret, err)
2013-06-20 14:34:58 +00:00
}
if ret, err := getBoolParam(""); err != nil || ret {
2013-06-27 04:33:55 +00:00
t.Fatalf("\"\" -> false, nil | got %t %s", ret, err)
2013-06-20 14:34:58 +00:00
}
if ret, err := getBoolParam("false"); err != nil || ret {
2013-06-27 04:33:55 +00:00
t.Fatalf("false -> false, nil | got %t %s", ret, err)
2013-06-20 14:34:58 +00:00
}
if ret, err := getBoolParam("0"); err != nil || ret {
2013-06-27 04:33:55 +00:00
t.Fatalf("0 -> false, nil | got %t %s", ret, err)
2013-06-20 14:34:58 +00:00
}
if ret, err := getBoolParam("faux"); err == nil || ret {
2013-06-27 04:33:55 +00:00
t.Fatalf("faux -> false, err | got %t %s", ret, err)
2013-06-20 14:34:58 +00:00
}
}
2013-05-10 03:13:52 +00:00
func TestGetVersion(t *testing.T) {
var err error
runtime := mkRuntime(t)
2013-05-10 03:13:52 +00:00
defer nuke(runtime)
2013-05-10 03:13:52 +00:00
srv := &Server{runtime: runtime}
r := httptest.NewRecorder()
2013-06-04 18:00:22 +00:00
if err := getVersion(srv, APIVERSION, r, nil, nil); err != nil {
t.Fatal(err)
}
2013-06-04 18:00:22 +00:00
v := &APIVersion{}
if err = json.Unmarshal(r.Body.Bytes(), v); err != nil {
t.Fatal(err)
}
2013-05-10 03:13:52 +00:00
if v.Version != VERSION {
2013-07-02 21:47:43 +00:00
t.Errorf("Expected version %s, %s found", VERSION, v.Version)
}
}
2013-05-10 03:13:52 +00:00
func TestGetInfo(t *testing.T) {
runtime := mkRuntime(t)
2013-05-09 22:47:06 +00:00
defer nuke(runtime)
2013-05-09 22:47:06 +00:00
srv := &Server{runtime: runtime}
initialImages, err := srv.runtime.graph.All()
if err != nil {
t.Fatal(err)
}
r := httptest.NewRecorder()
2013-06-04 18:00:22 +00:00
if err := getInfo(srv, APIVERSION, r, nil, nil); err != nil {
t.Fatal(err)
}
2013-06-04 18:00:22 +00:00
infos := &APIInfo{}
err = json.Unmarshal(r.Body.Bytes(), infos)
if err != nil {
t.Fatal(err)
}
if infos.Images != len(initialImages) {
2013-07-02 21:47:43 +00:00
t.Errorf("Expected images: %d, %d found", len(initialImages), infos.Images)
}
}
2013-07-18 14:54:58 +00:00
func TestGetEvents(t *testing.T) {
runtime := mkRuntime(t)
srv := &Server{
runtime: runtime,
events: make([]utils.JSONMessage, 0, 64),
listeners: make(map[string]chan utils.JSONMessage),
}
srv.LogEvent("fakeaction", "fakeid")
srv.LogEvent("fakeaction2", "fakeid")
req, err := http.NewRequest("GET", "/events?since=1", nil)
if err != nil {
t.Fatal(err)
}
r := httptest.NewRecorder()
setTimeout(t, "", 500*time.Millisecond, func() {
if err := getEvents(srv, APIVERSION, r, req, nil); err != nil {
t.Fatal(err)
}
})
dec := json.NewDecoder(r.Body)
for i := 0; i < 2; i++ {
var jm utils.JSONMessage
if err := dec.Decode(&jm); err == io.EOF {
break
} else if err != nil {
t.Fatal(err)
}
if jm != srv.events[i] {
t.Fatalf("Event received it different than expected")
}
}
}
2013-06-04 18:00:22 +00:00
func TestGetImagesJSON(t *testing.T) {
runtime := mkRuntime(t)
defer nuke(runtime)
srv := &Server{runtime: runtime}
// all=0
initialImages, err := srv.Images(false, "")
if err != nil {
t.Fatal(err)
}
req, err := http.NewRequest("GET", "/images/json?all=0", nil)
if err != nil {
t.Fatal(err)
}
r := httptest.NewRecorder()
2013-06-04 18:00:22 +00:00
if err := getImagesJSON(srv, APIVERSION, r, req, nil); err != nil {
t.Fatal(err)
}
2013-06-04 18:00:22 +00:00
images := []APIImages{}
if err := json.Unmarshal(r.Body.Bytes(), &images); err != nil {
t.Fatal(err)
}
if len(images) != len(initialImages) {
2013-07-02 21:47:43 +00:00
t.Errorf("Expected %d image, %d found", len(initialImages), len(images))
}
found := false
for _, img := range images {
if img.Repository == unitTestImageName {
found = true
break
}
}
if !found {
2013-07-02 21:47:43 +00:00
t.Errorf("Expected image %s, %+v found", unitTestImageName, images)
}
2013-05-10 14:49:47 +00:00
r2 := httptest.NewRecorder()
// all=1
initialImages, err = srv.Images(true, "")
if err != nil {
t.Fatal(err)
}
2013-05-16 13:45:29 +00:00
req2, err := http.NewRequest("GET", "/images/json?all=true", nil)
2013-05-10 14:49:47 +00:00
if err != nil {
t.Fatal(err)
}
2013-06-04 18:00:22 +00:00
if err := getImagesJSON(srv, APIVERSION, r2, req2, nil); err != nil {
2013-05-10 14:49:47 +00:00
t.Fatal(err)
}
2013-06-04 18:00:22 +00:00
images2 := []APIImages{}
if err := json.Unmarshal(r2.Body.Bytes(), &images2); err != nil {
2013-05-10 14:49:47 +00:00
t.Fatal(err)
}
if len(images2) != len(initialImages) {
2013-07-02 21:47:43 +00:00
t.Errorf("Expected %d image, %d found", len(initialImages), len(images2))
2013-05-10 14:49:47 +00:00
}
found = false
for _, img := range images2 {
if img.ID == GetTestImage(runtime).ID {
found = true
break
}
}
if !found {
t.Errorf("Retrieved image Id differs, expected %s, received %+v", GetTestImage(runtime).ID, images2)
2013-05-10 14:49:47 +00:00
}
r3 := httptest.NewRecorder()
2013-05-10 14:49:47 +00:00
// filter=a
req3, err := http.NewRequest("GET", "/images/json?filter=aaaaaaaaaa", nil)
2013-05-10 14:49:47 +00:00
if err != nil {
t.Fatal(err)
}
2013-06-04 18:00:22 +00:00
if err := getImagesJSON(srv, APIVERSION, r3, req3, nil); err != nil {
2013-05-10 14:49:47 +00:00
t.Fatal(err)
}
2013-06-04 18:00:22 +00:00
images3 := []APIImages{}
if err := json.Unmarshal(r3.Body.Bytes(), &images3); err != nil {
2013-05-10 14:49:47 +00:00
t.Fatal(err)
}
if len(images3) != 0 {
2013-07-02 21:47:43 +00:00
t.Errorf("Expected 0 image, %d found", len(images3))
2013-05-10 14:49:47 +00:00
}
2013-05-16 13:45:29 +00:00
r4 := httptest.NewRecorder()
// all=foobar
req4, err := http.NewRequest("GET", "/images/json?all=foobar", nil)
if err != nil {
t.Fatal(err)
}
2013-06-04 18:00:22 +00:00
err = getImagesJSON(srv, APIVERSION, r4, req4, nil)
2013-05-16 13:45:29 +00:00
if err == nil {
t.Fatalf("Error expected, received none")
}
httpError(r4, err)
if r4.Code != http.StatusBadRequest {
t.Fatalf("%d Bad Request expected, received %d\n", http.StatusBadRequest, r4.Code)
}
}
2013-05-10 03:13:52 +00:00
func TestGetImagesViz(t *testing.T) {
runtime := mkRuntime(t)
2013-05-10 14:49:47 +00:00
defer nuke(runtime)
srv := &Server{runtime: runtime}
r := httptest.NewRecorder()
2013-06-04 18:00:22 +00:00
if err := getImagesViz(srv, APIVERSION, r, nil, nil); err != nil {
2013-05-10 14:49:47 +00:00
t.Fatal(err)
}
if r.Code != http.StatusOK {
t.Fatalf("%d OK expected, received %d\n", http.StatusOK, r.Code)
}
reader := bufio.NewReader(r.Body)
line, err := reader.ReadString('\n')
if err != nil {
t.Fatal(err)
}
if line != "digraph docker {\n" {
2013-07-02 21:47:43 +00:00
t.Errorf("Expected digraph docker {\n, %s found", line)
}
}
2013-05-10 03:13:52 +00:00
func TestGetImagesHistory(t *testing.T) {
runtime := mkRuntime(t)
2013-05-10 00:51:27 +00:00
defer nuke(runtime)
srv := &Server{runtime: runtime}
r := httptest.NewRecorder()
2013-06-04 18:00:22 +00:00
if err := getImagesHistory(srv, APIVERSION, r, nil, map[string]string{"name": unitTestImageName}); err != nil {
2013-05-10 00:51:27 +00:00
t.Fatal(err)
}
2013-06-04 18:00:22 +00:00
history := []APIHistory{}
if err := json.Unmarshal(r.Body.Bytes(), &history); err != nil {
2013-05-10 00:51:27 +00:00
t.Fatal(err)
}
if len(history) != 1 {
2013-07-02 21:47:43 +00:00
t.Errorf("Expected 1 line, %d found", len(history))
2013-05-10 00:51:27 +00:00
}
}
2013-05-10 03:13:52 +00:00
func TestGetImagesByName(t *testing.T) {
runtime := mkRuntime(t)
2013-05-10 00:51:27 +00:00
defer nuke(runtime)
srv := &Server{runtime: runtime}
r := httptest.NewRecorder()
2013-06-04 18:00:22 +00:00
if err := getImagesByName(srv, APIVERSION, r, nil, map[string]string{"name": unitTestImageName}); err != nil {
2013-05-10 00:51:27 +00:00
t.Fatal(err)
}
2013-05-10 03:13:52 +00:00
img := &Image{}
if err := json.Unmarshal(r.Body.Bytes(), img); err != nil {
2013-05-10 00:51:27 +00:00
t.Fatal(err)
}
if img.ID != unitTestImageID {
2013-05-10 03:13:52 +00:00
t.Errorf("Error inspecting image")
}
}
2013-05-10 00:51:27 +00:00
2013-06-04 18:00:22 +00:00
func TestGetContainersJSON(t *testing.T) {
runtime := mkRuntime(t)
2013-05-10 03:13:52 +00:00
defer nuke(runtime)
2013-05-10 00:51:27 +00:00
2013-05-10 03:13:52 +00:00
srv := &Server{runtime: runtime}
container, err := NewBuilder(runtime).Create(&Config{
2013-06-04 18:00:22 +00:00
Image: GetTestImage(runtime).ID,
2013-05-10 03:13:52 +00:00
Cmd: []string{"echo", "test"},
})
2013-05-10 00:51:27 +00:00
if err != nil {
t.Fatal(err)
}
2013-05-10 03:13:52 +00:00
defer runtime.Destroy(container)
req, err := http.NewRequest("GET", "/containers/json?all=1", nil)
2013-05-10 03:13:52 +00:00
if err != nil {
t.Fatal(err)
}
r := httptest.NewRecorder()
2013-06-04 18:00:22 +00:00
if err := getContainersJSON(srv, APIVERSION, r, req, nil); err != nil {
2013-05-10 03:13:52 +00:00
t.Fatal(err)
}
2013-06-04 18:00:22 +00:00
containers := []APIContainers{}
if err := json.Unmarshal(r.Body.Bytes(), &containers); err != nil {
2013-05-10 03:13:52 +00:00
t.Fatal(err)
}
if len(containers) != 1 {
2013-07-02 21:47:43 +00:00
t.Fatalf("Expected %d container, %d found", 1, len(containers))
2013-05-10 03:13:52 +00:00
}
2013-06-04 18:00:22 +00:00
if containers[0].ID != container.ID {
t.Fatalf("Container ID mismatch. Expected: %s, received: %s\n", container.ID, containers[0].ID)
2013-05-10 00:51:27 +00:00
}
}
2013-05-10 03:13:52 +00:00
func TestGetContainersExport(t *testing.T) {
runtime := mkRuntime(t)
2013-05-10 14:49:47 +00:00
defer nuke(runtime)
srv := &Server{runtime: runtime}
builder := NewBuilder(runtime)
// Create a container and remove a file
container, err := builder.Create(
&Config{
2013-06-04 18:00:22 +00:00
Image: GetTestImage(runtime).ID,
2013-05-10 19:28:07 +00:00
Cmd: []string{"touch", "/test"},
2013-05-10 14:49:47 +00:00
},
)
if err != nil {
t.Fatal(err)
}
defer runtime.Destroy(container)
if err := container.Run(); err != nil {
t.Fatal(err)
}
r := httptest.NewRecorder()
2013-06-04 18:00:22 +00:00
if err = getContainersExport(srv, APIVERSION, r, nil, map[string]string{"name": container.ID}); err != nil {
2013-05-10 14:49:47 +00:00
t.Fatal(err)
}
if r.Code != http.StatusOK {
t.Fatalf("%d OK expected, received %d\n", http.StatusOK, r.Code)
}
2013-05-10 19:28:07 +00:00
found := false
for tarReader := tar.NewReader(r.Body); ; {
h, err := tarReader.Next()
if err != nil {
if err == io.EOF {
break
}
t.Fatal(err)
}
if h.Name == "./test" {
found = true
break
}
}
if !found {
t.Fatalf("The created test file has not been found in the exported image")
2013-05-10 14:49:47 +00:00
}
2013-05-10 03:13:52 +00:00
}
2013-05-10 04:54:43 +00:00
func TestGetContainersChanges(t *testing.T) {
runtime := mkRuntime(t)
2013-05-10 04:54:43 +00:00
defer nuke(runtime)
srv := &Server{runtime: runtime}
2013-05-10 03:13:52 +00:00
2013-05-10 04:54:43 +00:00
builder := NewBuilder(runtime)
2013-05-10 03:13:52 +00:00
2013-05-10 04:54:43 +00:00
// Create a container and remove a file
container, err := builder.Create(
&Config{
2013-06-04 18:00:22 +00:00
Image: GetTestImage(runtime).ID,
2013-05-10 04:54:43 +00:00
Cmd: []string{"/bin/rm", "/etc/passwd"},
},
)
if err != nil {
t.Fatal(err)
}
defer runtime.Destroy(container)
2013-05-10 03:13:52 +00:00
2013-05-10 04:54:43 +00:00
if err := container.Run(); err != nil {
t.Fatal(err)
}
r := httptest.NewRecorder()
2013-06-04 18:00:22 +00:00
if err := getContainersChanges(srv, APIVERSION, r, nil, map[string]string{"name": container.ID}); err != nil {
2013-05-10 04:54:43 +00:00
t.Fatal(err)
}
changes := []Change{}
if err := json.Unmarshal(r.Body.Bytes(), &changes); err != nil {
2013-05-10 04:54:43 +00:00
t.Fatal(err)
}
// Check the changelog
success := false
for _, elem := range changes {
if elem.Path == "/etc/passwd" && elem.Kind == 2 {
success = true
}
}
if !success {
t.Fatalf("/etc/passwd as been removed but is not present in the diff")
}
}
2013-05-10 03:13:52 +00:00
2013-07-01 15:19:42 +00:00
func TestGetContainersTop(t *testing.T) {
2013-06-28 16:27:00 +00:00
runtime, err := newTestRuntime()
if err != nil {
t.Fatal(err)
}
defer nuke(runtime)
srv := &Server{runtime: runtime}
builder := NewBuilder(runtime)
container, err := builder.Create(
&Config{
Image: GetTestImage(runtime).ID,
Cmd: []string{"/bin/sh", "-c", "cat"},
OpenStdin: true,
2013-06-28 16:27:00 +00:00
},
)
if err != nil {
t.Fatal(err)
}
defer runtime.Destroy(container)
defer func() {
2013-08-12 17:53:06 +00:00
// Make sure the process dies before destroying runtime
container.stdin.Close()
container.WaitTimeout(2 * time.Second)
}()
2013-06-28 16:27:00 +00:00
hostConfig := &HostConfig{}
if err := container.Start(hostConfig); err != nil {
t.Fatal(err)
}
setTimeout(t, "Waiting for the container to be started timed out", 10*time.Second, func() {
for {
if container.State.Running {
break
}
time.Sleep(10 * time.Millisecond)
}
})
2013-06-28 16:27:00 +00:00
if !container.State.Running {
t.Fatalf("Container should be running")
2013-06-28 16:27:00 +00:00
}
// Make sure sh spawn up cat
setTimeout(t, "read/write assertion timed out", 2*time.Second, func() {
in, _ := container.StdinPipe()
out, _ := container.StdoutPipe()
if err := assertPipe("hello\n", "hello", out, in, 15); err != nil {
t.Fatal(err)
}
})
2013-06-28 16:27:00 +00:00
r := httptest.NewRecorder()
2013-07-19 10:06:32 +00:00
req, err := http.NewRequest("GET", "/"+container.ID+"/top?ps_args=u", bytes.NewReader([]byte{}))
if err != nil {
t.Fatal(err)
}
if err := getContainersTop(srv, APIVERSION, r, req, map[string]string{"name": container.ID}); err != nil {
2013-06-28 16:27:00 +00:00
t.Fatal(err)
}
2013-07-19 10:06:32 +00:00
procs := APITop{}
2013-06-28 16:27:00 +00:00
if err := json.Unmarshal(r.Body.Bytes(), &procs); err != nil {
t.Fatal(err)
}
2013-07-19 10:06:32 +00:00
if len(procs.Titles) != 11 {
t.Fatalf("Expected 11 titles, found %d.", len(procs.Titles))
2013-06-28 16:27:00 +00:00
}
2013-07-19 10:06:32 +00:00
if procs.Titles[0] != "USER" || procs.Titles[10] != "COMMAND" {
t.Fatalf("Expected Titles[0] to be USER and Titles[10] to be COMMAND, found %s and %s.", procs.Titles[0], procs.Titles[10])
2013-06-28 16:27:00 +00:00
}
2013-07-19 10:06:32 +00:00
if len(procs.Processes) != 2 {
t.Fatalf("Expected 2 processes, found %d.", len(procs.Processes))
}
if procs.Processes[0][10] != "/bin/sh" && procs.Processes[0][10] != "cat" {
t.Fatalf("Expected `cat` or `/bin/sh`, found %s.", procs.Processes[0][10])
2013-07-19 10:06:32 +00:00
}
if procs.Processes[1][10] != "/bin/sh" && procs.Processes[1][10] != "cat" {
t.Fatalf("Expected `cat` or `/bin/sh`, found %s.", procs.Processes[1][10])
2013-06-28 16:27:00 +00:00
}
}
2013-05-10 04:53:28 +00:00
func TestGetContainersByName(t *testing.T) {
runtime := mkRuntime(t)
2013-05-10 14:49:47 +00:00
defer nuke(runtime)
srv := &Server{runtime: runtime}
builder := NewBuilder(runtime)
// Create a container and remove a file
container, err := builder.Create(
&Config{
2013-06-04 18:00:22 +00:00
Image: GetTestImage(runtime).ID,
2013-05-10 19:28:07 +00:00
Cmd: []string{"echo", "test"},
2013-05-10 14:49:47 +00:00
},
)
if err != nil {
t.Fatal(err)
}
defer runtime.Destroy(container)
r := httptest.NewRecorder()
2013-06-04 18:00:22 +00:00
if err := getContainersByName(srv, APIVERSION, r, nil, map[string]string{"name": container.ID}); err != nil {
2013-05-10 14:49:47 +00:00
t.Fatal(err)
}
2013-05-10 19:28:07 +00:00
outContainer := &Container{}
if err := json.Unmarshal(r.Body.Bytes(), outContainer); err != nil {
2013-05-10 14:49:47 +00:00
t.Fatal(err)
}
2013-06-04 18:00:22 +00:00
if outContainer.ID != container.ID {
2013-08-12 17:53:06 +00:00
t.Fatalf("Wrong containers retrieved. Expected %s, received %s", container.ID, outContainer.ID)
2013-05-10 19:28:07 +00:00
}
2013-05-10 03:13:52 +00:00
}
func TestPostCommit(t *testing.T) {
runtime := mkRuntime(t)
2013-05-10 14:49:47 +00:00
defer nuke(runtime)
srv := &Server{runtime: runtime}
builder := NewBuilder(runtime)
// Create a container and remove a file
container, err := builder.Create(
&Config{
2013-06-04 18:00:22 +00:00
Image: GetTestImage(runtime).ID,
2013-05-10 19:28:07 +00:00
Cmd: []string{"touch", "/test"},
2013-05-10 14:49:47 +00:00
},
)
if err != nil {
t.Fatal(err)
}
defer runtime.Destroy(container)
if err := container.Run(); err != nil {
t.Fatal(err)
}
2013-06-04 18:00:22 +00:00
req, err := http.NewRequest("POST", "/commit?repo=testrepo&testtag=tag&container="+container.ID, bytes.NewReader([]byte{}))
2013-05-10 14:49:47 +00:00
if err != nil {
t.Fatal(err)
}
r := httptest.NewRecorder()
2013-06-04 18:00:22 +00:00
if err := postCommit(srv, APIVERSION, r, req, nil); err != nil {
2013-05-10 14:49:47 +00:00
t.Fatal(err)
}
if r.Code != http.StatusCreated {
t.Fatalf("%d Created expected, received %d\n", http.StatusCreated, r.Code)
}
2013-05-10 19:28:07 +00:00
2013-06-04 18:00:22 +00:00
apiID := &APIID{}
if err := json.Unmarshal(r.Body.Bytes(), apiID); err != nil {
2013-05-10 19:28:07 +00:00
t.Fatal(err)
}
2013-06-04 18:00:22 +00:00
if _, err := runtime.graph.Get(apiID.ID); err != nil {
2013-05-10 19:28:07 +00:00
t.Fatalf("The image has not been commited")
}
2013-05-10 03:13:52 +00:00
}
2013-05-10 02:19:24 +00:00
func TestPostContainersCreate(t *testing.T) {
runtime := mkRuntime(t)
2013-05-09 23:20:07 +00:00
defer nuke(runtime)
2013-05-09 22:47:06 +00:00
2013-05-09 23:20:07 +00:00
srv := &Server{runtime: runtime}
2013-05-09 22:47:06 +00:00
2013-06-04 18:00:22 +00:00
configJSON, err := json.Marshal(&Config{
Image: GetTestImage(runtime).ID,
2013-05-10 02:19:24 +00:00
Memory: 33554432,
Cmd: []string{"touch", "/test"},
})
2013-05-09 23:20:07 +00:00
if err != nil {
t.Fatal(err)
}
2013-05-09 22:47:06 +00:00
2013-06-04 18:00:22 +00:00
req, err := http.NewRequest("POST", "/containers/create", bytes.NewReader(configJSON))
2013-05-09 23:20:07 +00:00
if err != nil {
t.Fatal(err)
}
2013-05-09 22:47:06 +00:00
r := httptest.NewRecorder()
2013-06-04 18:00:22 +00:00
if err := postContainersCreate(srv, APIVERSION, r, req, nil); err != nil {
2013-05-09 23:20:07 +00:00
t.Fatal(err)
}
2013-05-10 02:19:24 +00:00
if r.Code != http.StatusCreated {
t.Fatalf("%d Created expected, received %d\n", http.StatusCreated, r.Code)
}
2013-05-09 22:47:06 +00:00
2013-06-04 18:00:22 +00:00
apiRun := &APIRun{}
if err := json.Unmarshal(r.Body.Bytes(), apiRun); err != nil {
2013-05-09 23:20:07 +00:00
t.Fatal(err)
}
2013-05-09 22:47:06 +00:00
2013-06-04 18:00:22 +00:00
container := srv.runtime.Get(apiRun.ID)
2013-05-10 02:19:24 +00:00
if container == nil {
t.Fatalf("Container not created")
2013-05-09 23:20:07 +00:00
}
2013-05-10 02:19:24 +00:00
if err := container.Run(); err != nil {
t.Fatal(err)
}
if _, err := os.Stat(path.Join(container.rwPath(), "test")); err != nil {
if os.IsNotExist(err) {
2013-05-14 22:37:35 +00:00
utils.Debugf("Err: %s", err)
2013-05-10 02:19:24 +00:00
t.Fatalf("The test file has not been created")
}
t.Fatal(err)
2013-05-09 23:20:07 +00:00
}
}
2013-05-10 03:13:52 +00:00
func TestPostContainersKill(t *testing.T) {
runtime := mkRuntime(t)
2013-05-10 02:19:24 +00:00
defer nuke(runtime)
2013-05-09 23:20:07 +00:00
2013-05-10 02:19:24 +00:00
srv := &Server{runtime: runtime}
2013-05-09 23:20:07 +00:00
2013-05-10 03:13:52 +00:00
container, err := NewBuilder(runtime).Create(
&Config{
2013-06-04 18:00:22 +00:00
Image: GetTestImage(runtime).ID,
2013-05-10 03:13:52 +00:00
Cmd: []string{"/bin/cat"},
OpenStdin: true,
},
)
2013-05-09 23:20:07 +00:00
if err != nil {
t.Fatal(err)
}
2013-05-10 02:19:24 +00:00
defer runtime.Destroy(container)
2013-05-09 23:20:07 +00:00
hostConfig := &HostConfig{}
if err := container.Start(hostConfig); err != nil {
2013-05-09 23:20:07 +00:00
t.Fatal(err)
}
2013-05-10 03:13:52 +00:00
// Give some time to the process to start
container.WaitTimeout(500 * time.Millisecond)
if !container.State.Running {
t.Errorf("Container should be running")
2013-05-10 02:19:24 +00:00
}
2013-05-10 03:13:52 +00:00
r := httptest.NewRecorder()
2013-06-04 18:00:22 +00:00
if err := postContainersKill(srv, APIVERSION, r, nil, map[string]string{"name": container.ID}); err != nil {
2013-05-09 23:20:07 +00:00
t.Fatal(err)
}
2013-05-10 03:13:52 +00:00
if r.Code != http.StatusNoContent {
t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
}
if container.State.Running {
t.Fatalf("The container hasn't been killed")
2013-05-09 23:20:07 +00:00
}
}
2013-05-10 03:13:52 +00:00
func TestPostContainersRestart(t *testing.T) {
runtime := mkRuntime(t)
2013-05-10 02:19:24 +00:00
defer nuke(runtime)
2013-05-09 23:20:07 +00:00
2013-05-10 02:19:24 +00:00
srv := &Server{runtime: runtime}
2013-05-09 23:20:07 +00:00
2013-05-10 02:19:24 +00:00
container, err := NewBuilder(runtime).Create(
&Config{
2013-06-04 18:00:22 +00:00
Image: GetTestImage(runtime).ID,
2013-05-10 02:19:24 +00:00
Cmd: []string{"/bin/cat"},
OpenStdin: true,
},
)
2013-05-09 23:20:07 +00:00
if err != nil {
t.Fatal(err)
}
2013-05-10 02:19:24 +00:00
defer runtime.Destroy(container)
hostConfig := &HostConfig{}
if err := container.Start(hostConfig); err != nil {
2013-05-10 03:13:52 +00:00
t.Fatal(err)
}
// Give some time to the process to start
container.WaitTimeout(500 * time.Millisecond)
if !container.State.Running {
t.Errorf("Container should be running")
}
2013-06-04 18:00:22 +00:00
req, err := http.NewRequest("POST", "/containers/"+container.ID+"/restart?t=1", bytes.NewReader([]byte{}))
2013-05-10 03:13:52 +00:00
if err != nil {
t.Fatal(err)
}
r := httptest.NewRecorder()
2013-06-04 18:00:22 +00:00
if err := postContainersRestart(srv, APIVERSION, r, req, map[string]string{"name": container.ID}); err != nil {
2013-05-09 23:20:07 +00:00
t.Fatal(err)
}
if r.Code != http.StatusNoContent {
t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
}
2013-05-10 02:19:24 +00:00
2013-05-10 03:13:52 +00:00
// Give some time to the process to restart
2013-05-10 02:19:24 +00:00
container.WaitTimeout(500 * time.Millisecond)
if !container.State.Running {
2013-05-10 03:13:52 +00:00
t.Fatalf("Container should be running")
2013-05-10 02:19:24 +00:00
}
if err := container.Kill(); err != nil {
t.Fatal(err)
}
2013-05-09 23:20:07 +00:00
}
2013-05-10 03:13:52 +00:00
func TestPostContainersStart(t *testing.T) {
runtime := mkRuntime(t)
2013-05-10 03:13:52 +00:00
defer nuke(runtime)
2013-05-09 23:20:07 +00:00
2013-05-10 03:13:52 +00:00
srv := &Server{runtime: runtime}
2013-05-09 23:20:07 +00:00
2013-05-10 03:13:52 +00:00
container, err := NewBuilder(runtime).Create(
&Config{
2013-06-04 18:00:22 +00:00
Image: GetTestImage(runtime).ID,
2013-05-10 03:13:52 +00:00
Cmd: []string{"/bin/cat"},
OpenStdin: true,
},
)
2013-05-09 23:20:07 +00:00
if err != nil {
t.Fatal(err)
}
2013-05-10 03:13:52 +00:00
defer runtime.Destroy(container)
2013-05-09 23:20:07 +00:00
hostConfigJSON, err := json.Marshal(&HostConfig{})
req, err := http.NewRequest("POST", "/containers/"+container.ID+"/start", bytes.NewReader(hostConfigJSON))
if err != nil {
t.Fatal(err)
}
2013-05-10 03:13:52 +00:00
r := httptest.NewRecorder()
if err := postContainersStart(srv, APIVERSION, r, req, map[string]string{"name": container.ID}); err != nil {
2013-05-09 23:20:07 +00:00
t.Fatal(err)
}
if r.Code != http.StatusNoContent {
t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
}
2013-05-10 03:13:52 +00:00
// Give some time to the process to start
container.WaitTimeout(500 * time.Millisecond)
if !container.State.Running {
t.Errorf("Container should be running")
}
r = httptest.NewRecorder()
if err = postContainersStart(srv, APIVERSION, r, req, map[string]string{"name": container.ID}); err == nil {
2013-08-12 17:53:06 +00:00
t.Fatalf("A running container should be able to be started")
2013-05-10 03:13:52 +00:00
}
if err := container.Kill(); err != nil {
t.Fatal(err)
}
2013-05-09 23:20:07 +00:00
}
2013-05-10 02:19:24 +00:00
func TestPostContainersStop(t *testing.T) {
runtime := mkRuntime(t)
2013-05-10 02:19:24 +00:00
defer nuke(runtime)
2013-05-09 23:20:07 +00:00
2013-05-10 02:19:24 +00:00
srv := &Server{runtime: runtime}
2013-05-09 23:20:07 +00:00
2013-05-10 02:19:24 +00:00
container, err := NewBuilder(runtime).Create(
&Config{
2013-06-04 18:00:22 +00:00
Image: GetTestImage(runtime).ID,
2013-05-10 02:19:24 +00:00
Cmd: []string{"/bin/cat"},
OpenStdin: true,
},
)
2013-05-09 23:20:07 +00:00
if err != nil {
t.Fatal(err)
}
2013-05-10 02:19:24 +00:00
defer runtime.Destroy(container)
2013-05-09 23:20:07 +00:00
hostConfig := &HostConfig{}
if err := container.Start(hostConfig); err != nil {
2013-05-09 23:20:07 +00:00
t.Fatal(err)
}
2013-05-10 02:19:24 +00:00
// Give some time to the process to start
container.WaitTimeout(500 * time.Millisecond)
if !container.State.Running {
t.Errorf("Container should be running")
}
// Note: as it is a POST request, it requires a body.
2013-06-04 18:00:22 +00:00
req, err := http.NewRequest("POST", "/containers/"+container.ID+"/stop?t=1", bytes.NewReader([]byte{}))
2013-05-10 02:19:24 +00:00
if err != nil {
t.Fatal(err)
}
r := httptest.NewRecorder()
2013-06-04 18:00:22 +00:00
if err := postContainersStop(srv, APIVERSION, r, req, map[string]string{"name": container.ID}); err != nil {
2013-05-10 02:19:24 +00:00
t.Fatal(err)
}
2013-05-09 23:20:07 +00:00
if r.Code != http.StatusNoContent {
t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
}
2013-05-10 02:19:24 +00:00
if container.State.Running {
t.Fatalf("The container hasn't been stopped")
}
2013-05-09 23:20:07 +00:00
}
2013-05-10 03:13:52 +00:00
func TestPostContainersWait(t *testing.T) {
runtime := mkRuntime(t)
2013-05-10 03:13:52 +00:00
defer nuke(runtime)
2013-05-09 23:20:07 +00:00
2013-05-10 03:13:52 +00:00
srv := &Server{runtime: runtime}
2013-05-09 23:20:07 +00:00
2013-05-10 03:13:52 +00:00
container, err := NewBuilder(runtime).Create(
&Config{
2013-06-04 18:00:22 +00:00
Image: GetTestImage(runtime).ID,
2013-05-10 03:13:52 +00:00
Cmd: []string{"/bin/sleep", "1"},
OpenStdin: true,
},
)
2013-05-09 23:20:07 +00:00
if err != nil {
t.Fatal(err)
}
2013-05-10 03:13:52 +00:00
defer runtime.Destroy(container)
2013-05-09 23:20:07 +00:00
hostConfig := &HostConfig{}
if err := container.Start(hostConfig); err != nil {
2013-05-09 23:20:07 +00:00
t.Fatal(err)
}
2013-05-10 03:13:52 +00:00
setTimeout(t, "Wait timed out", 3*time.Second, func() {
r := httptest.NewRecorder()
2013-06-04 18:00:22 +00:00
if err := postContainersWait(srv, APIVERSION, r, nil, map[string]string{"name": container.ID}); err != nil {
2013-05-10 03:13:52 +00:00
t.Fatal(err)
}
2013-06-04 18:00:22 +00:00
apiWait := &APIWait{}
if err := json.Unmarshal(r.Body.Bytes(), apiWait); err != nil {
2013-05-10 03:13:52 +00:00
t.Fatal(err)
}
if apiWait.StatusCode != 0 {
t.Fatalf("Non zero exit code for sleep: %d\n", apiWait.StatusCode)
}
})
2013-05-09 23:20:07 +00:00
2013-05-10 03:13:52 +00:00
if container.State.Running {
t.Fatalf("The container should be stopped after wait")
2013-05-09 23:20:07 +00:00
}
}
2013-05-10 03:13:52 +00:00
func TestPostContainersAttach(t *testing.T) {
runtime := mkRuntime(t)
defer nuke(runtime)
srv := &Server{runtime: runtime}
container, err := NewBuilder(runtime).Create(
&Config{
2013-06-04 18:00:22 +00:00
Image: GetTestImage(runtime).ID,
Cmd: []string{"/bin/cat"},
OpenStdin: true,
},
)
if err != nil {
t.Fatal(err)
}
defer runtime.Destroy(container)
// Start the process
hostConfig := &HostConfig{}
if err := container.Start(hostConfig); err != nil {
t.Fatal(err)
}
stdin, stdinPipe := io.Pipe()
stdout, stdoutPipe := io.Pipe()
2013-08-12 17:53:06 +00:00
// Try to avoid the timeout in destroy. Best effort, don't check error
defer func() {
closeWrap(stdin, stdinPipe, stdout, stdoutPipe)
container.Kill()
}()
// Attach to it
c1 := make(chan struct{})
go func() {
defer close(c1)
r := &hijackTester{
ResponseRecorder: httptest.NewRecorder(),
in: stdin,
out: stdoutPipe,
}
2013-06-04 18:00:22 +00:00
req, err := http.NewRequest("POST", "/containers/"+container.ID+"/attach?stream=1&stdin=1&stdout=1&stderr=1", bytes.NewReader([]byte{}))
if err != nil {
t.Fatal(err)
}
2013-06-04 18:00:22 +00:00
if err := postContainersAttach(srv, APIVERSION, r, req, map[string]string{"name": container.ID}); err != nil {
t.Fatal(err)
}
}()
// Acknowledge hijack
setTimeout(t, "hijack acknowledge timed out", 2*time.Second, func() {
stdout.Read([]byte{})
stdout.Read(make([]byte, 4096))
})
setTimeout(t, "read/write assertion timed out", 2*time.Second, func() {
if err := assertPipe("hello\n", "hello", stdout, stdinPipe, 15); err != nil {
t.Fatal(err)
}
})
// Close pipes (client disconnects)
if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
t.Fatal(err)
}
// Wait for attach to finish, the client disconnected, therefore, Attach finished his job
setTimeout(t, "Waiting for CmdAttach timed out", 10*time.Second, func() {
<-c1
})
// We closed stdin, expect /bin/cat to still be running
// Wait a little bit to make sure container.monitor() did his thing
err = container.WaitTimeout(500 * time.Millisecond)
if err == nil || !container.State.Running {
t.Fatalf("/bin/cat is not running after closing stdin")
}
2013-08-12 17:53:06 +00:00
// Try to avoid the timeout in destroy. Best effort, don't check error
cStdin, _ := container.StdinPipe()
cStdin.Close()
container.Wait()
2013-05-10 03:13:52 +00:00
}
2013-05-10 19:28:07 +00:00
// FIXME: Test deleting running container
2013-05-10 02:19:24 +00:00
// FIXME: Test deleting container with volume
// FIXME: Test deleting volume in use by other container
func TestDeleteContainers(t *testing.T) {
runtime := mkRuntime(t)
2013-05-10 02:19:24 +00:00
defer nuke(runtime)
2013-05-09 23:20:07 +00:00
2013-05-10 02:19:24 +00:00
srv := &Server{runtime: runtime}
2013-05-09 23:20:07 +00:00
2013-05-10 02:19:24 +00:00
container, err := NewBuilder(runtime).Create(&Config{
2013-06-04 18:00:22 +00:00
Image: GetTestImage(runtime).ID,
2013-05-10 02:19:24 +00:00
Cmd: []string{"touch", "/test"},
})
2013-05-09 23:20:07 +00:00
if err != nil {
t.Fatal(err)
}
2013-05-10 02:19:24 +00:00
defer runtime.Destroy(container)
if err := container.Run(); err != nil {
t.Fatal(err)
}
2013-06-04 18:00:22 +00:00
req, err := http.NewRequest("DELETE", "/containers/"+container.ID, nil)
2013-05-09 23:20:07 +00:00
if err != nil {
t.Fatal(err)
}
r := httptest.NewRecorder()
2013-06-04 18:00:22 +00:00
if err := deleteContainers(srv, APIVERSION, r, req, map[string]string{"name": container.ID}); err != nil {
2013-05-10 02:19:24 +00:00
t.Fatal(err)
}
2013-05-09 23:20:07 +00:00
if r.Code != http.StatusNoContent {
t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
}
2013-05-10 02:19:24 +00:00
2013-06-04 18:00:22 +00:00
if c := runtime.Get(container.ID); c != nil {
2013-05-10 02:19:24 +00:00
t.Fatalf("The container as not been deleted")
}
if _, err := os.Stat(path.Join(container.rwPath(), "test")); err == nil {
t.Fatalf("The test file has not been deleted")
}
2013-05-09 23:20:07 +00:00
}
func TestOptionsRoute(t *testing.T) {
runtime := mkRuntime(t)
defer nuke(runtime)
srv := &Server{runtime: runtime, enableCors: true}
r := httptest.NewRecorder()
router, err := createRouter(srv, false)
if err != nil {
t.Fatal(err)
}
req, err := http.NewRequest("OPTIONS", "/", nil)
if err != nil {
t.Fatal(err)
}
router.ServeHTTP(r, req)
if r.Code != http.StatusOK {
t.Errorf("Expected response for OPTIONS request to be \"200\", %v found.", r.Code)
}
}
func TestGetEnabledCors(t *testing.T) {
runtime := mkRuntime(t)
defer nuke(runtime)
srv := &Server{runtime: runtime, enableCors: true}
r := httptest.NewRecorder()
router, err := createRouter(srv, false)
if err != nil {
t.Fatal(err)
}
req, err := http.NewRequest("GET", "/version", nil)
if err != nil {
t.Fatal(err)
}
router.ServeHTTP(r, req)
if r.Code != http.StatusOK {
t.Errorf("Expected response for OPTIONS request to be \"200\", %v found.", r.Code)
}
allowOrigin := r.Header().Get("Access-Control-Allow-Origin")
allowHeaders := r.Header().Get("Access-Control-Allow-Headers")
allowMethods := r.Header().Get("Access-Control-Allow-Methods")
if allowOrigin != "*" {
t.Errorf("Expected header Access-Control-Allow-Origin to be \"*\", %s found.", allowOrigin)
}
if allowHeaders != "Origin, X-Requested-With, Content-Type, Accept" {
t.Errorf("Expected header Access-Control-Allow-Headers to be \"Origin, X-Requested-With, Content-Type, Accept\", %s found.", allowHeaders)
}
if allowMethods != "GET, POST, DELETE, PUT, OPTIONS" {
t.Errorf("Expected hearder Access-Control-Allow-Methods to be \"GET, POST, DELETE, PUT, OPTIONS\", %s found.", allowMethods)
}
}
2013-05-10 03:13:52 +00:00
func TestDeleteImages(t *testing.T) {
runtime := mkRuntime(t)
2013-05-15 13:11:39 +00:00
defer nuke(runtime)
srv := &Server{runtime: runtime}
initialImages, err := srv.Images(false, "")
if err != nil {
t.Fatal(err)
}
2013-05-15 13:11:39 +00:00
if err := srv.runtime.repositories.Set("test", "test", unitTestImageName, true); err != nil {
t.Fatal(err)
}
images, err := srv.Images(false, "")
if err != nil {
t.Fatal(err)
}
if len(images) != len(initialImages)+1 {
2013-07-02 21:47:43 +00:00
t.Errorf("Expected %d images, %d found", len(initialImages)+1, len(images))
2013-05-15 13:11:39 +00:00
}
2013-07-05 16:58:39 +00:00
req, err := http.NewRequest("DELETE", "/images/"+unitTestImageID, nil)
if err != nil {
t.Fatal(err)
}
2013-05-15 13:11:39 +00:00
r := httptest.NewRecorder()
2013-07-05 16:58:39 +00:00
if err := deleteImages(srv, APIVERSION, r, req, map[string]string{"name": unitTestImageID}); err == nil {
t.Fatalf("Expected conflict error, got none")
}
req2, err := http.NewRequest("DELETE", "/images/test:test", nil)
if err != nil {
2013-05-15 13:11:39 +00:00
t.Fatal(err)
}
2013-07-05 16:58:39 +00:00
r2 := httptest.NewRecorder()
if err := deleteImages(srv, APIVERSION, r2, req2, map[string]string{"name": "test:test"}); err != nil {
t.Fatal(err)
}
if r2.Code != http.StatusOK {
t.Fatalf("%d OK expected, received %d\n", http.StatusOK, r.Code)
2013-05-15 13:11:39 +00:00
}
2013-06-10 21:05:54 +00:00
var outs []APIRmi
2013-07-05 16:58:39 +00:00
if err := json.Unmarshal(r2.Body.Bytes(), &outs); err != nil {
t.Fatal(err)
}
if len(outs) != 1 {
t.Fatalf("Expected %d event (untagged), got %d", 1, len(outs))
}
2013-05-15 13:11:39 +00:00
images, err = srv.Images(false, "")
if err != nil {
t.Fatal(err)
}
if len(images) != len(initialImages) {
2013-07-02 21:47:43 +00:00
t.Errorf("Expected %d image, %d found", len(initialImages), len(images))
2013-05-15 13:11:39 +00:00
}
/* if c := runtime.Get(container.Id); c != nil {
t.Fatalf("The container as not been deleted")
}
if _, err := os.Stat(path.Join(container.rwPath(), "test")); err == nil {
t.Fatalf("The test file has not been deleted")
} */
}
2013-08-03 20:55:54 +00:00
func TestJsonContentType(t *testing.T) {
if !matchesContentType("application/json", "application/json") {
t.Fail()
}
if !matchesContentType("application/json; charset=utf-8", "application/json") {
t.Fail()
}
if matchesContentType("dockerapplication/json", "application/json") {
t.Fail()
}
}
func TestPostContainersCopy(t *testing.T) {
runtime := mkRuntime(t)
defer nuke(runtime)
srv := &Server{runtime: runtime}
builder := NewBuilder(runtime)
// Create a container and remove a file
container, err := builder.Create(
&Config{
Image: GetTestImage(runtime).ID,
Cmd: []string{"touch", "/test.txt"},
},
)
if err != nil {
t.Fatal(err)
}
defer runtime.Destroy(container)
if err := container.Run(); err != nil {
t.Fatal(err)
}
r := httptest.NewRecorder()
copyData := APICopy{HostPath: ".", Resource: "/test.txt"}
jsonData, err := json.Marshal(copyData)
if err != nil {
t.Fatal(err)
}
req, err := http.NewRequest("POST", "/containers/"+container.ID+"/copy", bytes.NewReader(jsonData))
if err != nil {
t.Fatal(err)
}
req.Header.Add("Content-Type", "application/json")
if err = postContainersCopy(srv, APIVERSION, r, req, map[string]string{"name": container.ID}); err != nil {
t.Fatal(err)
}
if r.Code != http.StatusOK {
t.Fatalf("%d OK expected, received %d\n", http.StatusOK, r.Code)
}
found := false
for tarReader := tar.NewReader(r.Body); ; {
h, err := tarReader.Next()
if err != nil {
if err == io.EOF {
break
}
t.Fatal(err)
}
if h.Name == "test.txt" {
found = true
break
}
}
if !found {
t.Fatalf("The created test file has not been found in the copied output")
}
}
// Mocked types for tests
type NopConn struct {
io.ReadCloser
io.Writer
}
func (c *NopConn) LocalAddr() net.Addr { return nil }
func (c *NopConn) RemoteAddr() net.Addr { return nil }
func (c *NopConn) SetDeadline(t time.Time) error { return nil }
func (c *NopConn) SetReadDeadline(t time.Time) error { return nil }
func (c *NopConn) SetWriteDeadline(t time.Time) error { return nil }
type hijackTester struct {
*httptest.ResponseRecorder
in io.ReadCloser
out io.Writer
}
func (t *hijackTester) Hijack() (net.Conn, *bufio.ReadWriter, error) {
bufrw := bufio.NewReadWriter(bufio.NewReader(t.in), bufio.NewWriter(t.out))
conn := &NopConn{
ReadCloser: t.in,
Writer: t.out,
}
return conn, bufrw, nil
2013-05-09 23:20:07 +00:00
}