2024-04-23 14:42:53 +02:00
|
|
|
package routeselector_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"slices"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/client/internal/routeselector"
|
|
|
|
"github.com/netbirdio/netbird/route"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRouteSelector_SelectRoutes(t *testing.T) {
|
2024-05-06 14:47:49 +02:00
|
|
|
allRoutes := []route.NetID{"route1", "route2", "route3"}
|
2024-04-23 14:42:53 +02:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
2024-05-06 14:47:49 +02:00
|
|
|
initialSelected []route.NetID
|
2024-04-23 14:42:53 +02:00
|
|
|
|
2024-05-06 14:47:49 +02:00
|
|
|
selectRoutes []route.NetID
|
2024-04-23 14:42:53 +02:00
|
|
|
append bool
|
|
|
|
|
2024-05-06 14:47:49 +02:00
|
|
|
wantSelected []route.NetID
|
2024-04-23 14:42:53 +02:00
|
|
|
wantError bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Select specific routes, initial all selected",
|
2024-05-06 14:47:49 +02:00
|
|
|
selectRoutes: []route.NetID{"route1", "route2"},
|
|
|
|
wantSelected: []route.NetID{"route1", "route2"},
|
2024-04-23 14:42:53 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Select specific routes, initial all deselected",
|
2024-05-06 14:47:49 +02:00
|
|
|
initialSelected: []route.NetID{},
|
|
|
|
selectRoutes: []route.NetID{"route1", "route2"},
|
|
|
|
wantSelected: []route.NetID{"route1", "route2"},
|
2024-04-23 14:42:53 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Select specific routes with initial selection",
|
2024-05-06 14:47:49 +02:00
|
|
|
initialSelected: []route.NetID{"route1"},
|
|
|
|
selectRoutes: []route.NetID{"route2", "route3"},
|
|
|
|
wantSelected: []route.NetID{"route2", "route3"},
|
2024-04-23 14:42:53 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Select non-existing route",
|
2024-05-06 14:47:49 +02:00
|
|
|
selectRoutes: []route.NetID{"route1", "route4"},
|
|
|
|
wantSelected: []route.NetID{"route1"},
|
2024-04-23 14:42:53 +02:00
|
|
|
wantError: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Append route with initial selection",
|
2024-05-06 14:47:49 +02:00
|
|
|
initialSelected: []route.NetID{"route1"},
|
|
|
|
selectRoutes: []route.NetID{"route2"},
|
2024-04-23 14:42:53 +02:00
|
|
|
append: true,
|
2024-05-06 14:47:49 +02:00
|
|
|
wantSelected: []route.NetID{"route1", "route2"},
|
2024-04-23 14:42:53 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Append route without initial selection",
|
2024-05-06 14:47:49 +02:00
|
|
|
selectRoutes: []route.NetID{"route2"},
|
2024-04-23 14:42:53 +02:00
|
|
|
append: true,
|
2024-05-06 14:47:49 +02:00
|
|
|
wantSelected: []route.NetID{"route2"},
|
2024-04-23 14:42:53 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
rs := routeselector.NewRouteSelector()
|
|
|
|
|
|
|
|
if tt.initialSelected != nil {
|
|
|
|
err := rs.SelectRoutes(tt.initialSelected, false, allRoutes)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := rs.SelectRoutes(tt.selectRoutes, tt.append, allRoutes)
|
|
|
|
if tt.wantError {
|
|
|
|
assert.Error(t, err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, id := range allRoutes {
|
|
|
|
assert.Equal(t, rs.IsSelected(id), slices.Contains(tt.wantSelected, id))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRouteSelector_SelectAllRoutes(t *testing.T) {
|
2024-05-06 14:47:49 +02:00
|
|
|
allRoutes := []route.NetID{"route1", "route2", "route3"}
|
2024-04-23 14:42:53 +02:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
2024-05-06 14:47:49 +02:00
|
|
|
initialSelected []route.NetID
|
2024-04-23 14:42:53 +02:00
|
|
|
|
2024-05-06 14:47:49 +02:00
|
|
|
wantSelected []route.NetID
|
2024-04-23 14:42:53 +02:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Initial all selected",
|
2024-05-06 14:47:49 +02:00
|
|
|
wantSelected: []route.NetID{"route1", "route2", "route3"},
|
2024-04-23 14:42:53 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Initial all deselected",
|
2024-05-06 14:47:49 +02:00
|
|
|
initialSelected: []route.NetID{},
|
|
|
|
wantSelected: []route.NetID{"route1", "route2", "route3"},
|
2024-04-23 14:42:53 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Initial some selected",
|
2024-05-06 14:47:49 +02:00
|
|
|
initialSelected: []route.NetID{"route1"},
|
|
|
|
wantSelected: []route.NetID{"route1", "route2", "route3"},
|
2024-04-23 14:42:53 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Initial all selected",
|
2024-05-06 14:47:49 +02:00
|
|
|
initialSelected: []route.NetID{"route1", "route2", "route3"},
|
|
|
|
wantSelected: []route.NetID{"route1", "route2", "route3"},
|
2024-04-23 14:42:53 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
rs := routeselector.NewRouteSelector()
|
|
|
|
|
|
|
|
if tt.initialSelected != nil {
|
|
|
|
err := rs.SelectRoutes(tt.initialSelected, false, allRoutes)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rs.SelectAllRoutes()
|
|
|
|
|
|
|
|
for _, id := range allRoutes {
|
|
|
|
assert.Equal(t, rs.IsSelected(id), slices.Contains(tt.wantSelected, id))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRouteSelector_DeselectRoutes(t *testing.T) {
|
2024-05-06 14:47:49 +02:00
|
|
|
allRoutes := []route.NetID{"route1", "route2", "route3"}
|
2024-04-23 14:42:53 +02:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
2024-05-06 14:47:49 +02:00
|
|
|
initialSelected []route.NetID
|
2024-04-23 14:42:53 +02:00
|
|
|
|
2024-05-06 14:47:49 +02:00
|
|
|
deselectRoutes []route.NetID
|
2024-04-23 14:42:53 +02:00
|
|
|
|
2024-05-06 14:47:49 +02:00
|
|
|
wantSelected []route.NetID
|
2024-04-23 14:42:53 +02:00
|
|
|
wantError bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Deselect specific routes, initial all selected",
|
2024-05-06 14:47:49 +02:00
|
|
|
deselectRoutes: []route.NetID{"route1", "route2"},
|
|
|
|
wantSelected: []route.NetID{"route3"},
|
2024-04-23 14:42:53 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Deselect specific routes, initial all deselected",
|
2024-05-06 14:47:49 +02:00
|
|
|
initialSelected: []route.NetID{},
|
|
|
|
deselectRoutes: []route.NetID{"route1", "route2"},
|
|
|
|
wantSelected: []route.NetID{},
|
2024-04-23 14:42:53 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Deselect specific routes with initial selection",
|
2024-05-06 14:47:49 +02:00
|
|
|
initialSelected: []route.NetID{"route1", "route2"},
|
|
|
|
deselectRoutes: []route.NetID{"route1", "route3"},
|
|
|
|
wantSelected: []route.NetID{"route2"},
|
2024-04-23 14:42:53 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Deselect non-existing route",
|
2024-05-06 14:47:49 +02:00
|
|
|
initialSelected: []route.NetID{"route1", "route2"},
|
|
|
|
deselectRoutes: []route.NetID{"route1", "route4"},
|
|
|
|
wantSelected: []route.NetID{"route2"},
|
2024-04-23 14:42:53 +02:00
|
|
|
wantError: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
rs := routeselector.NewRouteSelector()
|
|
|
|
|
|
|
|
if tt.initialSelected != nil {
|
|
|
|
err := rs.SelectRoutes(tt.initialSelected, false, allRoutes)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := rs.DeselectRoutes(tt.deselectRoutes, allRoutes)
|
|
|
|
if tt.wantError {
|
|
|
|
assert.Error(t, err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, id := range allRoutes {
|
|
|
|
assert.Equal(t, rs.IsSelected(id), slices.Contains(tt.wantSelected, id))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRouteSelector_DeselectAll(t *testing.T) {
|
2024-05-06 14:47:49 +02:00
|
|
|
allRoutes := []route.NetID{"route1", "route2", "route3"}
|
2024-04-23 14:42:53 +02:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
2024-05-06 14:47:49 +02:00
|
|
|
initialSelected []route.NetID
|
2024-04-23 14:42:53 +02:00
|
|
|
|
2024-05-06 14:47:49 +02:00
|
|
|
wantSelected []route.NetID
|
2024-04-23 14:42:53 +02:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Initial all selected",
|
2024-05-06 14:47:49 +02:00
|
|
|
wantSelected: []route.NetID{},
|
2024-04-23 14:42:53 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Initial all deselected",
|
2024-05-06 14:47:49 +02:00
|
|
|
initialSelected: []route.NetID{},
|
|
|
|
wantSelected: []route.NetID{},
|
2024-04-23 14:42:53 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Initial some selected",
|
2024-05-06 14:47:49 +02:00
|
|
|
initialSelected: []route.NetID{"route1", "route2"},
|
|
|
|
wantSelected: []route.NetID{},
|
2024-04-23 14:42:53 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Initial all selected",
|
2024-05-06 14:47:49 +02:00
|
|
|
initialSelected: []route.NetID{"route1", "route2", "route3"},
|
|
|
|
wantSelected: []route.NetID{},
|
2024-04-23 14:42:53 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
rs := routeselector.NewRouteSelector()
|
|
|
|
|
|
|
|
if tt.initialSelected != nil {
|
|
|
|
err := rs.SelectRoutes(tt.initialSelected, false, allRoutes)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rs.DeselectAllRoutes()
|
|
|
|
|
|
|
|
for _, id := range allRoutes {
|
|
|
|
assert.Equal(t, rs.IsSelected(id), slices.Contains(tt.wantSelected, id))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRouteSelector_IsSelected(t *testing.T) {
|
|
|
|
rs := routeselector.NewRouteSelector()
|
|
|
|
|
2024-05-06 14:47:49 +02:00
|
|
|
err := rs.SelectRoutes([]route.NetID{"route1", "route2"}, false, []route.NetID{"route1", "route2", "route3"})
|
2024-04-23 14:42:53 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.True(t, rs.IsSelected("route1"))
|
|
|
|
assert.True(t, rs.IsSelected("route2"))
|
|
|
|
assert.False(t, rs.IsSelected("route3"))
|
|
|
|
assert.False(t, rs.IsSelected("route4"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRouteSelector_FilterSelected(t *testing.T) {
|
|
|
|
rs := routeselector.NewRouteSelector()
|
|
|
|
|
2024-05-06 14:47:49 +02:00
|
|
|
err := rs.SelectRoutes([]route.NetID{"route1", "route2"}, false, []route.NetID{"route1", "route2", "route3"})
|
2024-04-23 14:42:53 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-05-06 14:47:49 +02:00
|
|
|
routes := route.HAMap{
|
2024-06-13 13:24:24 +02:00
|
|
|
"route1|10.0.0.0/8": {},
|
|
|
|
"route2|192.168.0.0/16": {},
|
|
|
|
"route3|172.16.0.0/12": {},
|
2024-04-23 14:42:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
filtered := rs.FilterSelected(routes)
|
|
|
|
|
2024-05-06 14:47:49 +02:00
|
|
|
assert.Equal(t, route.HAMap{
|
2024-06-13 13:24:24 +02:00
|
|
|
"route1|10.0.0.0/8": {},
|
|
|
|
"route2|192.168.0.0/16": {},
|
2024-04-23 14:42:53 +02:00
|
|
|
}, filtered)
|
|
|
|
}
|