docker_cli_diff_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 := stripTrailingCharacters(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 := stripTrailingCharacters(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 := stripTrailingCharacters(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. }
  80. for _, line := range strings.Split(out, "\n") {
  81. if line != "" && !expected[line] {
  82. t.Errorf("%q is shown in the diff but shouldn't", line)
  83. }
  84. }
  85. logDone("diff - ensure that only kmsg and ptmx in diff")
  86. }