docker_api_containers_test.go 54 KB

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