moby--moby/api_test.go

1276 lines
28 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/auth"
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-05-10 03:13:52 +00:00
func TestGetAuth(t *testing.T) {
2013-05-09 22:47:06 +00:00
runtime, err := newTestRuntime()
if err != nil {
t.Fatal(err)
}
2013-05-09 22:47:06 +00:00
defer nuke(runtime)
2013-05-09 22:47:06 +00:00
srv := &Server{runtime: runtime}
2013-05-09 22:47:06 +00:00
r := httptest.NewRecorder()
2013-05-09 22:47:06 +00:00
authConfig := &auth.AuthConfig{
Username: "utest",
Password: "utest",
Email: "utest@yopmail.com",
}
2013-05-09 22:47:06 +00:00
authConfigJson, err := json.Marshal(authConfig)
if err != nil {
t.Fatal(err)
}
2013-05-09 22:47:06 +00:00
req, err := http.NewRequest("POST", "/auth", bytes.NewReader(authConfigJson))
if err != nil {
t.Fatal(err)
}
body, err := postAuth(srv, r, req, nil)
if err != nil {
t.Fatal(err)
}
if body == nil {
t.Fatalf("No body received\n")
}
2013-05-10 14:49:47 +00:00
if r.Code != http.StatusOK && r.Code != 0 {
t.Fatalf("%d OK or 0 expected, received %d\n", http.StatusOK, r.Code)
}
2013-05-10 03:13:52 +00:00
if runtime.authConfig.Username != authConfig.Username ||
runtime.authConfig.Password != authConfig.Password ||
runtime.authConfig.Email != authConfig.Email {
t.Fatalf("The auth configuration hasn't been set correctly")
}
}
2013-05-10 03:13:52 +00:00
func TestGetVersion(t *testing.T) {
runtime, err := newTestRuntime()
if err != nil {
t.Fatal(err)
}
2013-05-10 03:13:52 +00:00
defer nuke(runtime)
2013-05-10 03:13:52 +00:00
srv := &Server{runtime: runtime}
body, err := getVersion(srv, nil, nil, nil)
if err != nil {
t.Fatal(err)
}
2013-05-10 03:13:52 +00:00
v := &ApiVersion{}
err = json.Unmarshal(body, v)
if err != nil {
t.Fatal(err)
}
2013-05-10 03:13:52 +00:00
if v.Version != VERSION {
t.Errorf("Excepted version %s, %s found", VERSION, v.Version)
}
}
2013-05-10 03:13:52 +00:00
func TestGetInfo(t *testing.T) {
2013-05-09 22:47:06 +00:00
runtime, err := newTestRuntime()
if err != nil {
t.Fatal(err)
}
2013-05-09 22:47:06 +00:00
defer nuke(runtime)
2013-05-09 22:47:06 +00:00
srv := &Server{runtime: runtime}
2013-05-10 03:13:52 +00:00
body, err := getInfo(srv, nil, nil, nil)
if err != nil {
t.Fatal(err)
}
2013-05-10 03:13:52 +00:00
infos := &ApiInfo{}
err = json.Unmarshal(body, infos)
if err != nil {
t.Fatal(err)
}
2013-05-10 03:13:52 +00:00
if infos.Version != VERSION {
t.Errorf("Excepted version %s, %s found", VERSION, infos.Version)
}
}
2013-05-10 03:13:52 +00:00
func TestGetImagesJson(t *testing.T) {
runtime, err := newTestRuntime()
if err != nil {
t.Fatal(err)
}
defer nuke(runtime)
srv := &Server{runtime: runtime}
2013-05-10 14:49:47 +00:00
// only_ids=0&all=0
req, err := http.NewRequest("GET", "/images/json?only_ids=0&all=0", nil)
if err != nil {
t.Fatal(err)
}
2013-05-10 03:13:52 +00:00
body, err := getImagesJson(srv, nil, req, nil)
if err != nil {
t.Fatal(err)
}
images := []ApiImages{}
err = json.Unmarshal(body, &images)
if err != nil {
t.Fatal(err)
}
if len(images) != 1 {
t.Errorf("Excepted 1 image, %d found", len(images))
}
2013-05-10 19:28:07 +00:00
if images[0].Repository != unitTestImageName {
t.Errorf("Excepted image %s, %s found", unitTestImageName, images[0].Repository)
}
2013-05-10 14:49:47 +00:00
// only_ids=1&all=1
req2, err := http.NewRequest("GET", "/images/json?only_ids=1&all=1", nil)
if err != nil {
t.Fatal(err)
}
body2, err := getImagesJson(srv, nil, req2, nil)
if err != nil {
t.Fatal(err)
}
images2 := []ApiImages{}
err = json.Unmarshal(body2, &images2)
if err != nil {
t.Fatal(err)
}
if len(images2) != 1 {
t.Errorf("Excepted 1 image, %d found", len(images2))
}
if images2[0].Repository != "" {
t.Errorf("Excepted no image Repository, %s found", images2[0].Repository)
}
2013-05-10 20:58:21 +00:00
if images2[0].Id != GetTestImage(runtime).ShortId() {
t.Errorf("Retrieved image Id differs, expected %s, received %s", GetTestImage(runtime).ShortId(), images2[0].Id)
2013-05-10 14:49:47 +00:00
}
// filter=a
req3, err := http.NewRequest("GET", "/images/json?filter=a", nil)
if err != nil {
t.Fatal(err)
}
body3, err := getImagesJson(srv, nil, req3, nil)
if err != nil {
t.Fatal(err)
}
images3 := []ApiImages{}
err = json.Unmarshal(body3, &images3)
if err != nil {
t.Fatal(err)
}
if len(images3) != 0 {
t.Errorf("Excepted 1 image, %d found", len(images3))
}
}
2013-05-10 03:13:52 +00:00
func TestGetImagesViz(t *testing.T) {
2013-05-10 14:49:47 +00:00
runtime, err := newTestRuntime()
if err != nil {
t.Fatal(err)
}
defer nuke(runtime)
srv := &Server{runtime: runtime}
r := httptest.NewRecorder()
_, err = getImagesViz(srv, r, nil, nil)
if err != nil {
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" {
t.Errorf("Excepted digraph docker {\n, %s found", line)
}
2013-05-10 03:13:52 +00:00
}
func TestGetImagesSearch(t *testing.T) {
runtime, err := newTestRuntime()
if err != nil {
t.Fatal(err)
}
defer nuke(runtime)
srv := &Server{runtime: runtime}
2013-05-10 03:13:52 +00:00
req, err := http.NewRequest("GET", "/images/search?term=redis", nil)
if err != nil {
t.Fatal(err)
}
2013-05-10 03:13:52 +00:00
body, err := getImagesSearch(srv, nil, req, nil)
if err != nil {
t.Fatal(err)
}
2013-05-10 03:13:52 +00:00
results := []ApiSearch{}
err = json.Unmarshal(body, &results)
if err != nil {
t.Fatal(err)
}
if len(results) < 2 {
t.Errorf("Excepted at least 2 lines, %d found", len(results))
}
}
2013-05-10 03:13:52 +00:00
func TestGetImagesHistory(t *testing.T) {
2013-05-10 00:51:27 +00:00
runtime, err := newTestRuntime()
if err != nil {
t.Fatal(err)
}
defer nuke(runtime)
srv := &Server{runtime: runtime}
body, err := getImagesHistory(srv, nil, nil, map[string]string{"name": unitTestImageName})
if err != nil {
t.Fatal(err)
}
history := []ApiHistory{}
err = json.Unmarshal(body, &history)
if err != nil {
t.Fatal(err)
}
if len(history) != 1 {
t.Errorf("Excepted 1 line, %d found", len(history))
}
}
2013-05-10 03:13:52 +00:00
func TestGetImagesByName(t *testing.T) {
2013-05-10 00:51:27 +00:00
runtime, err := newTestRuntime()
if err != nil {
t.Fatal(err)
}
defer nuke(runtime)
srv := &Server{runtime: runtime}
2013-05-10 03:13:52 +00:00
body, err := getImagesByName(srv, nil, nil, map[string]string{"name": unitTestImageName})
2013-05-10 00:51:27 +00:00
if err != nil {
t.Fatal(err)
}
2013-05-10 03:13:52 +00:00
img := &Image{}
err = json.Unmarshal(body, img)
2013-05-10 00:51:27 +00:00
if err != nil {
t.Fatal(err)
}
2013-05-10 19:28:07 +00:00
if img.Id != GetTestImage(runtime).Id || img.Comment != "Imported from http://get.docker.io/images/busybox" {
2013-05-10 03:13:52 +00:00
t.Errorf("Error inspecting image")
}
}
2013-05-10 00:51:27 +00:00
2013-05-10 03:13:52 +00:00
func TestGetContainersPs(t *testing.T) {
runtime, err := newTestRuntime()
if err != nil {
t.Fatal(err)
}
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{
Image: GetTestImage(runtime).Id,
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?quiet=1&all=1", nil)
if err != nil {
t.Fatal(err)
}
body, err := getContainersPs(srv, nil, req, nil)
if err != nil {
t.Fatal(err)
}
containers := []ApiContainers{}
err = json.Unmarshal(body, &containers)
if err != nil {
t.Fatal(err)
}
if len(containers) != 1 {
t.Fatalf("Excepted %d container, %d found", 1, len(containers))
}
if containers[0].Id != container.ShortId() {
t.Fatalf("Container ID mismatch. Expected: %s, received: %s\n", container.ShortId(), containers[0].Id)
2013-05-10 00:51:27 +00:00
}
}
2013-05-10 03:13:52 +00:00
func TestGetContainersExport(t *testing.T) {
2013-05-10 14:49:47 +00:00
runtime, err := newTestRuntime()
if err != nil {
t.Fatal(err)
}
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,
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()
_, err = getContainersExport(srv, r, nil, map[string]string{"name": container.Id})
if err != nil {
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, err := newTestRuntime()
if err != nil {
t.Fatal(err)
}
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{
Image: GetTestImage(runtime).Id,
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)
}
body, err := getContainersChanges(srv, nil, nil, map[string]string{"name": container.Id})
if err != nil {
t.Fatal(err)
}
changes := []Change{}
if err := json.Unmarshal(body, &changes); err != nil {
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-05-10 04:53:28 +00:00
func TestGetContainersByName(t *testing.T) {
2013-05-10 14:49:47 +00:00
runtime, err := newTestRuntime()
if err != nil {
t.Fatal(err)
}
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,
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)
body, err := getContainersByName(srv, nil, nil, map[string]string{"name": container.Id})
if err != nil {
t.Fatal(err)
}
2013-05-10 19:28:07 +00:00
outContainer := &Container{}
if err := json.Unmarshal(body, outContainer); err != nil {
2013-05-10 14:49:47 +00:00
t.Fatal(err)
}
2013-05-10 19:28:07 +00:00
if outContainer.Id != container.Id {
t.Fatalf("Wrong containers retrieved. Expected %s, recieved %s", container.Id, outContainer.Id)
}
2013-05-10 03:13:52 +00:00
}
func TestPostAuth(t *testing.T) {
2013-05-10 00:51:27 +00:00
runtime, err := newTestRuntime()
if err != nil {
t.Fatal(err)
}
defer nuke(runtime)
srv := &Server{runtime: runtime}
2013-05-10 03:13:52 +00:00
authConfigOrig := &auth.AuthConfig{
Username: "utest",
Email: "utest@yopmail.com",
}
runtime.authConfig = authConfigOrig
body, err := getAuth(srv, nil, nil, nil)
2013-05-10 00:51:27 +00:00
if err != nil {
t.Fatal(err)
}
2013-05-10 03:13:52 +00:00
authConfig := &auth.AuthConfig{}
err = json.Unmarshal(body, authConfig)
2013-05-10 00:51:27 +00:00
if err != nil {
t.Fatal(err)
}
2013-05-10 03:13:52 +00:00
if authConfig.Username != authConfigOrig.Username || authConfig.Email != authConfigOrig.Email {
t.Errorf("The retrieve auth mismatch with the one set.")
2013-05-10 00:51:27 +00:00
}
}
2013-05-09 22:47:06 +00:00
2013-05-10 03:13:52 +00:00
func TestPostCommit(t *testing.T) {
2013-05-10 14:49:47 +00:00
runtime, err := newTestRuntime()
if err != nil {
t.Fatal(err)
}
defer nuke(runtime)
srv := &Server{runtime: runtime}
r := httptest.NewRecorder()
builder := NewBuilder(runtime)
// Create a container and remove a file
container, err := builder.Create(
&Config{
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)
}
req, err := http.NewRequest("POST", "/commit?repo=testrepo&testtag=tag&container="+container.Id, bytes.NewReader([]byte{}))
if err != nil {
t.Fatal(err)
}
body, err := postCommit(srv, r, req, nil)
if err != nil {
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
apiId := &ApiId{}
if err := json.Unmarshal(body, apiId); err != nil {
t.Fatal(err)
}
if _, err := runtime.graph.Get(apiId.Id); err != nil {
t.Fatalf("The image has not been commited")
}
2013-05-10 03:13:52 +00:00
}
func TestPostBuild(t *testing.T) {
2013-05-10 14:49:47 +00:00
runtime, err := newTestRuntime()
if err != nil {
t.Fatal(err)
}
defer nuke(runtime)
srv := &Server{runtime: runtime}
stdin, stdinPipe := io.Pipe()
stdout, stdoutPipe := io.Pipe()
c1 := make(chan struct{})
go func() {
r := &hijackTester{
ResponseRecorder: httptest.NewRecorder(),
in: stdin,
out: stdoutPipe,
}
body, err := postBuild(srv, r, nil, nil)
close(c1)
if err != nil {
t.Fatal(err)
}
if body != nil {
t.Fatalf("No body expected, received: %s\n", body)
}
}()
// 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("from docker-ut\n", "FROM docker-ut", 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 build to finish, the client disconnected, therefore, Build finished his job
setTimeout(t, "Waiting for CmdBuild timed out", 2*time.Second, func() {
<-c1
})
2013-05-10 03:13:52 +00:00
}
func TestPostImagesCreate(t *testing.T) {
2013-05-10 19:28:07 +00:00
// FIXME: Use the staging in order to perform tests
// runtime, err := newTestRuntime()
// if err != nil {
// t.Fatal(err)
// }
// defer nuke(runtime)
// srv := &Server{runtime: runtime}
// stdin, stdinPipe := io.Pipe()
// stdout, stdoutPipe := io.Pipe()
// c1 := make(chan struct{})
// go func() {
// defer close(c1)
// r := &hijackTester{
// ResponseRecorder: httptest.NewRecorder(),
// in: stdin,
// out: stdoutPipe,
// }
// req, err := http.NewRequest("POST", "/images/create?fromImage="+unitTestImageName, bytes.NewReader([]byte{}))
// if err != nil {
// t.Fatal(err)
// }
// body, err := postImagesCreate(srv, r, req, nil)
// if err != nil {
// t.Fatal(err)
// }
// if body != nil {
// t.Fatalf("No body expected, received: %s\n", body)
// }
// }()
// // Acknowledge hijack
// setTimeout(t, "hijack acknowledge timed out", 2*time.Second, func() {
// stdout.Read([]byte{})
// stdout.Read(make([]byte, 4096))
// })
// setTimeout(t, "Waiting for imagesCreate output", 5*time.Second, func() {
// reader := bufio.NewReader(stdout)
// line, err := reader.ReadString('\n')
// if err != nil {
// t.Fatal(err)
// }
// if !strings.HasPrefix(line, "Pulling repository d from") {
// t.Fatalf("Expected Pulling repository docker-ut from..., found %s", line)
// }
// })
// // Close pipes (client disconnects)
// if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
// t.Fatal(err)
// }
// // Wait for imagesCreate to finish, the client disconnected, therefore, Create finished his job
// setTimeout(t, "Waiting for imagesCreate timed out", 10*time.Second, func() {
// <-c1
// })
2013-05-10 03:13:52 +00:00
}
2013-05-10 19:28:07 +00:00
// func TestPostImagesInsert(t *testing.T) {
// //FIXME: Implement this test (or remove this endpoint)
// t.Log("Test not implemented")
// }
2013-05-10 03:13:52 +00:00
func TestPostImagesPush(t *testing.T) {
2013-05-10 19:28:07 +00:00
//FIXME: Use staging in order to perform tests
// runtime, err := newTestRuntime()
// if err != nil {
// t.Fatal(err)
// }
// defer nuke(runtime)
// srv := &Server{runtime: runtime}
// stdin, stdinPipe := io.Pipe()
// stdout, stdoutPipe := io.Pipe()
// c1 := make(chan struct{})
// go func() {
// r := &hijackTester{
// ResponseRecorder: httptest.NewRecorder(),
// in: stdin,
// out: stdoutPipe,
// }
// req, err := http.NewRequest("POST", "/images/docker-ut/push", bytes.NewReader([]byte{}))
// if err != nil {
// t.Fatal(err)
// }
// body, err := postImagesPush(srv, r, req, map[string]string{"name": "docker-ut"})
// close(c1)
// if err != nil {
// t.Fatal(err)
// }
// if body != nil {
// t.Fatalf("No body expected, received: %s\n", body)
// }
// }()
// // Acknowledge hijack
// setTimeout(t, "hijack acknowledge timed out", 2*time.Second, func() {
// stdout.Read([]byte{})
// stdout.Read(make([]byte, 4096))
// })
// setTimeout(t, "Waiting for imagesCreate output", 5*time.Second, func() {
// reader := bufio.NewReader(stdout)
// line, err := reader.ReadString('\n')
// if err != nil {
// t.Fatal(err)
// }
// if !strings.HasPrefix(line, "Processing checksum") {
// t.Fatalf("Processing checksum..., found %s", line)
// }
// })
// // Close pipes (client disconnects)
// if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
// t.Fatal(err)
// }
// // Wait for imagesPush to finish, the client disconnected, therefore, Push finished his job
// setTimeout(t, "Waiting for imagesPush timed out", 10*time.Second, func() {
// <-c1
// })
2013-05-10 03:13:52 +00:00
}
func TestPostImagesTag(t *testing.T) {
2013-05-10 19:28:07 +00:00
// FIXME: Use staging in order to perform tests
// runtime, err := newTestRuntime()
// if err != nil {
// t.Fatal(err)
// }
// defer nuke(runtime)
// srv := &Server{runtime: runtime}
// r := httptest.NewRecorder()
// req, err := http.NewRequest("POST", "/images/docker-ut/tag?repo=testrepo&tag=testtag", bytes.NewReader([]byte{}))
// if err != nil {
// t.Fatal(err)
// }
// body, err := postImagesTag(srv, r, req, map[string]string{"name": "docker-ut"})
// if err != nil {
// t.Fatal(err)
// }
// if body != nil {
// t.Fatalf("No body expected, received: %s\n", body)
// }
// if r.Code != http.StatusCreated {
// t.Fatalf("%d Created expected, received %d\n", http.StatusCreated, r.Code)
// }
2013-05-10 03:13:52 +00:00
}
2013-05-10 02:19:24 +00:00
func TestPostContainersCreate(t *testing.T) {
2013-05-09 23:20:07 +00:00
runtime, err := newTestRuntime()
if err != nil {
t.Fatal(err)
}
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-05-09 23:20:07 +00:00
r := httptest.NewRecorder()
2013-05-09 22:47:06 +00:00
2013-05-10 02:19:24 +00:00
configJson, err := json.Marshal(&Config{
Image: GetTestImage(runtime).Id,
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-05-10 02:19:24 +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
2013-05-10 02:19:24 +00:00
body, err := postContainersCreate(srv, r, req, nil)
2013-05-09 23:20:07 +00:00
if err != nil {
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-05-10 02:19:24 +00:00
apiRun := &ApiRun{}
if err := json.Unmarshal(body, apiRun); err != nil {
2013-05-09 23:20:07 +00:00
t.Fatal(err)
}
2013-05-09 22:47:06 +00:00
2013-05-10 02:19:24 +00:00
container := srv.runtime.Get(apiRun.Id)
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) {
Debugf("Err: %s", err)
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) {
2013-05-10 02:19:24 +00:00
runtime, err := newTestRuntime()
if err != nil {
t.Fatal(err)
}
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{
Image: GetTestImage(runtime).Id,
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
2013-05-10 03:13:52 +00:00
if err := container.Start(); 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()
body, err := postContainersKill(srv, r, nil, map[string]string{"name": container.Id})
2013-05-09 23:20:07 +00:00
if err != nil {
t.Fatal(err)
}
2013-05-10 03:13:52 +00:00
if body != nil {
t.Fatalf("No body expected, received: %s\n", body)
2013-05-10 02:19:24 +00:00
}
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) {
2013-05-10 02:19:24 +00:00
runtime, err := newTestRuntime()
if err != nil {
t.Fatal(err)
}
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{
Image: GetTestImage(runtime).Id,
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-10 03:13:52 +00:00
if err := container.Start(); err != nil {
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-05-10 02:19:24 +00:00
r := httptest.NewRecorder()
2013-05-09 23:20:07 +00:00
2013-05-10 03:13:52 +00:00
req, err := http.NewRequest("POST", "/containers/"+container.Id+"/restart?t=1", bytes.NewReader([]byte{}))
if err != nil {
t.Fatal(err)
}
body, err := postContainersRestart(srv, r, req, map[string]string{"name": container.Id})
2013-05-09 23:20:07 +00:00
if err != nil {
t.Fatal(err)
}
if body != nil {
t.Fatalf("No body expected, received: %s\n", body)
}
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, err := newTestRuntime()
if err != nil {
t.Fatal(err)
}
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{
Image: GetTestImage(runtime).Id,
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
2013-05-10 03:13:52 +00:00
r := httptest.NewRecorder()
body, err := postContainersStart(srv, r, nil, map[string]string{"name": container.Id})
2013-05-09 23:20:07 +00:00
if err != nil {
t.Fatal(err)
}
if body != nil {
t.Fatalf("No body expected, received: %s\n", body)
}
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")
}
if _, err = postContainersStart(srv, r, nil, map[string]string{"name": container.Id}); err == nil {
t.Fatalf("A running containter should be able to be started")
}
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, err := newTestRuntime()
if err != nil {
t.Fatal(err)
}
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{
Image: GetTestImage(runtime).Id,
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
2013-05-10 02:19:24 +00:00
if err := container.Start(); 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")
}
r := httptest.NewRecorder()
// Note: as it is a POST request, it requires a body.
req, err := http.NewRequest("POST", "/containers/"+container.Id+"/stop?t=1", bytes.NewReader([]byte{}))
if err != nil {
t.Fatal(err)
}
body, err := postContainersStop(srv, r, req, map[string]string{"name": container.Id})
if err != nil {
t.Fatal(err)
}
2013-05-09 23:20:07 +00:00
if body != nil {
t.Fatalf("No body expected, received: %s\n", body)
}
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, err := newTestRuntime()
2013-05-09 23:20:07 +00:00
if err != nil {
t.Fatal(err)
}
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{
Image: GetTestImage(runtime).Id,
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
2013-05-10 03:13:52 +00:00
if err := container.Start(); 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() {
2013-05-10 04:53:28 +00:00
body, err := postContainersWait(srv, nil, nil, map[string]string{"name": container.Id})
2013-05-10 03:13:52 +00:00
if err != nil {
t.Fatal(err)
}
apiWait := &ApiWait{}
if err := json.Unmarshal(body, apiWait); err != nil {
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, err := newTestRuntime()
if err != nil {
t.Fatal(err)
}
defer nuke(runtime)
srv := &Server{runtime: runtime}
container, err := NewBuilder(runtime).Create(
&Config{
Image: GetTestImage(runtime).Id,
Cmd: []string{"/bin/cat"},
OpenStdin: true,
},
)
if err != nil {
t.Fatal(err)
}
defer runtime.Destroy(container)
// Start the process
if err := container.Start(); err != nil {
t.Fatal(err)
}
stdin, stdinPipe := io.Pipe()
stdout, stdoutPipe := io.Pipe()
// Attach to it
c1 := make(chan struct{})
go func() {
// We're simulating a disconnect so the return value doesn't matter. What matters is the
// fact that CmdAttach returns.
r := &hijackTester{
ResponseRecorder: httptest.NewRecorder(),
in: stdin,
out: stdoutPipe,
}
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)
}
body, err := postContainersAttach(srv, r, req, map[string]string{"name": container.Id})
close(c1)
if err != nil {
t.Fatal(err)
}
if body != nil {
t.Fatalf("No body expected, received: %s\n", body)
}
}()
// 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", 2*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")
}
// Try to avoid the timeoout 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, err := newTestRuntime()
if err != nil {
t.Fatal(err)
}
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{
Image: GetTestImage(runtime).Id,
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)
}
r := httptest.NewRecorder()
2013-05-09 23:20:07 +00:00
2013-05-10 02:19:24 +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)
}
2013-05-10 02:19:24 +00:00
body, err := deleteContainers(srv, r, req, map[string]string{"name": container.Id})
if err != nil {
t.Fatal(err)
}
2013-05-09 23:20:07 +00:00
if body != nil {
t.Fatalf("No body expected, received: %s\n", body)
}
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 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-05-09 23:20:07 +00:00
}
2013-05-10 03:13:52 +00:00
func TestDeleteImages(t *testing.T) {
//FIXME: Implement this test
t.Log("Test not implemented")
}
// 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
}