vfs_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //go:build linux
  2. // +build linux
  3. package vfs // import "github.com/docker/docker/daemon/graphdriver/vfs"
  4. import (
  5. "archive/tar"
  6. "bytes"
  7. "errors"
  8. "io"
  9. "os"
  10. "path/filepath"
  11. "syscall"
  12. "testing"
  13. "github.com/moby/sys/mount"
  14. "gotest.tools/v3/assert"
  15. "github.com/docker/docker/daemon/graphdriver"
  16. "github.com/docker/docker/daemon/graphdriver/graphtest"
  17. )
  18. // This avoids creating a new driver for each test if all tests are run
  19. // Make sure to put new tests between TestVfsSetup and TestVfsTeardown
  20. func TestVfsSetup(t *testing.T) {
  21. graphtest.GetDriver(t, "vfs")
  22. }
  23. func TestVfsCreateEmpty(t *testing.T) {
  24. graphtest.DriverTestCreateEmpty(t, "vfs")
  25. }
  26. func TestVfsCreateBase(t *testing.T) {
  27. graphtest.DriverTestCreateBase(t, "vfs")
  28. }
  29. func TestVfsCreateSnap(t *testing.T) {
  30. graphtest.DriverTestCreateSnap(t, "vfs")
  31. }
  32. func TestVfsSetQuota(t *testing.T) {
  33. graphtest.DriverTestSetQuota(t, "vfs", false)
  34. }
  35. func TestVfsTeardown(t *testing.T) {
  36. graphtest.PutDriver(t)
  37. }
  38. func TestXattrUnsupportedByBackingFS(t *testing.T) {
  39. rootdir := t.TempDir()
  40. // The ramfs filesystem is unconditionally compiled into the kernel,
  41. // and does not support extended attributes.
  42. err := mount.Mount("ramfs", rootdir, "ramfs", "")
  43. if errors.Is(err, syscall.EPERM) {
  44. t.Skip("test requires the ability to mount a filesystem")
  45. }
  46. assert.NilError(t, err)
  47. defer mount.Unmount(rootdir)
  48. var buf bytes.Buffer
  49. tw := tar.NewWriter(&buf)
  50. const (
  51. filename = "test.txt"
  52. content = "hello world\n"
  53. )
  54. assert.NilError(t, tw.WriteHeader(&tar.Header{
  55. Name: filename,
  56. Mode: 0o644,
  57. Size: int64(len(content)),
  58. PAXRecords: map[string]string{
  59. "SCHILY.xattr.user.test": "helloxattr",
  60. },
  61. }))
  62. _, err = io.WriteString(tw, content)
  63. assert.NilError(t, err)
  64. assert.NilError(t, tw.Close())
  65. testlayer := buf.Bytes()
  66. for _, tt := range []struct {
  67. name string
  68. opts []string
  69. expectErrIs error
  70. }{
  71. {
  72. name: "Default",
  73. expectErrIs: syscall.EOPNOTSUPP,
  74. },
  75. {
  76. name: "xattrs=i_want_broken_containers",
  77. opts: []string{"xattrs=i_want_broken_containers"},
  78. },
  79. } {
  80. t.Run(tt.name, func(t *testing.T) {
  81. subdir := filepath.Join(rootdir, tt.name)
  82. assert.NilError(t, os.Mkdir(subdir, 0o755))
  83. d, err := graphdriver.GetDriver("vfs", nil,
  84. graphdriver.Options{DriverOptions: tt.opts, Root: subdir})
  85. assert.NilError(t, err)
  86. defer d.Cleanup()
  87. assert.NilError(t, d.Create("test", "", nil))
  88. _, err = d.ApplyDiff("test", "", bytes.NewReader(testlayer))
  89. assert.ErrorIs(t, err, tt.expectErrIs)
  90. if err == nil {
  91. path, err := d.Get("test", "")
  92. assert.NilError(t, err)
  93. defer d.Put("test")
  94. actual, err := os.ReadFile(filepath.Join(path, filename))
  95. assert.NilError(t, err)
  96. assert.Equal(t, string(actual), content)
  97. }
  98. })
  99. }
  100. }