2023-03-24 09:25:49 +00:00
|
|
|
//go:build integration
|
|
|
|
|
2021-09-06 12:59:09 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-10-19 07:52:41 +00:00
|
|
|
"archive/zip"
|
2021-09-06 12:59:09 +00:00
|
|
|
"bytes"
|
|
|
|
"context"
|
2024-05-06 17:34:49 +00:00
|
|
|
"encoding/base64"
|
2021-09-06 12:59:09 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"mime/multipart"
|
|
|
|
"net/http"
|
2024-05-06 17:34:49 +00:00
|
|
|
"os"
|
2021-10-19 07:52:41 +00:00
|
|
|
"sort"
|
2021-09-06 12:59:09 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-03-16 08:40:08 +00:00
|
|
|
containerv2 "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/container"
|
2024-05-06 17:34:49 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/bearer"
|
2023-03-07 14:08:53 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/acl"
|
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
2024-05-06 17:34:49 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/eacl"
|
2023-03-07 14:08:53 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/pool"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
|
2021-09-06 12:59:09 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2024-05-06 17:34:49 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
2021-09-06 12:59:09 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/testcontainers/testcontainers-go"
|
|
|
|
"github.com/testcontainers/testcontainers-go/wait"
|
2023-11-28 08:29:08 +00:00
|
|
|
"go.uber.org/zap/zapcore"
|
2021-09-06 12:59:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type putResponse struct {
|
|
|
|
CID string `json:"container_id"`
|
|
|
|
OID string `json:"object_id"`
|
|
|
|
}
|
|
|
|
|
2022-04-20 09:17:20 +00:00
|
|
|
const (
|
2023-05-16 14:35:58 +00:00
|
|
|
testContainerName = "friendly"
|
|
|
|
testListenAddress = "localhost:8082"
|
|
|
|
testHost = "http://" + testListenAddress
|
2022-04-20 09:17:20 +00:00
|
|
|
)
|
|
|
|
|
2021-09-06 12:59:09 +00:00
|
|
|
func TestIntegration(t *testing.T) {
|
2021-11-01 06:54:09 +00:00
|
|
|
rootCtx := context.Background()
|
2023-05-16 14:35:58 +00:00
|
|
|
aioImage := "truecloudlab/frostfs-aio:"
|
2022-04-20 09:17:20 +00:00
|
|
|
versions := []string{
|
2024-05-06 17:34:49 +00:00
|
|
|
"1.2.7",
|
|
|
|
"1.3.0",
|
2024-06-25 13:12:30 +00:00
|
|
|
"1.5.0",
|
2022-04-20 09:17:20 +00:00
|
|
|
}
|
2021-09-06 12:59:09 +00:00
|
|
|
key, err := keys.NewPrivateKeyFromHex("1dd37fba80fec4e6a6f13fd708d8dcb3b29def768017052f6c930fa1c5d90bbb")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-05-06 17:34:49 +00:00
|
|
|
file, err := os.CreateTemp("", "wallet")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.Remove(file.Name())
|
|
|
|
makeTempWallet(t, key, file.Name())
|
|
|
|
|
2022-07-25 09:47:48 +00:00
|
|
|
var ownerID user.ID
|
|
|
|
user.IDFromKey(&ownerID, key.PrivateKey.PublicKey)
|
|
|
|
|
2021-09-06 12:59:09 +00:00
|
|
|
for _, version := range versions {
|
2021-11-01 06:54:09 +00:00
|
|
|
ctx, cancel2 := context.WithCancel(rootCtx)
|
|
|
|
|
2021-09-06 12:59:09 +00:00
|
|
|
aioContainer := createDockerContainer(ctx, t, aioImage+version)
|
2024-05-06 17:34:49 +00:00
|
|
|
server, cancel := runServer(file.Name())
|
2021-09-06 12:59:09 +00:00
|
|
|
clientPool := getPool(ctx, t, key)
|
2022-07-25 09:47:48 +00:00
|
|
|
CID, err := createContainer(ctx, t, clientPool, ownerID, version)
|
2022-03-01 20:38:03 +00:00
|
|
|
require.NoError(t, err, version)
|
2021-09-06 12:59:09 +00:00
|
|
|
|
2024-05-06 17:34:49 +00:00
|
|
|
token := makeBearerToken(t, key, ownerID, version)
|
|
|
|
|
2022-04-20 09:17:20 +00:00
|
|
|
t.Run("simple put "+version, func(t *testing.T) { simplePut(ctx, t, clientPool, CID, version) })
|
2024-05-06 17:34:49 +00:00
|
|
|
t.Run("put with bearer token in header"+version, func(t *testing.T) { putWithBearerTokenInHeader(ctx, t, clientPool, CID, token) })
|
|
|
|
t.Run("put with bearer token in cookie"+version, func(t *testing.T) { putWithBearerTokenInCookie(ctx, t, clientPool, CID, token) })
|
2022-11-17 15:01:38 +00:00
|
|
|
t.Run("put with duplicate keys "+version, func(t *testing.T) { putWithDuplicateKeys(t, CID) })
|
2022-07-25 09:47:48 +00:00
|
|
|
t.Run("simple get "+version, func(t *testing.T) { simpleGet(ctx, t, clientPool, ownerID, CID, version) })
|
|
|
|
t.Run("get by attribute "+version, func(t *testing.T) { getByAttr(ctx, t, clientPool, ownerID, CID, version) })
|
|
|
|
t.Run("get zip "+version, func(t *testing.T) { getZip(ctx, t, clientPool, ownerID, CID, version) })
|
2023-11-28 08:29:08 +00:00
|
|
|
t.Run("test namespaces "+version, func(t *testing.T) { checkNamespaces(ctx, t, clientPool, ownerID, CID, version) })
|
2021-09-06 12:59:09 +00:00
|
|
|
|
|
|
|
cancel()
|
2022-09-09 06:57:48 +00:00
|
|
|
server.Wait()
|
2021-09-06 12:59:09 +00:00
|
|
|
err = aioContainer.Terminate(ctx)
|
|
|
|
require.NoError(t, err)
|
2021-11-01 06:54:09 +00:00
|
|
|
cancel2()
|
2021-09-06 12:59:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-06 17:34:49 +00:00
|
|
|
func runServer(pathToWallet string) (App, context.CancelFunc) {
|
2021-09-06 12:59:09 +00:00
|
|
|
cancelCtx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
v := getDefaultConfig()
|
2024-05-06 17:34:49 +00:00
|
|
|
v.Set(cfgWalletPath, pathToWallet)
|
|
|
|
v.Set(cfgWalletPassphrase, "")
|
|
|
|
|
2023-11-28 08:29:08 +00:00
|
|
|
l, lvl := newStdoutLogger(zapcore.DebugLevel)
|
2022-09-08 14:57:22 +00:00
|
|
|
application := newApp(cancelCtx, WithConfig(v), WithLogger(l, lvl))
|
2023-08-31 08:37:03 +00:00
|
|
|
go application.Serve()
|
2021-09-06 12:59:09 +00:00
|
|
|
|
2022-09-09 06:57:48 +00:00
|
|
|
return application, cancel
|
2021-09-06 12:59:09 +00:00
|
|
|
}
|
|
|
|
|
2022-08-22 15:29:43 +00:00
|
|
|
func simplePut(ctx context.Context, t *testing.T, p *pool.Pool, CID cid.ID, version string) {
|
|
|
|
url := testHost + "/upload/" + CID.String()
|
2022-04-20 09:17:20 +00:00
|
|
|
makePutRequestAndCheck(ctx, t, p, CID, url)
|
|
|
|
|
2023-05-16 14:35:58 +00:00
|
|
|
url = testHost + "/upload/" + testContainerName
|
|
|
|
makePutRequestAndCheck(ctx, t, p, CID, url)
|
2022-04-20 09:17:20 +00:00
|
|
|
}
|
|
|
|
|
2024-05-06 17:34:49 +00:00
|
|
|
func putWithBearerTokenInHeader(ctx context.Context, t *testing.T, p *pool.Pool, CID cid.ID, token string) {
|
|
|
|
url := testHost + "/upload/" + CID.String()
|
|
|
|
|
|
|
|
request, content, attributes := makePutRequest(t, url)
|
|
|
|
request.Header.Set("Authorization", "Bearer "+token)
|
|
|
|
resp, err := http.DefaultClient.Do(request)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
checkPutResponse(ctx, t, p, CID, resp, content, attributes)
|
|
|
|
}
|
|
|
|
|
|
|
|
func putWithBearerTokenInCookie(ctx context.Context, t *testing.T, p *pool.Pool, CID cid.ID, token string) {
|
|
|
|
url := testHost + "/upload/" + CID.String()
|
|
|
|
|
|
|
|
request, content, attributes := makePutRequest(t, url)
|
|
|
|
request.AddCookie(&http.Cookie{Name: "Bearer", Value: token})
|
|
|
|
resp, err := http.DefaultClient.Do(request)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
checkPutResponse(ctx, t, p, CID, resp, content, attributes)
|
|
|
|
}
|
|
|
|
|
2022-08-22 15:29:43 +00:00
|
|
|
func makePutRequestAndCheck(ctx context.Context, t *testing.T, p *pool.Pool, cnrID cid.ID, url string) {
|
2024-05-06 17:34:49 +00:00
|
|
|
request, content, attributes := makePutRequest(t, url)
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(request)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
checkPutResponse(ctx, t, p, cnrID, resp, content, attributes)
|
|
|
|
}
|
|
|
|
|
|
|
|
func makePutRequest(t *testing.T, url string) (*http.Request, string, map[string]string) {
|
2021-09-06 12:59:09 +00:00
|
|
|
content := "content of file"
|
|
|
|
keyAttr, valAttr := "User-Attribute", "user value"
|
|
|
|
attributes := map[string]string{
|
|
|
|
object.AttributeFileName: "newFile.txt",
|
|
|
|
keyAttr: valAttr,
|
|
|
|
}
|
|
|
|
|
|
|
|
var buff bytes.Buffer
|
|
|
|
w := multipart.NewWriter(&buff)
|
|
|
|
fw, err := w.CreateFormFile("file", attributes[object.AttributeFileName])
|
|
|
|
require.NoError(t, err)
|
|
|
|
_, err = io.Copy(fw, bytes.NewBufferString(content))
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = w.Close()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-04-20 09:17:20 +00:00
|
|
|
request, err := http.NewRequest(http.MethodPost, url, &buff)
|
2021-09-06 12:59:09 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
request.Header.Set("Content-Type", w.FormDataContentType())
|
|
|
|
request.Header.Set("X-Attribute-"+keyAttr, valAttr)
|
|
|
|
|
2024-05-06 17:34:49 +00:00
|
|
|
return request, content, attributes
|
|
|
|
}
|
2022-04-20 09:17:20 +00:00
|
|
|
|
2024-05-06 17:34:49 +00:00
|
|
|
func checkPutResponse(ctx context.Context, t *testing.T, p *pool.Pool, cnrID cid.ID, resp *http.Response, content string, attributes map[string]string) {
|
2021-09-06 12:59:09 +00:00
|
|
|
defer func() {
|
2022-04-20 09:17:20 +00:00
|
|
|
err := resp.Body.Close()
|
2021-09-06 12:59:09 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}()
|
|
|
|
|
2022-04-20 09:17:20 +00:00
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
fmt.Println(string(body))
|
|
|
|
}
|
|
|
|
require.Equal(t, http.StatusOK, resp.StatusCode)
|
|
|
|
|
2021-09-06 12:59:09 +00:00
|
|
|
addr := &putResponse{}
|
2022-04-20 09:17:20 +00:00
|
|
|
err = json.Unmarshal(body, addr)
|
2021-09-06 12:59:09 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-04-20 09:17:20 +00:00
|
|
|
err = cnrID.DecodeString(addr.CID)
|
2021-09-06 12:59:09 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-08-22 15:29:43 +00:00
|
|
|
var id oid.ID
|
2022-04-19 15:46:51 +00:00
|
|
|
err = id.DecodeString(addr.OID)
|
2021-09-06 12:59:09 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-07-25 09:47:48 +00:00
|
|
|
var objectAddress oid.Address
|
2022-08-22 15:29:43 +00:00
|
|
|
objectAddress.SetContainer(cnrID)
|
|
|
|
objectAddress.SetObject(id)
|
2021-09-06 12:59:09 +00:00
|
|
|
|
|
|
|
payload := bytes.NewBuffer(nil)
|
2022-02-08 16:24:23 +00:00
|
|
|
|
2022-04-07 12:56:18 +00:00
|
|
|
var prm pool.PrmObjectGet
|
2022-07-25 09:47:48 +00:00
|
|
|
prm.SetAddress(objectAddress)
|
2022-04-07 12:56:18 +00:00
|
|
|
|
2022-04-20 09:17:20 +00:00
|
|
|
res, err := p.GetObject(ctx, prm)
|
2022-02-08 16:24:23 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = io.Copy(payload, res.Payload)
|
2021-09-06 12:59:09 +00:00
|
|
|
require.NoError(t, err)
|
2022-02-08 16:24:23 +00:00
|
|
|
|
2021-09-06 12:59:09 +00:00
|
|
|
require.Equal(t, content, payload.String())
|
|
|
|
|
2022-02-08 16:24:23 +00:00
|
|
|
for _, attribute := range res.Header.Attributes() {
|
2021-09-06 12:59:09 +00:00
|
|
|
require.Equal(t, attributes[attribute.Key()], attribute.Value())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-17 15:01:38 +00:00
|
|
|
func putWithDuplicateKeys(t *testing.T, CID cid.ID) {
|
|
|
|
url := testHost + "/upload/" + CID.String()
|
|
|
|
|
|
|
|
attr := "X-Attribute-User-Attribute"
|
|
|
|
content := "content of file"
|
|
|
|
valOne, valTwo := "first_value", "second_value"
|
|
|
|
fileName := "newFile.txt"
|
|
|
|
|
|
|
|
var buff bytes.Buffer
|
|
|
|
w := multipart.NewWriter(&buff)
|
|
|
|
fw, err := w.CreateFormFile("file", fileName)
|
|
|
|
require.NoError(t, err)
|
|
|
|
_, err = io.Copy(fw, bytes.NewBufferString(content))
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = w.Close()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
request, err := http.NewRequest(http.MethodPost, url, &buff)
|
|
|
|
require.NoError(t, err)
|
|
|
|
request.Header.Set("Content-Type", w.FormDataContentType())
|
|
|
|
request.Header.Add(attr, valOne)
|
|
|
|
request.Header.Add(attr, valTwo)
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(request)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err := resp.Body.Close()
|
|
|
|
require.NoError(t, err)
|
|
|
|
}()
|
|
|
|
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "key duplication error: "+attr+"\n", string(body))
|
|
|
|
require.Equal(t, http.StatusBadRequest, resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
2022-08-22 15:29:43 +00:00
|
|
|
func simpleGet(ctx context.Context, t *testing.T, clientPool *pool.Pool, ownerID user.ID, CID cid.ID, version string) {
|
2021-09-06 12:59:09 +00:00
|
|
|
content := "content of file"
|
|
|
|
attributes := map[string]string{
|
|
|
|
"some-attr": "some-get-value",
|
|
|
|
}
|
|
|
|
|
2022-07-25 09:47:48 +00:00
|
|
|
id := putObject(ctx, t, clientPool, ownerID, CID, content, attributes)
|
2021-09-06 12:59:09 +00:00
|
|
|
|
2022-08-22 15:29:43 +00:00
|
|
|
resp, err := http.Get(testHost + "/get/" + CID.String() + "/" + id.String())
|
2021-09-06 12:59:09 +00:00
|
|
|
require.NoError(t, err)
|
2022-04-20 09:17:20 +00:00
|
|
|
checkGetResponse(t, resp, content, attributes)
|
|
|
|
|
2023-05-16 14:35:58 +00:00
|
|
|
resp, err = http.Get(testHost + "/get/" + testContainerName + "/" + id.String())
|
|
|
|
require.NoError(t, err)
|
|
|
|
checkGetResponse(t, resp, content, attributes)
|
2022-04-20 09:17:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func checkGetResponse(t *testing.T, resp *http.Response, content string, attributes map[string]string) {
|
2021-09-06 12:59:09 +00:00
|
|
|
defer func() {
|
2022-04-20 09:17:20 +00:00
|
|
|
err := resp.Body.Close()
|
2021-09-06 12:59:09 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}()
|
|
|
|
|
|
|
|
data, err := io.ReadAll(resp.Body)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, content, string(data))
|
|
|
|
|
|
|
|
for k, v := range attributes {
|
|
|
|
require.Equal(t, v, resp.Header.Get("X-Attribute-"+k))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-25 09:47:48 +00:00
|
|
|
func checkGetByAttrResponse(t *testing.T, resp *http.Response, content string, attributes map[string]string) {
|
|
|
|
defer func() {
|
|
|
|
err := resp.Body.Close()
|
|
|
|
require.NoError(t, err)
|
|
|
|
}()
|
|
|
|
|
|
|
|
data, err := io.ReadAll(resp.Body)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, content, string(data))
|
|
|
|
|
|
|
|
for k, v := range attributes {
|
|
|
|
require.Equal(t, v, resp.Header.Get(k))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-22 15:29:43 +00:00
|
|
|
func getByAttr(ctx context.Context, t *testing.T, clientPool *pool.Pool, ownerID user.ID, CID cid.ID, version string) {
|
2021-09-06 12:59:09 +00:00
|
|
|
keyAttr, valAttr := "some-attr", "some-get-by-attr-value"
|
|
|
|
content := "content of file"
|
|
|
|
attributes := map[string]string{keyAttr: valAttr}
|
|
|
|
|
2022-07-25 09:47:48 +00:00
|
|
|
id := putObject(ctx, t, clientPool, ownerID, CID, content, attributes)
|
2021-09-06 12:59:09 +00:00
|
|
|
|
|
|
|
expectedAttr := map[string]string{
|
|
|
|
"X-Attribute-" + keyAttr: valAttr,
|
2022-02-08 16:24:23 +00:00
|
|
|
"x-object-id": id.String(),
|
2021-09-06 12:59:09 +00:00
|
|
|
"x-container-id": CID.String(),
|
|
|
|
}
|
|
|
|
|
2022-08-22 15:29:43 +00:00
|
|
|
resp, err := http.Get(testHost + "/get_by_attribute/" + CID.String() + "/" + keyAttr + "/" + valAttr)
|
2021-09-06 12:59:09 +00:00
|
|
|
require.NoError(t, err)
|
2022-04-20 09:17:20 +00:00
|
|
|
checkGetByAttrResponse(t, resp, content, expectedAttr)
|
|
|
|
|
2023-05-16 14:35:58 +00:00
|
|
|
resp, err = http.Get(testHost + "/get_by_attribute/" + testContainerName + "/" + keyAttr + "/" + valAttr)
|
|
|
|
require.NoError(t, err)
|
|
|
|
checkGetByAttrResponse(t, resp, content, expectedAttr)
|
2022-04-20 09:17:20 +00:00
|
|
|
}
|
|
|
|
|
2022-08-22 15:29:43 +00:00
|
|
|
func getZip(ctx context.Context, t *testing.T, clientPool *pool.Pool, ownerID user.ID, CID cid.ID, version string) {
|
2021-10-19 07:52:41 +00:00
|
|
|
names := []string{"zipfolder/dir/name1.txt", "zipfolder/name2.txt"}
|
|
|
|
contents := []string{"content of file1", "content of file2"}
|
2022-11-02 12:08:14 +00:00
|
|
|
attributes1 := map[string]string{object.AttributeFilePath: names[0]}
|
|
|
|
attributes2 := map[string]string{object.AttributeFilePath: names[1]}
|
2021-10-19 07:52:41 +00:00
|
|
|
|
2022-07-25 09:47:48 +00:00
|
|
|
putObject(ctx, t, clientPool, ownerID, CID, contents[0], attributes1)
|
|
|
|
putObject(ctx, t, clientPool, ownerID, CID, contents[1], attributes2)
|
2021-10-19 07:52:41 +00:00
|
|
|
|
2022-08-22 15:29:43 +00:00
|
|
|
baseURL := testHost + "/zip/" + CID.String()
|
2022-04-20 09:17:20 +00:00
|
|
|
makeZipTest(t, baseURL, names, contents)
|
2021-10-19 07:52:41 +00:00
|
|
|
|
2023-05-16 14:35:58 +00:00
|
|
|
baseURL = testHost + "/zip/" + testContainerName
|
|
|
|
makeZipTest(t, baseURL, names, contents)
|
2022-04-20 09:17:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func makeZipTest(t *testing.T, baseURL string, names, contents []string) {
|
|
|
|
url := baseURL + "/zipfolder"
|
|
|
|
makeZipRequest(t, url, names, contents)
|
2021-10-19 07:52:41 +00:00
|
|
|
|
|
|
|
// check nested folder
|
2022-04-20 09:17:20 +00:00
|
|
|
url = baseURL + "/zipfolder/dir"
|
|
|
|
makeZipRequest(t, url, names[:1], contents[:1])
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeZipRequest(t *testing.T, url string, names, contents []string) {
|
|
|
|
resp, err := http.Get(url)
|
2021-10-19 07:52:41 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer func() {
|
2022-04-20 09:17:20 +00:00
|
|
|
err := resp.Body.Close()
|
2021-10-19 07:52:41 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}()
|
|
|
|
|
2022-04-20 09:17:20 +00:00
|
|
|
data, err := io.ReadAll(resp.Body)
|
2021-10-19 07:52:41 +00:00
|
|
|
require.NoError(t, err)
|
2022-06-17 09:17:02 +00:00
|
|
|
checkZip(t, data, int64(len(data)), names, contents)
|
2021-10-19 07:52:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func checkZip(t *testing.T, data []byte, length int64, names, contents []string) {
|
|
|
|
readerAt := bytes.NewReader(data)
|
|
|
|
|
|
|
|
zipReader, err := zip.NewReader(readerAt, length)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, len(names), len(zipReader.File))
|
|
|
|
|
|
|
|
sort.Slice(zipReader.File, func(i, j int) bool {
|
|
|
|
return zipReader.File[i].FileHeader.Name < zipReader.File[j].FileHeader.Name
|
|
|
|
})
|
|
|
|
|
|
|
|
for i, f := range zipReader.File {
|
|
|
|
require.Equal(t, names[i], f.FileHeader.Name)
|
|
|
|
|
|
|
|
rc, err := f.Open()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
all, err := io.ReadAll(rc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, contents[i], string(all))
|
|
|
|
|
|
|
|
err = rc.Close()
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-28 08:29:08 +00:00
|
|
|
func checkNamespaces(ctx context.Context, t *testing.T, clientPool *pool.Pool, ownerID user.ID, CID cid.ID, version string) {
|
|
|
|
content := "content of file"
|
|
|
|
attributes := map[string]string{
|
|
|
|
"some-attr": "some-get-value",
|
|
|
|
}
|
|
|
|
|
|
|
|
id := putObject(ctx, t, clientPool, ownerID, CID, content, attributes)
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodGet, testHost+"/get/"+testContainerName+"/"+id.String(), nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
req.Header.Set(defaultNamespaceHeader, "")
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
checkGetResponse(t, resp, content, attributes)
|
|
|
|
|
|
|
|
req, err = http.NewRequest(http.MethodGet, testHost+"/get/"+testContainerName+"/"+id.String(), nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
req.Header.Set(defaultNamespaceHeader, "root")
|
|
|
|
|
|
|
|
resp, err = http.DefaultClient.Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
checkGetResponse(t, resp, content, attributes)
|
|
|
|
|
|
|
|
req, err = http.NewRequest(http.MethodGet, testHost+"/get/"+testContainerName+"/"+id.String(), nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
req.Header.Set(defaultNamespaceHeader, "root2")
|
|
|
|
|
|
|
|
resp, err = http.DefaultClient.Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, http.StatusNotFound, resp.StatusCode)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-09-06 12:59:09 +00:00
|
|
|
func createDockerContainer(ctx context.Context, t *testing.T, image string) testcontainers.Container {
|
|
|
|
req := testcontainers.ContainerRequest{
|
|
|
|
Image: image,
|
|
|
|
WaitingFor: wait.NewLogStrategy("aio container started").WithStartupTimeout(30 * time.Second),
|
|
|
|
Name: "aio",
|
|
|
|
Hostname: "aio",
|
|
|
|
NetworkMode: "host",
|
|
|
|
}
|
|
|
|
aioC, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
|
|
|
|
ContainerRequest: req,
|
|
|
|
Started: true,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return aioC
|
|
|
|
}
|
|
|
|
|
|
|
|
func getDefaultConfig() *viper.Viper {
|
|
|
|
v := settings()
|
2022-07-13 10:44:04 +00:00
|
|
|
v.SetDefault(cfgPeers+".0.address", "localhost:8080")
|
2021-09-06 12:59:09 +00:00
|
|
|
v.SetDefault(cfgPeers+".0.weight", 1)
|
2021-12-07 11:57:16 +00:00
|
|
|
v.SetDefault(cfgPeers+".0.priority", 1)
|
2021-09-06 12:59:09 +00:00
|
|
|
|
2022-07-13 10:44:04 +00:00
|
|
|
v.SetDefault(cfgRPCEndpoint, "http://localhost:30333")
|
2022-11-24 14:49:21 +00:00
|
|
|
v.SetDefault("server.0.address", testListenAddress)
|
2022-04-20 09:17:20 +00:00
|
|
|
|
2021-09-06 12:59:09 +00:00
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2022-03-16 08:10:56 +00:00
|
|
|
func getPool(ctx context.Context, t *testing.T, key *keys.PrivateKey) *pool.Pool {
|
2022-04-07 12:56:18 +00:00
|
|
|
var prm pool.InitParameters
|
|
|
|
prm.SetKey(&key.PrivateKey)
|
|
|
|
prm.SetNodeDialTimeout(5 * time.Second)
|
|
|
|
prm.AddNode(pool.NewNodeParam(1, "localhost:8080", 1))
|
2021-09-06 12:59:09 +00:00
|
|
|
|
2022-04-07 12:56:18 +00:00
|
|
|
clientPool, err := pool.NewPool(prm)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = clientPool.Dial(ctx)
|
2021-09-06 12:59:09 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
return clientPool
|
|
|
|
}
|
|
|
|
|
2022-08-22 15:29:43 +00:00
|
|
|
func createContainer(ctx context.Context, t *testing.T, clientPool *pool.Pool, ownerID user.ID, version string) (cid.ID, error) {
|
2022-07-25 09:47:48 +00:00
|
|
|
var policy netmap.PlacementPolicy
|
|
|
|
err := policy.DecodeString("REP 1")
|
2021-09-06 12:59:09 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-07-25 09:47:48 +00:00
|
|
|
var cnr container.Container
|
|
|
|
cnr.Init()
|
|
|
|
cnr.SetPlacementPolicy(policy)
|
|
|
|
cnr.SetBasicACL(acl.PublicRWExtended)
|
|
|
|
cnr.SetOwner(ownerID)
|
|
|
|
|
|
|
|
container.SetCreationTime(&cnr, time.Now())
|
2021-09-06 12:59:09 +00:00
|
|
|
|
2023-05-16 14:35:58 +00:00
|
|
|
var domain container.Domain
|
|
|
|
domain.SetName(testContainerName)
|
2023-03-16 08:40:08 +00:00
|
|
|
|
2023-05-16 14:35:58 +00:00
|
|
|
cnr.SetAttribute(containerv2.SysAttributeName, domain.Name())
|
|
|
|
cnr.SetAttribute(containerv2.SysAttributeZone, domain.Zone())
|
2022-04-20 09:17:20 +00:00
|
|
|
|
2022-04-07 12:56:18 +00:00
|
|
|
var waitPrm pool.WaitParams
|
|
|
|
waitPrm.SetTimeout(15 * time.Second)
|
|
|
|
waitPrm.SetPollInterval(3 * time.Second)
|
|
|
|
|
|
|
|
var prm pool.PrmContainerPut
|
2022-07-25 09:47:48 +00:00
|
|
|
prm.SetContainer(cnr)
|
2022-04-07 12:56:18 +00:00
|
|
|
prm.SetWaitParams(waitPrm)
|
|
|
|
|
|
|
|
CID, err := clientPool.PutContainer(ctx, prm)
|
2022-03-01 20:38:03 +00:00
|
|
|
if err != nil {
|
2022-08-22 15:29:43 +00:00
|
|
|
return cid.ID{}, err
|
2022-03-01 20:38:03 +00:00
|
|
|
}
|
2021-09-06 12:59:09 +00:00
|
|
|
fmt.Println(CID.String())
|
|
|
|
|
2022-03-01 20:38:03 +00:00
|
|
|
return CID, err
|
2021-09-06 12:59:09 +00:00
|
|
|
}
|
|
|
|
|
2022-08-22 15:29:43 +00:00
|
|
|
func putObject(ctx context.Context, t *testing.T, clientPool *pool.Pool, ownerID user.ID, CID cid.ID, content string, attributes map[string]string) oid.ID {
|
2022-03-02 10:46:11 +00:00
|
|
|
obj := object.New()
|
2022-08-22 15:29:43 +00:00
|
|
|
obj.SetContainerID(CID)
|
2024-06-14 13:43:58 +00:00
|
|
|
obj.SetOwnerID(ownerID)
|
2021-09-06 12:59:09 +00:00
|
|
|
|
2022-03-16 08:10:56 +00:00
|
|
|
var attrs []object.Attribute
|
2021-09-06 12:59:09 +00:00
|
|
|
for key, val := range attributes {
|
|
|
|
attr := object.NewAttribute()
|
|
|
|
attr.SetKey(key)
|
|
|
|
attr.SetValue(val)
|
2022-03-16 08:10:56 +00:00
|
|
|
attrs = append(attrs, *attr)
|
2021-09-06 12:59:09 +00:00
|
|
|
}
|
2022-03-02 10:46:11 +00:00
|
|
|
obj.SetAttributes(attrs...)
|
2021-09-06 12:59:09 +00:00
|
|
|
|
2022-04-07 12:56:18 +00:00
|
|
|
var prm pool.PrmObjectPut
|
|
|
|
prm.SetHeader(*obj)
|
|
|
|
prm.SetPayload(bytes.NewBufferString(content))
|
|
|
|
|
2024-08-20 13:14:18 +00:00
|
|
|
res, err := clientPool.PutObject(ctx, prm)
|
2021-09-06 12:59:09 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-08-20 13:14:18 +00:00
|
|
|
return res.ObjectID
|
2021-09-06 12:59:09 +00:00
|
|
|
}
|
2024-05-06 17:34:49 +00:00
|
|
|
|
|
|
|
func makeBearerToken(t *testing.T, key *keys.PrivateKey, ownerID user.ID, version string) string {
|
|
|
|
tkn := new(bearer.Token)
|
|
|
|
tkn.ForUser(ownerID)
|
|
|
|
tkn.SetExp(10000)
|
|
|
|
|
|
|
|
if version == "1.2.7" {
|
|
|
|
tkn.SetEACLTable(*eacl.NewTable())
|
|
|
|
} else {
|
|
|
|
tkn.SetImpersonate(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := tkn.Sign(key.PrivateKey)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
t64 := base64.StdEncoding.EncodeToString(tkn.Marshal())
|
|
|
|
require.NotEmpty(t, t64)
|
|
|
|
|
|
|
|
return t64
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeTempWallet(t *testing.T, key *keys.PrivateKey, path string) {
|
|
|
|
w, err := wallet.NewWallet(path)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
acc := wallet.NewAccountFromPrivateKey(key)
|
|
|
|
err = acc.Encrypt("", w.Scrypt)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
w.AddAccount(acc)
|
|
|
|
|
|
|
|
err = w.Save()
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|