From 81a2ffc4073674d7b545dfd0a5b02adacd119b1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Mon, 7 Jan 2019 16:19:55 +0100 Subject: [PATCH] CoreAPI: Don't panic when testing incomplete implementions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Ɓukasz Magiera --- core/coreapi/interface/tests/api.go | 16 ++++++++++ core/coreapi/interface/tests/block.go | 7 +++++ core/coreapi/interface/tests/dag.go | 7 +++++ core/coreapi/interface/tests/dht.go | 8 +++++ core/coreapi/interface/tests/key.go | 8 +++++ core/coreapi/interface/tests/name.go | 7 +++++ core/coreapi/interface/tests/object.go | 7 +++++ core/coreapi/interface/tests/path.go | 42 ++++++++++++++++++++------ core/coreapi/interface/tests/pin.go | 8 +++++ core/coreapi/interface/tests/pubsub.go | 8 +++++ core/coreapi/interface/tests/unixfs.go | 7 +++++ 11 files changed, 116 insertions(+), 9 deletions(-) diff --git a/core/coreapi/interface/tests/api.go b/core/coreapi/interface/tests/api.go index 23ec0612bcc..7a4bd738624 100644 --- a/core/coreapi/interface/tests/api.go +++ b/core/coreapi/interface/tests/api.go @@ -2,12 +2,15 @@ package tests import ( "context" + "errors" "testing" "time" coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface" ) +var apiNotImplemented = errors.New("api not implemented") + func (tp *provider) makeAPI(ctx context.Context) (coreiface.CoreAPI, error) { api, err := tp.MakeAPISwarm(ctx, false, 1) if err != nil { @@ -76,3 +79,16 @@ func TestApi(p Provider) func(t *testing.T) { }) } } + +func (tp *provider) hasApi(t *testing.T, tf func(coreiface.CoreAPI) error) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) + if err != nil { + t.Fatal(err) + } + + if err := tf(api); err != nil { + t.Fatal(api) + } +} diff --git a/core/coreapi/interface/tests/block.go b/core/coreapi/interface/tests/block.go index d1117cc50c2..81a6fb0617b 100644 --- a/core/coreapi/interface/tests/block.go +++ b/core/coreapi/interface/tests/block.go @@ -13,6 +13,13 @@ import ( ) func (tp *provider) TestBlock(t *testing.T) { + tp.hasApi(t, func(api coreiface.CoreAPI) error { + if api.Block() == nil { + return apiNotImplemented + } + return nil + }) + t.Run("TestBlockPut", tp.TestBlockPut) t.Run("TestBlockPutFormat", tp.TestBlockPutFormat) t.Run("TestBlockPutHash", tp.TestBlockPutHash) diff --git a/core/coreapi/interface/tests/dag.go b/core/coreapi/interface/tests/dag.go index d5026394347..70a45aa204c 100644 --- a/core/coreapi/interface/tests/dag.go +++ b/core/coreapi/interface/tests/dag.go @@ -13,6 +13,13 @@ import ( ) func (tp *provider) TestDag(t *testing.T) { + tp.hasApi(t, func(api coreiface.CoreAPI) error { + if api.Dag() == nil { + return apiNotImplemented + } + return nil + }) + t.Run("TestPut", tp.TestPut) t.Run("TestPutWithHash", tp.TestPutWithHash) t.Run("TestPath", tp.TestDagPath) diff --git a/core/coreapi/interface/tests/dht.go b/core/coreapi/interface/tests/dht.go index 9b77f16792c..3ec77d33bf1 100644 --- a/core/coreapi/interface/tests/dht.go +++ b/core/coreapi/interface/tests/dht.go @@ -5,10 +5,18 @@ import ( "io" "testing" + "github.com/ipfs/go-ipfs/core/coreapi/interface" "github.com/ipfs/go-ipfs/core/coreapi/interface/options" ) func (tp *provider) TestDht(t *testing.T) { + tp.hasApi(t, func(api iface.CoreAPI) error { + if api.Dht() == nil { + return apiNotImplemented + } + return nil + }) + t.Run("TestDhtFindPeer", tp.TestDhtFindPeer) t.Run("TestDhtFindProviders", tp.TestDhtFindProviders) t.Run("TestDhtProvide", tp.TestDhtProvide) diff --git a/core/coreapi/interface/tests/key.go b/core/coreapi/interface/tests/key.go index 99c30c30233..8dd6af57f48 100644 --- a/core/coreapi/interface/tests/key.go +++ b/core/coreapi/interface/tests/key.go @@ -5,10 +5,18 @@ import ( "strings" "testing" + "github.com/ipfs/go-ipfs/core/coreapi/interface" opt "github.com/ipfs/go-ipfs/core/coreapi/interface/options" ) func (tp *provider) TestKey(t *testing.T) { + tp.hasApi(t, func(api iface.CoreAPI) error { + if api.Key() == nil { + return apiNotImplemented + } + return nil + }) + t.Run("TestListSelf", tp.TestListSelf) t.Run("TestRenameSelf", tp.TestRenameSelf) t.Run("TestRemoveSelf", tp.TestRemoveSelf) diff --git a/core/coreapi/interface/tests/name.go b/core/coreapi/interface/tests/name.go index e114b26dea5..639e72c3d99 100644 --- a/core/coreapi/interface/tests/name.go +++ b/core/coreapi/interface/tests/name.go @@ -16,6 +16,13 @@ import ( ) func (tp *provider) TestName(t *testing.T) { + tp.hasApi(t, func(api coreiface.CoreAPI) error { + if api.Name() == nil { + return apiNotImplemented + } + return nil + }) + t.Run("TestPublishResolve", tp.TestPublishResolve) t.Run("TestBasicPublishResolveKey", tp.TestBasicPublishResolveKey) t.Run("TestBasicPublishResolveTimeout", tp.TestBasicPublishResolveTimeout) diff --git a/core/coreapi/interface/tests/object.go b/core/coreapi/interface/tests/object.go index 1cd24aac25c..81d5b4117ce 100644 --- a/core/coreapi/interface/tests/object.go +++ b/core/coreapi/interface/tests/object.go @@ -13,6 +13,13 @@ import ( ) func (tp *provider) TestObject(t *testing.T) { + tp.hasApi(t, func(api iface.CoreAPI) error { + if api.Object() == nil { + return apiNotImplemented + } + return nil + }) + t.Run("TestNew", tp.TestNew) t.Run("TestObjectPut", tp.TestObjectPut) t.Run("TestObjectGet", tp.TestObjectGet) diff --git a/core/coreapi/interface/tests/path.go b/core/coreapi/interface/tests/path.go index 7057d628640..50a1977d50b 100644 --- a/core/coreapi/interface/tests/path.go +++ b/core/coreapi/interface/tests/path.go @@ -26,23 +26,27 @@ func (tp *provider) TestMutablePath(t *testing.T) { t.Fatal(err) } - // get self /ipns path - keys, err := api.Key().List(ctx) + blk, err := api.Block().Put(ctx, strings.NewReader(`foo`)) if err != nil { t.Fatal(err) } - if !keys[0].Path().Mutable() { - t.Error("expected self /ipns path to be mutable") + if blk.Path().Mutable() { + t.Error("expected /ipld path to be immutable") } - blk, err := api.Block().Put(ctx, strings.NewReader(`foo`)) + // get self /ipns path + if api.Key() == nil { + t.Fatal(".Key not implemented") + } + + keys, err := api.Key().List(ctx) if err != nil { - t.Error(err) + t.Fatal(err) } - if blk.Path().Mutable() { - t.Error("expected /ipld path to be immutable") + if !keys[0].Path().Mutable() { + t.Error("expected self /ipns path to be mutable") } } @@ -54,6 +58,10 @@ func (tp *provider) TestPathRemainder(t *testing.T) { t.Fatal(err) } + if api.Dag() == nil { + t.Fatal(".Dag not implemented") + } + obj, err := api.Dag().Put(ctx, strings.NewReader(`{"foo": {"bar": "baz"}}`)) if err != nil { t.Fatal(err) @@ -82,6 +90,10 @@ func (tp *provider) TestEmptyPathRemainder(t *testing.T) { t.Fatal(err) } + if api.Dag() == nil { + t.Fatal(".Dag not implemented") + } + obj, err := api.Dag().Put(ctx, strings.NewReader(`{"foo": {"bar": "baz"}}`)) if err != nil { t.Fatal(err) @@ -114,6 +126,10 @@ func (tp *provider) TestInvalidPathRemainder(t *testing.T) { t.Fatal(err) } + if api.Dag() == nil { + t.Fatal(".Dag not implemented") + } + obj, err := api.Dag().Put(ctx, strings.NewReader(`{"foo": {"bar": "baz"}}`)) if err != nil { t.Fatal(err) @@ -138,9 +154,17 @@ func (tp *provider) TestPathRoot(t *testing.T) { t.Fatal(err) } + if api.Block() == nil { + t.Fatal(".Block not implemented") + } + blk, err := api.Block().Put(ctx, strings.NewReader(`foo`), options.Block.Format("raw")) if err != nil { - t.Error(err) + t.Fatal(err) + } + + if api.Dag() == nil { + t.Fatal(".Dag not implemented") } obj, err := api.Dag().Put(ctx, strings.NewReader(`{"foo": {"/": "`+blk.Path().Cid().String()+`"}}`)) diff --git a/core/coreapi/interface/tests/pin.go b/core/coreapi/interface/tests/pin.go index 823281ab144..87ad8a00489 100644 --- a/core/coreapi/interface/tests/pin.go +++ b/core/coreapi/interface/tests/pin.go @@ -2,6 +2,7 @@ package tests import ( "context" + "github.com/ipfs/go-ipfs/core/coreapi/interface" "strings" "testing" @@ -9,6 +10,13 @@ import ( ) func (tp *provider) TestPin(t *testing.T) { + tp.hasApi(t, func(api iface.CoreAPI) error { + if api.Pin() == nil { + return apiNotImplemented + } + return nil + }) + t.Run("TestPinAdd", tp.TestPinAdd) t.Run("TestPinSimple", tp.TestPinSimple) t.Run("TestPinRecursive", tp.TestPinRecursive) diff --git a/core/coreapi/interface/tests/pubsub.go b/core/coreapi/interface/tests/pubsub.go index 3462b47551d..b993f51dc55 100644 --- a/core/coreapi/interface/tests/pubsub.go +++ b/core/coreapi/interface/tests/pubsub.go @@ -2,12 +2,20 @@ package tests import ( "context" + "github.com/ipfs/go-ipfs/core/coreapi/interface" "github.com/ipfs/go-ipfs/core/coreapi/interface/options" "testing" "time" ) func (tp *provider) TestPubSub(t *testing.T) { + tp.hasApi(t, func(api iface.CoreAPI) error { + if api.PubSub() == nil { + return apiNotImplemented + } + return nil + }) + t.Run("TestBasicPubSub", tp.TestBasicPubSub) } diff --git a/core/coreapi/interface/tests/unixfs.go b/core/coreapi/interface/tests/unixfs.go index f411ad24da5..ddb3145f7a0 100644 --- a/core/coreapi/interface/tests/unixfs.go +++ b/core/coreapi/interface/tests/unixfs.go @@ -24,6 +24,13 @@ import ( ) func (tp *provider) TestUnixfs(t *testing.T) { + tp.hasApi(t, func(api coreiface.CoreAPI) error { + if api.Unixfs() == nil { + return apiNotImplemented + } + return nil + }) + t.Run("TestAdd", tp.TestAdd) t.Run("TestAddPinned", tp.TestAddPinned) t.Run("TestAddHashOnly", tp.TestAddHashOnly)