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