Commit 5f641462 authored by Han-Wen Nienhuys's avatar Han-Wen Nienhuys

Add helper methods for Mkdir and WriteFile in loopback_test.

parent 2001c7c2
...@@ -37,6 +37,25 @@ type testCase struct { ...@@ -37,6 +37,25 @@ type testCase struct {
const testTtl = 100 * time.Millisecond const testTtl = 100 * time.Millisecond
// Mkdir is a utility wrapper for os.Mkdir, aborting the test if it fails.
func (tc *testCase) Mkdir(name string, mode os.FileMode) {
if err := os.Mkdir(name, mode); err != nil {
tc.tester.Fatalf("Mkdir(%q,%v): %v", name, mode, err)
}
}
// WriteFile is a utility wrapper for ioutil.WriteFile, aborting the
// test if it fails.
func (tc *testCase) WriteFile(name string, content []byte, mode os.FileMode) {
if err := ioutil.WriteFile(name, content, mode); err != nil {
if len(content) > 50 {
content = append(content[:50], '.', '.', '.')
}
tc.tester.Fatalf("WriteFile(%q, %q, %o): %v", name, content, mode, err)
}
}
// Create and mount filesystem. // Create and mount filesystem.
func NewTestCase(t *testing.T) *testCase { func NewTestCase(t *testing.T) *testCase {
tc := &testCase{} tc := &testCase{}
...@@ -56,8 +75,8 @@ func NewTestCase(t *testing.T) *testCase { ...@@ -56,8 +75,8 @@ func NewTestCase(t *testing.T) *testCase {
tc.orig = tc.tmpDir + "/orig" tc.orig = tc.tmpDir + "/orig"
tc.mnt = tc.tmpDir + "/mnt" tc.mnt = tc.tmpDir + "/mnt"
os.Mkdir(tc.orig, 0700) tc.Mkdir(tc.orig, 0700)
os.Mkdir(tc.mnt, 0700) tc.Mkdir(tc.mnt, 0700)
tc.mountFile = filepath.Join(tc.mnt, name) tc.mountFile = filepath.Join(tc.mnt, name)
tc.mountSubdir = filepath.Join(tc.mnt, subdir) tc.mountSubdir = filepath.Join(tc.mnt, subdir)
...@@ -122,12 +141,8 @@ func TestReadThrough(t *testing.T) { ...@@ -122,12 +141,8 @@ func TestReadThrough(t *testing.T) {
defer tc.Cleanup() defer tc.Cleanup()
content := RandomData(125) content := RandomData(125)
err := ioutil.WriteFile(tc.origFile, content, 0700) tc.WriteFile(tc.origFile, content, 0700)
if err != nil { err := os.Chmod(tc.mountFile, os.FileMode(mode))
t.Fatalf("WriteFile failed: %v", err)
}
err = os.Chmod(tc.mountFile, os.FileMode(mode))
if err != nil { if err != nil {
t.Fatalf("Chmod failed: %v", err) t.Fatalf("Chmod failed: %v", err)
} }
...@@ -158,12 +173,9 @@ func TestRemove(t *testing.T) { ...@@ -158,12 +173,9 @@ func TestRemove(t *testing.T) {
defer tc.Cleanup() defer tc.Cleanup()
contents := []byte{1, 2, 3} contents := []byte{1, 2, 3}
err := ioutil.WriteFile(tc.origFile, []byte(contents), 0700) tc.WriteFile(tc.origFile, []byte(contents), 0700)
if err != nil {
t.Fatalf("WriteFile failed: %v", err)
}
err = os.Remove(tc.mountFile) err := os.Remove(tc.mountFile)
if err != nil { if err != nil {
t.Fatalf("Remove failed: %v", err) t.Fatalf("Remove failed: %v", err)
} }
...@@ -241,18 +253,13 @@ func TestLinkCreate(t *testing.T) { ...@@ -241,18 +253,13 @@ func TestLinkCreate(t *testing.T) {
defer tc.Cleanup() defer tc.Cleanup()
content := RandomData(125) content := RandomData(125)
err := ioutil.WriteFile(tc.origFile, content, 0700) tc.WriteFile(tc.origFile, content, 0700)
if err != nil {
t.Fatalf("WriteFile failed: %v", err) tc.Mkdir(tc.origSubdir, 0777)
}
err = os.Mkdir(tc.origSubdir, 0777)
if err != nil {
t.Fatalf("Mkdir failed: %v", err)
}
// Link. // Link.
mountSubfile := filepath.Join(tc.mountSubdir, "subfile") mountSubfile := filepath.Join(tc.mountSubdir, "subfile")
err = os.Link(tc.mountFile, mountSubfile) err := os.Link(tc.mountFile, mountSubfile)
if err != nil { if err != nil {
t.Fatalf("Link failed: %v", err) t.Fatalf("Link failed: %v", err)
} }
...@@ -298,11 +305,9 @@ func TestLinkExisting(t *testing.T) { ...@@ -298,11 +305,9 @@ func TestLinkExisting(t *testing.T) {
c := RandomData(5) c := RandomData(5)
err := ioutil.WriteFile(tc.orig+"/file1", c, 0644) tc.WriteFile(tc.orig+"/file1", c, 0644)
if err != nil {
t.Fatalf("WriteFile failed: %v", err) err := os.Link(tc.orig+"/file1", tc.orig+"/file2")
}
err = os.Link(tc.orig+"/file1", tc.orig+"/file2")
if err != nil { if err != nil {
t.Fatalf("Link failed: %v", err) t.Fatalf("Link failed: %v", err)
} }
...@@ -336,11 +341,8 @@ func TestLinkForget(t *testing.T) { ...@@ -336,11 +341,8 @@ func TestLinkForget(t *testing.T) {
c := "hello" c := "hello"
err := ioutil.WriteFile(tc.orig+"/file1", []byte(c), 0644) tc.WriteFile(tc.orig+"/file1", []byte(c), 0644)
if err != nil { err := os.Link(tc.orig+"/file1", tc.orig+"/file2")
t.Fatalf("WriteFile failed: %v", err)
}
err = os.Link(tc.orig+"/file1", tc.orig+"/file2")
if err != nil { if err != nil {
t.Fatalf("Link failed: %v", err) t.Fatalf("Link failed: %v", err)
} }
...@@ -364,9 +366,7 @@ func TestLinkForget(t *testing.T) { ...@@ -364,9 +366,7 @@ func TestLinkForget(t *testing.T) {
if err := os.Remove(fn); err != nil { if err := os.Remove(fn); err != nil {
t.Fatalf("Remove", err) t.Fatalf("Remove", err)
} }
if err := ioutil.WriteFile(fn, []byte(c), 0644); err != nil { tc.WriteFile(fn, []byte(c), 0644)
t.Fatalf("WriteFile", err)
}
} }
for i, fn := range []string{"file1", "file2"} { for i, fn := range []string{"file1", "file2"} {
fn = tc.mnt + "/" + fn fn = tc.mnt + "/" + fn
...@@ -393,14 +393,11 @@ func TestSymlink(t *testing.T) { ...@@ -393,14 +393,11 @@ func TestSymlink(t *testing.T) {
t.Log("testing symlink/readlink.") t.Log("testing symlink/readlink.")
contents := []byte{1, 2, 3} contents := []byte{1, 2, 3}
err := ioutil.WriteFile(tc.origFile, []byte(contents), 0700) tc.WriteFile(tc.origFile, []byte(contents), 0700)
if err != nil {
t.Fatalf("WriteFile failed: %v", err)
}
linkFile := "symlink-file" linkFile := "symlink-file"
orig := "hello.txt" orig := "hello.txt"
err = os.Symlink(orig, filepath.Join(tc.mnt, linkFile)) err := os.Symlink(orig, filepath.Join(tc.mnt, linkFile))
if err != nil { if err != nil {
t.Fatalf("Symlink failed: %v", err) t.Fatalf("Symlink failed: %v", err)
...@@ -432,13 +429,9 @@ func TestRename(t *testing.T) { ...@@ -432,13 +429,9 @@ func TestRename(t *testing.T) {
defer tc.Cleanup() defer tc.Cleanup()
contents := []byte{1, 2, 3} contents := []byte{1, 2, 3}
if err := ioutil.WriteFile(tc.origFile, []byte(contents), 0700); err != nil { tc.WriteFile(tc.origFile, []byte(contents), 0700)
t.Fatalf("WriteFile failed: %v", err)
}
sd := tc.mnt + "/testRename" sd := tc.mnt + "/testRename"
if err := os.MkdirAll(sd, 0777); err != nil { tc.Mkdir(sd, 0777)
t.Fatalf("MkdirAll(%q): %v", sd, err)
}
subFile := sd + "/subfile" subFile := sd + "/subfile"
if err := os.Rename(tc.mountFile, subFile); err != nil { if err := os.Rename(tc.mountFile, subFile); err != nil {
...@@ -461,14 +454,10 @@ func TestDelRename(t *testing.T) { ...@@ -461,14 +454,10 @@ func TestDelRename(t *testing.T) {
t.Log("Testing del+rename.") t.Log("Testing del+rename.")
sd := tc.mnt + "/testDelRename" sd := tc.mnt + "/testDelRename"
if err := os.MkdirAll(sd, 0755); err != nil { tc.Mkdir(sd, 0755)
t.Fatalf("MkdirAll failed: %v", err)
}
d := sd + "/dest" d := sd + "/dest"
if err := ioutil.WriteFile(d, []byte("blabla"), 0644); err != nil { tc.WriteFile(d, []byte("blabla"), 0644)
t.Fatalf("WriteFile failed: %v", err)
}
f, err := os.Open(d) f, err := os.Open(d)
if err != nil { if err != nil {
...@@ -481,10 +470,7 @@ func TestDelRename(t *testing.T) { ...@@ -481,10 +470,7 @@ func TestDelRename(t *testing.T) {
} }
s := sd + "/src" s := sd + "/src"
if err = ioutil.WriteFile(s, []byte("blabla"), 0644); err != nil { tc.WriteFile(s, []byte("blabla"), 0644)
t.Fatalf("WriteFile failed: %v", err)
}
if err := os.Rename(s, d); err != nil { if err := os.Rename(s, d); err != nil {
t.Fatalf("Rename failed: %v", err) t.Fatalf("Rename failed: %v", err)
} }
...@@ -497,19 +483,13 @@ func TestOverwriteRename(t *testing.T) { ...@@ -497,19 +483,13 @@ func TestOverwriteRename(t *testing.T) {
t.Log("Testing rename overwrite.") t.Log("Testing rename overwrite.")
sd := tc.mnt + "/testOverwriteRename" sd := tc.mnt + "/testOverwriteRename"
if err := os.MkdirAll(sd, 0755); err != nil { tc.Mkdir(sd, 0755)
t.Fatalf("MkdirAll failed: %v", err)
}
d := sd + "/dest" d := sd + "/dest"
if err := ioutil.WriteFile(d, []byte("blabla"), 0644); err != nil { tc.WriteFile(d, []byte("blabla"), 0644)
t.Fatalf("WriteFile failed: %v", err)
}
s := sd + "/src" s := sd + "/src"
if err := ioutil.WriteFile(s, []byte("blabla"), 0644); err != nil { tc.WriteFile(s, []byte("blabla"), 0644)
t.Fatalf("WriteFile failed: %v", err)
}
if err := os.Rename(s, d); err != nil { if err := os.Rename(s, d); err != nil {
t.Fatalf("Rename failed: %v", err) t.Fatalf("Rename failed: %v", err)
...@@ -525,9 +505,7 @@ func TestAccess(t *testing.T) { ...@@ -525,9 +505,7 @@ func TestAccess(t *testing.T) {
defer tc.Cleanup() defer tc.Cleanup()
contents := []byte{1, 2, 3} contents := []byte{1, 2, 3}
if err := ioutil.WriteFile(tc.origFile, []byte(contents), 0700); err != nil { tc.WriteFile(tc.origFile, []byte(contents), 0700)
t.Fatalf("WriteFile failed: %v", err)
}
if err := os.Chmod(tc.origFile, 0); err != nil { if err := os.Chmod(tc.origFile, 0); err != nil {
t.Fatalf("Chmod failed: %v", err) t.Fatalf("Chmod failed: %v", err)
} }
...@@ -568,12 +546,8 @@ func TestReaddir(t *testing.T) { ...@@ -568,12 +546,8 @@ func TestReaddir(t *testing.T) {
defer tc.Cleanup() defer tc.Cleanup()
contents := []byte{1, 2, 3} contents := []byte{1, 2, 3}
if err := ioutil.WriteFile(tc.origFile, []byte(contents), 0700); err != nil { tc.WriteFile(tc.origFile, []byte(contents), 0700)
t.Fatalf(" failed: %v", err) tc.Mkdir(tc.origSubdir, 0777)
}
if err := os.Mkdir(tc.origSubdir, 0777); err != nil {
t.Fatalf("Mkdir failed: %v", err)
}
dir, err := os.Open(tc.mnt) dir, err := os.Open(tc.mnt)
if err != nil { if err != nil {
...@@ -607,9 +581,7 @@ func TestFSync(t *testing.T) { ...@@ -607,9 +581,7 @@ func TestFSync(t *testing.T) {
defer tc.Cleanup() defer tc.Cleanup()
contents := []byte{1, 2, 3} contents := []byte{1, 2, 3}
if err := ioutil.WriteFile(tc.origFile, []byte(contents), 0700); err != nil { tc.WriteFile(tc.origFile, []byte(contents), 0700)
t.Fatalf("WriteFile failed: %v", err)
}
f, err := os.OpenFile(tc.mountFile, os.O_WRONLY, 0) f, err := os.OpenFile(tc.mountFile, os.O_WRONLY, 0)
if err != nil { if err != nil {
...@@ -631,9 +603,7 @@ func TestFSync(t *testing.T) { ...@@ -631,9 +603,7 @@ func TestFSync(t *testing.T) {
func TestReadZero(t *testing.T) { func TestReadZero(t *testing.T) {
tc := NewTestCase(t) tc := NewTestCase(t)
defer tc.Cleanup() defer tc.Cleanup()
if err := ioutil.WriteFile(tc.origFile, []byte{}, 0644); err != nil { tc.WriteFile(tc.origFile, []byte{}, 0644)
t.Fatalf("WriteFile failed: %v", err)
}
back, err := ioutil.ReadFile(tc.mountFile) back, err := ioutil.ReadFile(tc.mountFile)
if err != nil { if err != nil {
...@@ -685,9 +655,7 @@ func TestReadLargeMemCheck(t *testing.T) { ...@@ -685,9 +655,7 @@ func TestReadLargeMemCheck(t *testing.T) {
defer tc.Cleanup() defer tc.Cleanup()
content := RandomData(385 * 1023) content := RandomData(385 * 1023)
if err := ioutil.WriteFile(tc.origFile, []byte(content), 0644); err != nil { tc.WriteFile(tc.origFile, []byte(content), 0644)
t.Fatalf("WriteFile failed: %v", err)
}
f, err := os.Open(tc.mountFile) f, err := os.Open(tc.mountFile)
if err != nil { if err != nil {
...@@ -722,9 +690,7 @@ func TestReadLarge(t *testing.T) { ...@@ -722,9 +690,7 @@ func TestReadLarge(t *testing.T) {
defer tc.Cleanup() defer tc.Cleanup()
content := RandomData(385 * 1023) content := RandomData(385 * 1023)
if err := ioutil.WriteFile(tc.origFile, []byte(content), 0644); err != nil { tc.WriteFile(tc.origFile, []byte(content), 0644)
t.Fatalf("WriteFile failed: %v", err)
}
back, err := ioutil.ReadFile(tc.mountFile) back, err := ioutil.ReadFile(tc.mountFile)
if err != nil { if err != nil {
...@@ -755,9 +721,8 @@ func TestLargeDirRead(t *testing.T) { ...@@ -755,9 +721,8 @@ func TestLargeDirRead(t *testing.T) {
names := make([]string, created) names := make([]string, created)
subdir := filepath.Join(tc.orig, "readdirSubdir") subdir := filepath.Join(tc.orig, "readdirSubdir")
if err := os.Mkdir(subdir, 0700); err != nil {
t.Fatalf("Mkdir(%q): %v", subdir, err) tc.Mkdir(subdir, 0700)
}
longname := "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" longname := "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
...@@ -770,9 +735,7 @@ func TestLargeDirRead(t *testing.T) { ...@@ -770,9 +735,7 @@ func TestLargeDirRead(t *testing.T) {
nameSet[base] = true nameSet[base] = true
if err := ioutil.WriteFile(name, []byte("bla"), 0777); err != nil { tc.WriteFile(name, []byte("bla"), 0777)
t.Fatalf("WriteFile(%q): %v", name, err)
}
names[i] = name names[i] = name
} }
...@@ -946,9 +909,7 @@ func TestDoubleOpen(t *testing.T) { ...@@ -946,9 +909,7 @@ func TestDoubleOpen(t *testing.T) {
tc := NewTestCase(t) tc := NewTestCase(t)
defer tc.Cleanup() defer tc.Cleanup()
if err := ioutil.WriteFile(tc.orig+"/file", []byte("blabla"), 0644); err != nil { tc.WriteFile(tc.orig+"/file", []byte("blabla"), 0644)
t.Fatalf("WriteFile failed: %v", err)
}
roFile, err := os.Open(tc.mnt + "/file") roFile, err := os.Open(tc.mnt + "/file")
if err != nil { if err != nil {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment