test(vfs/vfstest): conformance suite and FaultFS; run it on localfs (2A.3)
vfstest.Run checks any writable vfs.FS through the API only (so it also runs over SFTP): Lstat/Stat/ReadDir with symlinks, Create Excl/Trunc, ReadAt, Mkdir, Rename in both modes, Remove (file, link, empty and non-empty dir), RemoveTree on a link to a directory, Chmod/Chtimes, RealPath, Sync and the error mapping. FaultFS hooks every FS and File method; FailNth and Gate cover ENOSPC on the n-th write and blocking ReadDir. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
5132daa292
commit
9d5b6bf0c4
4 changed files with 840 additions and 0 deletions
13
internal/vfs/localfs/conformance_test.go
Normal file
13
internal/vfs/localfs/conformance_test.go
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
package localfs_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"vcom/internal/vfs"
|
||||
"vcom/internal/vfs/localfs"
|
||||
"vcom/internal/vfs/vfstest"
|
||||
)
|
||||
|
||||
func TestConformance(t *testing.T) {
|
||||
vfstest.Run(t, func(t *testing.T) (vfs.FS, string) { return localfs.New(), t.TempDir() })
|
||||
}
|
||||
468
internal/vfs/vfstest/conformance.go
Normal file
468
internal/vfs/vfstest/conformance.go
Normal file
|
|
@ -0,0 +1,468 @@
|
|||
// Package vfstest holds the conformance suite every vfs.FS must pass and
|
||||
// FaultFS, a wrapper that injects errors, delays and blocking (design §11.1,
|
||||
// §11.3).
|
||||
//
|
||||
// func TestConformance(t *testing.T) {
|
||||
// vfstest.Run(t, func(t *testing.T) (vfs.FS, string) { return localfs.New(), t.TempDir() })
|
||||
// }
|
||||
package vfstest
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"io/fs"
|
||||
"sort"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"vcom/internal/vfs"
|
||||
)
|
||||
|
||||
// Factory returns the FS under test and an existing, empty, writable
|
||||
// directory on it. It is called once per subtest.
|
||||
type Factory func(t *testing.T) (fsys vfs.FS, root string)
|
||||
|
||||
// Run executes the conformance suite. Everything is set up and checked
|
||||
// through the vfs API only, so the same suite runs over SFTP. Subtests that
|
||||
// need an optional capability (symlinks, chmod, chtimes, atomic replace) are
|
||||
// skipped when Caps says it is missing. Read-only FSes are not supported yet
|
||||
// (they need a pre-built fixture; see archivefs, stage 5A).
|
||||
func Run(t *testing.T, newFS Factory) {
|
||||
t.Helper()
|
||||
tests := []struct {
|
||||
name string
|
||||
fn func(t *testing.T, h *h)
|
||||
}{
|
||||
{"LstatStatSymlinks", testLstatStatSymlinks},
|
||||
{"ReadDirSymlinks", testReadDirSymlinks},
|
||||
{"CreateExcl", testCreateExcl},
|
||||
{"CreateTrunc", testCreateTrunc},
|
||||
{"OpenReadAt", testOpenReadAt},
|
||||
{"Mkdir", testMkdir},
|
||||
{"RenameNoReplace", testRenameNoReplace},
|
||||
{"RenameReplace", testRenameReplace},
|
||||
{"Remove", testRemove},
|
||||
{"RemoveDoesNotFollowLinks", testRemoveDoesNotFollowLinks},
|
||||
{"RemoveTreeSymlinkToDir", testRemoveTreeSymlinkToDir},
|
||||
{"Chmod", testChmod},
|
||||
{"Chtimes", testChtimes},
|
||||
{"RealPath", testRealPath},
|
||||
{"ErrorMapping", testErrorMapping},
|
||||
{"Sync", testSync},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
fsys, root := newFS(t)
|
||||
if fsys.Caps().ReadOnly {
|
||||
t.Skip("read-only FS: conformance needs a writable root")
|
||||
}
|
||||
tc.fn(t, &h{t: t, fs: fsys, root: root, ctx: context.Background()})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// h bundles the FS under test with small assertion helpers.
|
||||
type h struct {
|
||||
t *testing.T
|
||||
fs vfs.FS
|
||||
root string
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
func (h *h) p(elem ...string) string {
|
||||
return h.fs.Paths().Join(append([]string{h.root}, elem...)...)
|
||||
}
|
||||
|
||||
func (h *h) must(err error, what string) {
|
||||
h.t.Helper()
|
||||
if err != nil {
|
||||
h.t.Fatalf("%s: %v", what, err)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *h) write(name, content string) {
|
||||
h.t.Helper()
|
||||
f, err := h.fs.Create(h.ctx, h.p(name), 0o644, vfs.CreateExcl)
|
||||
h.must(err, "create "+name)
|
||||
_, err = io.WriteString(f, content)
|
||||
h.must(err, "write "+name)
|
||||
h.must(f.Close(), "close "+name)
|
||||
}
|
||||
|
||||
func (h *h) read(name string) string {
|
||||
h.t.Helper()
|
||||
f, err := h.fs.Open(h.ctx, h.p(name))
|
||||
h.must(err, "open "+name)
|
||||
defer f.Close()
|
||||
b, err := io.ReadAll(f)
|
||||
h.must(err, "read "+name)
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func (h *h) mkdir(name string) {
|
||||
h.t.Helper()
|
||||
h.must(h.fs.Mkdir(h.ctx, h.p(name), 0o755), "mkdir "+name)
|
||||
}
|
||||
|
||||
func (h *h) symlink(target, name string) {
|
||||
h.t.Helper()
|
||||
h.must(h.fs.Symlink(h.ctx, target, h.p(name)), "symlink "+name)
|
||||
}
|
||||
|
||||
func (h *h) needSymlinks() {
|
||||
h.t.Helper()
|
||||
if !h.fs.Caps().Symlinks {
|
||||
h.t.Skip("FS has no symlinks")
|
||||
}
|
||||
}
|
||||
|
||||
func (h *h) exists(name string) bool {
|
||||
h.t.Helper()
|
||||
_, err := h.fs.Lstat(h.ctx, h.p(name))
|
||||
if err != nil && !errors.Is(err, vfs.ErrNotExist) {
|
||||
h.t.Fatalf("lstat %s: %v", name, err)
|
||||
}
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func (h *h) wantErr(err error, target error, what string) {
|
||||
h.t.Helper()
|
||||
if !errors.Is(err, target) {
|
||||
h.t.Fatalf("%s: err = %v, want %v", what, err, target)
|
||||
}
|
||||
}
|
||||
|
||||
// fixture: file f ("hello"), dir d with d/inner, links lf->f, ld->d, broken->missing.
|
||||
func (h *h) linkFixture() {
|
||||
h.t.Helper()
|
||||
h.needSymlinks()
|
||||
h.write("f", "hello")
|
||||
h.mkdir("d")
|
||||
h.write("d/inner", "x")
|
||||
h.symlink("f", "lf")
|
||||
h.symlink("d", "ld")
|
||||
h.symlink("missing", "broken")
|
||||
}
|
||||
|
||||
func testLstatStatSymlinks(t *testing.T, h *h) {
|
||||
h.linkFixture()
|
||||
li, err := h.fs.Lstat(h.ctx, h.p("ld"))
|
||||
h.must(err, "lstat ld")
|
||||
if !li.IsSymlink() || li.IsDir() || li.Name != "ld" {
|
||||
t.Fatalf("Lstat(ld) = %+v, want a symlink named ld", li)
|
||||
}
|
||||
if li.Target == nil || !li.Target.IsDir() || !li.NavigableDir() {
|
||||
t.Fatalf("Lstat(ld).Target = %+v, want a directory", li.Target)
|
||||
}
|
||||
si, err := h.fs.Stat(h.ctx, h.p("ld"))
|
||||
h.must(err, "stat ld")
|
||||
if !si.IsDir() || si.IsSymlink() {
|
||||
t.Fatalf("Stat(ld) = %+v, want the directory", si)
|
||||
}
|
||||
si, err = h.fs.Stat(h.ctx, h.p("lf"))
|
||||
h.must(err, "stat lf")
|
||||
if !si.Mode.IsRegular() || si.Size != 5 {
|
||||
t.Fatalf("Stat(lf) = %+v, want a 5-byte regular file", si)
|
||||
}
|
||||
bi, err := h.fs.Lstat(h.ctx, h.p("broken"))
|
||||
h.must(err, "lstat broken")
|
||||
if !bi.IsSymlink() || bi.Target != nil || bi.LinkErr == nil {
|
||||
t.Fatalf("Lstat(broken) = %+v, want symlink with Target=nil and LinkErr", bi)
|
||||
}
|
||||
_, err = h.fs.Stat(h.ctx, h.p("broken"))
|
||||
h.wantErr(err, vfs.ErrNotExist, "Stat(broken)")
|
||||
tgt, err := h.fs.ReadLink(h.ctx, h.p("ld"))
|
||||
h.must(err, "readlink")
|
||||
if tgt != "d" {
|
||||
t.Fatalf("ReadLink(ld) = %q, want %q", tgt, "d")
|
||||
}
|
||||
fi, err := h.fs.Lstat(h.ctx, h.p("f"))
|
||||
h.must(err, "lstat f")
|
||||
if fi.IsSymlink() || fi.Target != nil || !fi.Mode.IsRegular() {
|
||||
t.Fatalf("Lstat(f) = %+v", fi)
|
||||
}
|
||||
}
|
||||
|
||||
func testReadDirSymlinks(t *testing.T, h *h) {
|
||||
h.linkFixture()
|
||||
ents, err := h.fs.ReadDir(h.ctx, h.root)
|
||||
h.must(err, "readdir")
|
||||
by := map[string]vfs.FileInfo{}
|
||||
var names []string
|
||||
for _, e := range ents {
|
||||
by[e.Name] = e
|
||||
names = append(names, e.Name)
|
||||
}
|
||||
sort.Strings(names)
|
||||
if strings.Join(names, ",") != "broken,d,f,ld,lf" {
|
||||
t.Fatalf("names = %v", names)
|
||||
}
|
||||
if e := by["d"]; !e.IsDir() || e.IsSymlink() {
|
||||
t.Errorf("d = %+v", e)
|
||||
}
|
||||
if e := by["f"]; !e.Mode.IsRegular() || e.Size != 5 {
|
||||
t.Errorf("f = %+v", e)
|
||||
}
|
||||
if e := by["ld"]; !e.IsSymlink() || e.IsDir() || !e.NavigableDir() {
|
||||
t.Errorf("ld = %+v (Target %+v), want symlink with NavigableDir", e, e.Target)
|
||||
}
|
||||
if e := by["lf"]; !e.IsSymlink() || e.NavigableDir() || !e.IsRegularLike() {
|
||||
t.Errorf("lf = %+v", e)
|
||||
}
|
||||
if e := by["broken"]; !e.IsSymlink() || e.Target != nil || e.LinkErr == nil {
|
||||
t.Errorf("broken = %+v", e)
|
||||
}
|
||||
}
|
||||
|
||||
func testCreateExcl(t *testing.T, h *h) {
|
||||
h.write("a", "first")
|
||||
_, err := h.fs.Create(h.ctx, h.p("a"), 0o644, vfs.CreateExcl)
|
||||
h.wantErr(err, vfs.ErrExist, "CreateExcl on existing file")
|
||||
if got := h.read("a"); got != "first" {
|
||||
t.Fatalf("existing file changed by failed CreateExcl: %q", got)
|
||||
}
|
||||
_, err = h.fs.Create(h.ctx, h.p("nodir", "a"), 0o644, vfs.CreateExcl)
|
||||
h.wantErr(err, vfs.ErrNotExist, "Create in missing directory")
|
||||
if h.fs.Caps().Symlinks {
|
||||
// O_EXCL never follows a (dangling) symlink.
|
||||
h.symlink("elsewhere", "trap")
|
||||
_, err = h.fs.Create(h.ctx, h.p("trap"), 0o644, vfs.CreateExcl)
|
||||
h.wantErr(err, vfs.ErrExist, "CreateExcl on dangling symlink")
|
||||
if h.exists("elsewhere") {
|
||||
t.Fatal("CreateExcl followed a dangling symlink")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testCreateTrunc(t *testing.T, h *h) {
|
||||
h.write("a", "a long first version")
|
||||
f, err := h.fs.Create(h.ctx, h.p("a"), 0o644, vfs.CreateTrunc)
|
||||
h.must(err, "CreateTrunc")
|
||||
_, err = io.WriteString(f, "short")
|
||||
h.must(err, "write")
|
||||
h.must(f.Close(), "close")
|
||||
if got := h.read("a"); got != "short" {
|
||||
t.Fatalf("after CreateTrunc: %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func testOpenReadAt(t *testing.T, h *h) {
|
||||
content := strings.Repeat("0123456789", 1000)
|
||||
h.write("big", content)
|
||||
f, err := h.fs.Open(h.ctx, h.p("big"))
|
||||
h.must(err, "open")
|
||||
defer f.Close()
|
||||
buf := make([]byte, 10)
|
||||
n, err := f.ReadAt(buf, 5005)
|
||||
if n != 10 || (err != nil && err != io.EOF) {
|
||||
t.Fatalf("ReadAt = %d, %v", n, err)
|
||||
}
|
||||
if string(buf) != "5678901234" {
|
||||
t.Fatalf("ReadAt content %q", buf)
|
||||
}
|
||||
fi, err := f.Stat()
|
||||
h.must(err, "file stat")
|
||||
if fi.Size != int64(len(content)) {
|
||||
t.Fatalf("File.Stat size %d", fi.Size)
|
||||
}
|
||||
all, err := io.ReadAll(f)
|
||||
h.must(err, "read")
|
||||
if !bytes.Equal(all, []byte(content)) {
|
||||
t.Fatal("content mismatch")
|
||||
}
|
||||
}
|
||||
|
||||
func testMkdir(t *testing.T, h *h) {
|
||||
h.mkdir("d")
|
||||
fi, err := h.fs.Stat(h.ctx, h.p("d"))
|
||||
h.must(err, "stat")
|
||||
if !fi.IsDir() {
|
||||
t.Fatalf("not a dir: %+v", fi)
|
||||
}
|
||||
h.wantErr(h.fs.Mkdir(h.ctx, h.p("d"), 0o755), vfs.ErrExist, "Mkdir existing dir")
|
||||
h.write("f", "")
|
||||
h.wantErr(h.fs.Mkdir(h.ctx, h.p("f"), 0o755), vfs.ErrExist, "Mkdir over a file")
|
||||
h.wantErr(h.fs.Mkdir(h.ctx, h.p("no", "such"), 0o755), vfs.ErrNotExist, "Mkdir with missing parent")
|
||||
}
|
||||
|
||||
func testRenameNoReplace(t *testing.T, h *h) {
|
||||
h.write("src", "new")
|
||||
h.write("dst", "old")
|
||||
err := h.fs.Rename(h.ctx, h.p("src"), h.p("dst"), vfs.NoReplace)
|
||||
h.wantErr(err, vfs.ErrExist, "Rename(NoReplace) onto existing file")
|
||||
if got := h.read("dst"); got != "old" {
|
||||
t.Fatalf("target changed: %q", got)
|
||||
}
|
||||
if got := h.read("src"); got != "new" {
|
||||
t.Fatalf("source changed: %q", got)
|
||||
}
|
||||
h.must(h.fs.Rename(h.ctx, h.p("src"), h.p("moved"), vfs.NoReplace), "rename to free name")
|
||||
if h.exists("src") || h.read("moved") != "new" {
|
||||
t.Fatal("rename to free name did not move")
|
||||
}
|
||||
h.mkdir("d1")
|
||||
h.write("d1/x", "1")
|
||||
h.mkdir("d2")
|
||||
h.wantErr(h.fs.Rename(h.ctx, h.p("d1"), h.p("d2"), vfs.NoReplace), vfs.ErrExist, "Rename(NoReplace) dir onto empty dir")
|
||||
h.must(h.fs.Rename(h.ctx, h.p("d1"), h.p("d3"), vfs.NoReplace), "rename dir")
|
||||
if h.read("d3/x") != "1" {
|
||||
t.Fatal("dir content lost")
|
||||
}
|
||||
}
|
||||
|
||||
func testRenameReplace(t *testing.T, h *h) {
|
||||
if !h.fs.Caps().AtomicReplace {
|
||||
t.Skip("no atomic replace: the transfer engine renames aside instead")
|
||||
}
|
||||
h.write("src", "new")
|
||||
h.write("dst", "old")
|
||||
h.must(h.fs.Rename(h.ctx, h.p("src"), h.p("dst"), vfs.Replace), "Rename(Replace)")
|
||||
if h.exists("src") {
|
||||
t.Fatal("source still exists")
|
||||
}
|
||||
if got := h.read("dst"); got != "new" {
|
||||
t.Fatalf("dst = %q", got)
|
||||
}
|
||||
h.must(h.fs.Rename(h.ctx, h.p("dst"), h.p("free"), vfs.Replace), "Rename(Replace) to free name")
|
||||
}
|
||||
|
||||
func testRemove(t *testing.T, h *h) {
|
||||
h.write("f", "x")
|
||||
h.must(h.fs.Remove(h.ctx, h.p("f")), "remove file")
|
||||
if h.exists("f") {
|
||||
t.Fatal("file still exists")
|
||||
}
|
||||
h.mkdir("empty")
|
||||
h.must(h.fs.Remove(h.ctx, h.p("empty")), "remove empty dir")
|
||||
if h.exists("empty") {
|
||||
t.Fatal("empty dir still exists")
|
||||
}
|
||||
h.mkdir("full")
|
||||
h.write("full/keep", "k")
|
||||
if err := h.fs.Remove(h.ctx, h.p("full")); err == nil {
|
||||
t.Fatal("Remove of a non-empty directory succeeded")
|
||||
}
|
||||
if h.read("full/keep") != "k" {
|
||||
t.Fatal("non-empty dir content damaged")
|
||||
}
|
||||
h.wantErr(h.fs.Remove(h.ctx, h.p("missing")), vfs.ErrNotExist, "Remove missing")
|
||||
}
|
||||
|
||||
func testRemoveDoesNotFollowLinks(t *testing.T, h *h) {
|
||||
h.linkFixture()
|
||||
h.must(h.fs.Remove(h.ctx, h.p("ld")), "remove link to dir")
|
||||
h.must(h.fs.Remove(h.ctx, h.p("lf")), "remove link to file")
|
||||
h.must(h.fs.Remove(h.ctx, h.p("broken")), "remove broken link")
|
||||
if h.exists("ld") || h.exists("lf") || h.exists("broken") {
|
||||
t.Fatal("links still exist")
|
||||
}
|
||||
if h.read("f") != "hello" || h.read("d/inner") != "x" {
|
||||
t.Fatal("link targets damaged")
|
||||
}
|
||||
}
|
||||
|
||||
func testRemoveTreeSymlinkToDir(t *testing.T, h *h) {
|
||||
h.needSymlinks()
|
||||
h.mkdir("data")
|
||||
h.write("data/precious", "keep me")
|
||||
h.symlink("data", "link")
|
||||
h.mkdir("tree")
|
||||
h.write("tree/f", "x")
|
||||
h.symlink("../data", "tree/inner")
|
||||
h.must(vfs.RemoveTree(h.ctx, h.fs, h.p("link"), nil), "RemoveTree(link)")
|
||||
h.must(vfs.RemoveTree(h.ctx, h.fs, h.p("tree"), nil), "RemoveTree(tree)")
|
||||
if h.exists("link") || h.exists("tree") {
|
||||
t.Fatal("not removed")
|
||||
}
|
||||
if h.read("data/precious") != "keep me" {
|
||||
t.Fatal("RemoveTree followed a symlink into its target (N-P0-3)")
|
||||
}
|
||||
}
|
||||
|
||||
func testChmod(t *testing.T, h *h) {
|
||||
if !h.fs.Caps().Chmod {
|
||||
t.Skip("no chmod")
|
||||
}
|
||||
h.write("f", "")
|
||||
for _, m := range []fs.FileMode{0o600, 0o755, 0o640} {
|
||||
h.must(h.fs.Chmod(h.ctx, h.p("f"), m), "chmod")
|
||||
fi, err := h.fs.Stat(h.ctx, h.p("f"))
|
||||
h.must(err, "stat")
|
||||
if fi.Mode.Perm() != m {
|
||||
t.Fatalf("mode = %v, want %v", fi.Mode.Perm(), m)
|
||||
}
|
||||
}
|
||||
h.wantErr(h.fs.Chmod(h.ctx, h.p("missing"), 0o600), vfs.ErrNotExist, "Chmod missing")
|
||||
}
|
||||
|
||||
func testChtimes(t *testing.T, h *h) {
|
||||
if !h.fs.Caps().Chtimes {
|
||||
t.Skip("no chtimes")
|
||||
}
|
||||
h.write("f", "")
|
||||
mt := time.Date(2001, 2, 3, 4, 5, 6, 0, time.UTC) // whole seconds: SFTP v3 has no sub-second times
|
||||
h.must(h.fs.Chtimes(h.ctx, h.p("f"), mt, mt), "chtimes")
|
||||
fi, err := h.fs.Stat(h.ctx, h.p("f"))
|
||||
h.must(err, "stat")
|
||||
if !fi.ModTime.Equal(mt) {
|
||||
t.Fatalf("mtime = %v, want %v", fi.ModTime, mt)
|
||||
}
|
||||
h.mkdir("d")
|
||||
h.must(h.fs.Chtimes(h.ctx, h.p("d"), mt, mt), "chtimes dir")
|
||||
fi, err = h.fs.Stat(h.ctx, h.p("d"))
|
||||
h.must(err, "stat dir")
|
||||
if !fi.ModTime.Equal(mt) {
|
||||
t.Fatalf("dir mtime = %v", fi.ModTime)
|
||||
}
|
||||
}
|
||||
|
||||
func testRealPath(t *testing.T, h *h) {
|
||||
h.mkdir("d")
|
||||
rd, err := h.fs.RealPath(h.ctx, h.p("d"))
|
||||
h.must(err, "realpath d")
|
||||
if !h.fs.Paths().IsAbs(rd) {
|
||||
t.Fatalf("RealPath not absolute: %q", rd)
|
||||
}
|
||||
if h.fs.Caps().Symlinks {
|
||||
h.symlink("d", "ld")
|
||||
rl, err := h.fs.RealPath(h.ctx, h.p("ld"))
|
||||
h.must(err, "realpath ld")
|
||||
if rl != rd {
|
||||
t.Fatalf("RealPath(ld) = %q, want %q", rl, rd)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testErrorMapping(t *testing.T, h *h) {
|
||||
missing := h.p("missing")
|
||||
_, err := h.fs.Stat(h.ctx, missing)
|
||||
h.wantErr(err, vfs.ErrNotExist, "Stat")
|
||||
_, err = h.fs.Lstat(h.ctx, missing)
|
||||
h.wantErr(err, vfs.ErrNotExist, "Lstat")
|
||||
_, err = h.fs.ReadDir(h.ctx, missing)
|
||||
h.wantErr(err, vfs.ErrNotExist, "ReadDir")
|
||||
_, err = h.fs.Open(h.ctx, missing)
|
||||
h.wantErr(err, vfs.ErrNotExist, "Open")
|
||||
h.wantErr(h.fs.Rename(h.ctx, missing, h.p("x"), vfs.NoReplace), vfs.ErrNotExist, "Rename missing source")
|
||||
h.write("file", "")
|
||||
if _, err := h.fs.ReadDir(h.ctx, h.p("file")); err == nil {
|
||||
t.Fatal("ReadDir on a regular file succeeded")
|
||||
}
|
||||
}
|
||||
|
||||
func testSync(t *testing.T, h *h) {
|
||||
f, err := h.fs.Create(h.ctx, h.p("s"), 0o644, vfs.CreateExcl)
|
||||
h.must(err, "create")
|
||||
defer f.Close()
|
||||
_, err = io.WriteString(f, "data")
|
||||
h.must(err, "write")
|
||||
err = f.Sync()
|
||||
if h.fs.Caps().Fsync {
|
||||
h.must(err, "Sync with Caps.Fsync")
|
||||
} else {
|
||||
h.wantErr(err, vfs.ErrUnsupported, "Sync without Caps.Fsync")
|
||||
}
|
||||
}
|
||||
272
internal/vfs/vfstest/faultfs.go
Normal file
272
internal/vfs/vfstest/faultfs.go
Normal file
|
|
@ -0,0 +1,272 @@
|
|||
package vfstest
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io/fs"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"vcom/internal/vfs"
|
||||
)
|
||||
|
||||
// Op names an operation FaultFS can intercept.
|
||||
type Op string
|
||||
|
||||
const (
|
||||
OpStat Op = "stat"
|
||||
OpLstat Op = "lstat"
|
||||
OpReadDir Op = "readdir"
|
||||
OpReadLink Op = "readlink"
|
||||
OpRealPath Op = "realpath"
|
||||
OpOpen Op = "open"
|
||||
OpCreate Op = "create"
|
||||
OpMkdir Op = "mkdir"
|
||||
OpRemove Op = "remove"
|
||||
OpRename Op = "rename" // p is the source
|
||||
OpSymlink Op = "symlink"
|
||||
OpChmod Op = "chmod"
|
||||
OpChtimes Op = "chtimes"
|
||||
OpTrash Op = "trash"
|
||||
// File methods; p is the path the file was opened or created with.
|
||||
OpRead Op = "read" // Read and ReadAt
|
||||
OpWrite Op = "write"
|
||||
OpClose Op = "close"
|
||||
OpSync Op = "sync"
|
||||
)
|
||||
|
||||
// FaultFS wraps an FS and calls Hook before every operation. A non-nil error
|
||||
// from Hook is returned instead of calling the inner FS (for Close the file is
|
||||
// still closed). Hook may also sleep or block on a channel. A nil Hook passes
|
||||
// everything through. Hook may be called from several goroutines.
|
||||
type FaultFS struct {
|
||||
vfs.FS
|
||||
Hook func(op Op, p string) error
|
||||
}
|
||||
|
||||
var (
|
||||
_ vfs.FS = (*FaultFS)(nil)
|
||||
_ vfs.Trasher = (*FaultFS)(nil)
|
||||
_ vfs.BirthTimer = (*FaultFS)(nil)
|
||||
)
|
||||
|
||||
func (f *FaultFS) hook(op Op, p string) error {
|
||||
if f.Hook == nil {
|
||||
return nil
|
||||
}
|
||||
return f.Hook(op, p)
|
||||
}
|
||||
|
||||
func (f *FaultFS) Stat(ctx context.Context, p string) (vfs.FileInfo, error) {
|
||||
if err := f.hook(OpStat, p); err != nil {
|
||||
return vfs.FileInfo{}, err
|
||||
}
|
||||
return f.FS.Stat(ctx, p)
|
||||
}
|
||||
|
||||
func (f *FaultFS) Lstat(ctx context.Context, p string) (vfs.FileInfo, error) {
|
||||
if err := f.hook(OpLstat, p); err != nil {
|
||||
return vfs.FileInfo{}, err
|
||||
}
|
||||
return f.FS.Lstat(ctx, p)
|
||||
}
|
||||
|
||||
func (f *FaultFS) ReadDir(ctx context.Context, p string) ([]vfs.FileInfo, error) {
|
||||
if err := f.hook(OpReadDir, p); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return f.FS.ReadDir(ctx, p)
|
||||
}
|
||||
|
||||
func (f *FaultFS) ReadLink(ctx context.Context, p string) (string, error) {
|
||||
if err := f.hook(OpReadLink, p); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return f.FS.ReadLink(ctx, p)
|
||||
}
|
||||
|
||||
func (f *FaultFS) RealPath(ctx context.Context, p string) (string, error) {
|
||||
if err := f.hook(OpRealPath, p); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return f.FS.RealPath(ctx, p)
|
||||
}
|
||||
|
||||
func (f *FaultFS) Open(ctx context.Context, p string) (vfs.File, error) {
|
||||
if err := f.hook(OpOpen, p); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
file, err := f.FS.Open(ctx, p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &faultFile{File: file, fs: f, p: p}, nil
|
||||
}
|
||||
|
||||
func (f *FaultFS) Create(ctx context.Context, p string, perm fs.FileMode, mode vfs.CreateMode) (vfs.File, error) {
|
||||
if err := f.hook(OpCreate, p); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
file, err := f.FS.Create(ctx, p, perm, mode)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &faultFile{File: file, fs: f, p: p}, nil
|
||||
}
|
||||
|
||||
func (f *FaultFS) Mkdir(ctx context.Context, p string, perm fs.FileMode) error {
|
||||
if err := f.hook(OpMkdir, p); err != nil {
|
||||
return err
|
||||
}
|
||||
return f.FS.Mkdir(ctx, p, perm)
|
||||
}
|
||||
|
||||
func (f *FaultFS) Remove(ctx context.Context, p string) error {
|
||||
if err := f.hook(OpRemove, p); err != nil {
|
||||
return err
|
||||
}
|
||||
return f.FS.Remove(ctx, p)
|
||||
}
|
||||
|
||||
func (f *FaultFS) Rename(ctx context.Context, from, to string, mode vfs.RenameMode) error {
|
||||
if err := f.hook(OpRename, from); err != nil {
|
||||
return err
|
||||
}
|
||||
return f.FS.Rename(ctx, from, to, mode)
|
||||
}
|
||||
|
||||
func (f *FaultFS) Symlink(ctx context.Context, target, p string) error {
|
||||
if err := f.hook(OpSymlink, p); err != nil {
|
||||
return err
|
||||
}
|
||||
return f.FS.Symlink(ctx, target, p)
|
||||
}
|
||||
|
||||
func (f *FaultFS) Chmod(ctx context.Context, p string, mode fs.FileMode) error {
|
||||
if err := f.hook(OpChmod, p); err != nil {
|
||||
return err
|
||||
}
|
||||
return f.FS.Chmod(ctx, p, mode)
|
||||
}
|
||||
|
||||
func (f *FaultFS) Chtimes(ctx context.Context, p string, atime, mtime time.Time) error {
|
||||
if err := f.hook(OpChtimes, p); err != nil {
|
||||
return err
|
||||
}
|
||||
return f.FS.Chtimes(ctx, p, atime, mtime)
|
||||
}
|
||||
|
||||
// Trash forwards to the inner FS, or returns ErrUnsupported if it has no trash.
|
||||
func (f *FaultFS) Trash(ctx context.Context, p string) error {
|
||||
if err := f.hook(OpTrash, p); err != nil {
|
||||
return err
|
||||
}
|
||||
if t, ok := f.FS.(vfs.Trasher); ok {
|
||||
return t.Trash(ctx, p)
|
||||
}
|
||||
return vfs.ErrUnsupported
|
||||
}
|
||||
|
||||
// BirthTime forwards to the inner FS (not hooked).
|
||||
func (f *FaultFS) BirthTime(ctx context.Context, p string) (time.Time, bool) {
|
||||
if b, ok := f.FS.(vfs.BirthTimer); ok {
|
||||
return b.BirthTime(ctx, p)
|
||||
}
|
||||
return time.Time{}, false
|
||||
}
|
||||
|
||||
type faultFile struct {
|
||||
vfs.File
|
||||
fs *FaultFS
|
||||
p string
|
||||
}
|
||||
|
||||
func (f *faultFile) Read(b []byte) (int, error) {
|
||||
if err := f.fs.hook(OpRead, f.p); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return f.File.Read(b)
|
||||
}
|
||||
|
||||
func (f *faultFile) ReadAt(b []byte, off int64) (int, error) {
|
||||
if err := f.fs.hook(OpRead, f.p); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return f.File.ReadAt(b, off)
|
||||
}
|
||||
|
||||
func (f *faultFile) Write(b []byte) (int, error) {
|
||||
if err := f.fs.hook(OpWrite, f.p); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return f.File.Write(b)
|
||||
}
|
||||
|
||||
func (f *faultFile) Sync() error {
|
||||
if err := f.fs.hook(OpSync, f.p); err != nil {
|
||||
return err
|
||||
}
|
||||
return f.File.Sync()
|
||||
}
|
||||
|
||||
func (f *faultFile) Close() error {
|
||||
herr := f.fs.hook(OpClose, f.p)
|
||||
cerr := f.File.Close()
|
||||
if herr != nil {
|
||||
return herr
|
||||
}
|
||||
return cerr
|
||||
}
|
||||
|
||||
// FailNth returns a Hook that fails the n-th (1-based) call of op with err
|
||||
// and passes everything else, e.g. ENOSPC on the 3rd write.
|
||||
func FailNth(op Op, n int, err error) func(Op, string) error {
|
||||
var count atomic.Int64
|
||||
return func(o Op, _ string) error {
|
||||
if o == op && count.Add(1) == int64(n) {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// Gate blocks the operations it is attached to until Release. Entered
|
||||
// receives a value each time an operation reaches the gate, so a test can
|
||||
// wait until the code under test is really stuck inside the FS.
|
||||
type Gate struct {
|
||||
Entered chan string
|
||||
release chan struct{}
|
||||
once sync.Once
|
||||
}
|
||||
|
||||
// NewGate returns a closed-for-business gate.
|
||||
func NewGate() *Gate {
|
||||
return &Gate{Entered: make(chan string, 64), release: make(chan struct{})}
|
||||
}
|
||||
|
||||
// Hook blocks every op in ops (all ops if none given) until Release.
|
||||
func (g *Gate) Hook(ops ...Op) func(Op, string) error {
|
||||
return func(op Op, p string) error {
|
||||
if len(ops) > 0 && !containsOp(ops, op) {
|
||||
return nil
|
||||
}
|
||||
select {
|
||||
case g.Entered <- p:
|
||||
default:
|
||||
}
|
||||
<-g.release
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// Release unblocks all current and future waiters.
|
||||
func (g *Gate) Release() { g.once.Do(func() { close(g.release) }) }
|
||||
|
||||
func containsOp(ops []Op, op Op) bool {
|
||||
for _, o := range ops {
|
||||
if o == op {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
87
internal/vfs/vfstest/faultfs_test.go
Normal file
87
internal/vfs/vfstest/faultfs_test.go
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
package vfstest_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"vcom/internal/testutil/fstree"
|
||||
"vcom/internal/vfs"
|
||||
"vcom/internal/vfs/localfs"
|
||||
"vcom/internal/vfs/vfstest"
|
||||
)
|
||||
|
||||
func TestFaultFSBlocksAndReleases(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
fstree.Build(t, dir, fstree.Spec{fstree.File("a", ""), fstree.File("b", "")})
|
||||
gate := vfstest.NewGate()
|
||||
fsys := &vfstest.FaultFS{FS: localfs.New(), Hook: gate.Hook(vfstest.OpReadDir)}
|
||||
t.Cleanup(gate.Release)
|
||||
|
||||
// Other operations are not gated.
|
||||
if _, err := fsys.Stat(context.Background(), dir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
type res struct {
|
||||
n int
|
||||
err error
|
||||
}
|
||||
done := make(chan res, 1)
|
||||
go func() {
|
||||
ents, err := fsys.ReadDir(context.Background(), dir)
|
||||
done <- res{len(ents), err}
|
||||
}()
|
||||
select {
|
||||
case p := <-gate.Entered:
|
||||
if p != dir {
|
||||
t.Fatalf("entered with %q", p)
|
||||
}
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("ReadDir never reached the gate")
|
||||
}
|
||||
select {
|
||||
case r := <-done:
|
||||
t.Fatalf("ReadDir returned while gated: %+v", r)
|
||||
case <-time.After(50 * time.Millisecond):
|
||||
}
|
||||
gate.Release()
|
||||
select {
|
||||
case r := <-done:
|
||||
if r.err != nil || r.n != 2 {
|
||||
t.Fatalf("after release: %+v", r)
|
||||
}
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("ReadDir still blocked after Release")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFaultFSFailNthWrite(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
fsys := &vfstest.FaultFS{FS: localfs.New(), Hook: vfstest.FailNth(vfstest.OpWrite, 3, syscall.ENOSPC)}
|
||||
f, err := fsys.Create(context.Background(), filepath.Join(dir, "f"), 0o644, vfs.CreateExcl)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer f.Close()
|
||||
for i := 1; i <= 4; i++ {
|
||||
_, err := io.WriteString(f, "x")
|
||||
if (i == 3) != errors.Is(err, syscall.ENOSPC) {
|
||||
t.Fatalf("write %d: %v", i, err)
|
||||
}
|
||||
}
|
||||
if _, ok := vfs.FS(fsys).(vfs.Trasher); !ok {
|
||||
t.Fatal("FaultFS should forward Trasher")
|
||||
}
|
||||
}
|
||||
|
||||
// The wrapper itself must not break the contract.
|
||||
func TestFaultFSConformance(t *testing.T) {
|
||||
vfstest.Run(t, func(t *testing.T) (vfs.FS, string) {
|
||||
return &vfstest.FaultFS{FS: localfs.New()}, t.TempDir()
|
||||
})
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue