Skip to content

Commit

Permalink
Move test utility functions to test repo (#203)
Browse files Browse the repository at this point in the history
The test package contains ligic used in tests in various projects. This logic was moved into the ipfs/go-test repo.
  • Loading branch information
gammazero authored Jun 26, 2024
1 parent 3b7d1ae commit 265ee16
Show file tree
Hide file tree
Showing 15 changed files with 58 additions and 200 deletions.
3 changes: 2 additions & 1 deletion dagsync/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/ipfs/go-cid"
"github.com/ipfs/go-datastore"
dssync "github.com/ipfs/go-datastore/sync"
"github.com/ipfs/go-test/random"
"github.com/ipld/go-ipld-prime/linking"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"github.com/ipld/go-ipld-prime/node/basicnode"
Expand Down Expand Up @@ -138,7 +139,7 @@ func TestSyncFnHttp(t *testing.T) {

// Try to sync with a non-existing cid to chack that sync returns with err,
// and SyncFinished watcher does not get event.
cids := test.RandomCids(1)
cids := random.Cids(1)
ctx, syncncl := context.WithTimeout(context.Background(), time.Second)
defer syncncl()

Expand Down
4 changes: 2 additions & 2 deletions dagsync/ipnisync/head/signedhead_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"testing"

"github.com/ipfs/go-cid"
"github.com/ipfs/go-test/random"
_ "github.com/ipld/go-ipld-prime/codec/dagjson"
headschema "github.com/ipni/go-libipni/dagsync/ipnisync/head"
"github.com/ipni/go-libipni/test"
ic "github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -69,7 +69,7 @@ func generateSignedHead() *headschema.SignedHead {
if err != nil {
panic(err.Error())
}
mhs := test.RandomMultihashes(1)
mhs := random.Multihashes(1)
headCid := cid.NewCidV1(cid.Raw, mhs[0])

sh, err := headschema.NewSignedHead(headCid, testTopic, privKey)
Expand Down
10 changes: 5 additions & 5 deletions dagsync/selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"testing"

"github.com/ipfs/go-cid"
"github.com/ipfs/go-test/random"
"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/datamodel"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
basicnode "github.com/ipld/go-ipld-prime/node/basic"
"github.com/ipld/go-ipld-prime/traversal/selector"
selectorbuilder "github.com/ipld/go-ipld-prime/traversal/selector/builder"
"github.com/ipni/go-libipni/dagsync/test"
"github.com/stretchr/testify/require"
)

Expand All @@ -31,8 +31,8 @@ func TestGetStopNodeWhenNil(t *testing.T) {
}

func TestGetRecursionLimit(t *testing.T) {
testCid := test.RandomCids(1)
testStopLink := cidlink.Link{Cid: testCid[0]}
testCid := random.Cids(1)[0]
testStopLink := cidlink.Link{Cid: testCid}
ssb := selectorbuilder.NewSelectorSpecBuilder(basicnode.Prototype.Any)
tests := []struct {
name string
Expand Down Expand Up @@ -98,8 +98,8 @@ func TestGetRecursionLimit(t *testing.T) {
}

func TestWithRecursionLimit(t *testing.T) {
testCid := test.RandomCids(1)
testStopLink := cidlink.Link{Cid: testCid[0]}
testCid := random.Cids(1)[0]
testStopLink := cidlink.Link{Cid: testCid}
ssb := selectorbuilder.NewSelectorSpecBuilder(basicnode.Prototype.Any)
tests := []struct {
name string
Expand Down
5 changes: 3 additions & 2 deletions dagsync/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/ipfs/go-cid"
"github.com/ipfs/go-datastore"
dssync "github.com/ipfs/go-datastore/sync"
"github.com/ipfs/go-test/random"
"github.com/ipld/go-ipld-prime"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"github.com/ipni/go-libipni/announce"
Expand Down Expand Up @@ -105,7 +106,7 @@ func TestSyncFn(t *testing.T) {

// Try to sync with a non-existing cid to check that sync returns with err,
// and SyncFinished watcher does not get event.
cids := test.RandomCids(1)
cids := random.Cids(1)
ctx, syncncl := context.WithTimeout(context.Background(), updateTimeout)
defer syncncl()
peerInfo := peer.AddrInfo{
Expand Down Expand Up @@ -309,7 +310,7 @@ func TestLatestSyncFailure(t *testing.T) {
Addrs: pub.Addrs(),
}
// Announce bad CID.
badCid := test.RandomCids(1)[0]
badCid := random.Cids(1)[0]
err = sub.Announce(context.Background(), badCid, pubInfo)
require.NoError(t, err)
// Check for fetch failure.
Expand Down
5 changes: 0 additions & 5 deletions dagsync/test/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/ipni/go-libipni/dagsync/ipnisync"
"github.com/ipni/go-libipni/ingest/schema"
"github.com/ipni/go-libipni/maurl"
"github.com/ipni/go-libipni/test"
"github.com/libp2p/go-libp2p"
pubsub "github.com/libp2p/go-libp2p-pubsub"
"github.com/libp2p/go-libp2p/core/crypto"
Expand Down Expand Up @@ -169,10 +168,6 @@ func encode(lsys ipld.LinkSystem, n ipld.Node) (ipld.Node, ipld.Link) {
return n, lnk
}

func RandomCids(n int) []cid.Cid {
return test.RandomCids(n)
}

func MkLinkSystem(ds datastore.Batching) ipld.LinkSystem {
lsys := cidlink.DefaultLinkSystem()
lsys.StorageReadOpener = func(_ ipld.LinkContext, lnk ipld.Link) (io.Reader, error) {
Expand Down
4 changes: 2 additions & 2 deletions dhash/dhash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"math/rand"
"testing"

"github.com/ipni/go-libipni/test"
"github.com/ipfs/go-test/random"
"github.com/multiformats/go-multihash"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -60,7 +60,7 @@ func TestCanDecryptEncryptedValue(t *testing.T) {
}

func TestSecondMultihash(t *testing.T) {
mh := test.RandomMultihashes(1)[0]
mh := random.Multihashes(1)[0]
smh := SecondMultihash(mh)

h := sha256.New()
Expand Down
4 changes: 2 additions & 2 deletions find/model/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"bytes"
"testing"

"github.com/ipfs/go-test/random"
"github.com/ipni/go-libipni/find/model"
"github.com/ipni/go-libipni/test"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multiaddr"
)
Expand All @@ -32,7 +32,7 @@ func TestMarshal(t *testing.T) {
MultihashResults: []model.MultihashResult{},
}

mhs := test.RandomMultihashes(3)
mhs := random.Multihashes(3)
for i := range mhs {
resp.MultihashResults = append(resp.MultihashResults, model.MultihashResult{
Multihash: mhs[i],
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/ipfs/go-graphsync v0.17.0
github.com/ipfs/go-ipld-format v0.6.0
github.com/ipfs/go-log/v2 v2.5.1
github.com/ipfs/go-test v0.0.1
github.com/ipld/go-ipld-prime v0.21.0
github.com/libp2p/go-libp2p v0.35.1
github.com/libp2p/go-libp2p-pubsub v0.11.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,8 @@ github.com/ipfs/go-metrics-interface v0.0.1 h1:j+cpbjYvu4R8zbleSs36gvB7jR+wsL2fG
github.com/ipfs/go-metrics-interface v0.0.1/go.mod h1:6s6euYU4zowdslK0GKHmqaIZ3j/b/tL7HTWtJ4VPgWY=
github.com/ipfs/go-peertaskqueue v0.8.1 h1:YhxAs1+wxb5jk7RvS0LHdyiILpNmRIRnZVztekOF0pg=
github.com/ipfs/go-peertaskqueue v0.8.1/go.mod h1:Oxxd3eaK279FxeydSPPVGHzbwVeHjatZ2GA8XD+KbPU=
github.com/ipfs/go-test v0.0.1 h1:ndnYcnWEXzYQqkYX18fh/p1Pn8HHnj1OdA/m+Z10wFE=
github.com/ipfs/go-test v0.0.1/go.mod h1:uDtFPEphmLrcPCitEY/xn1QnR7wm7iFBVrOIjkdxSZ0=
github.com/ipfs/go-unixfs v0.4.4 h1:D/dLBOJgny5ZLIur2vIXVQVW0EyDHdOMBDEhgHrt6rY=
github.com/ipfs/go-unixfs v0.4.4/go.mod h1:TSG7G1UuT+l4pNj91raXAPkX0BhJi3jST1FDTfQ5QyM=
github.com/ipfs/go-unixfsnode v1.7.4 h1:iLvKyAVKUYOIAW2t4kDYqsT7VLGj31eXJE2aeqGfbwA=
Expand Down
18 changes: 3 additions & 15 deletions ingest/model/ingest_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,13 @@ import (
"bytes"
"testing"

"github.com/ipni/go-libipni/test"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
p2ptest "github.com/libp2p/go-libp2p/core/test"
"github.com/ipfs/go-test/random"
)

func TestIngestRequest(t *testing.T) {
mhs := test.RandomMultihashes(1)

mhs := random.Multihashes(1)
metadata := []byte("test-metadata")

privKey, pubKey, err := p2ptest.RandTestKeyPair(crypto.Ed25519, 256)
if err != nil {
t.Fatal(err)
}
peerID, err := peer.IDFromPublicKey(pubKey)
if err != nil {
t.Fatal(err)
}
peerID, privKey, _ := random.Identity()

ctxID := []byte("test-context-id")
address := "/ip4/127.0.0.1/tcp/7777"
Expand Down
Loading

0 comments on commit 265ee16

Please sign in to comment.