docker_cli_diff_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package main
  2. import (
  3. "os/exec"
  4. "strings"
  5. "github.com/go-check/check"
  6. )
  7. // ensure that an added file shows up in docker diff
  8. func (s *DockerSuite) TestDiffFilenameShownInOutput(c *check.C) {
  9. containerCmd := `echo foo > /root/bar`
  10. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", containerCmd)
  11. out, _, err := runCommandWithOutput(runCmd)
  12. if err != nil {
  13. c.Fatalf("failed to start the container: %s, %v", out, err)
  14. }
  15. cleanCID := strings.TrimSpace(out)
  16. diffCmd := exec.Command(dockerBinary, "diff", cleanCID)
  17. out, _, err = runCommandWithOutput(diffCmd)
  18. if err != nil {
  19. c.Fatalf("failed to run diff: %s %v", out, err)
  20. }
  21. found := false
  22. for _, line := range strings.Split(out, "\n") {
  23. if strings.Contains("A /root/bar", line) {
  24. found = true
  25. break
  26. }
  27. }
  28. if !found {
  29. c.Errorf("couldn't find the new file in docker diff's output: %v", out)
  30. }
  31. }
  32. // test to ensure GH #3840 doesn't occur any more
  33. func (s *DockerSuite) TestDiffEnsureDockerinitFilesAreIgnored(c *check.C) {
  34. // this is a list of files which shouldn't show up in `docker diff`
  35. dockerinitFiles := []string{"/etc/resolv.conf", "/etc/hostname", "/etc/hosts", "/.dockerinit", "/.dockerenv"}
  36. containerCount := 5
  37. // we might not run into this problem from the first run, so start a few containers
  38. for i := 0; i < containerCount; i++ {
  39. containerCmd := `echo foo > /root/bar`
  40. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", containerCmd)
  41. out, _, err := runCommandWithOutput(runCmd)
  42. if err != nil {
  43. c.Fatal(out, err)
  44. }
  45. cleanCID := strings.TrimSpace(out)
  46. diffCmd := exec.Command(dockerBinary, "diff", cleanCID)
  47. out, _, err = runCommandWithOutput(diffCmd)
  48. if err != nil {
  49. c.Fatalf("failed to run diff: %s, %v", out, err)
  50. }
  51. for _, filename := range dockerinitFiles {
  52. if strings.Contains(out, filename) {
  53. c.Errorf("found file which should've been ignored %v in diff output", filename)
  54. }
  55. }
  56. }
  57. }
  58. func (s *DockerSuite) TestDiffEnsureOnlyKmsgAndPtmx(c *check.C) {
  59. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sleep", "0")
  60. out, _, err := runCommandWithOutput(runCmd)
  61. if err != nil {
  62. c.Fatal(out, err)
  63. }
  64. cleanCID := strings.TrimSpace(out)
  65. diffCmd := exec.Command(dockerBinary, "diff", cleanCID)
  66. out, _, err = runCommandWithOutput(diffCmd)
  67. if err != nil {
  68. c.Fatalf("failed to run diff: %s, %v", out, err)
  69. }
  70. expected := map[string]bool{
  71. "C /dev": true,
  72. "A /dev/full": true, // busybox
  73. "C /dev/ptmx": true, // libcontainer
  74. "A /dev/kmsg": true, // lxc
  75. "A /dev/fd": true,
  76. "A /dev/fuse": true,
  77. "A /dev/ptmx": true,
  78. "A /dev/null": true,
  79. "A /dev/random": true,
  80. "A /dev/stdout": true,
  81. "A /dev/stderr": true,
  82. "A /dev/tty1": true,
  83. "A /dev/stdin": true,
  84. "A /dev/tty": true,
  85. "A /dev/urandom": true,
  86. "A /dev/zero": true,
  87. }
  88. for _, line := range strings.Split(out, "\n") {
  89. if line != "" && !expected[line] {
  90. c.Errorf("%q is shown in the diff but shouldn't", line)
  91. }
  92. }
  93. }