docker_cli_diff_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. // we might not run into this problem from the first run, so start a few containers
  37. for i := 0; i < 20; i++ {
  38. containerCmd := `echo foo > /root/bar`
  39. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", containerCmd)
  40. out, _, err := runCommandWithOutput(runCmd)
  41. if err != nil {
  42. c.Fatal(out, err)
  43. }
  44. cleanCID := strings.TrimSpace(out)
  45. diffCmd := exec.Command(dockerBinary, "diff", cleanCID)
  46. out, _, err = runCommandWithOutput(diffCmd)
  47. if err != nil {
  48. c.Fatalf("failed to run diff: %s, %v", out, err)
  49. }
  50. for _, filename := range dockerinitFiles {
  51. if strings.Contains(out, filename) {
  52. c.Errorf("found file which should've been ignored %v in diff output", filename)
  53. }
  54. }
  55. }
  56. }
  57. func (s *DockerSuite) TestDiffEnsureOnlyKmsgAndPtmx(c *check.C) {
  58. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sleep", "0")
  59. out, _, err := runCommandWithOutput(runCmd)
  60. if err != nil {
  61. c.Fatal(out, err)
  62. }
  63. cleanCID := strings.TrimSpace(out)
  64. diffCmd := exec.Command(dockerBinary, "diff", cleanCID)
  65. out, _, err = runCommandWithOutput(diffCmd)
  66. if err != nil {
  67. c.Fatalf("failed to run diff: %s, %v", out, err)
  68. }
  69. expected := map[string]bool{
  70. "C /dev": true,
  71. "A /dev/full": true, // busybox
  72. "C /dev/ptmx": true, // libcontainer
  73. "A /dev/kmsg": true, // lxc
  74. "A /dev/fd": true,
  75. "A /dev/fuse": true,
  76. "A /dev/ptmx": true,
  77. "A /dev/null": true,
  78. "A /dev/random": true,
  79. "A /dev/stdout": true,
  80. "A /dev/stderr": true,
  81. "A /dev/tty1": true,
  82. "A /dev/stdin": true,
  83. "A /dev/tty": true,
  84. "A /dev/urandom": true,
  85. "A /dev/zero": true,
  86. }
  87. for _, line := range strings.Split(out, "\n") {
  88. if line != "" && !expected[line] {
  89. c.Errorf("%q is shown in the diff but shouldn't", line)
  90. }
  91. }
  92. }