docker_cli_diff_test.go 2.7 KB

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