Updated hello_fs_test.
parent
08f495e8b0
commit
f099d9868f
|
@ -17,20 +17,16 @@ package hellofs_test
|
|||
import (
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
"syscall"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jacobsa/fuse"
|
||||
"github.com/jacobsa/fuse/samples"
|
||||
"github.com/jacobsa/fuse/samples/hellofs"
|
||||
"github.com/googlecloudplatform/gcsfuse/timeutil"
|
||||
. "github.com/jacobsa/oglematchers"
|
||||
. "github.com/jacobsa/ogletest"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func TestHelloFS(t *testing.T) { RunTests(t) }
|
||||
|
@ -40,8 +36,7 @@ func TestHelloFS(t *testing.T) { RunTests(t) }
|
|||
////////////////////////////////////////////////////////////////////////
|
||||
|
||||
type HelloFSTest struct {
|
||||
clock timeutil.SimulatedClock
|
||||
mfs *fuse.MountedFileSystem
|
||||
samples.SampleTest
|
||||
}
|
||||
|
||||
var _ SetUpInterface = &HelloFSTest{}
|
||||
|
@ -50,53 +45,15 @@ var _ TearDownInterface = &HelloFSTest{}
|
|||
func init() { RegisterTestSuite(&HelloFSTest{}) }
|
||||
|
||||
func (t *HelloFSTest) SetUp(ti *TestInfo) {
|
||||
var err error
|
||||
|
||||
// Set up a fixed, non-zero time.
|
||||
t.clock.SetTime(time.Now())
|
||||
|
||||
// Set up a temporary directory for mounting.
|
||||
mountPoint, err := ioutil.TempDir("", "hello_fs_test")
|
||||
if err != nil {
|
||||
panic("ioutil.TempDir: " + err.Error())
|
||||
}
|
||||
|
||||
// Mount a file system.
|
||||
fs := &hellofs.HelloFS{
|
||||
Clock: &t.clock,
|
||||
Clock: &t.Clock,
|
||||
}
|
||||
|
||||
if t.mfs, err = fuse.Mount(mountPoint, fs, &fuse.MountConfig{}); err != nil {
|
||||
panic("Mount: " + err.Error())
|
||||
}
|
||||
|
||||
if err = t.mfs.WaitForReady(context.Background()); err != nil {
|
||||
panic("MountedFileSystem.WaitForReady: " + err.Error())
|
||||
}
|
||||
t.SampleTest.Initialize(fs, &fuse.MountConfig{})
|
||||
}
|
||||
|
||||
func (t *HelloFSTest) TearDown() {
|
||||
// Unmount the file system. Try again on "resource busy" errors.
|
||||
delay := 10 * time.Millisecond
|
||||
for {
|
||||
err := t.mfs.Unmount()
|
||||
if err == nil {
|
||||
break
|
||||
}
|
||||
|
||||
if strings.Contains(err.Error(), "resource busy") {
|
||||
log.Println("Resource busy error while unmounting; trying again")
|
||||
time.Sleep(delay)
|
||||
delay = time.Duration(1.3 * float64(delay))
|
||||
continue
|
||||
}
|
||||
|
||||
panic("MountedFileSystem.Unmount: " + err.Error())
|
||||
}
|
||||
|
||||
if err := t.mfs.Join(context.Background()); err != nil {
|
||||
panic("MountedFileSystem.Join: " + err.Error())
|
||||
}
|
||||
t.SampleTest.Destroy()
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
|
@ -104,7 +61,7 @@ func (t *HelloFSTest) TearDown() {
|
|||
////////////////////////////////////////////////////////////////////////
|
||||
|
||||
func (t *HelloFSTest) ReadDir_Root() {
|
||||
entries, err := ioutil.ReadDir(t.mfs.Dir())
|
||||
entries, err := ioutil.ReadDir(t.Dir)
|
||||
|
||||
AssertEq(nil, err)
|
||||
AssertEq(2, len(entries))
|
||||
|
@ -115,7 +72,7 @@ func (t *HelloFSTest) ReadDir_Root() {
|
|||
ExpectEq("dir", fi.Name())
|
||||
ExpectEq(0, fi.Size())
|
||||
ExpectEq(os.ModeDir|0555, fi.Mode())
|
||||
ExpectEq(0, t.clock.Now().Sub(fi.ModTime()), "ModTime: %v", fi.ModTime())
|
||||
ExpectEq(0, t.Clock.Now().Sub(fi.ModTime()), "ModTime: %v", fi.ModTime())
|
||||
ExpectTrue(fi.IsDir())
|
||||
|
||||
// hello
|
||||
|
@ -123,12 +80,12 @@ func (t *HelloFSTest) ReadDir_Root() {
|
|||
ExpectEq("hello", fi.Name())
|
||||
ExpectEq(len("Hello, world!"), fi.Size())
|
||||
ExpectEq(0444, fi.Mode())
|
||||
ExpectEq(0, t.clock.Now().Sub(fi.ModTime()), "ModTime: %v", fi.ModTime())
|
||||
ExpectEq(0, t.Clock.Now().Sub(fi.ModTime()), "ModTime: %v", fi.ModTime())
|
||||
ExpectFalse(fi.IsDir())
|
||||
}
|
||||
|
||||
func (t *HelloFSTest) ReadDir_Dir() {
|
||||
entries, err := ioutil.ReadDir(path.Join(t.mfs.Dir(), "dir"))
|
||||
entries, err := ioutil.ReadDir(path.Join(t.Dir, "dir"))
|
||||
|
||||
AssertEq(nil, err)
|
||||
AssertEq(1, len(entries))
|
||||
|
@ -139,76 +96,76 @@ func (t *HelloFSTest) ReadDir_Dir() {
|
|||
ExpectEq("world", fi.Name())
|
||||
ExpectEq(len("Hello, world!"), fi.Size())
|
||||
ExpectEq(0444, fi.Mode())
|
||||
ExpectEq(0, t.clock.Now().Sub(fi.ModTime()), "ModTime: %v", fi.ModTime())
|
||||
ExpectEq(0, t.Clock.Now().Sub(fi.ModTime()), "ModTime: %v", fi.ModTime())
|
||||
ExpectFalse(fi.IsDir())
|
||||
}
|
||||
|
||||
func (t *HelloFSTest) ReadDir_NonExistent() {
|
||||
_, err := ioutil.ReadDir(path.Join(t.mfs.Dir(), "foobar"))
|
||||
_, err := ioutil.ReadDir(path.Join(t.Dir, "foobar"))
|
||||
|
||||
AssertNe(nil, err)
|
||||
ExpectThat(err, Error(HasSubstr("no such file")))
|
||||
}
|
||||
|
||||
func (t *HelloFSTest) Stat_Hello() {
|
||||
fi, err := os.Stat(path.Join(t.mfs.Dir(), "hello"))
|
||||
fi, err := os.Stat(path.Join(t.Dir, "hello"))
|
||||
AssertEq(nil, err)
|
||||
|
||||
ExpectEq("hello", fi.Name())
|
||||
ExpectEq(len("Hello, world!"), fi.Size())
|
||||
ExpectEq(0444, fi.Mode())
|
||||
ExpectEq(0, t.clock.Now().Sub(fi.ModTime()), "ModTime: %v", fi.ModTime())
|
||||
ExpectEq(0, t.Clock.Now().Sub(fi.ModTime()), "ModTime: %v", fi.ModTime())
|
||||
ExpectFalse(fi.IsDir())
|
||||
ExpectEq(1, fi.Sys().(*syscall.Stat_t).Nlink)
|
||||
}
|
||||
|
||||
func (t *HelloFSTest) Stat_Dir() {
|
||||
fi, err := os.Stat(path.Join(t.mfs.Dir(), "dir"))
|
||||
fi, err := os.Stat(path.Join(t.Dir, "dir"))
|
||||
AssertEq(nil, err)
|
||||
|
||||
ExpectEq("dir", fi.Name())
|
||||
ExpectEq(0, fi.Size())
|
||||
ExpectEq(0555|os.ModeDir, fi.Mode())
|
||||
ExpectEq(0, t.clock.Now().Sub(fi.ModTime()), "ModTime: %v", fi.ModTime())
|
||||
ExpectEq(0, t.Clock.Now().Sub(fi.ModTime()), "ModTime: %v", fi.ModTime())
|
||||
ExpectTrue(fi.IsDir())
|
||||
ExpectEq(1, fi.Sys().(*syscall.Stat_t).Nlink)
|
||||
}
|
||||
|
||||
func (t *HelloFSTest) Stat_World() {
|
||||
fi, err := os.Stat(path.Join(t.mfs.Dir(), "dir/world"))
|
||||
fi, err := os.Stat(path.Join(t.Dir, "dir/world"))
|
||||
AssertEq(nil, err)
|
||||
|
||||
ExpectEq("world", fi.Name())
|
||||
ExpectEq(len("Hello, world!"), fi.Size())
|
||||
ExpectEq(0444, fi.Mode())
|
||||
ExpectEq(0, t.clock.Now().Sub(fi.ModTime()), "ModTime: %v", fi.ModTime())
|
||||
ExpectEq(0, t.Clock.Now().Sub(fi.ModTime()), "ModTime: %v", fi.ModTime())
|
||||
ExpectFalse(fi.IsDir())
|
||||
ExpectEq(1, fi.Sys().(*syscall.Stat_t).Nlink)
|
||||
}
|
||||
|
||||
func (t *HelloFSTest) Stat_NonExistent() {
|
||||
_, err := os.Stat(path.Join(t.mfs.Dir(), "foobar"))
|
||||
_, err := os.Stat(path.Join(t.Dir, "foobar"))
|
||||
|
||||
AssertNe(nil, err)
|
||||
ExpectThat(err, Error(HasSubstr("no such file")))
|
||||
}
|
||||
|
||||
func (t *HelloFSTest) ReadFile_Hello() {
|
||||
slice, err := ioutil.ReadFile(path.Join(t.mfs.Dir(), "hello"))
|
||||
slice, err := ioutil.ReadFile(path.Join(t.Dir, "hello"))
|
||||
|
||||
AssertEq(nil, err)
|
||||
ExpectEq("Hello, world!", string(slice))
|
||||
}
|
||||
|
||||
func (t *HelloFSTest) ReadFile_Dir() {
|
||||
_, err := ioutil.ReadFile(path.Join(t.mfs.Dir(), "dir"))
|
||||
_, err := ioutil.ReadFile(path.Join(t.Dir, "dir"))
|
||||
|
||||
AssertNe(nil, err)
|
||||
ExpectThat(err, Error(HasSubstr("is a directory")))
|
||||
}
|
||||
|
||||
func (t *HelloFSTest) ReadFile_World() {
|
||||
slice, err := ioutil.ReadFile(path.Join(t.mfs.Dir(), "dir/world"))
|
||||
slice, err := ioutil.ReadFile(path.Join(t.Dir, "dir/world"))
|
||||
|
||||
AssertEq(nil, err)
|
||||
ExpectEq("Hello, world!", string(slice))
|
||||
|
@ -221,7 +178,7 @@ func (t *HelloFSTest) OpenAndRead() {
|
|||
var err error
|
||||
|
||||
// Open the file.
|
||||
f, err := os.Open(path.Join(t.mfs.Dir(), "hello"))
|
||||
f, err := os.Open(path.Join(t.Dir, "hello"))
|
||||
defer func() {
|
||||
if f != nil {
|
||||
ExpectEq(nil, f.Close())
|
||||
|
@ -268,7 +225,7 @@ func (t *HelloFSTest) OpenAndRead() {
|
|||
}
|
||||
|
||||
func (t *HelloFSTest) Open_NonExistent() {
|
||||
_, err := os.Open(path.Join(t.mfs.Dir(), "foobar"))
|
||||
_, err := os.Open(path.Join(t.Dir, "foobar"))
|
||||
|
||||
AssertNe(nil, err)
|
||||
ExpectThat(err, Error(HasSubstr("no such file")))
|
||||
|
|
|
@ -77,6 +77,13 @@ func (t *SampleTest) initialize(
|
|||
return
|
||||
}
|
||||
|
||||
// Wait for it to be read.
|
||||
err = t.mfs.WaitForReady(t.Ctx)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("WaitForReady: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue