forked from filecoin-project/boost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_util.go
119 lines (101 loc) · 3.76 KB
/
test_util.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
package piecedirectory
import (
"context"
"io"
"os"
"testing"
"time"
"github.com/filecoin-project/boost/extern/boostd-data/model"
"github.com/filecoin-project/boost/piecedirectory/types"
mock_piecedirectory "github.com/filecoin-project/boost/piecedirectory/types/mocks"
"github.com/filecoin-project/boost/testutil"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-commp-utils/writer"
"github.com/filecoin-project/go-state-types/abi"
"github.com/golang/mock/gomock"
"github.com/ipfs/go-cid"
"github.com/ipld/go-car/v2"
"github.com/stretchr/testify/require"
)
// Get the index records from the CAR file
func GetRecords(t *testing.T, reader car.SectionReader) []model.Record {
_, err := reader.Seek(0, io.SeekStart)
require.NoError(t, err)
blockReader, err := car.NewBlockReader(reader)
require.NoError(t, err)
var recs []model.Record
blockMetadata, err := blockReader.SkipNext()
for err == nil {
recs = append(recs, model.Record{
Cid: blockMetadata.Cid,
OffsetSize: model.OffsetSize{
Offset: blockMetadata.Offset,
Size: blockMetadata.Size,
},
})
blockMetadata, err = blockReader.SkipNext()
}
require.ErrorIs(t, err, io.EOF)
return recs
}
func CreateCarFile(t *testing.T, seed ...int) (cid.Cid, string) {
var rseed int
if len(seed) == 0 {
rseed = int(time.Now().UnixMilli())
} else {
rseed = seed[0]
}
randomFilePath, err := testutil.CreateRandomFile(t.TempDir(), rseed, 64*1024)
require.NoError(t, err)
root, carFilePath, err := testutil.CreateDenseCARv2(t.TempDir(), randomFilePath)
require.NoError(t, err)
return root, carFilePath
}
func CalculateCommp(t *testing.T, rdr io.ReadSeeker) writer.DataCIDSize {
_, err := rdr.Seek(0, io.SeekStart)
require.NoError(t, err)
w := &writer.Writer{}
_, err = io.CopyBuffer(w, rdr, make([]byte, writer.CommPBuf))
require.NoError(t, err)
commp, err := w.Sum()
require.NoError(t, err)
return commp
}
func CreateMockPieceReader(t *testing.T, reader car.SectionReader) *mock_piecedirectory.MockPieceReader {
ctrl := gomock.NewController(t)
pr := mock_piecedirectory.NewMockPieceReader(ctrl)
pr.EXPECT().GetReader(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().DoAndReturn(
func(_ context.Context, _ address.Address, _ abi.SectorNumber, _ abi.PaddedPieceSize, _ abi.PaddedPieceSize) (types.SectionReader, error) {
_, err := reader.Seek(0, io.SeekStart)
return &MockSectionReader{reader}, err
})
return pr
}
func CreateMockPieceReaders(t *testing.T, readers map[abi.SectorNumber]car.SectionReader) *mock_piecedirectory.MockPieceReader {
ctrl := gomock.NewController(t)
pr := mock_piecedirectory.NewMockPieceReader(ctrl)
for sectorNumber := range readers {
pr.EXPECT().GetReader(gomock.Any(), gomock.Any(), sectorNumber, gomock.Any(), gomock.Any()).AnyTimes().DoAndReturn(
func(_ context.Context, _ address.Address, sectorNumber abi.SectorNumber, _ abi.PaddedPieceSize, _ abi.PaddedPieceSize) (types.SectionReader, error) {
r := readers[sectorNumber]
_, err := r.Seek(0, io.SeekStart)
return &MockSectionReader{r}, err
})
}
return pr
}
type MockSectionReader struct {
car.SectionReader
}
func (MockSectionReader) Close() error { return nil }
// like `CreateMockPieceReader`, but returns a reader over the contents of a file.
func CreateMockPieceReaderFromPath(t *testing.T, path string) *mock_piecedirectory.MockPieceReader {
ctrl := gomock.NewController(t)
pr := mock_piecedirectory.NewMockPieceReader(ctrl)
pr.EXPECT().GetReader(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().DoAndReturn(
func(_ context.Context, _ address.Address, _ abi.SectorNumber, _ abi.PaddedPieceSize, _ abi.PaddedPieceSize) (types.SectionReader, error) {
f, err := os.Open(path)
return f, err
})
return pr
}