vfs_test.go 2.7 KB

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