micro/network/router/table/default_test.go

228 lines
5.6 KiB
Go
Raw Normal View History

package table
2019-07-02 00:57:27 +03:00
import "testing"
2019-07-02 01:15:45 +03:00
func testSetup() (Table, Route) {
2019-07-02 00:57:27 +03:00
table := NewTable()
2019-07-02 01:15:45 +03:00
route := Route{
Destination: "dest.svc",
Gateway: "dest.gw",
Router: "dest.router",
Network: "dest.network",
Metric: 10,
2019-07-02 00:57:27 +03:00
}
2019-07-02 01:15:45 +03:00
return table, route
2019-07-02 00:57:27 +03:00
}
2019-07-02 01:15:45 +03:00
func TestAdd(t *testing.T) {
table, route := testSetup()
testTableSize := table.Size()
2019-07-02 00:57:27 +03:00
if err := table.Add(route); err != nil {
t.Errorf("error adding route: %s", err)
}
testTableSize += 1
2019-07-02 00:57:27 +03:00
// adds new route for the original destination
route.Gateway = "dest.gw2"
if err := table.Add(route); err != nil {
t.Errorf("error adding route: %s", err)
}
testTableSize += 1
2019-07-02 00:57:27 +03:00
if table.Size() != testTableSize {
t.Errorf("invalid number of routes. expected: %d, found: %d", testTableSize, table.Size())
2019-07-02 00:57:27 +03:00
}
// adding the same route under Insert policy must error
2019-07-02 00:57:27 +03:00
if err := table.Add(route); err != ErrDuplicateRoute {
t.Errorf("error adding route. Expected error: %s, found: %s", ErrDuplicateRoute, err)
2019-07-02 00:57:27 +03:00
}
}
func TestDelete(t *testing.T) {
2019-07-02 01:15:45 +03:00
table, route := testSetup()
testTableSize := table.Size()
2019-07-02 00:57:27 +03:00
if err := table.Add(route); err != nil {
t.Errorf("error adding route: %s", err)
}
testTableSize += 1
2019-07-02 00:57:27 +03:00
// should fail to delete non-existant route
prevDest := route.Destination
2019-07-02 00:57:27 +03:00
route.Destination = "randDest"
if err := table.Delete(route); err != ErrRouteNotFound {
t.Errorf("error deleting route. Expected error: %s, found: %s", ErrRouteNotFound, err)
2019-07-02 00:57:27 +03:00
}
// we should be able to delete the existing route
route.Destination = prevDest
2019-07-02 00:57:27 +03:00
if err := table.Delete(route); err != nil {
t.Errorf("error deleting route: %s", err)
}
testTableSize -= 1
2019-07-02 00:57:27 +03:00
if table.Size() != testTableSize {
t.Errorf("invalid number of routes. expected: %d, found: %d", testTableSize, table.Size())
2019-07-02 00:57:27 +03:00
}
}
func TestUpdate(t *testing.T) {
2019-07-02 01:15:45 +03:00
table, route := testSetup()
testTableSize := table.Size()
2019-07-02 00:57:27 +03:00
if err := table.Add(route); err != nil {
t.Errorf("error adding route: %s", err)
}
testTableSize += 1
2019-07-02 00:57:27 +03:00
// change the metric of the original route
2019-07-02 00:57:27 +03:00
route.Metric = 200
if err := table.Update(route); err != nil {
t.Errorf("error updating route: %s", err)
}
// the size of the table should not change as we're only updating the metric of an existing route
if table.Size() != testTableSize {
t.Errorf("invalid number of routes. expected: %d, found: %d", testTableSize, table.Size())
2019-07-02 00:57:27 +03:00
}
// this should error as the destination does not exist
route.Destination = "rand.dest"
2019-07-02 00:57:27 +03:00
if err := table.Update(route); err != ErrRouteNotFound {
t.Errorf("error updating route. Expected error: %s, found: %s", ErrRouteNotFound, err)
2019-07-02 00:57:27 +03:00
}
if table.Size() != testTableSize {
t.Errorf("invalid number of routes. expected: %d, found: %d", testTableSize, table.Size())
2019-07-02 00:57:27 +03:00
}
}
2019-07-02 00:57:27 +03:00
func TestList(t *testing.T) {
table, route := testSetup()
2019-07-02 00:57:27 +03:00
dest := []string{"one.svc", "two.svc", "three.svc"}
2019-07-02 00:57:27 +03:00
for i := 0; i < len(dest); i++ {
route.Destination = dest[i]
if err := table.Add(route); err != nil {
t.Errorf("error adding route: %s", err)
}
2019-07-02 00:57:27 +03:00
}
routes, err := table.List()
if err != nil {
t.Errorf("error listing routes: %s", err)
}
2019-07-02 00:57:27 +03:00
if len(routes) != len(dest) {
t.Errorf("incorrect number of routes listed. Expected: %d, found: %d", len(dest), len(routes))
2019-07-02 00:57:27 +03:00
}
if len(routes) != table.Size() {
t.Errorf("mismatch number of routes and table size. Routes: %d, Size: %d", len(routes), table.Size())
2019-07-02 00:57:27 +03:00
}
}
2019-07-02 01:15:45 +03:00
func TestLookup(t *testing.T) {
2019-07-02 01:15:45 +03:00
table, route := testSetup()
dest := []string{"svc1", "svc2", "svc3"}
net := []string{"net1", "net2", "net1"}
rtr := []string{"router1", "router2", "router3"}
2019-07-02 01:15:45 +03:00
for i := 0; i < len(dest); i++ {
route.Destination = dest[i]
route.Network = net[i]
route.Router = rtr[i]
2019-07-02 01:15:45 +03:00
if err := table.Add(route); err != nil {
t.Errorf("error adding route: %s", err)
}
}
// return all routes
query := NewQuery()
routes, err := table.Lookup(query)
2019-07-02 01:15:45 +03:00
if err != nil {
t.Errorf("error looking up routes: %s", err)
2019-07-02 01:15:45 +03:00
}
if len(routes) != table.Size() {
t.Errorf("incorrect number of routes returned. expected: %d, found: %d", table.Size(), len(routes))
2019-07-02 01:15:45 +03:00
}
// query particular net
query = NewQuery(QueryNetwork("net1"))
routes, err = table.Lookup(query)
if err != nil {
t.Errorf("error looking up routes: %s", err)
}
if len(routes) != 2 {
t.Errorf("incorrect number of routes returned. expected: %d, found: %d", 2, len(routes))
}
// query particular router
router := "router1"
query = NewQuery(QueryRouter(router))
routes, err = table.Lookup(query)
if err != nil {
t.Errorf("error looking up routes: %s", err)
}
if len(routes) != 1 {
t.Errorf("incorrect number of routes returned. expected: %d, found: %d", 1, len(routes))
}
if routes[0].Router != router {
t.Errorf("incorrect route returned. Expected router: %s, found: %s", router, routes[0].Router)
}
// query particular route
network := "net1"
query = NewQuery(
QueryRouter(router),
QueryNetwork(network),
)
routes, err = table.Lookup(query)
if err != nil {
t.Errorf("error looking up routes: %s", err)
}
if len(routes) != 1 {
t.Errorf("incorrect number of routes returned. expected: %d, found: %d", 1, len(routes))
}
if routes[0].Router != router {
t.Errorf("incorrect route returned. Expected router: %s, found: %s", router, routes[0].Router)
}
if routes[0].Network != network {
t.Errorf("incorrect network returned. Expected network: %s, found: %s", network, routes[0].Network)
}
// bullshit route query
query = NewQuery(QueryDestination("foobar"))
routes, err = table.Lookup(query)
if err != nil {
t.Errorf("error looking up routes: %s", err)
}
if len(routes) != 0 {
t.Errorf("incorrect number of routes returned. expected: %d, found: %d", 0, len(routes))
2019-07-02 01:15:45 +03:00
}
}