Skip to content

Commit

Permalink
IPFS-test WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
djdv committed Sep 4, 2019
1 parent fe21e45 commit eda58f3
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 64 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/Kubuxu/gocovmerge v0.0.0-20161216165753-7ecaa51963cd
github.com/blang/semver v3.5.1+incompatible
github.com/bren2010/proquint v0.0.0-20160323162903-38337c27106d
github.com/djdv/p9 v0.0.0-20190903035542-f10bd070d5fa
github.com/djdv/p9 v0.0.0-20190904195120-79d6235e5781
github.com/dustin/go-humanize v1.0.0
github.com/elgris/jsondiff v0.0.0-20160530203242-765b5c24c302
github.com/fatih/color v1.7.0 // indirect
Expand Down
7 changes: 2 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ github.com/dgryski/go-farm v0.0.0-20190104051053-3adb47b1fb0f/go.mod h1:SqUrOPUn
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2 h1:tdlZCpZ/P9DhczCTSixgIKmwPv6+wP5DGjqLYw5SUiA=
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/djdv/p9 v0.0.0-20190903035542-f10bd070d5fa h1:io9eEeeCTX2C/8X9B+bqjAC4tfTdWnnP/xGvavjGsU8=
github.com/djdv/p9 v0.0.0-20190903035542-f10bd070d5fa/go.mod h1:zkE6LlYAu33C3BZhXmX0eTFyHDwM7cAnRvqd+JcEf4M=
github.com/djdv/p9 v0.0.0-20190904195120-79d6235e5781 h1:S4uzVqSytBLRlOp444yimbtrIqm/A31BC7KbyTpgAm8=
github.com/djdv/p9 v0.0.0-20190904195120-79d6235e5781/go.mod h1:58+5oyIFRxi9jt7uQrdqpmUryJDTsxY8N8GxhnNocLQ=
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/elgris/jsondiff v0.0.0-20160530203242-765b5c24c302 h1:QV0ZrfBLpFc2KDk+a4LJefDczXnonRwrYrQJY/9L4dA=
Expand Down Expand Up @@ -197,8 +197,6 @@ github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/hugelgupf/p9 v0.0.0-20190902012917-f08d8f7f979b h1:8IouwKZVKhBvn3+SjnR7OFPIdCsDaN8rGOvbqI7zV5k=
github.com/hugelgupf/p9 v0.0.0-20190902012917-f08d8f7f979b/go.mod h1:lQha5pHJCBOvLDb0yaewOSud/TGvhlp9jtfQuIbgzJU=
github.com/hugelgupf/socketpair v0.0.0-20190730060125-05d35a94e714 h1:/jC7qQFrv8CrSJVmaolDVOxTfS9kc36uB6H40kdbQq8=
github.com/hugelgupf/socketpair v0.0.0-20190730060125-05d35a94e714/go.mod h1:2Goc3h8EklBH5mspfHFxBnEoURQCGzQQH1ga9Myjvis=
github.com/huin/goupnp v0.0.0-20180415215157-1395d1447324/go.mod h1:MZ2ZmwcBpvOoJ22IJsc7va19ZwoheaBk43rKg12SKag=
Expand Down Expand Up @@ -869,7 +867,6 @@ golang.org/x/sys v0.0.0-20190526052359-791d8a0f4d09/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20190610200419-93c9922d18ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb h1:fgwFCsaw9buMuxNd6+DQfAuSFqbNiQZpcgJQAgJsK6k=
golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190730183949-1393eb018365/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190903213830-1f305c863dab h1:2WmrFWBmUPXp+o/5X0nS66SLRS6DKwZlgFD76BKThvc=
golang.org/x/sys v0.0.0-20190903213830-1f305c863dab/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.0.0-20170915090833-1cbadb444a80/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
190 changes: 158 additions & 32 deletions plugin/plugins/filesystem/filesystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"testing"
"time"

"github.com/djdv/p9/localfs"
"github.com/djdv/p9/p9"
files "github.com/ipfs/go-ipfs-files"

Expand All @@ -24,6 +25,11 @@ import (
corepath "github.com/ipfs/interface-go-ipfs-core/path"
)

var attrMaskIPFSTest = p9.AttrMask{
Mode: true,
Size: true,
}

