docker_api_containers_test.go 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223
  1. package main
  2. import (
  3. "archive/tar"
  4. "bytes"
  5. "encoding/json"
  6. "io"
  7. "net/http"
  8. "net/http/httputil"
  9. "os"
  10. "os/exec"
  11. "strings"
  12. "time"
  13. "github.com/docker/docker/api/types"
  14. "github.com/docker/docker/pkg/stringid"
  15. "github.com/go-check/check"
  16. )
  17. func (s *DockerSuite) TestContainerApiGetAll(c *check.C) {
  18. startCount, err := getContainerCount()
  19. if err != nil {
  20. c.Fatalf("Cannot query container count: %v", err)
  21. }
  22. name := "getall"
  23. runCmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "true")
  24. out, _, err := runCommandWithOutput(runCmd)
  25. if err != nil {
  26. c.Fatalf("Error on container creation: %v, output: %q", err, out)
  27. }
  28. status, body, err := sockRequest("GET", "/containers/json?all=1", nil)
  29. c.Assert(status, check.Equals, http.StatusOK)
  30. c.Assert(err, check.IsNil)
  31. var inspectJSON []struct {
  32. Names []string
  33. }
  34. if err = json.Unmarshal(body, &inspectJSON); err != nil {
  35. c.Fatalf("unable to unmarshal response body: %v", err)
  36. }
  37. if len(inspectJSON) != startCount+1 {
  38. c.Fatalf("Expected %d container(s), %d found (started with: %d)", startCount+1, len(inspectJSON), startCount)
  39. }
  40. if actual := inspectJSON[0].Names[0]; actual != "/"+name {
  41. c.Fatalf("Container Name mismatch. Expected: %q, received: %q\n", "/"+name, actual)
  42. }
  43. }
  44. func (s *DockerSuite) TestContainerApiGetExport(c *check.C) {
  45. name := "exportcontainer"
  46. runCmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "touch", "/test")
  47. out, _, err := runCommandWithOutput(runCmd)
  48. if err != nil {
  49. c.Fatalf("Error on container creation: %v, output: %q", err, out)
  50. }
  51. status, body, err := sockRequest("GET", "/containers/"+name+"/export", nil)
  52. c.Assert(status, check.Equals, http.StatusOK)
  53. c.Assert(err, check.IsNil)
  54. found := false
  55. for tarReader := tar.NewReader(bytes.NewReader(body)); ; {
  56. h, err := tarReader.Next()
  57. if err != nil {
  58. if err == io.EOF {
  59. break
  60. }
  61. c.Fatal(err)
  62. }
  63. if h.Name == "test" {
  64. found = true
  65. break
  66. }
  67. }
  68. if !found {
  69. c.Fatalf("The created test file has not been found in the exported image")
  70. }
  71. }
  72. func (s *DockerSuite) TestContainerApiGetChanges(c *check.C) {
  73. name := "changescontainer"
  74. runCmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "rm", "/etc/passwd")
  75. out, _, err := runCommandWithOutput(runCmd)
  76. if err != nil {
  77. c.Fatalf("Error on container creation: %v, output: %q", err, out)
  78. }
  79. status, body, err := sockRequest("GET", "/containers/"+name+"/changes", nil)
  80. c.Assert(status, check.Equals, http.StatusOK)
  81. c.Assert(err, check.IsNil)
  82. changes := []struct {
  83. Kind int
  84. Path string
  85. }{}
  86. if err = json.Unmarshal(body, &changes); err != nil {
  87. c.Fatalf("unable to unmarshal response body: %v", err)
  88. }
  89. // Check the changelog for removal of /etc/passwd
  90. success := false
  91. for _, elem := range changes {
  92. if elem.Path == "/etc/passwd" && elem.Kind == 2 {
  93. success = true
  94. }
  95. }
  96. if !success {
  97. c.Fatalf("/etc/passwd has been removed but is not present in the diff")
  98. }
  99. }
  100. func (s *DockerSuite) TestContainerApiStartVolumeBinds(c *check.C) {
  101. name := "testing"
  102. config := map[string]interface{}{
  103. "Image": "busybox",
  104. "Volumes": map[string]struct{}{"/tmp": {}},
  105. }
  106. status, _, err := sockRequest("POST", "/containers/create?name="+name, config)
  107. c.Assert(status, check.Equals, http.StatusCreated)
  108. c.Assert(err, check.IsNil)
  109. bindPath := randomUnixTmpDirPath("test")
  110. config = map[string]interface{}{
  111. "Binds": []string{bindPath + ":/tmp"},
  112. }
  113. status, _, err = sockRequest("POST", "/containers/"+name+"/start", config)
  114. c.Assert(status, check.Equals, http.StatusNoContent)
  115. c.Assert(err, check.IsNil)
  116. pth, err := inspectFieldMap(name, "Volumes", "/tmp")
  117. if err != nil {
  118. c.Fatal(err)
  119. }
  120. if pth != bindPath {
  121. c.Fatalf("expected volume host path to be %s, got %s", bindPath, pth)
  122. }
  123. }
  124. // Test for GH#10618
  125. func (s *DockerSuite) TestContainerApiStartDupVolumeBinds(c *check.C) {
  126. name := "testdups"
  127. config := map[string]interface{}{
  128. "Image": "busybox",
  129. "Volumes": map[string]struct{}{"/tmp": {}},
  130. }
  131. status, _, err := sockRequest("POST", "/containers/create?name="+name, config)
  132. c.Assert(status, check.Equals, http.StatusCreated)
  133. c.Assert(err, check.IsNil)
  134. bindPath1 := randomUnixTmpDirPath("test1")
  135. bindPath2 := randomUnixTmpDirPath("test2")
  136. config = map[string]interface{}{
  137. "Binds": []string{bindPath1 + ":/tmp", bindPath2 + ":/tmp"},
  138. }
  139. status, body, err := sockRequest("POST", "/containers/"+name+"/start", config)
  140. c.Assert(status, check.Equals, http.StatusInternalServerError)
  141. c.Assert(err, check.IsNil)
  142. if !strings.Contains(string(body), "Duplicate volume") {
  143. c.Fatalf("Expected failure due to duplicate bind mounts to same path, instead got: %q with error: %v", string(body), err)
  144. }
  145. }
  146. func (s *DockerSuite) TestContainerApiStartVolumesFrom(c *check.C) {
  147. volName := "voltst"
  148. volPath := "/tmp"
  149. if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "--name", volName, "-v", volPath, "busybox")); err != nil {
  150. c.Fatal(out, err)
  151. }
  152. name := "TestContainerApiStartDupVolumeBinds"
  153. config := map[string]interface{}{
  154. "Image": "busybox",
  155. "Volumes": map[string]struct{}{volPath: {}},
  156. }
  157. status, _, err := sockRequest("POST", "/containers/create?name="+name, config)
  158. c.Assert(status, check.Equals, http.StatusCreated)
  159. c.Assert(err, check.IsNil)
  160. config = map[string]interface{}{
  161. "VolumesFrom": []string{volName},
  162. }
  163. status, _, err = sockRequest("POST", "/containers/"+name+"/start", config)
  164. c.Assert(status, check.Equals, http.StatusNoContent)
  165. c.Assert(err, check.IsNil)
  166. pth, err := inspectFieldMap(name, "Volumes", volPath)
  167. if err != nil {
  168. c.Fatal(err)
  169. }
  170. pth2, err := inspectFieldMap(volName, "Volumes", volPath)
  171. if err != nil {
  172. c.Fatal(err)
  173. }
  174. if pth != pth2 {
  175. c.Fatalf("expected volume host path to be %s, got %s", pth, pth2)
  176. }
  177. }
  178. // Ensure that volumes-from has priority over binds/anything else
  179. // This is pretty much the same as TestRunApplyVolumesFromBeforeVolumes, except with passing the VolumesFrom and the bind on start
  180. func (s *DockerSuite) TestVolumesFromHasPriority(c *check.C) {
  181. volName := "voltst2"
  182. volPath := "/tmp"
  183. if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "--name", volName, "-v", volPath, "busybox")); err != nil {
  184. c.Fatal(out, err)
  185. }
  186. name := "testing"
  187. config := map[string]interface{}{
  188. "Image": "busybox",
  189. "Volumes": map[string]struct{}{volPath: {}},
  190. }
  191. status, _, err := sockRequest("POST", "/containers/create?name="+name, config)
  192. c.Assert(status, check.Equals, http.StatusCreated)
  193. c.Assert(err, check.IsNil)
  194. bindPath := randomUnixTmpDirPath("test")
  195. config = map[string]interface{}{
  196. "VolumesFrom": []string{volName},
  197. "Binds": []string{bindPath + ":/tmp"},
  198. }
  199. status, _, err = sockRequest("POST", "/containers/"+name+"/start", config)
  200. c.Assert(status, check.Equals, http.StatusNoContent)
  201. c.Assert(err, check.IsNil)
  202. pth, err := inspectFieldMap(name, "Volumes", volPath)
  203. if err != nil {
  204. c.Fatal(err)
  205. }
  206. pth2, err := inspectFieldMap(volName, "Volumes", volPath)
  207. if err != nil {
  208. c.Fatal(err)
  209. }
  210. if pth != pth2 {
  211. c.Fatalf("expected volume host path to be %s, got %s", pth, pth2)
  212. }
  213. }
  214. func (s *DockerSuite) TestGetContainerStats(c *check.C) {
  215. var (
  216. name = "statscontainer"
  217. runCmd = exec.Command(dockerBinary, "run", "-d", "--name", name, "busybox", "top")
  218. )
  219. out, _, err := runCommandWithOutput(runCmd)
  220. if err != nil {
  221. c.Fatalf("Error on container creation: %v, output: %q", err, out)
  222. }
  223. type b struct {
  224. status int
  225. body []byte
  226. err error
  227. }
  228. bc := make(chan b, 1)
  229. go func() {
  230. status, body, err := sockRequest("GET", "/containers/"+name+"/stats", nil)
  231. bc <- b{status, body, err}
  232. }()
  233. // allow some time to stream the stats from the container
  234. time.Sleep(4 * time.Second)
  235. if _, err := runCommand(exec.Command(dockerBinary, "rm", "-f", name)); err != nil {
  236. c.Fatal(err)
  237. }
  238. // collect the results from the stats stream or timeout and fail
  239. // if the stream was not disconnected.
  240. select {
  241. case <-time.After(2 * time.Second):
  242. c.Fatal("stream was not closed after container was removed")
  243. case sr := <-bc:
  244. c.Assert(sr.err, check.IsNil)
  245. c.Assert(sr.status, check.Equals, http.StatusOK)
  246. dec := json.NewDecoder(bytes.NewBuffer(sr.body))
  247. var s *types.Stats
  248. // decode only one object from the stream
  249. if err := dec.Decode(&s); err != nil {
  250. c.Fatal(err)
  251. }
  252. }
  253. }
  254. func (s *DockerSuite) TestGetStoppedContainerStats(c *check.C) {
  255. // TODO: this test does nothing because we are c.Assert'ing in goroutine
  256. var (
  257. name = "statscontainer"
  258. runCmd = exec.Command(dockerBinary, "create", "--name", name, "busybox", "top")
  259. )
  260. out, _, err := runCommandWithOutput(runCmd)
  261. if err != nil {
  262. c.Fatalf("Error on container creation: %v, output: %q", err, out)
  263. }
  264. go func() {
  265. // We'll never get return for GET stats from sockRequest as of now,
  266. // just send request and see if panic or error would happen on daemon side.
  267. status, _, err := sockRequest("GET", "/containers/"+name+"/stats", nil)
  268. c.Assert(status, check.Equals, http.StatusOK)
  269. c.Assert(err, check.IsNil)
  270. }()
  271. // allow some time to send request and let daemon deal with it
  272. time.Sleep(1 * time.Second)
  273. }
  274. func (s *DockerSuite) TestBuildApiDockerfilePath(c *check.C) {
  275. // Test to make sure we stop people from trying to leave the
  276. // build context when specifying the path to the dockerfile
  277. buffer := new(bytes.Buffer)
  278. tw := tar.NewWriter(buffer)
  279. defer tw.Close()
  280. dockerfile := []byte("FROM busybox")
  281. if err := tw.WriteHeader(&tar.Header{
  282. Name: "Dockerfile",
  283. Size: int64(len(dockerfile)),
  284. }); err != nil {
  285. c.Fatalf("failed to write tar file header: %v", err)
  286. }
  287. if _, err := tw.Write(dockerfile); err != nil {
  288. c.Fatalf("failed to write tar file content: %v", err)
  289. }
  290. if err := tw.Close(); err != nil {
  291. c.Fatalf("failed to close tar archive: %v", err)
  292. }
  293. res, body, err := sockRequestRaw("POST", "/build?dockerfile=../Dockerfile", buffer, "application/x-tar")
  294. c.Assert(res.StatusCode, check.Equals, http.StatusInternalServerError)
  295. c.Assert(err, check.IsNil)
  296. out, err := readBody(body)
  297. if err != nil {
  298. c.Fatal(err)
  299. }
  300. if !strings.Contains(string(out), "must be within the build context") {
  301. c.Fatalf("Didn't complain about leaving build context: %s", out)
  302. }
  303. }
  304. func (s *DockerSuite) TestBuildApiDockerFileRemote(c *check.C) {
  305. server, err := fakeStorage(map[string]string{
  306. "testD": `FROM busybox
  307. COPY * /tmp/
  308. RUN find / -name ba*
  309. RUN find /tmp/`,
  310. })
  311. if err != nil {
  312. c.Fatal(err)
  313. }
  314. defer server.Close()
  315. res, body, err := sockRequestRaw("POST", "/build?dockerfile=baz&remote="+server.URL()+"/testD", nil, "application/json")
  316. c.Assert(res.StatusCode, check.Equals, http.StatusOK)
  317. c.Assert(err, check.IsNil)
  318. buf, err := readBody(body)
  319. if err != nil {
  320. c.Fatal(err)
  321. }
  322. // Make sure Dockerfile exists.
  323. // Make sure 'baz' doesn't exist ANYWHERE despite being mentioned in the URL
  324. out := string(buf)
  325. if !strings.Contains(out, "/tmp/Dockerfile") ||
  326. strings.Contains(out, "baz") {
  327. c.Fatalf("Incorrect output: %s", out)
  328. }
  329. }
  330. func (s *DockerSuite) TestBuildApiLowerDockerfile(c *check.C) {
  331. git, err := fakeGIT("repo", map[string]string{
  332. "dockerfile": `FROM busybox
  333. RUN echo from dockerfile`,
  334. }, false)
  335. if err != nil {
  336. c.Fatal(err)
  337. }
  338. defer git.Close()
  339. res, body, err := sockRequestRaw("POST", "/build?remote="+git.RepoURL, nil, "application/json")
  340. c.Assert(res.StatusCode, check.Equals, http.StatusOK)
  341. c.Assert(err, check.IsNil)
  342. buf, err := readBody(body)
  343. if err != nil {
  344. c.Fatal(err)
  345. }
  346. out := string(buf)
  347. if !strings.Contains(out, "from dockerfile") {
  348. c.Fatalf("Incorrect output: %s", out)
  349. }
  350. }
  351. func (s *DockerSuite) TestBuildApiBuildGitWithF(c *check.C) {
  352. git, err := fakeGIT("repo", map[string]string{
  353. "baz": `FROM busybox
  354. RUN echo from baz`,
  355. "Dockerfile": `FROM busybox
  356. RUN echo from Dockerfile`,
  357. }, false)
  358. if err != nil {
  359. c.Fatal(err)
  360. }
  361. defer git.Close()
  362. // Make sure it tries to 'dockerfile' query param value
  363. res, body, err := sockRequestRaw("POST", "/build?dockerfile=baz&remote="+git.RepoURL, nil, "application/json")
  364. c.Assert(res.StatusCode, check.Equals, http.StatusOK)
  365. c.Assert(err, check.IsNil)
  366. buf, err := readBody(body)
  367. if err != nil {
  368. c.Fatal(err)
  369. }
  370. out := string(buf)
  371. if !strings.Contains(out, "from baz") {
  372. c.Fatalf("Incorrect output: %s", out)
  373. }
  374. }
  375. func (s *DockerSuite) TestBuildApiDoubleDockerfile(c *check.C) {
  376. testRequires(c, UnixCli) // dockerfile overwrites Dockerfile on Windows
  377. git, err := fakeGIT("repo", map[string]string{
  378. "Dockerfile": `FROM busybox
  379. RUN echo from Dockerfile`,
  380. "dockerfile": `FROM busybox
  381. RUN echo from dockerfile`,
  382. }, false)
  383. if err != nil {
  384. c.Fatal(err)
  385. }
  386. defer git.Close()
  387. // Make sure it tries to 'dockerfile' query param value
  388. res, body, err := sockRequestRaw("POST", "/build?remote="+git.RepoURL, nil, "application/json")
  389. c.Assert(res.StatusCode, check.Equals, http.StatusOK)
  390. c.Assert(err, check.IsNil)
  391. buf, err := readBody(body)
  392. if err != nil {
  393. c.Fatal(err)
  394. }
  395. out := string(buf)
  396. if !strings.Contains(out, "from Dockerfile") {
  397. c.Fatalf("Incorrect output: %s", out)
  398. }
  399. }
  400. func (s *DockerSuite) TestBuildApiDockerfileSymlink(c *check.C) {
  401. // Test to make sure we stop people from trying to leave the
  402. // build context when specifying a symlink as the path to the dockerfile
  403. buffer := new(bytes.Buffer)
  404. tw := tar.NewWriter(buffer)
  405. defer tw.Close()
  406. if err := tw.WriteHeader(&tar.Header{
  407. Name: "Dockerfile",
  408. Typeflag: tar.TypeSymlink,
  409. Linkname: "/etc/passwd",
  410. }); err != nil {
  411. c.Fatalf("failed to write tar file header: %v", err)
  412. }
  413. if err := tw.Close(); err != nil {
  414. c.Fatalf("failed to close tar archive: %v", err)
  415. }
  416. res, body, err := sockRequestRaw("POST", "/build", buffer, "application/x-tar")
  417. c.Assert(res.StatusCode, check.Equals, http.StatusInternalServerError)
  418. c.Assert(err, check.IsNil)
  419. out, err := readBody(body)
  420. if err != nil {
  421. c.Fatal(err)
  422. }
  423. // The reason the error is "Cannot locate specified Dockerfile" is because
  424. // in the builder, the symlink is resolved within the context, therefore
  425. // Dockerfile -> /etc/passwd becomes etc/passwd from the context which is
  426. // a nonexistent file.
  427. if !strings.Contains(string(out), "Cannot locate specified Dockerfile: Dockerfile") {
  428. c.Fatalf("Didn't complain about leaving build context: %s", out)
  429. }
  430. }
  431. // #9981 - Allow a docker created volume (ie, one in /var/lib/docker/volumes) to be used to overwrite (via passing in Binds on api start) an existing volume
  432. func (s *DockerSuite) TestPostContainerBindNormalVolume(c *check.C) {
  433. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "create", "-v", "/foo", "--name=one", "busybox"))
  434. if err != nil {
  435. c.Fatal(err, out)
  436. }
  437. fooDir, err := inspectFieldMap("one", "Volumes", "/foo")
  438. if err != nil {
  439. c.Fatal(err)
  440. }
  441. out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "create", "-v", "/foo", "--name=two", "busybox"))
  442. if err != nil {
  443. c.Fatal(err, out)
  444. }
  445. bindSpec := map[string][]string{"Binds": {fooDir + ":/foo"}}
  446. status, _, err := sockRequest("POST", "/containers/two/start", bindSpec)
  447. c.Assert(status, check.Equals, http.StatusNoContent)
  448. c.Assert(err, check.IsNil)
  449. fooDir2, err := inspectFieldMap("two", "Volumes", "/foo")
  450. if err != nil {
  451. c.Fatal(err)
  452. }
  453. if fooDir2 != fooDir {
  454. c.Fatalf("expected volume path to be %s, got: %s", fooDir, fooDir2)
  455. }
  456. }
  457. func (s *DockerSuite) TestContainerApiPause(c *check.C) {
  458. defer unpauseAllContainers()
  459. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sleep", "30")
  460. out, _, err := runCommandWithOutput(runCmd)
  461. if err != nil {
  462. c.Fatalf("failed to create a container: %s, %v", out, err)
  463. }
  464. ContainerID := strings.TrimSpace(out)
  465. status, _, err := sockRequest("POST", "/containers/"+ContainerID+"/pause", nil)
  466. c.Assert(status, check.Equals, http.StatusNoContent)
  467. c.Assert(err, check.IsNil)
  468. pausedContainers, err := getSliceOfPausedContainers()
  469. if err != nil {
  470. c.Fatalf("error thrown while checking if containers were paused: %v", err)
  471. }
  472. if len(pausedContainers) != 1 || stringid.TruncateID(ContainerID) != pausedContainers[0] {
  473. c.Fatalf("there should be one paused container and not %d", len(pausedContainers))
  474. }
  475. status, _, err = sockRequest("POST", "/containers/"+ContainerID+"/unpause", nil)
  476. c.Assert(status, check.Equals, http.StatusNoContent)
  477. c.Assert(err, check.IsNil)
  478. pausedContainers, err = getSliceOfPausedContainers()
  479. if err != nil {
  480. c.Fatalf("error thrown while checking if containers were paused: %v", err)
  481. }
  482. if pausedContainers != nil {
  483. c.Fatalf("There should be no paused container.")
  484. }
  485. }
  486. func (s *DockerSuite) TestContainerApiTop(c *check.C) {
  487. out, err := exec.Command(dockerBinary, "run", "-d", "busybox", "/bin/sh", "-c", "top").CombinedOutput()
  488. if err != nil {
  489. c.Fatal(err, out)
  490. }
  491. id := strings.TrimSpace(string(out))
  492. if err := waitRun(id); err != nil {
  493. c.Fatal(err)
  494. }
  495. type topResp struct {
  496. Titles []string
  497. Processes [][]string
  498. }
  499. var top topResp
  500. status, b, err := sockRequest("GET", "/containers/"+id+"/top?ps_args=aux", nil)
  501. c.Assert(status, check.Equals, http.StatusOK)
  502. c.Assert(err, check.IsNil)
  503. if err := json.Unmarshal(b, &top); err != nil {
  504. c.Fatal(err)
  505. }
  506. if len(top.Titles) != 11 {
  507. c.Fatalf("expected 11 titles, found %d: %v", len(top.Titles), top.Titles)
  508. }
  509. if top.Titles[0] != "USER" || top.Titles[10] != "COMMAND" {
  510. c.Fatalf("expected `USER` at `Titles[0]` and `COMMAND` at Titles[10]: %v", top.Titles)
  511. }
  512. if len(top.Processes) != 2 {
  513. c.Fatalf("expected 2 processes, found %d: %v", len(top.Processes), top.Processes)
  514. }
  515. if top.Processes[0][10] != "/bin/sh -c top" {
  516. c.Fatalf("expected `/bin/sh -c top`, found: %s", top.Processes[0][10])
  517. }
  518. if top.Processes[1][10] != "top" {
  519. c.Fatalf("expected `top`, found: %s", top.Processes[1][10])
  520. }
  521. }
  522. func (s *DockerSuite) TestContainerApiCommit(c *check.C) {
  523. cName := "testapicommit"
  524. out, err := exec.Command(dockerBinary, "run", "--name="+cName, "busybox", "/bin/sh", "-c", "touch /test").CombinedOutput()
  525. if err != nil {
  526. c.Fatal(err, out)
  527. }
  528. name := "TestContainerApiCommit"
  529. status, b, err := sockRequest("POST", "/commit?repo="+name+"&testtag=tag&container="+cName, nil)
  530. c.Assert(status, check.Equals, http.StatusCreated)
  531. c.Assert(err, check.IsNil)
  532. type resp struct {
  533. Id string
  534. }
  535. var img resp
  536. if err := json.Unmarshal(b, &img); err != nil {
  537. c.Fatal(err)
  538. }
  539. cmd, err := inspectField(img.Id, "Config.Cmd")
  540. if err != nil {
  541. c.Fatal(err)
  542. }
  543. if cmd != "{[/bin/sh -c touch /test]}" {
  544. c.Fatalf("got wrong Cmd from commit: %q", cmd)
  545. }
  546. // sanity check, make sure the image is what we think it is
  547. out, err = exec.Command(dockerBinary, "run", img.Id, "ls", "/test").CombinedOutput()
  548. if err != nil {
  549. c.Fatalf("error checking committed image: %v - %q", err, string(out))
  550. }
  551. }
  552. func (s *DockerSuite) TestContainerApiCreate(c *check.C) {
  553. config := map[string]interface{}{
  554. "Image": "busybox",
  555. "Cmd": []string{"/bin/sh", "-c", "touch /test && ls /test"},
  556. }
  557. status, b, err := sockRequest("POST", "/containers/create", config)
  558. c.Assert(status, check.Equals, http.StatusCreated)
  559. c.Assert(err, check.IsNil)
  560. type createResp struct {
  561. Id string
  562. }
  563. var container createResp
  564. if err := json.Unmarshal(b, &container); err != nil {
  565. c.Fatal(err)
  566. }
  567. out, err := exec.Command(dockerBinary, "start", "-a", container.Id).CombinedOutput()
  568. if err != nil {
  569. c.Fatal(out, err)
  570. }
  571. if strings.TrimSpace(string(out)) != "/test" {
  572. c.Fatalf("expected output `/test`, got %q", out)
  573. }
  574. }
  575. func (s *DockerSuite) TestContainerApiCreateWithHostName(c *check.C) {
  576. var hostName = "test-host"
  577. config := map[string]interface{}{
  578. "Image": "busybox",
  579. "Hostname": hostName,
  580. }
  581. _, b, err := sockRequest("POST", "/containers/create", config)
  582. if err != nil && !strings.Contains(err.Error(), "200 OK: 201") {
  583. c.Fatal(err)
  584. }
  585. type createResp struct {
  586. Id string
  587. }
  588. var container createResp
  589. if err := json.Unmarshal(b, &container); err != nil {
  590. c.Fatal(err)
  591. }
  592. var id = container.Id
  593. _, bodyGet, err := sockRequest("GET", "/containers/"+id+"/json", nil)
  594. type configLocal struct {
  595. Hostname string
  596. }
  597. type getResponse struct {
  598. Id string
  599. Config configLocal
  600. }
  601. var containerInfo getResponse
  602. if err := json.Unmarshal(bodyGet, &containerInfo); err != nil {
  603. c.Fatal(err)
  604. }
  605. var hostNameActual = containerInfo.Config.Hostname
  606. if hostNameActual != "test-host" {
  607. c.Fatalf("Mismatched Hostname, Expected %v, Actual: %v ", hostName, hostNameActual)
  608. }
  609. }
  610. func (s *DockerSuite) TestContainerApiVerifyHeader(c *check.C) {
  611. config := map[string]interface{}{
  612. "Image": "busybox",
  613. }
  614. create := func(ct string) (*http.Response, io.ReadCloser, error) {
  615. jsonData := bytes.NewBuffer(nil)
  616. if err := json.NewEncoder(jsonData).Encode(config); err != nil {
  617. c.Fatal(err)
  618. }
  619. return sockRequestRaw("POST", "/containers/create", jsonData, ct)
  620. }
  621. // Try with no content-type
  622. res, body, err := create("")
  623. c.Assert(err, check.IsNil)
  624. c.Assert(res.StatusCode, check.Equals, http.StatusInternalServerError)
  625. body.Close()
  626. // Try with wrong content-type
  627. res, body, err = create("application/xml")
  628. c.Assert(err, check.IsNil)
  629. c.Assert(res.StatusCode, check.Equals, http.StatusInternalServerError)
  630. body.Close()
  631. // now application/json
  632. res, body, err = create("application/json")
  633. c.Assert(err, check.IsNil)
  634. c.Assert(res.StatusCode, check.Equals, http.StatusCreated)
  635. body.Close()
  636. }
  637. // Issue 7941 - test to make sure a "null" in JSON is just ignored.
  638. // W/o this fix a null in JSON would be parsed into a string var as "null"
  639. func (s *DockerSuite) TestContainerApiPostCreateNull(c *check.C) {
  640. config := `{
  641. "Hostname":"",
  642. "Domainname":"",
  643. "Memory":0,
  644. "MemorySwap":0,
  645. "CpuShares":0,
  646. "Cpuset":null,
  647. "AttachStdin":true,
  648. "AttachStdout":true,
  649. "AttachStderr":true,
  650. "PortSpecs":null,
  651. "ExposedPorts":{},
  652. "Tty":true,
  653. "OpenStdin":true,
  654. "StdinOnce":true,
  655. "Env":[],
  656. "Cmd":"ls",
  657. "Image":"busybox",
  658. "Volumes":{},
  659. "WorkingDir":"",
  660. "Entrypoint":null,
  661. "NetworkDisabled":false,
  662. "OnBuild":null}`
  663. res, body, err := sockRequestRaw("POST", "/containers/create", strings.NewReader(config), "application/json")
  664. c.Assert(res.StatusCode, check.Equals, http.StatusCreated)
  665. c.Assert(err, check.IsNil)
  666. b, err := readBody(body)
  667. if err != nil {
  668. c.Fatal(err)
  669. }
  670. type createResp struct {
  671. Id string
  672. }
  673. var container createResp
  674. if err := json.Unmarshal(b, &container); err != nil {
  675. c.Fatal(err)
  676. }
  677. out, err := inspectField(container.Id, "HostConfig.CpusetCpus")
  678. if err != nil {
  679. c.Fatal(err, out)
  680. }
  681. if out != "" {
  682. c.Fatalf("expected empty string, got %q", out)
  683. }
  684. }
  685. func (s *DockerSuite) TestCreateWithTooLowMemoryLimit(c *check.C) {
  686. config := `{
  687. "Image": "busybox",
  688. "Cmd": "ls",
  689. "OpenStdin": true,
  690. "CpuShares": 100,
  691. "Memory": 524287
  692. }`
  693. res, body, _ := sockRequestRaw("POST", "/containers/create", strings.NewReader(config), "application/json")
  694. b, err2 := readBody(body)
  695. if err2 != nil {
  696. c.Fatal(err2)
  697. }
  698. c.Assert(res.StatusCode, check.Equals, http.StatusInternalServerError)
  699. c.Assert(strings.Contains(string(b), "Minimum memory limit allowed is 4MB"), check.Equals, true)
  700. }
  701. func (s *DockerSuite) TestStartWithTooLowMemoryLimit(c *check.C) {
  702. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "create", "busybox"))
  703. if err != nil {
  704. c.Fatal(err, out)
  705. }
  706. containerID := strings.TrimSpace(out)
  707. config := `{
  708. "CpuShares": 100,
  709. "Memory": 524287
  710. }`
  711. res, body, _ := sockRequestRaw("POST", "/containers/"+containerID+"/start", strings.NewReader(config), "application/json")
  712. b, err2 := readBody(body)
  713. if err2 != nil {
  714. c.Fatal(err2)
  715. }
  716. c.Assert(res.StatusCode, check.Equals, http.StatusInternalServerError)
  717. c.Assert(strings.Contains(string(b), "Minimum memory limit allowed is 4MB"), check.Equals, true)
  718. }
  719. func (s *DockerSuite) TestContainerApiRename(c *check.C) {
  720. runCmd := exec.Command(dockerBinary, "run", "--name", "TestContainerApiRename", "-d", "busybox", "sh")
  721. out, _, err := runCommandWithOutput(runCmd)
  722. c.Assert(err, check.IsNil)
  723. containerID := strings.TrimSpace(out)
  724. newName := "TestContainerApiRenameNew"
  725. statusCode, _, err := sockRequest("POST", "/containers/"+containerID+"/rename?name="+newName, nil)
  726. // 204 No Content is expected, not 200
  727. c.Assert(statusCode, check.Equals, http.StatusNoContent)
  728. c.Assert(err, check.IsNil)
  729. name, err := inspectField(containerID, "Name")
  730. if name != "/"+newName {
  731. c.Fatalf("Failed to rename container, expected %v, got %v. Container rename API failed", newName, name)
  732. }
  733. }
  734. func (s *DockerSuite) TestContainerApiKill(c *check.C) {
  735. name := "test-api-kill"
  736. runCmd := exec.Command(dockerBinary, "run", "-di", "--name", name, "busybox", "top")
  737. out, _, err := runCommandWithOutput(runCmd)
  738. if err != nil {
  739. c.Fatalf("Error on container creation: %v, output: %q", err, out)
  740. }
  741. status, _, err := sockRequest("POST", "/containers/"+name+"/kill", nil)
  742. c.Assert(status, check.Equals, http.StatusNoContent)
  743. c.Assert(err, check.IsNil)
  744. state, err := inspectField(name, "State.Running")
  745. if err != nil {
  746. c.Fatal(err)
  747. }
  748. if state != "false" {
  749. c.Fatalf("got wrong State from container %s: %q", name, state)
  750. }
  751. }
  752. func (s *DockerSuite) TestContainerApiRestart(c *check.C) {
  753. name := "test-api-restart"
  754. runCmd := exec.Command(dockerBinary, "run", "-di", "--name", name, "busybox", "top")
  755. out, _, err := runCommandWithOutput(runCmd)
  756. if err != nil {
  757. c.Fatalf("Error on container creation: %v, output: %q", err, out)
  758. }
  759. status, _, err := sockRequest("POST", "/containers/"+name+"/restart?t=1", nil)
  760. c.Assert(status, check.Equals, http.StatusNoContent)
  761. c.Assert(err, check.IsNil)
  762. if err := waitInspect(name, "{{ .State.Restarting }} {{ .State.Running }}", "false true", 5); err != nil {
  763. c.Fatal(err)
  764. }
  765. }
  766. func (s *DockerSuite) TestContainerApiRestartNotimeoutParam(c *check.C) {
  767. name := "test-api-restart-no-timeout-param"
  768. runCmd := exec.Command(dockerBinary, "run", "-di", "--name", name, "busybox", "top")
  769. out, _, err := runCommandWithOutput(runCmd)
  770. if err != nil {
  771. c.Fatalf("Error on container creation: %v, output: %q", err, out)
  772. }
  773. id := strings.TrimSpace(out)
  774. c.Assert(waitRun(id), check.IsNil)
  775. status, _, err := sockRequest("POST", "/containers/"+name+"/restart", nil)
  776. c.Assert(status, check.Equals, http.StatusNoContent)
  777. c.Assert(err, check.IsNil)
  778. if err := waitInspect(name, "{{ .State.Restarting }} {{ .State.Running }}", "false true", 5); err != nil {
  779. c.Fatal(err)
  780. }
  781. }
  782. func (s *DockerSuite) TestContainerApiStart(c *check.C) {
  783. name := "testing-start"
  784. config := map[string]interface{}{
  785. "Image": "busybox",
  786. "Cmd": []string{"/bin/sh", "-c", "/bin/top"},
  787. "OpenStdin": true,
  788. }
  789. status, _, err := sockRequest("POST", "/containers/create?name="+name, config)
  790. c.Assert(status, check.Equals, http.StatusCreated)
  791. c.Assert(err, check.IsNil)
  792. conf := make(map[string]interface{})
  793. status, _, err = sockRequest("POST", "/containers/"+name+"/start", conf)
  794. c.Assert(status, check.Equals, http.StatusNoContent)
  795. c.Assert(err, check.IsNil)
  796. // second call to start should give 304
  797. status, _, err = sockRequest("POST", "/containers/"+name+"/start", conf)
  798. c.Assert(status, check.Equals, http.StatusNotModified)
  799. c.Assert(err, check.IsNil)
  800. }
  801. func (s *DockerSuite) TestContainerApiStop(c *check.C) {
  802. name := "test-api-stop"
  803. runCmd := exec.Command(dockerBinary, "run", "-di", "--name", name, "busybox", "top")
  804. out, _, err := runCommandWithOutput(runCmd)
  805. if err != nil {
  806. c.Fatalf("Error on container creation: %v, output: %q", err, out)
  807. }
  808. status, _, err := sockRequest("POST", "/containers/"+name+"/stop?t=1", nil)
  809. c.Assert(status, check.Equals, http.StatusNoContent)
  810. c.Assert(err, check.IsNil)
  811. if err := waitInspect(name, "{{ .State.Running }}", "false", 5); err != nil {
  812. c.Fatal(err)
  813. }
  814. // second call to start should give 304
  815. status, _, err = sockRequest("POST", "/containers/"+name+"/stop?t=1", nil)
  816. c.Assert(status, check.Equals, http.StatusNotModified)
  817. c.Assert(err, check.IsNil)
  818. }
  819. func (s *DockerSuite) TestContainerApiWait(c *check.C) {
  820. name := "test-api-wait"
  821. runCmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "sleep", "5")
  822. out, _, err := runCommandWithOutput(runCmd)
  823. if err != nil {
  824. c.Fatalf("Error on container creation: %v, output: %q", err, out)
  825. }
  826. status, body, err := sockRequest("POST", "/containers/"+name+"/wait", nil)
  827. c.Assert(status, check.Equals, http.StatusOK)
  828. c.Assert(err, check.IsNil)
  829. if err := waitInspect(name, "{{ .State.Running }}", "false", 5); err != nil {
  830. c.Fatal(err)
  831. }
  832. var waitres types.ContainerWaitResponse
  833. if err := json.Unmarshal(body, &waitres); err != nil {
  834. c.Fatalf("unable to unmarshal response body: %v", err)
  835. }
  836. if waitres.StatusCode != 0 {
  837. c.Fatalf("Expected wait response StatusCode to be 0, got %d", waitres.StatusCode)
  838. }
  839. }
  840. func (s *DockerSuite) TestContainerApiCopy(c *check.C) {
  841. name := "test-container-api-copy"
  842. runCmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "touch", "/test.txt")
  843. _, err := runCommand(runCmd)
  844. c.Assert(err, check.IsNil)
  845. postData := types.CopyConfig{
  846. Resource: "/test.txt",
  847. }
  848. status, body, err := sockRequest("POST", "/containers/"+name+"/copy", postData)
  849. c.Assert(err, check.IsNil)
  850. c.Assert(status, check.Equals, http.StatusOK)
  851. found := false
  852. for tarReader := tar.NewReader(bytes.NewReader(body)); ; {
  853. h, err := tarReader.Next()
  854. if err != nil {
  855. if err == io.EOF {
  856. break
  857. }
  858. c.Fatal(err)
  859. }
  860. if h.Name == "test.txt" {
  861. found = true
  862. break
  863. }
  864. }
  865. c.Assert(found, check.Equals, true)
  866. }
  867. func (s *DockerSuite) TestContainerApiCopyResourcePathEmpty(c *check.C) {
  868. name := "test-container-api-copy-resource-empty"
  869. runCmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "touch", "/test.txt")
  870. _, err := runCommand(runCmd)
  871. c.Assert(err, check.IsNil)
  872. postData := types.CopyConfig{
  873. Resource: "",
  874. }
  875. status, body, err := sockRequest("POST", "/containers/"+name+"/copy", postData)
  876. c.Assert(err, check.IsNil)
  877. c.Assert(status, check.Equals, http.StatusInternalServerError)
  878. c.Assert(string(body), check.Matches, "Path cannot be empty\n")
  879. }
  880. func (s *DockerSuite) TestContainerApiCopyResourcePathNotFound(c *check.C) {
  881. name := "test-container-api-copy-resource-not-found"
  882. runCmd := exec.Command(dockerBinary, "run", "--name", name, "busybox")
  883. _, err := runCommand(runCmd)
  884. c.Assert(err, check.IsNil)
  885. postData := types.CopyConfig{
  886. Resource: "/notexist",
  887. }
  888. status, body, err := sockRequest("POST", "/containers/"+name+"/copy", postData)
  889. c.Assert(err, check.IsNil)
  890. c.Assert(status, check.Equals, http.StatusInternalServerError)
  891. c.Assert(string(body), check.Matches, "Could not find the file /notexist in container "+name+"\n")
  892. }
  893. func (s *DockerSuite) TestContainerApiCopyContainerNotFound(c *check.C) {
  894. postData := types.CopyConfig{
  895. Resource: "/something",
  896. }
  897. status, _, err := sockRequest("POST", "/containers/notexists/copy", postData)
  898. c.Assert(err, check.IsNil)
  899. c.Assert(status, check.Equals, http.StatusNotFound)
  900. }
  901. func (s *DockerSuite) TestContainerApiDelete(c *check.C) {
  902. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
  903. out, _, err := runCommandWithOutput(runCmd)
  904. c.Assert(err, check.IsNil)
  905. id := strings.TrimSpace(out)
  906. c.Assert(waitRun(id), check.IsNil)
  907. stopCmd := exec.Command(dockerBinary, "stop", id)
  908. _, err = runCommand(stopCmd)
  909. c.Assert(err, check.IsNil)
  910. status, _, err := sockRequest("DELETE", "/containers/"+id, nil)
  911. c.Assert(err, check.IsNil)
  912. c.Assert(status, check.Equals, http.StatusNoContent)
  913. }
  914. func (s *DockerSuite) TestContainerApiDeleteNotExist(c *check.C) {
  915. status, body, err := sockRequest("DELETE", "/containers/doesnotexist", nil)
  916. c.Assert(err, check.IsNil)
  917. c.Assert(status, check.Equals, http.StatusNotFound)
  918. c.Assert(string(body), check.Matches, "no such id: doesnotexist\n")
  919. }
  920. func (s *DockerSuite) TestContainerApiDeleteForce(c *check.C) {
  921. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
  922. out, _, err := runCommandWithOutput(runCmd)
  923. c.Assert(err, check.IsNil)
  924. id := strings.TrimSpace(out)
  925. c.Assert(waitRun(id), check.IsNil)
  926. status, _, err := sockRequest("DELETE", "/containers/"+id+"?force=1", nil)
  927. c.Assert(err, check.IsNil)
  928. c.Assert(status, check.Equals, http.StatusNoContent)
  929. }
  930. func (s *DockerSuite) TestContainerApiDeleteRemoveLinks(c *check.C) {
  931. runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "tlink1", "busybox", "top")
  932. out, _, err := runCommandWithOutput(runCmd)
  933. c.Assert(err, check.IsNil)
  934. id := strings.TrimSpace(out)
  935. c.Assert(waitRun(id), check.IsNil)
  936. runCmd = exec.Command(dockerBinary, "run", "--link", "tlink1:tlink1", "--name", "tlink2", "-d", "busybox", "top")
  937. out, _, err = runCommandWithOutput(runCmd)
  938. c.Assert(err, check.IsNil)
  939. id2 := strings.TrimSpace(out)
  940. c.Assert(waitRun(id2), check.IsNil)
  941. links, err := inspectFieldJSON(id2, "HostConfig.Links")
  942. c.Assert(err, check.IsNil)
  943. if links != "[\"/tlink1:/tlink2/tlink1\"]" {
  944. c.Fatal("expected to have links between containers")
  945. }
  946. status, _, err := sockRequest("DELETE", "/containers/tlink2/tlink1?link=1", nil)
  947. c.Assert(err, check.IsNil)
  948. c.Assert(status, check.Equals, http.StatusNoContent)
  949. linksPostRm, err := inspectFieldJSON(id2, "HostConfig.Links")
  950. c.Assert(err, check.IsNil)
  951. if linksPostRm != "null" {
  952. c.Fatal("call to api deleteContainer links should have removed the specified links")
  953. }
  954. }
  955. func (s *DockerSuite) TestContainerApiDeleteConflict(c *check.C) {
  956. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
  957. out, _, err := runCommandWithOutput(runCmd)
  958. c.Assert(err, check.IsNil)
  959. id := strings.TrimSpace(out)
  960. c.Assert(waitRun(id), check.IsNil)
  961. status, _, err := sockRequest("DELETE", "/containers/"+id, nil)
  962. c.Assert(status, check.Equals, http.StatusConflict)
  963. c.Assert(err, check.IsNil)
  964. }
  965. func (s *DockerSuite) TestContainerApiDeleteRemoveVolume(c *check.C) {
  966. testRequires(c, SameHostDaemon)
  967. runCmd := exec.Command(dockerBinary, "run", "-d", "-v", "/testvolume", "busybox", "top")
  968. out, _, err := runCommandWithOutput(runCmd)
  969. c.Assert(err, check.IsNil)
  970. id := strings.TrimSpace(out)
  971. c.Assert(waitRun(id), check.IsNil)
  972. vol, err := inspectFieldMap(id, "Volumes", "/testvolume")
  973. c.Assert(err, check.IsNil)
  974. _, err = os.Stat(vol)
  975. c.Assert(err, check.IsNil)
  976. status, _, err := sockRequest("DELETE", "/containers/"+id+"?v=1&force=1", nil)
  977. c.Assert(status, check.Equals, http.StatusNoContent)
  978. c.Assert(err, check.IsNil)
  979. if _, err := os.Stat(vol); !os.IsNotExist(err) {
  980. c.Fatalf("expected to get ErrNotExist error, got %v", err)
  981. }
  982. }
  983. // Regression test for https://github.com/docker/docker/issues/6231
  984. func (s *DockerSuite) TestContainersApiChunkedEncoding(c *check.C) {
  985. out, _ := dockerCmd(c, "create", "-v", "/foo", "busybox", "true")
  986. id := strings.TrimSpace(out)
  987. conn, err := sockConn(time.Duration(10 * time.Second))
  988. if err != nil {
  989. c.Fatal(err)
  990. }
  991. client := httputil.NewClientConn(conn, nil)
  992. defer client.Close()
  993. bindCfg := strings.NewReader(`{"Binds": ["/tmp:/foo"]}`)
  994. req, err := http.NewRequest("POST", "/containers/"+id+"/start", bindCfg)
  995. if err != nil {
  996. c.Fatal(err)
  997. }
  998. req.Header.Set("Content-Type", "application/json")
  999. // This is a cheat to make the http request do chunked encoding
  1000. // Otherwise (just setting the Content-Encoding to chunked) net/http will overwrite
  1001. // https://golang.org/src/pkg/net/http/request.go?s=11980:12172
  1002. req.ContentLength = -1
  1003. resp, err := client.Do(req)
  1004. if err != nil {
  1005. c.Fatalf("error starting container with chunked encoding: %v", err)
  1006. }
  1007. resp.Body.Close()
  1008. if resp.StatusCode != 204 {
  1009. c.Fatalf("expected status code 204, got %d", resp.StatusCode)
  1010. }
  1011. out, err = inspectFieldJSON(id, "HostConfig.Binds")
  1012. if err != nil {
  1013. c.Fatal(err)
  1014. }
  1015. var binds []string
  1016. if err := json.NewDecoder(strings.NewReader(out)).Decode(&binds); err != nil {
  1017. c.Fatal(err)
  1018. }
  1019. if len(binds) != 1 {
  1020. c.Fatalf("got unexpected binds: %v", binds)
  1021. }
  1022. expected := "/tmp:/foo"
  1023. if binds[0] != expected {
  1024. c.Fatalf("got incorrect bind spec, wanted %s, got: %s", expected, binds[0])
  1025. }
  1026. }