-
Notifications
You must be signed in to change notification settings - Fork 38
/
common.go
98 lines (80 loc) · 2.53 KB
/
common.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
package blobstortest
import (
"math/rand"
"testing"
objectCore "github.com/nspcc-dev/neofs-node/pkg/core/object"
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor/common"
cidtest "github.com/nspcc-dev/neofs-sdk-go/container/id/test"
objectSDK "github.com/nspcc-dev/neofs-sdk-go/object"
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
oidtest "github.com/nspcc-dev/neofs-sdk-go/object/id/test"
"github.com/stretchr/testify/require"
)
// Constructor constructs blobstor component.
// Each call must create a component using different file-system path.
type Constructor = func(t *testing.T) common.Storage
// objectDesc is a helper structure to avoid multiple `Marshal` invokes during tests.
type objectDesc struct {
obj *objectSDK.Object
addr oid.Address
raw []byte
storageID []byte
}
func TestAll(t *testing.T, cons Constructor, min, max uint64) {
t.Run("get", func(t *testing.T) {
TestGet(t, cons, min, max)
})
t.Run("get range", func(t *testing.T) {
TestGetRange(t, cons, min, max)
})
t.Run("delete", func(t *testing.T) {
TestDelete(t, cons, min, max)
})
t.Run("exists", func(t *testing.T) {
TestExists(t, cons, min, max)
})
t.Run("iterate", func(t *testing.T) {
TestIterate(t, cons, min, max)
})
}
func TestInfo(t *testing.T, cons Constructor, expectedType string, expectedPath string) {
s := cons(t)
require.Equal(t, expectedType, s.Type())
require.Equal(t, expectedPath, s.Path())
}
func prepare(t *testing.T, count int, s common.Storage, min, max uint64) []objectDesc {
objects := make([]objectDesc, count)
for i := range objects {
objects[i].obj = NewObject(min + uint64(rand.Intn(int(max-min+1)))) // not too large
objects[i].addr = objectCore.AddressOf(objects[i].obj)
raw, err := objects[i].obj.Marshal()
require.NoError(t, err)
objects[i].raw = raw
}
for i := range objects {
var prm common.PutPrm
prm.Address = objects[i].addr
prm.Object = objects[i].obj
prm.RawData = objects[i].raw
putRes, err := s.Put(prm)
require.NoError(t, err)
objects[i].storageID = putRes.StorageID
}
return objects
}
// NewObject creates a regular object of specified size with a random payload.
func NewObject(sz uint64) *objectSDK.Object {
raw := objectSDK.New()
raw.SetID(oidtest.ID())
raw.SetContainerID(cidtest.ID())
payload := make([]byte, sz)
//nolint:staticcheck
rand.Read(payload)
raw.SetPayload(payload)
// fit the binary size to the required
data, _ := raw.Marshal()
if ln := uint64(len(data)); ln > sz {
raw.SetPayload(raw.Payload()[:sz-(ln-sz)])
}
return raw
}