overlayfs_linux_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package container
  2. import (
  3. "context"
  4. "io"
  5. "strings"
  6. "testing"
  7. "github.com/docker/docker/api/types"
  8. "github.com/docker/docker/integration/internal/container"
  9. "github.com/docker/docker/pkg/archive"
  10. "github.com/docker/docker/pkg/dmesg"
  11. "gotest.tools/v3/assert"
  12. "gotest.tools/v3/skip"
  13. )
  14. func TestNoOverlayfsWarningsAboutUndefinedBehaviors(t *testing.T) {
  15. skip.If(t, testEnv.DaemonInfo.OSType != "linux", "overlayfs is only available on linux")
  16. skip.If(t, testEnv.IsRemoteDaemon(), "local daemon is needed for kernel log access")
  17. skip.If(t, testEnv.IsRootless(), "root is needed for reading kernel log")
  18. defer setupTest(t)()
  19. client := testEnv.APIClient()
  20. ctx := context.Background()
  21. cID := container.Run(ctx, t, client, container.WithCmd("sh", "-c", `while true; do echo $RANDOM >>/file; sleep 0.1; done`))
  22. testCases := []struct {
  23. name string
  24. operation func(t *testing.T) error
  25. }{
  26. {name: "diff", operation: func(*testing.T) error {
  27. _, err := client.ContainerDiff(ctx, cID)
  28. return err
  29. }},
  30. {name: "export", operation: func(*testing.T) error {
  31. rc, err := client.ContainerExport(ctx, cID)
  32. if err == nil {
  33. defer rc.Close()
  34. _, err = io.Copy(io.Discard, rc)
  35. }
  36. return err
  37. }},
  38. {name: "cp to container", operation: func(t *testing.T) error {
  39. archive, err := archive.Generate("new-file", "hello-world")
  40. assert.NilError(t, err, "failed to create a temporary archive")
  41. return client.CopyToContainer(ctx, cID, "/", archive, types.CopyToContainerOptions{})
  42. }},
  43. {name: "cp from container", operation: func(*testing.T) error {
  44. rc, _, err := client.CopyFromContainer(ctx, cID, "/file")
  45. if err == nil {
  46. defer rc.Close()
  47. _, err = io.Copy(io.Discard, rc)
  48. }
  49. return err
  50. }},
  51. }
  52. for _, tc := range testCases {
  53. tc := tc
  54. t.Run(tc.name, func(t *testing.T) {
  55. prev := dmesgLines(256)
  56. err := tc.operation(t)
  57. assert.NilError(t, err)
  58. after := dmesgLines(2048)
  59. diff := diffDmesg(prev, after)
  60. for _, line := range diff {
  61. overlayfs := strings.Contains(line, "overlayfs: ")
  62. lowerDirInUse := strings.Contains(line, "lowerdir is in-use as ")
  63. upperDirInUse := strings.Contains(line, "upperdir is in-use as ")
  64. workDirInuse := strings.Contains(line, "workdir is in-use as ")
  65. undefinedBehavior := strings.Contains(line, "will result in undefined behavior")
  66. if overlayfs && (lowerDirInUse || upperDirInUse || workDirInuse) && undefinedBehavior {
  67. t.Errorf("%s caused overlayfs kernel warning: %s", tc.name, line)
  68. }
  69. }
  70. })
  71. }
  72. }
  73. func dmesgLines(bytes int) []string {
  74. data := dmesg.Dmesg(bytes)
  75. return strings.Split(strings.TrimSpace(string(data)), "\n")
  76. }
  77. func diffDmesg(prev, next []string) []string {
  78. // All lines have a timestamp, so just take the last one from the previous
  79. // log and find it in the new log.
  80. lastPrev := prev[len(prev)-1]
  81. for idx := len(next) - 1; idx >= 0; idx-- {
  82. line := next[idx]
  83. if line == lastPrev {
  84. nextIdx := idx + 1
  85. if nextIdx < len(next) {
  86. return next[nextIdx:]
  87. } else {
  88. // Found at the last position, log is the same.
  89. return nil
  90. }
  91. }
  92. }
  93. return next
  94. }