2021-01-14 17:39:48 +00:00
|
|
|
package layer
|
|
|
|
|
|
|
|
import (
|
2022-05-12 16:58:11 +00:00
|
|
|
"encoding/hex"
|
2021-01-14 17:39:48 +00:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-09-10 06:56:56 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/data"
|
2022-04-25 09:57:58 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/checksum"
|
2021-11-15 12:56:16 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object"
|
2022-02-08 16:54:04 +00:00
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2022-04-25 09:57:58 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/user"
|
2021-01-14 17:39:48 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
defaultTestCreated = time.Now()
|
|
|
|
defaultTestPayload = []byte("test object payload")
|
|
|
|
defaultTestPayloadLength = int64(len(defaultTestPayload))
|
|
|
|
defaultTestContentType = http.DetectContentType(defaultTestPayload)
|
|
|
|
)
|
|
|
|
|
2022-05-25 17:25:43 +00:00
|
|
|
func newTestObject(id oid.ID, bkt *data.BucketInfo, name string) *object.Object {
|
2021-01-14 17:39:48 +00:00
|
|
|
filename := object.NewAttribute()
|
|
|
|
filename.SetKey(object.AttributeFileName)
|
|
|
|
filename.SetValue(name)
|
|
|
|
|
|
|
|
created := object.NewAttribute()
|
|
|
|
created.SetKey(object.AttributeTimestamp)
|
|
|
|
created.SetValue(strconv.FormatInt(defaultTestCreated.Unix(), 10))
|
|
|
|
|
2021-07-07 15:49:59 +00:00
|
|
|
contentType := object.NewAttribute()
|
|
|
|
contentType.SetKey(object.AttributeContentType)
|
|
|
|
contentType.SetValue(defaultTestContentType)
|
|
|
|
|
2022-03-03 14:37:48 +00:00
|
|
|
obj := object.New()
|
2022-05-25 17:25:43 +00:00
|
|
|
obj.SetID(id)
|
|
|
|
obj.SetOwnerID(&bkt.Owner)
|
|
|
|
obj.SetContainerID(bkt.CID)
|
2022-03-03 14:37:48 +00:00
|
|
|
obj.SetPayload(defaultTestPayload)
|
2022-03-16 09:02:16 +00:00
|
|
|
obj.SetAttributes(*filename, *created, *contentType)
|
2022-03-03 14:37:48 +00:00
|
|
|
obj.SetPayloadSize(uint64(defaultTestPayloadLength))
|
2021-01-14 17:39:48 +00:00
|
|
|
|
2022-03-03 14:37:48 +00:00
|
|
|
return obj
|
2021-01-14 17:39:48 +00:00
|
|
|
}
|
|
|
|
|
2022-05-25 17:25:43 +00:00
|
|
|
func newTestInfo(obj oid.ID, bkt *data.BucketInfo, name string, isDir bool) *data.ObjectInfo {
|
2022-04-25 09:57:58 +00:00
|
|
|
var hashSum checksum.Checksum
|
2021-09-10 06:56:56 +00:00
|
|
|
info := &data.ObjectInfo{
|
2022-05-25 17:25:43 +00:00
|
|
|
ID: obj,
|
2021-01-14 17:39:48 +00:00
|
|
|
Name: name,
|
|
|
|
Bucket: bkt.Name,
|
2021-08-27 21:33:50 +00:00
|
|
|
CID: bkt.CID,
|
2021-01-14 17:39:48 +00:00
|
|
|
Size: defaultTestPayloadLength,
|
|
|
|
ContentType: defaultTestContentType,
|
|
|
|
Created: time.Unix(defaultTestCreated.Unix(), 0),
|
|
|
|
Owner: bkt.Owner,
|
|
|
|
Headers: make(map[string]string),
|
2022-05-12 16:58:11 +00:00
|
|
|
HashSum: hex.EncodeToString(hashSum.Value()),
|
2021-01-14 17:39:48 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 09:59:33 +00:00
|
|
|
if isDir {
|
2021-08-27 21:33:50 +00:00
|
|
|
info.IsDir = true
|
2021-01-14 17:39:48 +00:00
|
|
|
info.Size = 0
|
|
|
|
info.ContentType = ""
|
|
|
|
info.Headers = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return info
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_objectInfoFromMeta(t *testing.T) {
|
2022-04-25 09:57:58 +00:00
|
|
|
var uid user.ID
|
|
|
|
var id oid.ID
|
|
|
|
var containerID cid.ID
|
2021-01-14 17:39:48 +00:00
|
|
|
|
2021-09-10 06:56:56 +00:00
|
|
|
bkt := &data.BucketInfo{
|
2021-01-14 17:39:48 +00:00
|
|
|
Name: "test-container",
|
2022-05-25 17:25:43 +00:00
|
|
|
CID: containerID,
|
|
|
|
Owner: uid,
|
2021-01-14 17:39:48 +00:00
|
|
|
Created: time.Now(),
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []struct {
|
2021-06-29 09:59:33 +00:00
|
|
|
name string
|
|
|
|
prefix string
|
2021-09-10 06:56:56 +00:00
|
|
|
result *data.ObjectInfo
|
2021-06-29 09:59:33 +00:00
|
|
|
object *object.Object
|
|
|
|
delimiter string
|
2021-01-14 17:39:48 +00:00
|
|
|
}{
|
|
|
|
{
|
2021-06-29 09:59:33 +00:00
|
|
|
name: "small.jpg",
|
2022-05-25 17:25:43 +00:00
|
|
|
result: newTestInfo(id, bkt, "small.jpg", false),
|
|
|
|
object: newTestObject(id, bkt, "small.jpg"),
|
2021-01-14 17:39:48 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-29 09:59:33 +00:00
|
|
|
name: "small.jpg not matched prefix",
|
|
|
|
prefix: "big",
|
|
|
|
result: nil,
|
2022-05-25 17:25:43 +00:00
|
|
|
object: newTestObject(id, bkt, "small.jpg"),
|
2021-01-14 17:39:48 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-29 09:59:33 +00:00
|
|
|
name: "small.jpg delimiter",
|
|
|
|
delimiter: "/",
|
2022-05-25 17:25:43 +00:00
|
|
|
result: newTestInfo(id, bkt, "small.jpg", false),
|
|
|
|
object: newTestObject(id, bkt, "small.jpg"),
|
2021-01-14 17:39:48 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-29 09:59:33 +00:00
|
|
|
name: "test/small.jpg",
|
2022-05-25 17:25:43 +00:00
|
|
|
result: newTestInfo(id, bkt, "test/small.jpg", false),
|
|
|
|
object: newTestObject(id, bkt, "test/small.jpg"),
|
2021-01-14 17:39:48 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-29 09:59:33 +00:00
|
|
|
name: "test/small.jpg with prefix and delimiter",
|
|
|
|
prefix: "test/",
|
|
|
|
delimiter: "/",
|
2022-05-25 17:25:43 +00:00
|
|
|
result: newTestInfo(id, bkt, "test/small.jpg", false),
|
|
|
|
object: newTestObject(id, bkt, "test/small.jpg"),
|
2021-01-14 17:39:48 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-29 09:59:33 +00:00
|
|
|
name: "a/b/small.jpg",
|
|
|
|
prefix: "a",
|
2022-05-25 17:25:43 +00:00
|
|
|
result: newTestInfo(id, bkt, "a/b/small.jpg", false),
|
|
|
|
object: newTestObject(id, bkt, "a/b/small.jpg"),
|
2021-01-14 17:39:48 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-29 09:59:33 +00:00
|
|
|
name: "a/b/small.jpg",
|
|
|
|
prefix: "a/",
|
|
|
|
delimiter: "/",
|
2022-05-25 17:25:43 +00:00
|
|
|
result: newTestInfo(id, bkt, "a/b/", true),
|
|
|
|
object: newTestObject(id, bkt, "a/b/small.jpg"),
|
2021-01-14 17:39:48 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-29 09:59:33 +00:00
|
|
|
name: "a/b/c/small.jpg",
|
|
|
|
prefix: "a/",
|
|
|
|
delimiter: "/",
|
2022-05-25 17:25:43 +00:00
|
|
|
result: newTestInfo(id, bkt, "a/b/", true),
|
|
|
|
object: newTestObject(id, bkt, "a/b/c/small.jpg"),
|
2021-01-14 17:39:48 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-29 09:59:33 +00:00
|
|
|
name: "a/b/c/small.jpg",
|
|
|
|
prefix: "a/b/c/s",
|
|
|
|
delimiter: "/",
|
2022-05-25 17:25:43 +00:00
|
|
|
result: newTestInfo(id, bkt, "a/b/c/small.jpg", false),
|
|
|
|
object: newTestObject(id, bkt, "a/b/c/small.jpg"),
|
2021-01-14 17:39:48 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-29 09:59:33 +00:00
|
|
|
name: "a/b/c/big.jpg",
|
|
|
|
prefix: "a/b/",
|
|
|
|
delimiter: "/",
|
2022-05-25 17:25:43 +00:00
|
|
|
result: newTestInfo(id, bkt, "a/b/c/", true),
|
|
|
|
object: newTestObject(id, bkt, "a/b/c/big.jpg"),
|
2021-01-14 17:39:48 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
2021-06-29 09:59:33 +00:00
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
info := objectInfoFromMeta(bkt, tc.object, tc.prefix, tc.delimiter)
|
2021-01-14 17:39:48 +00:00
|
|
|
require.Equal(t, tc.result, info)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|