2022-03-01 15:07:15 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/xml"
|
2022-06-01 14:50:30 +00:00
|
|
|
"io"
|
2022-03-01 15:07:15 +00:00
|
|
|
"math/rand"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2022-06-15 12:17:29 +00:00
|
|
|
"net/url"
|
2022-06-01 14:50:30 +00:00
|
|
|
"strconv"
|
2022-03-01 15:07:15 +00:00
|
|
|
"testing"
|
2022-06-01 14:50:30 +00:00
|
|
|
"time"
|
2022-03-01 15:07:15 +00:00
|
|
|
|
2022-12-14 16:58:00 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-s3-gw/api"
|
|
|
|
"github.com/TrueCloudLab/frostfs-s3-gw/api/data"
|
|
|
|
"github.com/TrueCloudLab/frostfs-s3-gw/api/layer"
|
|
|
|
"github.com/TrueCloudLab/frostfs-s3-gw/api/resolver"
|
|
|
|
cid "github.com/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
"github.com/TrueCloudLab/frostfs-sdk-go/netmap"
|
|
|
|
"github.com/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
oid "github.com/TrueCloudLab/frostfs-sdk-go/object/id"
|
|
|
|
"github.com/TrueCloudLab/frostfs-sdk-go/user"
|
2022-03-04 13:07:27 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2022-03-01 15:07:15 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-04-07 14:36:44 +00:00
|
|
|
"go.uber.org/zap"
|
2022-03-01 15:07:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type handlerContext struct {
|
2022-10-04 08:31:09 +00:00
|
|
|
owner user.ID
|
|
|
|
t *testing.T
|
2022-06-24 12:39:30 +00:00
|
|
|
h *handler
|
|
|
|
tp *layer.TestNeoFS
|
|
|
|
context context.Context
|
2022-03-01 15:07:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (hc *handlerContext) Handler() *handler {
|
|
|
|
return hc.h
|
|
|
|
}
|
|
|
|
|
2022-06-06 11:09:09 +00:00
|
|
|
func (hc *handlerContext) MockedPool() *layer.TestNeoFS {
|
2022-03-01 15:07:15 +00:00
|
|
|
return hc.tp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hc *handlerContext) Layer() layer.Client {
|
|
|
|
return hc.h.obj
|
|
|
|
}
|
|
|
|
|
2022-06-24 12:39:30 +00:00
|
|
|
func (hc *handlerContext) Context() context.Context {
|
|
|
|
return hc.context
|
|
|
|
}
|
|
|
|
|
2022-11-14 09:37:47 +00:00
|
|
|
type placementPolicyMock struct {
|
|
|
|
defaultPolicy netmap.PlacementPolicy
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *placementPolicyMock) Default() netmap.PlacementPolicy {
|
|
|
|
return p.defaultPolicy
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *placementPolicyMock) Get(string) (netmap.PlacementPolicy, bool) {
|
|
|
|
return netmap.PlacementPolicy{}, false
|
|
|
|
}
|
|
|
|
|
2022-03-01 15:07:15 +00:00
|
|
|
func prepareHandlerContext(t *testing.T) *handlerContext {
|
|
|
|
key, err := keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-06-15 12:17:29 +00:00
|
|
|
l := zap.NewExample()
|
2022-06-06 11:09:09 +00:00
|
|
|
tp := layer.NewTestNeoFS()
|
2022-03-01 15:07:15 +00:00
|
|
|
|
2022-09-12 13:46:55 +00:00
|
|
|
testResolver := &resolver.Resolver{Name: "test_resolver"}
|
2022-06-27 09:08:26 +00:00
|
|
|
testResolver.SetResolveFunc(func(_ context.Context, name string) (cid.ID, error) {
|
2022-03-04 13:07:27 +00:00
|
|
|
return tp.ContainerID(name)
|
2022-03-01 15:07:15 +00:00
|
|
|
})
|
|
|
|
|
2022-10-04 08:31:09 +00:00
|
|
|
var owner user.ID
|
|
|
|
user.IDFromKey(&owner, key.PrivateKey.PublicKey)
|
|
|
|
|
2022-03-01 15:07:15 +00:00
|
|
|
layerCfg := &layer.Config{
|
2022-05-20 08:26:35 +00:00
|
|
|
Caches: layer.DefaultCachesConfigs(zap.NewExample()),
|
|
|
|
AnonKey: layer.AnonymousKey{Key: key},
|
|
|
|
Resolver: testResolver,
|
2022-05-31 08:12:53 +00:00
|
|
|
TreeService: layer.NewTreeService(),
|
2022-03-01 15:07:15 +00:00
|
|
|
}
|
|
|
|
|
2022-11-14 09:37:47 +00:00
|
|
|
var pp netmap.PlacementPolicy
|
|
|
|
err = pp.DecodeString("REP 1")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-03-01 15:07:15 +00:00
|
|
|
h := &handler{
|
|
|
|
log: l,
|
|
|
|
obj: layer.NewLayer(l, tp, layerCfg),
|
2022-08-10 18:54:24 +00:00
|
|
|
cfg: &Config{
|
2022-11-09 10:07:18 +00:00
|
|
|
Policy: &placementPolicyMock{defaultPolicy: pp},
|
2022-08-10 18:54:24 +00:00
|
|
|
},
|
2022-03-01 15:07:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &handlerContext{
|
2022-10-04 08:31:09 +00:00
|
|
|
owner: owner,
|
|
|
|
t: t,
|
2022-06-24 12:39:30 +00:00
|
|
|
h: h,
|
|
|
|
tp: tp,
|
2022-10-04 08:31:09 +00:00
|
|
|
context: context.WithValue(context.Background(), api.BoxData, newTestAccessBox(t, key)),
|
2022-03-01 15:07:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-04 08:31:09 +00:00
|
|
|
func createTestBucket(hc *handlerContext, bktName string) *data.BucketInfo {
|
|
|
|
_, err := hc.MockedPool().CreateContainer(hc.Context(), layer.PrmContainerCreate{
|
|
|
|
Creator: hc.owner,
|
2022-06-29 14:43:52 +00:00
|
|
|
Name: bktName,
|
2022-03-04 13:07:27 +00:00
|
|
|
})
|
2022-10-04 08:31:09 +00:00
|
|
|
require.NoError(hc.t, err)
|
2022-08-01 16:52:09 +00:00
|
|
|
|
2022-10-04 08:31:09 +00:00
|
|
|
bktInfo, err := hc.Layer().GetBucketInfo(hc.Context(), bktName)
|
|
|
|
require.NoError(hc.t, err)
|
2022-08-01 16:52:09 +00:00
|
|
|
return bktInfo
|
2022-03-01 15:07:15 +00:00
|
|
|
}
|
|
|
|
|
2022-10-04 08:31:09 +00:00
|
|
|
func createTestBucketWithLock(hc *handlerContext, bktName string, conf *data.ObjectLockConfiguration) *data.BucketInfo {
|
|
|
|
cnrID, err := hc.MockedPool().CreateContainer(hc.Context(), layer.PrmContainerCreate{
|
|
|
|
Creator: hc.owner,
|
2022-03-04 13:07:27 +00:00
|
|
|
Name: bktName,
|
|
|
|
AdditionalAttributes: [][2]string{{layer.AttributeLockEnabled, "true"}},
|
|
|
|
})
|
2022-10-04 08:31:09 +00:00
|
|
|
require.NoError(hc.t, err)
|
2022-03-01 15:07:15 +00:00
|
|
|
|
2022-04-25 09:57:58 +00:00
|
|
|
var ownerID user.ID
|
|
|
|
|
2022-03-18 13:04:09 +00:00
|
|
|
bktInfo := &data.BucketInfo{
|
2022-06-27 09:08:26 +00:00
|
|
|
CID: cnrID,
|
2022-03-18 13:04:09 +00:00
|
|
|
Name: bktName,
|
|
|
|
ObjectLockEnabled: true,
|
2022-05-25 17:25:43 +00:00
|
|
|
Owner: ownerID,
|
2022-03-18 13:04:09 +00:00
|
|
|
}
|
|
|
|
|
2022-03-01 15:07:15 +00:00
|
|
|
sp := &layer.PutSettingsParams{
|
2022-03-18 13:04:09 +00:00
|
|
|
BktInfo: bktInfo,
|
2022-03-01 15:07:15 +00:00
|
|
|
Settings: &data.BucketSettings{
|
2022-07-20 10:30:19 +00:00
|
|
|
Versioning: data.VersioningEnabled,
|
2022-03-01 15:07:15 +00:00
|
|
|
LockConfiguration: conf,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-10-04 08:31:09 +00:00
|
|
|
err = hc.Layer().PutBucketSettings(hc.Context(), sp)
|
|
|
|
require.NoError(hc.t, err)
|
2022-03-18 13:04:09 +00:00
|
|
|
|
|
|
|
return bktInfo
|
2022-03-01 15:07:15 +00:00
|
|
|
}
|
|
|
|
|
2022-10-04 08:31:09 +00:00
|
|
|
func createTestObject(hc *handlerContext, bktInfo *data.BucketInfo, objName string) *data.ObjectInfo {
|
2022-03-01 15:07:15 +00:00
|
|
|
content := make([]byte, 1024)
|
|
|
|
_, err := rand.Read(content)
|
2022-10-04 08:31:09 +00:00
|
|
|
require.NoError(hc.t, err)
|
2022-03-01 15:07:15 +00:00
|
|
|
|
2022-06-01 14:50:30 +00:00
|
|
|
header := map[string]string{
|
|
|
|
object.AttributeTimestamp: strconv.FormatInt(time.Now().UTC().Unix(), 10),
|
|
|
|
}
|
|
|
|
|
2022-10-14 14:36:43 +00:00
|
|
|
extObjInfo, err := hc.Layer().PutObject(hc.Context(), &layer.PutObjectParams{
|
2022-03-18 13:04:09 +00:00
|
|
|
BktInfo: bktInfo,
|
|
|
|
Object: objName,
|
|
|
|
Size: int64(len(content)),
|
|
|
|
Reader: bytes.NewReader(content),
|
2022-06-01 14:50:30 +00:00
|
|
|
Header: header,
|
2022-03-01 15:07:15 +00:00
|
|
|
})
|
2022-10-04 08:31:09 +00:00
|
|
|
require.NoError(hc.t, err)
|
2022-06-15 12:17:29 +00:00
|
|
|
|
2022-10-14 14:36:43 +00:00
|
|
|
return extObjInfo.ObjectInfo
|
2022-03-01 15:07:15 +00:00
|
|
|
}
|
|
|
|
|
2022-10-04 08:31:09 +00:00
|
|
|
func prepareTestRequest(hc *handlerContext, bktName, objName string, body interface{}) (*httptest.ResponseRecorder, *http.Request) {
|
|
|
|
return prepareTestFullRequest(hc, bktName, objName, make(url.Values), body)
|
2022-06-15 12:17:29 +00:00
|
|
|
}
|
|
|
|
|
2022-10-04 08:31:09 +00:00
|
|
|
func prepareTestFullRequest(hc *handlerContext, bktName, objName string, query url.Values, body interface{}) (*httptest.ResponseRecorder, *http.Request) {
|
2022-03-01 15:07:15 +00:00
|
|
|
rawBody, err := xml.Marshal(body)
|
2022-10-04 08:31:09 +00:00
|
|
|
require.NoError(hc.t, err)
|
2022-03-01 15:07:15 +00:00
|
|
|
|
2022-10-04 08:31:09 +00:00
|
|
|
return prepareTestRequestWithQuery(hc, bktName, objName, query, rawBody)
|
2022-08-01 16:52:09 +00:00
|
|
|
}
|
|
|
|
|
2022-10-04 08:31:09 +00:00
|
|
|
func prepareTestRequestWithQuery(hc *handlerContext, bktName, objName string, query url.Values, body []byte) (*httptest.ResponseRecorder, *http.Request) {
|
2022-03-01 15:07:15 +00:00
|
|
|
w := httptest.NewRecorder()
|
2022-08-01 16:52:09 +00:00
|
|
|
r := httptest.NewRequest(http.MethodPut, defaultURL, bytes.NewReader(body))
|
2022-06-15 12:17:29 +00:00
|
|
|
r.URL.RawQuery = query.Encode()
|
2022-03-01 15:07:15 +00:00
|
|
|
|
|
|
|
reqInfo := api.NewReqInfo(w, r, api.ObjectRequest{Bucket: bktName, Object: objName})
|
2022-10-04 08:31:09 +00:00
|
|
|
r = r.WithContext(api.SetReqInfo(hc.Context(), reqInfo))
|
2022-03-01 15:07:15 +00:00
|
|
|
|
|
|
|
return w, r
|
|
|
|
}
|
2022-06-01 14:50:30 +00:00
|
|
|
|
2022-10-04 08:31:09 +00:00
|
|
|
func prepareTestPayloadRequest(hc *handlerContext, bktName, objName string, payload io.Reader) (*httptest.ResponseRecorder, *http.Request) {
|
2022-06-01 09:54:27 +00:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r := httptest.NewRequest(http.MethodPut, defaultURL, payload)
|
|
|
|
|
|
|
|
reqInfo := api.NewReqInfo(w, r, api.ObjectRequest{Bucket: bktName, Object: objName})
|
2022-10-04 08:31:09 +00:00
|
|
|
r = r.WithContext(api.SetReqInfo(hc.Context(), reqInfo))
|
2022-06-01 09:54:27 +00:00
|
|
|
|
|
|
|
return w, r
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseTestResponse(t *testing.T, response *httptest.ResponseRecorder, body interface{}) {
|
|
|
|
assertStatus(t, response, http.StatusOK)
|
|
|
|
err := xml.NewDecoder(response.Result().Body).Decode(body)
|
|
|
|
require.NoError(t, err)
|
2022-06-01 14:50:30 +00:00
|
|
|
}
|
2022-06-24 12:39:30 +00:00
|
|
|
|
|
|
|
func existInMockedNeoFS(tc *handlerContext, bktInfo *data.BucketInfo, objInfo *data.ObjectInfo) bool {
|
|
|
|
p := &layer.GetObjectParams{
|
|
|
|
BucketInfo: bktInfo,
|
|
|
|
ObjectInfo: objInfo,
|
|
|
|
Writer: io.Discard,
|
|
|
|
}
|
|
|
|
|
|
|
|
return tc.Layer().GetObject(tc.Context(), p) == nil
|
|
|
|
}
|
|
|
|
|
2022-08-22 08:02:58 +00:00
|
|
|
func listOIDsFromMockedNeoFS(t *testing.T, tc *handlerContext, bktName string) []oid.ID {
|
2022-06-24 12:39:30 +00:00
|
|
|
bktInfo, err := tc.Layer().GetBucketInfo(tc.Context(), bktName)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-08-22 08:02:58 +00:00
|
|
|
return tc.MockedPool().AllObjects(bktInfo.CID)
|
2022-06-24 12:39:30 +00:00
|
|
|
}
|
2022-08-01 16:52:09 +00:00
|
|
|
|
|
|
|
func assertStatus(t *testing.T, w *httptest.ResponseRecorder, status int) {
|
|
|
|
if w.Code != status {
|
|
|
|
resp, err := io.ReadAll(w.Result().Body)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Failf(t, "unexpected status", "expected: %d, actual: %d, resp: '%s'", status, w.Code, string(resp))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func readResponse(t *testing.T, w *httptest.ResponseRecorder, status int, model interface{}) {
|
|
|
|
assertStatus(t, w, status)
|
|
|
|
err := xml.NewDecoder(w.Result().Body).Decode(model)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|