docker_api_containers_test.go 53 KB

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