Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move coreapi tests to the interface #5865

Merged
merged 6 commits into from
Jan 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions core/coreapi/dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ type dagBatch struct {
// Returns the path of the inserted data.
func (api *DagAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.DagPutOption) (coreiface.ResolvedPath, error) {
nd, err := getNode(src, opts...)
if err != nil {
return nil, err
}

err = api.dag.Add(ctx, nd)
if err != nil {
Expand Down
109 changes: 0 additions & 109 deletions core/coreapi/dht_test.go

This file was deleted.

78 changes: 78 additions & 0 deletions core/coreapi/interface/tests/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package tests

import (
"context"
"testing"
"time"

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

func (tp *provider) makeAPI(ctx context.Context) (coreiface.CoreAPI, error) {
api, err := tp.MakeAPISwarm(ctx, false, 1)
if err != nil {
return nil, err
}

return api[0], nil
}

type Provider interface {
// Make creates n nodes. fullIdentity set to false can be ignored
MakeAPISwarm(ctx context.Context, fullIdentity bool, n int) ([]coreiface.CoreAPI, error)
}

func (tp *provider) MakeAPISwarm(ctx context.Context, fullIdentity bool, n int) ([]coreiface.CoreAPI, error) {
tp.apis <- 1
go func() {
<-ctx.Done()
tp.apis <- -1
}()

return tp.Provider.MakeAPISwarm(ctx, fullIdentity, n)
}

type provider struct {
Provider

apis chan int
}

func TestApi(p Provider) func(t *testing.T) {
running := 1
apis := make(chan int)
zeroRunning := make(chan struct{})
go func() {
for i := range apis {
running += i
if running < 1 {
close(zeroRunning)
return
}
}
}()

tp := &provider{Provider: p, apis: apis}

return func(t *testing.T) {
t.Run("Block", tp.TestBlock)
t.Run("Dag", tp.TestDag)
t.Run("Dht", tp.TestDht)
t.Run("Key", tp.TestKey)
t.Run("Name", tp.TestName)
t.Run("Object", tp.TestObject)
t.Run("Path", tp.TestPath)
t.Run("Pin", tp.TestPin)
t.Run("PubSub", tp.TestPubSub)
t.Run("Unixfs", tp.TestUnixfs)

apis <- -1
t.Run("TestsCancelCtx", func(t *testing.T) {
select {
case <-zeroRunning:
case <-time.After(time.Second):
t.Errorf("%d test swarms(s) not closed", running)
}
})
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package coreapi_test
package tests

import (
"context"
Expand All @@ -12,43 +12,55 @@ import (
mh "gx/ipfs/QmerPMzPk1mJVowm8KgmoknWa4yCYvvugMPsgWmDNUvDLW/go-multihash"
)

func TestBlockPut(t *testing.T) {
ctx := context.Background()
_, api, err := makeAPI(ctx)
func (tp *provider) TestBlock(t *testing.T) {
t.Run("TestBlockPut", tp.TestBlockPut)
t.Run("TestBlockPutFormat", tp.TestBlockPutFormat)
t.Run("TestBlockPutHash", tp.TestBlockPutHash)
t.Run("TestBlockGet", tp.TestBlockGet)
t.Run("TestBlockRm", tp.TestBlockRm)
t.Run("TestBlockStat", tp.TestBlockStat)
}

func (tp *provider) TestBlockPut(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
api, err := tp.makeAPI(ctx)
if err != nil {
t.Error(err)
t.Fatal(err)
}

res, err := api.Block().Put(ctx, strings.NewReader(`Hello`))
if err != nil {
t.Error(err)
t.Fatal(err)
}

if res.Path().Cid().String() != "QmPyo15ynbVrSTVdJL9th7JysHaAbXt9dM9tXk1bMHbRtk" {
t.Errorf("got wrong cid: %s", res.Path().Cid().String())
}
}

func TestBlockPutFormat(t *testing.T) {
ctx := context.Background()
_, api, err := makeAPI(ctx)
func (tp *provider) TestBlockPutFormat(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
api, err := tp.makeAPI(ctx)
if err != nil {
t.Error(err)
}

res, err := api.Block().Put(ctx, strings.NewReader(`Hello`), opt.Block.Format("cbor"))
if err != nil {
t.Error(err)
t.Fatal(err)
}

if res.Path().Cid().String() != "zdpuAn4amuLWo8Widi5v6VQpuo2dnpnwbVE3oB6qqs7mDSeoa" {
t.Errorf("got wrong cid: %s", res.Path().Cid().String())
}
}

func TestBlockPutHash(t *testing.T) {
ctx := context.Background()
_, api, err := makeAPI(ctx)
func (tp *provider) TestBlockPutHash(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
api, err := tp.makeAPI(ctx)
if err != nil {
t.Error(err)
}
Expand All @@ -63,16 +75,17 @@ func TestBlockPutHash(t *testing.T) {
}
}

func TestBlockGet(t *testing.T) {
ctx := context.Background()
_, api, err := makeAPI(ctx)
func (tp *provider) TestBlockGet(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
api, err := tp.makeAPI(ctx)
if err != nil {
t.Error(err)
}

res, err := api.Block().Put(ctx, strings.NewReader(`Hello`), opt.Block.Hash(mh.KECCAK_512, -1))
if err != nil {
t.Error(err)
t.Fatal(err)
}

r, err := api.Block().Get(ctx, res.Path())
Expand Down Expand Up @@ -103,16 +116,17 @@ func TestBlockGet(t *testing.T) {
}
}

func TestBlockRm(t *testing.T) {
ctx := context.Background()
_, api, err := makeAPI(ctx)
func (tp *provider) TestBlockRm(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
api, err := tp.makeAPI(ctx)
if err != nil {
t.Error(err)
}

res, err := api.Block().Put(ctx, strings.NewReader(`Hello`))
if err != nil {
t.Error(err)
t.Fatal(err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the pattern for the replacement of Error with Fatal? Are we testing the first use of the CoreAPI generally?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I understand - it's an existing inconsistency and you didn't want to change them wholesale.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH I just started changing them here as I was working on running tests in go-ipfs-http-api and this got committed. I'll commit more of those fixes in another PR.

}

r, err := api.Block().Get(ctx, res.Path())
Expand Down Expand Up @@ -156,16 +170,17 @@ func TestBlockRm(t *testing.T) {
}
}

func TestBlockStat(t *testing.T) {
ctx := context.Background()
_, api, err := makeAPI(ctx)
func (tp *provider) TestBlockStat(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
api, err := tp.makeAPI(ctx)
if err != nil {
t.Error(err)
}

res, err := api.Block().Put(ctx, strings.NewReader(`Hello`))
if err != nil {
t.Error(err)
t.Fatal(err)
}

stat, err := api.Block().Stat(ctx, res.Path())
Expand Down
Loading