docker_cli_diff_test.go 2.8 KB

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