2022-01-10 08:34:14 +00:00
|
|
|
package netmap
|
|
|
|
|
|
|
|
import (
|
2023-11-16 12:16:48 +00:00
|
|
|
"encoding/base64"
|
2022-01-10 08:34:14 +00:00
|
|
|
"encoding/json"
|
2023-09-15 10:48:34 +00:00
|
|
|
"fmt"
|
2022-01-10 08:34:14 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2023-11-16 12:16:48 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
2022-01-10 08:34:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestCase represents collection of placement policy tests for a single node set.
|
|
|
|
type TestCase struct {
|
2023-11-16 12:16:48 +00:00
|
|
|
Name string `json:"name" yaml:"name"`
|
|
|
|
Nodes []NodeInfo `json:"nodes" yaml:"nodes"`
|
2022-01-10 08:34:14 +00:00
|
|
|
Tests map[string]struct {
|
2023-11-16 12:16:48 +00:00
|
|
|
Policy PlacementPolicy
|
|
|
|
Pivot Base64
|
|
|
|
Result [][]int `json:"result,omitempty" yaml:"result,omitempty"`
|
|
|
|
Error string `json:"error,omitempty" yaml:"error,omitempty"`
|
2022-01-10 08:34:14 +00:00
|
|
|
Placement struct {
|
2023-11-16 12:16:48 +00:00
|
|
|
Pivot Base64 `json:"pivot" yaml:"pivot"`
|
|
|
|
Result [][]int `json:"result,omitempty" yaml:"result,omitempty"`
|
|
|
|
} `json:"placement,omitempty" yaml:"placement,omitempty"`
|
2022-01-10 08:34:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-16 12:16:48 +00:00
|
|
|
// Base64 is a type that will hold the decoded Base64 data.
|
|
|
|
type Base64 []byte
|
|
|
|
|
|
|
|
func (b *Base64) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
var base64Str string
|
|
|
|
if err := unmarshal(&base64Str); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
decodedBytes, err := base64.StdEncoding.DecodeString(base64Str)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*b = decodedBytes
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
var _, _ json.Unmarshaler = new(NodeInfo), new(PlacementPolicy)
|
|
|
|
|
2022-06-07 02:12:39 +00:00
|
|
|
func compareNodes(t testing.TB, expected [][]int, nodes nodes, actual [][]NodeInfo) {
|
2022-01-10 08:34:14 +00:00
|
|
|
require.Equal(t, len(expected), len(actual))
|
|
|
|
for i := range expected {
|
|
|
|
require.Equal(t, len(expected[i]), len(actual[i]))
|
|
|
|
for j, index := range expected[i] {
|
|
|
|
require.Equal(t, nodes[index], actual[i][j])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPlacementPolicy_Interopability(t *testing.T) {
|
2023-11-16 12:16:48 +00:00
|
|
|
const testsDir = "./yml_tests"
|
2022-01-10 08:34:14 +00:00
|
|
|
|
|
|
|
f, err := os.Open(testsDir)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
ds, err := f.ReadDir(0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
for i := range ds {
|
2023-09-15 10:48:34 +00:00
|
|
|
filename := filepath.Join(testsDir, ds[i].Name())
|
|
|
|
bs, err := os.ReadFile(filename)
|
2022-01-10 08:34:14 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var tc TestCase
|
2023-11-16 12:16:48 +00:00
|
|
|
require.NoError(t, yaml.Unmarshal(bs, &tc), "cannot unmarshal %s", ds[i].Name())
|
2022-01-10 08:34:14 +00:00
|
|
|
|
2022-06-07 01:59:21 +00:00
|
|
|
srcNodes := make([]NodeInfo, len(tc.Nodes))
|
|
|
|
copy(srcNodes, tc.Nodes)
|
|
|
|
|
2023-09-15 10:48:34 +00:00
|
|
|
t.Run(fmt.Sprintf("%s:%s", filename, tc.Name), func(t *testing.T) {
|
2022-06-07 08:25:34 +00:00
|
|
|
var nm NetMap
|
2022-06-07 02:12:39 +00:00
|
|
|
nm.SetNodes(tc.Nodes)
|
2022-01-10 08:34:14 +00:00
|
|
|
|
|
|
|
for name, tt := range tc.Tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
2022-06-07 08:25:34 +00:00
|
|
|
v, err := nm.ContainerNodes(tt.Policy, tt.Pivot)
|
2022-01-10 08:34:14 +00:00
|
|
|
if tt.Result == nil {
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), tt.Error)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
2022-06-07 01:59:21 +00:00
|
|
|
require.Equal(t, srcNodes, tc.Nodes)
|
2022-01-10 08:34:14 +00:00
|
|
|
|
2022-06-07 02:12:39 +00:00
|
|
|
compareNodes(t, tt.Result, tc.Nodes, v)
|
2022-01-10 08:34:14 +00:00
|
|
|
|
|
|
|
if tt.Placement.Result != nil {
|
2022-06-07 08:25:34 +00:00
|
|
|
res, err := nm.PlacementVectors(v, tt.Placement.Pivot)
|
2022-01-10 08:34:14 +00:00
|
|
|
require.NoError(t, err)
|
2022-06-07 02:12:39 +00:00
|
|
|
compareNodes(t, tt.Placement.Result, tc.Nodes, res)
|
2022-06-07 01:59:21 +00:00
|
|
|
require.Equal(t, srcNodes, tc.Nodes)
|
2022-01-10 08:34:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-06-07 03:38:35 +00:00
|
|
|
|
|
|
|
func BenchmarkPlacementPolicyInteropability(b *testing.B) {
|
2023-11-16 12:16:48 +00:00
|
|
|
const testsDir = "./yml_tests"
|
2022-06-07 03:38:35 +00:00
|
|
|
|
|
|
|
f, err := os.Open(testsDir)
|
|
|
|
require.NoError(b, err)
|
|
|
|
|
|
|
|
ds, err := f.ReadDir(0)
|
|
|
|
require.NoError(b, err)
|
|
|
|
|
|
|
|
for i := range ds {
|
2022-10-19 06:57:55 +00:00
|
|
|
bs, err := os.ReadFile(filepath.Join(testsDir, ds[i].Name()))
|
2022-06-07 03:38:35 +00:00
|
|
|
require.NoError(b, err)
|
|
|
|
|
|
|
|
var tc TestCase
|
2023-11-16 12:16:48 +00:00
|
|
|
require.NoError(b, yaml.Unmarshal(bs, &tc), "cannot unmarshal %s", ds[i].Name())
|
2022-06-07 03:38:35 +00:00
|
|
|
|
|
|
|
b.Run(tc.Name, func(b *testing.B) {
|
2022-06-07 08:25:34 +00:00
|
|
|
var nm NetMap
|
2022-06-07 02:12:39 +00:00
|
|
|
nm.SetNodes(tc.Nodes)
|
2022-06-07 03:38:35 +00:00
|
|
|
require.NoError(b, err)
|
|
|
|
|
|
|
|
for name, tt := range tc.Tests {
|
|
|
|
b.Run(name, func(b *testing.B) {
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
b.StartTimer()
|
2022-06-07 08:25:34 +00:00
|
|
|
v, err := nm.ContainerNodes(tt.Policy, tt.Pivot)
|
2022-06-07 03:38:35 +00:00
|
|
|
b.StopTimer()
|
|
|
|
if tt.Result == nil {
|
|
|
|
require.Error(b, err)
|
|
|
|
require.Contains(b, err.Error(), tt.Error)
|
|
|
|
} else {
|
|
|
|
require.NoError(b, err)
|
|
|
|
|
2022-06-07 02:12:39 +00:00
|
|
|
compareNodes(b, tt.Result, tc.Nodes, v)
|
2022-06-07 03:38:35 +00:00
|
|
|
|
|
|
|
if tt.Placement.Result != nil {
|
|
|
|
b.StartTimer()
|
2022-06-07 08:25:34 +00:00
|
|
|
res, err := nm.PlacementVectors(v, tt.Placement.Pivot)
|
2022-06-07 03:38:35 +00:00
|
|
|
b.StopTimer()
|
|
|
|
require.NoError(b, err)
|
2022-06-07 02:12:39 +00:00
|
|
|
compareNodes(b, tt.Placement.Result, tc.Nodes, res)
|
2022-06-07 03:38:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-11 09:18:41 +00:00
|
|
|
func BenchmarkManySelects(b *testing.B) {
|
2023-11-16 12:16:48 +00:00
|
|
|
testsFile := filepath.Join("yml_tests", "many_selects.yml")
|
2022-10-19 06:57:55 +00:00
|
|
|
bs, err := os.ReadFile(testsFile)
|
2022-03-11 09:18:41 +00:00
|
|
|
require.NoError(b, err)
|
|
|
|
|
|
|
|
var tc TestCase
|
2023-11-16 12:16:48 +00:00
|
|
|
require.NoError(b, yaml.Unmarshal(bs, &tc))
|
2022-03-11 09:18:41 +00:00
|
|
|
tt, ok := tc.Tests["Select"]
|
|
|
|
require.True(b, ok)
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
var nm NetMap
|
2022-06-07 02:12:39 +00:00
|
|
|
nm.SetNodes(tc.Nodes)
|
2022-03-11 09:18:41 +00:00
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2022-06-07 08:25:34 +00:00
|
|
|
_, err = nm.ContainerNodes(tt.Policy, tt.Pivot)
|
2022-03-11 09:18:41 +00:00
|
|
|
if err != nil {
|
|
|
|
b.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|