docker_cli_ps_test.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. package main
  2. import (
  3. "fmt"
  4. "os/exec"
  5. "reflect"
  6. "strconv"
  7. "strings"
  8. "testing"
  9. "time"
  10. )
  11. func TestPsListContainers(t *testing.T) {
  12. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
  13. out, _, err := runCommandWithOutput(runCmd)
  14. if err != nil {
  15. t.Fatal(out, err)
  16. }
  17. firstID := stripTrailingCharacters(out)
  18. runCmd = exec.Command(dockerBinary, "run", "-d", "busybox", "top")
  19. out, _, err = runCommandWithOutput(runCmd)
  20. if err != nil {
  21. t.Fatal(out, err)
  22. }
  23. secondID := stripTrailingCharacters(out)
  24. // not long running
  25. runCmd = exec.Command(dockerBinary, "run", "-d", "busybox", "true")
  26. out, _, err = runCommandWithOutput(runCmd)
  27. if err != nil {
  28. t.Fatal(out, err)
  29. }
  30. thirdID := stripTrailingCharacters(out)
  31. runCmd = exec.Command(dockerBinary, "run", "-d", "busybox", "top")
  32. out, _, err = runCommandWithOutput(runCmd)
  33. if err != nil {
  34. t.Fatal(out, err)
  35. }
  36. fourthID := stripTrailingCharacters(out)
  37. // make sure third one is not running
  38. runCmd = exec.Command(dockerBinary, "wait", thirdID)
  39. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  40. t.Fatal(out, err)
  41. }
  42. // all
  43. runCmd = exec.Command(dockerBinary, "ps", "-a")
  44. out, _, err = runCommandWithOutput(runCmd)
  45. if err != nil {
  46. t.Fatal(out, err)
  47. }
  48. if !assertContainerList(out, []string{fourthID, thirdID, secondID, firstID}) {
  49. t.Error("Container list is not in the correct order")
  50. }
  51. // running
  52. runCmd = exec.Command(dockerBinary, "ps")
  53. out, _, err = runCommandWithOutput(runCmd)
  54. if err != nil {
  55. t.Fatal(out, err)
  56. }
  57. if !assertContainerList(out, []string{fourthID, secondID, firstID}) {
  58. t.Error("Container list is not in the correct order")
  59. }
  60. // from here all flag '-a' is ignored
  61. // limit
  62. runCmd = exec.Command(dockerBinary, "ps", "-n=2", "-a")
  63. out, _, err = runCommandWithOutput(runCmd)
  64. if err != nil {
  65. t.Fatal(out, err)
  66. }
  67. expected := []string{fourthID, thirdID}
  68. if !assertContainerList(out, expected) {
  69. t.Error("Container list is not in the correct order")
  70. }
  71. runCmd = exec.Command(dockerBinary, "ps", "-n=2")
  72. out, _, err = runCommandWithOutput(runCmd)
  73. if err != nil {
  74. t.Fatal(out, err)
  75. }
  76. if !assertContainerList(out, expected) {
  77. t.Error("Container list is not in the correct order")
  78. }
  79. // since
  80. runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "-a")
  81. out, _, err = runCommandWithOutput(runCmd)
  82. if err != nil {
  83. t.Fatal(out, err)
  84. }
  85. expected = []string{fourthID, thirdID, secondID}
  86. if !assertContainerList(out, expected) {
  87. t.Error("Container list is not in the correct order")
  88. }
  89. runCmd = exec.Command(dockerBinary, "ps", "--since", firstID)
  90. out, _, err = runCommandWithOutput(runCmd)
  91. if err != nil {
  92. t.Fatal(out, err)
  93. }
  94. if !assertContainerList(out, expected) {
  95. t.Error("Container list is not in the correct order")
  96. }
  97. // before
  98. runCmd = exec.Command(dockerBinary, "ps", "--before", thirdID, "-a")
  99. out, _, err = runCommandWithOutput(runCmd)
  100. if err != nil {
  101. t.Fatal(out, err)
  102. }
  103. expected = []string{secondID, firstID}
  104. if !assertContainerList(out, expected) {
  105. t.Error("Container list is not in the correct order")
  106. }
  107. runCmd = exec.Command(dockerBinary, "ps", "--before", thirdID)
  108. out, _, err = runCommandWithOutput(runCmd)
  109. if err != nil {
  110. t.Fatal(out, err)
  111. }
  112. if !assertContainerList(out, expected) {
  113. t.Error("Container list is not in the correct order")
  114. }
  115. // since & before
  116. runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "--before", fourthID, "-a")
  117. out, _, err = runCommandWithOutput(runCmd)
  118. if err != nil {
  119. t.Fatal(out, err)
  120. }
  121. expected = []string{thirdID, secondID}
  122. if !assertContainerList(out, expected) {
  123. t.Error("Container list is not in the correct order")
  124. }
  125. runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "--before", fourthID)
  126. out, _, err = runCommandWithOutput(runCmd)
  127. if err != nil {
  128. t.Fatal(out, err)
  129. }
  130. if !assertContainerList(out, expected) {
  131. t.Error("Container list is not in the correct order")
  132. }
  133. // since & limit
  134. runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "-n=2", "-a")
  135. out, _, err = runCommandWithOutput(runCmd)
  136. if err != nil {
  137. t.Fatal(out, err)
  138. }
  139. expected = []string{fourthID, thirdID}
  140. if !assertContainerList(out, expected) {
  141. t.Error("Container list is not in the correct order")
  142. }
  143. runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "-n=2")
  144. out, _, err = runCommandWithOutput(runCmd)
  145. if err != nil {
  146. t.Fatal(out, err)
  147. }
  148. if !assertContainerList(out, expected) {
  149. t.Error("Container list is not in the correct order")
  150. }
  151. // before & limit
  152. runCmd = exec.Command(dockerBinary, "ps", "--before", fourthID, "-n=1", "-a")
  153. out, _, err = runCommandWithOutput(runCmd)
  154. if err != nil {
  155. t.Fatal(out, err)
  156. }
  157. expected = []string{thirdID}
  158. if !assertContainerList(out, expected) {
  159. t.Error("Container list is not in the correct order")
  160. }
  161. runCmd = exec.Command(dockerBinary, "ps", "--before", fourthID, "-n=1")
  162. out, _, err = runCommandWithOutput(runCmd)
  163. if err != nil {
  164. t.Fatal(out, err)
  165. }
  166. if !assertContainerList(out, expected) {
  167. t.Error("Container list is not in the correct order")
  168. }
  169. // since & before & limit
  170. runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "--before", fourthID, "-n=1", "-a")
  171. out, _, err = runCommandWithOutput(runCmd)
  172. if err != nil {
  173. t.Fatal(out, err)
  174. }
  175. expected = []string{thirdID}
  176. if !assertContainerList(out, expected) {
  177. t.Error("Container list is not in the correct order")
  178. }
  179. runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "--before", fourthID, "-n=1")
  180. out, _, err = runCommandWithOutput(runCmd)
  181. if err != nil {
  182. t.Fatal(out, err)
  183. }
  184. if !assertContainerList(out, expected) {
  185. t.Error("Container list is not in the correct order")
  186. }
  187. deleteAllContainers()
  188. logDone("ps - test ps options")
  189. }
  190. func assertContainerList(out string, expected []string) bool {
  191. lines := strings.Split(strings.Trim(out, "\n "), "\n")
  192. if len(lines)-1 != len(expected) {
  193. return false
  194. }
  195. containerIDIndex := strings.Index(lines[0], "CONTAINER ID")
  196. for i := 0; i < len(expected); i++ {
  197. foundID := lines[i+1][containerIDIndex : containerIDIndex+12]
  198. if foundID != expected[i][:12] {
  199. return false
  200. }
  201. }
  202. return true
  203. }
  204. func TestPsListContainersSize(t *testing.T) {
  205. cmd := exec.Command(dockerBinary, "run", "-d", "busybox", "echo", "hello")
  206. runCommandWithOutput(cmd)
  207. cmd = exec.Command(dockerBinary, "ps", "-s", "-n=1")
  208. base_out, _, err := runCommandWithOutput(cmd)
  209. base_lines := strings.Split(strings.Trim(base_out, "\n "), "\n")
  210. base_sizeIndex := strings.Index(base_lines[0], "SIZE")
  211. base_foundSize := base_lines[1][base_sizeIndex:]
  212. base_bytes, err := strconv.Atoi(strings.Split(base_foundSize, " ")[0])
  213. if err != nil {
  214. t.Fatal(err)
  215. }
  216. name := "test_size"
  217. runCmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "sh", "-c", "echo 1 > test")
  218. out, _, err := runCommandWithOutput(runCmd)
  219. if err != nil {
  220. t.Fatal(out, err)
  221. }
  222. id, err := getIDByName(name)
  223. if err != nil {
  224. t.Fatal(err)
  225. }
  226. runCmd = exec.Command(dockerBinary, "ps", "-s", "-n=1")
  227. wait := make(chan struct{})
  228. go func() {
  229. out, _, err = runCommandWithOutput(runCmd)
  230. close(wait)
  231. }()
  232. select {
  233. case <-wait:
  234. case <-time.After(3 * time.Second):
  235. t.Fatalf("Calling \"docker ps -s\" timed out!")
  236. }
  237. if err != nil {
  238. t.Fatal(out, err)
  239. }
  240. lines := strings.Split(strings.Trim(out, "\n "), "\n")
  241. sizeIndex := strings.Index(lines[0], "SIZE")
  242. idIndex := strings.Index(lines[0], "CONTAINER ID")
  243. foundID := lines[1][idIndex : idIndex+12]
  244. if foundID != id[:12] {
  245. t.Fatalf("Expected id %s, got %s", id[:12], foundID)
  246. }
  247. expectedSize := fmt.Sprintf("%d B", (2 + base_bytes))
  248. foundSize := lines[1][sizeIndex:]
  249. if foundSize != expectedSize {
  250. t.Fatalf("Expected size %q, got %q", expectedSize, foundSize)
  251. }
  252. deleteAllContainers()
  253. logDone("ps - test ps size")
  254. }
  255. func TestPsListContainersFilterStatus(t *testing.T) {
  256. // FIXME: this should test paused, but it makes things hang and its wonky
  257. // this is because paused containers can't be controlled by signals
  258. deleteAllContainers()
  259. // start exited container
  260. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox")
  261. out, _, err := runCommandWithOutput(runCmd)
  262. if err != nil {
  263. t.Fatal(out, err)
  264. }
  265. firstID := stripTrailingCharacters(out)
  266. // make sure the exited cintainer is not running
  267. runCmd = exec.Command(dockerBinary, "wait", firstID)
  268. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  269. t.Fatal(out, err)
  270. }
  271. // start running container
  272. runCmd = exec.Command(dockerBinary, "run", "-itd", "busybox")
  273. out, _, err = runCommandWithOutput(runCmd)
  274. if err != nil {
  275. t.Fatal(out, err)
  276. }
  277. secondID := stripTrailingCharacters(out)
  278. // filter containers by exited
  279. runCmd = exec.Command(dockerBinary, "ps", "-q", "--filter=status=exited")
  280. out, _, err = runCommandWithOutput(runCmd)
  281. if err != nil {
  282. t.Fatal(out, err)
  283. }
  284. containerOut := strings.TrimSpace(out)
  285. if containerOut != firstID[:12] {
  286. t.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out)
  287. }
  288. runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--filter=status=running")
  289. out, _, err = runCommandWithOutput(runCmd)
  290. if err != nil {
  291. t.Fatal(out, err)
  292. }
  293. containerOut = strings.TrimSpace(out)
  294. if containerOut != secondID[:12] {
  295. t.Fatalf("Expected id %s, got %s for running filter, output: %q", secondID[:12], containerOut, out)
  296. }
  297. deleteAllContainers()
  298. logDone("ps - test ps filter status")
  299. }
  300. func TestPsListContainersFilterID(t *testing.T) {
  301. // start container
  302. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox")
  303. out, _, err := runCommandWithOutput(runCmd)
  304. if err != nil {
  305. t.Fatal(out, err)
  306. }
  307. firstID := stripTrailingCharacters(out)
  308. // start another container
  309. runCmd = exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "sleep 360")
  310. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  311. t.Fatal(out, err)
  312. }
  313. // filter containers by id
  314. runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--filter=id="+firstID)
  315. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  316. t.Fatal(out, err)
  317. }
  318. containerOut := strings.TrimSpace(out)
  319. if containerOut != firstID[:12] {
  320. t.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out)
  321. }
  322. deleteAllContainers()
  323. logDone("ps - test ps filter id")
  324. }
  325. func TestPsListContainersFilterName(t *testing.T) {
  326. // start container
  327. runCmd := exec.Command(dockerBinary, "run", "-d", "--name=a_name_to_match", "busybox")
  328. out, _, err := runCommandWithOutput(runCmd)
  329. if err != nil {
  330. t.Fatal(out, err)
  331. }
  332. firstID := stripTrailingCharacters(out)
  333. // start another container
  334. runCmd = exec.Command(dockerBinary, "run", "-d", "--name=b_name_to_match", "busybox", "sh", "-c", "sleep 360")
  335. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  336. t.Fatal(out, err)
  337. }
  338. // filter containers by name
  339. runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--filter=name=a_name_to_match")
  340. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  341. t.Fatal(out, err)
  342. }
  343. containerOut := strings.TrimSpace(out)
  344. if containerOut != firstID[:12] {
  345. t.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out)
  346. }
  347. deleteAllContainers()
  348. logDone("ps - test ps filter name")
  349. }
  350. func TestPsListContainersFilterExited(t *testing.T) {
  351. defer deleteAllContainers()
  352. runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "top", "busybox", "top")
  353. if out, _, err := runCommandWithOutput(runCmd); err != nil {
  354. t.Fatal(out, err)
  355. }
  356. runCmd = exec.Command(dockerBinary, "run", "--name", "zero1", "busybox", "true")
  357. if out, _, err := runCommandWithOutput(runCmd); err != nil {
  358. t.Fatal(out, err)
  359. }
  360. firstZero, err := getIDByName("zero1")
  361. if err != nil {
  362. t.Fatal(err)
  363. }
  364. runCmd = exec.Command(dockerBinary, "run", "--name", "zero2", "busybox", "true")
  365. if out, _, err := runCommandWithOutput(runCmd); err != nil {
  366. t.Fatal(out, err)
  367. }
  368. secondZero, err := getIDByName("zero2")
  369. if err != nil {
  370. t.Fatal(err)
  371. }
  372. runCmd = exec.Command(dockerBinary, "run", "--name", "nonzero1", "busybox", "false")
  373. if out, _, err := runCommandWithOutput(runCmd); err == nil {
  374. t.Fatal("Should fail.", out, err)
  375. }
  376. firstNonZero, err := getIDByName("nonzero1")
  377. if err != nil {
  378. t.Fatal(err)
  379. }
  380. runCmd = exec.Command(dockerBinary, "run", "--name", "nonzero2", "busybox", "false")
  381. if out, _, err := runCommandWithOutput(runCmd); err == nil {
  382. t.Fatal("Should fail.", out, err)
  383. }
  384. secondNonZero, err := getIDByName("nonzero2")
  385. if err != nil {
  386. t.Fatal(err)
  387. }
  388. // filter containers by exited=0
  389. runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--no-trunc", "--filter=exited=0")
  390. out, _, err := runCommandWithOutput(runCmd)
  391. if err != nil {
  392. t.Fatal(out, err)
  393. }
  394. ids := strings.Split(strings.TrimSpace(out), "\n")
  395. if len(ids) != 2 {
  396. t.Fatalf("Should be 2 zero exited containerst got %d", len(ids))
  397. }
  398. if ids[0] != secondZero {
  399. t.Fatalf("First in list should be %q, got %q", secondZero, ids[0])
  400. }
  401. if ids[1] != firstZero {
  402. t.Fatalf("Second in list should be %q, got %q", firstZero, ids[1])
  403. }
  404. runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--no-trunc", "--filter=exited=1")
  405. out, _, err = runCommandWithOutput(runCmd)
  406. if err != nil {
  407. t.Fatal(out, err)
  408. }
  409. ids = strings.Split(strings.TrimSpace(out), "\n")
  410. if len(ids) != 2 {
  411. t.Fatalf("Should be 2 zero exited containerst got %d", len(ids))
  412. }
  413. if ids[0] != secondNonZero {
  414. t.Fatalf("First in list should be %q, got %q", secondNonZero, ids[0])
  415. }
  416. if ids[1] != firstNonZero {
  417. t.Fatalf("Second in list should be %q, got %q", firstNonZero, ids[1])
  418. }
  419. logDone("ps - test ps filter exited")
  420. }
  421. func TestPsRightTagName(t *testing.T) {
  422. tag := "asybox:shmatest"
  423. defer deleteAllContainers()
  424. defer deleteImages(tag)
  425. if out, err := exec.Command(dockerBinary, "tag", "busybox", tag).CombinedOutput(); err != nil {
  426. t.Fatalf("Failed to tag image: %s, out: %q", err, out)
  427. }
  428. var id1 string
  429. if out, err := exec.Command(dockerBinary, "run", "-d", "busybox", "top").CombinedOutput(); err != nil {
  430. t.Fatalf("Failed to run container: %s, out: %q", err, out)
  431. } else {
  432. id1 = strings.TrimSpace(string(out))
  433. }
  434. var id2 string
  435. if out, err := exec.Command(dockerBinary, "run", "-d", tag, "top").CombinedOutput(); err != nil {
  436. t.Fatalf("Failed to run container: %s, out: %q", err, out)
  437. } else {
  438. id2 = strings.TrimSpace(string(out))
  439. }
  440. out, err := exec.Command(dockerBinary, "ps", "--no-trunc").CombinedOutput()
  441. if err != nil {
  442. t.Fatalf("Failed to run 'ps': %s, out: %q", err, out)
  443. }
  444. lines := strings.Split(strings.TrimSpace(string(out)), "\n")
  445. // skip header
  446. lines = lines[1:]
  447. if len(lines) != 2 {
  448. t.Fatalf("There should be 2 running container, got %d", len(lines))
  449. }
  450. for _, line := range lines {
  451. f := strings.Fields(line)
  452. switch f[0] {
  453. case id1:
  454. if f[1] != "busybox:latest" {
  455. t.Fatalf("Expected %s tag for id %s, got %s", "busybox", id1, f[1])
  456. }
  457. case id2:
  458. if f[1] != tag {
  459. t.Fatalf("Expected %s tag for id %s, got %s", tag, id1, f[1])
  460. }
  461. default:
  462. t.Fatalf("Unexpected id %s, expected %s and %s", f[0], id1, id2)
  463. }
  464. }
  465. logDone("ps - right tags for containers")
  466. }
  467. func TestPsLinkedWithNoTrunc(t *testing.T) {
  468. defer deleteAllContainers()
  469. if out, err := exec.Command(dockerBinary, "run", "--name=first", "-d", "busybox", "top").CombinedOutput(); err != nil {
  470. t.Fatalf("Output: %s, err: %s", out, err)
  471. }
  472. if out, err := exec.Command(dockerBinary, "run", "--name=second", "--link=first:first", "-d", "busybox", "top").CombinedOutput(); err != nil {
  473. t.Fatalf("Output: %s, err: %s", out, err)
  474. }
  475. out, err := exec.Command(dockerBinary, "ps", "--no-trunc").CombinedOutput()
  476. if err != nil {
  477. t.Fatalf("Output: %s, err: %s", out, err)
  478. }
  479. lines := strings.Split(strings.TrimSpace(string(out)), "\n")
  480. // strip header
  481. lines = lines[1:]
  482. expected := []string{"second", "first,second/first"}
  483. var names []string
  484. for _, l := range lines {
  485. fields := strings.Fields(l)
  486. names = append(names, fields[len(fields)-1])
  487. }
  488. if !reflect.DeepEqual(expected, names) {
  489. t.Fatalf("Expected array: %v, got: %v", expected, names)
  490. }
  491. }