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"
|
|
|
|
|
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"
|
|
|
|
oidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id/test"
|
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(),
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
},
|
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(),
|
|
|
|
}
|
|
|
|
|
|
|
|
version := &data.NodeVersion{
|
|
|
|
BaseNodeVersion: data.BaseNodeVersion{
|
|
|
|
OID: oidtest.ID(),
|
|
|
|
Size: 10,
|
|
|
|
ETag: "etag",
|
|
|
|
FilePath: "path/to/version",
|
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
|
|
|
}
|