Update github.com/fsouza/go-dockerclient to pick up IpcMode support.

This commit is contained in:
Mrunal Patel
2015-01-22 12:03:38 -08:00
parent 80ad188912
commit 683fd13ce6
20 changed files with 509 additions and 66 deletions

View File

@@ -31,7 +31,7 @@ _lint_verbose() {
_install_linter() {
if [[ ! -x "${GOPATH}/bin/golint" ]] ; then
go get -u github.com/golang/lint/golint
go get -u -f github.com/golang/lint/golint
fi
}

View File

@@ -34,7 +34,7 @@ import (
// For more details on the remote API, check http://goo.gl/G3plxW.
type DockerServer struct {
containers []*docker.Container
execs []*docker.Exec
execs []*docker.ExecInspect
cMut sync.RWMutex
images []docker.Image
iMut sync.RWMutex
@@ -99,7 +99,9 @@ func (s *DockerServer) buildMuxer() {
s.mux.Path("/containers/{id:.*}/attach").Methods("POST").HandlerFunc(s.handlerWrapper(s.attachContainer))
s.mux.Path("/containers/{id:.*}").Methods("DELETE").HandlerFunc(s.handlerWrapper(s.removeContainer))
s.mux.Path("/containers/{id:.*}/exec").Methods("POST").HandlerFunc(s.handlerWrapper(s.createExecContainer))
s.mux.Path("/exec/{id:.*}/resize").Methods("POST").HandlerFunc(s.handlerWrapper(s.resizeExecContainer))
s.mux.Path("/exec/{id:.*}/start").Methods("POST").HandlerFunc(s.handlerWrapper(s.startExecContainer))
s.mux.Path("/exec/{id:.*}/json").Methods("GET").HandlerFunc(s.handlerWrapper(s.inspectExecContainer))
s.mux.Path("/images/create").Methods("POST").HandlerFunc(s.handlerWrapper(s.pullImage))
s.mux.Path("/build").Methods("POST").HandlerFunc(s.handlerWrapper(s.buildImage))
s.mux.Path("/images/json").Methods("GET").HandlerFunc(s.handlerWrapper(s.listImages))
@@ -724,10 +726,31 @@ func (s *DockerServer) getImage(w http.ResponseWriter, r *http.Request) {
}
func (s *DockerServer) createExecContainer(w http.ResponseWriter, r *http.Request) {
id := mux.Vars(r)["id"]
container, _, err := s.findContainer(id)
if err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
exec := docker.ExecInspect{
ID: "id-exec-created-by-test",
Container: *container,
}
var params docker.CreateExecOptions
err = json.NewDecoder(r.Body).Decode(&params)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if len(params.Cmd) > 0 {
exec.ProcessConfig.EntryPoint = params.Cmd[0]
if len(params.Cmd) > 1 {
exec.ProcessConfig.Arguments = params.Cmd[1:]
}
}
s.execs = append(s.execs, &exec)
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
exec := docker.Exec{ID: "id-exec-created-by-test"}
s.execs = append(s.execs, &exec)
json.NewEncoder(w).Encode(map[string]string{"Id": exec.ID})
}
@@ -742,3 +765,27 @@ func (s *DockerServer) startExecContainer(w http.ResponseWriter, r *http.Request
}
w.WriteHeader(http.StatusNotFound)
}
func (s *DockerServer) resizeExecContainer(w http.ResponseWriter, r *http.Request) {
id := mux.Vars(r)["id"]
for _, exec := range s.execs {
if exec.ID == id {
w.WriteHeader(http.StatusOK)
return
}
}
w.WriteHeader(http.StatusNotFound)
}
func (s *DockerServer) inspectExecContainer(w http.ResponseWriter, r *http.Request) {
id := mux.Vars(r)["id"]
for _, exec := range s.execs {
if exec.ID == id {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(exec)
return
}
}
w.WriteHeader(http.StatusNotFound)
}

View File

@@ -1089,3 +1089,83 @@ func TestDefaultHandler(t *testing.T) {
t.Fatalf("DefaultHandler: Expected to return server.mux, got: %#v", server.DefaultHandler())
}
}
func TestCreateExecContainer(t *testing.T) {
server := DockerServer{}
addContainers(&server, 2)
server.buildMuxer()
recorder := httptest.NewRecorder()
body := `{"Cmd": ["bash", "-c", "ls"]}`
path := fmt.Sprintf("/containers/%s/exec", server.containers[0].ID)
request, _ := http.NewRequest("POST", path, strings.NewReader(body))
server.ServeHTTP(recorder, request)
if recorder.Code != http.StatusOK {
t.Fatalf("CreateExec: wrong status. Want %d. Got %d.", http.StatusOK, recorder.Code)
}
serverExec := server.execs[0]
var got docker.Exec
err := json.NewDecoder(recorder.Body).Decode(&got)
if err != nil {
t.Fatal(err)
}
if got.ID != serverExec.ID {
t.Errorf("CreateExec: wrong value. Want %#v. Got %#v.", serverExec.ID, got.ID)
}
expected := docker.ExecInspect{
ID: got.ID,
ProcessConfig: docker.ExecProcessConfig{
EntryPoint: "bash",
Arguments: []string{"-c", "ls"},
},
Container: *server.containers[0],
}
if !reflect.DeepEqual(*serverExec, expected) {
t.Errorf("InspectContainer: wrong value. Want:\n%#v\nGot:\n%#v\n", expected, *serverExec)
}
}
func TestInspectExecContainer(t *testing.T) {
server := DockerServer{}
addContainers(&server, 1)
server.buildMuxer()
recorder := httptest.NewRecorder()
body := `{"Cmd": ["bash", "-c", "ls"]}`
path := fmt.Sprintf("/containers/%s/exec", server.containers[0].ID)
request, _ := http.NewRequest("POST", path, strings.NewReader(body))
server.ServeHTTP(recorder, request)
if recorder.Code != http.StatusOK {
t.Fatalf("CreateExec: wrong status. Want %d. Got %d.", http.StatusOK, recorder.Code)
}
var got docker.Exec
err := json.NewDecoder(recorder.Body).Decode(&got)
if err != nil {
t.Fatal(err)
}
path = fmt.Sprintf("/exec/%s/json", got.ID)
request, _ = http.NewRequest("GET", path, nil)
server.ServeHTTP(recorder, request)
if recorder.Code != http.StatusOK {
t.Fatalf("CreateExec: wrong status. Want %d. Got %d.", http.StatusOK, recorder.Code)
}
var got2 docker.ExecInspect
err = json.NewDecoder(recorder.Body).Decode(&got2)
if err != nil {
t.Fatal(err)
}
expected := docker.ExecInspect{
ID: got.ID,
ProcessConfig: docker.ExecProcessConfig{
EntryPoint: "bash",
Arguments: []string{"-c", "ls"},
},
Container: *server.containers[0],
}
got2.Container.State.StartedAt = expected.Container.State.StartedAt
got2.Container.State.FinishedAt = expected.Container.State.FinishedAt
got2.Container.Config = expected.Container.Config
got2.Container.Created = expected.Container.Created
got2.Container.NetworkSettings = expected.Container.NetworkSettings
if !reflect.DeepEqual(got2, expected) {
t.Errorf("InspectContainer: wrong value. Want:\n%#v\nGot:\n%#v\n", expected, got2)
}
}