2022-03-03 14:16:49 +00:00
|
|
|
package blobstor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
objectCore "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/object"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/common"
|
|
|
|
cidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id/test"
|
|
|
|
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
oidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id/test"
|
2022-03-03 14:16:49 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestExists(t *testing.T) {
|
2022-12-23 17:35:35 +00:00
|
|
|
dir, err := os.MkdirTemp("", "frostfs*")
|
2022-03-03 14:16:49 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
t.Cleanup(func() { _ = os.RemoveAll(dir) })
|
|
|
|
|
|
|
|
const smallSizeLimit = 512
|
|
|
|
|
2022-07-11 12:34:17 +00:00
|
|
|
b := New(
|
|
|
|
WithStorages(defaultStorages(dir, smallSizeLimit)))
|
2022-06-28 13:42:50 +00:00
|
|
|
require.NoError(t, b.Open(false))
|
2022-03-03 14:16:49 +00:00
|
|
|
require.NoError(t, b.Init())
|
|
|
|
|
|
|
|
objects := []*objectSDK.Object{
|
|
|
|
testObject(smallSizeLimit / 2),
|
|
|
|
testObject(smallSizeLimit + 1),
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range objects {
|
2022-07-06 13:41:35 +00:00
|
|
|
var prm common.PutPrm
|
|
|
|
prm.Object = objects[i]
|
2022-03-03 14:16:49 +00:00
|
|
|
_, err = b.Put(prm)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2022-07-06 12:10:21 +00:00
|
|
|
var prm common.ExistsPrm
|
2022-03-03 14:16:49 +00:00
|
|
|
for i := range objects {
|
2022-07-06 12:10:21 +00:00
|
|
|
prm.Address = objectCore.AddressOf(objects[i])
|
2022-03-03 14:16:49 +00:00
|
|
|
|
|
|
|
res, err := b.Exists(prm)
|
|
|
|
require.NoError(t, err)
|
2022-07-06 12:10:21 +00:00
|
|
|
require.True(t, res.Exists)
|
2022-03-03 14:16:49 +00:00
|
|
|
}
|
|
|
|
|
2022-07-06 12:10:21 +00:00
|
|
|
prm.Address = oidtest.Address()
|
2022-03-03 14:16:49 +00:00
|
|
|
res, err := b.Exists(prm)
|
|
|
|
require.NoError(t, err)
|
2022-07-06 12:10:21 +00:00
|
|
|
require.False(t, res.Exists)
|
2022-03-03 14:16:49 +00:00
|
|
|
|
|
|
|
t.Run("corrupt direcrory", func(t *testing.T) {
|
|
|
|
var bigDir string
|
|
|
|
de, err := os.ReadDir(dir)
|
|
|
|
require.NoError(t, err)
|
|
|
|
for i := range de {
|
|
|
|
if de[i].Name() != blobovniczaDir {
|
|
|
|
bigDir = filepath.Join(dir, de[i].Name())
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
require.NotEmpty(t, bigDir)
|
|
|
|
|
|
|
|
require.NoError(t, os.Chmod(dir, 0))
|
2022-07-11 12:34:17 +00:00
|
|
|
t.Cleanup(func() { require.NoError(t, os.Chmod(dir, 0777)) })
|
2022-03-03 14:16:49 +00:00
|
|
|
|
|
|
|
// Object exists, first error is logged.
|
2022-07-06 12:10:21 +00:00
|
|
|
prm.Address = objectCore.AddressOf(objects[0])
|
2022-03-03 14:16:49 +00:00
|
|
|
res, err := b.Exists(prm)
|
|
|
|
require.NoError(t, err)
|
2022-07-06 12:10:21 +00:00
|
|
|
require.True(t, res.Exists)
|
2022-03-03 14:16:49 +00:00
|
|
|
|
|
|
|
// Object doesn't exist, first error is returned.
|
2022-07-06 12:10:21 +00:00
|
|
|
prm.Address = objectCore.AddressOf(objects[1])
|
2022-03-03 14:16:49 +00:00
|
|
|
_, err = b.Exists(prm)
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
}
|
2022-07-05 13:47:39 +00:00
|
|
|
|
|
|
|
func testObject(sz uint64) *objectSDK.Object {
|
|
|
|
raw := objectSDK.New()
|
|
|
|
|
|
|
|
raw.SetID(oidtest.ID())
|
|
|
|
raw.SetContainerID(cidtest.ID())
|
|
|
|
|
|
|
|
raw.SetPayload(make([]byte, sz))
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|