2020-11-26 14:26:53 +00:00
|
|
|
package blobovnicza
|
|
|
|
|
|
|
|
import (
|
2023-03-13 11:37:35 +00:00
|
|
|
"context"
|
2023-08-09 13:01:44 +00:00
|
|
|
"crypto/rand"
|
2020-11-26 14:26:53 +00:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/logger/test"
|
2023-08-04 11:14:07 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client"
|
2023-03-07 13:38:26 +00:00
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
|
|
|
oidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id/test"
|
2020-11-26 14:26:53 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func testPutGet(t *testing.T, blz *Blobovnicza, addr oid.Address, sz uint64, assertErrPut, assertErrGet func(error) bool) oid.Address {
|
2020-12-02 09:58:42 +00:00
|
|
|
// create binary object
|
|
|
|
data := make([]byte, sz)
|
2022-03-03 14:16:49 +00:00
|
|
|
rand.Read(data)
|
2020-12-02 09:58:42 +00:00
|
|
|
|
2022-05-23 14:21:14 +00:00
|
|
|
var pPut PutPrm
|
2020-12-02 09:58:42 +00:00
|
|
|
pPut.SetAddress(addr)
|
|
|
|
pPut.SetMarshaledObject(data)
|
2023-06-20 08:24:14 +00:00
|
|
|
_, err := blz.Put(context.Background(), pPut)
|
2022-03-17 08:03:58 +00:00
|
|
|
if assertErrPut != nil {
|
|
|
|
require.True(t, assertErrPut(err))
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2020-11-26 14:26:53 +00:00
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
if assertErrGet != nil {
|
|
|
|
testGet(t, blz, addr, data, assertErrGet)
|
2020-11-26 14:26:53 +00:00
|
|
|
}
|
|
|
|
|
2020-12-02 09:58:42 +00:00
|
|
|
return addr
|
2020-11-26 14:26:53 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func testGet(t *testing.T, blz *Blobovnicza, addr oid.Address, expObj []byte, assertErr func(error) bool) {
|
2022-05-23 14:15:16 +00:00
|
|
|
var pGet GetPrm
|
2020-11-26 14:26:53 +00:00
|
|
|
pGet.SetAddress(addr)
|
|
|
|
|
|
|
|
// try to read object from Blobovnicza
|
2023-03-13 11:37:35 +00:00
|
|
|
res, err := blz.Get(context.Background(), pGet)
|
2022-03-17 08:03:58 +00:00
|
|
|
if assertErr != nil {
|
|
|
|
require.True(t, assertErr(err))
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2020-11-26 14:26:53 +00:00
|
|
|
|
2022-03-17 08:03:58 +00:00
|
|
|
if assertErr == nil {
|
2020-11-26 14:26:53 +00:00
|
|
|
require.Equal(t, expObj, res.Object())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBlobovnicza(t *testing.T) {
|
|
|
|
p := "./test_blz"
|
|
|
|
|
|
|
|
sizeLim := uint64(256 * 1 << 10) // 256KB
|
|
|
|
objSizeLim := sizeLim / 2
|
|
|
|
|
|
|
|
// create Blobovnicza instance
|
|
|
|
blz := New(
|
|
|
|
WithPath(p),
|
|
|
|
WithObjectSizeLimit(objSizeLim),
|
|
|
|
WithFullSizeLimit(sizeLim),
|
2024-01-09 08:37:41 +00:00
|
|
|
WithLogger(test.NewLogger(t)),
|
2020-11-26 14:26:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
defer os.Remove(p)
|
|
|
|
|
|
|
|
// open Blobovnicza
|
2024-10-21 08:20:17 +00:00
|
|
|
require.NoError(t, blz.Open(context.Background()))
|
2020-11-26 14:26:53 +00:00
|
|
|
|
|
|
|
// initialize Blobovnicza
|
|
|
|
require.NoError(t, blz.Init())
|
|
|
|
|
|
|
|
// try to read non-existent address
|
2023-08-04 11:14:07 +00:00
|
|
|
testGet(t, blz, oidtest.Address(), nil, client.IsErrObjectNotFound)
|
2020-11-26 14:26:53 +00:00
|
|
|
|
|
|
|
filled := uint64(15 * 1 << 10)
|
|
|
|
|
|
|
|
// test object 15KB
|
2022-05-31 17:00:41 +00:00
|
|
|
addr := testPutGet(t, blz, oidtest.Address(), filled, nil, nil)
|
2020-11-26 14:26:53 +00:00
|
|
|
|
|
|
|
// remove the object
|
2022-05-23 14:15:16 +00:00
|
|
|
var dPrm DeletePrm
|
2020-11-26 14:26:53 +00:00
|
|
|
dPrm.SetAddress(addr)
|
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
_, err := blz.Delete(context.Background(), dPrm)
|
2020-11-26 14:26:53 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// should return 404
|
2023-08-04 11:14:07 +00:00
|
|
|
testGet(t, blz, addr, nil, client.IsErrObjectNotFound)
|
2020-11-26 14:26:53 +00:00
|
|
|
|
|
|
|
// fill Blobovnicza fully
|
|
|
|
for ; filled < sizeLim; filled += objSizeLim {
|
2022-05-31 17:00:41 +00:00
|
|
|
testPutGet(t, blz, oidtest.Address(), objSizeLim, nil, nil)
|
2020-11-26 14:26:53 +00:00
|
|
|
}
|
|
|
|
|
2023-08-30 20:36:48 +00:00
|
|
|
// blobovnizca accepts object event if full
|
2022-05-31 17:00:41 +00:00
|
|
|
testPutGet(t, blz, oidtest.Address(), 1024, func(err error) bool {
|
2023-08-30 20:36:48 +00:00
|
|
|
return err == nil
|
2022-03-17 08:03:58 +00:00
|
|
|
}, nil)
|
2020-11-26 14:26:53 +00:00
|
|
|
|
|
|
|
require.NoError(t, blz.Close())
|
|
|
|
}
|