diff --git a/core/coreapi/dag.go b/core/coreapi/dag.go index 998e1888d23..ddaf6c7849d 100644 --- a/core/coreapi/dag.go +++ b/core/coreapi/dag.go @@ -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 { diff --git a/core/coreapi/dht_test.go b/core/coreapi/dht_test.go deleted file mode 100644 index a213f7a44da..00000000000 --- a/core/coreapi/dht_test.go +++ /dev/null @@ -1,109 +0,0 @@ -package coreapi_test - -import ( - "context" - "io" - "io/ioutil" - "testing" - - "github.com/ipfs/go-ipfs/core/coreapi/interface" - "github.com/ipfs/go-ipfs/core/coreapi/interface/options" - - blocks "gx/ipfs/QmWoXtvgC8inqFkAATB7cp2Dax7XBi9VDvSg9RCCZufmRk/go-block-format" - peer "gx/ipfs/QmY5Grm8pJdiSSVsYxx4uNRgweY72EmYwuSDbRnbFok3iY/go-libp2p-peer" -) - -func TestDhtFindPeer(t *testing.T) { - ctx := context.Background() - nds, apis, err := makeAPISwarm(ctx, true, 5) - if err != nil { - t.Fatal(err) - } - - pi, err := apis[2].Dht().FindPeer(ctx, peer.ID(nds[0].Identity)) - if err != nil { - t.Fatal(err) - } - - if pi.Addrs[0].String() != "/ip4/127.0.0.1/tcp/4001" { - t.Errorf("got unexpected address from FindPeer: %s", pi.Addrs[0].String()) - } - - pi, err = apis[1].Dht().FindPeer(ctx, peer.ID(nds[2].Identity)) - if err != nil { - t.Fatal(err) - } - - if pi.Addrs[0].String() != "/ip4/127.0.2.1/tcp/4001" { - t.Errorf("got unexpected address from FindPeer: %s", pi.Addrs[0].String()) - } -} - -func TestDhtFindProviders(t *testing.T) { - ctx := context.Background() - nds, apis, err := makeAPISwarm(ctx, true, 5) - if err != nil { - t.Fatal(err) - } - - p, err := addTestObject(ctx, apis[0]) - if err != nil { - t.Fatal(err) - } - - out, err := apis[2].Dht().FindProviders(ctx, p, options.Dht.NumProviders(1)) - if err != nil { - t.Fatal(err) - } - - provider := <-out - - if provider.ID.String() != nds[0].Identity.String() { - t.Errorf("got wrong provider: %s != %s", provider.ID.String(), nds[0].Identity.String()) - } -} - -func TestDhtProvide(t *testing.T) { - ctx := context.Background() - nds, apis, err := makeAPISwarm(ctx, true, 5) - if err != nil { - t.Fatal(err) - } - - // TODO: replace once there is local add on unixfs or somewhere - data, err := ioutil.ReadAll(&io.LimitedReader{R: rnd, N: 4092}) - if err != nil { - t.Fatal(err) - } - - b := blocks.NewBlock(data) - nds[0].Blockstore.Put(b) - p := iface.IpfsPath(b.Cid()) - - out, err := apis[2].Dht().FindProviders(ctx, p, options.Dht.NumProviders(1)) - if err != nil { - t.Fatal(err) - } - - provider := <-out - - if provider.ID.String() != "" { - t.Errorf("got wrong provider: %s != %s", provider.ID.String(), nds[0].Identity.String()) - } - - err = apis[0].Dht().Provide(ctx, p) - if err != nil { - t.Fatal(err) - } - - out, err = apis[2].Dht().FindProviders(ctx, p, options.Dht.NumProviders(1)) - if err != nil { - t.Fatal(err) - } - - provider = <-out - - if provider.ID.String() != nds[0].Identity.String() { - t.Errorf("got wrong provider: %s != %s", provider.ID.String(), nds[0].Identity.String()) - } -} diff --git a/core/coreapi/interface/tests/api.go b/core/coreapi/interface/tests/api.go new file mode 100644 index 00000000000..23ec0612bcc --- /dev/null +++ b/core/coreapi/interface/tests/api.go @@ -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) + } + }) + } +} diff --git a/core/coreapi/block_test.go b/core/coreapi/interface/tests/block.go similarity index 69% rename from core/coreapi/block_test.go rename to core/coreapi/interface/tests/block.go index 45f8415b9b2..d1117cc50c2 100644 --- a/core/coreapi/block_test.go +++ b/core/coreapi/interface/tests/block.go @@ -1,4 +1,4 @@ -package coreapi_test +package tests import ( "context" @@ -12,16 +12,26 @@ 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" { @@ -29,16 +39,17 @@ func TestBlockPut(t *testing.T) { } } -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" { @@ -46,9 +57,10 @@ func TestBlockPutFormat(t *testing.T) { } } -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) } @@ -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()) @@ -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()) @@ -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()) diff --git a/core/coreapi/dag_test.go b/core/coreapi/interface/tests/dag.go similarity index 69% rename from core/coreapi/dag_test.go rename to core/coreapi/interface/tests/dag.go index 8a198e272c1..d5026394347 100644 --- a/core/coreapi/dag_test.go +++ b/core/coreapi/interface/tests/dag.go @@ -1,4 +1,4 @@ -package coreapi_test +package tests import ( "context" @@ -12,6 +12,14 @@ import ( mh "gx/ipfs/QmerPMzPk1mJVowm8KgmoknWa4yCYvvugMPsgWmDNUvDLW/go-multihash" ) +func (tp *provider) TestDag(t *testing.T) { + t.Run("TestPut", tp.TestPut) + t.Run("TestPutWithHash", tp.TestPutWithHash) + t.Run("TestPath", tp.TestDagPath) + t.Run("TestTree", tp.TestTree) + t.Run("TestBatch", tp.TestBatch) +} + var ( treeExpected = map[string]struct{}{ "a": {}, @@ -22,16 +30,17 @@ var ( } ) -func TestPut(t *testing.T) { - ctx := context.Background() - _, api, err := makeAPI(ctx) +func (tp *provider) TestPut(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.Dag().Put(ctx, strings.NewReader(`"Hello"`)) if err != nil { - t.Error(err) + t.Fatal(err) } if res.Cid().String() != "zdpuAqckYF3ToF3gcJNxPZXmnmGuXd3gxHCXhq81HGxBejEvv" { @@ -39,16 +48,17 @@ func TestPut(t *testing.T) { } } -func TestPutWithHash(t *testing.T) { - ctx := context.Background() - _, api, err := makeAPI(ctx) +func (tp *provider) TestPutWithHash(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.Dag().Put(ctx, strings.NewReader(`"Hello"`), opt.Dag.Hash(mh.ID, -1)) if err != nil { - t.Error(err) + t.Fatal(err) } if res.Cid().String() != "z5hRLNd2sv4z1c" { @@ -56,21 +66,22 @@ func TestPutWithHash(t *testing.T) { } } -func TestPath(t *testing.T) { - ctx := context.Background() - _, api, err := makeAPI(ctx) +func (tp *provider) TestDagPath(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) if err != nil { t.Error(err) } sub, err := api.Dag().Put(ctx, strings.NewReader(`"foo"`)) if err != nil { - t.Error(err) + t.Fatal(err) } res, err := api.Dag().Put(ctx, strings.NewReader(`{"lnk": {"/": "`+sub.Cid().String()+`"}}`)) if err != nil { - t.Error(err) + t.Fatal(err) } p, err := coreiface.ParsePath(path.Join(res.Cid().String(), "lnk")) @@ -88,16 +99,17 @@ func TestPath(t *testing.T) { } } -func TestTree(t *testing.T) { - ctx := context.Background() - _, api, err := makeAPI(ctx) +func (tp *provider) TestTree(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) if err != nil { t.Error(err) } c, err := api.Dag().Put(ctx, strings.NewReader(`{"a": 123, "b": "foo", "c": {"d": 321, "e": 111}}`)) if err != nil { - t.Error(err) + t.Fatal(err) } res, err := api.Dag().Get(ctx, c) @@ -117,9 +129,10 @@ func TestTree(t *testing.T) { } } -func TestBatch(t *testing.T) { - ctx := context.Background() - _, api, err := makeAPI(ctx) +func (tp *provider) TestBatch(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) if err != nil { t.Error(err) } @@ -128,7 +141,7 @@ func TestBatch(t *testing.T) { c, err := batch.Put(ctx, strings.NewReader(`"Hello"`)) if err != nil { - t.Error(err) + t.Fatal(err) } if c.Cid().String() != "zdpuAqckYF3ToF3gcJNxPZXmnmGuXd3gxHCXhq81HGxBejEvv" { diff --git a/core/coreapi/interface/tests/dht.go b/core/coreapi/interface/tests/dht.go new file mode 100644 index 00000000000..9b77f16792c --- /dev/null +++ b/core/coreapi/interface/tests/dht.go @@ -0,0 +1,135 @@ +package tests + +import ( + "context" + "io" + "testing" + + "github.com/ipfs/go-ipfs/core/coreapi/interface/options" +) + +func (tp *provider) TestDht(t *testing.T) { + t.Run("TestDhtFindPeer", tp.TestDhtFindPeer) + t.Run("TestDhtFindProviders", tp.TestDhtFindProviders) + t.Run("TestDhtProvide", tp.TestDhtProvide) +} + +func (tp *provider) TestDhtFindPeer(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + apis, err := tp.MakeAPISwarm(ctx, true, 5) + if err != nil { + t.Fatal(err) + } + + self0, err := apis[0].Key().Self(ctx) + if err != nil { + t.Fatal(err) + } + + pi, err := apis[2].Dht().FindPeer(ctx, self0.ID()) + if err != nil { + t.Fatal(err) + } + + if pi.Addrs[0].String() != "/ip4/127.0.0.1/tcp/4001" { + t.Errorf("got unexpected address from FindPeer: %s", pi.Addrs[0].String()) + } + + self2, err := apis[2].Key().Self(ctx) + if err != nil { + t.Fatal(err) + } + + pi, err = apis[1].Dht().FindPeer(ctx, self2.ID()) + if err != nil { + t.Fatal(err) + } + + if pi.Addrs[0].String() != "/ip4/127.0.2.1/tcp/4001" { + t.Errorf("got unexpected address from FindPeer: %s", pi.Addrs[0].String()) + } +} + +func (tp *provider) TestDhtFindProviders(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + apis, err := tp.MakeAPISwarm(ctx, true, 5) + if err != nil { + t.Fatal(err) + } + + p, err := addTestObject(ctx, apis[0]) + if err != nil { + t.Fatal(err) + } + + out, err := apis[2].Dht().FindProviders(ctx, p, options.Dht.NumProviders(1)) + if err != nil { + t.Fatal(err) + } + + provider := <-out + + self0, err := apis[0].Key().Self(ctx) + if err != nil { + t.Fatal(err) + } + + if provider.ID.String() != self0.ID().String() { + t.Errorf("got wrong provider: %s != %s", provider.ID.String(), self0.ID().String()) + } +} + +func (tp *provider) TestDhtProvide(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + apis, err := tp.MakeAPISwarm(ctx, true, 5) + if err != nil { + t.Fatal(err) + } + + off0, err := apis[0].WithOptions(options.Api.Offline(true)) + if err != nil { + t.Fatal(err) + } + + s, err := off0.Block().Put(ctx, &io.LimitedReader{R: rnd, N: 4092}) + if err != nil { + t.Fatal(err) + } + + p := s.Path() + + out, err := apis[2].Dht().FindProviders(ctx, p, options.Dht.NumProviders(1)) + if err != nil { + t.Fatal(err) + } + + provider := <-out + + self0, err := apis[0].Key().Self(ctx) + if err != nil { + t.Fatal(err) + } + + if provider.ID.String() != "" { + t.Errorf("got wrong provider: %s != %s", provider.ID.String(), self0.ID().String()) + } + + err = apis[0].Dht().Provide(ctx, p) + if err != nil { + t.Fatal(err) + } + + out, err = apis[2].Dht().FindProviders(ctx, p, options.Dht.NumProviders(1)) + if err != nil { + t.Fatal(err) + } + + provider = <-out + + if provider.ID.String() != self0.ID().String() { + t.Errorf("got wrong provider: %s != %s", provider.ID.String(), self0.ID().String()) + } +} diff --git a/core/coreapi/key_test.go b/core/coreapi/interface/tests/key.go similarity index 70% rename from core/coreapi/key_test.go rename to core/coreapi/interface/tests/key.go index ce906c8d191..99c30c30233 100644 --- a/core/coreapi/key_test.go +++ b/core/coreapi/interface/tests/key.go @@ -1,4 +1,4 @@ -package coreapi_test +package tests import ( "context" @@ -8,14 +8,39 @@ import ( opt "github.com/ipfs/go-ipfs/core/coreapi/interface/options" ) -func TestListSelf(t *testing.T) { - ctx := context.Background() - _, api, err := makeAPI(ctx) +func (tp *provider) TestKey(t *testing.T) { + t.Run("TestListSelf", tp.TestListSelf) + t.Run("TestRenameSelf", tp.TestRenameSelf) + t.Run("TestRemoveSelf", tp.TestRemoveSelf) + t.Run("TestGenerate", tp.TestGenerate) + t.Run("TestGenerateSize", tp.TestGenerateSize) + t.Run("TestGenerateType", tp.TestGenerateType) + t.Run("TestGenerateExisting", tp.TestGenerateExisting) + t.Run("TestList", tp.TestList) + t.Run("TestRename", tp.TestRename) + t.Run("TestRenameToSelf", tp.TestRenameToSelf) + t.Run("TestRenameToSelfForce", tp.TestRenameToSelfForce) + t.Run("TestRenameOverwriteNoForce", tp.TestRenameOverwriteNoForce) + t.Run("TestRenameOverwrite", tp.TestRenameOverwrite) + t.Run("TestRenameSameNameNoForce", tp.TestRenameSameNameNoForce) + t.Run("TestRenameSameName", tp.TestRenameSameName) + t.Run("TestRemove", tp.TestRemove) +} + +func (tp *provider) TestListSelf(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) if err != nil { t.Fatal(err) return } + self, err := api.Key().Self(ctx) + if err != nil { + t.Fatal(err) + } + keys, err := api.Key().List(ctx) if err != nil { t.Fatalf("failed to list keys: %s", err) @@ -31,14 +56,15 @@ func TestListSelf(t *testing.T) { t.Errorf("expected the key to be called 'self', got '%s'", keys[0].Name()) } - if keys[0].Path().String() != "/ipns/"+testPeerID { - t.Errorf("expected the key to have path '/ipns/%s', got '%s'", testPeerID, keys[0].Path().String()) + if keys[0].Path().String() != "/ipns/"+self.ID().Pretty() { + t.Errorf("expected the key to have path '/ipns/%s', got '%s'", self.ID().Pretty(), keys[0].Path().String()) } } -func TestRenameSelf(t *testing.T) { - ctx := context.Background() - _, api, err := makeAPI(ctx) +func (tp *provider) TestRenameSelf(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) if err != nil { t.Fatal(err) return @@ -63,9 +89,10 @@ func TestRenameSelf(t *testing.T) { } } -func TestRemoveSelf(t *testing.T) { - ctx := context.Background() - _, api, err := makeAPI(ctx) +func (tp *provider) TestRemoveSelf(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) if err != nil { t.Fatal(err) return @@ -81,9 +108,10 @@ func TestRemoveSelf(t *testing.T) { } } -func TestGenerate(t *testing.T) { - ctx := context.Background() - _, api, err := makeAPI(ctx) +func (tp *provider) TestGenerate(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) if err != nil { t.Error(err) } @@ -103,9 +131,10 @@ func TestGenerate(t *testing.T) { } } -func TestGenerateSize(t *testing.T) { - ctx := context.Background() - _, api, err := makeAPI(ctx) +func (tp *provider) TestGenerateSize(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) if err != nil { t.Error(err) } @@ -125,11 +154,12 @@ func TestGenerateSize(t *testing.T) { } } -func TestGenerateType(t *testing.T) { - ctx := context.Background() +func (tp *provider) TestGenerateType(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() t.Skip("disabled until libp2p/specs#111 is fixed") - _, api, err := makeAPI(ctx) + api, err := tp.makeAPI(ctx) if err != nil { t.Error(err) } @@ -150,9 +180,10 @@ func TestGenerateType(t *testing.T) { } } -func TestGenerateExisting(t *testing.T) { - ctx := context.Background() - _, api, err := makeAPI(ctx) +func (tp *provider) TestGenerateExisting(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) if err != nil { t.Error(err) } @@ -182,9 +213,10 @@ func TestGenerateExisting(t *testing.T) { } } -func TestList(t *testing.T) { - ctx := context.Background() - _, api, err := makeAPI(ctx) +func (tp *provider) TestList(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) if err != nil { t.Error(err) } @@ -227,9 +259,10 @@ func TestList(t *testing.T) { } } -func TestRename(t *testing.T) { - ctx := context.Background() - _, api, err := makeAPI(ctx) +func (tp *provider) TestRename(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) if err != nil { t.Error(err) } @@ -255,9 +288,10 @@ func TestRename(t *testing.T) { } } -func TestRenameToSelf(t *testing.T) { - ctx := context.Background() - _, api, err := makeAPI(ctx) +func (tp *provider) TestRenameToSelf(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) if err != nil { t.Error(err) } @@ -278,9 +312,10 @@ func TestRenameToSelf(t *testing.T) { } } -func TestRenameToSelfForce(t *testing.T) { - ctx := context.Background() - _, api, err := makeAPI(ctx) +func (tp *provider) TestRenameToSelfForce(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) if err != nil { t.Error(err) } @@ -301,9 +336,10 @@ func TestRenameToSelfForce(t *testing.T) { } } -func TestRenameOverwriteNoForce(t *testing.T) { - ctx := context.Background() - _, api, err := makeAPI(ctx) +func (tp *provider) TestRenameOverwriteNoForce(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) if err != nil { t.Error(err) } @@ -330,9 +366,10 @@ func TestRenameOverwriteNoForce(t *testing.T) { } } -func TestRenameOverwrite(t *testing.T) { - ctx := context.Background() - _, api, err := makeAPI(ctx) +func (tp *provider) TestRenameOverwrite(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) if err != nil { t.Error(err) } @@ -368,9 +405,10 @@ func TestRenameOverwrite(t *testing.T) { } } -func TestRenameSameNameNoForce(t *testing.T) { - ctx := context.Background() - _, api, err := makeAPI(ctx) +func (tp *provider) TestRenameSameNameNoForce(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) if err != nil { t.Error(err) } @@ -396,9 +434,10 @@ func TestRenameSameNameNoForce(t *testing.T) { } } -func TestRenameSameName(t *testing.T) { - ctx := context.Background() - _, api, err := makeAPI(ctx) +func (tp *provider) TestRenameSameName(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) if err != nil { t.Error(err) } @@ -424,9 +463,10 @@ func TestRenameSameName(t *testing.T) { } } -func TestRemove(t *testing.T) { - ctx := context.Background() - _, api, err := makeAPI(ctx) +func (tp *provider) TestRemove(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) if err != nil { t.Error(err) } diff --git a/core/coreapi/name_test.go b/core/coreapi/interface/tests/name.go similarity index 68% rename from core/coreapi/name_test.go rename to core/coreapi/interface/tests/name.go index c93d67f6f38..e114b26dea5 100644 --- a/core/coreapi/name_test.go +++ b/core/coreapi/interface/tests/name.go @@ -1,21 +1,26 @@ -package coreapi_test +package tests import ( "context" - "github.com/ipfs/go-ipfs/core" "io" "math/rand" "path" "testing" "time" - files "gx/ipfs/QmXWZCd8jfaHmt4UDSnjKmGcrQMw95bDGWqEeVLVJjoANX/go-ipfs-files" + "gx/ipfs/QmXWZCd8jfaHmt4UDSnjKmGcrQMw95bDGWqEeVLVJjoANX/go-ipfs-files" ipath "gx/ipfs/QmZErC2Ay6WuGi96CPg316PwitdwgLo6RxZRqVjJjRj2MR/go-path" coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface" opt "github.com/ipfs/go-ipfs/core/coreapi/interface/options" ) +func (tp *provider) TestName(t *testing.T) { + t.Run("TestPublishResolve", tp.TestPublishResolve) + t.Run("TestBasicPublishResolveKey", tp.TestBasicPublishResolveKey) + t.Run("TestBasicPublishResolveTimeout", tp.TestBasicPublishResolveTimeout) +} + var rnd = rand.New(rand.NewSource(0x62796532303137)) func addTestObject(ctx context.Context, api coreiface.CoreAPI) (coreiface.Path, error) { @@ -30,36 +35,40 @@ func appendPath(p coreiface.Path, sub string) coreiface.Path { return p } -func TestPublishResolve(t *testing.T) { - ctx := context.Background() - init := func() (*core.IpfsNode, coreiface.CoreAPI, coreiface.Path) { - nds, apis, err := makeAPISwarm(ctx, true, 5) +func (tp *provider) TestPublishResolve(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + init := func() (coreiface.CoreAPI, coreiface.Path) { + apis, err := tp.MakeAPISwarm(ctx, true, 5) if err != nil { t.Fatal(err) - return nil, nil, nil + return nil, nil } - n := nds[0] api := apis[0] p, err := addTestObject(ctx, api) if err != nil { t.Fatal(err) - return nil, nil, nil + return nil, nil } - return n, api, p + return api, p } run := func(t *testing.T, ropts []opt.NameResolveOption) { t.Run("basic", func(t *testing.T) { - n, api, p := init() + api, p := init() e, err := api.Name().Publish(ctx, p) if err != nil { t.Fatal(err) - return } - if e.Name() != n.Identity.Pretty() { - t.Errorf("expected e.Name to equal '%s', got '%s'", n.Identity.Pretty(), e.Name()) + self, err := api.Key().Self(ctx) + if err != nil { + t.Fatal(err) + } + + if e.Name() != self.ID().Pretty() { + t.Errorf("expected e.Name to equal '%s', got '%s'", self.ID().Pretty(), e.Name()) } if e.Value().String() != p.String() { @@ -69,7 +78,6 @@ func TestPublishResolve(t *testing.T) { resPath, err := api.Name().Resolve(ctx, e.Name(), ropts...) if err != nil { t.Fatal(err) - return } if resPath.String() != p.String() { @@ -78,15 +86,19 @@ func TestPublishResolve(t *testing.T) { }) t.Run("publishPath", func(t *testing.T) { - n, api, p := init() + api, p := init() e, err := api.Name().Publish(ctx, appendPath(p, "/test")) if err != nil { t.Fatal(err) - return } - if e.Name() != n.Identity.Pretty() { - t.Errorf("expected e.Name to equal '%s', got '%s'", n.Identity.Pretty(), e.Name()) + self, err := api.Key().Self(ctx) + if err != nil { + t.Fatal(err) + } + + if e.Name() != self.ID().Pretty() { + t.Errorf("expected e.Name to equal '%s', got '%s'", self.ID().Pretty(), e.Name()) } if e.Value().String() != p.String()+"/test" { @@ -96,7 +108,6 @@ func TestPublishResolve(t *testing.T) { resPath, err := api.Name().Resolve(ctx, e.Name(), ropts...) if err != nil { t.Fatal(err) - return } if resPath.String() != p.String()+"/test" { @@ -105,15 +116,19 @@ func TestPublishResolve(t *testing.T) { }) t.Run("revolvePath", func(t *testing.T) { - n, api, p := init() + api, p := init() e, err := api.Name().Publish(ctx, p) if err != nil { t.Fatal(err) - return } - if e.Name() != n.Identity.Pretty() { - t.Errorf("expected e.Name to equal '%s', got '%s'", n.Identity.Pretty(), e.Name()) + self, err := api.Key().Self(ctx) + if err != nil { + t.Fatal(err) + } + + if e.Name() != self.ID().Pretty() { + t.Errorf("expected e.Name to equal '%s', got '%s'", self.ID().Pretty(), e.Name()) } if e.Value().String() != p.String() { @@ -123,7 +138,6 @@ func TestPublishResolve(t *testing.T) { resPath, err := api.Name().Resolve(ctx, e.Name()+"/test", ropts...) if err != nil { t.Fatal(err) - return } if resPath.String() != p.String()+"/test" { @@ -132,15 +146,19 @@ func TestPublishResolve(t *testing.T) { }) t.Run("publishRevolvePath", func(t *testing.T) { - n, api, p := init() + api, p := init() e, err := api.Name().Publish(ctx, appendPath(p, "/a")) if err != nil { t.Fatal(err) - return } - if e.Name() != n.Identity.Pretty() { - t.Errorf("expected e.Name to equal '%s', got '%s'", n.Identity.Pretty(), e.Name()) + self, err := api.Key().Self(ctx) + if err != nil { + t.Fatal(err) + } + + if e.Name() != self.ID().Pretty() { + t.Errorf("expected e.Name to equal '%s', got '%s'", self.ID().Pretty(), e.Name()) } if e.Value().String() != p.String()+"/a" { @@ -150,7 +168,6 @@ func TestPublishResolve(t *testing.T) { resPath, err := api.Name().Resolve(ctx, e.Name()+"/b", ropts...) if err != nil { t.Fatal(err) - return } if resPath.String() != p.String()+"/a/b" { @@ -168,31 +185,28 @@ func TestPublishResolve(t *testing.T) { }) } -func TestBasicPublishResolveKey(t *testing.T) { - ctx := context.Background() - _, apis, err := makeAPISwarm(ctx, true, 5) +func (tp *provider) TestBasicPublishResolveKey(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + apis, err := tp.MakeAPISwarm(ctx, true, 5) if err != nil { t.Fatal(err) - return } api := apis[0] k, err := api.Key().Generate(ctx, "foo") if err != nil { t.Fatal(err) - return } p, err := addTestObject(ctx, api) if err != nil { t.Fatal(err) - return } e, err := api.Name().Publish(ctx, p, opt.Name.Key(k.Name())) if err != nil { t.Fatal(err) - return } if ipath.Join([]string{"/ipns", e.Name()}) != k.Path().String() { @@ -206,7 +220,6 @@ func TestBasicPublishResolveKey(t *testing.T) { resPath, err := api.Name().Resolve(ctx, e.Name()) if err != nil { t.Fatal(err) - return } if resPath.String() != p.String() { @@ -214,31 +227,33 @@ func TestBasicPublishResolveKey(t *testing.T) { } } -func TestBasicPublishResolveTimeout(t *testing.T) { +func (tp *provider) TestBasicPublishResolveTimeout(t *testing.T) { t.Skip("ValidTime doesn't appear to work at this time resolution") - ctx := context.Background() - nds, apis, err := makeAPISwarm(ctx, true, 5) + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + apis, err := tp.MakeAPISwarm(ctx, true, 5) if err != nil { t.Fatal(err) - return } - n := nds[0] api := apis[0] p, err := addTestObject(ctx, api) if err != nil { t.Fatal(err) - return } e, err := api.Name().Publish(ctx, p, opt.Name.ValidTime(time.Millisecond*100)) if err != nil { t.Fatal(err) - return } - if e.Name() != n.Identity.Pretty() { - t.Errorf("expected e.Name to equal '%s', got '%s'", n.Identity.Pretty(), e.Name()) + self, err := api.Key().Self(ctx) + if err != nil { + t.Fatal(err) + } + + if e.Name() != self.ID().Pretty() { + t.Errorf("expected e.Name to equal '%s', got '%s'", self.ID().Pretty(), e.Name()) } if e.Value().String() != p.String() { @@ -250,7 +265,6 @@ func TestBasicPublishResolveTimeout(t *testing.T) { _, err = api.Name().Resolve(ctx, e.Name()) if err == nil { t.Fatal("Expected an error") - return } } diff --git a/core/coreapi/object_test.go b/core/coreapi/interface/tests/object.go similarity index 75% rename from core/coreapi/object_test.go rename to core/coreapi/interface/tests/object.go index 8df9c04d253..1cd24aac25c 100644 --- a/core/coreapi/object_test.go +++ b/core/coreapi/interface/tests/object.go @@ -1,4 +1,4 @@ -package coreapi_test +package tests import ( "bytes" @@ -12,9 +12,25 @@ import ( opt "github.com/ipfs/go-ipfs/core/coreapi/interface/options" ) -func TestNew(t *testing.T) { - ctx := context.Background() - _, api, err := makeAPI(ctx) +func (tp *provider) TestObject(t *testing.T) { + t.Run("TestNew", tp.TestNew) + t.Run("TestObjectPut", tp.TestObjectPut) + t.Run("TestObjectGet", tp.TestObjectGet) + t.Run("TestObjectData", tp.TestObjectData) + t.Run("TestObjectLinks", tp.TestObjectLinks) + t.Run("TestObjectStat", tp.TestObjectStat) + t.Run("TestObjectAddLink", tp.TestObjectAddLink) + t.Run("TestObjectAddLinkCreate", tp.TestObjectAddLinkCreate) + t.Run("TestObjectRmLink", tp.TestObjectRmLink) + t.Run("TestObjectAddData", tp.TestObjectAddData) + t.Run("TestObjectSetData", tp.TestObjectSetData) + t.Run("TestDiffTest", tp.TestDiffTest) +} + +func (tp *provider) TestNew(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) if err != nil { t.Fatal(err) } @@ -38,9 +54,10 @@ func TestNew(t *testing.T) { } } -func TestObjectPut(t *testing.T) { - ctx := context.Background() - _, api, err := makeAPI(ctx) +func (tp *provider) TestObjectPut(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) if err != nil { t.Fatal(err) } @@ -78,9 +95,10 @@ func TestObjectPut(t *testing.T) { } } -func TestObjectGet(t *testing.T) { - ctx := context.Background() - _, api, err := makeAPI(ctx) +func (tp *provider) TestObjectGet(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) if err != nil { t.Fatal(err) } @@ -100,9 +118,10 @@ func TestObjectGet(t *testing.T) { } } -func TestObjectData(t *testing.T) { - ctx := context.Background() - _, api, err := makeAPI(ctx) +func (tp *provider) TestObjectData(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) if err != nil { t.Fatal(err) } @@ -127,9 +146,10 @@ func TestObjectData(t *testing.T) { } } -func TestObjectLinks(t *testing.T) { - ctx := context.Background() - _, api, err := makeAPI(ctx) +func (tp *provider) TestObjectLinks(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) if err != nil { t.Fatal(err) } @@ -162,9 +182,10 @@ func TestObjectLinks(t *testing.T) { } } -func TestObjectStat(t *testing.T) { - ctx := context.Background() - _, api, err := makeAPI(ctx) +func (tp *provider) TestObjectStat(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) if err != nil { t.Fatal(err) } @@ -209,9 +230,10 @@ func TestObjectStat(t *testing.T) { } } -func TestObjectAddLink(t *testing.T) { - ctx := context.Background() - _, api, err := makeAPI(ctx) +func (tp *provider) TestObjectAddLink(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) if err != nil { t.Fatal(err) } @@ -249,9 +271,10 @@ func TestObjectAddLink(t *testing.T) { } } -func TestObjectAddLinkCreate(t *testing.T) { - ctx := context.Background() - _, api, err := makeAPI(ctx) +func (tp *provider) TestObjectAddLinkCreate(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) if err != nil { t.Fatal(err) } @@ -297,9 +320,10 @@ func TestObjectAddLinkCreate(t *testing.T) { } } -func TestObjectRmLink(t *testing.T) { - ctx := context.Background() - _, api, err := makeAPI(ctx) +func (tp *provider) TestObjectRmLink(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) if err != nil { t.Fatal(err) } @@ -329,9 +353,10 @@ func TestObjectRmLink(t *testing.T) { } } -func TestObjectAddData(t *testing.T) { - ctx := context.Background() - _, api, err := makeAPI(ctx) +func (tp *provider) TestObjectAddData(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) if err != nil { t.Fatal(err) } @@ -358,9 +383,10 @@ func TestObjectAddData(t *testing.T) { } } -func TestObjectSetData(t *testing.T) { - ctx := context.Background() - _, api, err := makeAPI(ctx) +func (tp *provider) TestObjectSetData(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) if err != nil { t.Fatal(err) } @@ -387,9 +413,10 @@ func TestObjectSetData(t *testing.T) { } } -func TestDiffTest(t *testing.T) { - ctx := context.Background() - _, api, err := makeAPI(ctx) +func (tp *provider) TestDiffTest(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) if err != nil { t.Fatal(err) } diff --git a/core/coreapi/path_test.go b/core/coreapi/interface/tests/path.go similarity index 69% rename from core/coreapi/path_test.go rename to core/coreapi/interface/tests/path.go index e71d94df9d2..e74053c0499 100644 --- a/core/coreapi/path_test.go +++ b/core/coreapi/interface/tests/path.go @@ -1,4 +1,4 @@ -package coreapi_test +package tests import ( "context" @@ -9,9 +9,18 @@ import ( "github.com/ipfs/go-ipfs/core/coreapi/interface/options" ) -func TestMutablePath(t *testing.T) { - ctx := context.Background() - _, api, err := makeAPI(ctx) +func (tp *provider) TestPath(t *testing.T) { + t.Run("TestMutablePath", tp.TestMutablePath) + t.Run("TestPathRemainder", tp.TestPathRemainder) + t.Run("TestEmptyPathRemainder", tp.TestEmptyPathRemainder) + t.Run("TestInvalidPathRemainder", tp.TestInvalidPathRemainder) + t.Run("TestPathRoot", tp.TestPathRoot) +} + +func (tp *provider) TestMutablePath(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) if err != nil { t.Fatal(err) } @@ -36,9 +45,10 @@ func TestMutablePath(t *testing.T) { } } -func TestPathRemainder(t *testing.T) { - ctx := context.Background() - _, api, err := makeAPI(ctx) +func (tp *provider) TestPathRemainder(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) if err != nil { t.Fatal(err) } @@ -63,9 +73,10 @@ func TestPathRemainder(t *testing.T) { } } -func TestEmptyPathRemainder(t *testing.T) { - ctx := context.Background() - _, api, err := makeAPI(ctx) +func (tp *provider) TestEmptyPathRemainder(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) if err != nil { t.Fatal(err) } @@ -94,9 +105,10 @@ func TestEmptyPathRemainder(t *testing.T) { } } -func TestInvalidPathRemainder(t *testing.T) { - ctx := context.Background() - _, api, err := makeAPI(ctx) +func (tp *provider) TestInvalidPathRemainder(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) if err != nil { t.Fatal(err) } @@ -117,9 +129,10 @@ func TestInvalidPathRemainder(t *testing.T) { } } -func TestPathRoot(t *testing.T) { - ctx := context.Background() - _, api, err := makeAPI(ctx) +func (tp *provider) TestPathRoot(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) if err != nil { t.Fatal(err) } diff --git a/core/coreapi/pin_test.go b/core/coreapi/interface/tests/pin.go similarity index 67% rename from core/coreapi/pin_test.go rename to core/coreapi/interface/tests/pin.go index a9a7547c3db..823281ab144 100644 --- a/core/coreapi/pin_test.go +++ b/core/coreapi/interface/tests/pin.go @@ -1,4 +1,4 @@ -package coreapi_test +package tests import ( "context" @@ -8,9 +8,16 @@ import ( opt "github.com/ipfs/go-ipfs/core/coreapi/interface/options" ) -func TestPinAdd(t *testing.T) { - ctx := context.Background() - _, api, err := makeAPI(ctx) +func (tp *provider) TestPin(t *testing.T) { + t.Run("TestPinAdd", tp.TestPinAdd) + t.Run("TestPinSimple", tp.TestPinSimple) + t.Run("TestPinRecursive", tp.TestPinRecursive) +} + +func (tp *provider) TestPinAdd(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) if err != nil { t.Error(err) } @@ -26,9 +33,10 @@ func TestPinAdd(t *testing.T) { } } -func TestPinSimple(t *testing.T) { - ctx := context.Background() - _, api, err := makeAPI(ctx) +func (tp *provider) TestPinSimple(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) if err != nil { t.Error(err) } @@ -75,9 +83,10 @@ func TestPinSimple(t *testing.T) { } } -func TestPinRecursive(t *testing.T) { - ctx := context.Background() - nd, api, err := makeAPI(ctx) +func (tp *provider) TestPinRecursive(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) if err != nil { t.Error(err) } @@ -176,36 +185,39 @@ func TestPinRecursive(t *testing.T) { t.Errorf("unexpected verify result count: %d", n) } - err = nd.Blockstore.DeleteBlock(p0.Cid()) - if err != nil { - t.Fatal(err) - } - - res, err = api.Pin().Verify(ctx) - if err != nil { - t.Fatal(err) - } - n = 0 - for r := range res { - if r.Ok() { - t.Error("expected pin to not be ok") + //TODO: figure out a way to test verify without touching IpfsNode + /* + err = api.Block().Rm(ctx, p0, opt.Block.Force(true)) + if err != nil { + t.Fatal(err) } - if len(r.BadNodes()) != 1 { - t.Fatalf("unexpected badNodes len") + res, err = api.Pin().Verify(ctx) + if err != nil { + t.Fatal(err) } - - if r.BadNodes()[0].Path().Cid().String() != p0.Cid().String() { - t.Error("unexpected badNode path") + n = 0 + for r := range res { + if r.Ok() { + t.Error("expected pin to not be ok") + } + + if len(r.BadNodes()) != 1 { + t.Fatalf("unexpected badNodes len") + } + + if r.BadNodes()[0].Path().Cid().String() != p0.Cid().String() { + t.Error("unexpected badNode path") + } + + if r.BadNodes()[0].Err().Error() != "merkledag: not found" { + t.Errorf("unexpected badNode error: %s", r.BadNodes()[0].Err().Error()) + } + n++ } - if r.BadNodes()[0].Err().Error() != "merkledag: not found" { - t.Errorf("unexpected badNode error: %s", r.BadNodes()[0].Err().Error()) + if n != 1 { + t.Errorf("unexpected verify result count: %d", n) } - n++ - } - - if n != 1 { - t.Errorf("unexpected verify result count: %d", n) - } + */ } diff --git a/core/coreapi/pubsub_test.go b/core/coreapi/interface/tests/pubsub.go similarity index 79% rename from core/coreapi/pubsub_test.go rename to core/coreapi/interface/tests/pubsub.go index 11a8c68a5af..3462b47551d 100644 --- a/core/coreapi/pubsub_test.go +++ b/core/coreapi/interface/tests/pubsub.go @@ -1,4 +1,4 @@ -package coreapi_test +package tests import ( "context" @@ -7,11 +7,15 @@ import ( "time" ) -func TestBasicPubSub(t *testing.T) { +func (tp *provider) TestPubSub(t *testing.T) { + t.Run("TestBasicPubSub", tp.TestBasicPubSub) +} + +func (tp *provider) TestBasicPubSub(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() - nds, apis, err := makeAPISwarm(ctx, true, 2) + apis, err := tp.MakeAPISwarm(ctx, true, 2) if err != nil { t.Fatal(err) } @@ -46,7 +50,12 @@ func TestBasicPubSub(t *testing.T) { t.Errorf("got invalid data: %s", string(m.Data())) } - if m.From() != nds[1].Identity { + self1, err := apis[1].Key().Self(ctx) + if err != nil { + t.Fatal(err) + } + + if m.From() != self1.ID() { t.Errorf("m.From didn't match") } @@ -59,7 +68,12 @@ func TestBasicPubSub(t *testing.T) { t.Fatalf("got incorrect number of peers: %d", len(peers)) } - if peers[0] != nds[0].Identity { + self0, err := apis[0].Key().Self(ctx) + if err != nil { + t.Fatal(err) + } + + if peers[0] != self0.ID() { t.Errorf("peer didn't match") } diff --git a/core/coreapi/unixfs_test.go b/core/coreapi/interface/tests/unixfs.go similarity index 80% rename from core/coreapi/unixfs_test.go rename to core/coreapi/interface/tests/unixfs.go index 91cb8ea87af..f411ad24da5 100644 --- a/core/coreapi/unixfs_test.go +++ b/core/coreapi/interface/tests/unixfs.go @@ -1,11 +1,8 @@ -package coreapi_test +package tests import ( "bytes" "context" - "encoding/base64" - "fmt" - "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid" "io" "io/ioutil" "math" @@ -15,30 +12,30 @@ import ( "sync" "testing" - "github.com/ipfs/go-ipfs/core" - "github.com/ipfs/go-ipfs/core/coreapi" coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface" "github.com/ipfs/go-ipfs/core/coreapi/interface/options" - "github.com/ipfs/go-ipfs/core/coreunix" - mock "github.com/ipfs/go-ipfs/core/mock" - "github.com/ipfs/go-ipfs/keystore" - "github.com/ipfs/go-ipfs/repo" - ci "gx/ipfs/QmNiJiXwWE3kRhZrC5ej3kSjWHm337pYfhjLGSCDNKJP2s/go-libp2p-crypto" - mocknet "gx/ipfs/QmRBaUEQEeFWywfrZJ64QgsmvcqgLSK3VbvGMR2NM2Edpf/go-libp2p/p2p/net/mock" + "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid" cbor "gx/ipfs/QmRoARq3nkUb13HSKZGepCZSWe5GrVPwx7xURJGZ7KWv9V/go-ipld-cbor" - files "gx/ipfs/QmXWZCd8jfaHmt4UDSnjKmGcrQMw95bDGWqEeVLVJjoANX/go-ipfs-files" - peer "gx/ipfs/QmY5Grm8pJdiSSVsYxx4uNRgweY72EmYwuSDbRnbFok3iY/go-libp2p-peer" - pstore "gx/ipfs/QmZ9zH2FnLcxv1xyzFeUpDUeo55xEhZQHgveZijcxr7TLj/go-libp2p-peerstore" - unixfs "gx/ipfs/Qmbvw7kpSM2p6rbQ57WGRhhqNfCiNGW6EKH4xgHLw4bsnB/go-unixfs" - config "gx/ipfs/QmcZfkbgwwwH5ZLTQRHkSQBDiDqd3skY2eU6MZRgWuXcse/go-ipfs-config" + "gx/ipfs/QmXWZCd8jfaHmt4UDSnjKmGcrQMw95bDGWqEeVLVJjoANX/go-ipfs-files" + "gx/ipfs/Qmbvw7kpSM2p6rbQ57WGRhhqNfCiNGW6EKH4xgHLw4bsnB/go-unixfs" mdag "gx/ipfs/QmdV35UHnL1FM52baPkeUo6u7Fxm2CRUkPTLRPxeF8a4Ap/go-merkledag" mh "gx/ipfs/QmerPMzPk1mJVowm8KgmoknWa4yCYvvugMPsgWmDNUvDLW/go-multihash" - datastore "gx/ipfs/Qmf4xQhNomPNhrtZc67qSnfJSjxjXs9LWvknJtSXwimPrM/go-datastore" - syncds "gx/ipfs/Qmf4xQhNomPNhrtZc67qSnfJSjxjXs9LWvknJtSXwimPrM/go-datastore/sync" ) -const testPeerID = "QmTFauExutTsy4XP6JbMFcw2Wa9645HJt2bTqL6qYDCKfe" +func (tp *provider) TestUnixfs(t *testing.T) { + t.Run("TestAdd", tp.TestAdd) + t.Run("TestAddPinned", tp.TestAddPinned) + t.Run("TestAddHashOnly", tp.TestAddHashOnly) + t.Run("TestGetEmptyFile", tp.TestGetEmptyFile) + t.Run("TestGetDir", tp.TestGetDir) + t.Run("TestGetNonUnixfs", tp.TestGetNonUnixfs) + t.Run("TestLs", tp.TestLs) + t.Run("TestEntriesExpired", tp.TestEntriesExpired) + t.Run("TestLsEmptyDir", tp.TestLsEmptyDir) + t.Run("TestLsNonUnixfs", tp.TestLsNonUnixfs) + t.Run("TestAddCloses", tp.TestAddCloses) +} // `echo -n 'hello, world!' | ipfs add` var hello = "/ipfs/QmQy2Dw4Wk7rdJKjThjYXzfFJNaRKRHhHP5gHHXroJMYxk" @@ -47,97 +44,6 @@ var helloStr = "hello, world!" // `echo -n | ipfs add` var emptyFile = "/ipfs/QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH" -func makeAPISwarm(ctx context.Context, fullIdentity bool, n int) ([]*core.IpfsNode, []coreiface.CoreAPI, error) { - mn := mocknet.New(ctx) - - nodes := make([]*core.IpfsNode, n) - apis := make([]coreiface.CoreAPI, n) - - for i := 0; i < n; i++ { - var ident config.Identity - if fullIdentity { - sk, pk, err := ci.GenerateKeyPair(ci.RSA, 512) - if err != nil { - return nil, nil, err - } - - id, err := peer.IDFromPublicKey(pk) - if err != nil { - return nil, nil, err - } - - kbytes, err := sk.Bytes() - if err != nil { - return nil, nil, err - } - - ident = config.Identity{ - PeerID: id.Pretty(), - PrivKey: base64.StdEncoding.EncodeToString(kbytes), - } - } else { - ident = config.Identity{ - PeerID: testPeerID, - } - } - - c := config.Config{} - c.Addresses.Swarm = []string{fmt.Sprintf("/ip4/127.0.%d.1/tcp/4001", i)} - c.Identity = ident - - r := &repo.Mock{ - C: c, - D: syncds.MutexWrap(datastore.NewMapDatastore()), - K: keystore.NewMemKeystore(), - } - - node, err := core.NewNode(ctx, &core.BuildCfg{ - Repo: r, - Host: mock.MockHostOption(mn), - Online: fullIdentity, - ExtraOpts: map[string]bool{ - "pubsub": true, - }, - }) - if err != nil { - return nil, nil, err - } - nodes[i] = node - apis[i], err = coreapi.NewCoreAPI(node) - if err != nil { - return nil, nil, err - } - } - - err := mn.LinkAll() - if err != nil { - return nil, nil, err - } - - bsinf := core.BootstrapConfigWithPeers( - []pstore.PeerInfo{ - nodes[0].Peerstore.PeerInfo(nodes[0].Identity), - }, - ) - - for _, n := range nodes[1:] { - if err := n.Bootstrap(bsinf); err != nil { - return nil, nil, err - } - } - - return nodes, apis, nil -} - -func makeAPI(ctx context.Context) (*core.IpfsNode, coreiface.CoreAPI, error) { - nd, api, err := makeAPISwarm(ctx, false, 1) - if err != nil { - return nil, nil, err - } - - return nd[0], api[0], nil -} - func strFile(data string) func() files.Node { return func() files.Node { return files.NewBytesFile([]byte(data)) @@ -172,9 +78,10 @@ func wrapped(name string) func(f files.Node) files.Node { } } -func TestAdd(t *testing.T) { - ctx := context.Background() - _, api, err := makeAPI(ctx) +func (tp *provider) TestAdd(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) if err != nil { t.Error(err) } @@ -629,9 +536,10 @@ func TestAdd(t *testing.T) { } } -func TestAddPinned(t *testing.T) { - ctx := context.Background() - _, api, err := makeAPI(ctx) +func (tp *provider) TestAddPinned(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) if err != nil { t.Error(err) } @@ -651,9 +559,10 @@ func TestAddPinned(t *testing.T) { } } -func TestAddHashOnly(t *testing.T) { - ctx := context.Background() - _, api, err := makeAPI(ctx) +func (tp *provider) TestAddHashOnly(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) if err != nil { t.Error(err) } @@ -676,14 +585,15 @@ func TestAddHashOnly(t *testing.T) { } } -func TestGetEmptyFile(t *testing.T) { - ctx := context.Background() - node, api, err := makeAPI(ctx) +func (tp *provider) TestGetEmptyFile(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) if err != nil { t.Fatal(err) } - _, err = coreunix.Add(node, strings.NewReader("")) + _, err = api.Unixfs().Add(ctx, files.NewBytesFile([]byte{})) if err != nil { t.Fatal(err) } @@ -708,14 +618,15 @@ func TestGetEmptyFile(t *testing.T) { } } -func TestGetDir(t *testing.T) { - ctx := context.Background() - node, api, err := makeAPI(ctx) +func (tp *provider) TestGetDir(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) if err != nil { t.Error(err) } edir := unixfs.EmptyDirNode() - err = node.DAG.Add(ctx, edir) + _, err = api.Dag().Put(ctx, bytes.NewReader(edir.RawData()), options.Dag.Codec(cid.DagProtobuf), options.Dag.InputEnc("raw")) if err != nil { t.Error(err) } @@ -740,15 +651,16 @@ func TestGetDir(t *testing.T) { } } -func TestGetNonUnixfs(t *testing.T) { - ctx := context.Background() - node, api, err := makeAPI(ctx) +func (tp *provider) TestGetNonUnixfs(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) if err != nil { t.Error(err) } nd := new(mdag.ProtoNode) - err = node.DAG.Add(ctx, nd) + _, err = api.Dag().Put(ctx, bytes.NewReader(nd.RawData()), options.Dag.Codec(nd.CidBuilder().GetCodec()), options.Dag.InputEnc("raw")) if err != nil { t.Error(err) } @@ -759,23 +671,20 @@ func TestGetNonUnixfs(t *testing.T) { } } -func TestLs(t *testing.T) { - ctx := context.Background() - node, api, err := makeAPI(ctx) +func (tp *provider) TestLs(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) if err != nil { t.Error(err) } r := strings.NewReader("content-of-file") - k, _, err := coreunix.AddWrapped(node, r, "name-of-file") - if err != nil { - t.Error(err) - } - parts := strings.Split(k, "/") - if len(parts) != 2 { - t.Errorf("unexpected path: %s", k) - } - p, err := coreiface.ParsePath("/ipfs/" + parts[0]) + p, err := api.Unixfs().Add(ctx, files.NewMapDirectory(map[string]files.Node{ + "0": files.NewMapDirectory(map[string]files.Node{ + "name-of-file": files.NewReaderFile(r), + }), + })) if err != nil { t.Error(err) } @@ -799,28 +708,25 @@ func TestLs(t *testing.T) { } } -func TestEntriesExpired(t *testing.T) { - ctx := context.Background() - node, api, err := makeAPI(ctx) +func (tp *provider) TestEntriesExpired(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) if err != nil { t.Error(err) } r := strings.NewReader("content-of-file") - k, _, err := coreunix.AddWrapped(node, r, "name-of-file") - if err != nil { - t.Error(err) - } - parts := strings.Split(k, "/") - if len(parts) != 2 { - t.Errorf("unexpected path: %s", k) - } - p, err := coreiface.ParsePath("/ipfs/" + parts[0]) + p, err := api.Unixfs().Add(ctx, files.NewMapDirectory(map[string]files.Node{ + "0": files.NewMapDirectory(map[string]files.Node{ + "name-of-file": files.NewReaderFile(r), + }), + })) if err != nil { t.Error(err) } - ctx, cancel := context.WithCancel(ctx) + ctx, cancel = context.WithCancel(ctx) nd, err := api.Unixfs().Get(ctx, p) if err != nil { @@ -846,14 +752,15 @@ func TestEntriesExpired(t *testing.T) { } } -func TestLsEmptyDir(t *testing.T) { - ctx := context.Background() - node, api, err := makeAPI(ctx) +func (tp *provider) TestLsEmptyDir(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) if err != nil { t.Error(err) } - err = node.DAG.Add(ctx, unixfs.EmptyDirNode()) + _, err = api.Unixfs().Add(ctx, files.NewMapDirectory(map[string]files.Node{"0": files.NewSliceDirectory([]files.DirEntry{})})) if err != nil { t.Error(err) } @@ -874,9 +781,10 @@ func TestLsEmptyDir(t *testing.T) { } // TODO(lgierth) this should test properly, with len(links) > 0 -func TestLsNonUnixfs(t *testing.T) { - ctx := context.Background() - node, api, err := makeAPI(ctx) +func (tp *provider) TestLsNonUnixfs(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) if err != nil { t.Error(err) } @@ -886,7 +794,7 @@ func TestLsNonUnixfs(t *testing.T) { t.Fatal(err) } - err = node.DAG.Add(ctx, nd) + _, err = api.Dag().Put(ctx, bytes.NewReader(nd.RawData()), options.Dag.Codec(cid.DagCBOR), options.Dag.InputEnc("raw")) if err != nil { t.Error(err) } @@ -931,9 +839,10 @@ func (f *closeTestF) Close() error { return nil } -func TestAddCloses(t *testing.T) { - ctx := context.Background() - _, api, err := makeAPI(ctx) +func (tp *provider) TestAddCloses(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + api, err := tp.makeAPI(ctx) if err != nil { t.Error(err) } diff --git a/core/coreapi/test/api_test.go b/core/coreapi/test/api_test.go new file mode 100644 index 00000000000..f44716b7e42 --- /dev/null +++ b/core/coreapi/test/api_test.go @@ -0,0 +1,114 @@ +package test + +import ( + "context" + "encoding/base64" + "fmt" + "github.com/ipfs/go-ipfs/core/coreapi/interface/tests" + "testing" + + "github.com/ipfs/go-ipfs/core" + "github.com/ipfs/go-ipfs/core/coreapi" + coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface" + mock "github.com/ipfs/go-ipfs/core/mock" + "github.com/ipfs/go-ipfs/keystore" + "github.com/ipfs/go-ipfs/repo" + + ci "gx/ipfs/QmNiJiXwWE3kRhZrC5ej3kSjWHm337pYfhjLGSCDNKJP2s/go-libp2p-crypto" + "gx/ipfs/QmRBaUEQEeFWywfrZJ64QgsmvcqgLSK3VbvGMR2NM2Edpf/go-libp2p/p2p/net/mock" + "gx/ipfs/QmY5Grm8pJdiSSVsYxx4uNRgweY72EmYwuSDbRnbFok3iY/go-libp2p-peer" + pstore "gx/ipfs/QmZ9zH2FnLcxv1xyzFeUpDUeo55xEhZQHgveZijcxr7TLj/go-libp2p-peerstore" + "gx/ipfs/QmcZfkbgwwwH5ZLTQRHkSQBDiDqd3skY2eU6MZRgWuXcse/go-ipfs-config" + "gx/ipfs/Qmf4xQhNomPNhrtZc67qSnfJSjxjXs9LWvknJtSXwimPrM/go-datastore" + syncds "gx/ipfs/Qmf4xQhNomPNhrtZc67qSnfJSjxjXs9LWvknJtSXwimPrM/go-datastore/sync" +) + +const testPeerID = "QmTFauExutTsy4XP6JbMFcw2Wa9645HJt2bTqL6qYDCKfe" + +type NodeProvider struct{} + +func (NodeProvider) MakeAPISwarm(ctx context.Context, fullIdentity bool, n int) ([]coreiface.CoreAPI, error) { + mn := mocknet.New(ctx) + + nodes := make([]*core.IpfsNode, n) + apis := make([]coreiface.CoreAPI, n) + + for i := 0; i < n; i++ { + var ident config.Identity + if fullIdentity { + sk, pk, err := ci.GenerateKeyPair(ci.RSA, 512) + if err != nil { + return nil, err + } + + id, err := peer.IDFromPublicKey(pk) + if err != nil { + return nil, err + } + + kbytes, err := sk.Bytes() + if err != nil { + return nil, err + } + + ident = config.Identity{ + PeerID: id.Pretty(), + PrivKey: base64.StdEncoding.EncodeToString(kbytes), + } + } else { + ident = config.Identity{ + PeerID: testPeerID, + } + } + + c := config.Config{} + c.Addresses.Swarm = []string{fmt.Sprintf("/ip4/127.0.%d.1/tcp/4001", i)} + c.Identity = ident + + r := &repo.Mock{ + C: c, + D: syncds.MutexWrap(datastore.NewMapDatastore()), + K: keystore.NewMemKeystore(), + } + + node, err := core.NewNode(ctx, &core.BuildCfg{ + Repo: r, + Host: mock.MockHostOption(mn), + Online: fullIdentity, + ExtraOpts: map[string]bool{ + "pubsub": true, + }, + }) + if err != nil { + return nil, err + } + nodes[i] = node + apis[i], err = coreapi.NewCoreAPI(node) + if err != nil { + return nil, err + } + } + + err := mn.LinkAll() + if err != nil { + return nil, err + } + + bsinf := core.BootstrapConfigWithPeers( + []pstore.PeerInfo{ + nodes[0].Peerstore.PeerInfo(nodes[0].Identity), + }, + ) + + for _, n := range nodes[1:] { + if err := n.Bootstrap(bsinf); err != nil { + return nil, err + } + } + + return apis, nil +} + +func TestIface(t *testing.T) { + tests.TestApi(&NodeProvider{})(t) +}