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"
|
|
|
|
"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"
|
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 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
|
|
|
|
}
|
|
|
|
|
2022-08-13 10:03:49 +00:00
|
|
|
func newTestNodeVersion(id oid.ID, name string) *data.NodeVersion {
|
|
|
|
return &data.NodeVersion{
|
|
|
|
BaseNodeVersion: data.BaseNodeVersion{
|
|
|
|
OID: id,
|
|
|
|
FilePath: name,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTryDirectory(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
|
2022-08-13 10:03:49 +00:00
|
|
|
node *data.NodeVersion
|
2021-06-29 09:59:33 +00:00
|
|
|
delimiter string
|
2021-01-14 17:39:48 +00:00
|
|
|
}{
|
|
|
|
{
|
2021-06-29 09:59:33 +00:00
|
|
|
name: "small.jpg",
|
2022-08-13 10:03:49 +00:00
|
|
|
result: nil,
|
|
|
|
node: newTestNodeVersion(id, "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-08-13 10:03:49 +00:00
|
|
|
node: newTestNodeVersion(id, "small.jpg"),
|
2021-01-14 17:39:48 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-29 09:59:33 +00:00
|
|
|
name: "small.jpg delimiter",
|
|
|
|
delimiter: "/",
|
2022-08-13 10:03:49 +00:00
|
|
|
result: nil,
|
|
|
|
node: newTestNodeVersion(id, "small.jpg"),
|
2021-01-14 17:39:48 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-29 09:59:33 +00:00
|
|
|
name: "test/small.jpg",
|
2022-08-13 10:03:49 +00:00
|
|
|
result: nil,
|
|
|
|
node: newTestNodeVersion(id, "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-08-13 10:03:49 +00:00
|
|
|
result: nil,
|
|
|
|
node: newTestNodeVersion(id, "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-08-13 10:03:49 +00:00
|
|
|
result: nil,
|
|
|
|
node: newTestNodeVersion(id, "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),
|
2022-08-13 10:03:49 +00:00
|
|
|
node: newTestNodeVersion(id, "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),
|
2022-08-13 10:03:49 +00:00
|
|
|
node: newTestNodeVersion(id, "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-08-13 10:03:49 +00:00
|
|
|
result: nil,
|
|
|
|
node: newTestNodeVersion(id, "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),
|
2022-08-13 10:03:49 +00:00
|
|
|
node: newTestNodeVersion(id, "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) {
|
2022-08-13 10:03:49 +00:00
|
|
|
info := tryDirectory(bkt, tc.node, tc.prefix, tc.delimiter)
|
|
|
|
if tc.result != nil {
|
|
|
|
tc.result.Created = time.Time{}
|
|
|
|
tc.result.Owner = user.ID{}
|
|
|
|
}
|
|
|
|
|
2021-01-14 17:39:48 +00:00
|
|
|
require.Equal(t, tc.result, info)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|