diff_test.go 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package container // import "github.com/docker/docker/integration/container"
  2. import (
  3. "context"
  4. "testing"
  5. "time"
  6. containertypes "github.com/docker/docker/api/types/container"
  7. "github.com/docker/docker/integration/internal/container"
  8. "github.com/docker/docker/integration/internal/request"
  9. "github.com/docker/docker/pkg/archive"
  10. "github.com/gotestyourself/gotestyourself/poll"
  11. "github.com/gotestyourself/gotestyourself/skip"
  12. "github.com/stretchr/testify/assert"
  13. "github.com/stretchr/testify/require"
  14. )
  15. // ensure that an added file shows up in docker diff
  16. func TestDiffFilenameShownInOutput(t *testing.T) {
  17. defer setupTest(t)()
  18. client := request.NewAPIClient(t)
  19. ctx := context.Background()
  20. cID := container.Run(t, ctx, client, container.WithCmd("sh", "-c", `mkdir /foo; echo xyzzy > /foo/bar`))
  21. // Wait for it to exit as cannot diff a running container on Windows, and
  22. // it will take a few seconds to exit. Also there's no way in Windows to
  23. // differentiate between an Add or a Modify, and all files are under
  24. // a "Files/" prefix.
  25. lookingFor := containertypes.ContainerChangeResponseItem{Kind: archive.ChangeAdd, Path: "/foo/bar"}
  26. if testEnv.OSType == "windows" {
  27. poll.WaitOn(t, containerIsInState(ctx, client, cID, "exited"), poll.WithDelay(100*time.Millisecond), poll.WithTimeout(60*time.Second))
  28. lookingFor = containertypes.ContainerChangeResponseItem{Kind: archive.ChangeModify, Path: "Files/foo/bar"}
  29. }
  30. items, err := client.ContainerDiff(ctx, cID)
  31. require.NoError(t, err)
  32. assert.Contains(t, items, lookingFor)
  33. }
  34. // test to ensure GH #3840 doesn't occur any more
  35. func TestDiffEnsureInitLayerFilesAreIgnored(t *testing.T) {
  36. skip.If(t, testEnv.DaemonInfo.OSType != "linux")
  37. defer setupTest(t)()
  38. client := request.NewAPIClient(t)
  39. ctx := context.Background()
  40. // this is a list of files which shouldn't show up in `docker diff`
  41. initLayerFiles := []string{"/etc/resolv.conf", "/etc/hostname", "/etc/hosts", "/.dockerenv"}
  42. containerCount := 5
  43. // we might not run into this problem from the first run, so start a few containers
  44. for i := 0; i < containerCount; i++ {
  45. cID := container.Run(t, ctx, client, container.WithCmd("sh", "-c", `echo foo > /root/bar`))
  46. items, err := client.ContainerDiff(ctx, cID)
  47. require.NoError(t, err)
  48. for _, item := range items {
  49. assert.NotContains(t, initLayerFiles, item.Path)
  50. }
  51. }
  52. }
  53. func TestDiffEnsureDefaultDevs(t *testing.T) {
  54. skip.If(t, testEnv.DaemonInfo.OSType != "linux")
  55. defer setupTest(t)()
  56. client := request.NewAPIClient(t)
  57. ctx := context.Background()
  58. cID := container.Run(t, ctx, client, container.WithCmd("sleep", "0"))
  59. items, err := client.ContainerDiff(ctx, cID)
  60. require.NoError(t, err)
  61. expected := []containertypes.ContainerChangeResponseItem{
  62. {Kind: archive.ChangeModify, Path: "/dev"},
  63. {Kind: archive.ChangeAdd, Path: "/dev/full"}, // busybox
  64. {Kind: archive.ChangeModify, Path: "/dev/ptmx"}, // libcontainer
  65. {Kind: archive.ChangeAdd, Path: "/dev/mqueue"},
  66. {Kind: archive.ChangeAdd, Path: "/dev/kmsg"},
  67. {Kind: archive.ChangeAdd, Path: "/dev/fd"},
  68. {Kind: archive.ChangeAdd, Path: "/dev/ptmx"},
  69. {Kind: archive.ChangeAdd, Path: "/dev/null"},
  70. {Kind: archive.ChangeAdd, Path: "/dev/random"},
  71. {Kind: archive.ChangeAdd, Path: "/dev/stdout"},
  72. {Kind: archive.ChangeAdd, Path: "/dev/stderr"},
  73. {Kind: archive.ChangeAdd, Path: "/dev/tty1"},
  74. {Kind: archive.ChangeAdd, Path: "/dev/stdin"},
  75. {Kind: archive.ChangeAdd, Path: "/dev/tty"},
  76. {Kind: archive.ChangeAdd, Path: "/dev/urandom"},
  77. }
  78. for _, item := range items {
  79. assert.Contains(t, expected, item)
  80. }
  81. }