func TestAll(t *testing.T) {
ctx := context.TODO()
core, err := initCore(ctx)
Expand All @@ -35,6 +41,7 @@ func TestAll(t *testing.T) {

t.Run("RootFS", func(t *testing.T) { testRootFS(t, ctx, core, logger) })
t.Run("PinFS", func(t *testing.T) { testPinFS(t, ctx, core, logger) })
t.Run("IPFS", func(t *testing.T) { testIPFS(t, ctx, core, logger) })
}

func testRootFS(t *testing.T, ctx context.Context, core coreiface.CoreAPI, logger logging.EventLogger) {
Expand Down Expand Up @@ -114,34 +121,125 @@ func testPinFS(t *testing.T, ctx context.Context, core coreiface.CoreAPI, logger
shallowCompare()

// test modifying pinset +1 again; generate garbage and pin it
{
if err := generateGarbage(env); err != nil {
t.Fatalf("Failed to generate test data: %s\n", err)
}

_, err := pinAddDir(ctx, core, env)
if err != nil {
t.Fatalf("Failed to add directory to IPFS: %s\n", err)
}
if err := generateGarbage(env); err != nil {
t.Fatalf("Failed to generate test data: %s\n", err)
}
if _, err = pinAddDir(ctx, core, env); err != nil {
t.Fatalf("Failed to add directory to IPFS: %s\n", err)
}
shallowCompare()

//TODO: type checking
}
func TestIPFS(t *testing.T) {
ctx := context.TODO()
core, err := initCore(ctx)
func testIPFS(t *testing.T, ctx context.Context, core coreiface.CoreAPI, logger logging.EventLogger) {
env, iEnv, err := initEnv(ctx, core)
if err != nil {
t.Fatalf("Failed to construct IPFS node: %s\n", err)
t.Fatalf("Failed to construct IPFS test environment: %s\n", err)
}
defer os.RemoveAll(env)

env, iEnv, err := initEnv(ctx, core)
localEnv, err := localfs.Attacher(env).Attach()
if err != nil {
t.Fatalf("Failed to construct IPFS test environment: %s\n", err)
t.Fatalf("Failed to attach to local resource %q: %s\n", env, err)
}

t.Logf("env:%v\niEnv:%v\nerr:%s\n", env, iEnv, err)
defer os.RemoveAll(env)
ipfsRoot, err := fsnodes.InitIPFS(ctx, core, logger).Attach()
if err != nil {
t.Fatalf("Failed to attach to IPFS resource: %s\n", err)
}
_, ipfsEnv, err := ipfsRoot.Walk([]string{gopath.Base(iEnv.String())})
if err != nil {
t.Fatalf("Failed to walk to IPFS test envrionment: %s\n", err)
}

recursiveCompare(t, localEnv, ipfsEnv)
}

//TODO: rename
func recursiveCompare(t *testing.T, f1, f2 p9.File) {
var expand func(p9.File) (map[string]p9.Attr, error)
expand = func(nineRef p9.File) (map[string]p9.Attr, error) {
ents, err := p9Readdir(nineRef)
if err != nil {
return nil, err
}

//TODO: current
// map[string]Stat; ["/sub/incantation"]{...}
///from root, walk(name); stat; if dir; recurse

res := make(map[string]p9.Attr)
for _, ent := range ents {
_, child, err := nineRef.Walk([]string{ent.Name})
if err != nil {
return nil, err
}

_, _, attr, err := child.GetAttr(attrMaskIPFSTest)
if err != nil {
return nil, err
}
res[ent.Name] = attr
//p9.AttrMaskAll

if ent.Type == p9.TypeDir {
subRes, err := expand(child)
if err != nil {
return nil, err
}
for name, attr := range subRes {
res[gopath.Join(ent.Name, name)] = attr
}
}
}
return res, nil
}

f1Map, err := expand(f1)
if err != nil {
t.Fatal(err)
}

f2Map, err := expand(f2)
if err != nil {
t.Fatal(err)
}

same := func(permissionContains p9.FileMode, base, target map[string]p9.Attr) bool {
if len(base) != len(target) {

var baseNames []string
var targetNames []string
for name, _ := range base {
baseNames = append(baseNames, name)
}
for name, _ := range target {
targetNames = append(targetNames, name)
}

t.Fatalf("map lengths don't match:\nbase:%v\ntarget:%v\n", baseNames, targetNames)
return false
}

for path, baseAttr := range base {
bMode := baseAttr.Mode
tMode := target[path].Mode

if bMode.FileType() != tMode.FileType() {
t.Fatalf("type for %q don't match:\nbase:%v\ntarget:%v\n", path, bMode, tMode)
return false
}

if ((bMode.Permissions() & permissionContains) & (tMode.Permissions() & permissionContains)) == 0 {
t.Fatalf("permissions for %q don't match (unfiltered):\nbase:%v\ntarget:%v\n", path, bMode.Permissions(), tMode.Permissions())
return false
}
}
return true
}
if !same(p9.Read, f1Map, f2Map) {
t.Fatalf("contents don't match \nf1:%v\nf2:%v\n", f1Map, f2Map)
}
}

func initCore(ctx context.Context) (coreiface.CoreAPI, error) {
Expand All @@ -160,33 +258,41 @@ func initCore(ctx context.Context) (coreiface.CoreAPI, error) {
const incantation = "May the bits passing through this device somehow help bring peace to this world"

func initEnv(ctx context.Context, core coreiface.CoreAPI) (string, corepath.Resolved, error) {
tempDir, err := ioutil.TempDir("", "ipfs-")
testDir, err := ioutil.TempDir("", "ipfs-")
if err != nil {
return "", nil, err
}

if err = ioutil.WriteFile(filepath.Join(tempDir, "empty"),
if err = ioutil.WriteFile(filepath.Join(testDir, "empty"),
[]byte(nil),
0644); err != nil {
return "", nil, err
}

if err = ioutil.WriteFile(filepath.Join(tempDir, "small"),
if err = ioutil.WriteFile(filepath.Join(testDir, "small"),
[]byte(incantation),
0644); err != nil {
return "", nil, err
}

if err := generateGarbage(tempDir); err != nil {
if err := generateGarbage(testDir); err != nil {
return "", nil, err
}

iPath, err := pinAddDir(ctx, core, tempDir)
testSubDir, err := ioutil.TempDir(testDir, "ipfs-")
if err != nil {
return "", nil, err
}
if err := generateGarbage(testSubDir); err != nil {
return "", nil, err
}

return tempDir, iPath, err
iPath, err := pinAddDir(ctx, core, testDir)
if err != nil {
return "", nil, err
}

return testDir, iPath, err
}

func pinAddDir(ctx context.Context, core coreiface.CoreAPI, path string) (corepath.Resolved, error) {
Expand Down Expand Up @@ -240,25 +346,45 @@ func pinNames(ctx context.Context, core coreiface.CoreAPI) ([]string, error) {
}

func p9PinNames(root p9.File) ([]string, error) {
_, rootDir, err := root.Walk(nil)
ents, err := p9Readdir(root)
if err != nil {
return nil, err
}

_, _, err = rootDir.Open(p9.ReadOnly)
names := make([]string, 0, len(ents))

for _, ent := range ents {
names = append(names, ent.Name)
}

return names, root.Close()
}

func p9Readdir(dir p9.File) ([]p9.Dirent, error) {
_, dir, err := dir.Walk(nil)
if err != nil {
return nil, err
}
ents, err := rootDir.Readdir(0, ^uint32(0))

_, _, err = dir.Open(p9.ReadOnly)
if err != nil {
return nil, err
}
defer dir.Close()
return dir.Readdir(0, ^uint32(0))
}

names := make([]string, 0, len(ents))

for _, ent := range ents {
names = append(names, ent.Name)
// NOTE: compares a subset of attributes, matching those of IPFS
func testIPFSCompare(t *testing.T, f1, f2 p9.File) {
_, _, f1Attr, err := f1.GetAttr(attrMaskIPFSTest)
if err != nil {
t.Errorf("Attr(%v) = %v, want nil", f1, err)
}
_, _, f2Attr, err := f2.GetAttr(attrMaskIPFSTest)
if err != nil {
t.Errorf("Attr(%v) = %v, want nil", f2, err)
}
if f1Attr != f2Attr {
t.Errorf("Attributes of same files do not match: %v and %v", f1Attr, f2Attr)
}

return names, rootDir.Close()
}
11 changes: 6 additions & 5 deletions plugin/plugins/filesystem/nodes/ipfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type IPFS struct {
}

//TODO: [review] check fields; better wrappers around inheritance init, etc.
func initIPFS(ctx context.Context, core coreiface.CoreAPI, logger logging.EventLogger) p9.Attacher {
func InitIPFS(ctx context.Context, core coreiface.CoreAPI, logger logging.EventLogger) p9.Attacher {
id := &IPFS{IPFSBase: newIPFSBase(ctx, newRootPath("/ipfs"), p9.TypeDir, core, logger)}
id.meta, id.metaMask = defaultRootAttr()
return id
Expand All @@ -37,14 +37,15 @@ func (id *IPFS) GetAttr(req p9.AttrMask) (p9.QID, p9.AttrMask, p9.Attr, error) {
return id.Qid, id.metaMask, id.meta, nil
}

var attrMask p9.AttrMask
if err := coreGetAttr(id.Ctx, &id.meta, &attrMask, id.core, id.Path); err != nil {
if err := coreGetAttr(id.Ctx, &id.meta, req, id.core, id.Path); err != nil {
return p9.QID{}, p9.AttrMask{}, p9.Attr{}, err
}
timeStamp(&id.meta, &attrMask)
id.Qid.Type = id.meta.Mode.QIDType()

return id.Qid, attrMask, id.meta, nil
metaClone := id.meta
metaClone.Filter(req)

return id.Qid, req, metaClone, nil
}

func (id *IPFS) Walk(names []string) ([]p9.QID, p9.File, error) {
Expand Down
2 changes: 1 addition & 1 deletion plugin/plugins/filesystem/nodes/pinfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (pd *PinFS) Walk(names []string) ([]p9.QID, p9.File, error) {
return []p9.QID{pd.Qid}, pd, nil
}

ipfsDir, err := initIPFS(pd.Ctx, pd.core, pd.Logger).Attach()
ipfsDir, err := InitIPFS(pd.Ctx, pd.core, pd.Logger).Attach()
if err != nil {
return nil, nil, err
}
Expand Down
Loading

0 comments on commit eda58f3

Please sign in to comment.