docker_cli_diff_test.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package main
  2. import (
  3. "strings"
  4. "time"
  5. "github.com/docker/docker/integration-cli/checker"
  6. "github.com/docker/docker/integration-cli/cli"
  7. "github.com/go-check/check"
  8. )
  9. // ensure that an added file shows up in docker diff
  10. func (s *DockerSuite) TestDiffFilenameShownInOutput(c *check.C) {
  11. containerCmd := `mkdir /foo; echo xyzzy > /foo/bar`
  12. out := cli.DockerCmd(c, "run", "-d", "busybox", "sh", "-c", containerCmd).Combined()
  13. // Wait for it to exit as cannot diff a running container on Windows, and
  14. // it will take a few seconds to exit. Also there's no way in Windows to
  15. // differentiate between an Add or a Modify, and all files are under
  16. // a "Files/" prefix.
  17. containerID := strings.TrimSpace(out)
  18. lookingFor := "A /foo/bar"
  19. if testEnv.DaemonPlatform() == "windows" {
  20. cli.WaitExited(c, containerID, 60*time.Second)
  21. lookingFor = "C Files/foo/bar"
  22. }
  23. cleanCID := strings.TrimSpace(out)
  24. out = cli.DockerCmd(c, "diff", cleanCID).Combined()
  25. found := false
  26. for _, line := range strings.Split(out, "\n") {
  27. if strings.Contains(line, lookingFor) {
  28. found = true
  29. break
  30. }
  31. }
  32. c.Assert(found, checker.True)
  33. }
  34. // test to ensure GH #3840 doesn't occur any more
  35. func (s *DockerSuite) TestDiffEnsureInitLayerFilesAreIgnored(c *check.C) {
  36. testRequires(c, DaemonIsLinux)
  37. // this is a list of files which shouldn't show up in `docker diff`
  38. initLayerFiles := []string{"/etc/resolv.conf", "/etc/hostname", "/etc/hosts", "/.dockerenv"}
  39. containerCount := 5
  40. // we might not run into this problem from the first run, so start a few containers
  41. for i := 0; i < containerCount; i++ {
  42. containerCmd := `echo foo > /root/bar`
  43. out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", containerCmd)
  44. cleanCID := strings.TrimSpace(out)
  45. out, _ = dockerCmd(c, "diff", cleanCID)
  46. for _, filename := range initLayerFiles {
  47. c.Assert(out, checker.Not(checker.Contains), filename)
  48. }
  49. }
  50. }
  51. func (s *DockerSuite) TestDiffEnsureDefaultDevs(c *check.C) {
  52. testRequires(c, DaemonIsLinux)
  53. out, _ := dockerCmd(c, "run", "-d", "busybox", "sleep", "0")
  54. cleanCID := strings.TrimSpace(out)
  55. out, _ = dockerCmd(c, "diff", cleanCID)
  56. expected := map[string]bool{
  57. "C /dev": true,
  58. "A /dev/full": true, // busybox
  59. "C /dev/ptmx": true, // libcontainer
  60. "A /dev/mqueue": true,
  61. "A /dev/kmsg": true,
  62. "A /dev/fd": true,
  63. "A /dev/ptmx": true,
  64. "A /dev/null": true,
  65. "A /dev/random": true,
  66. "A /dev/stdout": true,
  67. "A /dev/stderr": true,
  68. "A /dev/tty1": true,
  69. "A /dev/stdin": true,
  70. "A /dev/tty": true,
  71. "A /dev/urandom": true,
  72. "A /dev/zero": true,
  73. }
  74. for _, line := range strings.Split(out, "\n") {
  75. c.Assert(line == "" || expected[line], checker.True, check.Commentf(line))
  76. }
  77. }
  78. // https://github.com/docker/docker/pull/14381#discussion_r33859347
  79. func (s *DockerSuite) TestDiffEmptyArgClientError(c *check.C) {
  80. out, _, err := dockerCmdWithError("diff", "")
  81. c.Assert(err, checker.NotNil)
  82. c.Assert(strings.TrimSpace(out), checker.Contains, "Container name cannot be empty")
  83. }