2023-09-08 11:17:14 +00:00
|
|
|
package main
|
2023-03-02 14:54:33 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2023-11-21 08:51:07 +00:00
|
|
|
"encoding/json"
|
2023-03-02 14:54:33 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/handler"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDefaultNamespace(t *testing.T) {
|
|
|
|
xmlBodyWithNamespace := `
|
|
|
|
<CompleteMultipartUpload xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
|
|
|
|
<Part>
|
|
|
|
<ETag>string</ETag>
|
|
|
|
<PartNumber>1</PartNumber>
|
|
|
|
</Part>
|
|
|
|
</CompleteMultipartUpload>
|
|
|
|
`
|
|
|
|
xmlBodyWithInvalidNamespace := `
|
|
|
|
<CompleteMultipartUpload xmlns="http://bla.bla.bla/">
|
|
|
|
<Part>
|
|
|
|
<ETag>string</ETag>
|
|
|
|
<PartNumber>1</PartNumber>
|
|
|
|
</Part>
|
|
|
|
</CompleteMultipartUpload>
|
|
|
|
`
|
|
|
|
xmlBody := `
|
|
|
|
<CompleteMultipartUpload>
|
|
|
|
<Part>
|
|
|
|
<ETag>string</ETag>
|
|
|
|
<PartNumber>1</PartNumber>
|
|
|
|
</Part>
|
|
|
|
</CompleteMultipartUpload>
|
|
|
|
`
|
2024-04-24 12:20:11 +00:00
|
|
|
xmlASCII := `<?xml version="1.0" encoding="US-ASCII"?>
|
|
|
|
<CompleteMultipartUpload>
|
|
|
|
<Part>
|
|
|
|
<PartNumber>1</PartNumber>
|
|
|
|
<ETag>
|
|
|
|
8b73814bee405ec32b5d1fc88cd5d97a
|
|
|
|
</ETag>
|
|
|
|
</Part>
|
|
|
|
</CompleteMultipartUpload>`
|
2023-03-02 14:54:33 +00:00
|
|
|
|
|
|
|
for _, tc := range []struct {
|
2023-09-08 11:17:14 +00:00
|
|
|
settings *appSettings
|
2023-03-02 14:54:33 +00:00
|
|
|
input string
|
|
|
|
err bool
|
|
|
|
}{
|
|
|
|
{
|
2023-09-08 11:17:14 +00:00
|
|
|
settings: &appSettings{
|
2023-10-09 12:34:51 +00:00
|
|
|
defaultXMLNS: false,
|
2023-09-08 11:17:14 +00:00
|
|
|
},
|
|
|
|
input: xmlBodyWithNamespace,
|
|
|
|
err: false,
|
2023-03-02 14:54:33 +00:00
|
|
|
},
|
|
|
|
{
|
2023-09-08 11:17:14 +00:00
|
|
|
settings: &appSettings{
|
2023-10-09 12:34:51 +00:00
|
|
|
defaultXMLNS: false,
|
2023-09-08 11:17:14 +00:00
|
|
|
},
|
|
|
|
input: xmlBody,
|
|
|
|
err: true,
|
2023-03-02 14:54:33 +00:00
|
|
|
},
|
|
|
|
{
|
2023-09-08 11:17:14 +00:00
|
|
|
settings: &appSettings{
|
2023-10-09 12:34:51 +00:00
|
|
|
defaultXMLNS: false,
|
2023-09-08 11:17:14 +00:00
|
|
|
},
|
|
|
|
input: xmlBodyWithInvalidNamespace,
|
|
|
|
err: true,
|
2023-03-02 14:54:33 +00:00
|
|
|
},
|
|
|
|
{
|
2023-09-08 11:17:14 +00:00
|
|
|
settings: &appSettings{
|
2023-10-09 12:34:51 +00:00
|
|
|
defaultXMLNS: true,
|
2023-09-08 11:17:14 +00:00
|
|
|
},
|
|
|
|
input: xmlBodyWithNamespace,
|
|
|
|
err: false,
|
2023-03-02 14:54:33 +00:00
|
|
|
},
|
|
|
|
{
|
2023-09-08 11:17:14 +00:00
|
|
|
settings: &appSettings{
|
2023-10-09 12:34:51 +00:00
|
|
|
defaultXMLNS: true,
|
2023-09-08 11:17:14 +00:00
|
|
|
},
|
|
|
|
input: xmlBody,
|
|
|
|
err: false,
|
2023-03-02 14:54:33 +00:00
|
|
|
},
|
|
|
|
{
|
2023-09-08 11:17:14 +00:00
|
|
|
settings: &appSettings{
|
2023-10-09 12:34:51 +00:00
|
|
|
defaultXMLNS: true,
|
2023-09-08 11:17:14 +00:00
|
|
|
},
|
|
|
|
input: xmlBodyWithInvalidNamespace,
|
|
|
|
err: true,
|
2023-03-02 14:54:33 +00:00
|
|
|
},
|
2024-04-24 12:20:11 +00:00
|
|
|
{
|
|
|
|
settings: &appSettings{
|
|
|
|
defaultXMLNS: true,
|
|
|
|
},
|
|
|
|
input: xmlASCII,
|
|
|
|
err: false,
|
|
|
|
},
|
2023-03-02 14:54:33 +00:00
|
|
|
} {
|
|
|
|
t.Run("", func(t *testing.T) {
|
|
|
|
model := new(handler.CompleteMultipartUpload)
|
2024-12-08 12:02:31 +00:00
|
|
|
err := tc.settings.NewXMLDecoder(bytes.NewBufferString(tc.input), "test").Decode(model)
|
2023-03-02 14:54:33 +00:00
|
|
|
if tc.err {
|
|
|
|
require.Error(t, err)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-11-21 08:51:07 +00:00
|
|
|
|
|
|
|
func TestNamespacesMarshaling(t *testing.T) {
|
|
|
|
dataJSON := `
|
|
|
|
{
|
|
|
|
"namespaces": {
|
|
|
|
"kapusta": {
|
|
|
|
"location_constraints": {
|
|
|
|
"default": "REP 3",
|
|
|
|
"load-1-1": "REP 1 CBF 1 SELECT 1 FROM *"
|
|
|
|
},
|
|
|
|
"copies_numbers": {
|
|
|
|
"default": [
|
|
|
|
0
|
|
|
|
],
|
|
|
|
"load-1-1": [
|
|
|
|
1
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"root": {
|
|
|
|
"location_constraints": {
|
|
|
|
"default": "REP 3",
|
|
|
|
"test": "{\"replicas\":[{\"count\":1,\"selector\":\"\"}],\"containerBackupFactor\":1,\"selectors\":[{\"name\":\"\",\"count\":1,\"clause\":\"CLAUSE_UNSPECIFIED\",\"attribute\":\"\",\"filter\":\"Color\"}],\"filters\":[{\"name\":\"Color\",\"key\":\"Color\",\"op\":\"EQ\",\"value\":\"Red\",\"filters\":[]}],\"unique\":false}"
|
|
|
|
},
|
|
|
|
"copies_numbers": {
|
|
|
|
"default": [
|
|
|
|
0
|
|
|
|
],
|
|
|
|
"load-1-1": [
|
|
|
|
1
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
var nsConfig NamespacesConfig
|
|
|
|
err := json.Unmarshal([]byte(dataJSON), &nsConfig)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
data, err := json.Marshal(nsConfig)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var nsConfig2 NamespacesConfig
|
|
|
|
err = json.Unmarshal(data, &nsConfig2)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, nsConfig, nsConfig2)
|
|
|
|
}
|