fusego/samples/hellofs/hello_fs_test.go

227 lines
5.6 KiB
Go
Raw Permalink Normal View History

// Copyright 2015 Google Inc. All Rights Reserved.
2015-03-04 00:27:42 +03:00
//
// 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.
2015-02-27 08:31:04 +03:00
package hellofs_test
import (
2015-02-27 08:06:55 +03:00
"io"
"io/ioutil"
"os"
2015-02-27 06:09:53 +03:00
"path"
"syscall"
"testing"
2015-04-01 01:05:26 +03:00
"github.com/jacobsa/fuse/fusetesting"
2015-03-20 02:50:36 +03:00
"github.com/jacobsa/fuse/samples"
2015-03-02 05:59:32 +03:00
"github.com/jacobsa/fuse/samples/hellofs"
2015-02-27 06:44:03 +03:00
. "github.com/jacobsa/oglematchers"
. "github.com/jacobsa/ogletest"
)
func TestHelloFS(t *testing.T) { RunTests(t) }
////////////////////////////////////////////////////////////////////////
// Boilerplate
////////////////////////////////////////////////////////////////////////
type HelloFSTest struct {
2015-03-20 02:50:36 +03:00
samples.SampleTest
}
func init() { RegisterTestSuite(&HelloFSTest{}) }
func (t *HelloFSTest) SetUp(ti *TestInfo) {
2015-03-24 07:47:26 +03:00
var err error
t.Server, err = hellofs.NewHelloFS(&t.Clock)
AssertEq(nil, err)
t.SampleTest.SetUp(ti)
}
////////////////////////////////////////////////////////////////////////
// Test functions
////////////////////////////////////////////////////////////////////////
func (t *HelloFSTest) ReadDir_Root() {
2015-04-01 01:05:26 +03:00
entries, err := fusetesting.ReadDirPicky(t.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|0555, fi.Mode())
2015-03-20 02:50:36 +03:00
ExpectEq(0, t.Clock.Now().Sub(fi.ModTime()), "ModTime: %v", fi.ModTime())
ExpectTrue(fi.IsDir())
// hello
fi = entries[1]
ExpectEq("hello", fi.Name())
ExpectEq(len("Hello, world!"), fi.Size())
ExpectEq(0444, fi.Mode())
2015-03-20 02:50:36 +03:00
ExpectEq(0, t.Clock.Now().Sub(fi.ModTime()), "ModTime: %v", fi.ModTime())
ExpectFalse(fi.IsDir())
}
func (t *HelloFSTest) ReadDir_Dir() {
2015-04-01 01:05:26 +03:00
entries, err := fusetesting.ReadDirPicky(path.Join(t.Dir, "dir"))
2015-02-27 06:09:53 +03:00
AssertEq(nil, err)
AssertEq(1, len(entries))
var fi os.FileInfo
// world
2015-02-27 06:43:05 +03:00
fi = entries[0]
2015-02-27 06:09:53 +03:00
ExpectEq("world", fi.Name())
ExpectEq(len("Hello, world!"), fi.Size())
ExpectEq(0444, fi.Mode())
2015-03-20 02:50:36 +03:00
ExpectEq(0, t.Clock.Now().Sub(fi.ModTime()), "ModTime: %v", fi.ModTime())
2015-02-27 06:09:53 +03:00
ExpectFalse(fi.IsDir())
}
func (t *HelloFSTest) ReadDir_NonExistent() {
2015-04-01 01:05:26 +03:00
_, err := fusetesting.ReadDirPicky(path.Join(t.Dir, "foobar"))
2015-02-27 06:44:03 +03:00
AssertNe(nil, err)
ExpectThat(err, Error(HasSubstr("no such file")))
}
2015-02-27 06:53:08 +03:00
func (t *HelloFSTest) Stat_Hello() {
2015-03-20 02:50:36 +03:00
fi, err := os.Stat(path.Join(t.Dir, "hello"))
2015-02-27 06:53:08 +03:00
AssertEq(nil, err)
ExpectEq("hello", fi.Name())
ExpectEq(len("Hello, world!"), fi.Size())
ExpectEq(0444, fi.Mode())
2015-03-20 02:50:36 +03:00
ExpectEq(0, t.Clock.Now().Sub(fi.ModTime()), "ModTime: %v", fi.ModTime())
2015-02-27 06:53:08 +03:00
ExpectFalse(fi.IsDir())
ExpectEq(1, fi.Sys().(*syscall.Stat_t).Nlink)
2015-02-27 06:53:08 +03:00
}
func (t *HelloFSTest) Stat_Dir() {
2015-03-20 02:50:36 +03:00
fi, err := os.Stat(path.Join(t.Dir, "dir"))
2015-02-27 06:54:39 +03:00
AssertEq(nil, err)
ExpectEq("dir", fi.Name())
ExpectEq(0, fi.Size())
ExpectEq(0555|os.ModeDir, fi.Mode())
2015-03-20 02:50:36 +03:00
ExpectEq(0, t.Clock.Now().Sub(fi.ModTime()), "ModTime: %v", fi.ModTime())
2015-02-27 06:54:39 +03:00
ExpectTrue(fi.IsDir())
ExpectEq(1, fi.Sys().(*syscall.Stat_t).Nlink)
}
func (t *HelloFSTest) Stat_World() {
2015-03-20 02:50:36 +03:00
fi, err := os.Stat(path.Join(t.Dir, "dir/world"))
2015-02-27 06:54:39 +03:00
AssertEq(nil, err)
ExpectEq("world", fi.Name())
ExpectEq(len("Hello, world!"), fi.Size())
ExpectEq(0444, fi.Mode())
2015-03-20 02:50:36 +03:00
ExpectEq(0, t.Clock.Now().Sub(fi.ModTime()), "ModTime: %v", fi.ModTime())
2015-02-27 06:54:39 +03:00
ExpectFalse(fi.IsDir())
ExpectEq(1, fi.Sys().(*syscall.Stat_t).Nlink)
}
func (t *HelloFSTest) Stat_NonExistent() {
2015-03-20 02:50:36 +03:00
_, err := os.Stat(path.Join(t.Dir, "foobar"))
2015-02-27 06:55:02 +03:00
AssertNe(nil, err)
ExpectThat(err, Error(HasSubstr("no such file")))
}
2015-02-27 06:56:57 +03:00
func (t *HelloFSTest) ReadFile_Hello() {
2015-03-20 02:50:36 +03:00
slice, err := ioutil.ReadFile(path.Join(t.Dir, "hello"))
2015-02-27 06:56:57 +03:00
AssertEq(nil, err)
ExpectEq("Hello, world!", string(slice))
}
func (t *HelloFSTest) ReadFile_Dir() {
2015-03-20 02:50:36 +03:00
_, err := ioutil.ReadFile(path.Join(t.Dir, "dir"))
2015-02-27 07:58:14 +03:00
AssertNe(nil, err)
ExpectThat(err, Error(HasSubstr("is a directory")))
2015-02-27 06:56:57 +03:00
}
func (t *HelloFSTest) ReadFile_World() {
2015-03-20 02:50:36 +03:00
slice, err := ioutil.ReadFile(path.Join(t.Dir, "dir/world"))
2015-02-27 07:57:32 +03:00
AssertEq(nil, err)
ExpectEq("Hello, world!", string(slice))
2015-02-27 06:56:57 +03:00
}
2015-02-27 08:06:55 +03:00
func (t *HelloFSTest) OpenAndRead() {
var buf []byte = make([]byte, 1024)
var n int
var off int64
var err error
2015-02-27 08:06:55 +03:00
// Open the file.
2015-03-20 02:50:36 +03:00
f, err := os.Open(path.Join(t.Dir, "hello"))
2015-02-27 08:06:55 +03:00
defer func() {
if f != nil {
ExpectEq(nil, f.Close())
}
}()
2015-02-27 06:56:57 +03:00
2015-02-27 08:06:55 +03:00
AssertEq(nil, err)
// Seeking shouldn't affect the random access reads below.
_, err = f.Seek(7, 0)
AssertEq(nil, err)
// Random access reads
n, err = f.ReadAt(buf[:2], 0)
AssertEq(nil, err)
ExpectEq(2, n)
ExpectEq("He", string(buf[:n]))
n, err = f.ReadAt(buf[:2], int64(len("Hel")))
AssertEq(nil, err)
ExpectEq(2, n)
ExpectEq("lo", string(buf[:n]))
n, err = f.ReadAt(buf[:3], int64(len("Hello, wo")))
AssertEq(nil, err)
ExpectEq(3, n)
ExpectEq("rld", string(buf[:n]))
// Read beyond end.
n, err = f.ReadAt(buf[:3], int64(len("Hello, world")))
AssertEq(io.EOF, err)
ExpectEq(1, n)
ExpectEq("!", string(buf[:n]))
// Seek then read the rest.
off, err = f.Seek(int64(len("Hel")), 0)
AssertEq(nil, err)
AssertEq(len("Hel"), off)
n, err = io.ReadFull(f, buf[:len("lo, world!")])
AssertEq(nil, err)
ExpectEq(len("lo, world!"), n)
ExpectEq("lo, world!", string(buf[:n]))
}
func (t *HelloFSTest) Open_NonExistent() {
2015-03-20 02:50:36 +03:00
_, err := os.Open(path.Join(t.Dir, "foobar"))
2015-02-27 08:07:47 +03:00
AssertNe(nil, err)
ExpectThat(err, Error(HasSubstr("no such file")))
}