Skip to content

Commit

Permalink
Move test utility functions to test repo (#202)
Browse files Browse the repository at this point in the history
* Move test utility functions to ipfs/go-test repo

The test package contains ligic used in tests in various projects. This logic was moved into the ipfs/go-test repo.

* Use ipfs/go-test
* simplify code with go-test
  • Loading branch information
gammazero authored Jun 27, 2024
1 parent f667069 commit e744ebf
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 @@ -157,7 +158,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 @@ -361,7 +362,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 @@ -10,6 +10,7 @@ require (
github.com/ipfs/go-datastore v0.6.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.2
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 @@ -139,6 +139,8 @@ github.com/ipfs/go-ipld-format v0.6.0 h1:VEJlA2kQ3LqFSIm5Vu6eIlSxD/Ze90xtc4Meten
github.com/ipfs/go-ipld-format v0.6.0/go.mod h1:g4QVMTn3marU3qXchwjpKPKgJv+zF+OlaKMyhJ4LHPg=
github.com/ipfs/go-log/v2 v2.5.1 h1:1XdUzF7048prq4aBjDQQ4SL5RxftpRGdXhNRwKSAlcY=
github.com/ipfs/go-log/v2 v2.5.1/go.mod h1:prSpmC1Gpllc9UYWxDiZDreBYw7zp4Iqp1kOLU9U5UI=
github.com/ipfs/go-test v0.0.2 h1:Wdxl4bKEdjEM8SLiulXMHlAQwHYOhX3CSBoUoEvncmM=
github.com/ipfs/go-test v0.0.2/go.mod h1:qhIM1EluEfElKKM6fnWxGn822/z9knUGM1+I/OAQNKI=
github.com/ipld/go-ipld-prime v0.21.0 h1:n4JmcpOlPDIxBcY037SVfpd1G+Sj1nKZah0m6QH9C2E=
github.com/ipld/go-ipld-prime v0.21.0/go.mod h1:3RLqy//ERg/y5oShXXdx5YIp50cFGOanyMctpPjsvxQ=
github.com/jackpal/go-nat-pmp v1.0.2 h1:KzKSgb7qkJvOUTqYl9/Hg/me3pWgBmERKrTGD7BdWus=
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 e744ebf

Please sign in to comment.