2015-02-27 00:54:16 +03:00
|
|
|
// Copyright 2015 Google Inc. All Rights Reserved.
|
|
|
|
// Author: jacobsa@google.com (Aaron Jacobs)
|
|
|
|
|
|
|
|
package samples_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
2015-02-27 06:09:53 +03:00
|
|
|
"path"
|
2015-02-27 00:54:16 +03:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2015-02-27 00:57:18 +03:00
|
|
|
"github.com/jacobsa/fuse"
|
|
|
|
"github.com/jacobsa/fuse/samples"
|
2015-02-27 00:54:16 +03:00
|
|
|
"github.com/jacobsa/gcsfuse/timeutil"
|
|
|
|
. "github.com/jacobsa/ogletest"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestHelloFS(t *testing.T) { RunTests(t) }
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// Boilerplate
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type HelloFSTest struct {
|
|
|
|
clock timeutil.SimulatedClock
|
2015-02-27 00:57:18 +03:00
|
|
|
mfs *fuse.MountedFileSystem
|
2015-02-27 00:54:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ SetUpInterface = &HelloFSTest{}
|
|
|
|
var _ TearDownInterface = &HelloFSTest{}
|
|
|
|
|
|
|
|
func init() { RegisterTestSuite(&HelloFSTest{}) }
|
|
|
|
|
|
|
|
func (t *HelloFSTest) SetUp(ti *TestInfo) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// Set up a fixed, non-zero time.
|
2015-02-27 06:09:14 +03:00
|
|
|
t.clock.SetTime(time.Now())
|
2015-02-27 00:54:16 +03:00
|
|
|
|
|
|
|
// 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 := &samples.HelloFS{
|
|
|
|
Clock: &t.clock,
|
|
|
|
}
|
|
|
|
|
2015-02-27 00:57:18 +03:00
|
|
|
if t.mfs, err = fuse.Mount(mountPoint, fs); err != nil {
|
2015-02-27 00:54:16 +03:00
|
|
|
panic("Mount: " + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = t.mfs.WaitForReady(context.Background()); err != nil {
|
|
|
|
panic("MountedFileSystem.WaitForReady: " + err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// Test functions
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
func (t *HelloFSTest) ReadDir_Root() {
|
|
|
|
entries, err := ioutil.ReadDir(t.mfs.Dir())
|
|
|
|
|
|
|
|
AssertEq(nil, err)
|
|
|
|
AssertEq(2, len(entries))
|
|
|
|
var fi os.FileInfo
|
|
|
|
|
|
|
|
// dir
|
|
|
|
fi = entries[0]
|
|
|
|
ExpectEq("dir", fi.Name())
|
|
|
|
ExpectEq(0, fi.Size())
|
|
|
|
ExpectEq(os.ModeDir|0500, fi.Mode())
|
2015-02-27 06:01:44 +03:00
|
|
|
ExpectEq(0, t.clock.Now().Sub(fi.ModTime()), "ModTime: %v", fi.ModTime())
|
2015-02-27 00:54:16 +03:00
|
|
|
ExpectTrue(fi.IsDir())
|
|
|
|
|
|
|
|
// hello
|
|
|
|
fi = entries[1]
|
|
|
|
ExpectEq("hello", fi.Name())
|
|
|
|
ExpectEq(len("Hello, world!"), fi.Size())
|
|
|
|
ExpectEq(0400, fi.Mode())
|
2015-02-27 06:01:44 +03:00
|
|
|
ExpectEq(0, t.clock.Now().Sub(fi.ModTime()), "ModTime: %v", fi.ModTime())
|
2015-02-27 00:54:16 +03:00
|
|
|
ExpectFalse(fi.IsDir())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *HelloFSTest) ReadDir_Dir() {
|
2015-02-27 06:09:53 +03:00
|
|
|
entries, err := ioutil.ReadDir(path.Join(t.mfs.Dir(), "dir"))
|
|
|
|
|
|
|
|
AssertEq(nil, err)
|
|
|
|
AssertEq(1, len(entries))
|
|
|
|
var fi os.FileInfo
|
|
|
|
|
|
|
|
// world
|
|
|
|
fi = entries[1]
|
|
|
|
ExpectEq("world", fi.Name())
|
|
|
|
ExpectEq(len("Hello, world!"), fi.Size())
|
|
|
|
ExpectEq(0400, fi.Mode())
|
|
|
|
ExpectEq(0, t.clock.Now().Sub(fi.ModTime()), "ModTime: %v", fi.ModTime())
|
|
|
|
ExpectFalse(fi.IsDir())
|
2015-02-27 00:54:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *HelloFSTest) ReadDir_NonExistent() {
|
|
|
|
AssertTrue(false, "TODO")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *HelloFSTest) Stat_Hello() {
|
|
|
|
AssertTrue(false, "TODO")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *HelloFSTest) Stat_Dir() {
|
|
|
|
AssertTrue(false, "TODO")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *HelloFSTest) Stat_World() {
|
|
|
|
AssertTrue(false, "TODO")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *HelloFSTest) Stat_NonExistent() {
|
|
|
|
AssertTrue(false, "TODO")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *HelloFSTest) Read_Hello() {
|
|
|
|
AssertTrue(false, "TODO")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *HelloFSTest) Read_World() {
|
|
|
|
AssertTrue(false, "TODO")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *HelloFSTest) Open_NonExistent() {
|
|
|
|
AssertTrue(false, "TODO")
|
|
|
|
}
|