fusego/samples/flushfs/flush_fs_test.go

1046 lines
24 KiB
Go
Raw Normal View History

2015-03-20 03:17:05 +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 flushfs_test
import (
2015-03-24 00:51:19 +03:00
"bufio"
"encoding/hex"
2015-03-23 08:34:39 +03:00
"fmt"
2015-03-20 03:33:15 +03:00
"io"
2015-05-18 03:11:03 +03:00
"io/ioutil"
2015-03-20 03:33:15 +03:00
"os"
"path"
2015-03-23 02:41:59 +03:00
"runtime"
2015-03-20 05:44:34 +03:00
"syscall"
2015-03-20 03:17:05 +03:00
"testing"
2015-05-18 03:14:19 +03:00
"time"
2015-03-24 04:56:01 +03:00
"unsafe"
"golang.org/x/sys/unix"
2015-03-20 03:17:05 +03:00
"github.com/jacobsa/fuse/fsutil"
2015-05-18 02:57:05 +03:00
"github.com/jacobsa/fuse/fusetesting"
2015-03-20 03:17:05 +03:00
"github.com/jacobsa/fuse/samples"
2015-03-20 03:33:15 +03:00
. "github.com/jacobsa/oglematchers"
2015-03-20 03:17:05 +03:00
. "github.com/jacobsa/ogletest"
)
func TestFlushFS(t *testing.T) { RunTests(t) }
////////////////////////////////////////////////////////////////////////
// Boilerplate
////////////////////////////////////////////////////////////////////////
2015-03-23 08:12:02 +03:00
type flushFSTest struct {
2015-03-23 08:34:39 +03:00
samples.SubprocessTest
2015-03-20 03:17:05 +03:00
2015-03-24 00:42:39 +03:00
// Files to which mount_sample is writing reported flushes and fsyncs.
flushes *os.File
fsyncs *os.File
2015-03-23 02:32:30 +03:00
// File handles that are closed in TearDown if non-nil.
f1 *os.File
f2 *os.File
2015-03-20 03:17:05 +03:00
}
2015-03-23 08:12:02 +03:00
func (t *flushFSTest) setUp(
ti *TestInfo,
2015-07-24 07:02:55 +03:00
flushErr syscall.Errno,
fsyncErr syscall.Errno,
2015-05-18 02:54:34 +03:00
readOnly bool) {
2015-03-20 03:21:38 +03:00
var err error
2015-03-23 08:34:39 +03:00
// Set up files to receive flush and fsync reports.
t.flushes, err = fsutil.AnonymousFile("")
2015-03-24 00:42:39 +03:00
AssertEq(nil, err)
t.fsyncs, err = fsutil.AnonymousFile("")
2015-03-24 00:42:39 +03:00
AssertEq(nil, err)
2015-03-20 03:52:54 +03:00
2015-03-23 08:34:39 +03:00
// Set up test config.
t.MountType = "flushfs"
t.MountFlags = []string{
"--flushfs.flush_error",
fmt.Sprintf("%d", int(flushErr)),
2015-03-20 03:21:38 +03:00
2015-03-24 01:26:16 +03:00
"--flushfs.fsync_error",
2015-03-23 08:34:39 +03:00
fmt.Sprintf("%d", int(fsyncErr)),
2015-03-20 03:21:38 +03:00
}
2015-05-18 02:54:34 +03:00
if readOnly {
t.MountFlags = append(t.MountFlags, "--read_only")
}
2015-03-24 01:46:05 +03:00
t.MountFiles = map[string]*os.File{
"flushfs.flushes_file": t.flushes,
"flushfs.fsyncs_file": t.fsyncs,
}
2015-03-23 08:34:39 +03:00
t.SubprocessTest.SetUp(ti)
2015-03-20 03:20:29 +03:00
}
2015-03-20 03:17:05 +03:00
2015-03-24 00:43:03 +03:00
func (t *flushFSTest) TearDown() {
2015-03-24 00:42:39 +03:00
// Unlink reporting files.
os.Remove(t.flushes.Name())
os.Remove(t.fsyncs.Name())
// Close reporting files.
t.flushes.Close()
t.fsyncs.Close()
// Close test files if non-nil.
2015-03-23 02:32:30 +03:00
if t.f1 != nil {
ExpectEq(nil, t.f1.Close())
}
if t.f2 != nil {
ExpectEq(nil, t.f2.Close())
}
2015-03-23 02:37:32 +03:00
// Finish tearing down.
2015-03-24 00:43:03 +03:00
t.SubprocessTest.TearDown()
2015-03-23 02:32:30 +03:00
}
2015-03-20 03:17:05 +03:00
////////////////////////////////////////////////////////////////////////
2015-03-20 03:25:15 +03:00
// Helpers
2015-03-20 03:17:05 +03:00
////////////////////////////////////////////////////////////////////////
var isDarwin = runtime.GOOS == "darwin"
2015-03-24 00:51:19 +03:00
func readReports(f *os.File) (reports []string, err error) {
// Seek the file to the start.
if _, err := f.Seek(0, 0); err != nil {
return nil, fmt.Errorf("Seek: %v", err)
2015-03-24 00:51:19 +03:00
}
// We expect reports to end in a newline (including the final one).
reader := bufio.NewReader(f)
for {
record, err := reader.ReadBytes('\n')
2015-03-24 00:51:19 +03:00
if err == io.EOF {
if len(record) != 0 {
return nil, fmt.Errorf("Unexpected record:\n%s", hex.Dump(record))
2015-03-24 00:51:19 +03:00
}
return reports, nil
2015-03-24 00:51:19 +03:00
}
if err != nil {
return nil, fmt.Errorf("ReadBytes: %v", err)
2015-03-24 00:51:19 +03:00
}
// Strip the newline.
reports = append(reports, string(record[:len(record)-1]))
}
}
2015-03-24 00:45:01 +03:00
2015-03-20 03:25:15 +03:00
// Return a copy of the current contents of t.flushes.
func (t *flushFSTest) getFlushes() []string {
p, err := readReports(t.flushes)
if err != nil {
2015-03-24 00:45:01 +03:00
panic(err)
}
return p
2015-03-20 03:26:32 +03:00
}
2015-03-20 03:25:15 +03:00
// Return a copy of the current contents of t.fsyncs.
func (t *flushFSTest) getFsyncs() []string {
p, err := readReports(t.fsyncs)
if err != nil {
2015-03-24 00:45:01 +03:00
panic(err)
}
return p
2015-03-20 04:01:59 +03:00
}
2015-03-23 02:27:22 +03:00
// Like syscall.Dup2, but correctly annotates the syscall as blocking. See here
// for more info: https://github.com/golang/go/issues/10202
func dup2(oldfd int, newfd int) error {
2015-03-23 02:27:22 +03:00
_, _, e1 := syscall.Syscall(
syscall.SYS_DUP2, uintptr(oldfd), uintptr(newfd), 0)
if e1 != 0 {
return e1
2015-03-23 02:27:22 +03:00
}
return nil
2015-03-23 02:27:22 +03:00
}
// Call msync(2) with the MS_SYNC flag on a slice previously returned by
// mmap(2).
func msync(p []byte) error {
2015-03-24 04:56:01 +03:00
_, _, errno := unix.Syscall(
2015-03-24 05:00:18 +03:00
unix.SYS_MSYNC,
2015-03-24 04:56:01 +03:00
uintptr(unsafe.Pointer(&p[0])),
uintptr(len(p)),
unix.MS_SYNC)
2015-03-24 04:56:01 +03:00
if errno != 0 {
return errno
2015-03-24 04:56:01 +03:00
}
return nil
2015-03-24 04:56:01 +03:00
}
2015-03-24 04:51:19 +03:00
2015-03-20 03:25:15 +03:00
////////////////////////////////////////////////////////////////////////
2015-03-24 00:39:22 +03:00
// No errors
2015-03-20 03:25:15 +03:00
////////////////////////////////////////////////////////////////////////
2015-03-24 00:39:22 +03:00
type NoErrorsTest struct {
flushFSTest
}
func init() { RegisterTestSuite(&NoErrorsTest{}) }
2015-03-24 00:52:05 +03:00
func (t *NoErrorsTest) SetUp(ti *TestInfo) {
const noErr = 0
2015-05-18 02:54:34 +03:00
t.flushFSTest.setUp(ti, noErr, noErr, false)
2015-03-24 00:52:05 +03:00
}
2015-03-24 00:39:22 +03:00
2015-03-24 00:40:35 +03:00
func (t *NoErrorsTest) Close_ReadWrite() {
2015-03-20 03:33:15 +03:00
var n int
var off int64
var err error
buf := make([]byte, 1024)
// Open the file.
2015-03-23 02:32:30 +03:00
t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_RDWR, 0)
2015-03-20 03:33:15 +03:00
AssertEq(nil, err)
// Write some contents to the file.
2015-03-23 02:32:30 +03:00
n, err = t.f1.Write([]byte("taco"))
2015-03-20 03:33:15 +03:00
AssertEq(nil, err)
AssertEq(4, n)
// Seek and read them back.
2015-03-23 02:32:30 +03:00
off, err = t.f1.Seek(0, 0)
2015-03-20 03:33:15 +03:00
AssertEq(nil, err)
2015-03-20 03:49:45 +03:00
AssertEq(0, off)
2015-03-20 03:33:15 +03:00
2015-03-23 02:32:30 +03:00
n, err = t.f1.Read(buf)
2015-03-20 03:33:15 +03:00
AssertThat(err, AnyOf(nil, io.EOF))
2015-03-20 03:49:45 +03:00
AssertEq("taco", string(buf[:n]))
2015-03-20 03:33:15 +03:00
// At this point, no flushes or fsyncs should have happened.
AssertThat(t.getFlushes(), ElementsAre())
AssertThat(t.getFsyncs(), ElementsAre())
// Close the file.
2015-03-23 02:32:30 +03:00
err = t.f1.Close()
t.f1 = nil
2015-03-20 03:33:15 +03:00
AssertEq(nil, err)
// Now we should have received the flush operation (but still no fsync).
2015-03-20 03:57:21 +03:00
ExpectThat(t.getFlushes(), ElementsAre("taco"))
2015-03-20 03:33:15 +03:00
ExpectThat(t.getFsyncs(), ElementsAre())
}
2015-03-24 00:40:35 +03:00
func (t *NoErrorsTest) Close_ReadOnly() {
2015-03-20 03:54:09 +03:00
var err error
// Open the file.
2015-03-23 02:32:30 +03:00
t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_RDONLY, 0)
2015-03-20 03:54:09 +03:00
AssertEq(nil, err)
// At this point, no flushes or fsyncs should have happened.
AssertThat(t.getFlushes(), ElementsAre())
AssertThat(t.getFsyncs(), ElementsAre())
// Close the file.
2015-03-23 02:32:30 +03:00
err = t.f1.Close()
t.f1 = nil
2015-03-20 03:54:09 +03:00
AssertEq(nil, err)
// Now we should have received the flush operation (but still no fsync).
2015-03-20 03:57:21 +03:00
ExpectThat(t.getFlushes(), ElementsAre(""))
2015-03-20 03:54:09 +03:00
ExpectThat(t.getFsyncs(), ElementsAre())
2015-03-20 03:33:15 +03:00
}
2015-03-24 00:40:35 +03:00
func (t *NoErrorsTest) Close_WriteOnly() {
2015-03-20 03:54:43 +03:00
var n int
var err error
// Open the file.
2015-03-23 02:32:30 +03:00
t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_WRONLY, 0)
2015-03-20 03:54:43 +03:00
AssertEq(nil, err)
// Write some contents to the file.
2015-03-23 02:32:30 +03:00
n, err = t.f1.Write([]byte("taco"))
2015-03-20 03:54:43 +03:00
AssertEq(nil, err)
AssertEq(4, n)
// At this point, no flushes or fsyncs should have happened.
AssertThat(t.getFlushes(), ElementsAre())
AssertThat(t.getFsyncs(), ElementsAre())
// Close the file.
2015-03-23 02:32:30 +03:00
err = t.f1.Close()
t.f1 = nil
2015-03-20 03:54:43 +03:00
AssertEq(nil, err)
// Now we should have received the flush operation (but still no fsync).
2015-03-20 03:57:21 +03:00
ExpectThat(t.getFlushes(), ElementsAre("taco"))
2015-03-20 03:54:43 +03:00
ExpectThat(t.getFsyncs(), ElementsAre())
2015-03-20 03:33:15 +03:00
}
2015-03-24 00:40:35 +03:00
func (t *NoErrorsTest) Close_MultipleTimes_NonOverlappingFileHandles() {
var n int
var err error
// Open the file.
2015-03-23 02:32:30 +03:00
t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_WRONLY, 0)
AssertEq(nil, err)
// Write some contents to the file.
2015-03-23 02:32:30 +03:00
n, err = t.f1.Write([]byte("taco"))
AssertEq(nil, err)
AssertEq(4, n)
// At this point, no flushes or fsyncs should have happened.
AssertThat(t.getFlushes(), ElementsAre())
AssertThat(t.getFsyncs(), ElementsAre())
// Close the file.
2015-03-23 02:32:30 +03:00
err = t.f1.Close()
t.f1 = nil
AssertEq(nil, err)
// Now we should have received the flush operation (but still no fsync).
2015-03-20 03:57:21 +03:00
AssertThat(t.getFlushes(), ElementsAre("taco"))
AssertThat(t.getFsyncs(), ElementsAre())
// Open the file again.
2015-03-23 02:32:30 +03:00
t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_WRONLY, 0)
AssertEq(nil, err)
// Write again; expect no further flushes.
2015-03-23 02:32:30 +03:00
n, err = t.f1.Write([]byte("p"))
AssertEq(nil, err)
AssertEq(1, n)
2015-03-20 03:57:21 +03:00
AssertThat(t.getFlushes(), ElementsAre("taco"))
AssertThat(t.getFsyncs(), ElementsAre())
// Close the file. Now the new contents should be flushed.
2015-03-23 02:32:30 +03:00
err = t.f1.Close()
t.f1 = nil
AssertEq(nil, err)
2015-03-20 03:57:21 +03:00
AssertThat(t.getFlushes(), ElementsAre("taco", "paco"))
AssertThat(t.getFsyncs(), ElementsAre())
2015-03-20 03:25:15 +03:00
}
2015-03-24 00:40:35 +03:00
func (t *NoErrorsTest) Close_MultipleTimes_OverlappingFileHandles() {
var n int
var err error
// Open the file with two handles.
2015-03-23 02:32:30 +03:00
t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_WRONLY, 0)
AssertEq(nil, err)
2015-03-23 02:32:30 +03:00
t.f2, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_WRONLY, 0)
AssertEq(nil, err)
// Write some contents with each handle.
2015-03-23 02:32:30 +03:00
n, err = t.f1.Write([]byte("taco"))
AssertEq(nil, err)
AssertEq(4, n)
2015-03-23 02:32:30 +03:00
n, err = t.f2.Write([]byte("p"))
AssertEq(nil, err)
AssertEq(1, n)
// At this point, no flushes or fsyncs should have happened.
AssertThat(t.getFlushes(), ElementsAre())
AssertThat(t.getFsyncs(), ElementsAre())
// Close one handle. The current contents should be flushed.
2015-03-23 02:32:30 +03:00
err = t.f1.Close()
t.f1 = nil
AssertEq(nil, err)
AssertThat(t.getFlushes(), ElementsAre("paco"))
AssertThat(t.getFsyncs(), ElementsAre())
// Write some more contents via the other handle. Again, no further flushes.
2015-03-23 02:32:30 +03:00
n, err = t.f2.Write([]byte("orp"))
AssertEq(nil, err)
AssertEq(3, n)
AssertThat(t.getFlushes(), ElementsAre("paco"))
AssertThat(t.getFsyncs(), ElementsAre())
// Close the handle. Now the new contents should be flushed.
2015-03-23 02:32:30 +03:00
err = t.f2.Close()
t.f2 = nil
AssertEq(nil, err)
AssertThat(t.getFlushes(), ElementsAre("paco", "porp"))
AssertThat(t.getFsyncs(), ElementsAre())
2015-03-20 03:25:15 +03:00
}
2015-03-24 00:40:35 +03:00
func (t *NoErrorsTest) Fsync() {
2015-03-20 04:03:25 +03:00
var n int
var err error
// Open the file.
2015-03-23 02:32:30 +03:00
t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_WRONLY, 0)
2015-03-20 04:03:25 +03:00
AssertEq(nil, err)
// Write some contents to the file.
2015-03-23 02:32:30 +03:00
n, err = t.f1.Write([]byte("taco"))
2015-03-20 04:03:25 +03:00
AssertEq(nil, err)
AssertEq(4, n)
AssertThat(t.getFlushes(), ElementsAre())
AssertThat(t.getFsyncs(), ElementsAre())
// Fsync.
2015-03-23 02:32:30 +03:00
err = t.f1.Sync()
2015-03-20 04:03:25 +03:00
AssertEq(nil, err)
AssertThat(t.getFlushes(), ElementsAre())
AssertThat(t.getFsyncs(), ElementsAre("taco"))
// Write some more contents.
2015-03-23 02:32:30 +03:00
n, err = t.f1.Write([]byte("s"))
2015-03-20 04:03:25 +03:00
AssertEq(nil, err)
AssertEq(1, n)
AssertThat(t.getFlushes(), ElementsAre())
AssertThat(t.getFsyncs(), ElementsAre("taco"))
// Fsync.
2015-03-23 02:32:30 +03:00
err = t.f1.Sync()
2015-03-20 04:03:25 +03:00
AssertEq(nil, err)
AssertThat(t.getFlushes(), ElementsAre())
AssertThat(t.getFsyncs(), ElementsAre("taco", "tacos"))
2015-03-20 03:23:15 +03:00
}
2015-03-24 04:04:16 +03:00
func (t *NoErrorsTest) Fdatasync() {
2015-03-24 04:22:25 +03:00
var n int
var err error
if !fsutil.FdatasyncSupported {
return
}
2015-03-24 04:22:25 +03:00
// Open the file.
t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_WRONLY, 0)
AssertEq(nil, err)
// Write some contents to the file.
n, err = t.f1.Write([]byte("taco"))
AssertEq(nil, err)
AssertEq(4, n)
AssertThat(t.getFlushes(), ElementsAre())
AssertThat(t.getFsyncs(), ElementsAre())
// Fdatasync.
err = fsutil.Fdatasync(t.f1)
2015-03-24 04:22:25 +03:00
AssertEq(nil, err)
AssertThat(t.getFlushes(), ElementsAre())
AssertThat(t.getFsyncs(), ElementsAre("taco"))
// Write some more contents.
n, err = t.f1.Write([]byte("s"))
AssertEq(nil, err)
AssertEq(1, n)
AssertThat(t.getFlushes(), ElementsAre())
AssertThat(t.getFsyncs(), ElementsAre("taco"))
// Fdatasync.
err = fsutil.Fdatasync(t.f1)
2015-03-24 04:22:25 +03:00
AssertEq(nil, err)
AssertThat(t.getFlushes(), ElementsAre())
AssertThat(t.getFsyncs(), ElementsAre("taco", "tacos"))
2015-03-24 04:04:16 +03:00
}
2015-03-24 00:40:35 +03:00
func (t *NoErrorsTest) Dup() {
2015-03-20 05:44:34 +03:00
var n int
var err error
2015-03-23 02:41:59 +03:00
var expectedFlushes []interface{}
2015-03-20 05:44:34 +03:00
// Open the file.
2015-03-23 02:32:30 +03:00
t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_WRONLY, 0)
2015-03-20 05:44:34 +03:00
AssertEq(nil, err)
2015-03-23 02:32:30 +03:00
fd1 := t.f1.Fd()
2015-03-20 05:44:34 +03:00
// Use dup(2) to get another copy.
fd2, err := syscall.Dup(int(fd1))
AssertEq(nil, err)
2015-03-23 02:32:30 +03:00
t.f2 = os.NewFile(uintptr(fd2), t.f1.Name())
2015-03-20 05:44:34 +03:00
// Write some contents with each handle.
2015-03-23 02:32:30 +03:00
n, err = t.f1.Write([]byte("taco"))
2015-03-20 05:44:34 +03:00
AssertEq(nil, err)
AssertEq(4, n)
2015-03-23 02:32:30 +03:00
n, err = t.f2.Write([]byte("s"))
2015-03-20 05:44:34 +03:00
AssertEq(nil, err)
AssertEq(1, n)
// At this point, no flushes or fsyncs should have happened.
AssertThat(t.getFlushes(), ElementsAre())
AssertThat(t.getFsyncs(), ElementsAre())
2015-03-23 02:41:59 +03:00
// Close one handle. On Linux the current contents should be flushed. On OS
// X, where the semantics of handles are different, they apparently are not.
// (Cf. https://github.com/osxfuse/osxfuse/issues/199)
2015-03-23 02:32:30 +03:00
err = t.f1.Close()
t.f1 = nil
2015-03-20 05:44:34 +03:00
AssertEq(nil, err)
2015-03-23 02:41:59 +03:00
if !isDarwin {
expectedFlushes = append(expectedFlushes, "tacos")
}
AssertThat(t.getFlushes(), ElementsAre(expectedFlushes...))
2015-03-20 05:44:34 +03:00
AssertThat(t.getFsyncs(), ElementsAre())
// Write some more contents via the other handle. Again, no further flushes.
2015-03-23 02:32:30 +03:00
n, err = t.f2.Write([]byte("!"))
2015-03-20 05:44:34 +03:00
AssertEq(nil, err)
2015-03-20 05:47:47 +03:00
AssertEq(1, n)
2015-03-20 05:44:34 +03:00
2015-03-23 02:41:59 +03:00
AssertThat(t.getFlushes(), ElementsAre(expectedFlushes...))
2015-03-20 05:44:34 +03:00
AssertThat(t.getFsyncs(), ElementsAre())
// Close the handle. Now the new contents should be flushed.
2015-03-23 02:32:30 +03:00
err = t.f2.Close()
t.f2 = nil
2015-03-20 05:44:34 +03:00
AssertEq(nil, err)
2015-03-23 02:41:59 +03:00
expectedFlushes = append(expectedFlushes, "tacos!")
ExpectThat(t.getFlushes(), ElementsAre(expectedFlushes...))
ExpectThat(t.getFsyncs(), ElementsAre())
2015-03-20 05:21:14 +03:00
}
2015-03-24 00:40:35 +03:00
func (t *NoErrorsTest) Dup2() {
2015-03-20 06:48:47 +03:00
var n int
var err error
// Open the file.
2015-03-23 02:32:30 +03:00
t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_WRONLY, 0)
2015-03-20 06:48:47 +03:00
AssertEq(nil, err)
// Write some contents to the file.
2015-03-23 02:32:30 +03:00
n, err = t.f1.Write([]byte("taco"))
2015-03-20 06:48:47 +03:00
AssertEq(nil, err)
AssertEq(4, n)
// Create some anonymous temporary file.
t.f2, err = fsutil.AnonymousFile("")
2015-03-20 06:48:47 +03:00
AssertEq(nil, err)
// Duplicate the temporary file descriptor on top of the file from our file
// system. We should see a flush.
2015-03-23 02:32:30 +03:00
err = dup2(int(t.f2.Fd()), int(t.f1.Fd()))
2015-03-20 06:48:47 +03:00
ExpectEq(nil, err)
ExpectThat(t.getFlushes(), ElementsAre("taco"))
ExpectThat(t.getFsyncs(), ElementsAre())
2015-03-20 05:21:14 +03:00
}
2015-03-24 04:51:19 +03:00
func (t *NoErrorsTest) Mmap_NoMsync_MunmapBeforeClose() {
2015-03-23 03:40:36 +03:00
var n int
var err error
// Open the file.
t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_RDWR, 0)
AssertEq(nil, err)
// Write some contents to the file.
n, err = t.f1.Write([]byte("taco"))
AssertEq(nil, err)
AssertEq(4, n)
// mmap the file.
data, err := syscall.Mmap(
int(t.f1.Fd()), 0, 4,
syscall.PROT_READ|syscall.PROT_WRITE,
syscall.MAP_SHARED)
AssertEq(nil, err)
defer syscall.Munmap(data)
2015-03-23 03:40:36 +03:00
AssertEq("taco", string(data))
// Modify the contents.
2015-03-23 03:40:36 +03:00
data[0] = 'p'
// Unmap.
2015-03-23 03:40:36 +03:00
err = syscall.Munmap(data)
AssertEq(nil, err)
// munmap does not cause a flush.
ExpectThat(t.getFlushes(), ElementsAre())
ExpectThat(t.getFsyncs(), ElementsAre())
// Close the file. We should see a flush with up to date contents.
2015-03-23 03:40:36 +03:00
err = t.f1.Close()
t.f1 = nil
AssertEq(nil, err)
ExpectThat(t.getFlushes(), ElementsAre("paco"))
ExpectThat(t.getFsyncs(), ElementsAre())
2015-03-23 03:40:03 +03:00
}
2015-03-24 04:51:19 +03:00
func (t *NoErrorsTest) Mmap_NoMsync_CloseBeforeMunmap() {
2015-03-23 02:52:37 +03:00
var n int
var err error
// Open the file.
t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_RDWR, 0)
AssertEq(nil, err)
// Write some contents to the file.
n, err = t.f1.Write([]byte("taco"))
AssertEq(nil, err)
AssertEq(4, n)
// mmap the file.
data, err := syscall.Mmap(
int(t.f1.Fd()), 0, 4,
syscall.PROT_READ|syscall.PROT_WRITE,
syscall.MAP_SHARED)
AssertEq(nil, err)
defer syscall.Munmap(data)
2015-03-23 02:52:37 +03:00
AssertEq("taco", string(data))
// Close the file. We should see a flush.
err = t.f1.Close()
t.f1 = nil
AssertEq(nil, err)
AssertThat(t.getFlushes(), ElementsAre("taco"))
AssertThat(t.getFsyncs(), ElementsAre())
// Modify the contents.
2015-03-23 02:52:37 +03:00
data[0] = 'p'
// Unmap.
2015-03-23 02:52:37 +03:00
err = syscall.Munmap(data)
AssertEq(nil, err)
2015-03-23 03:40:03 +03:00
// munmap does not cause a flush.
ExpectThat(t.getFlushes(), ElementsAre("taco"))
2015-03-23 02:52:37 +03:00
ExpectThat(t.getFsyncs(), ElementsAre())
2015-03-20 05:21:14 +03:00
}
2015-03-20 05:24:02 +03:00
2015-03-24 04:51:19 +03:00
func (t *NoErrorsTest) Mmap_WithMsync_MunmapBeforeClose() {
var n int
var err error
var expectedFsyncs []interface{}
// Open the file.
t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_RDWR, 0)
AssertEq(nil, err)
// Write some contents to the file.
n, err = t.f1.Write([]byte("taco"))
AssertEq(nil, err)
AssertEq(4, n)
// mmap the file.
data, err := syscall.Mmap(
int(t.f1.Fd()), 0, 4,
syscall.PROT_READ|syscall.PROT_WRITE,
syscall.MAP_SHARED)
AssertEq(nil, err)
defer syscall.Munmap(data)
AssertEq("taco", string(data))
// Modify the contents.
data[0] = 'p'
// msync. This causes an fsync, except on OS X (cf.
// https://github.com/osxfuse/osxfuse/issues/202).
2015-03-24 04:58:59 +03:00
err = msync(data)
ExpectEq(nil, err)
if !isDarwin {
expectedFsyncs = append(expectedFsyncs, "paco")
}
ExpectThat(t.getFlushes(), ElementsAre())
ExpectThat(t.getFsyncs(), ElementsAre(expectedFsyncs...))
// Unmap. This does not cause anything.
err = syscall.Munmap(data)
AssertEq(nil, err)
ExpectThat(t.getFlushes(), ElementsAre())
ExpectThat(t.getFsyncs(), ElementsAre(expectedFsyncs...))
// Close the file. We should now see a flush with the modified contents, even
// on OS X.
err = t.f1.Close()
t.f1 = nil
AssertEq(nil, err)
ExpectThat(t.getFlushes(), ElementsAre("paco"))
ExpectThat(t.getFsyncs(), ElementsAre(expectedFsyncs...))
2015-03-24 04:51:19 +03:00
}
func (t *NoErrorsTest) Mmap_WithMsync_CloseBeforeMunmap() {
var n int
var err error
var expectedFsyncs []interface{}
// Open the file.
t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_RDWR, 0)
AssertEq(nil, err)
// Write some contents to the file.
n, err = t.f1.Write([]byte("taco"))
AssertEq(nil, err)
AssertEq(4, n)
// mmap the file.
data, err := syscall.Mmap(
int(t.f1.Fd()), 0, 4,
syscall.PROT_READ|syscall.PROT_WRITE,
syscall.MAP_SHARED)
AssertEq(nil, err)
defer syscall.Munmap(data)
AssertEq("taco", string(data))
// Close the file. We should see a flush.
err = t.f1.Close()
t.f1 = nil
AssertEq(nil, err)
AssertThat(t.getFlushes(), ElementsAre("taco"))
AssertThat(t.getFsyncs(), ElementsAre())
// Modify the contents.
data[0] = 'p'
// msync. This causes an fsync, except on OS X (cf.
// https://github.com/osxfuse/osxfuse/issues/202).
err = msync(data)
ExpectEq(nil, err)
if !isDarwin {
expectedFsyncs = append(expectedFsyncs, "paco")
}
ExpectThat(t.getFlushes(), ElementsAre("taco"))
ExpectThat(t.getFsyncs(), ElementsAre(expectedFsyncs...))
// Unmap. Again, this does not cause a flush.
err = syscall.Munmap(data)
AssertEq(nil, err)
ExpectThat(t.getFlushes(), ElementsAre("taco"))
ExpectThat(t.getFsyncs(), ElementsAre(expectedFsyncs...))
2015-03-24 04:51:19 +03:00
}
2015-03-24 00:40:35 +03:00
func (t *NoErrorsTest) Directory() {
2015-03-24 04:02:48 +03:00
var err error
// Open the directory.
t.f1, err = os.Open(path.Join(t.Dir, "bar"))
AssertEq(nil, err)
// Sanity check: stat it.
fi, err := t.f1.Stat()
AssertEq(nil, err)
AssertEq(0777|os.ModeDir, fi.Mode())
// Sync it.
err = t.f1.Sync()
AssertEq(nil, err)
// Close it.
err = t.f1.Close()
t.f1 = nil
AssertEq(nil, err)
// No flushes or fsync requests should have been received.
ExpectThat(t.getFlushes(), ElementsAre())
2021-08-02 10:27:30 +03:00
// TODO(stapelberg): this test fails on my machine (Linux 5.13.5, with Go
// 1.16.6), not yet sure why:
// ExpectThat(t.getFsyncs(), ElementsAre())
2015-03-20 05:24:02 +03:00
}
2015-03-23 08:11:01 +03:00
////////////////////////////////////////////////////////////////////////
2015-03-24 00:39:22 +03:00
// Flush error
2015-03-23 08:11:01 +03:00
////////////////////////////////////////////////////////////////////////
2015-03-24 00:39:22 +03:00
type FlushErrorTest struct {
2015-03-23 08:11:01 +03:00
flushFSTest
}
2015-03-24 00:39:22 +03:00
func init() { RegisterTestSuite(&FlushErrorTest{}) }
2015-03-23 08:11:01 +03:00
2015-03-24 00:52:56 +03:00
func (t *FlushErrorTest) SetUp(ti *TestInfo) {
const noErr = 0
2015-07-24 07:02:55 +03:00
t.flushFSTest.setUp(ti, syscall.ENOENT, noErr, false)
2015-03-24 00:52:56 +03:00
}
2015-03-23 08:35:42 +03:00
2015-03-24 00:40:35 +03:00
func (t *FlushErrorTest) Close() {
2015-03-24 00:39:22 +03:00
var err error
2015-03-23 08:11:01 +03:00
2015-03-24 00:39:22 +03:00
// Open the file.
t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_RDWR, 0)
AssertEq(nil, err)
// Close the file.
err = t.f1.Close()
t.f1 = nil
ExpectThat(err, Error(HasSubstr("no such file")))
2015-03-23 08:11:01 +03:00
}
2015-03-24 00:40:35 +03:00
func (t *FlushErrorTest) Dup() {
2015-03-24 00:39:22 +03:00
var err error
2015-03-23 08:11:01 +03:00
2015-03-24 00:39:22 +03:00
// Open the file.
t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_WRONLY, 0)
AssertEq(nil, err)
fd1 := t.f1.Fd()
// Use dup(2) to get another copy.
fd2, err := syscall.Dup(int(fd1))
AssertEq(nil, err)
t.f2 = os.NewFile(uintptr(fd2), t.f1.Name())
// Close by the first handle. On OS X, where the semantics of file handles
// are different (cf. https://github.com/osxfuse/osxfuse/issues/199), this
// does not result in an error.
err = t.f1.Close()
t.f1 = nil
if isDarwin {
2015-03-24 00:39:22 +03:00
AssertEq(nil, err)
} else {
ExpectThat(err, Error(HasSubstr("no such file")))
}
// Close by the second handle.
err = t.f2.Close()
t.f2 = nil
ExpectThat(err, Error(HasSubstr("no such file")))
}
2015-03-24 00:40:35 +03:00
func (t *FlushErrorTest) Dup2() {
2015-03-24 00:39:22 +03:00
var err error
// Open the file.
t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_WRONLY, 0)
AssertEq(nil, err)
// Create some anonymous temporary file.
t.f2, err = fsutil.AnonymousFile("")
2015-03-24 00:39:22 +03:00
AssertEq(nil, err)
// Duplicate the temporary file descriptor on top of the file from our file
// system. We shouldn't see the flush error.
err = dup2(int(t.f2.Fd()), int(t.f1.Fd()))
ExpectEq(nil, err)
}
2015-03-23 08:11:01 +03:00
////////////////////////////////////////////////////////////////////////
2015-05-18 02:52:10 +03:00
// Fsync error
2015-03-23 08:11:01 +03:00
////////////////////////////////////////////////////////////////////////
type FsyncErrorTest struct {
flushFSTest
}
func init() { RegisterTestSuite(&FsyncErrorTest{}) }
2015-03-24 00:52:56 +03:00
func (t *FsyncErrorTest) SetUp(ti *TestInfo) {
const noErr = 0
2015-07-24 07:02:55 +03:00
t.flushFSTest.setUp(ti, noErr, syscall.ENOENT, false)
2015-03-24 00:52:56 +03:00
}
2015-03-24 00:39:22 +03:00
2015-03-24 00:40:35 +03:00
func (t *FsyncErrorTest) Fsync() {
2015-03-24 00:39:22 +03:00
var err error
// Open the file.
t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_RDWR, 0)
AssertEq(nil, err)
// Fsync.
err = t.f1.Sync()
ExpectThat(err, Error(HasSubstr("no such file")))
}
2015-03-24 04:04:16 +03:00
func (t *FsyncErrorTest) Fdatasync() {
2015-03-24 04:24:00 +03:00
var err error
if !fsutil.FdatasyncSupported {
return
}
2015-03-24 04:24:00 +03:00
// Open the file.
t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_RDWR, 0)
AssertEq(nil, err)
// Fdatasync.
err = fsutil.Fdatasync(t.f1)
2015-03-24 04:24:00 +03:00
ExpectThat(err, Error(HasSubstr("no such file")))
2015-03-24 04:04:16 +03:00
}
2015-03-24 05:49:44 +03:00
func (t *FsyncErrorTest) Msync() {
var err error
// On OS X, msync does not cause SyncFile.
if isDarwin {
return
}
// Open the file.
t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_RDWR, 0)
AssertEq(nil, err)
// mmap the file.
data, err := syscall.Mmap(
int(t.f1.Fd()), 0, 4,
syscall.PROT_READ|syscall.PROT_WRITE,
syscall.MAP_SHARED)
AssertEq(nil, err)
defer syscall.Munmap(data)
2015-03-24 05:49:44 +03:00
// msync the mapping.
err = msync(data)
ExpectThat(err, Error(HasSubstr("no such file")))
// Unmap.
err = syscall.Munmap(data)
AssertEq(nil, err)
}
2015-05-18 02:52:10 +03:00
////////////////////////////////////////////////////////////////////////
// Read-only mount
////////////////////////////////////////////////////////////////////////
type ReadOnlyTest struct {
flushFSTest
}
func init() { RegisterTestSuite(&ReadOnlyTest{}) }
func (t *ReadOnlyTest) SetUp(ti *TestInfo) {
2015-05-18 02:54:34 +03:00
const noErr = 0
t.flushFSTest.setUp(ti, noErr, noErr, true)
2015-05-18 02:52:10 +03:00
}
func (t *ReadOnlyTest) ReadRoot() {
2015-05-18 03:07:41 +03:00
var fi os.FileInfo
// Read.
2015-05-18 02:57:05 +03:00
entries, err := fusetesting.ReadDirPicky(t.Dir)
AssertEq(nil, err)
AssertEq(2, len(entries))
2015-05-18 03:07:41 +03:00
// bar
fi = entries[0]
ExpectEq("bar", fi.Name())
2015-05-18 03:15:16 +03:00
ExpectEq(os.FileMode(0777)|os.ModeDir, fi.Mode())
2015-05-18 03:07:41 +03:00
// foo
fi = entries[1]
ExpectEq("foo", fi.Name())
2015-05-18 03:15:16 +03:00
ExpectEq(os.FileMode(0777), fi.Mode())
2015-05-18 02:57:05 +03:00
}
func (t *ReadOnlyTest) StatFiles() {
2015-05-18 03:15:51 +03:00
var fi os.FileInfo
var err error
// bar
fi, err = os.Stat(path.Join(t.Dir, "bar"))
AssertEq(nil, err)
ExpectEq("bar", fi.Name())
ExpectEq(os.FileMode(0777)|os.ModeDir, fi.Mode())
// foo
fi, err = os.Stat(path.Join(t.Dir, "foo"))
AssertEq(nil, err)
ExpectEq("foo", fi.Name())
ExpectEq(os.FileMode(0777), fi.Mode())
2015-05-18 02:57:05 +03:00
}
func (t *ReadOnlyTest) ReadFile() {
2015-05-18 03:16:49 +03:00
_, err := ioutil.ReadFile(path.Join(t.Dir, "foo"))
ExpectEq(nil, err)
2015-05-18 02:52:10 +03:00
}
2015-05-18 02:57:05 +03:00
func (t *ReadOnlyTest) ReadDir() {
2015-05-18 03:16:49 +03:00
_, err := fusetesting.ReadDirPicky(path.Join(t.Dir, "bar"))
ExpectEq(nil, err)
2015-05-18 02:52:10 +03:00
}
func (t *ReadOnlyTest) CreateFile() {
2015-05-18 03:11:03 +03:00
err := ioutil.WriteFile(path.Join(t.Dir, "blah"), []byte{}, 0400)
ExpectThat(err, Error(HasSubstr("read-only")))
2015-05-18 02:52:10 +03:00
}
func (t *ReadOnlyTest) Mkdir() {
2015-05-18 03:11:21 +03:00
err := os.Mkdir(path.Join(t.Dir, "blah"), 0700)
ExpectThat(err, Error(HasSubstr("read-only")))
2015-05-18 02:52:10 +03:00
}
func (t *ReadOnlyTest) OpenForWrite() {
2015-05-18 03:13:28 +03:00
modes := []int{
os.O_WRONLY,
os.O_RDWR,
}
for _, mode := range modes {
f, err := os.OpenFile(path.Join(t.Dir, "foo"), mode, 0700)
f.Close()
ExpectThat(err, Error(HasSubstr("read-only")), "mode: %v", mode)
}
2015-05-18 02:52:10 +03:00
}
func (t *ReadOnlyTest) Chtimes() {
2015-05-18 03:14:19 +03:00
err := os.Chtimes(path.Join(t.Dir, "foo"), time.Now(), time.Now())
ExpectThat(err, Error(MatchesRegexp("read-only|not permitted|permission denied")))
2015-05-18 02:52:10 +03:00
}
func (t *ReadOnlyTest) Chmod() {
2015-05-18 03:14:57 +03:00
err := os.Chmod(path.Join(t.Dir, "foo"), 0700)
ExpectThat(err, Error(HasSubstr("read-only")))
2015-05-18 02:52:10 +03:00
}