2013-05-08 15:35:50 +00:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2013-05-09 22:47:06 +00:00
|
|
|
"bytes"
|
2013-05-08 15:35:50 +00:00
|
|
|
"encoding/json"
|
|
|
|
"github.com/dotcloud/docker/auth"
|
2013-05-09 22:47:06 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2013-05-10 02:19:24 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
2013-05-08 15:35:50 +00:00
|
|
|
"testing"
|
2013-05-10 02:19:24 +00:00
|
|
|
"time"
|
2013-05-08 15:35:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAuth(t *testing.T) {
|
2013-05-09 22:47:06 +00:00
|
|
|
runtime, err := newTestRuntime()
|
2013-05-08 15:35:50 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-05-09 22:47:06 +00:00
|
|
|
defer nuke(runtime)
|
2013-05-08 15:35:50 +00:00
|
|
|
|
2013-05-09 22:47:06 +00:00
|
|
|
srv := &Server{runtime: runtime}
|
2013-05-08 15:35:50 +00:00
|
|
|
|
2013-05-09 22:47:06 +00:00
|
|
|
r := httptest.NewRecorder()
|
2013-05-08 15:35:50 +00:00
|
|
|
|
2013-05-09 22:47:06 +00:00
|
|
|
authConfig := &auth.AuthConfig{
|
|
|
|
Username: "utest",
|
|
|
|
Password: "utest",
|
|
|
|
Email: "utest@yopmail.com",
|
2013-05-08 15:35:50 +00:00
|
|
|
}
|
|
|
|
|
2013-05-09 22:47:06 +00:00
|
|
|
authConfigJson, err := json.Marshal(authConfig)
|
2013-05-08 15:35:50 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-05-09 22:47:06 +00:00
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", "/auth", bytes.NewReader(authConfigJson))
|
2013-05-08 15:35:50 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-05-09 23:28:47 +00:00
|
|
|
body, err := postAuth(srv, r, req, nil)
|
2013-05-08 15:35:50 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-05-09 23:20:06 +00:00
|
|
|
if body == nil {
|
|
|
|
t.Fatalf("No body received\n")
|
2013-05-08 15:35:50 +00:00
|
|
|
}
|
2013-05-09 23:20:06 +00:00
|
|
|
if r.Code != http.StatusOK {
|
|
|
|
t.Fatalf("%d OK expected, received %d\n", http.StatusOK, r.Code)
|
2013-05-08 15:35:50 +00:00
|
|
|
}
|
|
|
|
|
2013-05-09 22:47:06 +00:00
|
|
|
authConfig = &auth.AuthConfig{}
|
2013-05-08 15:35:50 +00:00
|
|
|
|
2013-05-09 22:47:06 +00:00
|
|
|
req, err = http.NewRequest("GET", "/auth", nil)
|
2013-05-08 15:35:50 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-05-09 23:28:47 +00:00
|
|
|
body, err = getAuth(srv, nil, req, nil)
|
2013-05-08 15:35:50 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-05-09 22:47:06 +00:00
|
|
|
err = json.Unmarshal(body, authConfig)
|
2013-05-08 15:35:50 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-05-09 22:47:06 +00:00
|
|
|
|
|
|
|
if authConfig.Username != "utest" {
|
|
|
|
t.Errorf("Expected username to be utest, %s found", authConfig.Username)
|
2013-05-08 15:35:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-09 22:47:06 +00:00
|
|
|
func TestVersion(t *testing.T) {
|
|
|
|
runtime, err := newTestRuntime()
|
2013-05-08 15:35:50 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-05-09 22:47:06 +00:00
|
|
|
defer nuke(runtime)
|
2013-05-08 15:35:50 +00:00
|
|
|
|
2013-05-09 22:47:06 +00:00
|
|
|
srv := &Server{runtime: runtime}
|
2013-05-08 15:35:50 +00:00
|
|
|
|
2013-05-09 23:28:47 +00:00
|
|
|
body, err := getVersion(srv, nil, nil, nil)
|
2013-05-08 15:35:50 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-05-09 22:47:06 +00:00
|
|
|
v := &ApiVersion{}
|
|
|
|
|
|
|
|
err = json.Unmarshal(body, v)
|
2013-05-08 15:35:50 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-05-09 22:47:06 +00:00
|
|
|
if v.Version != VERSION {
|
|
|
|
t.Errorf("Excepted version %s, %s found", VERSION, v.Version)
|
2013-05-08 15:35:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-10 00:50:56 +00:00
|
|
|
func TestContainersExport(t *testing.T) {
|
|
|
|
//FIXME: Implement this test
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetImages(t *testing.T) {
|
2013-05-09 23:20:06 +00:00
|
|
|
runtime, err := newTestRuntime()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer nuke(runtime)
|
2013-05-08 15:35:50 +00:00
|
|
|
|
2013-05-09 23:20:06 +00:00
|
|
|
srv := &Server{runtime: runtime}
|
2013-05-08 15:35:50 +00:00
|
|
|
|
2013-05-09 23:20:06 +00:00
|
|
|
// FIXME: Do more tests with filter
|
|
|
|
req, err := http.NewRequest("GET", "/images?quiet=0&all=0", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-05-09 23:28:47 +00:00
|
|
|
body, err := getImages(srv, nil, req, nil)
|
2013-05-09 23:20:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
images := []ApiImages{}
|
|
|
|
err = json.Unmarshal(body, &images)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-05-08 15:35:50 +00:00
|
|
|
|
2013-05-09 23:20:06 +00:00
|
|
|
if len(images) != 1 {
|
|
|
|
t.Errorf("Excepted 1 image, %d found", len(images))
|
|
|
|
}
|
|
|
|
|
|
|
|
if images[0].Repository != "docker-ut" {
|
|
|
|
t.Errorf("Excepted image docker-ut, %s found", images[0].Repository)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInfo(t *testing.T) {
|
|
|
|
runtime, err := newTestRuntime()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer nuke(runtime)
|
|
|
|
|
|
|
|
srv := &Server{runtime: runtime}
|
|
|
|
|
2013-05-09 23:28:47 +00:00
|
|
|
body, err := getInfo(srv, nil, nil, nil)
|
2013-05-09 23:20:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
infos := &ApiInfo{}
|
|
|
|
err = json.Unmarshal(body, infos)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if infos.Version != VERSION {
|
|
|
|
t.Errorf("Excepted version %s, %s found", VERSION, infos.Version)
|
|
|
|
}
|
|
|
|
}
|
2013-05-08 15:35:50 +00:00
|
|
|
|
2013-05-10 00:51:27 +00:00
|
|
|
func TestHistory(t *testing.T) {
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestImagesSearch(t *testing.T) {
|
|
|
|
runtime, err := newTestRuntime()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer nuke(runtime)
|
|
|
|
|
|
|
|
srv := &Server{runtime: runtime}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", "/images/search?term=redis", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := getImagesSearch(srv, nil, req, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetImage(t *testing.T) {
|
|
|
|
runtime, err := newTestRuntime()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer nuke(runtime)
|
|
|
|
|
|
|
|
srv := &Server{runtime: runtime}
|
|
|
|
|
|
|
|
body, err := getImagesByName(srv, nil, nil, map[string]string{"name": unitTestImageName})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
img := &Image{}
|
|
|
|
|
|
|
|
err = json.Unmarshal(body, img)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if img.Comment != "Imported from http://get.docker.io/images/busybox" {
|
|
|
|
t.Errorf("Error inspecting image")
|
|
|
|
}
|
|
|
|
}
|
2013-05-09 22:47:06 +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 02:19:24 +00:00
|
|
|
func TestGetContainersPs(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{"echo", "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)
|
2013-05-09 23:20:07 +00:00
|
|
|
|
2013-05-10 02:19:24 +00:00
|
|
|
req, err := http.NewRequest("GET", "/containers?quiet=1&all=1", 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 := getContainersPs(srv, nil, req, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
containers := []ApiContainers{}
|
|
|
|
err = json.Unmarshal(body, &containers)
|
2013-05-09 23:20:07 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-05-10 02:19:24 +00:00
|
|
|
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-09 23:20:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-10 02:19:24 +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 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)
|
|
|
|
|
|
|
|
r := httptest.NewRecorder()
|
2013-05-09 23:20:07 +00:00
|
|
|
|
2013-05-10 02:19:24 +00:00
|
|
|
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 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")
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func testContainerRestart(t *testing.T, srv *Server, id string) {
|
|
|
|
|
|
|
|
r := httptest.NewRecorder()
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", "/containers/"+id+"/restart?t=1", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-05-09 23:28:47 +00:00
|
|
|
body, err := postContainersRestart(srv, r, req, nil)
|
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
|
|
|
// testContainerRestart(t, srv, id)
|
|
|
|
// testContainerKill(t, srv, id)
|
|
|
|
// testContainerWait(t, srv, id)
|
|
|
|
// testDeleteContainer(t, srv, id)
|
|
|
|
// testListContainers(t, srv, 0)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func testContainerKill(t *testing.T, srv *Server, id string) {
|
|
|
|
|
|
|
|
r := httptest.NewRecorder()
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", "/containers/"+id+"/kill", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-05-09 23:28:47 +00:00
|
|
|
body, err := postContainersKill(srv, r, req, nil)
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testContainerWait(t *testing.T, srv *Server, id string) {
|
|
|
|
|
|
|
|
r := httptest.NewRecorder()
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", "/containers/"+id+"/wait", nil)
|
|
|
|
req.Header.Set("Content-Type", "plain/text")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-05-09 23:28:47 +00:00
|
|
|
body, err := postContainersWait(srv, r, req, nil)
|
2013-05-09 23:20:07 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if body == nil {
|
|
|
|
t.Fatalf("Body expected, received: nil\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.Code != http.StatusOK {
|
|
|
|
t.Fatalf("%d OK expected, received %d\n", http.StatusNoContent, r.Code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-10 02:19:24 +00:00
|
|
|
// FIXME: Test deleting runnign container
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
|
|
|
func testContainerChanges(t *testing.T, srv *Server, id string) {
|
|
|
|
|
|
|
|
r := httptest.NewRecorder()
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", "/containers/"+id+"/changes", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-05-09 23:28:47 +00:00
|
|
|
body, err := getContainersChanges(srv, r, req, nil)
|
2013-05-09 23:20:07 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if body == nil {
|
|
|
|
t.Fatalf("Body expected, received: nil\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.Code != http.StatusOK {
|
|
|
|
t.Fatalf("%d OK expected, received %d\n", http.StatusNoContent, r.Code)
|
|
|
|
}
|
|
|
|
}
|