docker_api_containers_test.go 54 KB

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