/
testutil.go
151 lines (135 loc) · 4.1 KB
/
testutil.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package testutil
import (
"context"
"io"
"os"
"path"
"path/filepath"
"runtime"
"testing"
"time"
datatransfer "github.com/filecoin-project/go-data-transfer/v2"
dtimpl "github.com/filecoin-project/go-data-transfer/v2/impl"
dtnetwork "github.com/filecoin-project/go-data-transfer/v2/network"
gstransport "github.com/filecoin-project/go-data-transfer/v2/transport/graphsync"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-datastore"
gsimpl "github.com/ipfs/go-graphsync/impl"
gsnet "github.com/ipfs/go-graphsync/network"
carv2 "github.com/ipld/go-car/v2"
"github.com/ipld/go-car/v2/blockstore"
"github.com/ipld/go-ipld-prime"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multiaddr"
"github.com/stretchr/testify/require"
)
// ThisDir gets the current directory of the source file its called in
func ThisDir(t *testing.T) string {
_, fname, _, ok := runtime.Caller(1)
require.True(t, ok)
return filepath.Dir(fname)
}
// RoBlockstore is just the needed interface for GetBstoreLen
type RoBlockstore interface {
AllKeysChan(ctx context.Context) (<-chan cid.Cid, error)
}
// GetBstoreLen gets the total CID cound in a blockstore
func GetBstoreLen(ctx context.Context, t *testing.T, bs RoBlockstore) int {
ch, err := bs.AllKeysChan(ctx)
require.NoError(t, err)
var len int
for range ch {
len++
}
return len
}
// OpenSampleCar opens a car file in the testdata directory to a blockstore
func OpenSampleCar(t *testing.T, carFileName string) *blockstore.ReadOnly {
carFilePath := filepath.Join(ThisDir(t), "../testdata", carFileName)
rdOnlyBS, err := blockstore.OpenReadOnly(carFilePath, carv2.ZeroLengthSectionAsEOF(true), blockstore.UseWholeCIDs(true))
require.NoError(t, err)
return rdOnlyBS
}
// SetupDataTransferOnHost generates a data transfer instance for the given libp2p host
func SetupDataTransferOnHost(t *testing.T, h host.Host, store datastore.Batching, lsys ipld.LinkSystem) datatransfer.Manager {
gsnet := gsnet.NewFromLibp2pHost(h)
dtNet := dtnetwork.NewFromLibp2pHost(h)
gs := gsimpl.New(context.Background(), gsnet, lsys)
tp := gstransport.NewTransport(h.ID(), gs)
dt, err := dtimpl.NewDataTransfer(store, dtNet, tp)
require.NoError(t, err)
ready := make(chan error, 1)
dt.OnReady(func(err error) {
ready <- err
})
require.NoError(t, dt.Start(context.Background()))
timer := time.NewTimer(500 * time.Millisecond)
select {
case readyErr := <-ready:
require.NoError(t, readyErr)
case <-timer.C:
require.FailNow(t, "data transfer did not initialize")
}
return dt
}
// CopyDir copies a whole directory recursively
func CopyDir(t *testing.T, src string, dst string) {
srcinfo, err := os.Stat(src)
require.NoError(t, err)
err = os.MkdirAll(dst, srcinfo.Mode())
require.NoError(t, err)
fds, err := os.ReadDir(src)
require.NoError(t, err)
for _, fd := range fds {
srcfp := path.Join(src, fd.Name())
dstfp := path.Join(dst, fd.Name())
if fd.IsDir() {
CopyDir(t, srcfp, dstfp)
} else {
CopyFile(t, srcfp, dstfp)
}
}
}
// CopyFile copies a single file from src to dst
func CopyFile(t *testing.T, src, dst string) {
srcfd, err := os.Open(src)
require.NoError(t, err)
defer srcfd.Close()
dstfd, err := os.Create(dst)
require.NoError(t, err)
defer dstfd.Close()
_, err = io.Copy(dstfd, srcfd)
require.NoError(t, err)
}
// Sometimes the peerstore is not populated immediately. This is resolved by a
// delay and retry.
func WaitForAddrs(h host.Host) peer.AddrInfo {
addrInfo := h.Peerstore().PeerInfo(h.ID())
var tries int
for len(addrInfo.Addrs) == 0 {
if tries == 10 {
break
}
tries++
time.Sleep(500 * time.Millisecond)
addrInfo = h.Peerstore().PeerInfo(h.ID())
}
return addrInfo
}
func MultiAddsToString(addrs []multiaddr.Multiaddr) []string {
var rAddrs []string
for _, addr := range addrs {
rAddrs = append(rAddrs, addr.String())
}
return rAddrs
}
func StringsToMultiaddrs(t *testing.T, addrs []string) []multiaddr.Multiaddr {
mAddrs := make([]multiaddr.Multiaddr, len(addrs))
for i, addr := range addrs {
ma, err := multiaddr.NewMultiaddr(addr)
require.NoError(t, err)
mAddrs[i] = ma
}
return mAddrs
}