1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-22 04:09:04 +08:00

coreapi: add tests for dag

License: MIT
Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
This commit is contained in:
Łukasz Magiera
2017-12-11 17:22:21 +01:00
parent 82924fbedf
commit b40a6f88eb
5 changed files with 125 additions and 23 deletions

View File

@ -26,7 +26,7 @@ func (api *CoreAPI) Unixfs() coreiface.UnixfsAPI {
} }
func (api *CoreAPI) Dag() coreiface.DagAPI { func (api *CoreAPI) Dag() coreiface.DagAPI {
return (*DagAPI)(api) return (*dagAPI)(api)
} }
func (api *CoreAPI) ResolveNode(ctx context.Context, p coreiface.Path) (coreiface.Node, error) { func (api *CoreAPI) ResolveNode(ctx context.Context, p coreiface.Path) (coreiface.Node, error) {

View File

@ -10,13 +10,13 @@ import (
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface" coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
coredag "github.com/ipfs/go-ipfs/core/coredag" coredag "github.com/ipfs/go-ipfs/core/coredag"
cid "gx/ipfs/QmNp85zy9RLrQ5oQD4hPyS39ezrrXpcaa7R4Y9kxdWQLLQ/go-cid" mh "gx/ipfs/QmYeKnKpubCMRiq3PGZcTREErthbb5Q9cXsCoSkD9bjEBd/go-multihash"
mh "gx/ipfs/QmU9a9NV9RdPNwZQDYd5uKsm6N6LJLSvLbywDDYFbaaC6P/go-multihash" cid "gx/ipfs/QmeSrf6pzut73u6zLQkRFQ3ygt3k6XFT2kjdYP8Tnkwwyg/go-cid"
) )
type DagAPI CoreAPI type dagAPI CoreAPI
func (api *DagAPI) Put(ctx context.Context, src io.Reader, inputEnc string, format *cid.Prefix) ([]coreiface.Node, error) { func (api *dagAPI) Put(ctx context.Context, src io.Reader, inputEnc string, format *cid.Prefix) ([]coreiface.Node, error) {
if format == nil { if format == nil {
format = &cid.Prefix{ format = &cid.Prefix{
Version: 1, Version: 1,
@ -51,11 +51,11 @@ func (api *DagAPI) Put(ctx context.Context, src io.Reader, inputEnc string, form
return out, nil return out, nil
} }
func (api *DagAPI) Get(ctx context.Context, path coreiface.Path) (coreiface.Node, error) { func (api *dagAPI) Get(ctx context.Context, path coreiface.Path) (coreiface.Node, error) {
return api.core().ResolveNode(ctx, path) return api.core().ResolveNode(ctx, path)
} }
func (api *DagAPI) Tree(ctx context.Context, p coreiface.Path, depth int) ([]coreiface.Path, error) { func (api *dagAPI) Tree(ctx context.Context, p coreiface.Path, depth int) ([]coreiface.Path, error) {
n, err := api.Get(ctx, p) n, err := api.Get(ctx, p)
if err != nil { if err != nil {
return nil, err return nil, err
@ -72,6 +72,6 @@ func (api *DagAPI) Tree(ctx context.Context, p coreiface.Path, depth int) ([]cor
return out, nil return out, nil
} }
func (api *DagAPI) core() coreiface.CoreAPI { func (api *dagAPI) core() coreiface.CoreAPI {
return (*CoreAPI)(api) return (*CoreAPI)(api)
} }

93
core/coreapi/dag_test.go Normal file
View File

@ -0,0 +1,93 @@
package coreapi_test
import (
"context"
"path"
"strings"
"testing"
coreapi "github.com/ipfs/go-ipfs/core/coreapi"
)
var (
treeExpected = map[string]struct{}{
"a": {},
"b": {},
"c": {},
"c/d": {},
"c/e": {},
}
)
func TestPut(t *testing.T) {
ctx := context.Background()
_, api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}
res, err := api.Dag().Put(ctx, strings.NewReader(`"Hello"`), "json", nil)
if err != nil {
t.Error(err)
}
if res[0].Cid().String() != "zdpuAqckYF3ToF3gcJNxPZXmnmGuXd3gxHCXhq81HGxBejEvv" {
t.Errorf("got wrong cid: %s", res[0].Cid().String())
}
}
func TestPath(t *testing.T) {
ctx := context.Background()
_, api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}
sub, err := api.Dag().Put(ctx, strings.NewReader(`"foo"`), "json", nil)
if err != nil {
t.Error(err)
}
res, err := api.Dag().Put(ctx, strings.NewReader(`{"lnk": {"/": "`+sub[0].Cid().String()+`"}}`), "json", nil)
if err != nil {
t.Error(err)
}
p, err := coreapi.ParsePath(path.Join(res[0].Cid().String(), "lnk"))
if err != nil {
t.Error(err)
}
nd, err := api.Dag().Get(ctx, p)
if err != nil {
t.Error(err)
}
if nd.Cid().String() != sub[0].Cid().String() {
t.Errorf("got unexpected cid %s, expected %s", nd.Cid().String(), sub[0].Cid().String())
}
}
func TestTree(t *testing.T) {
ctx := context.Background()
_, api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}
res, err := api.Dag().Put(ctx, strings.NewReader(`{"a": 123, "b": "foo", "c": {"d": 321, "e": 111}}`), "json", nil)
if err != nil {
t.Error(err)
}
lst := res[0].Tree("", -1)
if len(lst) != len(treeExpected) {
t.Errorf("tree length of %d doesn't match expected %d", len(lst), len(treeExpected))
}
for _, ent := range lst {
if _, ok := treeExpected[ent]; !ok {
t.Errorf("unexpected tree entry %s", ent)
}
}
}

View File

@ -41,7 +41,7 @@ type CoreAPI interface {
// ResolveNode resolves the path (if not resolved already) using Unixfs // ResolveNode resolves the path (if not resolved already) using Unixfs
// resolver, gets and returns the resolved Node // resolver, gets and returns the resolved Node
ResolveNode(context.Context, Path) (Node, error) //TODO: should this get dropped in favor of DagAPI.Get? ResolveNode(context.Context, Path) (Node, error)
} }
// UnixfsAPI is the basic interface to immutable files in IPFS // UnixfsAPI is the basic interface to immutable files in IPFS
@ -56,9 +56,17 @@ type UnixfsAPI interface {
Ls(context.Context, Path) ([]*Link, error) Ls(context.Context, Path) ([]*Link, error)
} }
// DagAPI specifies the interface to IPLD
type DagAPI interface { type DagAPI interface {
Put(ctx context.Context, src io.Reader, inputEnc string, format *cid.Prefix) ([]Node, error) // Put inserts data using specified format and input encoding.
// If format is not specified (nil), default dag-cbor/sha256 is used
Put(ctx context.Context, src io.Reader, inputEnc string, format *cid.Prefix) ([]Node, error) //TODO: make format optional
// Get attempts to resolve and get the node specified by the path
Get(ctx context.Context, path Path) (Node, error) Get(ctx context.Context, path Path) (Node, error)
// Tree returns list of paths within a node specified by the path.
// To get all paths in a tree, set depth to -1
Tree(ctx context.Context, path Path, depth int) ([]Path, error) Tree(ctx context.Context, path Path, depth int) ([]Path, error)
} }

View File

@ -17,6 +17,7 @@ import (
config "github.com/ipfs/go-ipfs/repo/config" config "github.com/ipfs/go-ipfs/repo/config"
ds2 "github.com/ipfs/go-ipfs/thirdparty/datastore2" ds2 "github.com/ipfs/go-ipfs/thirdparty/datastore2"
unixfs "github.com/ipfs/go-ipfs/unixfs" unixfs "github.com/ipfs/go-ipfs/unixfs"
cbor "gx/ipfs/QmeZv9VXw2SfVbX55LV6kGTWASKBc9ZxAVqGBeJcDGdoXy/go-ipld-cbor" cbor "gx/ipfs/QmeZv9VXw2SfVbX55LV6kGTWASKBc9ZxAVqGBeJcDGdoXy/go-ipld-cbor"
) )
@ -30,7 +31,7 @@ var emptyDir = coreapi.ResolvedPath("/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbs
// `echo -n | ipfs add` // `echo -n | ipfs add`
var emptyFile = coreapi.ResolvedPath("/ipfs/QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH", nil, nil) var emptyFile = coreapi.ResolvedPath("/ipfs/QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH", nil, nil)
func makeAPI(ctx context.Context) (*core.IpfsNode, coreiface.UnixfsAPI, error) { func makeAPI(ctx context.Context) (*core.IpfsNode, coreiface.CoreAPI, error) {
r := &repo.Mock{ r := &repo.Mock{
C: config.Config{ C: config.Config{
Identity: config.Identity{ Identity: config.Identity{
@ -43,7 +44,7 @@ func makeAPI(ctx context.Context) (*core.IpfsNode, coreiface.UnixfsAPI, error) {
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
api := coreapi.NewCoreAPI(node).Unixfs() api := coreapi.NewCoreAPI(node)
return node, api, nil return node, api, nil
} }
@ -55,7 +56,7 @@ func TestAdd(t *testing.T) {
} }
str := strings.NewReader(helloStr) str := strings.NewReader(helloStr)
p, err := api.Add(ctx, str) p, err := api.Unixfs().Add(ctx, str)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@ -64,7 +65,7 @@ func TestAdd(t *testing.T) {
t.Fatalf("expected path %s, got: %s", hello, p) t.Fatalf("expected path %s, got: %s", hello, p)
} }
r, err := api.Cat(ctx, hello) r, err := api.Unixfs().Cat(ctx, hello)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -87,7 +88,7 @@ func TestAddEmptyFile(t *testing.T) {
} }
str := strings.NewReader("") str := strings.NewReader("")
p, err := api.Add(ctx, str) p, err := api.Unixfs().Add(ctx, str)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@ -115,7 +116,7 @@ func TestCatBasic(t *testing.T) {
t.Fatalf("expected CID %s, got: %s", hello, p) t.Fatalf("expected CID %s, got: %s", hello, p)
} }
r, err := api.Cat(ctx, hello) r, err := api.Unixfs().Cat(ctx, hello)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -142,7 +143,7 @@ func TestCatEmptyFile(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
r, err := api.Cat(ctx, emptyFile) r, err := api.Unixfs().Cat(ctx, emptyFile)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -174,7 +175,7 @@ func TestCatDir(t *testing.T) {
t.Fatalf("expected path %s, got: %s", emptyDir, p) t.Fatalf("expected path %s, got: %s", emptyDir, p)
} }
_, err = api.Cat(ctx, emptyDir) _, err = api.Unixfs().Cat(ctx, emptyDir)
if err != coreiface.ErrIsDir { if err != coreiface.ErrIsDir {
t.Fatalf("expected ErrIsDir, got: %s", err) t.Fatalf("expected ErrIsDir, got: %s", err)
} }
@ -192,7 +193,7 @@ func TestCatNonUnixfs(t *testing.T) {
t.Error(err) t.Error(err)
} }
_, err = api.Cat(ctx, coreapi.ParseCid(c)) _, err = api.Unixfs().Cat(ctx, coreapi.ParseCid(c))
if !strings.Contains(err.Error(), "proto: required field") { if !strings.Contains(err.Error(), "proto: required field") {
t.Fatalf("expected protobuf error, got: %s", err) t.Fatalf("expected protobuf error, got: %s", err)
} }
@ -205,7 +206,7 @@ func TestCatOffline(t *testing.T) {
t.Error(err) t.Error(err)
} }
_, err = api.Cat(ctx, coreapi.ResolvedPath("/ipns/Qmfoobar", nil, nil)) _, err = api.Unixfs().Cat(ctx, coreapi.ResolvedPath("/ipns/Qmfoobar", nil, nil))
if err != coreiface.ErrOffline { if err != coreiface.ErrOffline {
t.Fatalf("expected ErrOffline, got: %s", err) t.Fatalf("expected ErrOffline, got: %s", err)
} }
@ -229,7 +230,7 @@ func TestLs(t *testing.T) {
} }
p := coreapi.ResolvedPath("/ipfs/"+parts[0], nil, nil) p := coreapi.ResolvedPath("/ipfs/"+parts[0], nil, nil)
links, err := api.Ls(ctx, p) links, err := api.Unixfs().Ls(ctx, p)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@ -260,7 +261,7 @@ func TestLsEmptyDir(t *testing.T) {
t.Error(err) t.Error(err)
} }
links, err := api.Ls(ctx, emptyDir) links, err := api.Unixfs().Ls(ctx, emptyDir)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@ -288,7 +289,7 @@ func TestLsNonUnixfs(t *testing.T) {
t.Error(err) t.Error(err)
} }
links, err := api.Ls(ctx, coreapi.ParseCid(c)) links, err := api.Unixfs().Ls(ctx, coreapi.ParseCid(c))
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }