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-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-05-08 11:35:50 -04:00
|
|
|
"testing"
|
2013-05-09 22:19:24 -04:00
|
|
|
"time"
|
2014-03-28 19:36:33 -04:00
|
|
|
|
2014-07-24 18:19:50 -04:00
|
|
|
"github.com/docker/docker/api"
|
|
|
|
"github.com/docker/docker/api/server"
|
2015-04-06 15:56:50 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2014-07-24 18:19:50 -04:00
|
|
|
"github.com/docker/docker/engine"
|
|
|
|
"github.com/docker/docker/runconfig"
|
|
|
|
"github.com/docker/docker/vendor/src/code.google.com/p/go/src/pkg/archive/tar"
|
2013-05-08 11:35:50 -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)
|
2014-04-17 17:43:01 -04:00
|
|
|
defer mkDaemonFromEngine(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,
|
2015-04-10 20:05:21 -04:00
|
|
|
Cmd: runconfig.NewCommand("/bin/cat"),
|
2013-05-09 23:13:52 -04:00
|
|
|
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
|
|
|
}
|
2015-01-13 16:41:34 -05:00
|
|
|
server.ServeRequest(eng, api.APIVERSION, r, req)
|
2013-11-18 14:25:13 -05:00
|
|
|
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)
|
2014-04-17 17:43:01 -04:00
|
|
|
defer mkDaemonFromEngine(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,
|
2015-04-10 20:05:21 -04:00
|
|
|
Cmd: runconfig.NewCommand("/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()
|
2015-01-13 16:41:34 -05:00
|
|
|
server.ServeRequest(eng, api.APIVERSION, r, req)
|
2013-11-18 14:25:13 -05:00
|
|
|
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)
|
2014-04-17 17:43:01 -04:00
|
|
|
defer mkDaemonFromEngine(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,
|
2015-04-10 20:05:21 -04:00
|
|
|
Cmd: runconfig.NewCommand("/bin/cat"),
|
2013-05-09 23:13:52 -04:00
|
|
|
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()
|
2015-01-13 16:41:34 -05:00
|
|
|
server.ServeRequest(eng, api.APIVERSION, r, req)
|
2013-11-18 14:25:13 -05:00
|
|
|
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)
|
2014-06-24 21:44:48 -04:00
|
|
|
|
|
|
|
req, err = http.NewRequest("POST", "/containers/"+containerID+"/start", bytes.NewReader(hostConfigJSON))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2013-05-09 23:13:52 -04:00
|
|
|
}
|
|
|
|
|
2014-06-24 21:44:48 -04:00
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
2013-05-10 18:24:07 -04:00
|
|
|
r = httptest.NewRecorder()
|
2015-01-13 16:41:34 -05:00
|
|
|
server.ServeRequest(eng, api.APIVERSION, r, req)
|
2014-06-24 21:44:48 -04:00
|
|
|
|
|
|
|
// Starting an already started container should return a 304
|
|
|
|
assertHttpNotError(r, t)
|
|
|
|
if r.Code != http.StatusNotModified {
|
|
|
|
t.Fatalf("%d NOT MODIFIER expected, received %d\n", http.StatusNotModified, r.Code)
|
|
|
|
}
|
2013-11-15 19:05:57 -05:00
|
|
|
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-05-09 22:19:24 -04:00
|
|
|
func TestPostContainersStop(t *testing.T) {
|
2013-11-18 14:25:13 -05:00
|
|
|
eng := NewTestEngine(t)
|
2014-04-17 17:43:01 -04:00
|
|
|
defer mkDaemonFromEngine(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,
|
2015-04-10 20:05:21 -04:00
|
|
|
Cmd: runconfig.NewCommand("/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()
|
2015-01-13 16:41:34 -05:00
|
|
|
server.ServeRequest(eng, api.APIVERSION, r, req)
|
2013-11-18 14:25:13 -05:00
|
|
|
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")
|
|
|
|
}
|
2014-06-24 21:44:48 -04:00
|
|
|
|
|
|
|
req, err = http.NewRequest("POST", "/containers/"+containerID+"/stop?t=1", bytes.NewReader([]byte{}))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r = httptest.NewRecorder()
|
2015-01-13 16:41:34 -05:00
|
|
|
server.ServeRequest(eng, api.APIVERSION, r, req)
|
2014-06-24 21:44:48 -04:00
|
|
|
|
|
|
|
// Stopping an already stopper container should return a 304
|
|
|
|
assertHttpNotError(r, t)
|
|
|
|
if r.Code != http.StatusNotModified {
|
|
|
|
t.Fatalf("%d NOT MODIFIER expected, received %d\n", http.StatusNotModified, r.Code)
|
|
|
|
}
|
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)
|
2014-04-17 17:43:01 -04:00
|
|
|
defer mkDaemonFromEngine(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,
|
2015-04-10 20:05:21 -04:00
|
|
|
Cmd: runconfig.NewCommand("/bin/sleep", "1"),
|
2013-05-09 23:13:52 -04:00
|
|
|
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
|
|
|
}
|
2015-01-13 16:41:34 -05:00
|
|
|
server.ServeRequest(eng, api.APIVERSION, r, req)
|
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)
|
2014-04-17 17:43:01 -04:00
|
|
|
defer mkDaemonFromEngine(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,
|
2015-04-10 20:05:21 -04:00
|
|
|
Cmd: runconfig.NewCommand("/bin/cat"),
|
2013-05-10 00:55:08 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2015-01-13 16:41:34 -05:00
|
|
|
server.ServeRequest(eng, api.APIVERSION, r, req)
|
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)
|
2014-04-17 17:43:01 -04:00
|
|
|
defer mkDaemonFromEngine(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,
|
2015-04-10 20:05:21 -04:00
|
|
|
Cmd: runconfig.NewCommand("/bin/sh", "-c", "/bin/cat >&2"),
|
2013-09-09 21:23:03 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2015-01-13 16:41:34 -05:00
|
|
|
server.ServeRequest(eng, api.APIVERSION, r, req)
|
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-06-10 18:02:40 -04:00
|
|
|
func TestOptionsRoute(t *testing.T) {
|
2013-11-15 19:05:57 -05:00
|
|
|
eng := NewTestEngine(t)
|
2014-04-17 17:43:01 -04:00
|
|
|
defer mkDaemonFromEngine(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)
|
|
|
|
}
|
2015-01-13 16:41:34 -05:00
|
|
|
server.ServeRequest(eng, api.APIVERSION, r, req)
|
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)
|
2014-04-17 17:43:01 -04:00
|
|
|
defer mkDaemonFromEngine(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)
|
|
|
|
}
|
2015-01-13 16:41:34 -05:00
|
|
|
server.ServeRequest(eng, api.APIVERSION, r, req)
|
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)
|
|
|
|
}
|
2014-11-01 12:22:28 -04:00
|
|
|
if allowHeaders != "Origin, X-Requested-With, Content-Type, Accept, X-Registry-Auth" {
|
|
|
|
t.Errorf("Expected header Access-Control-Allow-Headers to be \"Origin, X-Requested-With, Content-Type, Accept, X-Registry-Auth\", %s found.", allowHeaders)
|
2013-06-03 21:39:00 -04:00
|
|
|
}
|
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
|
2014-04-17 17:43:01 -04:00
|
|
|
defer mkDaemonFromEngine(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
|
|
|
|
2015-04-13 22:46:29 -04:00
|
|
|
d := getDaemon(eng)
|
|
|
|
if err := d.Repositories().Tag("test", "test", unitTestImageName, true); 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, "")
|
|
|
|
|
2015-04-07 21:57:54 -04:00
|
|
|
if len(images[0].RepoTags) != len(initialImages[0].RepoTags)+1 {
|
|
|
|
t.Errorf("Expected %d images, %d found", len(initialImages[0].RepoTags)+1, len(images[0].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()
|
2015-01-13 16:41:34 -05:00
|
|
|
server.ServeRequest(eng, api.APIVERSION, r, req)
|
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()
|
2015-01-13 16:41:34 -05:00
|
|
|
server.ServeRequest(eng, api.APIVERSION, r2, req2)
|
2013-11-18 14:25:13 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-04-06 15:56:50 -04:00
|
|
|
delImages := []types.ImageDelete{}
|
|
|
|
err = json.Unmarshal(r2.Body.Bytes(), &delImages)
|
|
|
|
if err != nil {
|
2013-05-31 10:37:02 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-04-06 15:56:50 -04:00
|
|
|
|
|
|
|
if len(delImages) != 1 {
|
|
|
|
t.Fatalf("Expected %d event (untagged), got %d", 1, len(delImages))
|
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
|
|
|
|
2015-04-07 21:57:54 -04:00
|
|
|
if len(images) != len(initialImages) {
|
|
|
|
t.Errorf("Expected %d image, %d found", len(initialImages), len(images))
|
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)
|
2014-04-17 17:43:01 -04:00
|
|
|
defer mkDaemonFromEngine(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,
|
2015-04-10 20:05:21 -04:00
|
|
|
Cmd: runconfig.NewCommand("touch", "/test.txt"),
|
2013-07-17 00:07:41 -04:00
|
|
|
},
|
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")
|
2015-01-13 16:41:34 -05:00
|
|
|
server.ServeRequest(eng, api.APIVERSION, r, req)
|
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)
|
2014-04-17 17:43:01 -04:00
|
|
|
defer mkDaemonFromEngine(eng, t).Nuke()
|
2014-02-14 19:43:55 -05:00
|
|
|
|
|
|
|
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")
|
2015-01-13 16:41:34 -05:00
|
|
|
server.ServeRequest(eng, api.APIVERSION, r, req)
|
2014-02-14 19:43:55 -05:00
|
|
|
if r.Code != http.StatusNotFound {
|
|
|
|
t.Fatalf("404 expected for id_not_found Container, received %v", r.Code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-30 22:06:31 -04:00
|
|
|
// Regression test for https://github.com/docker/docker/issues/6231
|
|
|
|
func TestConstainersStartChunkedEncodingHostConfig(t *testing.T) {
|
|
|
|
eng := NewTestEngine(t)
|
|
|
|
defer mkDaemonFromEngine(eng, t).Nuke()
|
|
|
|
|
|
|
|
r := httptest.NewRecorder()
|
|
|
|
|
|
|
|
var testData engine.Env
|
|
|
|
testData.Set("Image", "docker-test-image")
|
|
|
|
testData.SetAuto("Volumes", map[string]struct{}{"/foo": {}})
|
|
|
|
testData.Set("Cmd", "true")
|
|
|
|
jsonData := bytes.NewBuffer(nil)
|
|
|
|
if err := testData.Encode(jsonData); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", "/containers/create?name=chunk_test", jsonData)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Add("Content-Type", "application/json")
|
2015-01-13 16:41:34 -05:00
|
|
|
server.ServeRequest(eng, api.APIVERSION, r, req)
|
2014-09-30 22:06:31 -04:00
|
|
|
assertHttpNotError(r, t)
|
|
|
|
|
|
|
|
var testData2 engine.Env
|
|
|
|
testData2.SetAuto("Binds", []string{"/tmp:/foo"})
|
|
|
|
jsonData = bytes.NewBuffer(nil)
|
|
|
|
if err := testData2.Encode(jsonData); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err = http.NewRequest("POST", "/containers/chunk_test/start", jsonData)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Add("Content-Type", "application/json")
|
|
|
|
// This is a cheat to make the http request do chunked encoding
|
|
|
|
// Otherwise (just setting the Content-Encoding to chunked) net/http will overwrite
|
2015-04-11 13:31:34 -04:00
|
|
|
// https://golang.org/src/pkg/net/http/request.go?s=11980:12172
|
2014-09-30 22:06:31 -04:00
|
|
|
req.ContentLength = -1
|
2015-01-13 16:41:34 -05:00
|
|
|
server.ServeRequest(eng, api.APIVERSION, r, req)
|
2014-09-30 22:06:31 -04:00
|
|
|
assertHttpNotError(r, t)
|
|
|
|
|
|
|
|
type config struct {
|
|
|
|
HostConfig struct {
|
|
|
|
Binds []string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err = http.NewRequest("GET", "/containers/chunk_test/json", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r2 := httptest.NewRecorder()
|
|
|
|
req.Header.Add("Content-Type", "application/json")
|
2015-01-13 16:41:34 -05:00
|
|
|
server.ServeRequest(eng, api.APIVERSION, r2, req)
|
2014-09-30 22:06:31 -04:00
|
|
|
assertHttpNotError(r, t)
|
|
|
|
|
|
|
|
c := config{}
|
|
|
|
|
|
|
|
json.Unmarshal(r2.Body.Bytes(), &c)
|
|
|
|
|
|
|
|
if len(c.HostConfig.Binds) == 0 {
|
|
|
|
t.Fatal("Chunked Encoding not handled")
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.HostConfig.Binds[0] != "/tmp:/foo" {
|
|
|
|
t.Fatal("Chunked encoding not properly handled, execpted binds to be /tmp:/foo, got:", c.HostConfig.Binds[0])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|