2020-09-22 10:56:43 +00:00
|
|
|
package attributes_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/attributes"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
|
2020-09-22 10:56:43 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
func testAttributeMap(t *testing.T, mSrc, mExp map[string]string) {
|
|
|
|
var node netmap.NodeInfo
|
|
|
|
|
|
|
|
s := make([]string, 0, len(mSrc))
|
|
|
|
for k, v := range mSrc {
|
|
|
|
s = append(s, k+":"+v)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := attributes.ReadNodeAttributes(&node, s)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
if mExp == nil {
|
|
|
|
mExp = mSrc
|
|
|
|
}
|
|
|
|
|
|
|
|
node.IterateAttributes(func(key, value string) {
|
|
|
|
v, ok := mExp[key]
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, value, v)
|
|
|
|
delete(mExp, key)
|
|
|
|
})
|
|
|
|
|
|
|
|
require.Empty(t, mExp)
|
|
|
|
}
|
|
|
|
|
2020-09-22 10:56:43 +00:00
|
|
|
func TestParseV2Attributes(t *testing.T) {
|
|
|
|
t.Run("empty", func(t *testing.T) {
|
2022-06-08 23:18:26 +00:00
|
|
|
var node netmap.NodeInfo
|
|
|
|
err := attributes.ReadNodeAttributes(&node, nil)
|
2020-09-22 10:56:43 +00:00
|
|
|
require.NoError(t, err)
|
2022-06-08 23:18:26 +00:00
|
|
|
require.Zero(t, node.NumberOfAttributes())
|
2020-09-22 10:56:43 +00:00
|
|
|
})
|
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
t.Run("empty key and/or value", func(t *testing.T) {
|
|
|
|
var node netmap.NodeInfo
|
|
|
|
err := attributes.ReadNodeAttributes(&node, []string{
|
|
|
|
":HDD",
|
|
|
|
})
|
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
err = attributes.ReadNodeAttributes(&node, []string{
|
|
|
|
"StorageType:",
|
|
|
|
})
|
|
|
|
require.Error(t, err)
|
2020-09-22 10:56:43 +00:00
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
err = attributes.ReadNodeAttributes(&node, []string{
|
|
|
|
":",
|
|
|
|
})
|
2020-09-22 10:56:43 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
t.Run("non-unique keys", func(t *testing.T) {
|
|
|
|
var node netmap.NodeInfo
|
|
|
|
err := attributes.ReadNodeAttributes(&node, []string{
|
|
|
|
"StorageType:HDD",
|
|
|
|
"StorageType:HDD",
|
|
|
|
})
|
2020-09-22 10:56:43 +00:00
|
|
|
require.Error(t, err)
|
2022-06-08 23:18:26 +00:00
|
|
|
})
|
2020-09-22 10:56:43 +00:00
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
t.Run("malformed", func(t *testing.T) {
|
|
|
|
var node netmap.NodeInfo
|
|
|
|
err := attributes.ReadNodeAttributes(&node, []string{"..."})
|
2020-09-22 10:56:43 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
err = attributes.ReadNodeAttributes(&node, []string{"a:b", ""})
|
2020-09-22 10:56:43 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
err = attributes.ReadNodeAttributes(&node, []string{"//"})
|
2020-09-22 10:56:43 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("correct", func(t *testing.T) {
|
2022-06-08 23:18:26 +00:00
|
|
|
testAttributeMap(t, map[string]string{
|
|
|
|
"Location": "Europe",
|
|
|
|
"StorageType": "HDD",
|
|
|
|
}, nil)
|
2020-09-22 10:56:43 +00:00
|
|
|
})
|
2021-08-04 09:21:40 +00:00
|
|
|
|
|
|
|
t.Run("escape characters", func(t *testing.T) {
|
2022-06-08 23:18:26 +00:00
|
|
|
testAttributeMap(t, map[string]string{
|
|
|
|
`K\:ey1`: `V\/alue`,
|
|
|
|
`Ke\/y2`: `Va\:lue`,
|
|
|
|
}, map[string]string{
|
|
|
|
`K:ey1`: `V\/alue`,
|
|
|
|
`Ke\/y2`: `Va:lue`,
|
2021-09-01 09:55:09 +00:00
|
|
|
})
|
2021-08-31 15:34:55 +00:00
|
|
|
})
|
2020-09-22 10:56:43 +00:00
|
|
|
}
|