docker_api_containers_test.go 38 KB

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