github.com/portworx/docker@v1.12.1/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 "time" 12 13 "github.com/docker/docker/pkg/integration/checker" 14 "github.com/go-check/check" 15 ) 16 17 // Regression test for #9414 18 func (s *DockerSuite) TestExecApiCreateNoCmd(c *check.C) { 19 name := "exec_test" 20 dockerCmd(c, "run", "-d", "-t", "--name", name, "busybox", "/bin/sh") 21 22 status, body, err := sockRequest("POST", fmt.Sprintf("/containers/%s/exec", name), map[string]interface{}{"Cmd": nil}) 23 c.Assert(err, checker.IsNil) 24 c.Assert(status, checker.Equals, http.StatusInternalServerError) 25 26 comment := check.Commentf("Expected message when creating exec command with no Cmd specified") 27 c.Assert(getErrorMessage(c, body), checker.Contains, "No exec command specified", comment) 28 } 29 30 func (s *DockerSuite) TestExecApiCreateNoValidContentType(c *check.C) { 31 name := "exec_test" 32 dockerCmd(c, "run", "-d", "-t", "--name", name, "busybox", "/bin/sh") 33 34 jsonData := bytes.NewBuffer(nil) 35 if err := json.NewEncoder(jsonData).Encode(map[string]interface{}{"Cmd": nil}); err != nil { 36 c.Fatalf("Can not encode data to json %s", err) 37 } 38 39 res, body, err := sockRequestRaw("POST", fmt.Sprintf("/containers/%s/exec", name), jsonData, "text/plain") 40 c.Assert(err, checker.IsNil) 41 c.Assert(res.StatusCode, checker.Equals, http.StatusInternalServerError) 42 43 b, err := readBody(body) 44 c.Assert(err, checker.IsNil) 45 46 comment := check.Commentf("Expected message when creating exec command with invalid Content-Type specified") 47 c.Assert(getErrorMessage(c, b), checker.Contains, "Content-Type specified", comment) 48 } 49 50 func (s *DockerSuite) TestExecApiCreateContainerPaused(c *check.C) { 51 // Not relevant on Windows as Windows containers cannot be paused 52 testRequires(c, DaemonIsLinux) 53 name := "exec_create_test" 54 dockerCmd(c, "run", "-d", "-t", "--name", name, "busybox", "/bin/sh") 55 56 dockerCmd(c, "pause", name) 57 status, body, err := sockRequest("POST", fmt.Sprintf("/containers/%s/exec", name), map[string]interface{}{"Cmd": []string{"true"}}) 58 c.Assert(err, checker.IsNil) 59 c.Assert(status, checker.Equals, http.StatusConflict) 60 61 comment := check.Commentf("Expected message when creating exec command with Container %s is paused", name) 62 c.Assert(getErrorMessage(c, body), checker.Contains, "Container "+name+" is paused, unpause the container before exec", comment) 63 } 64 65 func (s *DockerSuite) TestExecApiStart(c *check.C) { 66 testRequires(c, DaemonIsLinux) // Uses pause/unpause but bits may be salvagable to Windows to Windows CI 67 dockerCmd(c, "run", "-d", "--name", "test", "busybox", "top") 68 69 id := createExec(c, "test") 70 startExec(c, id, http.StatusOK) 71 72 id = createExec(c, "test") 73 dockerCmd(c, "stop", "test") 74 75 startExec(c, id, http.StatusNotFound) 76 77 dockerCmd(c, "start", "test") 78 startExec(c, id, http.StatusNotFound) 79 80 // make sure exec is created before pausing 81 id = createExec(c, "test") 82 dockerCmd(c, "pause", "test") 83 startExec(c, id, http.StatusConflict) 84 dockerCmd(c, "unpause", "test") 85 startExec(c, id, http.StatusOK) 86 } 87 88 func (s *DockerSuite) TestExecApiStartBackwardsCompatible(c *check.C) { 89 runSleepingContainer(c, "-d", "--name", "test") 90 id := createExec(c, "test") 91 92 resp, body, err := sockRequestRaw("POST", fmt.Sprintf("/v1.20/exec/%s/start", id), strings.NewReader(`{"Detach": true}`), "text/plain") 93 c.Assert(err, checker.IsNil) 94 95 b, err := readBody(body) 96 comment := check.Commentf("response body: %s", b) 97 c.Assert(err, checker.IsNil, comment) 98 c.Assert(resp.StatusCode, checker.Equals, http.StatusOK, comment) 99 } 100 101 // #19362 102 func (s *DockerSuite) TestExecApiStartMultipleTimesError(c *check.C) { 103 runSleepingContainer(c, "-d", "--name", "test") 104 execID := createExec(c, "test") 105 startExec(c, execID, http.StatusOK) 106 107 timeout := time.After(60 * time.Second) 108 var execJSON struct{ Running bool } 109 for { 110 select { 111 case <-timeout: 112 c.Fatal("timeout waiting for exec to start") 113 default: 114 } 115 116 inspectExec(c, execID, &execJSON) 117 if !execJSON.Running { 118 break 119 } 120 } 121 122 startExec(c, execID, http.StatusConflict) 123 } 124 125 // #20638 126 func (s *DockerSuite) TestExecApiStartWithDetach(c *check.C) { 127 name := "foo" 128 runSleepingContainer(c, "-d", "-t", "--name", name) 129 data := map[string]interface{}{ 130 "cmd": []string{"true"}, 131 "AttachStdin": true, 132 } 133 _, b, err := sockRequest("POST", fmt.Sprintf("/containers/%s/exec", name), data) 134 c.Assert(err, checker.IsNil, check.Commentf(string(b))) 135 136 createResp := struct { 137 ID string `json:"Id"` 138 }{} 139 c.Assert(json.Unmarshal(b, &createResp), checker.IsNil, check.Commentf(string(b))) 140 141 _, body, err := sockRequestRaw("POST", fmt.Sprintf("/exec/%s/start", createResp.ID), strings.NewReader(`{"Detach": true}`), "application/json") 142 c.Assert(err, checker.IsNil) 143 144 b, err = readBody(body) 145 comment := check.Commentf("response body: %s", b) 146 c.Assert(err, checker.IsNil, comment) 147 148 resp, _, err := sockRequestRaw("GET", "/_ping", nil, "") 149 c.Assert(err, checker.IsNil) 150 if resp.StatusCode != http.StatusOK { 151 c.Fatal("daemon is down, it should alive") 152 } 153 } 154 155 func createExec(c *check.C, name string) string { 156 _, b, err := sockRequest("POST", fmt.Sprintf("/containers/%s/exec", name), map[string]interface{}{"Cmd": []string{"true"}}) 157 c.Assert(err, checker.IsNil, check.Commentf(string(b))) 158 159 createResp := struct { 160 ID string `json:"Id"` 161 }{} 162 c.Assert(json.Unmarshal(b, &createResp), checker.IsNil, check.Commentf(string(b))) 163 return createResp.ID 164 } 165 166 func startExec(c *check.C, id string, code int) { 167 resp, body, err := sockRequestRaw("POST", fmt.Sprintf("/exec/%s/start", id), strings.NewReader(`{"Detach": true}`), "application/json") 168 c.Assert(err, checker.IsNil) 169 170 b, err := readBody(body) 171 comment := check.Commentf("response body: %s", b) 172 c.Assert(err, checker.IsNil, comment) 173 c.Assert(resp.StatusCode, checker.Equals, code, comment) 174 } 175 176 func inspectExec(c *check.C, id string, out interface{}) { 177 resp, body, err := sockRequestRaw("GET", fmt.Sprintf("/exec/%s/json", id), nil, "") 178 c.Assert(err, checker.IsNil) 179 defer body.Close() 180 c.Assert(resp.StatusCode, checker.Equals, http.StatusOK) 181 err = json.NewDecoder(body).Decode(out) 182 c.Assert(err, checker.IsNil) 183 }