Skip to content

Commit

Permalink
CoreAPI: Don't panic when testing incomplete implementions
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
  • Loading branch information
magik6k authored and Stebalien committed Jan 8, 2019
1 parent 42a15ba commit 81a2ffc
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 9 deletions.
16 changes: 16 additions & 0 deletions core/coreapi/interface/tests/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}
7 changes: 7 additions & 0 deletions core/coreapi/interface/tests/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions core/coreapi/interface/tests/dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions core/coreapi/interface/tests/dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions core/coreapi/interface/tests/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions core/coreapi/interface/tests/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions core/coreapi/interface/tests/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
42 changes: 33 additions & 9 deletions core/coreapi/interface/tests/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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()+`"}}`))
Expand Down
8 changes: 8 additions & 0 deletions core/coreapi/interface/tests/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@ package tests

import (
"context"
"github.com/ipfs/go-ipfs/core/coreapi/interface"
"strings"
"testing"

opt "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
)

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)
Expand Down
8 changes: 8 additions & 0 deletions core/coreapi/interface/tests/pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
7 changes: 7 additions & 0 deletions core/coreapi/interface/tests/unixfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 81a2ffc

Please sign in to comment.