2021-10-27 10:00:35 +00:00
|
|
|
package netmap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
2023-03-07 11:20:03 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/netmap"
|
2021-10-27 10:00:35 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestContext_ProcessFilters(t *testing.T) {
|
2022-03-11 09:13:08 +00:00
|
|
|
fs := []Filter{
|
2022-06-07 08:25:34 +00:00
|
|
|
newFilter("StorageSSD", "Storage", "SSD", netmap.EQ),
|
|
|
|
newFilter("GoodRating", "Rating", "4", netmap.GE),
|
|
|
|
newFilter("Main", "", "", netmap.AND,
|
2021-10-27 10:00:35 +00:00
|
|
|
newFilter("StorageSSD", "", "", 0),
|
2022-06-07 08:25:34 +00:00
|
|
|
newFilter("", "IntField", "123", netmap.LT),
|
2021-10-27 10:00:35 +00:00
|
|
|
newFilter("GoodRating", "", "", 0)),
|
|
|
|
}
|
2022-06-07 02:12:39 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
c := newContext(NetMap{})
|
2021-10-27 10:00:35 +00:00
|
|
|
p := newPlacementPolicy(1, nil, nil, fs)
|
|
|
|
require.NoError(t, c.processFilters(p))
|
2022-06-07 08:25:34 +00:00
|
|
|
require.Equal(t, 3, len(c.processedFilters))
|
2021-10-27 10:00:35 +00:00
|
|
|
for _, f := range fs {
|
2022-06-07 08:25:34 +00:00
|
|
|
require.Equal(t, f.m, *c.processedFilters[f.m.GetName()])
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
require.Equal(t, uint64(4), c.numCache[fs[1].m.GetValue()])
|
|
|
|
require.Equal(t, uint64(123), c.numCache[fs[2].m.GetFilters()[1].GetValue()])
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext_ProcessFiltersInvalid(t *testing.T) {
|
|
|
|
errTestCases := []struct {
|
|
|
|
name string
|
2022-03-11 09:13:08 +00:00
|
|
|
filter Filter
|
2021-10-27 10:00:35 +00:00
|
|
|
err error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"UnnamedTop",
|
2022-06-07 08:25:34 +00:00
|
|
|
newFilter("", "Storage", "SSD", netmap.EQ),
|
|
|
|
errUnnamedTopFilter,
|
2021-10-27 10:00:35 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"InvalidReference",
|
2022-06-07 08:25:34 +00:00
|
|
|
newFilter("Main", "", "", netmap.AND,
|
2021-10-27 10:00:35 +00:00
|
|
|
newFilter("StorageSSD", "", "", 0)),
|
2022-06-07 08:25:34 +00:00
|
|
|
errFilterNotFound,
|
2021-10-27 10:00:35 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"NonEmptyKeyed",
|
2022-06-07 08:25:34 +00:00
|
|
|
newFilter("Main", "Storage", "SSD", netmap.EQ,
|
2021-10-27 10:00:35 +00:00
|
|
|
newFilter("StorageSSD", "", "", 0)),
|
2022-06-07 08:25:34 +00:00
|
|
|
errNonEmptyFilters,
|
2021-10-27 10:00:35 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"InvalidNumber",
|
2022-06-07 08:25:34 +00:00
|
|
|
newFilter("Main", "Rating", "three", netmap.GE),
|
|
|
|
errInvalidNumber,
|
2021-10-27 10:00:35 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"InvalidOp",
|
|
|
|
newFilter("Main", "Rating", "3", 0),
|
2022-06-07 08:25:34 +00:00
|
|
|
errInvalidFilterOp,
|
2021-10-27 10:00:35 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"InvalidName",
|
2022-06-07 08:25:34 +00:00
|
|
|
newFilter("*", "Rating", "3", netmap.GE),
|
|
|
|
errInvalidFilterName,
|
2021-10-27 10:00:35 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range errTestCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2022-06-07 08:25:34 +00:00
|
|
|
c := newContext(NetMap{})
|
2022-03-11 09:13:08 +00:00
|
|
|
p := newPlacementPolicy(1, nil, nil, []Filter{tc.filter})
|
2021-10-27 10:00:35 +00:00
|
|
|
err := c.processFilters(p)
|
|
|
|
require.True(t, errors.Is(err, tc.err), "got: %v", err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-10 08:34:14 +00:00
|
|
|
func TestFilter_MatchSimple_InvalidOp(t *testing.T) {
|
2022-06-07 02:12:39 +00:00
|
|
|
var b NodeInfo
|
2022-06-07 08:25:34 +00:00
|
|
|
b.SetAttribute("Rating", "4")
|
|
|
|
b.SetAttribute("Country", "Germany")
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
f := newFilter("Main", "Rating", "5", netmap.EQ)
|
|
|
|
c := newContext(NetMap{})
|
2022-03-11 09:13:08 +00:00
|
|
|
p := newPlacementPolicy(1, nil, nil, []Filter{f})
|
2021-10-27 10:00:35 +00:00
|
|
|
require.NoError(t, c.processFilters(p))
|
|
|
|
|
2022-01-10 08:34:14 +00:00
|
|
|
// just for the coverage
|
2022-06-07 08:25:34 +00:00
|
|
|
f.m.SetOp(0)
|
|
|
|
require.False(t, c.match(&f.m, b))
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|