docker_cli_diff_test.go 2.5 KB

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