Skip to content

Commit

Permalink
Merge pull request #5865 from ipfs/feat/coreapi-tests
Browse files Browse the repository at this point in the history
Move coreapi tests to the interface
  • Loading branch information
Stebalien authored Jan 4, 2019
2 parents 776bead + 44fc750 commit 821c36c
Show file tree
Hide file tree
Showing 14 changed files with 796 additions and 518 deletions.
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)
}

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

0 comments on commit 821c36c

Please sign in to comment.