2023-03-14 14:31:15 +00:00
|
|
|
package tree
|
2022-04-22 07:18:21 +00:00
|
|
|
|
|
|
|
import (
|
2023-03-14 14:31:15 +00:00
|
|
|
"context"
|
2022-04-22 07:18:21 +00:00
|
|
|
"testing"
|
2024-01-17 14:26:02 +00:00
|
|
|
"time"
|
2022-04-22 07:18:21 +00:00
|
|
|
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/data"
|
2023-03-14 14:31:15 +00:00
|
|
|
cidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id/test"
|
2024-07-18 13:40:55 +00:00
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2023-03-14 14:31:15 +00:00
|
|
|
oidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id/test"
|
2024-01-17 14:26:02 +00:00
|
|
|
usertest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user/test"
|
2024-02-12 12:28:55 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2022-04-22 07:18:21 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-04-03 14:26:17 +00:00
|
|
|
"go.uber.org/zap/zaptest"
|
2022-04-22 07:18:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestLockConfigurationEncoding(t *testing.T) {
|
|
|
|
for _, tc := range []struct {
|
|
|
|
name string
|
|
|
|
encoded string
|
|
|
|
expectedEncoded string
|
|
|
|
expected data.ObjectLockConfiguration
|
|
|
|
error bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "empty",
|
|
|
|
encoded: "",
|
|
|
|
expectedEncoded: "",
|
|
|
|
expected: data.ObjectLockConfiguration{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Enabled",
|
|
|
|
encoded: "Enabled",
|
|
|
|
expectedEncoded: "Enabled",
|
|
|
|
expected: data.ObjectLockConfiguration{
|
|
|
|
ObjectLockEnabled: "Enabled",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Fully enabled",
|
|
|
|
encoded: "Enabled,10,COMPLIANCE,",
|
|
|
|
expectedEncoded: "Enabled,10,COMPLIANCE,0",
|
|
|
|
expected: data.ObjectLockConfiguration{
|
|
|
|
ObjectLockEnabled: "Enabled",
|
|
|
|
Rule: &data.ObjectLockRule{
|
|
|
|
DefaultRetention: &data.DefaultRetention{
|
|
|
|
Days: 10,
|
|
|
|
Mode: "COMPLIANCE",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Missing numbers",
|
|
|
|
encoded: "Enabled,,COMPLIANCE,",
|
|
|
|
expectedEncoded: "Enabled,0,COMPLIANCE,0",
|
|
|
|
expected: data.ObjectLockConfiguration{
|
|
|
|
ObjectLockEnabled: "Enabled",
|
|
|
|
Rule: &data.ObjectLockRule{
|
|
|
|
DefaultRetention: &data.DefaultRetention{
|
|
|
|
Mode: "COMPLIANCE",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Missing all",
|
|
|
|
encoded: ",,,",
|
|
|
|
expectedEncoded: ",0,,0",
|
|
|
|
expected: data.ObjectLockConfiguration{Rule: &data.ObjectLockRule{DefaultRetention: &data.DefaultRetention{}}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Invalid args",
|
|
|
|
encoded: ",,",
|
|
|
|
error: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Invalid days",
|
|
|
|
encoded: ",a,,",
|
|
|
|
error: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Invalid years",
|
|
|
|
encoded: ",,,b",
|
|
|
|
error: true,
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
lockConfiguration, err := parseLockConfiguration(tc.encoded)
|
|
|
|
if tc.error {
|
|
|
|
require.Error(t, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tc.expected, *lockConfiguration)
|
|
|
|
|
|
|
|
encoded := encodeLockConfiguration(lockConfiguration)
|
|
|
|
require.Equal(t, tc.expectedEncoded, encoded)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-09-13 10:35:30 +00:00
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func TestTreeServiceSettings(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
memCli, err := NewTreeServiceClientMemory()
|
|
|
|
require.NoError(t, err)
|
2023-04-03 14:26:17 +00:00
|
|
|
treeService := NewTree(memCli, zaptest.NewLogger(t))
|
2023-03-14 14:31:15 +00:00
|
|
|
|
|
|
|
bktInfo := &data.BucketInfo{
|
|
|
|
CID: cidtest.ID(),
|
|
|
|
}
|
|
|
|
|
2024-02-12 12:28:55 +00:00
|
|
|
key, err := keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
settings := &data.BucketSettings{
|
|
|
|
Versioning: "Versioning",
|
|
|
|
LockConfiguration: &data.ObjectLockConfiguration{
|
|
|
|
ObjectLockEnabled: "Enabled",
|
|
|
|
Rule: &data.ObjectLockRule{
|
|
|
|
DefaultRetention: &data.DefaultRetention{
|
|
|
|
Days: 1,
|
|
|
|
Mode: "mode",
|
|
|
|
},
|
|
|
|
},
|
2022-09-13 10:35:30 +00:00
|
|
|
},
|
2024-02-12 12:28:55 +00:00
|
|
|
OwnerKey: key.PublicKey(),
|
2023-03-14 14:31:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = treeService.PutSettingsNode(ctx, bktInfo, settings)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
storedSettings, err := treeService.GetSettingsNode(ctx, bktInfo)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, settings, storedSettings)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTreeServiceAddVersion(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
memCli, err := NewTreeServiceClientMemory()
|
|
|
|
require.NoError(t, err)
|
2023-04-03 14:26:17 +00:00
|
|
|
treeService := NewTree(memCli, zaptest.NewLogger(t))
|
2023-03-14 14:31:15 +00:00
|
|
|
|
|
|
|
bktInfo := &data.BucketInfo{
|
|
|
|
CID: cidtest.ID(),
|
|
|
|
}
|
|
|
|
|
2024-03-01 14:11:07 +00:00
|
|
|
userID := usertest.ID()
|
|
|
|
|
2024-01-17 14:26:02 +00:00
|
|
|
now := time.Now()
|
2023-03-14 14:31:15 +00:00
|
|
|
version := &data.NodeVersion{
|
|
|
|
BaseNodeVersion: data.BaseNodeVersion{
|
|
|
|
OID: oidtest.ID(),
|
|
|
|
Size: 10,
|
|
|
|
ETag: "etag",
|
|
|
|
FilePath: "path/to/version",
|
2024-03-01 14:11:07 +00:00
|
|
|
Owner: &userID,
|
2024-01-17 14:26:02 +00:00
|
|
|
Created: &now,
|
2022-09-13 10:35:30 +00:00
|
|
|
},
|
2023-03-14 14:31:15 +00:00
|
|
|
IsUnversioned: true,
|
2022-09-13 10:35:30 +00:00
|
|
|
}
|
2023-03-14 14:31:15 +00:00
|
|
|
|
|
|
|
nodeID, err := treeService.AddVersion(ctx, bktInfo, version)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
storedNode, err := treeService.GetUnversioned(ctx, bktInfo, "path/to/version")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, nodeID, storedNode.ID)
|
|
|
|
require.Equal(t, version.BaseNodeVersion.Size, storedNode.Size)
|
|
|
|
require.Equal(t, version.BaseNodeVersion.ETag, storedNode.ETag)
|
|
|
|
require.Equal(t, version.BaseNodeVersion.ETag, storedNode.ETag)
|
|
|
|
require.Equal(t, version.BaseNodeVersion.FilePath, storedNode.FilePath)
|
|
|
|
require.Equal(t, version.BaseNodeVersion.OID, storedNode.OID)
|
|
|
|
|
|
|
|
versions, err := treeService.GetVersions(ctx, bktInfo, "path/to/version")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, versions, 1)
|
|
|
|
require.Equal(t, storedNode, versions[0])
|
2022-09-13 10:35:30 +00:00
|
|
|
}
|
2023-09-28 09:12:29 +00:00
|
|
|
|
|
|
|
func TestGetLatestNode(t *testing.T) {
|
|
|
|
for _, tc := range []struct {
|
|
|
|
name string
|
|
|
|
nodes []NodeResponse
|
2024-05-20 16:19:40 +00:00
|
|
|
expectedNodeID uint64
|
2023-09-28 09:12:29 +00:00
|
|
|
error bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "empty",
|
|
|
|
nodes: []NodeResponse{},
|
|
|
|
error: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "one node of the object version",
|
|
|
|
nodes: []NodeResponse{
|
|
|
|
nodeResponse{
|
|
|
|
nodeID: 1,
|
|
|
|
parentID: 0,
|
|
|
|
timestamp: 1,
|
|
|
|
meta: []nodeMeta{
|
|
|
|
{
|
|
|
|
key: oidKV,
|
|
|
|
value: []byte(oidtest.ID().String()),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-05-20 16:19:40 +00:00
|
|
|
expectedNodeID: 1,
|
2023-09-28 09:12:29 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "one node of the object version and one node of the secondary object",
|
|
|
|
nodes: []NodeResponse{
|
|
|
|
nodeResponse{
|
|
|
|
nodeID: 2,
|
|
|
|
parentID: 0,
|
|
|
|
timestamp: 3,
|
|
|
|
meta: []nodeMeta{},
|
|
|
|
},
|
|
|
|
nodeResponse{
|
|
|
|
nodeID: 1,
|
|
|
|
parentID: 0,
|
|
|
|
timestamp: 1,
|
|
|
|
meta: []nodeMeta{
|
|
|
|
{
|
|
|
|
key: oidKV,
|
|
|
|
value: []byte(oidtest.ID().String()),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-05-20 16:19:40 +00:00
|
|
|
expectedNodeID: 1,
|
2023-09-28 09:12:29 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "all nodes represent a secondary object",
|
|
|
|
nodes: []NodeResponse{
|
|
|
|
nodeResponse{
|
|
|
|
nodeID: 2,
|
|
|
|
parentID: 0,
|
|
|
|
timestamp: 3,
|
|
|
|
meta: []nodeMeta{},
|
|
|
|
},
|
|
|
|
nodeResponse{
|
|
|
|
nodeID: 4,
|
|
|
|
parentID: 0,
|
|
|
|
timestamp: 5,
|
|
|
|
meta: []nodeMeta{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
error: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "several nodes of different types and with different timestamp",
|
|
|
|
nodes: []NodeResponse{
|
|
|
|
nodeResponse{
|
|
|
|
nodeID: 1,
|
|
|
|
parentID: 0,
|
|
|
|
timestamp: 1,
|
|
|
|
meta: []nodeMeta{
|
|
|
|
{
|
|
|
|
key: oidKV,
|
|
|
|
value: []byte(oidtest.ID().String()),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
nodeResponse{
|
|
|
|
nodeID: 3,
|
|
|
|
parentID: 0,
|
|
|
|
timestamp: 3,
|
|
|
|
meta: []nodeMeta{},
|
|
|
|
},
|
|
|
|
nodeResponse{
|
|
|
|
nodeID: 4,
|
|
|
|
parentID: 0,
|
|
|
|
timestamp: 4,
|
|
|
|
meta: []nodeMeta{
|
|
|
|
{
|
|
|
|
key: oidKV,
|
|
|
|
value: []byte(oidtest.ID().String()),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
nodeResponse{
|
|
|
|
nodeID: 6,
|
|
|
|
parentID: 0,
|
|
|
|
timestamp: 6,
|
|
|
|
meta: []nodeMeta{},
|
|
|
|
},
|
|
|
|
},
|
2024-05-20 16:19:40 +00:00
|
|
|
expectedNodeID: 4,
|
2023-09-28 09:12:29 +00:00
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2024-07-12 12:27:00 +00:00
|
|
|
actualNode, err := getLatestVersionNode(tc.nodes)
|
2023-09-28 09:12:29 +00:00
|
|
|
if tc.error {
|
|
|
|
require.Error(t, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
2024-07-12 12:27:00 +00:00
|
|
|
require.EqualValues(t, []uint64{tc.expectedNodeID}, actualNode.GetNodeID())
|
2023-09-28 09:12:29 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-07-18 13:40:55 +00:00
|
|
|
|
|
|
|
func TestSplitTreeMultiparts(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
memCli, err := NewTreeServiceClientMemory()
|
|
|
|
require.NoError(t, err)
|
|
|
|
treeService := NewTree(memCli, zaptest.NewLogger(t))
|
|
|
|
|
|
|
|
bktInfo := &data.BucketInfo{
|
|
|
|
CID: cidtest.ID(),
|
|
|
|
}
|
|
|
|
|
|
|
|
multipartInfo := &data.MultipartInfo{
|
|
|
|
Key: "multipart",
|
|
|
|
UploadID: "id",
|
|
|
|
Meta: map[string]string{},
|
|
|
|
Owner: usertest.ID(),
|
|
|
|
}
|
|
|
|
|
|
|
|
err = treeService.CreateMultipartUpload(ctx, bktInfo, multipartInfo)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
multipartInfo, err = treeService.GetMultipartUpload(ctx, bktInfo, multipartInfo.Key, multipartInfo.UploadID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var objIDs []oid.ID
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
objID := oidtest.ID()
|
|
|
|
_, err = memCli.AddNode(ctx, bktInfo, systemTree, multipartInfo.ID, map[string]string{
|
|
|
|
partNumberKV: "1",
|
|
|
|
oidKV: objID.EncodeToString(),
|
|
|
|
ownerKV: usertest.ID().EncodeToString(),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
objIDs = append(objIDs, objID)
|
|
|
|
}
|
|
|
|
|
|
|
|
parts, err := treeService.GetParts(ctx, bktInfo, multipartInfo.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, parts, 2)
|
|
|
|
|
|
|
|
objToDeletes, err := treeService.AddPart(ctx, bktInfo, multipartInfo.ID, &data.PartInfo{
|
|
|
|
Key: multipartInfo.Key,
|
|
|
|
UploadID: multipartInfo.UploadID,
|
|
|
|
Number: 1,
|
|
|
|
OID: oidtest.ID(),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.EqualValues(t, objIDs, objToDeletes, "oids to delete mismatched")
|
|
|
|
|
|
|
|
parts, err = treeService.GetParts(ctx, bktInfo, multipartInfo.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, parts, 1)
|
|
|
|
}
|