mount_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package fuse_test
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "path"
  6. "runtime"
  7. "strings"
  8. "testing"
  9. "golang.org/x/net/context"
  10. "github.com/jacobsa/fuse"
  11. "github.com/jacobsa/fuse/fuseops"
  12. "github.com/jacobsa/fuse/fuseutil"
  13. )
  14. ////////////////////////////////////////////////////////////////////////
  15. // minimalFS
  16. ////////////////////////////////////////////////////////////////////////
  17. // A minimal fuseutil.FileSystem that can successfully mount but do nothing
  18. // else.
  19. type minimalFS struct {
  20. fuseutil.NotImplementedFileSystem
  21. }
  22. func (fs *minimalFS) StatFS(
  23. ctx context.Context,
  24. op *fuseops.StatFSOp) (err error) {
  25. return
  26. }
  27. ////////////////////////////////////////////////////////////////////////
  28. // Tests
  29. ////////////////////////////////////////////////////////////////////////
  30. func TestSuccessfulMount(t *testing.T) {
  31. ctx := context.Background()
  32. // Set up a temporary directory.
  33. dir, err := ioutil.TempDir("", "mount_test")
  34. if err != nil {
  35. t.Fatal("ioutil.TempDir: %v", err)
  36. }
  37. defer os.RemoveAll(dir)
  38. // Mount.
  39. fs := &minimalFS{}
  40. mfs, err := fuse.Mount(
  41. dir,
  42. fuseutil.NewFileSystemServer(fs),
  43. &fuse.MountConfig{})
  44. if err != nil {
  45. t.Fatalf("fuse.Mount: %v", err)
  46. }
  47. defer func() {
  48. if err := mfs.Join(ctx); err != nil {
  49. t.Errorf("Joining: %v", err)
  50. }
  51. }()
  52. defer fuse.Unmount(mfs.Dir())
  53. }
  54. func TestNonEmptyMountPoint(t *testing.T) {
  55. ctx := context.Background()
  56. // osxfuse appears to be happy to mount over a non-empty mount point.
  57. //
  58. // We leave this test in for Linux, because it tickles the behavior of
  59. // fusermount writing to stderr and exiting with an error code. We want to
  60. // make sure that a descriptive error makes it back to the user.
  61. if runtime.GOOS == "darwin" {
  62. return
  63. }
  64. // Set up a temporary directory.
  65. dir, err := ioutil.TempDir("", "mount_test")
  66. if err != nil {
  67. t.Fatal("ioutil.TempDir: %v", err)
  68. }
  69. defer os.RemoveAll(dir)
  70. // Add a file within it.
  71. err = ioutil.WriteFile(path.Join(dir, "foo"), []byte{}, 0600)
  72. if err != nil {
  73. t.Fatalf("ioutil.WriteFile: %v", err)
  74. }
  75. // Attempt to mount.
  76. fs := &minimalFS{}
  77. mfs, err := fuse.Mount(
  78. dir,
  79. fuseutil.NewFileSystemServer(fs),
  80. &fuse.MountConfig{})
  81. if err == nil {
  82. fuse.Unmount(mfs.Dir())
  83. mfs.Join(ctx)
  84. t.Fatal("fuse.Mount returned nil")
  85. }
  86. const want = "not empty"
  87. if got := err.Error(); !strings.Contains(got, want) {
  88. t.Errorf("Unexpected error: %v", got)
  89. }
  90. }
  91. func TestNonexistentMountPoint(t *testing.T) {
  92. ctx := context.Background()
  93. // Set up a temporary directory.
  94. dir, err := ioutil.TempDir("", "mount_test")
  95. if err != nil {
  96. t.Fatal("ioutil.TempDir: %v", err)
  97. }
  98. defer os.RemoveAll(dir)
  99. // Attempt to mount into a sub-directory that doesn't exist.
  100. fs := &minimalFS{}
  101. mfs, err := fuse.Mount(
  102. path.Join(dir, "foo"),
  103. fuseutil.NewFileSystemServer(fs),
  104. &fuse.MountConfig{})
  105. if err == nil {
  106. fuse.Unmount(mfs.Dir())
  107. mfs.Join(ctx)
  108. t.Fatal("fuse.Mount returned nil")
  109. }
  110. const want = "no such file"
  111. if got := err.Error(); !strings.Contains(got, want) {
  112. t.Errorf("Unexpected error: %v", got)
  113. }
  114. }