docker_api_containers_test.go 54 KB

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