lib9p

Go 9P library.
Log | Files | Refs | LICENSE

commit ed829c7ac2ec03e281e50e204ebcd70134c25780
parent 394806bf11cc201fdb8abcc731966460e485d3dd
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Thu,  4 Jan 2024 15:00:42 +0900

devide testfs's file tree data to testdata.
I want more simple solution

Diffstat:
Mclient/file_test.go | 4++++
Mclient/fs_test.go | 140+++++++++++++++++++++++++++++++------------------------------------------------
Mfs_test.go | 78++++++++++++++++++++----------------------------------------------------------
Atestdata/fs.go | 33+++++++++++++++++++++++++++++++++
4 files changed, 111 insertions(+), 144 deletions(-)

diff --git a/client/file_test.go b/client/file_test.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "path" + "strings" "testing" "git.mtkn.jp/lib9p" @@ -44,6 +45,9 @@ func TestReadDir(t *testing.T) { func testReadDir(t *testing.T, cfs *FS, tfs *testFS, cwd string) { cf, err := cfs.Open(cwd) if err != nil { + if strings.Contains(err.Error(), "permission") { + return + } t.Fatalf("open %s: %v", cwd, err) } defer cf.Close() diff --git a/client/fs_test.go b/client/fs_test.go @@ -4,9 +4,11 @@ import ( "bytes" "fmt" "io/fs" + "path" "strings" "git.mtkn.jp/lib9p" + "git.mtkn.jp/lib9p/testdata" ) type testFile struct { @@ -91,10 +93,12 @@ type testFS struct { // If slow is true, each function on files of this testFS waits for // something arrives on waitc. slow bool + nextQid uint64 // waitc is used to wait some timing to emulate slow response. waitc chan struct{} // groups holds the information of groups and it leader and members. groups map[string]group + } type group struct { @@ -104,7 +108,7 @@ type group struct { func (fs *testFS) OpenFile(path string, omode lib9p.OpenMode) (lib9p.File, error) { wnames := strings.Split(path, "/") - f, err := fs.Walk(wnames) + f, err := fs.walk(wnames) if err != nil { return nil, fmt.Errorf("walk: %v", err) } @@ -128,7 +132,7 @@ func (fs *testFS) IsGroupMember(gid, uid string) bool { return g.members[uid] } -func (fs *testFS) Walk(wnames []string) (*testFile, error) { +func (fs *testFS) walk(wnames []string) (*testFile, error) { if len(wnames) == 1 && (wnames[0] == "." || wnames[0] == "") { return fs.root, nil } @@ -146,96 +150,60 @@ L: return cwd, nil } -func (fs *testFS) WalkPath(pathname string) (*testFile, error) { +func (fs *testFS) walkPath(pathname string) (*testFile, error) { wnames := strings.Split(pathname, "/") - return fs.Walk(wnames) + return fs.walk(wnames) } var testfs *testFS func init() { - testfs = &testFS{ - root: &testFile{ - stat: lib9p.Stat{ - Qid: lib9p.Qid{Path: 0, Type: lib9p.QTDIR}, - Mode: lib9p.FileMode(fs.ModeDir | 0755), - Name: "root", - Uid: "glenda", - Gid: "glenda", - Muid: "glenda", - }, - children: []*testFile{ - &testFile{ - content: []byte("a\n"), - stat: lib9p.Stat{ - Qid: lib9p.Qid{Path: 1, Type: lib9p.QTFILE}, - Mode: lib9p.FileMode(0644), - Name: "a", - Uid: "glenda", - Gid: "glenda", - Muid: "glenda", - }, - }, - &testFile{ - content: []byte("b\n"), - stat: lib9p.Stat{ - Qid: lib9p.Qid{Path: 2, Type: lib9p.QTFILE}, - Mode: lib9p.FileMode(0600), - Name: "b", - Uid: "ken", - Gid: "ken", - Muid: "ken", - }, - }, - &testFile{ - stat: lib9p.Stat{ - Qid: lib9p.Qid{Path: 3, Type: lib9p.QTDIR}, - Mode: lib9p.FileMode(fs.ModeDir | 0755), - Name: "dir", - Uid: "rob", - Gid: "rob", - Muid: "rob", - }, - children: []*testFile{ - &testFile{ - content: []byte("unko\n"), - stat: lib9p.Stat{ - Qid: lib9p.Qid{Path: 4, Type: lib9p.QTFILE}, - Mode: lib9p.FileMode(0666), - Name: "file", - Uid: "brian", - Gid: "brian", - Muid: "dennis", - }, - }, - }, - }, - }, - }, - waitc: make(chan struct{}), - groups: map[string]group{ - "bell": group{ - leader: "glenda", - members: map[string]bool{ - "glenda": true, - "ken": true, - "dennis": true, - "brian": true, - "rob": true, - }, - }, - "kessoku": group{ - leader: "ijichi", - members: map[string]bool{ - "ijichi": true, - "yamada": true, - "gotoh": true, - "kita": true, - }, - }, - }, - } - SetfsysAndparent(testfs, nil) + fileTree := testdata.FileTree + testfs = new(testFS) + for i, f := range fileTree { + var ff *testFile + if i == 0 { // root of the testfs + testfs.root = new(testFile) + ff = testfs.root + ff.parent = testfs.root + } else { + d, err := testfs.walkPath(path.Dir(f.Path)) + if err != nil { + panic(err) + } + ff = new(testFile) + d.children = append(d.children, ff) + ff.parent = d + } + ff.content = []byte(f.Content) + var qt lib9p.QidType + if f.Mode&fs.ModeDir != 0 { + qt = lib9p.QTDIR + } else { + qt = lib9p.QTFILE + } + ff.stat = lib9p.Stat{ + Qid: lib9p.Qid{Path: uint64(i), Type: qt}, + Mode: f.Mode, + Name: path.Base(f.Path), + Uid: f.Uid, + Gid: f.Gid, + Muid: f.Muid, + } + ff.fsys = testfs + testfs.nextQid++ + } + testfs.waitc = make(chan struct{}) + testfs.groups = make(map[string]group) + for _, g := range testdata.Groups { + testfs.groups[g.Name] = group{ + leader: g.Leader, + members: make(map[string]bool), + } + for _, u := range g.Members { + testfs.groups[g.Name].members[u] = true + } + } } // SetfsysAndparent sets file.fsys and file.parent for every file in the fsys. diff --git a/fs_test.go b/fs_test.go @@ -8,6 +8,8 @@ import ( "strings" "testing" "time" + + "git.mtkn.jp/lib9p/testdata" ) type testFile struct { @@ -219,20 +221,7 @@ func (fs *testFS) walkPath(pathname string) (*testFile, error) { var testfs *testFS func init() { - fileTree := []struct { - pathname string - mode fs.FileMode - uid, gid, muid string - content string - }{ - {".", fs.ModeDir | 0755, "glenda", "glenda", "glenda", ""}, - {"a", 0644, "glenda", "glenda", "glenda", "a\n"}, - {"b", 0600, "ken", "ken", "ken", "b\n"}, - {"c", 0644, "glenda", "glenda", "glenda", ""}, - {"d", fs.ModeDir | 0770, "glenda", "bell", "glenda", ""}, - {"dir", fs.ModeDir | 0755, "rob", "rob", "rob", ""}, - {"dir/file", 0666, "brian", "brian", "dennis", "unko\n"}, - } + fileTree := testdata.FileTree testfs = new(testFS) for i, f := range fileTree { var ff *testFile @@ -241,7 +230,7 @@ func init() { ff = testfs.root ff.parent = testfs.root } else { - d, err := testfs.walkPath(path.Dir(f.pathname)) + d, err := testfs.walkPath(path.Dir(f.Path)) if err != nil { panic(err) } @@ -249,61 +238,34 @@ func init() { d.children = append(d.children, ff) ff.parent = d } - ff.content = []byte(f.content) + ff.content = []byte(f.Content) var qt QidType - if f.mode&fs.ModeDir != 0 { + if f.Mode&fs.ModeDir != 0 { qt = QTDIR } else { qt = QTFILE } ff.stat = Stat{ Qid: Qid{Path: uint64(i), Type: qt}, - Mode: f.mode, - Name: path.Base(f.pathname), - Uid: f.uid, - Gid: f.gid, - Muid: f.muid, + Mode: f.Mode, + Name: path.Base(f.Path), + Uid: f.Uid, + Gid: f.Gid, + Muid: f.Muid, } ff.fsys = testfs testfs.nextQid++ } testfs.waitc = make(chan struct{}) - testfs.groups = map[string]group{ - "glenda": group{ - leader: "glenda", - members: map[string]bool{"glenda": true}, - }, - "ken": group{ - leader: "ken", - members: map[string]bool{"ken": true}, - }, - "brian": group{ - leader: "brian", - members: map[string]bool{"brian": true}, - }, - "rob": group{ - leader: "rob", - members: map[string]bool{"rob": true}, - }, - "bell": group{ - leader: "glenda", - members: map[string]bool{ - "glenda": true, - "ken": true, - "dennis": true, - "brian": true, - "rob": true, - }, - }, - "kessoku": group{ - leader: "ijichi", - members: map[string]bool{ - "ijichi": true, - "yamada": true, - "gotoh": true, - "kita": true, - }, - }, + testfs.groups = make(map[string]group) + for _, g := range testdata.Groups { + testfs.groups[g.Name] = group{ + leader: g.Leader, + members: make(map[string]bool), + } + for _, u := range g.Members { + testfs.groups[g.Name].members[u] = true + } } } diff --git a/testdata/fs.go b/testdata/fs.go @@ -0,0 +1,33 @@ +package testdata + +import ( + "io/fs" +) + +var FileTree = []struct { + Path string + Mode fs.FileMode + Uid, Gid, Muid string + Content string +}{ + {".", fs.ModeDir | 0755, "glenda", "glenda", "glenda", ""}, + {"a", 0644, "glenda", "glenda", "glenda", "a\n"}, + {"b", 0600, "ken", "ken", "ken", "b\n"}, + {"c", 0644, "glenda", "glenda", "glenda", ""}, + {"d", fs.ModeDir | 0770, "glenda", "bell", "glenda", ""}, + {"dir", fs.ModeDir | 0755, "rob", "rob", "rob", ""}, + {"dir/file", 0666, "brian", "brian", "dennis", "unko\n"}, +} + +var Groups = []struct { + Name string + Leader string + Members []string +}{ + {"glenda", "glenda", []string{"glenda"}}, + {"ken", "ken", []string{"ken"}}, + {"brian", "brian", []string{"brian"}}, + {"rob", "rob", []string{"rob"}}, + {"bell", "glenda", []string{"glenda", "ken", "dennis", "brian", "rob"}}, + {"kessoku", "ijichi", []string{"ijichi", "yamada", "gotoh", "kita"}}, +}