fusego/samples/cachingfs/caching_fs_test.go

245 lines
5.8 KiB
Go
Raw Normal View History

2015-03-17 04:44:12 +03:00
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cachingfs_test
import (
"io/ioutil"
"log"
2015-03-17 04:58:25 +03:00
"os"
"path"
2015-03-17 04:44:12 +03:00
"strings"
2015-03-17 05:55:04 +03:00
"syscall"
2015-03-17 04:44:12 +03:00
"testing"
"time"
"github.com/jacobsa/fuse"
"github.com/jacobsa/fuse/samples/cachingfs"
2015-03-17 05:55:42 +03:00
"github.com/jacobsa/gcsfuse/timeutil"
2015-03-17 04:44:12 +03:00
. "github.com/jacobsa/ogletest"
"golang.org/x/net/context"
)
func TestHelloFS(t *testing.T) { RunTests(t) }
////////////////////////////////////////////////////////////////////////
// Boilerplate
////////////////////////////////////////////////////////////////////////
type CachingFSTest struct {
2015-03-17 05:55:04 +03:00
dir string
fs cachingfs.CachingFS
mfs *fuse.MountedFileSystem
initialMtime time.Time
2015-03-17 04:44:12 +03:00
}
var _ TearDownInterface = &CachingFSTest{}
func (t *CachingFSTest) setUp(
lookupEntryTimeout time.Duration,
getattrTimeout time.Duration) {
var err error
// Set up a temporary directory for mounting.
2015-03-17 04:58:25 +03:00
t.dir, err = ioutil.TempDir("", "caching_fs_test")
2015-03-17 04:44:12 +03:00
AssertEq(nil, err)
2015-03-17 05:55:04 +03:00
// Create the file system.
t.fs, err = cachingfs.NewCachingFS(lookupEntryTimeout, getattrTimeout)
2015-03-17 04:44:12 +03:00
AssertEq(nil, err)
// Mount it.
2015-03-17 05:55:04 +03:00
t.mfs, err = fuse.Mount(t.dir, t.fs)
2015-03-17 04:44:12 +03:00
AssertEq(nil, err)
err = t.mfs.WaitForReady(context.Background())
AssertEq(nil, err)
2015-03-17 05:55:04 +03:00
// Set up the mtime.
t.initialMtime = time.Date(2012, 8, 15, 22, 56, 0, 0, time.Local)
t.fs.SetMtime(t.initialMtime)
2015-03-17 04:44:12 +03:00
}
2015-03-17 04:44:42 +03:00
func (t *CachingFSTest) TearDown() {
2015-03-17 04:45:06 +03:00
// Was the file system mounted?
if t.mfs == nil {
return
}
2015-03-17 04:44:12 +03:00
// 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())
}
}
2015-03-17 05:55:04 +03:00
func getInodeID(fi os.FileInfo) uint64 {
return fi.Sys().(*syscall.Stat_t).Ino
}
2015-03-17 04:44:12 +03:00
////////////////////////////////////////////////////////////////////////
2015-03-17 04:50:58 +03:00
// Basics
////////////////////////////////////////////////////////////////////////
type BasicsTest struct {
2015-03-17 04:56:56 +03:00
CachingFSTest
2015-03-17 04:50:58 +03:00
}
2015-03-17 04:56:56 +03:00
var _ SetUpInterface = &BasicsTest{}
2015-03-17 04:50:58 +03:00
func init() { RegisterTestSuite(&BasicsTest{}) }
2015-03-17 04:56:56 +03:00
func (t *BasicsTest) SetUp(ti *TestInfo) {
const (
lookupEntryTimeout = 0
getattrTimeout = 0
)
t.CachingFSTest.setUp(lookupEntryTimeout, getattrTimeout)
}
2015-03-17 05:38:51 +03:00
func (t *BasicsTest) StatNonexistent() {
names := []string{
"blah",
2015-03-17 05:38:51 +03:00
"bar",
"dir/blah",
"dir/dir",
"dir/foo",
}
for _, n := range names {
_, err := os.Stat(path.Join(t.dir, n))
2015-03-17 04:58:25 +03:00
AssertNe(nil, err)
ExpectTrue(os.IsNotExist(err), "n: %s, err: %v", n, err)
}
2015-03-17 04:50:58 +03:00
}
func (t *BasicsTest) StatFoo() {
2015-03-17 05:40:11 +03:00
fi, err := os.Stat(path.Join(t.dir, "foo"))
AssertEq(nil, err)
ExpectEq("foo", fi.Name())
ExpectEq(cachingfs.FooSize, fi.Size())
ExpectEq(0777, fi.Mode())
2015-03-17 05:55:42 +03:00
ExpectThat(fi.ModTime(), timeutil.TimeEq(t.initialMtime))
2015-03-17 05:40:11 +03:00
ExpectFalse(fi.IsDir())
2015-03-17 05:55:04 +03:00
ExpectEq(t.fs.FooID(), getInodeID(fi))
2015-03-17 04:50:58 +03:00
}
func (t *BasicsTest) StatDir() {
2015-03-17 05:47:02 +03:00
fi, err := os.Stat(path.Join(t.dir, "dir"))
AssertEq(nil, err)
ExpectEq("dir", fi.Name())
ExpectEq(os.ModeDir|0777, fi.Mode())
2015-03-17 05:55:42 +03:00
ExpectThat(fi.ModTime(), timeutil.TimeEq(t.initialMtime))
2015-03-17 05:47:02 +03:00
ExpectTrue(fi.IsDir())
2015-03-17 05:55:04 +03:00
ExpectEq(t.fs.DirID(), getInodeID(fi))
2015-03-17 04:50:58 +03:00
}
func (t *BasicsTest) StatBar() {
2015-03-17 05:47:02 +03:00
fi, err := os.Stat(path.Join(t.dir, "dir/bar"))
AssertEq(nil, err)
ExpectEq("bar", fi.Name())
ExpectEq(cachingfs.BarSize, fi.Size())
ExpectEq(0777, fi.Mode())
2015-03-17 05:55:42 +03:00
ExpectThat(fi.ModTime(), timeutil.TimeEq(t.initialMtime))
2015-03-17 05:47:02 +03:00
ExpectFalse(fi.IsDir())
2015-03-17 05:55:04 +03:00
ExpectEq(t.fs.BarID(), getInodeID(fi))
2015-03-17 04:50:58 +03:00
}
////////////////////////////////////////////////////////////////////////
// No caching
2015-03-17 04:44:12 +03:00
////////////////////////////////////////////////////////////////////////
2015-03-17 04:50:58 +03:00
type NoCachingTest struct {
CachingFSTest
}
var _ SetUpInterface = &NoCachingTest{}
func init() { RegisterTestSuite(&NoCachingTest{}) }
func (t *NoCachingTest) SetUp(ti *TestInfo) {
const (
lookupEntryTimeout = 0
getattrTimeout = 0
)
t.CachingFSTest.setUp(lookupEntryTimeout, getattrTimeout)
}
func (t *NoCachingTest) StatStat() {
2015-03-17 05:55:04 +03:00
var err error
// Stat everything.
fooBefore, err := os.Stat(path.Join(t.dir, "foo"))
AssertEq(nil, err)
dirBefore, err := os.Stat(path.Join(t.dir, "dir"))
AssertEq(nil, err)
2015-03-17 05:56:37 +03:00
barBefore, err := os.Stat(path.Join(t.dir, "dir/bar"))
2015-03-17 05:55:04 +03:00
AssertEq(nil, err)
// Stat again.
fooAfter, err := os.Stat(path.Join(t.dir, "foo"))
AssertEq(nil, err)
dirAfter, err := os.Stat(path.Join(t.dir, "dir"))
AssertEq(nil, err)
2015-03-17 05:56:37 +03:00
barAfter, err := os.Stat(path.Join(t.dir, "dir/bar"))
2015-03-17 05:55:04 +03:00
AssertEq(nil, err)
// Make sure everything matches.
2015-03-17 05:56:37 +03:00
ExpectThat(fooAfter.ModTime(), timeutil.TimeEq(fooBefore.ModTime()))
ExpectThat(dirAfter.ModTime(), timeutil.TimeEq(dirBefore.ModTime()))
ExpectThat(barAfter.ModTime(), timeutil.TimeEq(barBefore.ModTime()))
2015-03-17 05:55:04 +03:00
ExpectEq(getInodeID(fooBefore), getInodeID(fooAfter))
ExpectEq(getInodeID(dirBefore), getInodeID(dirAfter))
ExpectEq(getInodeID(barBefore), getInodeID(barAfter))
2015-03-17 04:50:58 +03:00
}
func (t *NoCachingTest) StatRenumberStat() {
AssertTrue(false, "TODO")
}
func (t *NoCachingTest) StatMtimeStat() {
AssertTrue(false, "TODO")
}
func (t *NoCachingTest) StatRenumberMtimeStat() {
2015-03-17 04:44:12 +03:00
AssertTrue(false, "TODO")
}