2013-05-08 11:35:50 -04:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2013-05-10 15:28:07 -04:00
|
|
|
"archive/tar"
|
2013-05-10 00:54:43 -04:00
|
|
|
"bufio"
|
2013-05-09 18:47:06 -04:00
|
|
|
"bytes"
|
2013-05-08 11:35:50 -04:00
|
|
|
"encoding/json"
|
2013-10-16 15:23:47 -04:00
|
|
|
"fmt"
|
2013-05-14 21:52:09 -04:00
|
|
|
"github.com/dotcloud/docker/utils"
|
2013-05-10 00:54:43 -04:00
|
|
|
"io"
|
|
|
|
"net"
|
2013-05-09 18:47:06 -04:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2013-05-09 22:19:24 -04:00
|
|
|
"os"
|
|
|
|
"path"
|
2013-10-16 15:23:47 -04:00
|
|
|
"strings"
|
2013-05-08 11:35:50 -04:00
|
|
|
"testing"
|
2013-05-09 22:19:24 -04:00
|
|
|
"time"
|
2013-05-08 11:35:50 -04:00
|
|
|
)
|
|
|
|
|
2013-06-20 10:34:58 -04:00
|
|
|
func TestGetBoolParam(t *testing.T) {
|
|
|
|
if ret, err := getBoolParam("true"); err != nil || !ret {
|
2013-06-27 00:33:55 -04:00
|
|
|
t.Fatalf("true -> true, nil | got %t %s", ret, err)
|
2013-06-20 10:34:58 -04:00
|
|
|
}
|
|
|
|
if ret, err := getBoolParam("True"); err != nil || !ret {
|
2013-06-27 00:33:55 -04:00
|
|
|
t.Fatalf("True -> true, nil | got %t %s", ret, err)
|
2013-06-20 10:34:58 -04:00
|
|
|
}
|
|
|
|
if ret, err := getBoolParam("1"); err != nil || !ret {
|
2013-06-27 00:33:55 -04:00
|
|
|
t.Fatalf("1 -> true, nil | got %t %s", ret, err)
|
2013-06-20 10:34:58 -04:00
|
|
|
}
|
|
|
|
if ret, err := getBoolParam(""); err != nil || ret {
|
2013-06-27 00:33:55 -04:00
|
|
|
t.Fatalf("\"\" -> false, nil | got %t %s", ret, err)
|
2013-06-20 10:34:58 -04:00
|
|
|
}
|
|
|
|
if ret, err := getBoolParam("false"); err != nil || ret {
|
2013-06-27 00:33:55 -04:00
|
|
|
t.Fatalf("false -> false, nil | got %t %s", ret, err)
|
2013-06-20 10:34:58 -04:00
|
|
|
}
|
|
|
|
if ret, err := getBoolParam("0"); err != nil || ret {
|
2013-06-27 00:33:55 -04:00
|
|
|
t.Fatalf("0 -> false, nil | got %t %s", ret, err)
|
2013-06-20 10:34:58 -04:00
|
|
|
}
|
|
|
|
if ret, err := getBoolParam("faux"); err == nil || ret {
|
2013-06-27 00:33:55 -04:00
|
|
|
t.Fatalf("faux -> false, err | got %t %s", ret, err)
|
2013-06-20 10:34:58 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-16 15:23:47 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-09 23:13:52 -04:00
|
|
|
func TestGetVersion(t *testing.T) {
|
2013-07-11 20:59:25 -04:00
|
|
|
var err error
|
|
|
|
runtime := mkRuntime(t)
|
2013-05-09 23:13:52 -04:00
|
|
|
defer nuke(runtime)
|
2013-05-08 11:35:50 -04:00
|
|
|
|
2013-05-09 23:13:52 -04:00
|
|
|
srv := &Server{runtime: runtime}
|
|
|
|
|
2013-05-10 18:24:07 -04:00
|
|
|
r := httptest.NewRecorder()
|
|
|
|
|
2013-06-04 14:00:22 -04:00
|
|
|
if err := getVersion(srv, APIVERSION, r, nil, nil); err != nil {
|
2013-05-08 11:35:50 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-06-04 14:00:22 -04:00
|
|
|
v := &APIVersion{}
|
2013-05-10 18:24:07 -04:00
|
|
|
if err = json.Unmarshal(r.Body.Bytes(), v); err != nil {
|
2013-05-08 11:35:50 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-05-09 23:13:52 -04:00
|
|
|
if v.Version != VERSION {
|
2013-07-02 17:47:43 -04:00
|
|
|
t.Errorf("Expected version %s, %s found", VERSION, v.Version)
|
2013-05-08 11:35:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-09 23:13:52 -04:00
|
|
|
func TestGetInfo(t *testing.T) {
|
2013-07-11 20:59:25 -04:00
|
|
|
runtime := mkRuntime(t)
|
2013-05-09 18:47:06 -04:00
|
|
|
defer nuke(runtime)
|
2013-05-08 11:35:50 -04:00
|
|
|
|
2013-05-09 18:47:06 -04:00
|
|
|
srv := &Server{runtime: runtime}
|
2013-05-08 11:35:50 -04:00
|
|
|
|
2013-08-31 23:31:21 -04:00
|
|
|
initialImages, err := srv.runtime.graph.Map()
|
2013-07-01 18:06:08 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-05-10 18:24:07 -04:00
|
|
|
r := httptest.NewRecorder()
|
|
|
|
|
2013-06-04 14:00:22 -04:00
|
|
|
if err := getInfo(srv, APIVERSION, r, nil, nil); err != nil {
|
2013-05-08 11:35:50 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-05-10 18:24:07 -04:00
|
|
|
|
2013-06-04 14:00:22 -04:00
|
|
|
infos := &APIInfo{}
|
2013-05-10 18:24:07 -04:00
|
|
|
err = json.Unmarshal(r.Body.Bytes(), infos)
|
2013-05-08 11:35:50 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-07-01 18:06:08 -04:00
|
|
|
if infos.Images != len(initialImages) {
|
2013-07-02 17:47:43 -04:00
|
|
|
t.Errorf("Expected images: %d, %d found", len(initialImages), infos.Images)
|
2013-05-08 11:35:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-18 10:54:58 -04:00
|
|
|
func TestGetEvents(t *testing.T) {
|
|
|
|
runtime := mkRuntime(t)
|
2013-10-16 16:10:20 -04:00
|
|
|
defer nuke(runtime)
|
2013-07-18 10:54:58 -04:00
|
|
|
srv := &Server{
|
|
|
|
runtime: runtime,
|
|
|
|
events: make([]utils.JSONMessage, 0, 64),
|
|
|
|
listeners: make(map[string]chan utils.JSONMessage),
|
|
|
|
}
|
|
|
|
|
2013-08-14 19:43:43 -04:00
|
|
|
srv.LogEvent("fakeaction", "fakeid", "fakeimage")
|
|
|
|
srv.LogEvent("fakeaction2", "fakeid", "fakeimage")
|
2013-07-18 10:54:58 -04:00
|
|
|
|
|
|
|
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 14:00:22 -04:00
|
|
|
func TestGetImagesJSON(t *testing.T) {
|
2013-07-11 20:59:25 -04:00
|
|
|
runtime := mkRuntime(t)
|
2013-05-09 19:20:06 -04:00
|
|
|
defer nuke(runtime)
|
2013-05-08 11:35:50 -04:00
|
|
|
|
2013-05-09 19:20:06 -04:00
|
|
|
srv := &Server{runtime: runtime}
|
2013-05-08 11:35:50 -04:00
|
|
|
|
2013-05-13 06:18:55 -04:00
|
|
|
// all=0
|
2013-07-01 20:19:39 -04:00
|
|
|
|
|
|
|
initialImages, err := srv.Images(false, "")
|
2013-07-01 14:45:45 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-05-13 06:18:55 -04:00
|
|
|
req, err := http.NewRequest("GET", "/images/json?all=0", nil)
|
2013-05-09 19:20:06 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-05-10 18:24:07 -04:00
|
|
|
r := httptest.NewRecorder()
|
|
|
|
|
2013-06-04 14:00:22 -04:00
|
|
|
if err := getImagesJSON(srv, APIVERSION, r, req, nil); err != nil {
|
2013-05-09 19:20:06 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-06-04 14:00:22 -04:00
|
|
|
images := []APIImages{}
|
2013-05-10 18:24:07 -04:00
|
|
|
if err := json.Unmarshal(r.Body.Bytes(), &images); err != nil {
|
2013-05-09 19:20:06 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-05-08 11:35:50 -04:00
|
|
|
|
2013-07-01 14:45:45 -04:00
|
|
|
if len(images) != len(initialImages) {
|
2013-07-02 17:47:43 -04:00
|
|
|
t.Errorf("Expected %d image, %d found", len(initialImages), len(images))
|
2013-05-09 19:20:06 -04:00
|
|
|
}
|
|
|
|
|
2013-07-01 14:45:45 -04:00
|
|
|
found := false
|
|
|
|
for _, img := range images {
|
2013-10-13 09:42:01 -04:00
|
|
|
if strings.Contains(img.RepoTags[0], unitTestImageName) {
|
2013-07-01 14:45:45 -04:00
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
2013-07-02 17:47:43 -04:00
|
|
|
t.Errorf("Expected image %s, %+v found", unitTestImageName, images)
|
2013-05-09 19:20:06 -04:00
|
|
|
}
|
2013-05-10 10:49:47 -04:00
|
|
|
|
2013-05-10 18:24:07 -04:00
|
|
|
r2 := httptest.NewRecorder()
|
|
|
|
|
2013-05-13 06:18:55 -04:00
|
|
|
// all=1
|
2013-07-01 20:19:39 -04:00
|
|
|
|
|
|
|
initialImages, err = srv.Images(true, "")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-05-16 09:45:29 -04:00
|
|
|
req2, err := http.NewRequest("GET", "/images/json?all=true", nil)
|
2013-05-10 10:49:47 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-06-04 14:00:22 -04:00
|
|
|
if err := getImagesJSON(srv, APIVERSION, r2, req2, nil); err != nil {
|
2013-05-10 10:49:47 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-06-04 14:00:22 -04:00
|
|
|
images2 := []APIImages{}
|
2013-05-10 18:24:07 -04:00
|
|
|
if err := json.Unmarshal(r2.Body.Bytes(), &images2); err != nil {
|
2013-05-10 10:49:47 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-07-01 14:45:45 -04:00
|
|
|
if len(images2) != len(initialImages) {
|
2013-07-02 17:47:43 -04:00
|
|
|
t.Errorf("Expected %d image, %d found", len(initialImages), len(images2))
|
2013-05-10 10:49:47 -04:00
|
|
|
}
|
|
|
|
|
2013-07-01 14:45:45 -04: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 10:49:47 -04:00
|
|
|
}
|
|
|
|
|
2013-05-10 18:24:07 -04:00
|
|
|
r3 := httptest.NewRecorder()
|
|
|
|
|
2013-05-10 10:49:47 -04:00
|
|
|
// filter=a
|
2013-07-01 14:45:45 -04:00
|
|
|
req3, err := http.NewRequest("GET", "/images/json?filter=aaaaaaaaaa", nil)
|
2013-05-10 10:49:47 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-06-04 14:00:22 -04:00
|
|
|
if err := getImagesJSON(srv, APIVERSION, r3, req3, nil); err != nil {
|
2013-05-10 10:49:47 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-06-04 14:00:22 -04:00
|
|
|
images3 := []APIImages{}
|
2013-05-10 18:24:07 -04:00
|
|
|
if err := json.Unmarshal(r3.Body.Bytes(), &images3); err != nil {
|
2013-05-10 10:49:47 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(images3) != 0 {
|
2013-07-02 17:47:43 -04:00
|
|
|
t.Errorf("Expected 0 image, %d found", len(images3))
|
2013-05-10 10:49:47 -04:00
|
|
|
}
|
2013-05-16 09:45:29 -04: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 14:00:22 -04:00
|
|
|
err = getImagesJSON(srv, APIVERSION, r4, req4, nil)
|
2013-05-16 09:45:29 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Error expected, received none")
|
|
|
|
}
|
|
|
|
|
2013-10-16 15:23:47 -04:00
|
|
|
if !strings.HasPrefix(err.Error(), "Bad parameter") {
|
|
|
|
t.Fatalf("Error should starts with \"Bad parameter\"")
|
|
|
|
}
|
|
|
|
http.Error(r4, err.Error(), http.StatusBadRequest)
|
|
|
|
|
2013-05-16 09:45:29 -04:00
|
|
|
if r4.Code != http.StatusBadRequest {
|
|
|
|
t.Fatalf("%d Bad Request expected, received %d\n", http.StatusBadRequest, r4.Code)
|
|
|
|
}
|
2013-05-09 19:20:06 -04:00
|
|
|
}
|
|
|
|
|
2013-05-09 23:13:52 -04:00
|
|
|
func TestGetImagesHistory(t *testing.T) {
|
2013-07-11 20:59:25 -04:00
|
|
|
runtime := mkRuntime(t)
|
2013-05-09 20:51:27 -04:00
|
|
|
defer nuke(runtime)
|
|
|
|
|
|
|
|
srv := &Server{runtime: runtime}
|
|
|
|
|
2013-05-10 18:24:07 -04:00
|
|
|
r := httptest.NewRecorder()
|
|
|
|
|
2013-06-04 14:00:22 -04:00
|
|
|
if err := getImagesHistory(srv, APIVERSION, r, nil, map[string]string{"name": unitTestImageName}); err != nil {
|
2013-05-09 20:51:27 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-06-04 14:00:22 -04:00
|
|
|
history := []APIHistory{}
|
2013-05-10 18:24:07 -04:00
|
|
|
if err := json.Unmarshal(r.Body.Bytes(), &history); err != nil {
|
2013-05-09 20:51:27 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(history) != 1 {
|
2013-07-02 17:47:43 -04:00
|
|
|
t.Errorf("Expected 1 line, %d found", len(history))
|
2013-05-09 20:51:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-09 23:13:52 -04:00
|
|
|
func TestGetImagesByName(t *testing.T) {
|
2013-07-11 20:59:25 -04:00
|
|
|
runtime := mkRuntime(t)
|
2013-05-09 20:51:27 -04:00
|
|
|
defer nuke(runtime)
|
|
|
|
|
|
|
|
srv := &Server{runtime: runtime}
|
|
|
|
|
2013-05-10 18:24:07 -04:00
|
|
|
r := httptest.NewRecorder()
|
2013-06-04 14:00:22 -04:00
|
|
|
if err := getImagesByName(srv, APIVERSION, r, nil, map[string]string{"name": unitTestImageName}); err != nil {
|
2013-05-09 20:51:27 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-05-09 23:13:52 -04:00
|
|
|
img := &Image{}
|
2013-05-10 18:24:07 -04:00
|
|
|
if err := json.Unmarshal(r.Body.Bytes(), img); err != nil {
|
2013-05-09 20:51:27 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-07-02 18:27:22 -04:00
|
|
|
if img.ID != unitTestImageID {
|
2013-05-09 23:13:52 -04:00
|
|
|
t.Errorf("Error inspecting image")
|
|
|
|
}
|
|
|
|
}
|
2013-05-09 20:51:27 -04:00
|
|
|
|
2013-06-04 14:00:22 -04:00
|
|
|
func TestGetContainersJSON(t *testing.T) {
|
2013-07-11 20:59:25 -04:00
|
|
|
runtime := mkRuntime(t)
|
2013-05-09 23:13:52 -04:00
|
|
|
defer nuke(runtime)
|
2013-05-09 20:51:27 -04:00
|
|
|
|
2013-05-09 23:13:52 -04:00
|
|
|
srv := &Server{runtime: runtime}
|
|
|
|
|
2013-10-08 15:15:29 -04:00
|
|
|
beginLen := runtime.containers.Len()
|
|
|
|
|
2013-10-04 22:25:15 -04:00
|
|
|
container, _, err := runtime.Create(&Config{
|
2013-06-04 14:00:22 -04:00
|
|
|
Image: GetTestImage(runtime).ID,
|
2013-05-09 23:13:52 -04:00
|
|
|
Cmd: []string{"echo", "test"},
|
2013-10-28 19:58:59 -04:00
|
|
|
}, "")
|
2013-05-09 20:51:27 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-05-09 23:13:52 -04:00
|
|
|
defer runtime.Destroy(container)
|
|
|
|
|
2013-05-28 12:08:05 -04:00
|
|
|
req, err := http.NewRequest("GET", "/containers/json?all=1", nil)
|
2013-05-09 23:13:52 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-05-10 18:24:07 -04:00
|
|
|
r := httptest.NewRecorder()
|
2013-06-04 14:00:22 -04:00
|
|
|
if err := getContainersJSON(srv, APIVERSION, r, req, nil); err != nil {
|
2013-05-09 23:13:52 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-06-04 14:00:22 -04:00
|
|
|
containers := []APIContainers{}
|
2013-05-10 18:24:07 -04:00
|
|
|
if err := json.Unmarshal(r.Body.Bytes(), &containers); err != nil {
|
2013-05-09 23:13:52 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-10-08 21:42:53 -04:00
|
|
|
if len(containers) != beginLen+1 {
|
|
|
|
t.Fatalf("Expected %d container, %d found (started with: %d)", beginLen+1, len(containers), beginLen)
|
2013-05-09 23:13:52 -04:00
|
|
|
}
|
2013-06-04 14:00:22 -04:00
|
|
|
if containers[0].ID != container.ID {
|
|
|
|
t.Fatalf("Container ID mismatch. Expected: %s, received: %s\n", container.ID, containers[0].ID)
|
2013-05-09 20:51:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-09 23:13:52 -04:00
|
|
|
func TestGetContainersExport(t *testing.T) {
|
2013-07-11 20:59:25 -04:00
|
|
|
runtime := mkRuntime(t)
|
2013-05-10 10:49:47 -04:00
|
|
|
defer nuke(runtime)
|
|
|
|
|
|
|
|
srv := &Server{runtime: runtime}
|
|
|
|
|
|
|
|
// Create a container and remove a file
|
2013-10-04 22:25:15 -04:00
|
|
|
container, _, err := runtime.Create(
|
2013-05-10 10:49:47 -04:00
|
|
|
&Config{
|
2013-06-04 14:00:22 -04:00
|
|
|
Image: GetTestImage(runtime).ID,
|
2013-05-10 15:28:07 -04:00
|
|
|
Cmd: []string{"touch", "/test"},
|
2013-05-10 10:49:47 -04:00
|
|
|
},
|
2013-10-28 19:58:59 -04:00
|
|
|
"",
|
2013-05-10 10:49:47 -04: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 14:00:22 -04:00
|
|
|
if err = getContainersExport(srv, APIVERSION, r, nil, map[string]string{"name": container.ID}); err != nil {
|
2013-05-10 10:49:47 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.Code != http.StatusOK {
|
|
|
|
t.Fatalf("%d OK expected, received %d\n", http.StatusOK, r.Code)
|
|
|
|
}
|
|
|
|
|
2013-05-10 15:28:07 -04: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 10:49:47 -04:00
|
|
|
}
|
2013-05-09 23:13:52 -04:00
|
|
|
}
|
|
|
|
|
2013-05-10 00:54:43 -04:00
|
|
|
func TestGetContainersChanges(t *testing.T) {
|
2013-07-11 20:59:25 -04:00
|
|
|
runtime := mkRuntime(t)
|
2013-05-10 00:54:43 -04:00
|
|
|
defer nuke(runtime)
|
|
|
|
|
|
|
|
srv := &Server{runtime: runtime}
|
2013-05-09 23:13:52 -04:00
|
|
|
|
2013-05-10 00:54:43 -04:00
|
|
|
// Create a container and remove a file
|
2013-10-04 22:25:15 -04:00
|
|
|
container, _, err := runtime.Create(
|
2013-05-10 00:54:43 -04:00
|
|
|
&Config{
|
2013-06-04 14:00:22 -04:00
|
|
|
Image: GetTestImage(runtime).ID,
|
2013-05-10 00:54:43 -04:00
|
|
|
Cmd: []string{"/bin/rm", "/etc/passwd"},
|
|
|
|
},
|
2013-10-28 19:58:59 -04:00
|
|
|
"",
|
2013-05-10 00:54:43 -04:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer runtime.Destroy(container)
|
2013-05-09 23:13:52 -04:00
|
|
|
|
2013-05-10 00:54:43 -04:00
|
|
|
if err := container.Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-05-10 18:24:07 -04:00
|
|
|
r := httptest.NewRecorder()
|
2013-06-04 14:00:22 -04:00
|
|
|
if err := getContainersChanges(srv, APIVERSION, r, nil, map[string]string{"name": container.ID}); err != nil {
|
2013-05-10 00:54:43 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
changes := []Change{}
|
2013-05-10 18:24:07 -04:00
|
|
|
if err := json.Unmarshal(r.Body.Bytes(), &changes); err != nil {
|
2013-05-10 00:54:43 -04: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-09 23:13:52 -04:00
|
|
|
|
2013-07-01 11:19:42 -04:00
|
|
|
func TestGetContainersTop(t *testing.T) {
|
2013-09-09 18:11:30 -04:00
|
|
|
t.Skip("Fixme. Skipping test for now. Reported error when testing using dind: 'api_test.go:527: Expected 2 processes, found 0.'")
|
2013-10-18 02:44:30 -04:00
|
|
|
runtime := mkRuntime(t)
|
2013-06-28 12:27:00 -04:00
|
|
|
defer nuke(runtime)
|
|
|
|
|
|
|
|
srv := &Server{runtime: runtime}
|
|
|
|
|
2013-10-04 22:25:15 -04:00
|
|
|
container, _, err := runtime.Create(
|
2013-06-28 12:27:00 -04:00
|
|
|
&Config{
|
2013-08-07 19:35:38 -04:00
|
|
|
Image: GetTestImage(runtime).ID,
|
|
|
|
Cmd: []string{"/bin/sh", "-c", "cat"},
|
|
|
|
OpenStdin: true,
|
2013-06-28 12:27:00 -04:00
|
|
|
},
|
2013-10-28 19:58:59 -04:00
|
|
|
"",
|
2013-06-28 12:27:00 -04:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer runtime.Destroy(container)
|
2013-08-07 19:35:38 -04:00
|
|
|
defer func() {
|
2013-08-12 13:53:06 -04:00
|
|
|
// Make sure the process dies before destroying runtime
|
2013-08-07 19:35:38 -04:00
|
|
|
container.stdin.Close()
|
|
|
|
container.WaitTimeout(2 * time.Second)
|
|
|
|
}()
|
|
|
|
|
2013-10-31 17:58:43 -04:00
|
|
|
if err := container.Start(); err != nil {
|
2013-06-28 12:27:00 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-08-07 19:35:38 -04:00
|
|
|
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 12:27:00 -04:00
|
|
|
|
|
|
|
if !container.State.Running {
|
2013-08-07 19:35:38 -04:00
|
|
|
t.Fatalf("Container should be running")
|
2013-06-28 12:27:00 -04:00
|
|
|
}
|
|
|
|
|
2013-08-07 19:35:38 -04: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 12:27:00 -04:00
|
|
|
r := httptest.NewRecorder()
|
2013-07-19 06:06:32 -04: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 12:27:00 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-07-19 06:06:32 -04:00
|
|
|
procs := APITop{}
|
2013-06-28 12:27:00 -04:00
|
|
|
if err := json.Unmarshal(r.Body.Bytes(), &procs); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-07-19 06:06:32 -04:00
|
|
|
if len(procs.Titles) != 11 {
|
|
|
|
t.Fatalf("Expected 11 titles, found %d.", len(procs.Titles))
|
2013-06-28 12:27:00 -04:00
|
|
|
}
|
2013-07-19 06:06:32 -04: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 12:27:00 -04:00
|
|
|
}
|
|
|
|
|
2013-07-19 06:06:32 -04:00
|
|
|
if len(procs.Processes) != 2 {
|
|
|
|
t.Fatalf("Expected 2 processes, found %d.", len(procs.Processes))
|
|
|
|
}
|
2013-08-07 19:35:38 -04:00
|
|
|
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 06:06:32 -04:00
|
|
|
}
|
2013-08-07 19:35:38 -04: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 12:27:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-10 00:53:28 -04:00
|
|
|
func TestGetContainersByName(t *testing.T) {
|
2013-07-11 20:59:25 -04:00
|
|
|
runtime := mkRuntime(t)
|
2013-05-10 10:49:47 -04:00
|
|
|
defer nuke(runtime)
|
|
|
|
|
|
|
|
srv := &Server{runtime: runtime}
|
|
|
|
|
|
|
|
// Create a container and remove a file
|
2013-10-04 22:25:15 -04:00
|
|
|
container, _, err := runtime.Create(
|
2013-05-10 10:49:47 -04:00
|
|
|
&Config{
|
2013-06-04 14:00:22 -04:00
|
|
|
Image: GetTestImage(runtime).ID,
|
2013-05-10 15:28:07 -04:00
|
|
|
Cmd: []string{"echo", "test"},
|
2013-05-10 10:49:47 -04:00
|
|
|
},
|
2013-10-28 19:58:59 -04:00
|
|
|
"",
|
2013-05-10 10:49:47 -04:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer runtime.Destroy(container)
|
|
|
|
|
2013-05-10 18:24:07 -04:00
|
|
|
r := httptest.NewRecorder()
|
2013-06-04 14:00:22 -04:00
|
|
|
if err := getContainersByName(srv, APIVERSION, r, nil, map[string]string{"name": container.ID}); err != nil {
|
2013-05-10 10:49:47 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-05-10 15:28:07 -04:00
|
|
|
outContainer := &Container{}
|
2013-05-10 18:24:07 -04:00
|
|
|
if err := json.Unmarshal(r.Body.Bytes(), outContainer); err != nil {
|
2013-05-10 10:49:47 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-06-04 14:00:22 -04:00
|
|
|
if outContainer.ID != container.ID {
|
2013-08-12 13:53:06 -04:00
|
|
|
t.Fatalf("Wrong containers retrieved. Expected %s, received %s", container.ID, outContainer.ID)
|
2013-05-10 15:28:07 -04:00
|
|
|
}
|
2013-05-09 23:13:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostCommit(t *testing.T) {
|
2013-07-11 20:59:25 -04:00
|
|
|
runtime := mkRuntime(t)
|
2013-05-10 10:49:47 -04:00
|
|
|
defer nuke(runtime)
|
|
|
|
|
|
|
|
srv := &Server{runtime: runtime}
|
|
|
|
|
|
|
|
// Create a container and remove a file
|
2013-10-04 22:25:15 -04:00
|
|
|
container, _, err := runtime.Create(
|
2013-05-10 10:49:47 -04:00
|
|
|
&Config{
|
2013-06-04 14:00:22 -04:00
|
|
|
Image: GetTestImage(runtime).ID,
|
2013-05-10 15:28:07 -04:00
|
|
|
Cmd: []string{"touch", "/test"},
|
2013-05-10 10:49:47 -04:00
|
|
|
},
|
2013-10-28 19:58:59 -04:00
|
|
|
"",
|
2013-05-10 10:49:47 -04:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer runtime.Destroy(container)
|
|
|
|
|
|
|
|
if err := container.Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-06-04 14:00:22 -04:00
|
|
|
req, err := http.NewRequest("POST", "/commit?repo=testrepo&testtag=tag&container="+container.ID, bytes.NewReader([]byte{}))
|
2013-05-10 10:49:47 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-05-10 18:24:07 -04:00
|
|
|
r := httptest.NewRecorder()
|
2013-06-04 14:00:22 -04:00
|
|
|
if err := postCommit(srv, APIVERSION, r, req, nil); err != nil {
|
2013-05-10 10:49:47 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if r.Code != http.StatusCreated {
|
|
|
|
t.Fatalf("%d Created expected, received %d\n", http.StatusCreated, r.Code)
|
|
|
|
}
|
2013-05-10 15:28:07 -04:00
|
|
|
|
2013-06-04 14:00:22 -04:00
|
|
|
apiID := &APIID{}
|
|
|
|
if err := json.Unmarshal(r.Body.Bytes(), apiID); err != nil {
|
2013-05-10 15:28:07 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-06-04 14:00:22 -04:00
|
|
|
if _, err := runtime.graph.Get(apiID.ID); err != nil {
|
2013-05-10 15:28:07 -04:00
|
|
|
t.Fatalf("The image has not been commited")
|
|
|
|
}
|
2013-05-09 23:13:52 -04:00
|
|
|
}
|
|
|
|
|
2013-05-09 22:19:24 -04:00
|
|
|
func TestPostContainersCreate(t *testing.T) {
|
2013-07-11 20:59:25 -04:00
|
|
|
runtime := mkRuntime(t)
|
2013-05-09 19:20:07 -04:00
|
|
|
defer nuke(runtime)
|
2013-05-09 18:47:06 -04:00
|
|
|
|
2013-05-09 19:20:07 -04:00
|
|
|
srv := &Server{runtime: runtime}
|
2013-05-09 18:47:06 -04:00
|
|
|
|
2013-06-04 14:00:22 -04:00
|
|
|
configJSON, err := json.Marshal(&Config{
|
|
|
|
Image: GetTestImage(runtime).ID,
|
2013-05-09 22:19:24 -04:00
|
|
|
Memory: 33554432,
|
|
|
|
Cmd: []string{"touch", "/test"},
|
|
|
|
})
|
2013-05-09 19:20:07 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-05-09 18:47:06 -04:00
|
|
|
|
2013-06-04 14:00:22 -04:00
|
|
|
req, err := http.NewRequest("POST", "/containers/create", bytes.NewReader(configJSON))
|
2013-05-09 19:20:07 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-05-09 18:47:06 -04:00
|
|
|
|
2013-05-10 18:24:07 -04:00
|
|
|
r := httptest.NewRecorder()
|
2013-06-04 14:00:22 -04:00
|
|
|
if err := postContainersCreate(srv, APIVERSION, r, req, nil); err != nil {
|
2013-05-09 19:20:07 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-05-09 22:19:24 -04:00
|
|
|
if r.Code != http.StatusCreated {
|
|
|
|
t.Fatalf("%d Created expected, received %d\n", http.StatusCreated, r.Code)
|
|
|
|
}
|
2013-05-09 18:47:06 -04:00
|
|
|
|
2013-06-04 14:00:22 -04:00
|
|
|
apiRun := &APIRun{}
|
2013-05-10 18:24:07 -04:00
|
|
|
if err := json.Unmarshal(r.Body.Bytes(), apiRun); err != nil {
|
2013-05-09 19:20:07 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-05-09 18:47:06 -04:00
|
|
|
|
2013-06-04 14:00:22 -04:00
|
|
|
container := srv.runtime.Get(apiRun.ID)
|
2013-05-09 22:19:24 -04:00
|
|
|
if container == nil {
|
|
|
|
t.Fatalf("Container not created")
|
2013-05-09 19:20:07 -04:00
|
|
|
}
|
|
|
|
|
2013-05-09 22:19:24 -04: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 18:37:35 -04:00
|
|
|
utils.Debugf("Err: %s", err)
|
2013-05-09 22:19:24 -04:00
|
|
|
t.Fatalf("The test file has not been created")
|
|
|
|
}
|
|
|
|
t.Fatal(err)
|
2013-05-09 19:20:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-09 23:13:52 -04:00
|
|
|
func TestPostContainersKill(t *testing.T) {
|
2013-07-11 20:59:25 -04:00
|
|
|
runtime := mkRuntime(t)
|
2013-05-09 22:19:24 -04:00
|
|
|
defer nuke(runtime)
|
2013-05-09 19:20:07 -04:00
|
|
|
|
2013-05-09 22:19:24 -04:00
|
|
|
srv := &Server{runtime: runtime}
|
2013-05-09 19:20:07 -04:00
|
|
|
|
2013-10-04 22:25:15 -04:00
|
|
|
container, _, err := runtime.Create(
|
2013-05-09 23:13:52 -04:00
|
|
|
&Config{
|
2013-06-04 14:00:22 -04:00
|
|
|
Image: GetTestImage(runtime).ID,
|
2013-05-09 23:13:52 -04:00
|
|
|
Cmd: []string{"/bin/cat"},
|
|
|
|
OpenStdin: true,
|
|
|
|
},
|
2013-10-28 19:58:59 -04:00
|
|
|
"",
|
2013-05-09 23:13:52 -04:00
|
|
|
)
|
2013-05-09 19:20:07 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-05-09 22:19:24 -04:00
|
|
|
defer runtime.Destroy(container)
|
2013-05-09 19:20:07 -04:00
|
|
|
|
2013-10-31 17:58:43 -04:00
|
|
|
if err := container.Start(); err != nil {
|
2013-05-09 19:20:07 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-05-09 23:13:52 -04: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-09 22:19:24 -04:00
|
|
|
}
|
2013-05-09 23:13:52 -04:00
|
|
|
|
|
|
|
r := httptest.NewRecorder()
|
2013-06-04 14:00:22 -04:00
|
|
|
if err := postContainersKill(srv, APIVERSION, r, nil, map[string]string{"name": container.ID}); err != nil {
|
2013-05-09 19:20:07 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-05-09 23:13:52 -04: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 19:20:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-09 23:13:52 -04:00
|
|
|
func TestPostContainersRestart(t *testing.T) {
|
2013-07-11 20:59:25 -04:00
|
|
|
runtime := mkRuntime(t)
|
2013-05-09 22:19:24 -04:00
|
|
|
defer nuke(runtime)
|
2013-05-09 19:20:07 -04:00
|
|
|
|
2013-05-09 22:19:24 -04:00
|
|
|
srv := &Server{runtime: runtime}
|
2013-05-09 19:20:07 -04:00
|
|
|
|
2013-10-04 22:25:15 -04:00
|
|
|
container, _, err := runtime.Create(
|
2013-05-09 22:19:24 -04:00
|
|
|
&Config{
|
2013-06-04 14:00:22 -04:00
|
|
|
Image: GetTestImage(runtime).ID,
|
2013-10-24 18:06:10 -04:00
|
|
|
Cmd: []string{"/bin/top"},
|
2013-05-09 22:19:24 -04:00
|
|
|
OpenStdin: true,
|
|
|
|
},
|
2013-10-28 19:58:59 -04:00
|
|
|
"",
|
2013-05-09 22:19:24 -04:00
|
|
|
)
|
2013-05-09 19:20:07 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-05-09 22:19:24 -04:00
|
|
|
defer runtime.Destroy(container)
|
|
|
|
|
2013-10-31 17:58:43 -04:00
|
|
|
if err := container.Start(); err != nil {
|
2013-05-09 23:13:52 -04: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 14:00:22 -04:00
|
|
|
req, err := http.NewRequest("POST", "/containers/"+container.ID+"/restart?t=1", bytes.NewReader([]byte{}))
|
2013-05-09 23:13:52 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-05-10 18:24:07 -04:00
|
|
|
r := httptest.NewRecorder()
|
2013-06-04 14:00:22 -04:00
|
|
|
if err := postContainersRestart(srv, APIVERSION, r, req, map[string]string{"name": container.ID}); err != nil {
|
2013-05-09 19:20:07 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if r.Code != http.StatusNoContent {
|
|
|
|
t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
|
|
|
|
}
|
2013-05-09 22:19:24 -04:00
|
|
|
|
2013-05-09 23:13:52 -04:00
|
|
|
// Give some time to the process to restart
|
2013-05-09 22:19:24 -04:00
|
|
|
container.WaitTimeout(500 * time.Millisecond)
|
|
|
|
|
|
|
|
if !container.State.Running {
|
2013-05-09 23:13:52 -04:00
|
|
|
t.Fatalf("Container should be running")
|
2013-05-09 22:19:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := container.Kill(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-05-09 19:20:07 -04:00
|
|
|
}
|
|
|
|
|
2013-05-09 23:13:52 -04:00
|
|
|
func TestPostContainersStart(t *testing.T) {
|
2013-07-11 20:59:25 -04:00
|
|
|
runtime := mkRuntime(t)
|
2013-05-09 23:13:52 -04:00
|
|
|
defer nuke(runtime)
|
2013-05-09 19:20:07 -04:00
|
|
|
|
2013-05-09 23:13:52 -04:00
|
|
|
srv := &Server{runtime: runtime}
|
2013-05-09 19:20:07 -04:00
|
|
|
|
2013-10-04 22:25:15 -04:00
|
|
|
container, _, err := runtime.Create(
|
2013-05-09 23:13:52 -04:00
|
|
|
&Config{
|
2013-06-04 14:00:22 -04:00
|
|
|
Image: GetTestImage(runtime).ID,
|
2013-05-09 23:13:52 -04:00
|
|
|
Cmd: []string{"/bin/cat"},
|
|
|
|
OpenStdin: true,
|
|
|
|
},
|
2013-10-28 19:58:59 -04:00
|
|
|
"",
|
2013-05-09 23:13:52 -04:00
|
|
|
)
|
2013-05-09 19:20:07 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-05-09 23:13:52 -04:00
|
|
|
defer runtime.Destroy(container)
|
2013-05-09 19:20:07 -04:00
|
|
|
|
2013-05-13 19:39:54 -04: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-10-16 15:32:03 -04:00
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
2013-05-09 23:13:52 -04:00
|
|
|
r := httptest.NewRecorder()
|
2013-05-13 19:39:54 -04:00
|
|
|
if err := postContainersStart(srv, APIVERSION, r, req, map[string]string{"name": container.ID}); err != nil {
|
2013-05-09 19:20:07 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if r.Code != http.StatusNoContent {
|
|
|
|
t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
|
|
|
|
}
|
2013-05-09 23:13:52 -04: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 18:24:07 -04:00
|
|
|
r = httptest.NewRecorder()
|
2013-05-13 19:39:54 -04:00
|
|
|
if err = postContainersStart(srv, APIVERSION, r, req, map[string]string{"name": container.ID}); err == nil {
|
2013-08-12 13:53:06 -04:00
|
|
|
t.Fatalf("A running container should be able to be started")
|
2013-05-09 23:13:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := container.Kill(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-05-09 19:20:07 -04:00
|
|
|
}
|
|
|
|
|
2013-05-09 22:19:24 -04:00
|
|
|
func TestPostContainersStop(t *testing.T) {
|
2013-07-11 20:59:25 -04:00
|
|
|
runtime := mkRuntime(t)
|
2013-05-09 22:19:24 -04:00
|
|
|
defer nuke(runtime)
|
2013-05-09 19:20:07 -04:00
|
|
|
|
2013-05-09 22:19:24 -04:00
|
|
|
srv := &Server{runtime: runtime}
|
2013-05-09 19:20:07 -04:00
|
|
|
|
2013-10-04 22:25:15 -04:00
|
|
|
container, _, err := runtime.Create(
|
2013-05-09 22:19:24 -04:00
|
|
|
&Config{
|
2013-06-04 14:00:22 -04:00
|
|
|
Image: GetTestImage(runtime).ID,
|
2013-10-24 18:06:10 -04:00
|
|
|
Cmd: []string{"/bin/top"},
|
2013-05-09 22:19:24 -04:00
|
|
|
OpenStdin: true,
|
|
|
|
},
|
2013-10-28 19:58:59 -04:00
|
|
|
"",
|
2013-05-09 22:19:24 -04:00
|
|
|
)
|
2013-05-09 19:20:07 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-05-09 22:19:24 -04:00
|
|
|
defer runtime.Destroy(container)
|
2013-05-09 19:20:07 -04:00
|
|
|
|
2013-10-31 17:58:43 -04:00
|
|
|
if err := container.Start(); err != nil {
|
2013-05-09 19:20:07 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-05-09 22:19:24 -04: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 14:00:22 -04:00
|
|
|
req, err := http.NewRequest("POST", "/containers/"+container.ID+"/stop?t=1", bytes.NewReader([]byte{}))
|
2013-05-09 22:19:24 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-05-10 18:24:07 -04:00
|
|
|
r := httptest.NewRecorder()
|
2013-06-04 14:00:22 -04:00
|
|
|
if err := postContainersStop(srv, APIVERSION, r, req, map[string]string{"name": container.ID}); err != nil {
|
2013-05-09 22:19:24 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-05-09 19:20:07 -04:00
|
|
|
if r.Code != http.StatusNoContent {
|
|
|
|
t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
|
|
|
|
}
|
2013-05-09 22:19:24 -04:00
|
|
|
if container.State.Running {
|
|
|
|
t.Fatalf("The container hasn't been stopped")
|
|
|
|
}
|
2013-05-09 19:20:07 -04:00
|
|
|
}
|
|
|
|
|
2013-05-09 23:13:52 -04:00
|
|
|
func TestPostContainersWait(t *testing.T) {
|
2013-07-11 20:59:25 -04:00
|
|
|
runtime := mkRuntime(t)
|
2013-05-09 23:13:52 -04:00
|
|
|
defer nuke(runtime)
|
2013-05-09 19:20:07 -04:00
|
|
|
|
2013-05-09 23:13:52 -04:00
|
|
|
srv := &Server{runtime: runtime}
|
2013-05-09 19:20:07 -04:00
|
|
|
|
2013-10-04 22:25:15 -04:00
|
|
|
container, _, err := runtime.Create(
|
2013-05-09 23:13:52 -04:00
|
|
|
&Config{
|
2013-06-04 14:00:22 -04:00
|
|
|
Image: GetTestImage(runtime).ID,
|
2013-05-09 23:13:52 -04:00
|
|
|
Cmd: []string{"/bin/sleep", "1"},
|
|
|
|
OpenStdin: true,
|
|
|
|
},
|
2013-10-28 19:58:59 -04:00
|
|
|
"",
|
2013-05-09 23:13:52 -04:00
|
|
|
)
|
2013-05-09 19:20:07 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-05-09 23:13:52 -04:00
|
|
|
defer runtime.Destroy(container)
|
2013-05-09 19:20:07 -04:00
|
|
|
|
2013-10-31 17:58:43 -04:00
|
|
|
if err := container.Start(); err != nil {
|
2013-05-09 19:20:07 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-05-09 23:13:52 -04:00
|
|
|
setTimeout(t, "Wait timed out", 3*time.Second, func() {
|
2013-05-10 18:24:07 -04:00
|
|
|
r := httptest.NewRecorder()
|
2013-06-04 14:00:22 -04:00
|
|
|
if err := postContainersWait(srv, APIVERSION, r, nil, map[string]string{"name": container.ID}); err != nil {
|
2013-05-09 23:13:52 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-06-04 14:00:22 -04:00
|
|
|
apiWait := &APIWait{}
|
2013-05-10 18:24:07 -04:00
|
|
|
if err := json.Unmarshal(r.Body.Bytes(), apiWait); err != nil {
|
2013-05-09 23:13:52 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if apiWait.StatusCode != 0 {
|
|
|
|
t.Fatalf("Non zero exit code for sleep: %d\n", apiWait.StatusCode)
|
|
|
|
}
|
|
|
|
})
|
2013-05-09 19:20:07 -04:00
|
|
|
|
2013-05-09 23:13:52 -04:00
|
|
|
if container.State.Running {
|
|
|
|
t.Fatalf("The container should be stopped after wait")
|
2013-05-09 19:20:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-09 23:13:52 -04:00
|
|
|
func TestPostContainersAttach(t *testing.T) {
|
2013-07-11 20:59:25 -04:00
|
|
|
runtime := mkRuntime(t)
|
2013-05-10 00:55:08 -04:00
|
|
|
defer nuke(runtime)
|
|
|
|
|
|
|
|
srv := &Server{runtime: runtime}
|
|
|
|
|
2013-10-04 22:25:15 -04:00
|
|
|
container, _, err := runtime.Create(
|
2013-05-10 00:55:08 -04:00
|
|
|
&Config{
|
2013-06-04 14:00:22 -04:00
|
|
|
Image: GetTestImage(runtime).ID,
|
2013-05-10 00:55:08 -04:00
|
|
|
Cmd: []string{"/bin/cat"},
|
|
|
|
OpenStdin: true,
|
|
|
|
},
|
2013-10-28 19:58:59 -04:00
|
|
|
"",
|
2013-05-10 00:55:08 -04:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer runtime.Destroy(container)
|
|
|
|
|
|
|
|
// Start the process
|
2013-10-31 17:58:43 -04:00
|
|
|
if err := container.Start(); err != nil {
|
2013-05-10 00:55:08 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
stdin, stdinPipe := io.Pipe()
|
|
|
|
stdout, stdoutPipe := io.Pipe()
|
|
|
|
|
2013-08-12 13:53:06 -04:00
|
|
|
// Try to avoid the timeout in destroy. Best effort, don't check error
|
2013-07-24 18:48:18 -04:00
|
|
|
defer func() {
|
|
|
|
closeWrap(stdin, stdinPipe, stdout, stdoutPipe)
|
|
|
|
container.Kill()
|
|
|
|
}()
|
|
|
|
|
2013-05-10 00:55:08 -04:00
|
|
|
// Attach to it
|
|
|
|
c1 := make(chan struct{})
|
|
|
|
go func() {
|
2013-05-10 18:24:07 -04:00
|
|
|
defer close(c1)
|
2013-05-10 00:55:08 -04:00
|
|
|
|
|
|
|
r := &hijackTester{
|
|
|
|
ResponseRecorder: httptest.NewRecorder(),
|
|
|
|
in: stdin,
|
|
|
|
out: stdoutPipe,
|
|
|
|
}
|
|
|
|
|
2013-06-04 14:00:22 -04:00
|
|
|
req, err := http.NewRequest("POST", "/containers/"+container.ID+"/attach?stream=1&stdin=1&stdout=1&stderr=1", bytes.NewReader([]byte{}))
|
2013-05-10 00:55:08 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-06-04 14:00:22 -04:00
|
|
|
if err := postContainersAttach(srv, APIVERSION, r, req, map[string]string{"name": container.ID}); err != nil {
|
2013-05-10 00:55:08 -04:00
|
|
|
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() {
|
2013-09-26 18:59:02 -04:00
|
|
|
if err := assertPipe("hello\n", string([]byte{1, 0, 0, 0, 0, 0, 0, 6})+"hello", stdout, stdinPipe, 15); err != nil {
|
2013-05-10 00:55:08 -04:00
|
|
|
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
|
2013-07-24 18:48:18 -04:00
|
|
|
setTimeout(t, "Waiting for CmdAttach timed out", 10*time.Second, func() {
|
2013-05-10 00:55:08 -04:00
|
|
|
<-c1
|
|
|
|
})
|
|
|
|
|
2013-09-09 21:23:03 -04:00
|
|
|
// 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 timeout in destroy. Best effort, don't check error
|
|
|
|
cStdin, _ := container.StdinPipe()
|
|
|
|
cStdin.Close()
|
|
|
|
container.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostContainersAttachStderr(t *testing.T) {
|
|
|
|
runtime := mkRuntime(t)
|
|
|
|
defer nuke(runtime)
|
|
|
|
|
|
|
|
srv := &Server{runtime: runtime}
|
|
|
|
|
2013-10-04 22:25:15 -04:00
|
|
|
container, _, err := runtime.Create(
|
2013-09-09 21:23:03 -04:00
|
|
|
&Config{
|
|
|
|
Image: GetTestImage(runtime).ID,
|
|
|
|
Cmd: []string{"/bin/sh", "-c", "/bin/cat >&2"},
|
|
|
|
OpenStdin: true,
|
|
|
|
},
|
2013-10-28 19:58:59 -04:00
|
|
|
"",
|
2013-09-09 21:23:03 -04:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer runtime.Destroy(container)
|
|
|
|
|
|
|
|
// Start the process
|
2013-10-31 17:58:43 -04:00
|
|
|
if err := container.Start(); err != nil {
|
2013-09-09 21:23:03 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
stdin, stdinPipe := io.Pipe()
|
|
|
|
stdout, stdoutPipe := io.Pipe()
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2013-09-26 18:59:02 -04:00
|
|
|
if err := assertPipe("hello\n", string([]byte{2, 0, 0, 0, 0, 0, 0, 6})+"hello", stdout, stdinPipe, 15); err != nil {
|
2013-05-10 00:55:08 -04:00
|
|
|
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
|
2013-07-24 18:48:18 -04:00
|
|
|
setTimeout(t, "Waiting for CmdAttach timed out", 10*time.Second, func() {
|
2013-05-10 00:55:08 -04:00
|
|
|
<-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 13:53:06 -04:00
|
|
|
// Try to avoid the timeout in destroy. Best effort, don't check error
|
2013-05-10 00:55:08 -04:00
|
|
|
cStdin, _ := container.StdinPipe()
|
|
|
|
cStdin.Close()
|
|
|
|
container.Wait()
|
2013-05-09 23:13:52 -04:00
|
|
|
}
|
|
|
|
|
2013-05-10 15:28:07 -04:00
|
|
|
// FIXME: Test deleting running container
|
2013-05-09 22:19:24 -04:00
|
|
|
// FIXME: Test deleting container with volume
|
|
|
|
// FIXME: Test deleting volume in use by other container
|
|
|
|
func TestDeleteContainers(t *testing.T) {
|
2013-07-11 20:59:25 -04:00
|
|
|
runtime := mkRuntime(t)
|
2013-05-09 22:19:24 -04:00
|
|
|
defer nuke(runtime)
|
2013-05-09 19:20:07 -04:00
|
|
|
|
2013-05-09 22:19:24 -04:00
|
|
|
srv := &Server{runtime: runtime}
|
2013-05-09 19:20:07 -04:00
|
|
|
|
2013-10-04 22:25:15 -04:00
|
|
|
container, _, err := runtime.Create(&Config{
|
2013-06-04 14:00:22 -04:00
|
|
|
Image: GetTestImage(runtime).ID,
|
2013-05-09 22:19:24 -04:00
|
|
|
Cmd: []string{"touch", "/test"},
|
2013-10-28 19:58:59 -04:00
|
|
|
}, "")
|
2013-05-09 19:20:07 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-05-09 22:19:24 -04:00
|
|
|
defer runtime.Destroy(container)
|
|
|
|
|
|
|
|
if err := container.Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-06-04 14:00:22 -04:00
|
|
|
req, err := http.NewRequest("DELETE", "/containers/"+container.ID, nil)
|
2013-05-09 19:20:07 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-05-10 18:24:07 -04:00
|
|
|
r := httptest.NewRecorder()
|
2013-06-04 14:00:22 -04:00
|
|
|
if err := deleteContainers(srv, APIVERSION, r, req, map[string]string{"name": container.ID}); err != nil {
|
2013-05-09 22:19:24 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-05-09 19:20:07 -04:00
|
|
|
if r.Code != http.StatusNoContent {
|
|
|
|
t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
|
|
|
|
}
|
2013-05-09 22:19:24 -04:00
|
|
|
|
2013-06-04 14:00:22 -04:00
|
|
|
if c := runtime.Get(container.ID); c != nil {
|
2013-05-09 22:19:24 -04: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 19:20:07 -04:00
|
|
|
}
|
|
|
|
|
2013-06-10 18:02:40 -04:00
|
|
|
func TestOptionsRoute(t *testing.T) {
|
2013-07-11 20:59:25 -04:00
|
|
|
runtime := mkRuntime(t)
|
2013-06-10 18:02:40 -04:00
|
|
|
defer nuke(runtime)
|
|
|
|
|
2013-10-04 22:25:15 -04:00
|
|
|
runtime.config.EnableCors = true
|
|
|
|
srv := &Server{runtime: runtime}
|
2013-06-10 18:02:40 -04:00
|
|
|
|
|
|
|
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)
|
2013-06-10 19:44:10 -04:00
|
|
|
if r.Code != http.StatusOK {
|
2013-06-10 18:02:40 -04:00
|
|
|
t.Errorf("Expected response for OPTIONS request to be \"200\", %v found.", r.Code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-03 21:39:00 -04:00
|
|
|
func TestGetEnabledCors(t *testing.T) {
|
2013-07-11 20:59:25 -04:00
|
|
|
runtime := mkRuntime(t)
|
2013-06-03 21:39:00 -04:00
|
|
|
defer nuke(runtime)
|
|
|
|
|
2013-10-04 22:25:15 -04:00
|
|
|
runtime.config.EnableCors = true
|
|
|
|
srv := &Server{runtime: runtime}
|
2013-06-03 21:39:00 -04:00
|
|
|
|
|
|
|
r := httptest.NewRecorder()
|
|
|
|
|
2013-06-10 18:02:40 -04:00
|
|
|
router, err := createRouter(srv, false)
|
|
|
|
if err != nil {
|
2013-06-03 21:39:00 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-06-10 18:02:40 -04:00
|
|
|
req, err := http.NewRequest("GET", "/version", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
router.ServeHTTP(r, req)
|
2013-06-10 19:44:10 -04:00
|
|
|
if r.Code != http.StatusOK {
|
2013-06-10 18:02:40 -04:00
|
|
|
t.Errorf("Expected response for OPTIONS request to be \"200\", %v found.", r.Code)
|
|
|
|
}
|
|
|
|
|
2013-06-03 21:39:00 -04:00
|
|
|
allowOrigin := r.Header().Get("Access-Control-Allow-Origin")
|
|
|
|
allowHeaders := r.Header().Get("Access-Control-Allow-Headers")
|
2013-06-10 18:02:40 -04:00
|
|
|
allowMethods := r.Header().Get("Access-Control-Allow-Methods")
|
2013-06-03 21:39:00 -04:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2013-06-10 18:02:40 -04:00
|
|
|
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-06-03 21:39:00 -04:00
|
|
|
}
|
|
|
|
|
2013-05-09 23:13:52 -04:00
|
|
|
func TestDeleteImages(t *testing.T) {
|
2013-07-11 20:59:25 -04:00
|
|
|
runtime := mkRuntime(t)
|
2013-05-15 09:11:39 -04:00
|
|
|
defer nuke(runtime)
|
|
|
|
|
|
|
|
srv := &Server{runtime: runtime}
|
|
|
|
|
2013-07-01 14:45:45 -04:00
|
|
|
initialImages, err := srv.Images(false, "")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-05-15 09:11:39 -04: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)
|
|
|
|
}
|
|
|
|
|
2013-10-13 09:42:01 -04:00
|
|
|
if len(images[0].RepoTags) != len(initialImages[0].RepoTags)+1 {
|
2013-07-02 17:47:43 -04:00
|
|
|
t.Errorf("Expected %d images, %d found", len(initialImages)+1, len(images))
|
2013-05-15 09:11:39 -04:00
|
|
|
}
|
|
|
|
|
2013-07-05 12:58:39 -04:00
|
|
|
req, err := http.NewRequest("DELETE", "/images/"+unitTestImageID, nil)
|
2013-05-20 14:31:45 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-05-15 09:11:39 -04:00
|
|
|
r := httptest.NewRecorder()
|
2013-07-05 12:58:39 -04: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 09:11:39 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-07-05 12:58:39 -04: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 {
|
2013-05-31 10:37:02 -04:00
|
|
|
t.Fatalf("%d OK expected, received %d\n", http.StatusOK, r.Code)
|
2013-05-15 09:11:39 -04:00
|
|
|
}
|
|
|
|
|
2013-06-10 17:05:54 -04:00
|
|
|
var outs []APIRmi
|
2013-07-05 12:58:39 -04:00
|
|
|
if err := json.Unmarshal(r2.Body.Bytes(), &outs); err != nil {
|
2013-05-31 10:37:02 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(outs) != 1 {
|
|
|
|
t.Fatalf("Expected %d event (untagged), got %d", 1, len(outs))
|
|
|
|
}
|
2013-05-15 09:11:39 -04:00
|
|
|
images, err = srv.Images(false, "")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-10-13 09:42:01 -04:00
|
|
|
if len(images[0].RepoTags) != len(initialImages[0].RepoTags) {
|
2013-07-02 17:47:43 -04:00
|
|
|
t.Errorf("Expected %d image, %d found", len(initialImages), len(images))
|
2013-05-15 09:11:39 -04: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-10 00:55:08 -04:00
|
|
|
}
|
|
|
|
|
2013-08-03 16:55:54 -04: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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-17 00:07:41 -04:00
|
|
|
func TestPostContainersCopy(t *testing.T) {
|
|
|
|
runtime := mkRuntime(t)
|
|
|
|
defer nuke(runtime)
|
|
|
|
|
|
|
|
srv := &Server{runtime: runtime}
|
|
|
|
|
|
|
|
// Create a container and remove a file
|
2013-10-04 22:25:15 -04:00
|
|
|
container, _, err := runtime.Create(
|
2013-07-17 00:07:41 -04:00
|
|
|
&Config{
|
|
|
|
Image: GetTestImage(runtime).ID,
|
|
|
|
Cmd: []string{"touch", "/test.txt"},
|
|
|
|
},
|
2013-10-28 19:58:59 -04:00
|
|
|
"",
|
2013-07-17 00:07:41 -04:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer runtime.Destroy(container)
|
|
|
|
|
|
|
|
if err := container.Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r := httptest.NewRecorder()
|
2013-07-17 10:25:49 -04:00
|
|
|
copyData := APICopy{HostPath: ".", Resource: "/test.txt"}
|
2013-07-17 00:07:41 -04:00
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-10 00:55:08 -04:00
|
|
|
// 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 19:20:07 -04:00
|
|
|
}
|