github.com/shiroyuki/docker@v1.9.0/integration-cli/docker_api_exec_test.go (about) 1 // +build !test_no_exec 2 3 package main 4 5 import ( 6 "bytes" 7 "encoding/json" 8 "fmt" 9 "net/http" 10 "strings" 11 12 "github.com/go-check/check" 13 ) 14 15 // Regression test for #9414 16 func (s *DockerSuite) TestExecApiCreateNoCmd(c *check.C) { 17 testRequires(c, DaemonIsLinux) 18 name := "exec_test" 19 dockerCmd(c, "run", "-d", "-t", "--name", name, "busybox", "/bin/sh") 20 21 status, body, err := sockRequest("POST", fmt.Sprintf("/containers/%s/exec", name), map[string]interface{}{"Cmd": nil}) 22 c.Assert(err, check.IsNil) 23 c.Assert(status, check.Equals, http.StatusInternalServerError) 24 25 if !bytes.Contains(body, []byte("No exec command specified")) { 26 c.Fatalf("Expected message when creating exec command with no Cmd specified") 27 } 28 } 29 30 func (s *DockerSuite) TestExecApiCreateNoValidContentType(c *check.C) { 31 testRequires(c, DaemonIsLinux) 32 name := "exec_test" 33 dockerCmd(c, "run", "-d", "-t", "--name", name, "busybox", "/bin/sh") 34 35 jsonData := bytes.NewBuffer(nil) 36 if err := json.NewEncoder(jsonData).Encode(map[string]interface{}{"Cmd": nil}); err != nil { 37 c.Fatalf("Can not encode data to json %s", err) 38 } 39 40 res, body, err := sockRequestRaw("POST", fmt.Sprintf("/containers/%s/exec", name), jsonData, "text/plain") 41 c.Assert(err, check.IsNil) 42 c.Assert(res.StatusCode, check.Equals, http.StatusInternalServerError) 43 44 b, err := readBody(body) 45 c.Assert(err, check.IsNil) 46 47 if !bytes.Contains(b, []byte("Content-Type specified")) { 48 c.Fatalf("Expected message when creating exec command with invalid Content-Type specified") 49 } 50 } 51 52 func (s *DockerSuite) TestExecAPIStart(c *check.C) { 53 dockerCmd(c, "run", "-d", "--name", "test", "busybox", "top") 54 55 createExec := func() string { 56 _, b, err := sockRequest("POST", fmt.Sprintf("/containers/%s/exec", "test"), map[string]interface{}{"Cmd": []string{"true"}}) 57 c.Assert(err, check.IsNil, check.Commentf(string(b))) 58 59 createResp := struct { 60 ID string `json:"Id"` 61 }{} 62 c.Assert(json.Unmarshal(b, &createResp), check.IsNil, check.Commentf(string(b))) 63 return createResp.ID 64 } 65 66 startExec := func(id string, code int) { 67 resp, body, err := sockRequestRaw("POST", fmt.Sprintf("/exec/%s/start", id), strings.NewReader(`{"Detach": true}`), "application/json") 68 c.Assert(err, check.IsNil) 69 70 b, err := readBody(body) 71 c.Assert(err, check.IsNil, check.Commentf(string(b))) 72 c.Assert(resp.StatusCode, check.Equals, code, check.Commentf(string(b))) 73 } 74 75 startExec(createExec(), http.StatusOK) 76 77 id := createExec() 78 dockerCmd(c, "stop", "test") 79 80 startExec(id, http.StatusNotFound) 81 82 dockerCmd(c, "start", "test") 83 startExec(id, http.StatusNotFound) 84 85 // make sure exec is created before pausing 86 id = createExec() 87 dockerCmd(c, "pause", "test") 88 startExec(id, http.StatusConflict) 89 dockerCmd(c, "unpause", "test") 90 startExec(id, http.StatusOK) 91 }