docker_cli_ps_test.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "os/exec"
  7. "path/filepath"
  8. "reflect"
  9. "strconv"
  10. "strings"
  11. "time"
  12. "github.com/go-check/check"
  13. )
  14. func (s *DockerSuite) TestPsListContainers(c *check.C) {
  15. out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
  16. firstID := strings.TrimSpace(out)
  17. out, _ = dockerCmd(c, "run", "-d", "busybox", "top")
  18. secondID := strings.TrimSpace(out)
  19. // not long running
  20. out, _ = dockerCmd(c, "run", "-d", "busybox", "true")
  21. thirdID := strings.TrimSpace(out)
  22. out, _ = dockerCmd(c, "run", "-d", "busybox", "top")
  23. fourthID := strings.TrimSpace(out)
  24. // make sure the second is running
  25. c.Assert(waitRun(secondID), check.IsNil)
  26. // make sure third one is not running
  27. dockerCmd(c, "wait", thirdID)
  28. // make sure the forth is running
  29. c.Assert(waitRun(fourthID), check.IsNil)
  30. // all
  31. out, _ = dockerCmd(c, "ps", "-a")
  32. if !assertContainerList(out, []string{fourthID, thirdID, secondID, firstID}) {
  33. c.Errorf("Container list is not in the correct order: %s", out)
  34. }
  35. // running
  36. out, _ = dockerCmd(c, "ps")
  37. if !assertContainerList(out, []string{fourthID, secondID, firstID}) {
  38. c.Errorf("Container list is not in the correct order: %s", out)
  39. }
  40. // from here all flag '-a' is ignored
  41. // limit
  42. out, _ = dockerCmd(c, "ps", "-n=2", "-a")
  43. expected := []string{fourthID, thirdID}
  44. if !assertContainerList(out, expected) {
  45. c.Errorf("Container list is not in the correct order: %s", out)
  46. }
  47. out, _ = dockerCmd(c, "ps", "-n=2")
  48. if !assertContainerList(out, expected) {
  49. c.Errorf("Container list is not in the correct order: %s", out)
  50. }
  51. // since
  52. out, _ = dockerCmd(c, "ps", "--since", firstID, "-a")
  53. expected = []string{fourthID, thirdID, secondID}
  54. if !assertContainerList(out, expected) {
  55. c.Errorf("Container list is not in the correct order: %s", out)
  56. }
  57. out, _ = dockerCmd(c, "ps", "--since", firstID)
  58. if !assertContainerList(out, expected) {
  59. c.Errorf("Container list is not in the correct order: %s", out)
  60. }
  61. // before
  62. out, _ = dockerCmd(c, "ps", "--before", thirdID, "-a")
  63. expected = []string{secondID, firstID}
  64. if !assertContainerList(out, expected) {
  65. c.Errorf("Container list is not in the correct order: %s", out)
  66. }
  67. out, _ = dockerCmd(c, "ps", "--before", thirdID)
  68. if !assertContainerList(out, expected) {
  69. c.Errorf("Container list is not in the correct order: %s", out)
  70. }
  71. // since & before
  72. out, _ = dockerCmd(c, "ps", "--since", firstID, "--before", fourthID, "-a")
  73. expected = []string{thirdID, secondID}
  74. if !assertContainerList(out, expected) {
  75. c.Errorf("Container list is not in the correct order: %s", out)
  76. }
  77. out, _ = dockerCmd(c, "ps", "--since", firstID, "--before", fourthID)
  78. if !assertContainerList(out, expected) {
  79. c.Errorf("Container list is not in the correct order: %s", out)
  80. }
  81. // since & limit
  82. out, _ = dockerCmd(c, "ps", "--since", firstID, "-n=2", "-a")
  83. expected = []string{fourthID, thirdID}
  84. if !assertContainerList(out, expected) {
  85. c.Errorf("Container list is not in the correct order: %s", out)
  86. }
  87. out, _ = dockerCmd(c, "ps", "--since", firstID, "-n=2")
  88. if !assertContainerList(out, expected) {
  89. c.Errorf("Container list is not in the correct order: %s", out)
  90. }
  91. // before & limit
  92. out, _ = dockerCmd(c, "ps", "--before", fourthID, "-n=1", "-a")
  93. expected = []string{thirdID}
  94. if !assertContainerList(out, expected) {
  95. c.Errorf("Container list is not in the correct order: %s", out)
  96. }
  97. out, _ = dockerCmd(c, "ps", "--before", fourthID, "-n=1")
  98. if !assertContainerList(out, expected) {
  99. c.Errorf("Container list is not in the correct order: %s", out)
  100. }
  101. out, _ = dockerCmd(c, "ps", "--since", firstID, "--before", fourthID, "-n=1", "-a")
  102. expected = []string{thirdID}
  103. if !assertContainerList(out, expected) {
  104. c.Errorf("Container list is not in the correct order: %s", out)
  105. }
  106. out, _ = dockerCmd(c, "ps", "--since", firstID, "--before", fourthID, "-n=1")
  107. if !assertContainerList(out, expected) {
  108. c.Errorf("Container list is not in the correct order: %s", out)
  109. }
  110. }
  111. func assertContainerList(out string, expected []string) bool {
  112. lines := strings.Split(strings.Trim(out, "\n "), "\n")
  113. if len(lines)-1 != len(expected) {
  114. return false
  115. }
  116. containerIDIndex := strings.Index(lines[0], "CONTAINER ID")
  117. for i := 0; i < len(expected); i++ {
  118. foundID := lines[i+1][containerIDIndex : containerIDIndex+12]
  119. if foundID != expected[i][:12] {
  120. return false
  121. }
  122. }
  123. return true
  124. }
  125. func (s *DockerSuite) TestPsListContainersSize(c *check.C) {
  126. dockerCmd(c, "run", "-d", "busybox", "echo", "hello")
  127. baseOut, _ := dockerCmd(c, "ps", "-s", "-n=1")
  128. baseLines := strings.Split(strings.Trim(baseOut, "\n "), "\n")
  129. baseSizeIndex := strings.Index(baseLines[0], "SIZE")
  130. baseFoundsize := baseLines[1][baseSizeIndex:]
  131. baseBytes, err := strconv.Atoi(strings.Split(baseFoundsize, " ")[0])
  132. if err != nil {
  133. c.Fatal(err)
  134. }
  135. name := "test_size"
  136. out, _ := dockerCmd(c, "run", "--name", name, "busybox", "sh", "-c", "echo 1 > test")
  137. id, err := getIDByName(name)
  138. if err != nil {
  139. c.Fatal(err)
  140. }
  141. runCmd := exec.Command(dockerBinary, "ps", "-s", "-n=1")
  142. wait := make(chan struct{})
  143. go func() {
  144. out, _, err = runCommandWithOutput(runCmd)
  145. close(wait)
  146. }()
  147. select {
  148. case <-wait:
  149. case <-time.After(3 * time.Second):
  150. c.Fatalf("Calling \"docker ps -s\" timed out!")
  151. }
  152. if err != nil {
  153. c.Fatal(out, err)
  154. }
  155. lines := strings.Split(strings.Trim(out, "\n "), "\n")
  156. if len(lines) != 2 {
  157. c.Fatalf("Expected 2 lines for 'ps -s -n=1' output, got %d", len(lines))
  158. }
  159. sizeIndex := strings.Index(lines[0], "SIZE")
  160. idIndex := strings.Index(lines[0], "CONTAINER ID")
  161. foundID := lines[1][idIndex : idIndex+12]
  162. if foundID != id[:12] {
  163. c.Fatalf("Expected id %s, got %s", id[:12], foundID)
  164. }
  165. expectedSize := fmt.Sprintf("%d B", (2 + baseBytes))
  166. foundSize := lines[1][sizeIndex:]
  167. if !strings.Contains(foundSize, expectedSize) {
  168. c.Fatalf("Expected size %q, got %q", expectedSize, foundSize)
  169. }
  170. }
  171. func (s *DockerSuite) TestPsListContainersFilterStatus(c *check.C) {
  172. // FIXME: this should test paused, but it makes things hang and its wonky
  173. // this is because paused containers can't be controlled by signals
  174. // start exited container
  175. out, _ := dockerCmd(c, "run", "-d", "busybox")
  176. firstID := strings.TrimSpace(out)
  177. // make sure the exited cintainer is not running
  178. dockerCmd(c, "wait", firstID)
  179. // start running container
  180. out, _ = dockerCmd(c, "run", "-itd", "busybox")
  181. secondID := strings.TrimSpace(out)
  182. // filter containers by exited
  183. out, _ = dockerCmd(c, "ps", "-q", "--filter=status=exited")
  184. containerOut := strings.TrimSpace(out)
  185. if containerOut != firstID[:12] {
  186. c.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out)
  187. }
  188. out, _ = dockerCmd(c, "ps", "-a", "-q", "--filter=status=running")
  189. containerOut = strings.TrimSpace(out)
  190. if containerOut != secondID[:12] {
  191. c.Fatalf("Expected id %s, got %s for running filter, output: %q", secondID[:12], containerOut, out)
  192. }
  193. out, _, _ = dockerCmdWithTimeout(time.Second*60, "ps", "-a", "-q", "--filter=status=rubbish")
  194. if !strings.Contains(out, "Unrecognised filter value for status") {
  195. c.Fatalf("Expected error response due to invalid status filter output: %q", out)
  196. }
  197. }
  198. func (s *DockerSuite) TestPsListContainersFilterID(c *check.C) {
  199. // start container
  200. out, _ := dockerCmd(c, "run", "-d", "busybox")
  201. firstID := strings.TrimSpace(out)
  202. // start another container
  203. dockerCmd(c, "run", "-d", "busybox", "top")
  204. // filter containers by id
  205. out, _ = dockerCmd(c, "ps", "-a", "-q", "--filter=id="+firstID)
  206. containerOut := strings.TrimSpace(out)
  207. if containerOut != firstID[:12] {
  208. c.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out)
  209. }
  210. }
  211. func (s *DockerSuite) TestPsListContainersFilterName(c *check.C) {
  212. // start container
  213. out, _ := dockerCmd(c, "run", "-d", "--name=a_name_to_match", "busybox")
  214. firstID := strings.TrimSpace(out)
  215. // start another container
  216. dockerCmd(c, "run", "-d", "--name=b_name_to_match", "busybox", "top")
  217. // filter containers by name
  218. out, _ = dockerCmd(c, "ps", "-a", "-q", "--filter=name=a_name_to_match")
  219. containerOut := strings.TrimSpace(out)
  220. if containerOut != firstID[:12] {
  221. c.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out)
  222. }
  223. }
  224. func (s *DockerSuite) TestPsListContainersFilterLabel(c *check.C) {
  225. // start container
  226. out, _ := dockerCmd(c, "run", "-d", "-l", "match=me", "-l", "second=tag", "busybox")
  227. firstID := strings.TrimSpace(out)
  228. // start another container
  229. out, _ = dockerCmd(c, "run", "-d", "-l", "match=me too", "busybox")
  230. secondID := strings.TrimSpace(out)
  231. // start third container
  232. out, _ = dockerCmd(c, "run", "-d", "-l", "nomatch=me", "busybox")
  233. thirdID := strings.TrimSpace(out)
  234. // filter containers by exact match
  235. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=label=match=me")
  236. containerOut := strings.TrimSpace(out)
  237. if containerOut != firstID {
  238. c.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID, containerOut, out)
  239. }
  240. // filter containers by two labels
  241. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=label=match=me", "--filter=label=second=tag")
  242. containerOut = strings.TrimSpace(out)
  243. if containerOut != firstID {
  244. c.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID, containerOut, out)
  245. }
  246. // filter containers by two labels, but expect not found because of AND behavior
  247. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=label=match=me", "--filter=label=second=tag-no")
  248. containerOut = strings.TrimSpace(out)
  249. if containerOut != "" {
  250. c.Fatalf("Expected nothing, got %s for exited filter, output: %q", containerOut, out)
  251. }
  252. // filter containers by exact key
  253. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=label=match")
  254. containerOut = strings.TrimSpace(out)
  255. if (!strings.Contains(containerOut, firstID) || !strings.Contains(containerOut, secondID)) || strings.Contains(containerOut, thirdID) {
  256. c.Fatalf("Expected ids %s,%s, got %s for exited filter, output: %q", firstID, secondID, containerOut, out)
  257. }
  258. }
  259. func (s *DockerSuite) TestPsListContainersFilterExited(c *check.C) {
  260. dockerCmd(c, "run", "-d", "--name", "top", "busybox", "top")
  261. dockerCmd(c, "run", "--name", "zero1", "busybox", "true")
  262. firstZero, err := getIDByName("zero1")
  263. if err != nil {
  264. c.Fatal(err)
  265. }
  266. dockerCmd(c, "run", "--name", "zero2", "busybox", "true")
  267. secondZero, err := getIDByName("zero2")
  268. if err != nil {
  269. c.Fatal(err)
  270. }
  271. if out, _, err := dockerCmdWithError("run", "--name", "nonzero1", "busybox", "false"); err == nil {
  272. c.Fatal("Should fail.", out, err)
  273. }
  274. firstNonZero, err := getIDByName("nonzero1")
  275. if err != nil {
  276. c.Fatal(err)
  277. }
  278. if out, _, err := dockerCmdWithError("run", "--name", "nonzero2", "busybox", "false"); err == nil {
  279. c.Fatal("Should fail.", out, err)
  280. }
  281. secondNonZero, err := getIDByName("nonzero2")
  282. if err != nil {
  283. c.Fatal(err)
  284. }
  285. // filter containers by exited=0
  286. out, _ := dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=exited=0")
  287. ids := strings.Split(strings.TrimSpace(out), "\n")
  288. if len(ids) != 2 {
  289. c.Fatalf("Should be 2 zero exited containers got %d: %s", len(ids), out)
  290. }
  291. if ids[0] != secondZero {
  292. c.Fatalf("First in list should be %q, got %q", secondZero, ids[0])
  293. }
  294. if ids[1] != firstZero {
  295. c.Fatalf("Second in list should be %q, got %q", firstZero, ids[1])
  296. }
  297. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=exited=1")
  298. ids = strings.Split(strings.TrimSpace(out), "\n")
  299. if len(ids) != 2 {
  300. c.Fatalf("Should be 2 zero exited containers got %d", len(ids))
  301. }
  302. if ids[0] != secondNonZero {
  303. c.Fatalf("First in list should be %q, got %q", secondNonZero, ids[0])
  304. }
  305. if ids[1] != firstNonZero {
  306. c.Fatalf("Second in list should be %q, got %q", firstNonZero, ids[1])
  307. }
  308. }
  309. func (s *DockerSuite) TestPsRightTagName(c *check.C) {
  310. tag := "asybox:shmatest"
  311. dockerCmd(c, "tag", "busybox", tag)
  312. var id1 string
  313. out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
  314. id1 = strings.TrimSpace(string(out))
  315. var id2 string
  316. out, _ = dockerCmd(c, "run", "-d", tag, "top")
  317. id2 = strings.TrimSpace(string(out))
  318. var imageID string
  319. out, _ = dockerCmd(c, "inspect", "-f", "{{.Id}}", "busybox")
  320. imageID = strings.TrimSpace(string(out))
  321. var id3 string
  322. out, _ = dockerCmd(c, "run", "-d", imageID, "top")
  323. id3 = strings.TrimSpace(string(out))
  324. out, _ = dockerCmd(c, "ps", "--no-trunc")
  325. lines := strings.Split(strings.TrimSpace(string(out)), "\n")
  326. // skip header
  327. lines = lines[1:]
  328. if len(lines) != 3 {
  329. c.Fatalf("There should be 3 running container, got %d", len(lines))
  330. }
  331. for _, line := range lines {
  332. f := strings.Fields(line)
  333. switch f[0] {
  334. case id1:
  335. if f[1] != "busybox" {
  336. c.Fatalf("Expected %s tag for id %s, got %s", "busybox", id1, f[1])
  337. }
  338. case id2:
  339. if f[1] != tag {
  340. c.Fatalf("Expected %s tag for id %s, got %s", tag, id2, f[1])
  341. }
  342. case id3:
  343. if f[1] != imageID {
  344. c.Fatalf("Expected %s imageID for id %s, got %s", tag, id3, f[1])
  345. }
  346. default:
  347. c.Fatalf("Unexpected id %s, expected %s and %s and %s", f[0], id1, id2, id3)
  348. }
  349. }
  350. }
  351. func (s *DockerSuite) TestPsLinkedWithNoTrunc(c *check.C) {
  352. dockerCmd(c, "run", "--name=first", "-d", "busybox", "top")
  353. dockerCmd(c, "run", "--name=second", "--link=first:first", "-d", "busybox", "top")
  354. out, _ := dockerCmd(c, "ps", "--no-trunc")
  355. lines := strings.Split(strings.TrimSpace(string(out)), "\n")
  356. // strip header
  357. lines = lines[1:]
  358. expected := []string{"second", "first,second/first"}
  359. var names []string
  360. for _, l := range lines {
  361. fields := strings.Fields(l)
  362. names = append(names, fields[len(fields)-1])
  363. }
  364. if !reflect.DeepEqual(expected, names) {
  365. c.Fatalf("Expected array: %v, got: %v", expected, names)
  366. }
  367. }
  368. func (s *DockerSuite) TestPsGroupPortRange(c *check.C) {
  369. portRange := "3800-3900"
  370. dockerCmd(c, "run", "-d", "--name", "porttest", "-p", portRange+":"+portRange, "busybox", "top")
  371. out, _ := dockerCmd(c, "ps")
  372. // check that the port range is in the output
  373. if !strings.Contains(string(out), portRange) {
  374. c.Fatalf("docker ps output should have had the port range %q: %s", portRange, string(out))
  375. }
  376. }
  377. func (s *DockerSuite) TestPsWithSize(c *check.C) {
  378. dockerCmd(c, "run", "-d", "--name", "sizetest", "busybox", "top")
  379. out, _ := dockerCmd(c, "ps", "--size")
  380. if !strings.Contains(out, "virtual") {
  381. c.Fatalf("docker ps with --size should show virtual size of container")
  382. }
  383. }
  384. func (s *DockerSuite) TestPsListContainersFilterCreated(c *check.C) {
  385. // create a container
  386. out, _ := dockerCmd(c, "create", "busybox")
  387. cID := strings.TrimSpace(out)
  388. shortCID := cID[:12]
  389. // Make sure it DOESN'T show up w/o a '-a' for normal 'ps'
  390. out, _ = dockerCmd(c, "ps", "-q")
  391. if strings.Contains(out, shortCID) {
  392. c.Fatalf("Should have not seen '%s' in ps output:\n%s", shortCID, out)
  393. }
  394. // Make sure it DOES show up as 'Created' for 'ps -a'
  395. out, _ = dockerCmd(c, "ps", "-a")
  396. hits := 0
  397. for _, line := range strings.Split(out, "\n") {
  398. if !strings.Contains(line, shortCID) {
  399. continue
  400. }
  401. hits++
  402. if !strings.Contains(line, "Created") {
  403. c.Fatalf("Missing 'Created' on '%s'", line)
  404. }
  405. }
  406. if hits != 1 {
  407. c.Fatalf("Should have seen '%s' in ps -a output once:%d\n%s", shortCID, hits, out)
  408. }
  409. // filter containers by 'create' - note, no -a needed
  410. out, _ = dockerCmd(c, "ps", "-q", "-f", "status=created")
  411. containerOut := strings.TrimSpace(out)
  412. if !strings.HasPrefix(cID, containerOut) {
  413. c.Fatalf("Expected id %s, got %s for filter, out: %s", cID, containerOut, out)
  414. }
  415. }
  416. func (s *DockerSuite) TestPsFormatMultiNames(c *check.C) {
  417. //create 2 containers and link them
  418. dockerCmd(c, "run", "--name=child", "-d", "busybox", "top")
  419. dockerCmd(c, "run", "--name=parent", "--link=child:linkedone", "-d", "busybox", "top")
  420. //use the new format capabilities to only list the names and --no-trunc to get all names
  421. out, _ := dockerCmd(c, "ps", "--format", "{{.Names}}", "--no-trunc")
  422. lines := strings.Split(strings.TrimSpace(string(out)), "\n")
  423. expected := []string{"parent", "child,parent/linkedone"}
  424. var names []string
  425. for _, l := range lines {
  426. names = append(names, l)
  427. }
  428. if !reflect.DeepEqual(expected, names) {
  429. c.Fatalf("Expected array with non-truncated names: %v, got: %v", expected, names)
  430. }
  431. //now list without turning off truncation and make sure we only get the non-link names
  432. out, _ = dockerCmd(c, "ps", "--format", "{{.Names}}")
  433. lines = strings.Split(strings.TrimSpace(string(out)), "\n")
  434. expected = []string{"parent", "child"}
  435. var truncNames []string
  436. for _, l := range lines {
  437. truncNames = append(truncNames, l)
  438. }
  439. if !reflect.DeepEqual(expected, truncNames) {
  440. c.Fatalf("Expected array with truncated names: %v, got: %v", expected, truncNames)
  441. }
  442. }
  443. func (s *DockerSuite) TestPsFormatHeaders(c *check.C) {
  444. // make sure no-container "docker ps" still prints the header row
  445. out, _ := dockerCmd(c, "ps", "--format", "table {{.ID}}")
  446. if out != "CONTAINER ID\n" {
  447. c.Fatalf(`Expected 'CONTAINER ID\n', got %v`, out)
  448. }
  449. // verify that "docker ps" with a container still prints the header row also
  450. dockerCmd(c, "run", "--name=test", "-d", "busybox", "top")
  451. out, _ = dockerCmd(c, "ps", "--format", "table {{.Names}}")
  452. if out != "NAMES\ntest\n" {
  453. c.Fatalf(`Expected 'NAMES\ntest\n', got %v`, out)
  454. }
  455. }
  456. func (s *DockerSuite) TestPsDefaultFormatAndQuiet(c *check.C) {
  457. config := `{
  458. "psFormat": "{{ .ID }} default"
  459. }`
  460. d, err := ioutil.TempDir("", "integration-cli-")
  461. c.Assert(err, check.IsNil)
  462. defer os.RemoveAll(d)
  463. err = ioutil.WriteFile(filepath.Join(d, "config.json"), []byte(config), 0644)
  464. c.Assert(err, check.IsNil)
  465. out, _ := dockerCmd(c, "run", "--name=test", "-d", "busybox", "top")
  466. id := strings.TrimSpace(out)
  467. out, _ = dockerCmd(c, "--config", d, "ps", "-q")
  468. if !strings.HasPrefix(id, strings.TrimSpace(out)) {
  469. c.Fatalf("Expected to print only the container id, got %v\n", out)
  470. }
  471. }
  472. // Test for GitHub issue #12595
  473. func (s *DockerSuite) TestPsImageIDAfterUpdate(c *check.C) {
  474. originalImageName := "busybox:TestPsImageIDAfterUpdate-original"
  475. updatedImageName := "busybox:TestPsImageIDAfterUpdate-updated"
  476. runCmd := exec.Command(dockerBinary, "tag", "busybox:latest", originalImageName)
  477. out, _, err := runCommandWithOutput(runCmd)
  478. c.Assert(err, check.IsNil)
  479. originalImageID, err := getIDByName(originalImageName)
  480. c.Assert(err, check.IsNil)
  481. runCmd = exec.Command(dockerBinary, "run", "-d", originalImageName, "top")
  482. out, _, err = runCommandWithOutput(runCmd)
  483. c.Assert(err, check.IsNil)
  484. containerID := strings.TrimSpace(out)
  485. linesOut, err := exec.Command(dockerBinary, "ps", "--no-trunc").CombinedOutput()
  486. c.Assert(err, check.IsNil)
  487. lines := strings.Split(strings.TrimSpace(string(linesOut)), "\n")
  488. // skip header
  489. lines = lines[1:]
  490. c.Assert(len(lines), check.Equals, 1)
  491. for _, line := range lines {
  492. f := strings.Fields(line)
  493. c.Assert(f[1], check.Equals, originalImageName)
  494. }
  495. runCmd = exec.Command(dockerBinary, "commit", containerID, updatedImageName)
  496. out, _, err = runCommandWithOutput(runCmd)
  497. c.Assert(err, check.IsNil)
  498. runCmd = exec.Command(dockerBinary, "tag", "-f", updatedImageName, originalImageName)
  499. out, _, err = runCommandWithOutput(runCmd)
  500. c.Assert(err, check.IsNil)
  501. linesOut, err = exec.Command(dockerBinary, "ps", "--no-trunc").CombinedOutput()
  502. c.Assert(err, check.IsNil)
  503. lines = strings.Split(strings.TrimSpace(string(linesOut)), "\n")
  504. // skip header
  505. lines = lines[1:]
  506. c.Assert(len(lines), check.Equals, 1)
  507. for _, line := range lines {
  508. f := strings.Fields(line)
  509. c.Assert(f[1], check.Equals, originalImageID)
  510. }
  511. }