2013-05-08 11:35:50 -04:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2013-11-15 19:05:57 -05:00
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2013-05-08 11:35:50 -04:00
|
|
|
"encoding/json"
|
2013-11-15 19:05:57 -05:00
|
|
|
"fmt"
|
2014-01-29 14:26:54 -05:00
|
|
|
"github.com/dotcloud/docker/api"
|
2014-02-11 21:10:23 -05:00
|
|
|
"github.com/dotcloud/docker/dockerversion"
|
2013-12-08 02:41:53 -05:00
|
|
|
"github.com/dotcloud/docker/engine"
|
2014-03-07 20:36:47 -05:00
|
|
|
"github.com/dotcloud/docker/image"
|
2014-02-11 23:04:39 -05:00
|
|
|
"github.com/dotcloud/docker/runconfig"
|
2014-03-07 21:42:29 -05:00
|
|
|
"github.com/dotcloud/docker/runtime"
|
2013-05-14 21:52:09 -04:00
|
|
|
"github.com/dotcloud/docker/utils"
|
2014-02-27 11:43:44 -05:00
|
|
|
"github.com/dotcloud/docker/vendor/src/code.google.com/p/go/src/pkg/archive/tar"
|
2013-05-10 00:54:43 -04:00
|
|
|
"io"
|
2014-02-17 20:44:53 -05:00
|
|
|
"io/ioutil"
|
2013-11-15 19:05:57 -05:00
|
|
|
"net"
|
2013-05-09 18:47:06 -04:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2013-11-15 19:05:57 -05: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-11-15 19:05:57 -05:00
|
|
|
func TestGetVersion(t *testing.T) {
|
|
|
|
eng := NewTestEngine(t)
|
|
|
|
defer mkRuntimeFromEngine(eng, t).Nuke()
|
2013-06-20 10:34:58 -04:00
|
|
|
|
2013-11-14 01:10:20 -05:00
|
|
|
var err error
|
2013-10-16 15:23:47 -04:00
|
|
|
r := httptest.NewRecorder()
|
|
|
|
|
2013-11-15 19:05:57 -05:00
|
|
|
req, err := http.NewRequest("GET", "/version", nil)
|
|
|
|
if err != nil {
|
2013-11-14 01:10:20 -05:00
|
|
|
t.Fatal(err)
|
2013-10-16 15:23:47 -04:00
|
|
|
}
|
2013-11-14 01:10:20 -05:00
|
|
|
// FIXME getting the version should require an actual running Server
|
2014-01-29 14:26:54 -05:00
|
|
|
if err := api.ServeRequest(eng, api.APIVERSION, r, req); err != nil {
|
2013-05-08 11:35:50 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-15 19:05:57 -05:00
|
|
|
assertHttpNotError(r, t)
|
2013-05-08 11:35:50 -04:00
|
|
|
|
2013-12-08 02:41:53 -05:00
|
|
|
out := engine.NewOutput()
|
|
|
|
v, err := out.AddEnv()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := io.Copy(out, r.Body); err != nil {
|
2013-05-08 11:35:50 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-12-08 02:41:53 -05:00
|
|
|
out.Close()
|
2014-02-11 21:10:23 -05:00
|
|
|
expected := dockerversion.VERSION
|
2013-12-08 02:41:53 -05:00
|
|
|
if result := v.Get("Version"); result != expected {
|
|
|
|
t.Errorf("Expected version %s, %s found", expected, result)
|
2013-05-08 11:35:50 -04:00
|
|
|
}
|
2014-01-08 03:14:00 -05:00
|
|
|
expected = "application/json"
|
|
|
|
if result := r.HeaderMap.Get("Content-Type"); result != expected {
|
|
|
|
t.Errorf("Expected Content-Type %s, %s found", expected, result)
|
|
|
|
}
|
2013-05-08 11:35:50 -04:00
|
|
|
}
|
|
|
|
|
2013-05-09 23:13:52 -04:00
|
|
|
func TestGetInfo(t *testing.T) {
|
2013-11-15 19:05:57 -05:00
|
|
|
eng := NewTestEngine(t)
|
|
|
|
defer mkRuntimeFromEngine(eng, t).Nuke()
|
2013-05-08 11:35:50 -04:00
|
|
|
|
2013-12-12 17:39:35 -05:00
|
|
|
job := eng.Job("images")
|
2014-01-21 18:06:23 -05:00
|
|
|
initialImages, err := job.Stdout.AddListTable()
|
2013-07-01 18:06:08 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-12-12 17:39:35 -05:00
|
|
|
if err := job.Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-15 19:05:57 -05:00
|
|
|
req, err := http.NewRequest("GET", "/info", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-05-10 18:24:07 -04:00
|
|
|
r := httptest.NewRecorder()
|
|
|
|
|
2014-01-29 14:26:54 -05:00
|
|
|
if err := api.ServeRequest(eng, api.APIVERSION, r, req); err != nil {
|
2013-05-08 11:35:50 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-18 14:25:13 -05:00
|
|
|
assertHttpNotError(r, t)
|
2013-05-10 18:24:07 -04:00
|
|
|
|
2013-12-11 13:35:21 -05:00
|
|
|
out := engine.NewOutput()
|
|
|
|
i, err := out.AddEnv()
|
2013-05-08 11:35:50 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-12-11 13:35:21 -05:00
|
|
|
if _, err := io.Copy(out, r.Body); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
out.Close()
|
2013-12-12 17:39:35 -05:00
|
|
|
if images := i.GetInt("Images"); images != initialImages.Len() {
|
|
|
|
t.Errorf("Expected images: %d, %d found", initialImages.Len(), images)
|
2013-05-08 11:35:50 -04:00
|
|
|
}
|
2014-01-08 03:14:00 -05:00
|
|
|
expected := "application/json"
|
|
|
|
if result := r.HeaderMap.Get("Content-Type"); result != expected {
|
|
|
|
t.Errorf("Expected Content-Type %s, %s found", expected, result)
|
|
|
|
}
|
2013-05-08 11:35:50 -04:00
|
|
|
}
|
|
|
|
|
2013-07-18 10:54:58 -04:00
|
|
|
func TestGetEvents(t *testing.T) {
|
2013-11-14 01:10:20 -05:00
|
|
|
eng := NewTestEngine(t)
|
|
|
|
srv := mkServerFromEngine(eng, t)
|
|
|
|
// FIXME: we might not need runtime, why not simply nuke
|
|
|
|
// the engine?
|
|
|
|
runtime := mkRuntimeFromEngine(eng, t)
|
2013-10-16 16:10:20 -04:00
|
|
|
defer nuke(runtime)
|
2013-07-18 10:54:58 -04:00
|
|
|
|
2013-11-14 01:10:20 -05:00
|
|
|
var events []*utils.JSONMessage
|
|
|
|
for _, parts := range [][3]string{
|
|
|
|
{"fakeaction", "fakeid", "fakeimage"},
|
|
|
|
{"fakeaction2", "fakeid", "fakeimage"},
|
|
|
|
} {
|
|
|
|
action, id, from := parts[0], parts[1], parts[2]
|
|
|
|
ev := srv.LogEvent(action, id, from)
|
|
|
|
events = append(events, ev)
|
|
|
|
}
|
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() {
|
2014-01-29 14:26:54 -05:00
|
|
|
if err := api.ServeRequest(eng, api.APIVERSION, r, req); err != nil {
|
2013-07-18 10:54:58 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-15 19:05:57 -05:00
|
|
|
assertHttpNotError(r, t)
|
2013-07-18 10:54:58 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2013-11-14 01:10:20 -05:00
|
|
|
if jm != *events[i] {
|
2013-07-18 10:54:58 -04:00
|
|
|
t.Fatalf("Event received it different than expected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-04 14:00:22 -04:00
|
|
|
func TestGetImagesJSON(t *testing.T) {
|
2013-11-15 19:05:57 -05:00
|
|
|
eng := NewTestEngine(t)
|
|
|
|
defer mkRuntimeFromEngine(eng, t).Nuke()
|
2013-05-08 11:35:50 -04:00
|
|
|
|
2013-12-12 17:39:35 -05:00
|
|
|
job := eng.Job("images")
|
2014-01-21 18:06:23 -05:00
|
|
|
initialImages, err := job.Stdout.AddListTable()
|
2013-07-01 14:45:45 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-12-12 17:39:35 -05:00
|
|
|
if err := job.Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-07-01 14:45:45 -04:00
|
|
|
|
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()
|
|
|
|
|
2014-01-29 14:26:54 -05:00
|
|
|
if err := api.ServeRequest(eng, api.APIVERSION, r, req); err != nil {
|
2013-05-09 19:20:06 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-18 14:25:13 -05:00
|
|
|
assertHttpNotError(r, t)
|
2013-05-09 19:20:06 -04:00
|
|
|
|
2013-12-12 17:39:35 -05:00
|
|
|
images := engine.NewTable("Created", 0)
|
2014-01-21 20:56:09 -05:00
|
|
|
if _, err := images.ReadListFrom(r.Body.Bytes()); err != nil {
|
2013-05-09 19:20:06 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-05-08 11:35:50 -04:00
|
|
|
|
2013-12-12 17:39:35 -05:00
|
|
|
if images.Len() != initialImages.Len() {
|
|
|
|
t.Errorf("Expected %d image, %d found", initialImages.Len(), images.Len())
|
2013-05-09 19:20:06 -04:00
|
|
|
}
|
|
|
|
|
2013-07-01 14:45:45 -04:00
|
|
|
found := false
|
2013-12-12 17:39:35 -05:00
|
|
|
for _, img := range images.Data {
|
|
|
|
if strings.Contains(img.GetList("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
|
|
|
|
2013-12-12 17:39:35 -05:00
|
|
|
initialImages = getAllImages(eng, t)
|
2013-07-01 20:19:39 -04:00
|
|
|
|
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)
|
|
|
|
}
|
2014-01-29 14:26:54 -05:00
|
|
|
if err := api.ServeRequest(eng, api.APIVERSION, r2, req2); err != nil {
|
2013-05-10 10:49:47 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-18 14:25:13 -05:00
|
|
|
assertHttpNotError(r2, t)
|
2013-11-15 19:05:57 -05:00
|
|
|
|
2014-01-29 15:31:49 -05:00
|
|
|
images2 := engine.NewTable("Id", 0)
|
2014-01-21 20:56:09 -05:00
|
|
|
if _, err := images2.ReadListFrom(r2.Body.Bytes()); err != nil {
|
2013-05-10 10:49:47 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-12-13 13:26:00 -05:00
|
|
|
if images2.Len() != initialImages.Len() {
|
|
|
|
t.Errorf("Expected %d image, %d found", initialImages.Len(), images2.Len())
|
2013-05-10 10:49:47 -04:00
|
|
|
}
|
|
|
|
|
2013-07-01 14:45:45 -04:00
|
|
|
found = false
|
2013-12-13 13:26:00 -05:00
|
|
|
for _, img := range images2.Data {
|
2014-01-29 15:31:49 -05:00
|
|
|
if img.Get("Id") == unitTestImageID {
|
2013-07-01 14:45:45 -04:00
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
2013-11-15 19:05:57 -05:00
|
|
|
t.Errorf("Retrieved image Id differs, expected %s, received %+v", unitTestImageID, 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)
|
|
|
|
}
|
|
|
|
|
2014-01-29 14:26:54 -05:00
|
|
|
if err := api.ServeRequest(eng, api.APIVERSION, r3, req3); err != nil {
|
2013-11-18 14:25:13 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assertHttpNotError(r3, t)
|
2013-05-10 10:49:47 -04:00
|
|
|
|
2014-01-29 15:31:49 -05:00
|
|
|
images3 := engine.NewTable("Id", 0)
|
2014-01-21 20:56:09 -05:00
|
|
|
if _, err := images3.ReadListFrom(r3.Body.Bytes()); err != nil {
|
2013-05-10 10:49:47 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-12-13 13:26:00 -05:00
|
|
|
if images3.Len() != 0 {
|
|
|
|
t.Errorf("Expected 0 image, %d found", images3.Len())
|
2013-05-16 09:45:29 -04:00
|
|
|
}
|
2013-05-09 19:20:06 -04:00
|
|
|
}
|
|
|
|
|
2013-05-09 23:13:52 -04:00
|
|
|
func TestGetImagesHistory(t *testing.T) {
|
2013-11-15 19:05:57 -05:00
|
|
|
eng := NewTestEngine(t)
|
|
|
|
defer mkRuntimeFromEngine(eng, t).Nuke()
|
2013-05-09 20:51:27 -04:00
|
|
|
|
2013-05-10 18:24:07 -04:00
|
|
|
r := httptest.NewRecorder()
|
|
|
|
|
2013-11-15 19:05:57 -05:00
|
|
|
req, err := http.NewRequest("GET", fmt.Sprintf("/images/%s/history", unitTestImageName), nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-01-29 14:26:54 -05:00
|
|
|
if err := api.ServeRequest(eng, api.APIVERSION, r, req); err != nil {
|
2013-05-09 20:51:27 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-18 14:25:13 -05:00
|
|
|
assertHttpNotError(r, t)
|
2013-05-09 20:51:27 -04:00
|
|
|
|
2014-01-14 14:43:58 -05:00
|
|
|
outs := engine.NewTable("Created", 0)
|
2014-01-21 20:56:09 -05:00
|
|
|
if _, err := outs.ReadListFrom(r.Body.Bytes()); err != nil {
|
2013-05-09 20:51:27 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-01-14 14:43:58 -05:00
|
|
|
if len(outs.Data) != 1 {
|
|
|
|
t.Errorf("Expected 1 line, %d found", len(outs.Data))
|
2013-05-09 20:51:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-09 23:13:52 -04:00
|
|
|
func TestGetImagesByName(t *testing.T) {
|
2013-11-18 14:25:13 -05:00
|
|
|
eng := NewTestEngine(t)
|
|
|
|
defer mkRuntimeFromEngine(eng, t).Nuke()
|
2013-05-09 20:51:27 -04:00
|
|
|
|
2013-11-15 19:05:57 -05:00
|
|
|
req, err := http.NewRequest("GET", "/images/"+unitTestImageName+"/json", nil)
|
|
|
|
if err != nil {
|
2013-11-18 14:25:13 -05:00
|
|
|
t.Fatal(err)
|
2013-11-15 19:05:57 -05:00
|
|
|
}
|
2013-05-09 20:51:27 -04:00
|
|
|
|
2013-05-10 18:24:07 -04:00
|
|
|
r := httptest.NewRecorder()
|
2014-01-29 14:26:54 -05:00
|
|
|
if err := api.ServeRequest(eng, api.APIVERSION, r, req); err != nil {
|
2013-11-18 14:25:13 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assertHttpNotError(r, t)
|
2013-05-09 20:51:27 -04:00
|
|
|
|
2014-03-07 20:36:47 -05:00
|
|
|
img := &image.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-11-18 14:25:13 -05:00
|
|
|
eng := NewTestEngine(t)
|
|
|
|
defer mkRuntimeFromEngine(eng, t).Nuke()
|
2013-05-09 23:13:52 -04:00
|
|
|
|
2014-01-16 17:00:18 -05:00
|
|
|
job := eng.Job("containers")
|
|
|
|
job.SetenvBool("all", true)
|
|
|
|
outs, err := job.Stdout.AddTable()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := job.Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
beginLen := len(outs.Data)
|
2013-10-08 15:15:29 -04:00
|
|
|
|
2014-02-11 23:04:39 -05:00
|
|
|
containerID := createTestContainer(eng, &runconfig.Config{
|
2013-11-15 19:05:57 -05:00
|
|
|
Image: unitTestImageID,
|
2013-05-09 23:13:52 -04:00
|
|
|
Cmd: []string{"echo", "test"},
|
2013-11-15 19:05:57 -05:00
|
|
|
}, t)
|
2013-05-09 23:13:52 -04:00
|
|
|
|
2013-11-20 02:37:03 -05:00
|
|
|
if containerID == "" {
|
|
|
|
t.Fatalf("Received empty container ID")
|
|
|
|
}
|
|
|
|
|
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()
|
2014-01-29 14:26:54 -05:00
|
|
|
if err := api.ServeRequest(eng, api.APIVERSION, r, req); err != nil {
|
2013-11-18 14:25:13 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assertHttpNotError(r, t)
|
2014-01-16 17:00:18 -05:00
|
|
|
containers := engine.NewTable("", 0)
|
|
|
|
if _, err := containers.ReadListFrom(r.Body.Bytes()); err != nil {
|
2013-05-09 23:13:52 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-01-16 17:00:18 -05:00
|
|
|
if len(containers.Data) != beginLen+1 {
|
|
|
|
t.Fatalf("Expected %d container, %d found (started with: %d)", beginLen+1, len(containers.Data), beginLen)
|
2013-05-09 23:13:52 -04:00
|
|
|
}
|
2014-01-29 15:31:49 -05:00
|
|
|
if id := containers.Data[0].Get("Id"); id != containerID {
|
2014-01-16 17:00:18 -05:00
|
|
|
t.Fatalf("Container ID mismatch. Expected: %s, received: %s\n", containerID, id)
|
2013-05-09 20:51:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-09 23:13:52 -04:00
|
|
|
func TestGetContainersExport(t *testing.T) {
|
2013-11-18 14:25:13 -05:00
|
|
|
eng := NewTestEngine(t)
|
|
|
|
defer mkRuntimeFromEngine(eng, t).Nuke()
|
2013-05-10 10:49:47 -04:00
|
|
|
|
|
|
|
// Create a container and remove a file
|
2013-11-18 14:25:13 -05:00
|
|
|
containerID := createTestContainer(eng,
|
2014-02-11 23:04:39 -05:00
|
|
|
&runconfig.Config{
|
2013-11-15 19:05:57 -05:00
|
|
|
Image: unitTestImageID,
|
2013-05-10 15:28:07 -04:00
|
|
|
Cmd: []string{"touch", "/test"},
|
2013-05-10 10:49:47 -04:00
|
|
|
},
|
2013-11-15 19:05:57 -05:00
|
|
|
t,
|
2013-05-10 10:49:47 -04:00
|
|
|
)
|
2013-11-15 19:05:57 -05:00
|
|
|
containerRun(eng, containerID, t)
|
2013-05-10 10:49:47 -04:00
|
|
|
|
|
|
|
r := httptest.NewRecorder()
|
2013-11-15 19:05:57 -05:00
|
|
|
|
2013-11-18 14:25:13 -05:00
|
|
|
req, err := http.NewRequest("GET", "/containers/"+containerID+"/export", nil)
|
2013-11-15 19:05:57 -05:00
|
|
|
if err != nil {
|
2013-05-10 10:49:47 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-01-29 14:26:54 -05:00
|
|
|
if err := api.ServeRequest(eng, api.APIVERSION, r, req); err != nil {
|
2013-11-18 14:25:13 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assertHttpNotError(r, t)
|
2013-05-10 10:49:47 -04:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2014-01-21 04:39:50 -05:00
|
|
|
if h.Name == "test" {
|
2013-05-10 15:28:07 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-02-16 20:44:25 -05:00
|
|
|
func TestSaveImageAndThenLoad(t *testing.T) {
|
|
|
|
eng := NewTestEngine(t)
|
|
|
|
defer mkRuntimeFromEngine(eng, t).Nuke()
|
|
|
|
|
|
|
|
// save image
|
|
|
|
r := httptest.NewRecorder()
|
|
|
|
req, err := http.NewRequest("GET", "/images/"+unitTestImageID+"/get", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := api.ServeRequest(eng, api.APIVERSION, r, req); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if r.Code != http.StatusOK {
|
|
|
|
t.Fatalf("%d OK expected, received %d\n", http.StatusOK, r.Code)
|
|
|
|
}
|
|
|
|
tarball := r.Body
|
|
|
|
|
|
|
|
// delete the image
|
|
|
|
r = httptest.NewRecorder()
|
|
|
|
req, err = http.NewRequest("DELETE", "/images/"+unitTestImageID, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := api.ServeRequest(eng, api.APIVERSION, r, req); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if r.Code != http.StatusOK {
|
|
|
|
t.Fatalf("%d OK expected, received %d\n", http.StatusOK, r.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure there is no image
|
|
|
|
r = httptest.NewRecorder()
|
|
|
|
req, err = http.NewRequest("GET", "/images/"+unitTestImageID+"/get", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := api.ServeRequest(eng, api.APIVERSION, r, req); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if r.Code != http.StatusNotFound {
|
|
|
|
t.Fatalf("%d NotFound expected, received %d\n", http.StatusNotFound, r.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
// load the image
|
|
|
|
r = httptest.NewRecorder()
|
|
|
|
req, err = http.NewRequest("POST", "/images/load", tarball)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := api.ServeRequest(eng, api.APIVERSION, r, req); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if r.Code != http.StatusOK {
|
|
|
|
t.Fatalf("%d OK expected, received %d\n", http.StatusOK, r.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
// finally make sure the image is there
|
|
|
|
r = httptest.NewRecorder()
|
|
|
|
req, err = http.NewRequest("GET", "/images/"+unitTestImageID+"/get", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := api.ServeRequest(eng, api.APIVERSION, r, req); 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 00:54:43 -04:00
|
|
|
func TestGetContainersChanges(t *testing.T) {
|
2013-11-18 14:25:13 -05:00
|
|
|
eng := NewTestEngine(t)
|
|
|
|
defer mkRuntimeFromEngine(eng, t).Nuke()
|
2013-05-09 23:13:52 -04:00
|
|
|
|
2013-05-10 00:54:43 -04:00
|
|
|
// Create a container and remove a file
|
2013-11-18 14:25:13 -05:00
|
|
|
containerID := createTestContainer(eng,
|
2014-02-11 23:04:39 -05:00
|
|
|
&runconfig.Config{
|
2013-11-15 19:05:57 -05:00
|
|
|
Image: unitTestImageID,
|
2013-05-10 00:54:43 -04:00
|
|
|
Cmd: []string{"/bin/rm", "/etc/passwd"},
|
|
|
|
},
|
2013-11-15 19:05:57 -05:00
|
|
|
t,
|
2013-05-10 00:54:43 -04:00
|
|
|
)
|
2013-11-18 14:25:13 -05:00
|
|
|
containerRun(eng, containerID, t)
|
2013-05-10 00:54:43 -04:00
|
|
|
|
2013-05-10 18:24:07 -04:00
|
|
|
r := httptest.NewRecorder()
|
2013-11-15 19:05:57 -05:00
|
|
|
req, err := http.NewRequest("GET", "/containers/"+containerID+"/changes", nil)
|
|
|
|
if err != nil {
|
2013-11-18 14:25:13 -05:00
|
|
|
t.Fatal(err)
|
2013-05-10 00:54:43 -04:00
|
|
|
}
|
2014-01-29 14:26:54 -05:00
|
|
|
if err := api.ServeRequest(eng, api.APIVERSION, r, req); err != nil {
|
2013-11-18 14:25:13 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assertHttpNotError(r, t)
|
2014-01-14 19:51:59 -05:00
|
|
|
outs := engine.NewTable("", 0)
|
2014-01-21 20:56:09 -05:00
|
|
|
if _, err := outs.ReadListFrom(r.Body.Bytes()); err != nil {
|
2013-05-10 00:54:43 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the changelog
|
|
|
|
success := false
|
2014-01-14 19:51:59 -05:00
|
|
|
for _, elem := range outs.Data {
|
|
|
|
if elem.Get("Path") == "/etc/passwd" && elem.GetInt("Kind") == 2 {
|
2013-05-10 00:54:43 -04:00
|
|
|
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-11-18 14:25:13 -05:00
|
|
|
eng := NewTestEngine(t)
|
|
|
|
defer mkRuntimeFromEngine(eng, t).Nuke()
|
2013-06-28 12:27:00 -04:00
|
|
|
|
2013-11-18 14:25:13 -05:00
|
|
|
containerID := createTestContainer(eng,
|
2014-02-11 23:04:39 -05:00
|
|
|
&runconfig.Config{
|
2013-11-15 19:05:57 -05:00
|
|
|
Image: unitTestImageID,
|
2013-08-07 19:35:38 -04:00
|
|
|
Cmd: []string{"/bin/sh", "-c", "cat"},
|
|
|
|
OpenStdin: true,
|
2013-06-28 12:27:00 -04:00
|
|
|
},
|
2013-11-15 19:05:57 -05:00
|
|
|
t,
|
2013-06-28 12:27:00 -04:00
|
|
|
)
|
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-11-15 19:05:57 -05:00
|
|
|
containerKill(eng, containerID, t)
|
|
|
|
containerWait(eng, containerID, t)
|
2013-08-07 19:35:38 -04:00
|
|
|
}()
|
|
|
|
|
2013-11-15 19:05:57 -05:00
|
|
|
startContainer(eng, containerID, t)
|
2013-06-28 12:27:00 -04:00
|
|
|
|
2013-08-07 19:35:38 -04:00
|
|
|
setTimeout(t, "Waiting for the container to be started timed out", 10*time.Second, func() {
|
|
|
|
for {
|
2013-11-15 19:05:57 -05:00
|
|
|
if containerRunning(eng, containerID, t) {
|
2013-08-07 19:35:38 -04:00
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
}
|
|
|
|
})
|
2013-06-28 12:27:00 -04:00
|
|
|
|
2013-11-15 19:05:57 -05:00
|
|
|
if !containerRunning(eng, containerID, t) {
|
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() {
|
2013-11-15 19:05:57 -05:00
|
|
|
in, out := containerAttach(eng, containerID, t)
|
2013-11-28 19:12:45 -05:00
|
|
|
if err := assertPipe("hello\n", "hello", out, in, 150); err != nil {
|
2013-08-07 19:35:38 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2013-06-28 12:27:00 -04:00
|
|
|
r := httptest.NewRecorder()
|
2013-12-16 07:29:06 -05:00
|
|
|
req, err := http.NewRequest("GET", "/containers/"+containerID+"/top?ps_args=aux", nil)
|
2013-07-19 06:06:32 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-01-29 14:26:54 -05:00
|
|
|
if err := api.ServeRequest(eng, api.APIVERSION, r, req); err != nil {
|
2013-11-18 14:25:13 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assertHttpNotError(r, t)
|
2014-01-25 02:15:40 -05:00
|
|
|
var procs engine.Env
|
|
|
|
if err := procs.Decode(r.Body); err != nil {
|
2013-06-28 12:27:00 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-01-25 02:15:40 -05:00
|
|
|
if len(procs.GetList("Titles")) != 11 {
|
|
|
|
t.Fatalf("Expected 11 titles, found %d.", len(procs.GetList("Titles")))
|
2013-06-28 12:27:00 -04:00
|
|
|
}
|
2014-01-25 02:15:40 -05:00
|
|
|
if procs.GetList("Titles")[0] != "USER" || procs.GetList("Titles")[10] != "COMMAND" {
|
|
|
|
t.Fatalf("Expected Titles[0] to be USER and Titles[10] to be COMMAND, found %s and %s.", procs.GetList("Titles")[0], procs.GetList("Titles")[10])
|
2013-06-28 12:27:00 -04:00
|
|
|
}
|
2014-01-25 02:15:40 -05:00
|
|
|
processes := [][]string{}
|
|
|
|
if err := procs.GetJson("Processes", &processes); err != nil {
|
|
|
|
t.Fatal(err)
|
2013-07-19 06:06:32 -04:00
|
|
|
}
|
2014-01-25 02:15:40 -05:00
|
|
|
if len(processes) != 2 {
|
|
|
|
t.Fatalf("Expected 2 processes, found %d.", len(processes))
|
2013-07-19 06:06:32 -04:00
|
|
|
}
|
2014-01-25 02:15:40 -05:00
|
|
|
if processes[0][10] != "/bin/sh -c cat" {
|
|
|
|
t.Fatalf("Expected `/bin/sh -c cat`, found %s.", processes[0][10])
|
|
|
|
}
|
|
|
|
if processes[1][10] != "/bin/sh -c cat" {
|
|
|
|
t.Fatalf("Expected `/bin/sh -c cat`, found %s.", 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-11-18 14:25:13 -05:00
|
|
|
eng := NewTestEngine(t)
|
|
|
|
defer mkRuntimeFromEngine(eng, t).Nuke()
|
2013-05-10 10:49:47 -04:00
|
|
|
|
|
|
|
// Create a container and remove a file
|
2013-11-18 14:25:13 -05:00
|
|
|
containerID := createTestContainer(eng,
|
2014-02-11 23:04:39 -05:00
|
|
|
&runconfig.Config{
|
2013-11-15 19:05:57 -05:00
|
|
|
Image: unitTestImageID,
|
2013-05-10 15:28:07 -04:00
|
|
|
Cmd: []string{"echo", "test"},
|
2013-05-10 10:49:47 -04:00
|
|
|
},
|
2013-11-15 19:05:57 -05:00
|
|
|
t,
|
2013-05-10 10:49:47 -04:00
|
|
|
)
|
|
|
|
|
2013-05-10 18:24:07 -04:00
|
|
|
r := httptest.NewRecorder()
|
2013-11-15 19:05:57 -05:00
|
|
|
req, err := http.NewRequest("GET", "/containers/"+containerID+"/json", nil)
|
|
|
|
if err != nil {
|
2013-11-18 14:25:13 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-01-29 14:26:54 -05:00
|
|
|
if err := api.ServeRequest(eng, api.APIVERSION, r, req); err != nil {
|
2013-11-18 14:25:13 -05:00
|
|
|
t.Fatal(err)
|
2013-05-10 10:49:47 -04:00
|
|
|
}
|
2013-11-18 14:25:13 -05:00
|
|
|
assertHttpNotError(r, t)
|
2014-03-07 21:42:29 -05:00
|
|
|
outContainer := &runtime.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-11-15 19:05:57 -05:00
|
|
|
if outContainer.ID != containerID {
|
|
|
|
t.Fatalf("Wrong containers retrieved. Expected %s, received %s", containerID, outContainer.ID)
|
2013-05-10 15:28:07 -04:00
|
|
|
}
|
2013-05-09 23:13:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostCommit(t *testing.T) {
|
2013-11-18 14:25:13 -05:00
|
|
|
eng := NewTestEngine(t)
|
|
|
|
defer mkRuntimeFromEngine(eng, t).Nuke()
|
|
|
|
srv := mkServerFromEngine(eng, t)
|
2013-05-10 10:49:47 -04:00
|
|
|
|
|
|
|
// Create a container and remove a file
|
2013-11-18 14:25:13 -05:00
|
|
|
containerID := createTestContainer(eng,
|
2014-02-11 23:04:39 -05:00
|
|
|
&runconfig.Config{
|
2013-11-15 19:05:57 -05:00
|
|
|
Image: unitTestImageID,
|
2013-05-10 15:28:07 -04:00
|
|
|
Cmd: []string{"touch", "/test"},
|
2013-05-10 10:49:47 -04:00
|
|
|
},
|
2013-11-15 19:05:57 -05:00
|
|
|
t,
|
2013-05-10 10:49:47 -04:00
|
|
|
)
|
|
|
|
|
2013-11-18 14:25:13 -05:00
|
|
|
containerRun(eng, containerID, t)
|
2013-05-10 10:49:47 -04:00
|
|
|
|
2013-11-15 19:05:57 -05:00
|
|
|
req, err := http.NewRequest("POST", "/commit?repo=testrepo&testtag=tag&container="+containerID, 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()
|
2014-01-29 14:26:54 -05:00
|
|
|
if err := api.ServeRequest(eng, api.APIVERSION, r, req); err != nil {
|
2013-11-18 14:25:13 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assertHttpNotError(r, t)
|
2013-05-10 10:49:47 -04:00
|
|
|
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
|
|
|
|
2014-01-25 02:15:40 -05:00
|
|
|
var env engine.Env
|
|
|
|
if err := env.Decode(r.Body); err != nil {
|
2013-05-10 15:28:07 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-01-25 02:15:40 -05:00
|
|
|
if _, err := srv.ImageInspect(env.Get("Id")); err != nil {
|
2013-11-15 19:05:57 -05:00
|
|
|
t.Fatalf("The image has not been committed")
|
2013-05-10 15:28:07 -04:00
|
|
|
}
|
2013-05-09 23:13:52 -04:00
|
|
|
}
|
|
|
|
|
2013-05-09 22:19:24 -04:00
|
|
|
func TestPostContainersCreate(t *testing.T) {
|
2013-10-27 22:20:00 -04:00
|
|
|
eng := NewTestEngine(t)
|
2013-11-15 19:05:57 -05:00
|
|
|
defer mkRuntimeFromEngine(eng, t).Nuke()
|
2013-05-09 18:47:06 -04:00
|
|
|
|
2014-02-11 23:04:39 -05:00
|
|
|
configJSON, err := json.Marshal(&runconfig.Config{
|
2013-11-15 19:05:57 -05:00
|
|
|
Image: unitTestImageID,
|
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()
|
2014-01-29 14:26:54 -05:00
|
|
|
if err := api.ServeRequest(eng, api.APIVERSION, r, req); err != nil {
|
2013-11-18 14:25:13 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assertHttpNotError(r, t)
|
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
|
|
|
|
2014-01-25 02:15:40 -05:00
|
|
|
var apiRun engine.Env
|
|
|
|
if err := apiRun.Decode(r.Body); err != nil {
|
2013-05-09 19:20:07 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-01-25 02:15:40 -05:00
|
|
|
containerID := apiRun.Get("Id")
|
2013-05-09 18:47:06 -04:00
|
|
|
|
2013-11-15 19:05:57 -05:00
|
|
|
containerAssertExists(eng, containerID, t)
|
2013-11-18 14:25:13 -05:00
|
|
|
containerRun(eng, containerID, t)
|
2013-05-09 22:19:24 -04:00
|
|
|
|
2013-11-15 19:05:57 -05:00
|
|
|
if !containerFileExists(eng, containerID, "test", t) {
|
|
|
|
t.Fatal("Test file was not created")
|
2013-05-09 19:20:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-09 23:13:52 -04:00
|
|
|
func TestPostContainersKill(t *testing.T) {
|
2013-11-18 14:25:13 -05:00
|
|
|
eng := NewTestEngine(t)
|
|
|
|
defer mkRuntimeFromEngine(eng, t).Nuke()
|
2013-05-09 19:20:07 -04:00
|
|
|
|
2013-11-18 14:25:13 -05:00
|
|
|
containerID := createTestContainer(eng,
|
2014-02-11 23:04:39 -05:00
|
|
|
&runconfig.Config{
|
2013-11-15 19:05:57 -05:00
|
|
|
Image: unitTestImageID,
|
2013-05-09 23:13:52 -04:00
|
|
|
Cmd: []string{"/bin/cat"},
|
|
|
|
OpenStdin: true,
|
|
|
|
},
|
2013-11-15 19:05:57 -05:00
|
|
|
t,
|
2013-05-09 23:13:52 -04:00
|
|
|
)
|
2013-05-09 19:20:07 -04:00
|
|
|
|
2013-11-18 14:25:13 -05:00
|
|
|
startContainer(eng, containerID, t)
|
2013-05-09 19:20:07 -04:00
|
|
|
|
2013-05-09 23:13:52 -04:00
|
|
|
// Give some time to the process to start
|
2013-11-15 19:05:57 -05:00
|
|
|
containerWaitTimeout(eng, containerID, t)
|
2013-05-09 23:13:52 -04:00
|
|
|
|
2013-11-15 19:05:57 -05:00
|
|
|
if !containerRunning(eng, containerID, t) {
|
2013-05-09 23:13:52 -04:00
|
|
|
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-11-15 19:05:57 -05:00
|
|
|
req, err := http.NewRequest("POST", "/containers/"+containerID+"/kill", bytes.NewReader([]byte{}))
|
|
|
|
if err != nil {
|
2013-11-18 14:25:13 -05:00
|
|
|
t.Fatal(err)
|
2013-05-09 19:20:07 -04:00
|
|
|
}
|
2014-01-29 14:26:54 -05:00
|
|
|
if err := api.ServeRequest(eng, api.APIVERSION, r, req); err != nil {
|
2013-11-18 14:25:13 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assertHttpNotError(r, t)
|
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)
|
|
|
|
}
|
2013-11-15 19:05:57 -05:00
|
|
|
if containerRunning(eng, containerID, t) {
|
2013-05-09 23:13:52 -04:00
|
|
|
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-11-18 14:25:13 -05:00
|
|
|
eng := NewTestEngine(t)
|
|
|
|
defer mkRuntimeFromEngine(eng, t).Nuke()
|
2013-05-09 19:20:07 -04:00
|
|
|
|
2013-11-18 14:25:13 -05:00
|
|
|
containerID := createTestContainer(eng,
|
2014-02-11 23:04:39 -05:00
|
|
|
&runconfig.Config{
|
2013-11-15 19:05:57 -05:00
|
|
|
Image: unitTestImageID,
|
2013-10-24 18:06:10 -04:00
|
|
|
Cmd: []string{"/bin/top"},
|
2013-05-09 22:19:24 -04:00
|
|
|
OpenStdin: true,
|
|
|
|
},
|
2013-11-15 19:05:57 -05:00
|
|
|
t,
|
2013-05-09 22:19:24 -04:00
|
|
|
)
|
|
|
|
|
2013-11-18 14:25:13 -05:00
|
|
|
startContainer(eng, containerID, t)
|
2013-05-09 23:13:52 -04:00
|
|
|
|
|
|
|
// Give some time to the process to start
|
2013-11-15 19:05:57 -05:00
|
|
|
containerWaitTimeout(eng, containerID, t)
|
2013-05-09 23:13:52 -04:00
|
|
|
|
2013-11-15 19:05:57 -05:00
|
|
|
if !containerRunning(eng, containerID, t) {
|
2013-05-09 23:13:52 -04:00
|
|
|
t.Errorf("Container should be running")
|
|
|
|
}
|
|
|
|
|
2013-11-15 19:05:57 -05:00
|
|
|
req, err := http.NewRequest("POST", "/containers/"+containerID+"/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()
|
2014-01-29 14:26:54 -05:00
|
|
|
if err := api.ServeRequest(eng, api.APIVERSION, r, req); err != nil {
|
2013-11-18 14:25:13 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assertHttpNotError(r, t)
|
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-05-09 23:13:52 -04:00
|
|
|
// Give some time to the process to restart
|
2013-11-15 19:05:57 -05:00
|
|
|
containerWaitTimeout(eng, containerID, t)
|
2013-05-09 22:19:24 -04:00
|
|
|
|
2013-11-15 19:05:57 -05:00
|
|
|
if !containerRunning(eng, containerID, t) {
|
2013-05-09 23:13:52 -04:00
|
|
|
t.Fatalf("Container should be running")
|
2013-05-09 22:19:24 -04:00
|
|
|
}
|
|
|
|
|
2013-11-18 14:25:13 -05:00
|
|
|
containerKill(eng, containerID, t)
|
2013-05-09 19:20:07 -04:00
|
|
|
}
|
|
|
|
|
2013-05-09 23:13:52 -04:00
|
|
|
func TestPostContainersStart(t *testing.T) {
|
2013-10-26 22:24:01 -04:00
|
|
|
eng := NewTestEngine(t)
|
2013-11-15 19:05:57 -05:00
|
|
|
defer mkRuntimeFromEngine(eng, t).Nuke()
|
2013-05-09 19:20:07 -04:00
|
|
|
|
2013-11-15 19:05:57 -05:00
|
|
|
containerID := createTestContainer(
|
2013-10-27 22:20:00 -04:00
|
|
|
eng,
|
2014-02-11 23:04:39 -05:00
|
|
|
&runconfig.Config{
|
2013-11-15 19:05:57 -05:00
|
|
|
Image: unitTestImageID,
|
2013-05-09 23:13:52 -04:00
|
|
|
Cmd: []string{"/bin/cat"},
|
|
|
|
OpenStdin: true,
|
|
|
|
},
|
2013-11-15 19:05:57 -05:00
|
|
|
t,
|
|
|
|
)
|
2013-05-09 19:20:07 -04:00
|
|
|
|
2014-02-11 23:04:39 -05:00
|
|
|
hostConfigJSON, err := json.Marshal(&runconfig.HostConfig{})
|
2013-05-13 19:39:54 -04:00
|
|
|
|
2013-11-15 19:05:57 -05:00
|
|
|
req, err := http.NewRequest("POST", "/containers/"+containerID+"/start", bytes.NewReader(hostConfigJSON))
|
2013-05-13 19:39:54 -04:00
|
|
|
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()
|
2014-01-29 14:26:54 -05:00
|
|
|
if err := api.ServeRequest(eng, api.APIVERSION, r, req); err != nil {
|
2013-11-18 14:25:13 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assertHttpNotError(r, t)
|
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 23:13:52 -04:00
|
|
|
|
2013-11-15 19:05:57 -05:00
|
|
|
containerAssertExists(eng, containerID, t)
|
2013-05-09 23:13:52 -04:00
|
|
|
// Give some time to the process to start
|
2013-10-27 22:20:00 -04:00
|
|
|
// FIXME: use Wait once it's available as a job
|
2013-11-15 19:05:57 -05:00
|
|
|
containerWaitTimeout(eng, containerID, t)
|
|
|
|
if !containerRunning(eng, containerID, t) {
|
2013-05-09 23:13:52 -04:00
|
|
|
t.Errorf("Container should be running")
|
|
|
|
}
|
|
|
|
|
2013-05-10 18:24:07 -04:00
|
|
|
r = httptest.NewRecorder()
|
2014-01-29 14:26:54 -05:00
|
|
|
if err := api.ServeRequest(eng, api.APIVERSION, r, req); err != nil {
|
2013-05-09 23:13:52 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-15 19:05:57 -05:00
|
|
|
// Starting an already started container should return an error
|
|
|
|
// FIXME: verify a precise error code. There is a possible bug here
|
|
|
|
// which causes this to return 404 even though the container exists.
|
|
|
|
assertHttpError(r, t)
|
|
|
|
containerAssertExists(eng, containerID, t)
|
2013-11-18 14:25:13 -05:00
|
|
|
containerKill(eng, containerID, t)
|
2013-05-09 19:20:07 -04:00
|
|
|
}
|
|
|
|
|
2013-11-29 16:43:37 -05:00
|
|
|
// Expected behaviour: using / as a bind mount source should throw an error
|
|
|
|
func TestRunErrorBindMountRootSource(t *testing.T) {
|
|
|
|
eng := NewTestEngine(t)
|
|
|
|
defer mkRuntimeFromEngine(eng, t).Nuke()
|
|
|
|
|
|
|
|
containerID := createTestContainer(
|
|
|
|
eng,
|
2014-02-11 23:04:39 -05:00
|
|
|
&runconfig.Config{
|
2013-11-29 16:43:37 -05:00
|
|
|
Image: unitTestImageID,
|
|
|
|
Cmd: []string{"/bin/cat"},
|
|
|
|
OpenStdin: true,
|
|
|
|
},
|
|
|
|
t,
|
|
|
|
)
|
|
|
|
|
2014-02-11 23:04:39 -05:00
|
|
|
hostConfigJSON, err := json.Marshal(&runconfig.HostConfig{
|
2013-11-29 16:43:37 -05:00
|
|
|
Binds: []string{"/:/tmp"},
|
|
|
|
})
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", "/containers/"+containerID+"/start", bytes.NewReader(hostConfigJSON))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
r := httptest.NewRecorder()
|
2014-01-29 14:26:54 -05:00
|
|
|
if err := api.ServeRequest(eng, api.APIVERSION, r, req); err != nil {
|
2013-11-29 16:43:37 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if r.Code != http.StatusInternalServerError {
|
|
|
|
containerKill(eng, containerID, t)
|
|
|
|
t.Fatal("should have failed to run when using / as a source for the bind mount")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-09 22:19:24 -04:00
|
|
|
func TestPostContainersStop(t *testing.T) {
|
2013-11-18 14:25:13 -05:00
|
|
|
eng := NewTestEngine(t)
|
|
|
|
defer mkRuntimeFromEngine(eng, t).Nuke()
|
2013-05-09 19:20:07 -04:00
|
|
|
|
2013-11-18 14:25:13 -05:00
|
|
|
containerID := createTestContainer(eng,
|
2014-02-11 23:04:39 -05:00
|
|
|
&runconfig.Config{
|
2013-11-15 19:05:57 -05:00
|
|
|
Image: unitTestImageID,
|
2013-10-24 18:06:10 -04:00
|
|
|
Cmd: []string{"/bin/top"},
|
2013-05-09 22:19:24 -04:00
|
|
|
OpenStdin: true,
|
|
|
|
},
|
2013-11-15 19:05:57 -05:00
|
|
|
t,
|
2013-05-09 22:19:24 -04:00
|
|
|
)
|
2013-05-09 19:20:07 -04:00
|
|
|
|
2013-11-18 14:25:13 -05:00
|
|
|
startContainer(eng, containerID, t)
|
2013-05-09 19:20:07 -04:00
|
|
|
|
2013-05-09 22:19:24 -04:00
|
|
|
// Give some time to the process to start
|
2013-11-15 19:05:57 -05:00
|
|
|
containerWaitTimeout(eng, containerID, t)
|
2013-05-09 22:19:24 -04:00
|
|
|
|
2013-11-15 19:05:57 -05:00
|
|
|
if !containerRunning(eng, containerID, t) {
|
2013-05-09 22:19:24 -04:00
|
|
|
t.Errorf("Container should be running")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: as it is a POST request, it requires a body.
|
2013-11-15 19:05:57 -05:00
|
|
|
req, err := http.NewRequest("POST", "/containers/"+containerID+"/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()
|
2014-01-29 14:26:54 -05:00
|
|
|
if err := api.ServeRequest(eng, api.APIVERSION, r, req); err != nil {
|
2013-11-18 14:25:13 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assertHttpNotError(r, t)
|
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-11-15 19:05:57 -05:00
|
|
|
if containerRunning(eng, containerID, t) {
|
2013-05-09 22:19:24 -04:00
|
|
|
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-11-18 14:25:13 -05:00
|
|
|
eng := NewTestEngine(t)
|
|
|
|
defer mkRuntimeFromEngine(eng, t).Nuke()
|
2013-05-09 19:20:07 -04:00
|
|
|
|
2013-11-18 14:25:13 -05:00
|
|
|
containerID := createTestContainer(eng,
|
2014-02-11 23:04:39 -05:00
|
|
|
&runconfig.Config{
|
2013-11-15 19:05:57 -05:00
|
|
|
Image: unitTestImageID,
|
2013-05-09 23:13:52 -04:00
|
|
|
Cmd: []string{"/bin/sleep", "1"},
|
|
|
|
OpenStdin: true,
|
|
|
|
},
|
2013-11-15 19:05:57 -05:00
|
|
|
t,
|
2013-05-09 23:13:52 -04:00
|
|
|
)
|
2013-11-18 14:25:13 -05:00
|
|
|
startContainer(eng, containerID, t)
|
2013-05-09 19:20:07 -04:00
|
|
|
|
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-11-15 19:05:57 -05:00
|
|
|
req, err := http.NewRequest("POST", "/containers/"+containerID+"/wait", bytes.NewReader([]byte{}))
|
|
|
|
if err != nil {
|
2013-11-18 14:25:13 -05:00
|
|
|
t.Fatal(err)
|
2013-05-09 23:13:52 -04:00
|
|
|
}
|
2014-01-29 14:26:54 -05:00
|
|
|
if err := api.ServeRequest(eng, api.APIVERSION, r, req); err != nil {
|
2013-11-18 14:25:13 -05:00
|
|
|
t.Fatal(err)
|
2013-11-15 19:05:57 -05:00
|
|
|
}
|
2013-11-18 14:25:13 -05:00
|
|
|
assertHttpNotError(r, t)
|
2014-01-25 02:15:40 -05:00
|
|
|
var apiWait engine.Env
|
|
|
|
if err := apiWait.Decode(r.Body); err != nil {
|
2013-05-09 23:13:52 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-01-25 02:15:40 -05:00
|
|
|
if apiWait.GetInt("StatusCode") != 0 {
|
|
|
|
t.Fatalf("Non zero exit code for sleep: %d\n", apiWait.GetInt("StatusCode"))
|
2013-05-09 23:13:52 -04:00
|
|
|
}
|
|
|
|
})
|
2013-05-09 19:20:07 -04:00
|
|
|
|
2013-11-15 19:05:57 -05:00
|
|
|
if containerRunning(eng, containerID, t) {
|
2013-05-09 23:13:52 -04:00
|
|
|
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-11-18 14:25:13 -05:00
|
|
|
eng := NewTestEngine(t)
|
|
|
|
defer mkRuntimeFromEngine(eng, t).Nuke()
|
2013-05-10 00:55:08 -04:00
|
|
|
|
2013-11-18 14:25:13 -05:00
|
|
|
containerID := createTestContainer(eng,
|
2014-02-11 23:04:39 -05:00
|
|
|
&runconfig.Config{
|
2013-11-15 19:05:57 -05:00
|
|
|
Image: unitTestImageID,
|
2013-05-10 00:55:08 -04:00
|
|
|
Cmd: []string{"/bin/cat"},
|
|
|
|
OpenStdin: true,
|
|
|
|
},
|
2013-11-15 19:05:57 -05:00
|
|
|
t,
|
2013-05-10 00:55:08 -04:00
|
|
|
)
|
|
|
|
// Start the process
|
2013-11-18 14:25:13 -05:00
|
|
|
startContainer(eng, containerID, t)
|
2013-05-10 00:55:08 -04:00
|
|
|
|
|
|
|
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)
|
2013-11-15 19:05:57 -05:00
|
|
|
containerKill(eng, containerID, t)
|
2013-07-24 18:48:18 -04:00
|
|
|
}()
|
|
|
|
|
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-11-15 19:05:57 -05:00
|
|
|
req, err := http.NewRequest("POST", "/containers/"+containerID+"/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)
|
|
|
|
}
|
|
|
|
|
2014-01-29 14:26:54 -05:00
|
|
|
if err := api.ServeRequest(eng, api.APIVERSION, r, req); err != nil {
|
2013-11-18 14:25:13 -05:00
|
|
|
t.Fatal(err)
|
2013-05-10 00:55:08 -04:00
|
|
|
}
|
2013-11-18 14:25:13 -05:00
|
|
|
assertHttpNotError(r.ResponseRecorder, t)
|
2013-05-10 00:55:08 -04:00
|
|
|
}()
|
|
|
|
|
|
|
|
// 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-11-28 19:12:45 -05:00
|
|
|
if err := assertPipe("hello\n", string([]byte{1, 0, 0, 0, 0, 0, 0, 6})+"hello", stdout, stdinPipe, 150); 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
|
2013-11-15 19:05:57 -05:00
|
|
|
containerWaitTimeout(eng, containerID, t)
|
2013-09-09 21:23:03 -04:00
|
|
|
|
|
|
|
// Try to avoid the timeout in destroy. Best effort, don't check error
|
2013-11-15 19:05:57 -05:00
|
|
|
cStdin, _ := containerAttach(eng, containerID, t)
|
2013-09-09 21:23:03 -04:00
|
|
|
cStdin.Close()
|
2013-11-15 19:05:57 -05:00
|
|
|
containerWait(eng, containerID, t)
|
2013-09-09 21:23:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostContainersAttachStderr(t *testing.T) {
|
2013-11-18 14:25:13 -05:00
|
|
|
eng := NewTestEngine(t)
|
|
|
|
defer mkRuntimeFromEngine(eng, t).Nuke()
|
2013-09-09 21:23:03 -04:00
|
|
|
|
2013-11-18 14:25:13 -05:00
|
|
|
containerID := createTestContainer(eng,
|
2014-02-11 23:04:39 -05:00
|
|
|
&runconfig.Config{
|
2013-11-15 19:05:57 -05:00
|
|
|
Image: unitTestImageID,
|
2013-09-09 21:23:03 -04:00
|
|
|
Cmd: []string{"/bin/sh", "-c", "/bin/cat >&2"},
|
|
|
|
OpenStdin: true,
|
|
|
|
},
|
2013-11-15 19:05:57 -05:00
|
|
|
t,
|
2013-09-09 21:23:03 -04:00
|
|
|
)
|
|
|
|
// Start the process
|
2013-11-18 14:25:13 -05:00
|
|
|
startContainer(eng, containerID, t)
|
2013-09-09 21:23:03 -04:00
|
|
|
|
|
|
|
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)
|
2013-11-15 19:05:57 -05:00
|
|
|
containerKill(eng, containerID, t)
|
2013-09-09 21:23:03 -04:00
|
|
|
}()
|
|
|
|
|
|
|
|
// Attach to it
|
|
|
|
c1 := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
defer close(c1)
|
|
|
|
|
|
|
|
r := &hijackTester{
|
|
|
|
ResponseRecorder: httptest.NewRecorder(),
|
|
|
|
in: stdin,
|
|
|
|
out: stdoutPipe,
|
|
|
|
}
|
|
|
|
|
2013-11-15 19:05:57 -05:00
|
|
|
req, err := http.NewRequest("POST", "/containers/"+containerID+"/attach?stream=1&stdin=1&stdout=1&stderr=1", bytes.NewReader([]byte{}))
|
2013-09-09 21:23:03 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-01-29 14:26:54 -05:00
|
|
|
if err := api.ServeRequest(eng, api.APIVERSION, r, req); err != nil {
|
2013-09-09 21:23:03 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-18 14:25:13 -05:00
|
|
|
assertHttpNotError(r.ResponseRecorder, t)
|
2013-09-09 21:23:03 -04:00
|
|
|
}()
|
|
|
|
|
|
|
|
// 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-11-28 19:12:45 -05:00
|
|
|
if err := assertPipe("hello\n", string([]byte{2, 0, 0, 0, 0, 0, 0, 6})+"hello", stdout, stdinPipe, 150); 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
|
2013-11-15 19:05:57 -05:00
|
|
|
containerWaitTimeout(eng, containerID, t)
|
2013-05-10 00:55:08 -04:00
|
|
|
|
2013-08-12 13:53:06 -04:00
|
|
|
// Try to avoid the timeout in destroy. Best effort, don't check error
|
2013-11-15 19:05:57 -05:00
|
|
|
cStdin, _ := containerAttach(eng, containerID, t)
|
2013-05-10 00:55:08 -04:00
|
|
|
cStdin.Close()
|
2013-11-15 19:05:57 -05:00
|
|
|
containerWait(eng, containerID, t)
|
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-11-18 14:25:13 -05:00
|
|
|
eng := NewTestEngine(t)
|
|
|
|
defer mkRuntimeFromEngine(eng, t).Nuke()
|
2013-05-09 22:19:24 -04:00
|
|
|
|
2013-11-18 14:25:13 -05:00
|
|
|
containerID := createTestContainer(eng,
|
2014-02-11 23:04:39 -05:00
|
|
|
&runconfig.Config{
|
2013-11-15 19:05:57 -05:00
|
|
|
Image: unitTestImageID,
|
|
|
|
Cmd: []string{"touch", "/test"},
|
|
|
|
},
|
|
|
|
t,
|
|
|
|
)
|
|
|
|
req, err := http.NewRequest("DELETE", "/containers/"+containerID, 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()
|
2014-01-29 14:26:54 -05:00
|
|
|
if err := api.ServeRequest(eng, api.APIVERSION, r, req); err != nil {
|
2013-11-18 14:25:13 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assertHttpNotError(r, t)
|
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-11-15 19:05:57 -05:00
|
|
|
containerAssertNotExists(eng, containerID, t)
|
2013-05-09 19:20:07 -04:00
|
|
|
}
|
|
|
|
|
2013-06-10 18:02:40 -04:00
|
|
|
func TestOptionsRoute(t *testing.T) {
|
2013-11-15 19:05:57 -05:00
|
|
|
eng := NewTestEngine(t)
|
|
|
|
defer mkRuntimeFromEngine(eng, t).Nuke()
|
2014-01-29 14:26:54 -05:00
|
|
|
|
2013-06-10 18:02:40 -04:00
|
|
|
r := httptest.NewRecorder()
|
2013-11-15 19:05:57 -05:00
|
|
|
req, err := http.NewRequest("OPTIONS", "/", nil)
|
2013-06-10 18:02:40 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-01-29 14:26:54 -05:00
|
|
|
if err := api.ServeRequest(eng, api.APIVERSION, r, req); err != nil {
|
2013-06-10 18:02:40 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-18 14:25:13 -05:00
|
|
|
assertHttpNotError(r, t)
|
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-11-15 19:05:57 -05:00
|
|
|
eng := NewTestEngine(t)
|
|
|
|
defer mkRuntimeFromEngine(eng, t).Nuke()
|
2014-01-29 14:26:54 -05:00
|
|
|
|
2013-06-03 21:39:00 -04:00
|
|
|
r := httptest.NewRecorder()
|
|
|
|
|
2013-11-15 19:05:57 -05:00
|
|
|
req, err := http.NewRequest("GET", "/version", nil)
|
2013-06-10 18:02:40 -04:00
|
|
|
if err != nil {
|
2013-06-03 21:39:00 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-01-29 14:26:54 -05:00
|
|
|
if err := api.ServeRequest(eng, api.APIVERSION, r, req); err != nil {
|
2013-06-10 18:02:40 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-18 14:25:13 -05:00
|
|
|
assertHttpNotError(r, t)
|
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-11-18 14:25:13 -05:00
|
|
|
eng := NewTestEngine(t)
|
2014-02-10 21:44:17 -05:00
|
|
|
//we expect errors, so we disable stderr
|
|
|
|
eng.Stderr = ioutil.Discard
|
2013-11-18 14:25:13 -05:00
|
|
|
defer mkRuntimeFromEngine(eng, t).Nuke()
|
2013-05-15 09:11:39 -04:00
|
|
|
|
2013-12-12 17:39:35 -05:00
|
|
|
initialImages := getImages(eng, t, true, "")
|
2013-07-01 14:45:45 -04:00
|
|
|
|
2013-12-11 20:52:41 -05:00
|
|
|
if err := eng.Job("tag", unitTestImageName, "test", "test").Run(); err != nil {
|
2013-05-15 09:11:39 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-12-12 17:39:35 -05:00
|
|
|
images := getImages(eng, t, true, "")
|
|
|
|
|
2013-12-13 13:26:00 -05:00
|
|
|
if len(images.Data[0].GetList("RepoTags")) != len(initialImages.Data[0].GetList("RepoTags"))+1 {
|
|
|
|
t.Errorf("Expected %d images, %d found", len(initialImages.Data[0].GetList("RepoTags"))+1, len(images.Data[0].GetList("RepoTags")))
|
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()
|
2014-01-29 14:26:54 -05:00
|
|
|
if err := api.ServeRequest(eng, api.APIVERSION, r, req); err != nil {
|
2013-11-18 14:25:13 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-15 19:05:57 -05:00
|
|
|
if r.Code != http.StatusConflict {
|
|
|
|
t.Fatalf("Expected http status 409-conflict, got %v", r.Code)
|
2013-07-05 12:58:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
2014-01-29 14:26:54 -05:00
|
|
|
if err := api.ServeRequest(eng, api.APIVERSION, r2, req2); err != nil {
|
2013-11-18 14:25:13 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assertHttpNotError(r2, t)
|
2013-07-05 12:58:39 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-01-25 02:15:40 -05:00
|
|
|
outs := engine.NewTable("Created", 0)
|
|
|
|
if _, err := outs.ReadListFrom(r2.Body.Bytes()); err != nil {
|
2013-05-31 10:37:02 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-01-25 02:15:40 -05:00
|
|
|
if len(outs.Data) != 1 {
|
|
|
|
t.Fatalf("Expected %d event (untagged), got %d", 1, len(outs.Data))
|
2013-05-31 10:37:02 -04:00
|
|
|
}
|
2013-12-12 17:39:35 -05:00
|
|
|
images = getImages(eng, t, false, "")
|
2013-05-15 09:11:39 -04:00
|
|
|
|
2013-12-12 17:39:35 -05:00
|
|
|
if images.Len() != initialImages.Len() {
|
|
|
|
t.Errorf("Expected %d image, %d found", initialImages.Len(), images.Len())
|
2013-05-15 09:11:39 -04:00
|
|
|
}
|
2013-05-10 00:55:08 -04:00
|
|
|
}
|
|
|
|
|
2013-07-17 00:07:41 -04:00
|
|
|
func TestPostContainersCopy(t *testing.T) {
|
2013-11-18 14:25:13 -05:00
|
|
|
eng := NewTestEngine(t)
|
|
|
|
defer mkRuntimeFromEngine(eng, t).Nuke()
|
2013-07-17 00:07:41 -04:00
|
|
|
|
|
|
|
// Create a container and remove a file
|
2013-11-18 14:25:13 -05:00
|
|
|
containerID := createTestContainer(eng,
|
2014-02-11 23:04:39 -05:00
|
|
|
&runconfig.Config{
|
2013-11-15 19:05:57 -05:00
|
|
|
Image: unitTestImageID,
|
2013-07-17 00:07:41 -04:00
|
|
|
Cmd: []string{"touch", "/test.txt"},
|
|
|
|
},
|
2013-11-15 19:05:57 -05:00
|
|
|
t,
|
2013-07-17 00:07:41 -04:00
|
|
|
)
|
2013-11-18 14:25:13 -05:00
|
|
|
containerRun(eng, containerID, t)
|
2013-07-17 00:07:41 -04:00
|
|
|
|
|
|
|
r := httptest.NewRecorder()
|
|
|
|
|
2014-01-25 02:15:40 -05:00
|
|
|
var copyData engine.Env
|
|
|
|
copyData.Set("Resource", "/test.txt")
|
|
|
|
copyData.Set("HostPath", ".")
|
|
|
|
|
|
|
|
jsonData := bytes.NewBuffer(nil)
|
|
|
|
if err := copyData.Encode(jsonData); err != nil {
|
2013-07-17 00:07:41 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-01-25 02:15:40 -05:00
|
|
|
req, err := http.NewRequest("POST", "/containers/"+containerID+"/copy", jsonData)
|
2013-07-17 00:07:41 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
req.Header.Add("Content-Type", "application/json")
|
2014-01-29 14:26:54 -05:00
|
|
|
if err := api.ServeRequest(eng, api.APIVERSION, r, req); err != nil {
|
2013-07-17 00:07:41 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-18 14:25:13 -05:00
|
|
|
assertHttpNotError(r, t)
|
2013-07-17 00:07:41 -04:00
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-14 19:43:55 -05:00
|
|
|
func TestPostContainersCopyWhenContainerNotFound(t *testing.T) {
|
|
|
|
eng := NewTestEngine(t)
|
|
|
|
defer mkRuntimeFromEngine(eng, t).Nuke()
|
|
|
|
|
|
|
|
r := httptest.NewRecorder()
|
|
|
|
|
|
|
|
var copyData engine.Env
|
|
|
|
copyData.Set("Resource", "/test.txt")
|
|
|
|
copyData.Set("HostPath", ".")
|
|
|
|
|
|
|
|
jsonData := bytes.NewBuffer(nil)
|
|
|
|
if err := copyData.Encode(jsonData); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", "/containers/id_not_found/copy", jsonData)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
req.Header.Add("Content-Type", "application/json")
|
|
|
|
if err := api.ServeRequest(eng, api.APIVERSION, r, req); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if r.Code != http.StatusNotFound {
|
|
|
|
t.Fatalf("404 expected for id_not_found Container, received %v", r.Code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|