docker_api_containers_test.go 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580
  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. c.Assert(status, checker.Equals, http.StatusNotModified)
  802. }
  803. func (s *DockerSuite) TestContainerApiStop(c *check.C) {
  804. testRequires(c, DaemonIsLinux)
  805. name := "test-api-stop"
  806. dockerCmd(c, "run", "-di", "--name", name, "busybox", "top")
  807. status, _, err := sockRequest("POST", "/containers/"+name+"/stop?t=1", nil)
  808. c.Assert(err, checker.IsNil)
  809. c.Assert(status, checker.Equals, http.StatusNoContent)
  810. c.Assert(waitInspect(name, "{{ .State.Running }}", "false", 5*time.Second), checker.IsNil)
  811. // second call to start should give 304
  812. status, _, err = sockRequest("POST", "/containers/"+name+"/stop?t=1", nil)
  813. c.Assert(err, checker.IsNil)
  814. c.Assert(status, checker.Equals, http.StatusNotModified)
  815. }
  816. func (s *DockerSuite) TestContainerApiWait(c *check.C) {
  817. testRequires(c, DaemonIsLinux)
  818. name := "test-api-wait"
  819. dockerCmd(c, "run", "--name", name, "busybox", "sleep", "5")
  820. status, body, err := sockRequest("POST", "/containers/"+name+"/wait", nil)
  821. c.Assert(err, checker.IsNil)
  822. c.Assert(status, checker.Equals, http.StatusOK)
  823. c.Assert(waitInspect(name, "{{ .State.Running }}", "false", 5*time.Second), checker.IsNil)
  824. var waitres types.ContainerWaitResponse
  825. c.Assert(json.Unmarshal(body, &waitres), checker.IsNil)
  826. c.Assert(waitres.StatusCode, checker.Equals, 0)
  827. }
  828. func (s *DockerSuite) TestContainerApiCopy(c *check.C) {
  829. testRequires(c, DaemonIsLinux)
  830. name := "test-container-api-copy"
  831. dockerCmd(c, "run", "--name", name, "busybox", "touch", "/test.txt")
  832. postData := types.CopyConfig{
  833. Resource: "/test.txt",
  834. }
  835. status, body, err := sockRequest("POST", "/containers/"+name+"/copy", postData)
  836. c.Assert(err, checker.IsNil)
  837. c.Assert(status, checker.Equals, http.StatusOK)
  838. found := false
  839. for tarReader := tar.NewReader(bytes.NewReader(body)); ; {
  840. h, err := tarReader.Next()
  841. if err != nil {
  842. if err == io.EOF {
  843. break
  844. }
  845. c.Fatal(err)
  846. }
  847. if h.Name == "test.txt" {
  848. found = true
  849. break
  850. }
  851. }
  852. c.Assert(found, checker.True)
  853. }
  854. func (s *DockerSuite) TestContainerApiCopyResourcePathEmpty(c *check.C) {
  855. testRequires(c, DaemonIsLinux)
  856. name := "test-container-api-copy-resource-empty"
  857. dockerCmd(c, "run", "--name", name, "busybox", "touch", "/test.txt")
  858. postData := types.CopyConfig{
  859. Resource: "",
  860. }
  861. status, body, err := sockRequest("POST", "/containers/"+name+"/copy", postData)
  862. c.Assert(err, checker.IsNil)
  863. c.Assert(status, checker.Equals, http.StatusInternalServerError)
  864. c.Assert(string(body), checker.Matches, "Path cannot be empty\n")
  865. }
  866. func (s *DockerSuite) TestContainerApiCopyResourcePathNotFound(c *check.C) {
  867. testRequires(c, DaemonIsLinux)
  868. name := "test-container-api-copy-resource-not-found"
  869. dockerCmd(c, "run", "--name", name, "busybox")
  870. postData := types.CopyConfig{
  871. Resource: "/notexist",
  872. }
  873. status, body, err := sockRequest("POST", "/containers/"+name+"/copy", postData)
  874. c.Assert(err, checker.IsNil)
  875. c.Assert(status, checker.Equals, http.StatusInternalServerError)
  876. c.Assert(string(body), checker.Matches, "Could not find the file /notexist in container "+name+"\n")
  877. }
  878. func (s *DockerSuite) TestContainerApiCopyContainerNotFound(c *check.C) {
  879. postData := types.CopyConfig{
  880. Resource: "/something",
  881. }
  882. status, _, err := sockRequest("POST", "/containers/notexists/copy", postData)
  883. c.Assert(err, checker.IsNil)
  884. c.Assert(status, checker.Equals, http.StatusNotFound)
  885. }
  886. func (s *DockerSuite) TestContainerApiDelete(c *check.C) {
  887. testRequires(c, DaemonIsLinux)
  888. out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
  889. id := strings.TrimSpace(out)
  890. c.Assert(waitRun(id), checker.IsNil)
  891. dockerCmd(c, "stop", id)
  892. status, _, err := sockRequest("DELETE", "/containers/"+id, nil)
  893. c.Assert(err, checker.IsNil)
  894. c.Assert(status, checker.Equals, http.StatusNoContent)
  895. }
  896. func (s *DockerSuite) TestContainerApiDeleteNotExist(c *check.C) {
  897. status, body, err := sockRequest("DELETE", "/containers/doesnotexist", nil)
  898. c.Assert(err, checker.IsNil)
  899. c.Assert(status, checker.Equals, http.StatusNotFound)
  900. c.Assert(string(body), checker.Matches, "No such container: doesnotexist\n")
  901. }
  902. func (s *DockerSuite) TestContainerApiDeleteForce(c *check.C) {
  903. testRequires(c, DaemonIsLinux)
  904. out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
  905. id := strings.TrimSpace(out)
  906. c.Assert(waitRun(id), checker.IsNil)
  907. status, _, err := sockRequest("DELETE", "/containers/"+id+"?force=1", nil)
  908. c.Assert(err, checker.IsNil)
  909. c.Assert(status, checker.Equals, http.StatusNoContent)
  910. }
  911. func (s *DockerSuite) TestContainerApiDeleteRemoveLinks(c *check.C) {
  912. testRequires(c, DaemonIsLinux)
  913. out, _ := dockerCmd(c, "run", "-d", "--name", "tlink1", "busybox", "top")
  914. id := strings.TrimSpace(out)
  915. c.Assert(waitRun(id), checker.IsNil)
  916. out, _ = dockerCmd(c, "run", "--link", "tlink1:tlink1", "--name", "tlink2", "-d", "busybox", "top")
  917. id2 := strings.TrimSpace(out)
  918. c.Assert(waitRun(id2), checker.IsNil)
  919. links, err := inspectFieldJSON(id2, "HostConfig.Links")
  920. c.Assert(err, checker.IsNil)
  921. c.Assert(links, checker.Equals, "[\"/tlink1:/tlink2/tlink1\"]", check.Commentf("expected to have links between containers"))
  922. status, b, err := sockRequest("DELETE", "/containers/tlink2/tlink1?link=1", nil)
  923. c.Assert(err, check.IsNil)
  924. c.Assert(status, check.Equals, http.StatusNoContent, check.Commentf(string(b)))
  925. linksPostRm, err := inspectFieldJSON(id2, "HostConfig.Links")
  926. c.Assert(err, checker.IsNil)
  927. c.Assert(linksPostRm, checker.Equals, "null", check.Commentf("call to api deleteContainer links should have removed the specified links"))
  928. }
  929. func (s *DockerSuite) TestContainerApiDeleteConflict(c *check.C) {
  930. testRequires(c, DaemonIsLinux)
  931. out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
  932. id := strings.TrimSpace(out)
  933. c.Assert(waitRun(id), checker.IsNil)
  934. status, _, err := sockRequest("DELETE", "/containers/"+id, nil)
  935. c.Assert(err, checker.IsNil)
  936. c.Assert(status, checker.Equals, http.StatusConflict)
  937. }
  938. func (s *DockerSuite) TestContainerApiDeleteRemoveVolume(c *check.C) {
  939. testRequires(c, DaemonIsLinux)
  940. testRequires(c, SameHostDaemon)
  941. out, _ := dockerCmd(c, "run", "-d", "-v", "/testvolume", "busybox", "top")
  942. id := strings.TrimSpace(out)
  943. c.Assert(waitRun(id), checker.IsNil)
  944. source, err := inspectMountSourceField(id, "/testvolume")
  945. _, err = os.Stat(source)
  946. c.Assert(err, checker.IsNil)
  947. status, _, err := sockRequest("DELETE", "/containers/"+id+"?v=1&force=1", nil)
  948. c.Assert(err, checker.IsNil)
  949. c.Assert(status, checker.Equals, http.StatusNoContent)
  950. _, err = os.Stat(source)
  951. c.Assert(os.IsNotExist(err), checker.True, check.Commentf("expected to get ErrNotExist error, got %v", err))
  952. }
  953. // Regression test for https://github.com/docker/docker/issues/6231
  954. func (s *DockerSuite) TestContainersApiChunkedEncoding(c *check.C) {
  955. testRequires(c, DaemonIsLinux)
  956. out, _ := dockerCmd(c, "create", "-v", "/foo", "busybox", "true")
  957. id := strings.TrimSpace(out)
  958. conn, err := sockConn(time.Duration(10 * time.Second))
  959. c.Assert(err, checker.IsNil)
  960. client := httputil.NewClientConn(conn, nil)
  961. defer client.Close()
  962. bindCfg := strings.NewReader(`{"Binds": ["/tmp:/foo"]}`)
  963. req, err := http.NewRequest("POST", "/containers/"+id+"/start", bindCfg)
  964. c.Assert(err, checker.IsNil)
  965. req.Header.Set("Content-Type", "application/json")
  966. // This is a cheat to make the http request do chunked encoding
  967. // Otherwise (just setting the Content-Encoding to chunked) net/http will overwrite
  968. // https://golang.org/src/pkg/net/http/request.go?s=11980:12172
  969. req.ContentLength = -1
  970. resp, err := client.Do(req)
  971. c.Assert(err, checker.IsNil, check.Commentf("error starting container with chunked encoding"))
  972. resp.Body.Close()
  973. c.Assert(resp.StatusCode, checker.Equals, 204)
  974. out, err = inspectFieldJSON(id, "HostConfig.Binds")
  975. c.Assert(err, checker.IsNil)
  976. var binds []string
  977. c.Assert(json.NewDecoder(strings.NewReader(out)).Decode(&binds), checker.IsNil)
  978. c.Assert(binds, checker.HasLen, 1, check.Commentf("Got unexpected binds: %v", binds))
  979. expected := "/tmp:/foo"
  980. c.Assert(binds[0], checker.Equals, expected, check.Commentf("got incorrect bind spec"))
  981. }
  982. func (s *DockerSuite) TestPostContainerStop(c *check.C) {
  983. testRequires(c, DaemonIsLinux)
  984. out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
  985. containerID := strings.TrimSpace(out)
  986. c.Assert(waitRun(containerID), checker.IsNil)
  987. statusCode, _, err := sockRequest("POST", "/containers/"+containerID+"/stop", nil)
  988. c.Assert(err, checker.IsNil)
  989. // 204 No Content is expected, not 200
  990. c.Assert(statusCode, checker.Equals, http.StatusNoContent)
  991. c.Assert(waitInspect(containerID, "{{ .State.Running }}", "false", 5*time.Second), checker.IsNil)
  992. }
  993. // #14170
  994. func (s *DockerSuite) TestPostContainersCreateWithStringOrSliceEntrypoint(c *check.C) {
  995. testRequires(c, DaemonIsLinux)
  996. config := struct {
  997. Image string
  998. Entrypoint string
  999. Cmd []string
  1000. }{"busybox", "echo", []string{"hello", "world"}}
  1001. _, _, err := sockRequest("POST", "/containers/create?name=echotest", config)
  1002. c.Assert(err, checker.IsNil)
  1003. out, _ := dockerCmd(c, "start", "-a", "echotest")
  1004. c.Assert(strings.TrimSpace(out), checker.Equals, "hello world")
  1005. config2 := struct {
  1006. Image string
  1007. Entrypoint []string
  1008. Cmd []string
  1009. }{"busybox", []string{"echo"}, []string{"hello", "world"}}
  1010. _, _, err = sockRequest("POST", "/containers/create?name=echotest2", config2)
  1011. c.Assert(err, checker.IsNil)
  1012. out, _ = dockerCmd(c, "start", "-a", "echotest2")
  1013. c.Assert(strings.TrimSpace(out), checker.Equals, "hello world")
  1014. }
  1015. // #14170
  1016. func (s *DockerSuite) TestPostContainersCreateWithStringOrSliceCmd(c *check.C) {
  1017. testRequires(c, DaemonIsLinux)
  1018. config := struct {
  1019. Image string
  1020. Entrypoint string
  1021. Cmd string
  1022. }{"busybox", "echo", "hello world"}
  1023. _, _, err := sockRequest("POST", "/containers/create?name=echotest", config)
  1024. c.Assert(err, checker.IsNil)
  1025. out, _ := dockerCmd(c, "start", "-a", "echotest")
  1026. c.Assert(strings.TrimSpace(out), checker.Equals, "hello world")
  1027. config2 := struct {
  1028. Image string
  1029. Cmd []string
  1030. }{"busybox", []string{"echo", "hello", "world"}}
  1031. _, _, err = sockRequest("POST", "/containers/create?name=echotest2", config2)
  1032. c.Assert(err, checker.IsNil)
  1033. out, _ = dockerCmd(c, "start", "-a", "echotest2")
  1034. c.Assert(strings.TrimSpace(out), checker.Equals, "hello world")
  1035. }
  1036. // regression #14318
  1037. func (s *DockerSuite) TestPostContainersCreateWithStringOrSliceCapAddDrop(c *check.C) {
  1038. testRequires(c, DaemonIsLinux)
  1039. config := struct {
  1040. Image string
  1041. CapAdd string
  1042. CapDrop string
  1043. }{"busybox", "NET_ADMIN", "SYS_ADMIN"}
  1044. status, _, err := sockRequest("POST", "/containers/create?name=capaddtest0", config)
  1045. c.Assert(err, checker.IsNil)
  1046. c.Assert(status, checker.Equals, http.StatusCreated)
  1047. config2 := struct {
  1048. Image string
  1049. CapAdd []string
  1050. CapDrop []string
  1051. }{"busybox", []string{"NET_ADMIN", "SYS_ADMIN"}, []string{"SETGID"}}
  1052. status, _, err = sockRequest("POST", "/containers/create?name=capaddtest1", config2)
  1053. c.Assert(err, checker.IsNil)
  1054. c.Assert(status, checker.Equals, http.StatusCreated)
  1055. }
  1056. // #14640
  1057. func (s *DockerSuite) TestPostContainersStartWithoutLinksInHostConfig(c *check.C) {
  1058. testRequires(c, DaemonIsLinux)
  1059. name := "test-host-config-links"
  1060. dockerCmd(c, "create", "--name", name, "busybox", "top")
  1061. hc, err := inspectFieldJSON(name, "HostConfig")
  1062. c.Assert(err, checker.IsNil)
  1063. config := `{"HostConfig":` + hc + `}`
  1064. res, b, err := sockRequestRaw("POST", "/containers/"+name+"/start", strings.NewReader(config), "application/json")
  1065. c.Assert(err, checker.IsNil)
  1066. c.Assert(res.StatusCode, checker.Equals, http.StatusNoContent)
  1067. b.Close()
  1068. }
  1069. // #14640
  1070. func (s *DockerSuite) TestPostContainersStartWithLinksInHostConfig(c *check.C) {
  1071. testRequires(c, DaemonIsLinux)
  1072. name := "test-host-config-links"
  1073. dockerCmd(c, "run", "--name", "foo", "-d", "busybox", "top")
  1074. dockerCmd(c, "create", "--name", name, "--link", "foo:bar", "busybox", "top")
  1075. hc, err := inspectFieldJSON(name, "HostConfig")
  1076. c.Assert(err, checker.IsNil)
  1077. config := `{"HostConfig":` + hc + `}`
  1078. res, b, err := sockRequestRaw("POST", "/containers/"+name+"/start", strings.NewReader(config), "application/json")
  1079. c.Assert(err, checker.IsNil)
  1080. c.Assert(res.StatusCode, checker.Equals, http.StatusNoContent)
  1081. b.Close()
  1082. }
  1083. // #14640
  1084. func (s *DockerSuite) TestPostContainersStartWithLinksInHostConfigIdLinked(c *check.C) {
  1085. testRequires(c, DaemonIsLinux)
  1086. name := "test-host-config-links"
  1087. out, _ := dockerCmd(c, "run", "--name", "link0", "-d", "busybox", "top")
  1088. id := strings.TrimSpace(out)
  1089. dockerCmd(c, "create", "--name", name, "--link", id, "busybox", "top")
  1090. hc, err := inspectFieldJSON(name, "HostConfig")
  1091. c.Assert(err, checker.IsNil)
  1092. config := `{"HostConfig":` + hc + `}`
  1093. res, b, err := sockRequestRaw("POST", "/containers/"+name+"/start", strings.NewReader(config), "application/json")
  1094. c.Assert(err, checker.IsNil)
  1095. c.Assert(res.StatusCode, checker.Equals, http.StatusNoContent)
  1096. b.Close()
  1097. }
  1098. // #14915
  1099. func (s *DockerSuite) TestContainersApiCreateNoHostConfig118(c *check.C) {
  1100. testRequires(c, DaemonIsLinux)
  1101. config := struct {
  1102. Image string
  1103. }{"busybox"}
  1104. status, _, err := sockRequest("POST", "/v1.18/containers/create", config)
  1105. c.Assert(err, checker.IsNil)
  1106. c.Assert(status, checker.Equals, http.StatusCreated)
  1107. }
  1108. // Ensure an error occurs when you have a container read-only rootfs but you
  1109. // extract an archive to a symlink in a writable volume which points to a
  1110. // directory outside of the volume.
  1111. func (s *DockerSuite) TestPutContainerArchiveErrSymlinkInVolumeToReadOnlyRootfs(c *check.C) {
  1112. // Requires local volume mount bind.
  1113. // --read-only + userns has remount issues
  1114. testRequires(c, SameHostDaemon, NotUserNamespace, DaemonIsLinux)
  1115. testVol := getTestDir(c, "test-put-container-archive-err-symlink-in-volume-to-read-only-rootfs-")
  1116. defer os.RemoveAll(testVol)
  1117. makeTestContentInDir(c, testVol)
  1118. cID := makeTestContainer(c, testContainerOptions{
  1119. readOnly: true,
  1120. volumes: defaultVolumes(testVol), // Our bind mount is at /vol2
  1121. })
  1122. defer deleteContainer(cID)
  1123. // Attempt to extract to a symlink in the volume which points to a
  1124. // directory outside the volume. This should cause an error because the
  1125. // rootfs is read-only.
  1126. query := make(url.Values, 1)
  1127. query.Set("path", "/vol2/symlinkToAbsDir")
  1128. urlPath := fmt.Sprintf("/v1.20/containers/%s/archive?%s", cID, query.Encode())
  1129. statusCode, body, err := sockRequest("PUT", urlPath, nil)
  1130. c.Assert(err, checker.IsNil)
  1131. if !isCpCannotCopyReadOnly(fmt.Errorf(string(body))) {
  1132. c.Fatalf("expected ErrContainerRootfsReadonly error, but got %d: %s", statusCode, string(body))
  1133. }
  1134. }
  1135. func (s *DockerSuite) TestContainersApiGetContainersJSONEmpty(c *check.C) {
  1136. testRequires(c, DaemonIsLinux)
  1137. status, body, err := sockRequest("GET", "/containers/json?all=1", nil)
  1138. c.Assert(err, checker.IsNil)
  1139. c.Assert(status, checker.Equals, http.StatusOK)
  1140. c.Assert(string(body), checker.Equals, "[]\n")
  1141. }
  1142. func (s *DockerSuite) TestPostContainersCreateWithWrongCpusetValues(c *check.C) {
  1143. testRequires(c, DaemonIsLinux)
  1144. c1 := struct {
  1145. Image string
  1146. CpusetCpus string
  1147. }{"busybox", "1-42,,"}
  1148. name := "wrong-cpuset-cpus"
  1149. status, body, err := sockRequest("POST", "/containers/create?name="+name, c1)
  1150. c.Assert(err, checker.IsNil)
  1151. c.Assert(status, checker.Equals, http.StatusInternalServerError)
  1152. expected := "Invalid value 1-42,, for cpuset cpus.\n"
  1153. c.Assert(string(body), checker.Equals, expected)
  1154. c2 := struct {
  1155. Image string
  1156. CpusetMems string
  1157. }{"busybox", "42-3,1--"}
  1158. name = "wrong-cpuset-mems"
  1159. status, body, err = sockRequest("POST", "/containers/create?name="+name, c2)
  1160. c.Assert(err, checker.IsNil)
  1161. c.Assert(status, checker.Equals, http.StatusInternalServerError)
  1162. expected = "Invalid value 42-3,1-- for cpuset mems.\n"
  1163. c.Assert(string(body), checker.Equals, expected)
  1164. }
  1165. func (s *DockerSuite) TestStartWithNilDNS(c *check.C) {
  1166. testRequires(c, DaemonIsLinux)
  1167. out, _ := dockerCmd(c, "create", "busybox")
  1168. containerID := strings.TrimSpace(out)
  1169. config := `{"HostConfig": {"Dns": null}}`
  1170. res, b, err := sockRequestRaw("POST", "/containers/"+containerID+"/start", strings.NewReader(config), "application/json")
  1171. c.Assert(err, checker.IsNil)
  1172. c.Assert(res.StatusCode, checker.Equals, http.StatusNoContent)
  1173. b.Close()
  1174. dns, err := inspectFieldJSON(containerID, "HostConfig.Dns")
  1175. c.Assert(err, checker.IsNil)
  1176. c.Assert(dns, checker.Equals, "[]")
  1177. }
  1178. func (s *DockerSuite) TestPostContainersCreateShmSizeNegative(c *check.C) {
  1179. testRequires(c, DaemonIsLinux)
  1180. config := map[string]interface{}{
  1181. "Image": "busybox",
  1182. "HostConfig": map[string]interface{}{"ShmSize": -1},
  1183. }
  1184. status, body, err := sockRequest("POST", "/containers/create", config)
  1185. c.Assert(err, check.IsNil)
  1186. c.Assert(status, check.Equals, http.StatusInternalServerError)
  1187. c.Assert(string(body), checker.Contains, "SHM size must be greater then 0")
  1188. }
  1189. func (s *DockerSuite) TestPostContainersCreateShmSizeHostConfigOmitted(c *check.C) {
  1190. testRequires(c, DaemonIsLinux)
  1191. var defaultSHMSize int64 = 67108864
  1192. config := map[string]interface{}{
  1193. "Image": "busybox",
  1194. "Cmd": "mount",
  1195. }
  1196. status, body, err := sockRequest("POST", "/containers/create", config)
  1197. c.Assert(err, check.IsNil)
  1198. c.Assert(status, check.Equals, http.StatusCreated)
  1199. var container types.ContainerCreateResponse
  1200. c.Assert(json.Unmarshal(body, &container), check.IsNil)
  1201. status, body, err = sockRequest("GET", "/containers/"+container.ID+"/json", nil)
  1202. c.Assert(err, check.IsNil)
  1203. c.Assert(status, check.Equals, http.StatusOK)
  1204. var containerJSON types.ContainerJSON
  1205. c.Assert(json.Unmarshal(body, &containerJSON), check.IsNil)
  1206. c.Assert(containerJSON.HostConfig.ShmSize, check.Equals, defaultSHMSize)
  1207. out, _ := dockerCmd(c, "start", "-i", containerJSON.ID)
  1208. shmRegexp := regexp.MustCompile(`shm on /dev/shm type tmpfs(.*)size=65536k`)
  1209. if !shmRegexp.MatchString(out) {
  1210. c.Fatalf("Expected shm of 64MB in mount command, got %v", out)
  1211. }
  1212. }
  1213. func (s *DockerSuite) TestPostContainersCreateShmSizeOmitted(c *check.C) {
  1214. testRequires(c, DaemonIsLinux)
  1215. config := map[string]interface{}{
  1216. "Image": "busybox",
  1217. "HostConfig": map[string]interface{}{},
  1218. "Cmd": "mount",
  1219. }
  1220. status, body, err := sockRequest("POST", "/containers/create", config)
  1221. c.Assert(err, check.IsNil)
  1222. c.Assert(status, check.Equals, http.StatusCreated)
  1223. var container types.ContainerCreateResponse
  1224. c.Assert(json.Unmarshal(body, &container), check.IsNil)
  1225. status, body, err = sockRequest("GET", "/containers/"+container.ID+"/json", nil)
  1226. c.Assert(err, check.IsNil)
  1227. c.Assert(status, check.Equals, http.StatusOK)
  1228. var containerJSON types.ContainerJSON
  1229. c.Assert(json.Unmarshal(body, &containerJSON), check.IsNil)
  1230. c.Assert(containerJSON.HostConfig.ShmSize, check.Equals, int64(67108864))
  1231. out, _ := dockerCmd(c, "start", "-i", containerJSON.ID)
  1232. shmRegexp := regexp.MustCompile(`shm on /dev/shm type tmpfs(.*)size=65536k`)
  1233. if !shmRegexp.MatchString(out) {
  1234. c.Fatalf("Expected shm of 64MB in mount command, got %v", out)
  1235. }
  1236. }
  1237. func (s *DockerSuite) TestPostContainersCreateWithShmSize(c *check.C) {
  1238. testRequires(c, DaemonIsLinux)
  1239. config := map[string]interface{}{
  1240. "Image": "busybox",
  1241. "Cmd": "mount",
  1242. "HostConfig": map[string]interface{}{"ShmSize": 1073741824},
  1243. }
  1244. status, body, err := sockRequest("POST", "/containers/create", config)
  1245. c.Assert(err, check.IsNil)
  1246. c.Assert(status, check.Equals, http.StatusCreated)
  1247. var container types.ContainerCreateResponse
  1248. c.Assert(json.Unmarshal(body, &container), check.IsNil)
  1249. status, body, err = sockRequest("GET", "/containers/"+container.ID+"/json", nil)
  1250. c.Assert(err, check.IsNil)
  1251. c.Assert(status, check.Equals, http.StatusOK)
  1252. var containerJSON types.ContainerJSON
  1253. c.Assert(json.Unmarshal(body, &containerJSON), check.IsNil)
  1254. c.Assert(containerJSON.HostConfig.ShmSize, check.Equals, int64(1073741824))
  1255. out, _ := dockerCmd(c, "start", "-i", containerJSON.ID)
  1256. shmRegex := regexp.MustCompile(`shm on /dev/shm type tmpfs(.*)size=1048576k`)
  1257. if !shmRegex.MatchString(out) {
  1258. c.Fatalf("Expected shm of 1GB in mount command, got %v", out)
  1259. }
  1260. }
  1261. func (s *DockerSuite) TestPostContainersCreateMemorySwappinessHostConfigOmitted(c *check.C) {
  1262. testRequires(c, DaemonIsLinux)
  1263. config := map[string]interface{}{
  1264. "Image": "busybox",
  1265. }
  1266. status, body, err := sockRequest("POST", "/containers/create", config)
  1267. c.Assert(err, check.IsNil)
  1268. c.Assert(status, check.Equals, http.StatusCreated)
  1269. var container types.ContainerCreateResponse
  1270. c.Assert(json.Unmarshal(body, &container), check.IsNil)
  1271. status, body, err = sockRequest("GET", "/containers/"+container.ID+"/json", nil)
  1272. c.Assert(err, check.IsNil)
  1273. c.Assert(status, check.Equals, http.StatusOK)
  1274. var containerJSON types.ContainerJSON
  1275. c.Assert(json.Unmarshal(body, &containerJSON), check.IsNil)
  1276. c.Assert(*containerJSON.HostConfig.MemorySwappiness, check.Equals, int64(-1))
  1277. }
  1278. // check validation is done daemon side and not only in cli
  1279. func (s *DockerSuite) TestPostContainersCreateWithOomScoreAdjInvalidRange(c *check.C) {
  1280. testRequires(c, DaemonIsLinux)
  1281. config := struct {
  1282. Image string
  1283. OomScoreAdj int
  1284. }{"busybox", 1001}
  1285. name := "oomscoreadj-over"
  1286. status, b, err := sockRequest("POST", "/containers/create?name="+name, config)
  1287. c.Assert(err, check.IsNil)
  1288. c.Assert(status, check.Equals, http.StatusInternalServerError)
  1289. expected := "Invalid value 1001, range for oom score adj is [-1000, 1000]."
  1290. if !strings.Contains(string(b), expected) {
  1291. c.Fatalf("Expected output to contain %q, got %q", expected, string(b))
  1292. }
  1293. config = struct {
  1294. Image string
  1295. OomScoreAdj int
  1296. }{"busybox", -1001}
  1297. name = "oomscoreadj-low"
  1298. status, b, err = sockRequest("POST", "/containers/create?name="+name, config)
  1299. c.Assert(err, check.IsNil)
  1300. c.Assert(status, check.Equals, http.StatusInternalServerError)
  1301. expected = "Invalid value -1001, range for oom score adj is [-1000, 1000]."
  1302. if !strings.Contains(string(b), expected) {
  1303. c.Fatalf("Expected output to contain %q, got %q", expected, string(b))
  1304. }
  1305. }