2021-01-14 17:39:48 +00:00
|
|
|
package layer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-06-04 13:01:42 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-api-go/pkg/container/id"
|
2021-01-14 17:39:48 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/object"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
|
2021-09-10 06:56:56 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/data"
|
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)
|
|
|
|
)
|
|
|
|
|
2021-09-10 06:56:56 +00:00
|
|
|
func newTestObject(oid *object.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)
|
|
|
|
|
2021-01-14 17:39:48 +00:00
|
|
|
raw := object.NewRaw()
|
|
|
|
raw.SetID(oid)
|
|
|
|
raw.SetOwnerID(bkt.Owner)
|
|
|
|
raw.SetContainerID(bkt.CID)
|
|
|
|
raw.SetPayload(defaultTestPayload)
|
2021-07-07 15:49:59 +00:00
|
|
|
raw.SetAttributes(filename, created, contentType)
|
2021-01-14 17:39:48 +00:00
|
|
|
raw.SetPayloadSize(uint64(defaultTestPayloadLength))
|
|
|
|
|
|
|
|
return raw.Object()
|
|
|
|
}
|
|
|
|
|
2021-09-10 06:56:56 +00:00
|
|
|
func newTestInfo(oid *object.ID, bkt *data.BucketInfo, name string, isDir bool) *data.ObjectInfo {
|
|
|
|
info := &data.ObjectInfo{
|
2021-08-27 21:33:50 +00:00
|
|
|
ID: oid,
|
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),
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
uid := owner.NewID()
|
|
|
|
oid := object.NewID()
|
2021-06-04 13:01:42 +00:00
|
|
|
containerID := cid.New()
|
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",
|
2021-06-04 13:01:42 +00:00
|
|
|
CID: containerID,
|
2021-01-14 17:39:48 +00:00
|
|
|
Owner: uid,
|
|
|
|
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",
|
|
|
|
result: newTestInfo(oid, bkt, "small.jpg", false),
|
|
|
|
object: newTestObject(oid, 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,
|
|
|
|
object: newTestObject(oid, bkt, "small.jpg"),
|
2021-01-14 17:39:48 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-29 09:59:33 +00:00
|
|
|
name: "small.jpg delimiter",
|
|
|
|
delimiter: "/",
|
|
|
|
result: newTestInfo(oid, bkt, "small.jpg", false),
|
|
|
|
object: newTestObject(oid, bkt, "small.jpg"),
|
2021-01-14 17:39:48 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-29 09:59:33 +00:00
|
|
|
name: "test/small.jpg",
|
|
|
|
result: newTestInfo(oid, bkt, "test/small.jpg", false),
|
|
|
|
object: newTestObject(oid, 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: "/",
|
|
|
|
result: newTestInfo(oid, bkt, "test/small.jpg", false),
|
|
|
|
object: newTestObject(oid, 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",
|
|
|
|
result: newTestInfo(oid, bkt, "a/b/small.jpg", false),
|
|
|
|
object: newTestObject(oid, 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: "/",
|
|
|
|
result: newTestInfo(oid, bkt, "a/b/", true),
|
|
|
|
object: newTestObject(oid, 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: "/",
|
|
|
|
result: newTestInfo(oid, bkt, "a/b/", true),
|
|
|
|
object: newTestObject(oid, 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: "/",
|
|
|
|
result: newTestInfo(oid, bkt, "a/b/c/small.jpg", false),
|
|
|
|
object: newTestObject(oid, 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: "/",
|
|
|
|
result: newTestInfo(oid, bkt, "a/b/c/", true),
|
|
|
|
object: newTestObject(oid, 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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|