docker_cli_diff_test.go 3.1 KB

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