Check for no Cmd on exec create endpoint

Fixes #9414

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
This commit is contained in:
Brian Goff
2014-12-01 17:54:15 -05:00
parent 653be15f17
commit c8a3d31332
7 changed files with 57 additions and 16 deletions
@@ -23,7 +23,7 @@ func TestContainerApiGetAll(t *testing.T) {
t.Fatalf("Error on container creation: %v, output: %q", err, out)
}
body, err := sockRequest("GET", "/containers/json?all=1")
body, err := sockRequest("GET", "/containers/json?all=1", nil)
if err != nil {
t.Fatalf("GET all containers sockRequest failed: %v", err)
}
@@ -56,7 +56,7 @@ func TestContainerApiGetExport(t *testing.T) {
t.Fatalf("Error on container creation: %v, output: %q", err, out)
}
body, err := sockRequest("GET", "/containers/"+name+"/export")
body, err := sockRequest("GET", "/containers/"+name+"/export", nil)
if err != nil {
t.Fatalf("GET containers/export sockRequest failed: %v", err)
}
@@ -92,7 +92,7 @@ func TestContainerApiGetChanges(t *testing.T) {
t.Fatalf("Error on container creation: %v, output: %q", err, out)
}
body, err := sockRequest("GET", "/containers/"+name+"/changes")
body, err := sockRequest("GET", "/containers/"+name+"/changes", nil)
if err != nil {
t.Fatalf("GET containers/changes sockRequest failed: %v", err)
}
+25
View File
@@ -0,0 +1,25 @@
package main
import (
"bytes"
"fmt"
"os/exec"
"testing"
)
// Regression test for #9414
func TestExecApiCreateNoCmd(t *testing.T) {
defer deleteAllContainers()
name := "exec_test"
runCmd := exec.Command(dockerBinary, "run", "-d", "-t", "--name", name, "busybox", "/bin/sh")
if out, _, err := runCommandWithOutput(runCmd); err != nil {
t.Fatal(out, err)
}
body, err := sockRequest("POST", fmt.Sprintf("/containers/%s/exec", name), map[string]interface{}{"Cmd": nil})
if err == nil || !bytes.Contains(body, []byte("No exec command specified")) {
t.Fatalf("Expected error when creating exec command with no Cmd specified: %q", err)
}
logDone("exec create API - returns error when missing Cmd")
}
+1 -1
View File
@@ -24,7 +24,7 @@ func TestInspectApiContainerResponse(t *testing.T) {
if testVersion != "latest" {
endpoint = "/" + testVersion + endpoint
}
body, err := sockRequest("GET", endpoint)
body, err := sockRequest("GET", endpoint, nil)
if err != nil {
t.Fatalf("sockRequest failed for %s version: %v", testVersion, err)
}
+2 -2
View File
@@ -16,7 +16,7 @@ func TestResizeApiResponse(t *testing.T) {
cleanedContainerID := stripTrailingCharacters(out)
endpoint := "/containers/" + cleanedContainerID + "/resize?h=40&w=40"
_, err = sockRequest("POST", endpoint)
_, err = sockRequest("POST", endpoint, nil)
if err != nil {
t.Fatalf("resize Request failed %v", err)
}
@@ -41,7 +41,7 @@ func TestResizeApiResponseWhenContainerNotStarted(t *testing.T) {
}
endpoint := "/containers/" + cleanedContainerID + "/resize?h=40&w=40"
body, err := sockRequest("POST", endpoint)
body, err := sockRequest("POST", endpoint, nil)
if err == nil {
t.Fatalf("resize should fail when container is not started")
}
+9 -2
View File
@@ -1,6 +1,8 @@
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
@@ -249,7 +251,7 @@ func (d *Daemon) Cmd(name string, arg ...string) (string, error) {
return string(b), err
}
func sockRequest(method, endpoint string) ([]byte, error) {
func sockRequest(method, endpoint string, data interface{}) ([]byte, error) {
// FIX: the path to sock should not be hardcoded
sock := filepath.Join("/", "var", "run", "docker.sock")
c, err := net.DialTimeout("unix", sock, time.Duration(10*time.Second))
@@ -260,7 +262,12 @@ func sockRequest(method, endpoint string) ([]byte, error) {
client := httputil.NewClientConn(c, nil)
defer client.Close()
req, err := http.NewRequest(method, endpoint, nil)
jsonData := bytes.NewBuffer(nil)
if err := json.NewEncoder(jsonData).Encode(data); err != nil {
return nil, err
}
req, err := http.NewRequest(method, endpoint, jsonData)
req.Header.Set("Content-Type", "application/json")
if err != nil {
return nil, fmt.Errorf("could not create new request: %v", err)