docker_api_containers_test.go 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621
  1. package main
  2. import (
  3. "archive/tar"
  4. "bytes"
  5. "encoding/json"
  6. "fmt"
  7. "io"
  8. "net/http"
  9. "net/http/httputil"
  10. "net/url"
  11. "os"
  12. "regexp"
  13. "strconv"
  14. "strings"
  15. "time"
  16. "github.com/docker/docker/pkg/integration"
  17. "github.com/docker/docker/pkg/integration/checker"
  18. "github.com/docker/docker/pkg/stringid"
  19. "github.com/docker/engine-api/types"
  20. containertypes "github.com/docker/engine-api/types/container"
  21. networktypes "github.com/docker/engine-api/types/network"
  22. "github.com/go-check/check"
  23. )
  24. func init() {
  25. if daemonPlatform == "windows" {
  26. sleepCmd = "sleep"
  27. }
  28. }
  29. func (s *DockerSuite) TestContainerApiGetAll(c *check.C) {
  30. startCount, err := getContainerCount()
  31. c.Assert(err, checker.IsNil, check.Commentf("Cannot query container count"))
  32. name := "getall"
  33. dockerCmd(c, "run", "--name", name, "busybox", "true")
  34. status, body, err := sockRequest("GET", "/containers/json?all=1", nil)
  35. c.Assert(err, checker.IsNil)
  36. c.Assert(status, checker.Equals, http.StatusOK)
  37. var inspectJSON []struct {
  38. Names []string
  39. }
  40. err = json.Unmarshal(body, &inspectJSON)
  41. c.Assert(err, checker.IsNil, check.Commentf("unable to unmarshal response body"))
  42. c.Assert(inspectJSON, checker.HasLen, startCount+1)
  43. actual := inspectJSON[0].Names[0]
  44. c.Assert(actual, checker.Equals, "/"+name)
  45. }
  46. // regression test for empty json field being omitted #13691
  47. func (s *DockerSuite) TestContainerApiGetJSONNoFieldsOmitted(c *check.C) {
  48. dockerCmd(c, "run", "busybox", "true")
  49. status, body, err := sockRequest("GET", "/containers/json?all=1", nil)
  50. c.Assert(err, checker.IsNil)
  51. c.Assert(status, checker.Equals, http.StatusOK)
  52. // empty Labels field triggered this bug, make sense to check for everything
  53. // cause even Ports for instance can trigger this bug
  54. // better safe than sorry..
  55. fields := []string{
  56. "Id",
  57. "Names",
  58. "Image",
  59. "Command",
  60. "Created",
  61. "Ports",
  62. "Labels",
  63. "Status",
  64. "NetworkSettings",
  65. }
  66. // decoding into types.Container do not work since it eventually unmarshal
  67. // and empty field to an empty go map, so we just check for a string
  68. for _, f := range fields {
  69. if !strings.Contains(string(body), f) {
  70. c.Fatalf("Field %s is missing and it shouldn't", f)
  71. }
  72. }
  73. }
  74. type containerPs struct {
  75. Names []string
  76. Ports []map[string]interface{}
  77. }
  78. // regression test for non-empty fields from #13901
  79. func (s *DockerSuite) TestContainerApiPsOmitFields(c *check.C) {
  80. // Problematic for Windows porting due to networking not yet being passed back
  81. testRequires(c, DaemonIsLinux)
  82. name := "pstest"
  83. port := 80
  84. dockerCmd(c, "run", "-d", "--name", name, "--expose", strconv.Itoa(port), "busybox", sleepCmd, "60")
  85. status, body, err := sockRequest("GET", "/containers/json?all=1", nil)
  86. c.Assert(err, checker.IsNil)
  87. c.Assert(status, checker.Equals, http.StatusOK)
  88. var resp []containerPs
  89. err = json.Unmarshal(body, &resp)
  90. c.Assert(err, checker.IsNil)
  91. var foundContainer *containerPs
  92. for _, container := range resp {
  93. for _, testName := range container.Names {
  94. if "/"+name == testName {
  95. foundContainer = &container
  96. break
  97. }
  98. }
  99. }
  100. c.Assert(foundContainer.Ports, checker.HasLen, 1)
  101. c.Assert(foundContainer.Ports[0]["PrivatePort"], checker.Equals, float64(port))
  102. _, ok := foundContainer.Ports[0]["PublicPort"]
  103. c.Assert(ok, checker.Not(checker.Equals), true)
  104. _, ok = foundContainer.Ports[0]["IP"]
  105. c.Assert(ok, checker.Not(checker.Equals), true)
  106. }
  107. func (s *DockerSuite) TestContainerApiGetExport(c *check.C) {
  108. // TODO: Investigate why this fails on Windows to Windows CI
  109. testRequires(c, DaemonIsLinux)
  110. name := "exportcontainer"
  111. dockerCmd(c, "run", "--name", name, "busybox", "touch", "/test")
  112. status, body, err := sockRequest("GET", "/containers/"+name+"/export", nil)
  113. c.Assert(err, checker.IsNil)
  114. c.Assert(status, checker.Equals, http.StatusOK)
  115. found := false
  116. for tarReader := tar.NewReader(bytes.NewReader(body)); ; {
  117. h, err := tarReader.Next()
  118. if err != nil && err == io.EOF {
  119. break
  120. }
  121. if h.Name == "test" {
  122. found = true
  123. break
  124. }
  125. }
  126. c.Assert(found, checker.True, check.Commentf("The created test file has not been found in the exported image"))
  127. }
  128. func (s *DockerSuite) TestContainerApiGetChanges(c *check.C) {
  129. // Not supported on Windows as Windows does not support docker diff (/containers/name/changes)
  130. testRequires(c, DaemonIsLinux)
  131. name := "changescontainer"
  132. dockerCmd(c, "run", "--name", name, "busybox", "rm", "/etc/passwd")
  133. status, body, err := sockRequest("GET", "/containers/"+name+"/changes", nil)
  134. c.Assert(err, checker.IsNil)
  135. c.Assert(status, checker.Equals, http.StatusOK)
  136. changes := []struct {
  137. Kind int
  138. Path string
  139. }{}
  140. c.Assert(json.Unmarshal(body, &changes), checker.IsNil, check.Commentf("unable to unmarshal response body"))
  141. // Check the changelog for removal of /etc/passwd
  142. success := false
  143. for _, elem := range changes {
  144. if elem.Path == "/etc/passwd" && elem.Kind == 2 {
  145. success = true
  146. }
  147. }
  148. c.Assert(success, checker.True, check.Commentf("/etc/passwd has been removed but is not present in the diff"))
  149. }
  150. func (s *DockerSuite) TestContainerApiStartVolumeBinds(c *check.C) {
  151. // TODO Windows CI: Investigate further why this fails on Windows to Windows CI.
  152. testRequires(c, DaemonIsLinux)
  153. path := "/foo"
  154. if daemonPlatform == "windows" {
  155. path = `c:\foo`
  156. }
  157. name := "testing"
  158. config := map[string]interface{}{
  159. "Image": "busybox",
  160. "Volumes": map[string]struct{}{path: {}},
  161. }
  162. status, _, err := sockRequest("POST", "/containers/create?name="+name, config)
  163. c.Assert(err, checker.IsNil)
  164. c.Assert(status, checker.Equals, http.StatusCreated)
  165. bindPath := randomTmpDirPath("test", daemonPlatform)
  166. config = map[string]interface{}{
  167. "Binds": []string{bindPath + ":" + path},
  168. }
  169. status, _, err = sockRequest("POST", "/containers/"+name+"/start", config)
  170. c.Assert(err, checker.IsNil)
  171. c.Assert(status, checker.Equals, http.StatusNoContent)
  172. pth, err := inspectMountSourceField(name, path)
  173. c.Assert(err, checker.IsNil)
  174. c.Assert(pth, checker.Equals, bindPath, check.Commentf("expected volume host path to be %s, got %s", bindPath, pth))
  175. }
  176. // Test for GH#10618
  177. func (s *DockerSuite) TestContainerApiStartDupVolumeBinds(c *check.C) {
  178. // TODO Windows to Windows CI - Port this
  179. testRequires(c, DaemonIsLinux)
  180. name := "testdups"
  181. config := map[string]interface{}{
  182. "Image": "busybox",
  183. "Volumes": map[string]struct{}{"/tmp": {}},
  184. }
  185. status, _, err := sockRequest("POST", "/containers/create?name="+name, config)
  186. c.Assert(err, checker.IsNil)
  187. c.Assert(status, checker.Equals, http.StatusCreated)
  188. bindPath1 := randomTmpDirPath("test1", daemonPlatform)
  189. bindPath2 := randomTmpDirPath("test2", daemonPlatform)
  190. config = map[string]interface{}{
  191. "Binds": []string{bindPath1 + ":/tmp", bindPath2 + ":/tmp"},
  192. }
  193. status, body, err := sockRequest("POST", "/containers/"+name+"/start", config)
  194. c.Assert(err, checker.IsNil)
  195. c.Assert(status, checker.Equals, http.StatusInternalServerError)
  196. c.Assert(string(body), checker.Contains, "Duplicate mount point", check.Commentf("Expected failure due to duplicate bind mounts to same path, instead got: %q with error: %v", string(body), err))
  197. }
  198. func (s *DockerSuite) TestContainerApiStartVolumesFrom(c *check.C) {
  199. // TODO Windows to Windows CI - Port this
  200. testRequires(c, DaemonIsLinux)
  201. volName := "voltst"
  202. volPath := "/tmp"
  203. dockerCmd(c, "run", "-d", "--name", volName, "-v", volPath, "busybox")
  204. name := "TestContainerApiStartVolumesFrom"
  205. config := map[string]interface{}{
  206. "Image": "busybox",
  207. "Volumes": map[string]struct{}{volPath: {}},
  208. }
  209. status, _, err := sockRequest("POST", "/containers/create?name="+name, config)
  210. c.Assert(err, checker.IsNil)
  211. c.Assert(status, checker.Equals, http.StatusCreated)
  212. config = map[string]interface{}{
  213. "VolumesFrom": []string{volName},
  214. }
  215. status, _, err = sockRequest("POST", "/containers/"+name+"/start", config)
  216. c.Assert(err, checker.IsNil)
  217. c.Assert(status, checker.Equals, http.StatusNoContent)
  218. pth, err := inspectMountSourceField(name, volPath)
  219. c.Assert(err, checker.IsNil)
  220. pth2, err := inspectMountSourceField(volName, volPath)
  221. c.Assert(err, checker.IsNil)
  222. c.Assert(pth, checker.Equals, pth2, check.Commentf("expected volume host path to be %s, got %s", pth, pth2))
  223. }
  224. func (s *DockerSuite) TestGetContainerStats(c *check.C) {
  225. // Problematic on Windows as Windows does not support stats
  226. testRequires(c, DaemonIsLinux)
  227. var (
  228. name = "statscontainer"
  229. )
  230. dockerCmd(c, "run", "-d", "--name", name, "busybox", "top")
  231. type b struct {
  232. status int
  233. body []byte
  234. err error
  235. }
  236. bc := make(chan b, 1)
  237. go func() {
  238. status, body, err := sockRequest("GET", "/containers/"+name+"/stats", nil)
  239. bc <- b{status, body, err}
  240. }()
  241. // allow some time to stream the stats from the container
  242. time.Sleep(4 * time.Second)
  243. dockerCmd(c, "rm", "-f", name)
  244. // collect the results from the stats stream or timeout and fail
  245. // if the stream was not disconnected.
  246. select {
  247. case <-time.After(2 * time.Second):
  248. c.Fatal("stream was not closed after container was removed")
  249. case sr := <-bc:
  250. c.Assert(sr.err, checker.IsNil)
  251. c.Assert(sr.status, checker.Equals, http.StatusOK)
  252. dec := json.NewDecoder(bytes.NewBuffer(sr.body))
  253. var s *types.Stats
  254. // decode only one object from the stream
  255. c.Assert(dec.Decode(&s), checker.IsNil)
  256. }
  257. }
  258. func (s *DockerSuite) TestGetContainerStatsRmRunning(c *check.C) {
  259. // Problematic on Windows as Windows does not support stats
  260. testRequires(c, DaemonIsLinux)
  261. out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
  262. id := strings.TrimSpace(out)
  263. buf := &integration.ChannelBuffer{make(chan []byte, 1)}
  264. defer buf.Close()
  265. chErr := make(chan error)
  266. go func() {
  267. _, body, err := sockRequestRaw("GET", "/containers/"+id+"/stats?stream=1", nil, "application/json")
  268. if err != nil {
  269. chErr <- err
  270. }
  271. defer body.Close()
  272. _, err = io.Copy(buf, body)
  273. chErr <- err
  274. }()
  275. defer func() {
  276. c.Assert(<-chErr, checker.IsNil)
  277. }()
  278. b := make([]byte, 32)
  279. // make sure we've got some stats
  280. _, err := buf.ReadTimeout(b, 2*time.Second)
  281. c.Assert(err, checker.IsNil)
  282. // Now remove without `-f` and make sure we are still pulling stats
  283. _, _, err = dockerCmdWithError("rm", id)
  284. c.Assert(err, checker.Not(checker.IsNil), check.Commentf("rm should have failed but didn't"))
  285. _, err = buf.ReadTimeout(b, 2*time.Second)
  286. c.Assert(err, checker.IsNil)
  287. dockerCmd(c, "rm", "-f", id)
  288. _, err = buf.ReadTimeout(b, 2*time.Second)
  289. c.Assert(err, checker.Not(checker.IsNil))
  290. }
  291. // regression test for gh13421
  292. // previous test was just checking one stat entry so it didn't fail (stats with
  293. // stream false always return one stat)
  294. func (s *DockerSuite) TestGetContainerStatsStream(c *check.C) {
  295. // Problematic on Windows as Windows does not support stats
  296. testRequires(c, DaemonIsLinux)
  297. name := "statscontainer"
  298. dockerCmd(c, "run", "-d", "--name", name, "busybox", "top")
  299. type b struct {
  300. status int
  301. body []byte
  302. err error
  303. }
  304. bc := make(chan b, 1)
  305. go func() {
  306. status, body, err := sockRequest("GET", "/containers/"+name+"/stats", nil)
  307. bc <- b{status, body, err}
  308. }()
  309. // allow some time to stream the stats from the container
  310. time.Sleep(4 * time.Second)
  311. dockerCmd(c, "rm", "-f", name)
  312. // collect the results from the stats stream or timeout and fail
  313. // if the stream was not disconnected.
  314. select {
  315. case <-time.After(2 * time.Second):
  316. c.Fatal("stream was not closed after container was removed")
  317. case sr := <-bc:
  318. c.Assert(sr.err, checker.IsNil)
  319. c.Assert(sr.status, checker.Equals, http.StatusOK)
  320. s := string(sr.body)
  321. // count occurrences of "read" of types.Stats
  322. if l := strings.Count(s, "read"); l < 2 {
  323. c.Fatalf("Expected more than one stat streamed, got %d", l)
  324. }
  325. }
  326. }
  327. func (s *DockerSuite) TestGetContainerStatsNoStream(c *check.C) {
  328. // Problematic on Windows as Windows does not support stats
  329. testRequires(c, DaemonIsLinux)
  330. name := "statscontainer"
  331. dockerCmd(c, "run", "-d", "--name", name, "busybox", "top")
  332. type b struct {
  333. status int
  334. body []byte
  335. err error
  336. }
  337. bc := make(chan b, 1)
  338. go func() {
  339. status, body, err := sockRequest("GET", "/containers/"+name+"/stats?stream=0", nil)
  340. bc <- b{status, body, err}
  341. }()
  342. // allow some time to stream the stats from the container
  343. time.Sleep(4 * time.Second)
  344. dockerCmd(c, "rm", "-f", name)
  345. // collect the results from the stats stream or timeout and fail
  346. // if the stream was not disconnected.
  347. select {
  348. case <-time.After(2 * time.Second):
  349. c.Fatal("stream was not closed after container was removed")
  350. case sr := <-bc:
  351. c.Assert(sr.err, checker.IsNil)
  352. c.Assert(sr.status, checker.Equals, http.StatusOK)
  353. s := string(sr.body)
  354. // count occurrences of "read" of types.Stats
  355. c.Assert(strings.Count(s, "read"), checker.Equals, 1, check.Commentf("Expected only one stat streamed, got %d", strings.Count(s, "read")))
  356. }
  357. }
  358. func (s *DockerSuite) TestGetStoppedContainerStats(c *check.C) {
  359. // Problematic on Windows as Windows does not support stats
  360. testRequires(c, DaemonIsLinux)
  361. // TODO: this test does nothing because we are c.Assert'ing in goroutine
  362. var (
  363. name = "statscontainer"
  364. )
  365. dockerCmd(c, "create", "--name", name, "busybox", "top")
  366. go func() {
  367. // We'll never get return for GET stats from sockRequest as of now,
  368. // just send request and see if panic or error would happen on daemon side.
  369. status, _, err := sockRequest("GET", "/containers/"+name+"/stats", nil)
  370. c.Assert(err, checker.IsNil)
  371. c.Assert(status, checker.Equals, http.StatusOK)
  372. }()
  373. // allow some time to send request and let daemon deal with it
  374. time.Sleep(1 * time.Second)
  375. }
  376. // #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
  377. func (s *DockerSuite) TestPostContainerBindNormalVolume(c *check.C) {
  378. // TODO Windows to Windows CI - Port this
  379. testRequires(c, DaemonIsLinux)
  380. dockerCmd(c, "create", "-v", "/foo", "--name=one", "busybox")
  381. fooDir, err := inspectMountSourceField("one", "/foo")
  382. c.Assert(err, checker.IsNil)
  383. dockerCmd(c, "create", "-v", "/foo", "--name=two", "busybox")
  384. bindSpec := map[string][]string{"Binds": {fooDir + ":/foo"}}
  385. status, _, err := sockRequest("POST", "/containers/two/start", bindSpec)
  386. c.Assert(err, checker.IsNil)
  387. c.Assert(status, checker.Equals, http.StatusNoContent)
  388. fooDir2, err := inspectMountSourceField("two", "/foo")
  389. c.Assert(err, checker.IsNil)
  390. c.Assert(fooDir2, checker.Equals, fooDir, check.Commentf("expected volume path to be %s, got: %s", fooDir, fooDir2))
  391. }
  392. func (s *DockerSuite) TestContainerApiPause(c *check.C) {
  393. // Problematic on Windows as Windows does not support pause
  394. testRequires(c, DaemonIsLinux)
  395. defer unpauseAllContainers()
  396. out, _ := dockerCmd(c, "run", "-d", "busybox", "sleep", "30")
  397. ContainerID := strings.TrimSpace(out)
  398. status, _, err := sockRequest("POST", "/containers/"+ContainerID+"/pause", nil)
  399. c.Assert(err, checker.IsNil)
  400. c.Assert(status, checker.Equals, http.StatusNoContent)
  401. pausedContainers, err := getSliceOfPausedContainers()
  402. c.Assert(err, checker.IsNil, check.Commentf("error thrown while checking if containers were paused"))
  403. if len(pausedContainers) != 1 || stringid.TruncateID(ContainerID) != pausedContainers[0] {
  404. c.Fatalf("there should be one paused container and not %d", len(pausedContainers))
  405. }
  406. status, _, err = sockRequest("POST", "/containers/"+ContainerID+"/unpause", nil)
  407. c.Assert(err, checker.IsNil)
  408. c.Assert(status, checker.Equals, http.StatusNoContent)
  409. pausedContainers, err = getSliceOfPausedContainers()
  410. c.Assert(err, checker.IsNil, check.Commentf("error thrown while checking if containers were paused"))
  411. c.Assert(pausedContainers, checker.IsNil, check.Commentf("There should be no paused container."))
  412. }
  413. func (s *DockerSuite) TestContainerApiTop(c *check.C) {
  414. // Problematic on Windows as Windows does not support top
  415. testRequires(c, DaemonIsLinux)
  416. out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "top")
  417. id := strings.TrimSpace(string(out))
  418. c.Assert(waitRun(id), checker.IsNil)
  419. type topResp struct {
  420. Titles []string
  421. Processes [][]string
  422. }
  423. var top topResp
  424. status, b, err := sockRequest("GET", "/containers/"+id+"/top?ps_args=aux", nil)
  425. c.Assert(err, checker.IsNil)
  426. c.Assert(status, checker.Equals, http.StatusOK)
  427. c.Assert(json.Unmarshal(b, &top), checker.IsNil)
  428. c.Assert(top.Titles, checker.HasLen, 11, check.Commentf("expected 11 titles, found %d: %v", len(top.Titles), top.Titles))
  429. if top.Titles[0] != "USER" || top.Titles[10] != "COMMAND" {
  430. c.Fatalf("expected `USER` at `Titles[0]` and `COMMAND` at Titles[10]: %v", top.Titles)
  431. }
  432. c.Assert(top.Processes, checker.HasLen, 2, check.Commentf("expected 2 processes, found %d: %v", len(top.Processes), top.Processes))
  433. c.Assert(top.Processes[0][10], checker.Equals, "/bin/sh -c top")
  434. c.Assert(top.Processes[1][10], checker.Equals, "top")
  435. }
  436. func (s *DockerSuite) TestContainerApiCommit(c *check.C) {
  437. cName := "testapicommit"
  438. dockerCmd(c, "run", "--name="+cName, "busybox", "/bin/sh", "-c", "touch /test")
  439. name := "testcontainerapicommit"
  440. status, b, err := sockRequest("POST", "/commit?repo="+name+"&testtag=tag&container="+cName, nil)
  441. c.Assert(err, checker.IsNil)
  442. c.Assert(status, checker.Equals, http.StatusCreated)
  443. type resp struct {
  444. ID string
  445. }
  446. var img resp
  447. c.Assert(json.Unmarshal(b, &img), checker.IsNil)
  448. cmd, err := inspectField(img.ID, "Config.Cmd")
  449. c.Assert(err, checker.IsNil)
  450. c.Assert(cmd, checker.Equals, "{[/bin/sh -c touch /test]}", check.Commentf("got wrong Cmd from commit: %q", cmd))
  451. // sanity check, make sure the image is what we think it is
  452. dockerCmd(c, "run", img.ID, "ls", "/test")
  453. }
  454. func (s *DockerSuite) TestContainerApiCommitWithLabelInConfig(c *check.C) {
  455. cName := "testapicommitwithconfig"
  456. dockerCmd(c, "run", "--name="+cName, "busybox", "/bin/sh", "-c", "touch /test")
  457. config := map[string]interface{}{
  458. "Labels": map[string]string{"key1": "value1", "key2": "value2"},
  459. }
  460. name := "testcontainerapicommitwithconfig"
  461. status, b, err := sockRequest("POST", "/commit?repo="+name+"&container="+cName, config)
  462. c.Assert(err, checker.IsNil)
  463. c.Assert(status, checker.Equals, http.StatusCreated)
  464. type resp struct {
  465. ID string
  466. }
  467. var img resp
  468. c.Assert(json.Unmarshal(b, &img), checker.IsNil)
  469. label1, err := inspectFieldMap(img.ID, "Config.Labels", "key1")
  470. c.Assert(err, checker.IsNil)
  471. c.Assert(label1, checker.Equals, "value1")
  472. label2, err := inspectFieldMap(img.ID, "Config.Labels", "key2")
  473. c.Assert(err, checker.IsNil)
  474. c.Assert(label2, checker.Equals, "value2")
  475. cmd, err := inspectField(img.ID, "Config.Cmd")
  476. c.Assert(err, checker.IsNil)
  477. c.Assert(cmd, checker.Equals, "{[/bin/sh -c touch /test]}", check.Commentf("got wrong Cmd from commit: %q", cmd))
  478. // sanity check, make sure the image is what we think it is
  479. dockerCmd(c, "run", img.ID, "ls", "/test")
  480. }
  481. func (s *DockerSuite) TestContainerApiBadPort(c *check.C) {
  482. // TODO Windows to Windows CI - Port this test
  483. testRequires(c, DaemonIsLinux)
  484. config := map[string]interface{}{
  485. "Image": "busybox",
  486. "Cmd": []string{"/bin/sh", "-c", "echo test"},
  487. "PortBindings": map[string]interface{}{
  488. "8080/tcp": []map[string]interface{}{
  489. {
  490. "HostIP": "",
  491. "HostPort": "aa80",
  492. },
  493. },
  494. },
  495. }
  496. jsonData := bytes.NewBuffer(nil)
  497. json.NewEncoder(jsonData).Encode(config)
  498. status, b, err := sockRequest("POST", "/containers/create", config)
  499. c.Assert(err, checker.IsNil)
  500. c.Assert(status, checker.Equals, http.StatusInternalServerError)
  501. c.Assert(strings.TrimSpace(string(b)), checker.Equals, `Invalid port specification: "aa80"`, check.Commentf("Incorrect error msg: %s", string(b)))
  502. }
  503. func (s *DockerSuite) TestContainerApiCreate(c *check.C) {
  504. config := map[string]interface{}{
  505. "Image": "busybox",
  506. "Cmd": []string{"/bin/sh", "-c", "touch /test && ls /test"},
  507. }
  508. status, b, err := sockRequest("POST", "/containers/create", config)
  509. c.Assert(err, checker.IsNil)
  510. c.Assert(status, checker.Equals, http.StatusCreated)
  511. type createResp struct {
  512. ID string
  513. }
  514. var container createResp
  515. c.Assert(json.Unmarshal(b, &container), checker.IsNil)
  516. out, _ := dockerCmd(c, "start", "-a", container.ID)
  517. c.Assert(strings.TrimSpace(out), checker.Equals, "/test")
  518. }
  519. func (s *DockerSuite) TestContainerApiCreateEmptyConfig(c *check.C) {
  520. config := map[string]interface{}{}
  521. status, b, err := sockRequest("POST", "/containers/create", config)
  522. c.Assert(err, checker.IsNil)
  523. c.Assert(status, checker.Equals, http.StatusInternalServerError)
  524. expected := "Config cannot be empty in order to create a container\n"
  525. c.Assert(string(b), checker.Equals, expected)
  526. }
  527. func (s *DockerSuite) TestContainerApiCreateMultipleNetworksConfig(c *check.C) {
  528. // Container creation must fail if client specified configurations for more than one network
  529. config := map[string]interface{}{
  530. "Image": "busybox",
  531. "NetworkingConfig": networktypes.NetworkingConfig{
  532. EndpointsConfig: map[string]*networktypes.EndpointSettings{
  533. "net1": {},
  534. "net2": {},
  535. "net3": {},
  536. },
  537. },
  538. }
  539. status, b, err := sockRequest("POST", "/containers/create", config)
  540. c.Assert(err, checker.IsNil)
  541. c.Assert(status, checker.Equals, http.StatusBadRequest)
  542. // network name order in error message is not deterministic
  543. c.Assert(string(b), checker.Contains, "Container cannot be connected to [")
  544. c.Assert(string(b), checker.Contains, "net1")
  545. c.Assert(string(b), checker.Contains, "net2")
  546. c.Assert(string(b), checker.Contains, "net3")
  547. }
  548. func (s *DockerSuite) TestContainerApiCreateWithHostName(c *check.C) {
  549. // TODO Windows: Port this test once hostname is supported
  550. testRequires(c, DaemonIsLinux)
  551. hostName := "test-host"
  552. config := map[string]interface{}{
  553. "Image": "busybox",
  554. "Hostname": hostName,
  555. }
  556. status, body, err := sockRequest("POST", "/containers/create", config)
  557. c.Assert(err, checker.IsNil)
  558. c.Assert(status, checker.Equals, http.StatusCreated)
  559. var container types.ContainerCreateResponse
  560. c.Assert(json.Unmarshal(body, &container), checker.IsNil)
  561. status, body, err = sockRequest("GET", "/containers/"+container.ID+"/json", nil)
  562. c.Assert(err, checker.IsNil)
  563. c.Assert(status, checker.Equals, http.StatusOK)
  564. var containerJSON types.ContainerJSON
  565. c.Assert(json.Unmarshal(body, &containerJSON), checker.IsNil)
  566. c.Assert(containerJSON.Config.Hostname, checker.Equals, hostName, check.Commentf("Mismatched Hostname"))
  567. }
  568. func (s *DockerSuite) TestContainerApiCreateWithDomainName(c *check.C) {
  569. // TODO Windows: Port this test once domain name is supported
  570. testRequires(c, DaemonIsLinux)
  571. domainName := "test-domain"
  572. config := map[string]interface{}{
  573. "Image": "busybox",
  574. "Domainname": domainName,
  575. }
  576. status, body, err := sockRequest("POST", "/containers/create", config)
  577. c.Assert(err, checker.IsNil)
  578. c.Assert(status, checker.Equals, http.StatusCreated)
  579. var container types.ContainerCreateResponse
  580. c.Assert(json.Unmarshal(body, &container), checker.IsNil)
  581. status, body, err = sockRequest("GET", "/containers/"+container.ID+"/json", nil)
  582. c.Assert(err, checker.IsNil)
  583. c.Assert(status, checker.Equals, http.StatusOK)
  584. var containerJSON types.ContainerJSON
  585. c.Assert(json.Unmarshal(body, &containerJSON), checker.IsNil)
  586. c.Assert(containerJSON.Config.Domainname, checker.Equals, domainName, check.Commentf("Mismatched Domainname"))
  587. }
  588. func (s *DockerSuite) TestContainerApiCreateBridgeNetworkMode(c *check.C) {
  589. // Windows does not support bridge
  590. testRequires(c, DaemonIsLinux)
  591. UtilCreateNetworkMode(c, "bridge")
  592. }
  593. func (s *DockerSuite) TestContainerApiCreateOtherNetworkModes(c *check.C) {
  594. // Windows does not support these network modes
  595. testRequires(c, DaemonIsLinux, NotUserNamespace)
  596. UtilCreateNetworkMode(c, "host")
  597. UtilCreateNetworkMode(c, "container:web1")
  598. }
  599. func UtilCreateNetworkMode(c *check.C, networkMode string) {
  600. config := map[string]interface{}{
  601. "Image": "busybox",
  602. "HostConfig": map[string]interface{}{"NetworkMode": networkMode},
  603. }
  604. status, body, err := sockRequest("POST", "/containers/create", config)
  605. c.Assert(err, checker.IsNil)
  606. c.Assert(status, checker.Equals, http.StatusCreated)
  607. var container types.ContainerCreateResponse
  608. c.Assert(json.Unmarshal(body, &container), checker.IsNil)
  609. status, body, err = sockRequest("GET", "/containers/"+container.ID+"/json", nil)
  610. c.Assert(err, checker.IsNil)
  611. c.Assert(status, checker.Equals, http.StatusOK)
  612. var containerJSON types.ContainerJSON
  613. c.Assert(json.Unmarshal(body, &containerJSON), checker.IsNil)
  614. c.Assert(containerJSON.HostConfig.NetworkMode, checker.Equals, containertypes.NetworkMode(networkMode), check.Commentf("Mismatched NetworkMode"))
  615. }
  616. func (s *DockerSuite) TestContainerApiCreateWithCpuSharesCpuset(c *check.C) {
  617. // TODO Windows to Windows CI. The CpuShares part could be ported.
  618. testRequires(c, DaemonIsLinux)
  619. config := map[string]interface{}{
  620. "Image": "busybox",
  621. "CpuShares": 512,
  622. "CpusetCpus": "0",
  623. }
  624. status, body, err := sockRequest("POST", "/containers/create", config)
  625. c.Assert(err, checker.IsNil)
  626. c.Assert(status, checker.Equals, http.StatusCreated)
  627. var container types.ContainerCreateResponse
  628. c.Assert(json.Unmarshal(body, &container), checker.IsNil)
  629. status, body, err = sockRequest("GET", "/containers/"+container.ID+"/json", nil)
  630. c.Assert(err, checker.IsNil)
  631. c.Assert(status, checker.Equals, http.StatusOK)
  632. var containerJSON types.ContainerJSON
  633. c.Assert(json.Unmarshal(body, &containerJSON), checker.IsNil)
  634. out, err := inspectField(containerJSON.ID, "HostConfig.CpuShares")
  635. c.Assert(err, checker.IsNil)
  636. c.Assert(out, checker.Equals, "512")
  637. outCpuset, errCpuset := inspectField(containerJSON.ID, "HostConfig.CpusetCpus")
  638. c.Assert(errCpuset, checker.IsNil, check.Commentf("Output: %s", outCpuset))
  639. c.Assert(outCpuset, checker.Equals, "0")
  640. }
  641. func (s *DockerSuite) TestContainerApiVerifyHeader(c *check.C) {
  642. config := map[string]interface{}{
  643. "Image": "busybox",
  644. }
  645. create := func(ct string) (*http.Response, io.ReadCloser, error) {
  646. jsonData := bytes.NewBuffer(nil)
  647. c.Assert(json.NewEncoder(jsonData).Encode(config), checker.IsNil)
  648. return sockRequestRaw("POST", "/containers/create", jsonData, ct)
  649. }
  650. // Try with no content-type
  651. res, body, err := create("")
  652. c.Assert(err, checker.IsNil)
  653. c.Assert(res.StatusCode, checker.Equals, http.StatusInternalServerError)
  654. body.Close()
  655. // Try with wrong content-type
  656. res, body, err = create("application/xml")
  657. c.Assert(err, checker.IsNil)
  658. c.Assert(res.StatusCode, checker.Equals, http.StatusInternalServerError)
  659. body.Close()
  660. // now application/json
  661. res, body, err = create("application/json")
  662. c.Assert(err, checker.IsNil)
  663. c.Assert(res.StatusCode, checker.Equals, http.StatusCreated)
  664. body.Close()
  665. }
  666. //Issue 14230. daemon should return 500 for invalid port syntax
  667. func (s *DockerSuite) TestContainerApiInvalidPortSyntax(c *check.C) {
  668. config := `{
  669. "Image": "busybox",
  670. "HostConfig": {
  671. "NetworkMode": "default",
  672. "PortBindings": {
  673. "19039;1230": [
  674. {}
  675. ]
  676. }
  677. }
  678. }`
  679. res, body, err := sockRequestRaw("POST", "/containers/create", strings.NewReader(config), "application/json")
  680. c.Assert(err, checker.IsNil)
  681. c.Assert(res.StatusCode, checker.Equals, http.StatusInternalServerError)
  682. b, err := readBody(body)
  683. c.Assert(err, checker.IsNil)
  684. c.Assert(string(b[:]), checker.Contains, "Invalid port")
  685. }
  686. // Issue 7941 - test to make sure a "null" in JSON is just ignored.
  687. // W/o this fix a null in JSON would be parsed into a string var as "null"
  688. func (s *DockerSuite) TestContainerApiPostCreateNull(c *check.C) {
  689. // TODO Windows to Windows CI. Bit of this with alternate fields checked
  690. // can probably be ported.
  691. testRequires(c, DaemonIsLinux)
  692. config := `{
  693. "Hostname":"",
  694. "Domainname":"",
  695. "Memory":0,
  696. "MemorySwap":0,
  697. "CpuShares":0,
  698. "Cpuset":null,
  699. "AttachStdin":true,
  700. "AttachStdout":true,
  701. "AttachStderr":true,
  702. "ExposedPorts":{},
  703. "Tty":true,
  704. "OpenStdin":true,
  705. "StdinOnce":true,
  706. "Env":[],
  707. "Cmd":"ls",
  708. "Image":"busybox",
  709. "Volumes":{},
  710. "WorkingDir":"",
  711. "Entrypoint":null,
  712. "NetworkDisabled":false,
  713. "OnBuild":null}`
  714. res, body, err := sockRequestRaw("POST", "/containers/create", strings.NewReader(config), "application/json")
  715. c.Assert(err, checker.IsNil)
  716. c.Assert(res.StatusCode, checker.Equals, http.StatusCreated)
  717. b, err := readBody(body)
  718. c.Assert(err, checker.IsNil)
  719. type createResp struct {
  720. ID string
  721. }
  722. var container createResp
  723. c.Assert(json.Unmarshal(b, &container), checker.IsNil)
  724. out, err := inspectField(container.ID, "HostConfig.CpusetCpus")
  725. c.Assert(err, checker.IsNil)
  726. c.Assert(out, checker.Equals, "")
  727. outMemory, errMemory := inspectField(container.ID, "HostConfig.Memory")
  728. c.Assert(outMemory, checker.Equals, "0")
  729. c.Assert(errMemory, checker.IsNil)
  730. outMemorySwap, errMemorySwap := inspectField(container.ID, "HostConfig.MemorySwap")
  731. c.Assert(outMemorySwap, checker.Equals, "0")
  732. c.Assert(errMemorySwap, checker.IsNil)
  733. }
  734. func (s *DockerSuite) TestCreateWithTooLowMemoryLimit(c *check.C) {
  735. // TODO Windows: Port once memory is supported
  736. testRequires(c, DaemonIsLinux)
  737. config := `{
  738. "Image": "busybox",
  739. "Cmd": "ls",
  740. "OpenStdin": true,
  741. "CpuShares": 100,
  742. "Memory": 524287
  743. }`
  744. res, body, err := sockRequestRaw("POST", "/containers/create", strings.NewReader(config), "application/json")
  745. c.Assert(err, checker.IsNil)
  746. b, err2 := readBody(body)
  747. c.Assert(err2, checker.IsNil)
  748. c.Assert(res.StatusCode, checker.Equals, http.StatusInternalServerError)
  749. c.Assert(string(b), checker.Contains, "Minimum memory limit allowed is 4MB")
  750. }
  751. func (s *DockerSuite) TestStartWithTooLowMemoryLimit(c *check.C) {
  752. // TODO Windows: Port once memory is supported
  753. testRequires(c, DaemonIsLinux)
  754. out, _ := dockerCmd(c, "create", "busybox")
  755. containerID := strings.TrimSpace(out)
  756. config := `{
  757. "CpuShares": 100,
  758. "Memory": 524287
  759. }`
  760. res, body, err := sockRequestRaw("POST", "/containers/"+containerID+"/start", strings.NewReader(config), "application/json")
  761. c.Assert(err, checker.IsNil)
  762. b, err2 := readBody(body)
  763. c.Assert(err2, checker.IsNil)
  764. c.Assert(res.StatusCode, checker.Equals, http.StatusInternalServerError)
  765. c.Assert(string(b), checker.Contains, "Minimum memory limit allowed is 4MB")
  766. }
  767. func (s *DockerSuite) TestContainerApiRename(c *check.C) {
  768. // TODO Windows: Enable for TP5. Fails on TP4.
  769. testRequires(c, DaemonIsLinux)
  770. out, _ := dockerCmd(c, "run", "--name", "TestContainerApiRename", "-d", "busybox", "sh")
  771. containerID := strings.TrimSpace(out)
  772. newName := "TestContainerApiRenameNew"
  773. statusCode, _, err := sockRequest("POST", "/containers/"+containerID+"/rename?name="+newName, nil)
  774. c.Assert(err, checker.IsNil)
  775. // 204 No Content is expected, not 200
  776. c.Assert(statusCode, checker.Equals, http.StatusNoContent)
  777. name, err := inspectField(containerID, "Name")
  778. c.Assert(name, checker.Equals, "/"+newName, check.Commentf("Failed to rename container"))
  779. }
  780. func (s *DockerSuite) TestContainerApiKill(c *check.C) {
  781. name := "test-api-kill"
  782. dockerCmd(c, "run", "-di", "--name", name, "busybox", sleepCmd, "60")
  783. status, _, err := sockRequest("POST", "/containers/"+name+"/kill", nil)
  784. c.Assert(err, checker.IsNil)
  785. c.Assert(status, checker.Equals, http.StatusNoContent)
  786. state, err := inspectField(name, "State.Running")
  787. c.Assert(err, checker.IsNil)
  788. c.Assert(state, checker.Equals, "false", check.Commentf("got wrong State from container %s: %q", name, state))
  789. }
  790. func (s *DockerSuite) TestContainerApiRestart(c *check.C) {
  791. // TODO Windows to Windows CI. This is flaky due to the timing
  792. testRequires(c, DaemonIsLinux)
  793. name := "test-api-restart"
  794. dockerCmd(c, "run", "-di", "--name", name, "busybox", "top")
  795. status, _, err := sockRequest("POST", "/containers/"+name+"/restart?t=1", nil)
  796. c.Assert(err, checker.IsNil)
  797. c.Assert(status, checker.Equals, http.StatusNoContent)
  798. c.Assert(waitInspect(name, "{{ .State.Restarting }} {{ .State.Running }}", "false true", 5*time.Second), checker.IsNil)
  799. }
  800. func (s *DockerSuite) TestContainerApiRestartNotimeoutParam(c *check.C) {
  801. // TODO Windows to Windows CI. This is flaky due to the timing
  802. testRequires(c, DaemonIsLinux)
  803. name := "test-api-restart-no-timeout-param"
  804. out, _ := dockerCmd(c, "run", "-di", "--name", name, "busybox", "top")
  805. id := strings.TrimSpace(out)
  806. c.Assert(waitRun(id), checker.IsNil)
  807. status, _, err := sockRequest("POST", "/containers/"+name+"/restart", nil)
  808. c.Assert(err, checker.IsNil)
  809. c.Assert(status, checker.Equals, http.StatusNoContent)
  810. c.Assert(waitInspect(name, "{{ .State.Restarting }} {{ .State.Running }}", "false true", 5*time.Second), checker.IsNil)
  811. }
  812. func (s *DockerSuite) TestContainerApiStart(c *check.C) {
  813. name := "testing-start"
  814. config := map[string]interface{}{
  815. "Image": "busybox",
  816. "Cmd": []string{"/bin/sh", "-c", sleepCmd, "60"},
  817. "OpenStdin": true,
  818. }
  819. status, _, err := sockRequest("POST", "/containers/create?name="+name, config)
  820. c.Assert(err, checker.IsNil)
  821. c.Assert(status, checker.Equals, http.StatusCreated)
  822. conf := make(map[string]interface{})
  823. status, _, err = sockRequest("POST", "/containers/"+name+"/start", conf)
  824. c.Assert(err, checker.IsNil)
  825. c.Assert(status, checker.Equals, http.StatusNoContent)
  826. // second call to start should give 304
  827. status, _, err = sockRequest("POST", "/containers/"+name+"/start", conf)
  828. c.Assert(err, checker.IsNil)
  829. c.Assert(status, checker.Equals, http.StatusNotModified)
  830. }
  831. func (s *DockerSuite) TestContainerApiStop(c *check.C) {
  832. name := "test-api-stop"
  833. dockerCmd(c, "run", "-di", "--name", name, "busybox", sleepCmd, "60")
  834. status, _, err := sockRequest("POST", "/containers/"+name+"/stop?t=30", nil)
  835. c.Assert(err, checker.IsNil)
  836. c.Assert(status, checker.Equals, http.StatusNoContent)
  837. c.Assert(waitInspect(name, "{{ .State.Running }}", "false", 60*time.Second), checker.IsNil)
  838. // second call to start should give 304
  839. status, _, err = sockRequest("POST", "/containers/"+name+"/stop?t=30", nil)
  840. c.Assert(err, checker.IsNil)
  841. c.Assert(status, checker.Equals, http.StatusNotModified)
  842. }
  843. func (s *DockerSuite) TestContainerApiWait(c *check.C) {
  844. name := "test-api-wait"
  845. dockerCmd(c, "run", "--name", name, "busybox", sleepCmd, "5")
  846. status, body, err := sockRequest("POST", "/containers/"+name+"/wait", nil)
  847. c.Assert(err, checker.IsNil)
  848. c.Assert(status, checker.Equals, http.StatusOK)
  849. c.Assert(waitInspect(name, "{{ .State.Running }}", "false", 60*time.Second), checker.IsNil)
  850. var waitres types.ContainerWaitResponse
  851. c.Assert(json.Unmarshal(body, &waitres), checker.IsNil)
  852. c.Assert(waitres.StatusCode, checker.Equals, 0)
  853. }
  854. func (s *DockerSuite) TestContainerApiCopy(c *check.C) {
  855. // TODO Windows to Windows CI. This can be ported.
  856. testRequires(c, DaemonIsLinux)
  857. name := "test-container-api-copy"
  858. dockerCmd(c, "run", "--name", name, "busybox", "touch", "/test.txt")
  859. postData := types.CopyConfig{
  860. Resource: "/test.txt",
  861. }
  862. status, body, err := sockRequest("POST", "/containers/"+name+"/copy", postData)
  863. c.Assert(err, checker.IsNil)
  864. c.Assert(status, checker.Equals, http.StatusOK)
  865. found := false
  866. for tarReader := tar.NewReader(bytes.NewReader(body)); ; {
  867. h, err := tarReader.Next()
  868. if err != nil {
  869. if err == io.EOF {
  870. break
  871. }
  872. c.Fatal(err)
  873. }
  874. if h.Name == "test.txt" {
  875. found = true
  876. break
  877. }
  878. }
  879. c.Assert(found, checker.True)
  880. }
  881. func (s *DockerSuite) TestContainerApiCopyResourcePathEmpty(c *check.C) {
  882. // TODO Windows to Windows CI. This can be ported.
  883. testRequires(c, DaemonIsLinux)
  884. name := "test-container-api-copy-resource-empty"
  885. dockerCmd(c, "run", "--name", name, "busybox", "touch", "/test.txt")
  886. postData := types.CopyConfig{
  887. Resource: "",
  888. }
  889. status, body, err := sockRequest("POST", "/containers/"+name+"/copy", postData)
  890. c.Assert(err, checker.IsNil)
  891. c.Assert(status, checker.Equals, http.StatusInternalServerError)
  892. c.Assert(string(body), checker.Matches, "Path cannot be empty\n")
  893. }
  894. func (s *DockerSuite) TestContainerApiCopyResourcePathNotFound(c *check.C) {
  895. // TODO Windows to Windows CI. This can be ported.
  896. testRequires(c, DaemonIsLinux)
  897. name := "test-container-api-copy-resource-not-found"
  898. dockerCmd(c, "run", "--name", name, "busybox")
  899. postData := types.CopyConfig{
  900. Resource: "/notexist",
  901. }
  902. status, body, err := sockRequest("POST", "/containers/"+name+"/copy", postData)
  903. c.Assert(err, checker.IsNil)
  904. c.Assert(status, checker.Equals, http.StatusInternalServerError)
  905. c.Assert(string(body), checker.Matches, "Could not find the file /notexist in container "+name+"\n")
  906. }
  907. func (s *DockerSuite) TestContainerApiCopyContainerNotFound(c *check.C) {
  908. postData := types.CopyConfig{
  909. Resource: "/something",
  910. }
  911. status, _, err := sockRequest("POST", "/containers/notexists/copy", postData)
  912. c.Assert(err, checker.IsNil)
  913. c.Assert(status, checker.Equals, http.StatusNotFound)
  914. }
  915. func (s *DockerSuite) TestContainerApiDelete(c *check.C) {
  916. out, _ := dockerCmd(c, "run", "-d", "busybox", sleepCmd, "60")
  917. id := strings.TrimSpace(out)
  918. c.Assert(waitRun(id), checker.IsNil)
  919. dockerCmd(c, "stop", id)
  920. status, _, err := sockRequest("DELETE", "/containers/"+id, nil)
  921. c.Assert(err, checker.IsNil)
  922. c.Assert(status, checker.Equals, http.StatusNoContent)
  923. }
  924. func (s *DockerSuite) TestContainerApiDeleteNotExist(c *check.C) {
  925. status, body, err := sockRequest("DELETE", "/containers/doesnotexist", nil)
  926. c.Assert(err, checker.IsNil)
  927. c.Assert(status, checker.Equals, http.StatusNotFound)
  928. c.Assert(string(body), checker.Matches, "No such container: doesnotexist\n")
  929. }
  930. func (s *DockerSuite) TestContainerApiDeleteForce(c *check.C) {
  931. out, _ := dockerCmd(c, "run", "-d", "busybox", sleepCmd, "60")
  932. id := strings.TrimSpace(out)
  933. c.Assert(waitRun(id), checker.IsNil)
  934. status, _, err := sockRequest("DELETE", "/containers/"+id+"?force=1", nil)
  935. c.Assert(err, checker.IsNil)
  936. c.Assert(status, checker.Equals, http.StatusNoContent)
  937. }
  938. func (s *DockerSuite) TestContainerApiDeleteRemoveLinks(c *check.C) {
  939. // Windows does not support links
  940. testRequires(c, DaemonIsLinux)
  941. out, _ := dockerCmd(c, "run", "-d", "--name", "tlink1", "busybox", "top")
  942. id := strings.TrimSpace(out)
  943. c.Assert(waitRun(id), checker.IsNil)
  944. out, _ = dockerCmd(c, "run", "--link", "tlink1:tlink1", "--name", "tlink2", "-d", "busybox", "top")
  945. id2 := strings.TrimSpace(out)
  946. c.Assert(waitRun(id2), checker.IsNil)
  947. links, err := inspectFieldJSON(id2, "HostConfig.Links")
  948. c.Assert(err, checker.IsNil)
  949. c.Assert(links, checker.Equals, "[\"/tlink1:/tlink2/tlink1\"]", check.Commentf("expected to have links between containers"))
  950. status, b, err := sockRequest("DELETE", "/containers/tlink2/tlink1?link=1", nil)
  951. c.Assert(err, check.IsNil)
  952. c.Assert(status, check.Equals, http.StatusNoContent, check.Commentf(string(b)))
  953. linksPostRm, err := inspectFieldJSON(id2, "HostConfig.Links")
  954. c.Assert(err, checker.IsNil)
  955. c.Assert(linksPostRm, checker.Equals, "null", check.Commentf("call to api deleteContainer links should have removed the specified links"))
  956. }
  957. func (s *DockerSuite) TestContainerApiDeleteConflict(c *check.C) {
  958. out, _ := dockerCmd(c, "run", "-d", "busybox", sleepCmd, "60")
  959. id := strings.TrimSpace(out)
  960. c.Assert(waitRun(id), checker.IsNil)
  961. status, _, err := sockRequest("DELETE", "/containers/"+id, nil)
  962. c.Assert(err, checker.IsNil)
  963. c.Assert(status, checker.Equals, http.StatusConflict)
  964. }
  965. func (s *DockerSuite) TestContainerApiDeleteRemoveVolume(c *check.C) {
  966. testRequires(c, SameHostDaemon)
  967. vol := "/testvolume"
  968. if daemonPlatform == "windows" {
  969. vol = `c:\testvolume`
  970. }
  971. out, _ := dockerCmd(c, "run", "-d", "-v", vol, "busybox", sleepCmd, "60")
  972. id := strings.TrimSpace(out)
  973. c.Assert(waitRun(id), checker.IsNil)
  974. source, err := inspectMountSourceField(id, vol)
  975. _, err = os.Stat(source)
  976. c.Assert(err, checker.IsNil)
  977. status, _, err := sockRequest("DELETE", "/containers/"+id+"?v=1&force=1", nil)
  978. c.Assert(err, checker.IsNil)
  979. c.Assert(status, checker.Equals, http.StatusNoContent)
  980. _, err = os.Stat(source)
  981. c.Assert(os.IsNotExist(err), checker.True, check.Commentf("expected to get ErrNotExist error, got %v", err))
  982. }
  983. // Regression test for https://github.com/docker/docker/issues/6231
  984. func (s *DockerSuite) TestContainerApiChunkedEncoding(c *check.C) {
  985. // TODO Windows CI: This can be ported
  986. testRequires(c, DaemonIsLinux)
  987. out, _ := dockerCmd(c, "create", "-v", "/foo", "busybox", "true")
  988. id := strings.TrimSpace(out)
  989. conn, err := sockConn(time.Duration(10 * time.Second))
  990. c.Assert(err, checker.IsNil)
  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. c.Assert(err, checker.IsNil)
  996. req.Header.Set("Content-Type", "application/json")
  997. // This is a cheat to make the http request do chunked encoding
  998. // Otherwise (just setting the Content-Encoding to chunked) net/http will overwrite
  999. // https://golang.org/src/pkg/net/http/request.go?s=11980:12172
  1000. req.ContentLength = -1
  1001. resp, err := client.Do(req)
  1002. c.Assert(err, checker.IsNil, check.Commentf("error starting container with chunked encoding"))
  1003. resp.Body.Close()
  1004. c.Assert(resp.StatusCode, checker.Equals, 204)
  1005. out, err = inspectFieldJSON(id, "HostConfig.Binds")
  1006. c.Assert(err, checker.IsNil)
  1007. var binds []string
  1008. c.Assert(json.NewDecoder(strings.NewReader(out)).Decode(&binds), checker.IsNil)
  1009. c.Assert(binds, checker.HasLen, 1, check.Commentf("Got unexpected binds: %v", binds))
  1010. expected := "/tmp:/foo"
  1011. c.Assert(binds[0], checker.Equals, expected, check.Commentf("got incorrect bind spec"))
  1012. }
  1013. func (s *DockerSuite) TestContainerApiPostContainerStop(c *check.C) {
  1014. out, _ := dockerCmd(c, "run", "-d", "busybox", sleepCmd, "60")
  1015. containerID := strings.TrimSpace(out)
  1016. c.Assert(waitRun(containerID), checker.IsNil)
  1017. statusCode, _, err := sockRequest("POST", "/containers/"+containerID+"/stop", nil)
  1018. c.Assert(err, checker.IsNil)
  1019. // 204 No Content is expected, not 200
  1020. c.Assert(statusCode, checker.Equals, http.StatusNoContent)
  1021. c.Assert(waitInspect(containerID, "{{ .State.Running }}", "false", 5*time.Second), checker.IsNil)
  1022. }
  1023. // #14170
  1024. func (s *DockerSuite) TestPostContainerApiCreateWithStringOrSliceEntrypoint(c *check.C) {
  1025. config := struct {
  1026. Image string
  1027. Entrypoint string
  1028. Cmd []string
  1029. }{"busybox", "echo", []string{"hello", "world"}}
  1030. _, _, err := sockRequest("POST", "/containers/create?name=echotest", config)
  1031. c.Assert(err, checker.IsNil)
  1032. out, _ := dockerCmd(c, "start", "-a", "echotest")
  1033. c.Assert(strings.TrimSpace(out), checker.Equals, "hello world")
  1034. config2 := struct {
  1035. Image string
  1036. Entrypoint []string
  1037. Cmd []string
  1038. }{"busybox", []string{"echo"}, []string{"hello", "world"}}
  1039. _, _, err = sockRequest("POST", "/containers/create?name=echotest2", config2)
  1040. c.Assert(err, checker.IsNil)
  1041. out, _ = dockerCmd(c, "start", "-a", "echotest2")
  1042. c.Assert(strings.TrimSpace(out), checker.Equals, "hello world")
  1043. }
  1044. // #14170
  1045. func (s *DockerSuite) TestPostContainersCreateWithStringOrSliceCmd(c *check.C) {
  1046. config := struct {
  1047. Image string
  1048. Entrypoint string
  1049. Cmd string
  1050. }{"busybox", "echo", "hello world"}
  1051. _, _, err := sockRequest("POST", "/containers/create?name=echotest", config)
  1052. c.Assert(err, checker.IsNil)
  1053. out, _ := dockerCmd(c, "start", "-a", "echotest")
  1054. c.Assert(strings.TrimSpace(out), checker.Equals, "hello world")
  1055. config2 := struct {
  1056. Image string
  1057. Cmd []string
  1058. }{"busybox", []string{"echo", "hello", "world"}}
  1059. _, _, err = sockRequest("POST", "/containers/create?name=echotest2", config2)
  1060. c.Assert(err, checker.IsNil)
  1061. out, _ = dockerCmd(c, "start", "-a", "echotest2")
  1062. c.Assert(strings.TrimSpace(out), checker.Equals, "hello world")
  1063. }
  1064. // regression #14318
  1065. func (s *DockerSuite) TestPostContainersCreateWithStringOrSliceCapAddDrop(c *check.C) {
  1066. // Windows doesn't support CapAdd/CapDrop
  1067. testRequires(c, DaemonIsLinux)
  1068. config := struct {
  1069. Image string
  1070. CapAdd string
  1071. CapDrop string
  1072. }{"busybox", "NET_ADMIN", "SYS_ADMIN"}
  1073. status, _, err := sockRequest("POST", "/containers/create?name=capaddtest0", config)
  1074. c.Assert(err, checker.IsNil)
  1075. c.Assert(status, checker.Equals, http.StatusCreated)
  1076. config2 := struct {
  1077. Image string
  1078. CapAdd []string
  1079. CapDrop []string
  1080. }{"busybox", []string{"NET_ADMIN", "SYS_ADMIN"}, []string{"SETGID"}}
  1081. status, _, err = sockRequest("POST", "/containers/create?name=capaddtest1", config2)
  1082. c.Assert(err, checker.IsNil)
  1083. c.Assert(status, checker.Equals, http.StatusCreated)
  1084. }
  1085. // #14640
  1086. func (s *DockerSuite) TestPostContainersStartWithoutLinksInHostConfig(c *check.C) {
  1087. // TODO Windows: Windows doesn't support supplying a hostconfig on start.
  1088. // An alternate test could be written to validate the negative testing aspect of this
  1089. testRequires(c, DaemonIsLinux)
  1090. name := "test-host-config-links"
  1091. dockerCmd(c, "create", "--name", name, "busybox", sleepCmd, "60")
  1092. hc, err := inspectFieldJSON(name, "HostConfig")
  1093. c.Assert(err, checker.IsNil)
  1094. config := `{"HostConfig":` + hc + `}`
  1095. res, b, err := sockRequestRaw("POST", "/containers/"+name+"/start", strings.NewReader(config), "application/json")
  1096. c.Assert(err, checker.IsNil)
  1097. c.Assert(res.StatusCode, checker.Equals, http.StatusNoContent)
  1098. b.Close()
  1099. }
  1100. // #14640
  1101. func (s *DockerSuite) TestPostContainersStartWithLinksInHostConfig(c *check.C) {
  1102. // TODO Windows: Windows doesn't support supplying a hostconfig on start.
  1103. // An alternate test could be written to validate the negative testing aspect of this
  1104. testRequires(c, DaemonIsLinux)
  1105. name := "test-host-config-links"
  1106. dockerCmd(c, "run", "--name", "foo", "-d", "busybox", "top")
  1107. dockerCmd(c, "create", "--name", name, "--link", "foo:bar", "busybox", "top")
  1108. hc, err := inspectFieldJSON(name, "HostConfig")
  1109. c.Assert(err, checker.IsNil)
  1110. config := `{"HostConfig":` + hc + `}`
  1111. res, b, err := sockRequestRaw("POST", "/containers/"+name+"/start", strings.NewReader(config), "application/json")
  1112. c.Assert(err, checker.IsNil)
  1113. c.Assert(res.StatusCode, checker.Equals, http.StatusNoContent)
  1114. b.Close()
  1115. }
  1116. // #14640
  1117. func (s *DockerSuite) TestPostContainersStartWithLinksInHostConfigIdLinked(c *check.C) {
  1118. // Windows does not support links
  1119. testRequires(c, DaemonIsLinux)
  1120. name := "test-host-config-links"
  1121. out, _ := dockerCmd(c, "run", "--name", "link0", "-d", "busybox", "top")
  1122. id := strings.TrimSpace(out)
  1123. dockerCmd(c, "create", "--name", name, "--link", id, "busybox", "top")
  1124. hc, err := inspectFieldJSON(name, "HostConfig")
  1125. c.Assert(err, checker.IsNil)
  1126. config := `{"HostConfig":` + hc + `}`
  1127. res, b, err := sockRequestRaw("POST", "/containers/"+name+"/start", strings.NewReader(config), "application/json")
  1128. c.Assert(err, checker.IsNil)
  1129. c.Assert(res.StatusCode, checker.Equals, http.StatusNoContent)
  1130. b.Close()
  1131. }
  1132. // #14915
  1133. func (s *DockerSuite) TestContainerApiCreateNoHostConfig118(c *check.C) {
  1134. config := struct {
  1135. Image string
  1136. }{"busybox"}
  1137. status, _, err := sockRequest("POST", "/v1.18/containers/create", config)
  1138. c.Assert(err, checker.IsNil)
  1139. c.Assert(status, checker.Equals, http.StatusCreated)
  1140. }
  1141. // Ensure an error occurs when you have a container read-only rootfs but you
  1142. // extract an archive to a symlink in a writable volume which points to a
  1143. // directory outside of the volume.
  1144. func (s *DockerSuite) TestPutContainerArchiveErrSymlinkInVolumeToReadOnlyRootfs(c *check.C) {
  1145. // Windows does not support read-only rootfs
  1146. // Requires local volume mount bind.
  1147. // --read-only + userns has remount issues
  1148. testRequires(c, SameHostDaemon, NotUserNamespace, DaemonIsLinux)
  1149. testVol := getTestDir(c, "test-put-container-archive-err-symlink-in-volume-to-read-only-rootfs-")
  1150. defer os.RemoveAll(testVol)
  1151. makeTestContentInDir(c, testVol)
  1152. cID := makeTestContainer(c, testContainerOptions{
  1153. readOnly: true,
  1154. volumes: defaultVolumes(testVol), // Our bind mount is at /vol2
  1155. })
  1156. defer deleteContainer(cID)
  1157. // Attempt to extract to a symlink in the volume which points to a
  1158. // directory outside the volume. This should cause an error because the
  1159. // rootfs is read-only.
  1160. query := make(url.Values, 1)
  1161. query.Set("path", "/vol2/symlinkToAbsDir")
  1162. urlPath := fmt.Sprintf("/v1.20/containers/%s/archive?%s", cID, query.Encode())
  1163. statusCode, body, err := sockRequest("PUT", urlPath, nil)
  1164. c.Assert(err, checker.IsNil)
  1165. if !isCpCannotCopyReadOnly(fmt.Errorf(string(body))) {
  1166. c.Fatalf("expected ErrContainerRootfsReadonly error, but got %d: %s", statusCode, string(body))
  1167. }
  1168. }
  1169. func (s *DockerSuite) TestContainerApiGetContainersJSONEmpty(c *check.C) {
  1170. status, body, err := sockRequest("GET", "/containers/json?all=1", nil)
  1171. c.Assert(err, checker.IsNil)
  1172. c.Assert(status, checker.Equals, http.StatusOK)
  1173. c.Assert(string(body), checker.Equals, "[]\n")
  1174. }
  1175. func (s *DockerSuite) TestPostContainersCreateWithWrongCpusetValues(c *check.C) {
  1176. // Not supported on Windows
  1177. testRequires(c, DaemonIsLinux)
  1178. c1 := struct {
  1179. Image string
  1180. CpusetCpus string
  1181. }{"busybox", "1-42,,"}
  1182. name := "wrong-cpuset-cpus"
  1183. status, body, err := sockRequest("POST", "/containers/create?name="+name, c1)
  1184. c.Assert(err, checker.IsNil)
  1185. c.Assert(status, checker.Equals, http.StatusInternalServerError)
  1186. expected := "Invalid value 1-42,, for cpuset cpus.\n"
  1187. c.Assert(string(body), checker.Equals, expected)
  1188. c2 := struct {
  1189. Image string
  1190. CpusetMems string
  1191. }{"busybox", "42-3,1--"}
  1192. name = "wrong-cpuset-mems"
  1193. status, body, err = sockRequest("POST", "/containers/create?name="+name, c2)
  1194. c.Assert(err, checker.IsNil)
  1195. c.Assert(status, checker.Equals, http.StatusInternalServerError)
  1196. expected = "Invalid value 42-3,1-- for cpuset mems.\n"
  1197. c.Assert(string(body), checker.Equals, expected)
  1198. }
  1199. func (s *DockerSuite) TestStartWithNilDNS(c *check.C) {
  1200. // TODO Windows: Add once DNS is supported
  1201. testRequires(c, DaemonIsLinux)
  1202. out, _ := dockerCmd(c, "create", "busybox")
  1203. containerID := strings.TrimSpace(out)
  1204. config := `{"HostConfig": {"Dns": null}}`
  1205. res, b, err := sockRequestRaw("POST", "/containers/"+containerID+"/start", strings.NewReader(config), "application/json")
  1206. c.Assert(err, checker.IsNil)
  1207. c.Assert(res.StatusCode, checker.Equals, http.StatusNoContent)
  1208. b.Close()
  1209. dns, err := inspectFieldJSON(containerID, "HostConfig.Dns")
  1210. c.Assert(err, checker.IsNil)
  1211. c.Assert(dns, checker.Equals, "[]")
  1212. }
  1213. func (s *DockerSuite) TestPostContainersCreateShmSizeNegative(c *check.C) {
  1214. // ShmSize is not supported on Windows
  1215. testRequires(c, DaemonIsLinux)
  1216. config := map[string]interface{}{
  1217. "Image": "busybox",
  1218. "HostConfig": map[string]interface{}{"ShmSize": -1},
  1219. }
  1220. status, body, err := sockRequest("POST", "/containers/create", config)
  1221. c.Assert(err, check.IsNil)
  1222. c.Assert(status, check.Equals, http.StatusInternalServerError)
  1223. c.Assert(string(body), checker.Contains, "SHM size must be greater then 0")
  1224. }
  1225. func (s *DockerSuite) TestPostContainersCreateShmSizeHostConfigOmitted(c *check.C) {
  1226. // ShmSize is not supported on Windows
  1227. testRequires(c, DaemonIsLinux)
  1228. var defaultSHMSize int64 = 67108864
  1229. config := map[string]interface{}{
  1230. "Image": "busybox",
  1231. "Cmd": "mount",
  1232. }
  1233. status, body, err := sockRequest("POST", "/containers/create", config)
  1234. c.Assert(err, check.IsNil)
  1235. c.Assert(status, check.Equals, http.StatusCreated)
  1236. var container types.ContainerCreateResponse
  1237. c.Assert(json.Unmarshal(body, &container), check.IsNil)
  1238. status, body, err = sockRequest("GET", "/containers/"+container.ID+"/json", nil)
  1239. c.Assert(err, check.IsNil)
  1240. c.Assert(status, check.Equals, http.StatusOK)
  1241. var containerJSON types.ContainerJSON
  1242. c.Assert(json.Unmarshal(body, &containerJSON), check.IsNil)
  1243. c.Assert(containerJSON.HostConfig.ShmSize, check.Equals, defaultSHMSize)
  1244. out, _ := dockerCmd(c, "start", "-i", containerJSON.ID)
  1245. shmRegexp := regexp.MustCompile(`shm on /dev/shm type tmpfs(.*)size=65536k`)
  1246. if !shmRegexp.MatchString(out) {
  1247. c.Fatalf("Expected shm of 64MB in mount command, got %v", out)
  1248. }
  1249. }
  1250. func (s *DockerSuite) TestPostContainersCreateShmSizeOmitted(c *check.C) {
  1251. // ShmSize is not supported on Windows
  1252. testRequires(c, DaemonIsLinux)
  1253. config := map[string]interface{}{
  1254. "Image": "busybox",
  1255. "HostConfig": map[string]interface{}{},
  1256. "Cmd": "mount",
  1257. }
  1258. status, body, err := sockRequest("POST", "/containers/create", config)
  1259. c.Assert(err, check.IsNil)
  1260. c.Assert(status, check.Equals, http.StatusCreated)
  1261. var container types.ContainerCreateResponse
  1262. c.Assert(json.Unmarshal(body, &container), check.IsNil)
  1263. status, body, err = sockRequest("GET", "/containers/"+container.ID+"/json", nil)
  1264. c.Assert(err, check.IsNil)
  1265. c.Assert(status, check.Equals, http.StatusOK)
  1266. var containerJSON types.ContainerJSON
  1267. c.Assert(json.Unmarshal(body, &containerJSON), check.IsNil)
  1268. c.Assert(containerJSON.HostConfig.ShmSize, check.Equals, int64(67108864))
  1269. out, _ := dockerCmd(c, "start", "-i", containerJSON.ID)
  1270. shmRegexp := regexp.MustCompile(`shm on /dev/shm type tmpfs(.*)size=65536k`)
  1271. if !shmRegexp.MatchString(out) {
  1272. c.Fatalf("Expected shm of 64MB in mount command, got %v", out)
  1273. }
  1274. }
  1275. func (s *DockerSuite) TestPostContainersCreateWithShmSize(c *check.C) {
  1276. // ShmSize is not supported on Windows
  1277. testRequires(c, DaemonIsLinux)
  1278. config := map[string]interface{}{
  1279. "Image": "busybox",
  1280. "Cmd": "mount",
  1281. "HostConfig": map[string]interface{}{"ShmSize": 1073741824},
  1282. }
  1283. status, body, err := sockRequest("POST", "/containers/create", config)
  1284. c.Assert(err, check.IsNil)
  1285. c.Assert(status, check.Equals, http.StatusCreated)
  1286. var container types.ContainerCreateResponse
  1287. c.Assert(json.Unmarshal(body, &container), check.IsNil)
  1288. status, body, err = sockRequest("GET", "/containers/"+container.ID+"/json", nil)
  1289. c.Assert(err, check.IsNil)
  1290. c.Assert(status, check.Equals, http.StatusOK)
  1291. var containerJSON types.ContainerJSON
  1292. c.Assert(json.Unmarshal(body, &containerJSON), check.IsNil)
  1293. c.Assert(containerJSON.HostConfig.ShmSize, check.Equals, int64(1073741824))
  1294. out, _ := dockerCmd(c, "start", "-i", containerJSON.ID)
  1295. shmRegex := regexp.MustCompile(`shm on /dev/shm type tmpfs(.*)size=1048576k`)
  1296. if !shmRegex.MatchString(out) {
  1297. c.Fatalf("Expected shm of 1GB in mount command, got %v", out)
  1298. }
  1299. }
  1300. func (s *DockerSuite) TestPostContainersCreateMemorySwappinessHostConfigOmitted(c *check.C) {
  1301. // Swappiness is not supported on Windows
  1302. testRequires(c, DaemonIsLinux)
  1303. config := map[string]interface{}{
  1304. "Image": "busybox",
  1305. }
  1306. status, body, err := sockRequest("POST", "/containers/create", config)
  1307. c.Assert(err, check.IsNil)
  1308. c.Assert(status, check.Equals, http.StatusCreated)
  1309. var container types.ContainerCreateResponse
  1310. c.Assert(json.Unmarshal(body, &container), check.IsNil)
  1311. status, body, err = sockRequest("GET", "/containers/"+container.ID+"/json", nil)
  1312. c.Assert(err, check.IsNil)
  1313. c.Assert(status, check.Equals, http.StatusOK)
  1314. var containerJSON types.ContainerJSON
  1315. c.Assert(json.Unmarshal(body, &containerJSON), check.IsNil)
  1316. c.Assert(*containerJSON.HostConfig.MemorySwappiness, check.Equals, int64(-1))
  1317. }
  1318. // check validation is done daemon side and not only in cli
  1319. func (s *DockerSuite) TestPostContainersCreateWithOomScoreAdjInvalidRange(c *check.C) {
  1320. // OomScoreAdj is not supported on Windows
  1321. testRequires(c, DaemonIsLinux)
  1322. config := struct {
  1323. Image string
  1324. OomScoreAdj int
  1325. }{"busybox", 1001}
  1326. name := "oomscoreadj-over"
  1327. status, b, err := sockRequest("POST", "/containers/create?name="+name, config)
  1328. c.Assert(err, check.IsNil)
  1329. c.Assert(status, check.Equals, http.StatusInternalServerError)
  1330. expected := "Invalid value 1001, range for oom score adj is [-1000, 1000]."
  1331. if !strings.Contains(string(b), expected) {
  1332. c.Fatalf("Expected output to contain %q, got %q", expected, string(b))
  1333. }
  1334. config = struct {
  1335. Image string
  1336. OomScoreAdj int
  1337. }{"busybox", -1001}
  1338. name = "oomscoreadj-low"
  1339. status, b, err = sockRequest("POST", "/containers/create?name="+name, config)
  1340. c.Assert(err, check.IsNil)
  1341. c.Assert(status, check.Equals, http.StatusInternalServerError)
  1342. expected = "Invalid value -1001, range for oom score adj is [-1000, 1000]."
  1343. if !strings.Contains(string(b), expected) {
  1344. c.Fatalf("Expected output to contain %q, got %q", expected, string(b))
  1345. }
  1346. }