Unverified 提交 96334bb0 作者: Steven Allen 提交者: GitHub

Merge pull request #5660 from ipfs/fix/resolve-paths

namesys: properly attach path in name.Resolve
...@@ -2,9 +2,11 @@ package coreapi_test ...@@ -2,9 +2,11 @@ package coreapi_test
import ( import (
"context" "context"
"github.com/ipfs/go-ipfs/core"
"io" "io"
"io/ioutil" "io/ioutil"
"math/rand" "math/rand"
"path"
"testing" "testing"
"time" "time"
...@@ -21,45 +23,150 @@ func addTestObject(ctx context.Context, api coreiface.CoreAPI) (coreiface.Path, ...@@ -21,45 +23,150 @@ func addTestObject(ctx context.Context, api coreiface.CoreAPI) (coreiface.Path,
return api.Unixfs().Add(ctx, files.NewReaderFile("", "", ioutil.NopCloser(&io.LimitedReader{R: rnd, N: 4092}), nil)) return api.Unixfs().Add(ctx, files.NewReaderFile("", "", ioutil.NopCloser(&io.LimitedReader{R: rnd, N: 4092}), nil))
} }
func TestBasicPublishResolve(t *testing.T) { func appendPath(p coreiface.Path, sub string) coreiface.Path {
ctx := context.Background() p, err := coreiface.ParsePath(path.Join(p.String(), sub))
nds, apis, err := makeAPISwarm(ctx, true, 5)
if err != nil { if err != nil {
t.Fatal(err) panic(err)
return
} }
n := nds[0] return p
api := apis[0] }
p, err := addTestObject(ctx, api) func TestPublishResolve(t *testing.T) {
if err != nil { ctx := context.Background()
t.Fatal(err) init := func() (*core.IpfsNode, coreiface.CoreAPI, coreiface.Path) {
return nds, apis, err := makeAPISwarm(ctx, true, 5)
} if err != nil {
t.Fatal(err)
return nil, nil, nil
}
n := nds[0]
api := apis[0]
e, err := api.Name().Publish(ctx, p) p, err := addTestObject(ctx, api)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
return return nil, nil, nil
}
return n, api, p
} }
if e.Name() != n.Identity.Pretty() { run := func(t *testing.T, ropts []opt.NameResolveOption) {
t.Errorf("expected e.Name to equal '%s', got '%s'", n.Identity.Pretty(), e.Name()) t.Run("basic", func(t *testing.T) {
} n, api, p := init()
e, err := api.Name().Publish(ctx, p)
if err != nil {
t.Fatal(err)
return
}
if e.Value().String() != p.String() { if e.Name() != n.Identity.Pretty() {
t.Errorf("expected paths to match, '%s'!='%s'", e.Value().String(), p.String()) t.Errorf("expected e.Name to equal '%s', got '%s'", n.Identity.Pretty(), e.Name())
} }
resPath, err := api.Name().Resolve(ctx, e.Name()) if e.Value().String() != p.String() {
if err != nil { t.Errorf("expected paths to match, '%s'!='%s'", e.Value().String(), p.String())
t.Fatal(err) }
return
}
if resPath.String() != p.String() { resPath, err := api.Name().Resolve(ctx, e.Name(), ropts...)
t.Errorf("expected paths to match, '%s'!='%s'", resPath.String(), p.String()) if err != nil {
t.Fatal(err)
return
}
if resPath.String() != p.String() {
t.Errorf("expected paths to match, '%s'!='%s'", resPath.String(), p.String())
}
})
t.Run("publishPath", func(t *testing.T) {
n, api, p := init()
e, err := api.Name().Publish(ctx, appendPath(p, "/test"))
if err != nil {
t.Fatal(err)
return
}
if e.Name() != n.Identity.Pretty() {
t.Errorf("expected e.Name to equal '%s', got '%s'", n.Identity.Pretty(), e.Name())
}
if e.Value().String() != p.String()+"/test" {
t.Errorf("expected paths to match, '%s'!='%s'", e.Value().String(), p.String())
}
resPath, err := api.Name().Resolve(ctx, e.Name(), ropts...)
if err != nil {
t.Fatal(err)
return
}
if resPath.String() != p.String()+"/test" {
t.Errorf("expected paths to match, '%s'!='%s'", resPath.String(), p.String()+"/test")
}
})
t.Run("revolvePath", func(t *testing.T) {
n, api, p := init()
e, err := api.Name().Publish(ctx, p)
if err != nil {
t.Fatal(err)
return
}
if e.Name() != n.Identity.Pretty() {
t.Errorf("expected e.Name to equal '%s', got '%s'", n.Identity.Pretty(), e.Name())
}
if e.Value().String() != p.String() {
t.Errorf("expected paths to match, '%s'!='%s'", e.Value().String(), p.String())
}
resPath, err := api.Name().Resolve(ctx, e.Name()+"/test", ropts...)
if err != nil {
t.Fatal(err)
return
}
if resPath.String() != p.String()+"/test" {
t.Errorf("expected paths to match, '%s'!='%s'", resPath.String(), p.String()+"/test")
}
})
t.Run("publishRevolvePath", func(t *testing.T) {
n, api, p := init()
e, err := api.Name().Publish(ctx, appendPath(p, "/a"))
if err != nil {
t.Fatal(err)
return
}
if e.Name() != n.Identity.Pretty() {
t.Errorf("expected e.Name to equal '%s', got '%s'", n.Identity.Pretty(), e.Name())
}
if e.Value().String() != p.String()+"/a" {
t.Errorf("expected paths to match, '%s'!='%s'", e.Value().String(), p.String())
}
resPath, err := api.Name().Resolve(ctx, e.Name()+"/b", ropts...)
if err != nil {
t.Fatal(err)
return
}
if resPath.String() != p.String()+"/a/b" {
t.Errorf("expected paths to match, '%s'!='%s'", resPath.String(), p.String()+"/a/b")
}
})
} }
t.Run("default", func(t *testing.T) {
run(t, []opt.NameResolveOption{})
})
t.Run("nocache", func(t *testing.T) {
run(t, []opt.NameResolveOption{opt.Name.Cache(false)})
})
} }
func TestBasicPublishResolveKey(t *testing.T) { func TestBasicPublishResolveKey(t *testing.T) {
......
...@@ -100,6 +100,14 @@ func (ns *mpns) resolveOnceAsync(ctx context.Context, name string, options opts. ...@@ -100,6 +100,14 @@ func (ns *mpns) resolveOnceAsync(ctx context.Context, name string, options opts.
key := segments[2] key := segments[2]
if p, ok := ns.cacheGet(key); ok { if p, ok := ns.cacheGet(key); ok {
if len(segments) > 3 {
var err error
p, err = path.FromSegments("", strings.TrimRight(p.String(), "/"), segments[3])
if err != nil {
emitOnceResult(ctx, out, onceResult{value: p, err: err})
}
}
out <- onceResult{value: p} out <- onceResult{value: p}
close(out) close(out)
return out return out
...@@ -139,7 +147,8 @@ func (ns *mpns) resolveOnceAsync(ctx context.Context, name string, options opts. ...@@ -139,7 +147,8 @@ func (ns *mpns) resolveOnceAsync(ctx context.Context, name string, options opts.
// Attach rest of the path // Attach rest of the path
if len(segments) > 3 { if len(segments) > 3 {
p, err := path.FromSegments("", strings.TrimRight(p.String(), "/"), segments[3]) var err error
p, err = path.FromSegments("", strings.TrimRight(p.String(), "/"), segments[3])
if err != nil { if err != nil {
emitOnceResult(ctx, out, onceResult{value: p, ttl: res.ttl, err: err}) emitOnceResult(ctx, out, onceResult{value: p, ttl: res.ttl, err: err})
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论