2019-07-02 00:57:27 +03:00
|
|
|
package router
|
|
|
|
|
|
|
|
import "testing"
|
|
|
|
|
2019-07-02 01:36:22 +03:00
|
|
|
// creates routing table and test route
|
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()
|
2019-07-02 01:36:22 +03:00
|
|
|
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)
|
|
|
|
}
|
2019-07-02 01:36:22 +03:00
|
|
|
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)
|
|
|
|
}
|
2019-07-02 01:36:22 +03:00
|
|
|
testTableSize += 1
|
2019-07-02 00:57:27 +03:00
|
|
|
|
2019-07-02 01:36:22 +03:00
|
|
|
// overrides an existing route
|
|
|
|
// NOTE: the size of the table should not change
|
2019-07-02 00:57:27 +03:00
|
|
|
route.Metric = 100
|
|
|
|
route.Policy = OverrideIfExists
|
|
|
|
|
|
|
|
if err := table.Add(route); err != nil {
|
|
|
|
t.Errorf("error adding route: %s", err)
|
|
|
|
}
|
|
|
|
|
2019-07-02 01:36:22 +03:00
|
|
|
if table.Size() != testTableSize {
|
|
|
|
t.Errorf("invalid number of routes. expected: %d, given: %d", testTableSize, table.Size())
|
2019-07-02 00:57:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// dont add new route if it already exists
|
2019-07-02 01:36:22 +03:00
|
|
|
// NOTE: The size of the table should not change
|
2019-07-02 00:57:27 +03:00
|
|
|
route.Policy = IgnoreIfExists
|
|
|
|
|
|
|
|
if err := table.Add(route); err != nil {
|
|
|
|
t.Errorf("error adding route: %s", err)
|
|
|
|
}
|
|
|
|
|
2019-07-02 01:36:22 +03:00
|
|
|
if table.Size() != testTableSize {
|
|
|
|
t.Errorf("invalid number of routes. expected: %d, given: %d", testTableSize, table.Size())
|
2019-07-02 00:57:27 +03:00
|
|
|
}
|
|
|
|
|
2019-07-02 01:36:22 +03:00
|
|
|
// adding the same route under AddIfNotExists policy must error
|
2019-07-02 00:57:27 +03:00
|
|
|
route.Policy = AddIfNotExists
|
|
|
|
|
|
|
|
if err := table.Add(route); err != ErrDuplicateRoute {
|
|
|
|
t.Errorf("error adding route. Expected error: %s, Given: %s", ErrDuplicateRoute, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDelete(t *testing.T) {
|
2019-07-02 01:15:45 +03:00
|
|
|
table, route := testSetup()
|
2019-07-02 01:36:22 +03:00
|
|
|
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)
|
|
|
|
}
|
2019-07-02 01:36:22 +03:00
|
|
|
testTableSize += 1
|
2019-07-02 00:57:27 +03:00
|
|
|
|
|
|
|
// should fail to delete non-existant route
|
2019-07-02 01:36:22 +03:00
|
|
|
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, given: %s", ErrRouteNotFound, err)
|
|
|
|
}
|
|
|
|
|
2019-07-02 01:36:22 +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)
|
|
|
|
}
|
2019-07-02 01:36:22 +03:00
|
|
|
testTableSize -= 1
|
2019-07-02 00:57:27 +03:00
|
|
|
|
2019-07-02 01:36:22 +03:00
|
|
|
if table.Size() != testTableSize {
|
|
|
|
t.Errorf("invalid number of routes. expected: %d, given: %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()
|
2019-07-02 01:36:22 +03:00
|
|
|
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)
|
|
|
|
}
|
2019-07-02 01:36:22 +03:00
|
|
|
testTableSize += 1
|
2019-07-02 00:57:27 +03:00
|
|
|
|
2019-07-02 01:36:22 +03:00
|
|
|
// change the metric of the original route
|
|
|
|
// NOTE: this should NOT change the size of the table
|
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)
|
|
|
|
}
|
|
|
|
|
2019-07-02 01:36:22 +03:00
|
|
|
if table.Size() != testTableSize {
|
|
|
|
t.Errorf("invalid number of routes. expected: %d, given: %d", testTableSize, table.Size())
|
2019-07-02 00:57:27 +03:00
|
|
|
}
|
|
|
|
|
2019-07-02 01:36:22 +03:00
|
|
|
// NOTE: routing table routes on <destination, gateway, network>
|
|
|
|
// this should add a new route
|
2019-07-02 00:57:27 +03:00
|
|
|
route.Destination = "new.dest"
|
|
|
|
|
|
|
|
if err := table.Update(route); err != nil {
|
|
|
|
t.Errorf("error updating route: %s", err)
|
|
|
|
}
|
2019-07-02 01:36:22 +03:00
|
|
|
testTableSize += 1
|
2019-07-02 00:57:27 +03:00
|
|
|
|
|
|
|
// NOTE: default policy is AddIfNotExists so the new route will be added here
|
2019-07-02 01:36:22 +03:00
|
|
|
if table.Size() != testTableSize {
|
|
|
|
t.Errorf("invalid number of routes. expected: %d, given: %d", testTableSize, table.Size())
|
2019-07-02 00:57:27 +03:00
|
|
|
}
|
|
|
|
|
2019-07-02 01:36:22 +03:00
|
|
|
// NOTE: we are hashing routes on <destination, gateway, network>
|
|
|
|
// this should add a new route
|
2019-07-02 00:57:27 +03:00
|
|
|
route.Gateway = "new.gw"
|
|
|
|
|
|
|
|
if err := table.Update(route); err != nil {
|
|
|
|
t.Errorf("error updating route: %s", err)
|
|
|
|
}
|
2019-07-02 01:36:22 +03:00
|
|
|
testTableSize += 1
|
2019-07-02 00:57:27 +03:00
|
|
|
|
2019-07-02 01:36:22 +03:00
|
|
|
if table.Size() != testTableSize {
|
|
|
|
t.Errorf("invalid number of routes. expected: %d, given: %d", testTableSize, table.Size())
|
2019-07-02 00:57:27 +03:00
|
|
|
}
|
|
|
|
|
2019-07-02 01:36:22 +03:00
|
|
|
// this should NOT add a new route as we are setting the policy to IgnoreIfExists
|
2019-07-02 00:57:27 +03:00
|
|
|
route.Destination = "rand.dest"
|
|
|
|
route.Policy = IgnoreIfExists
|
|
|
|
|
|
|
|
if err := table.Update(route); err != ErrRouteNotFound {
|
|
|
|
t.Errorf("error updating route. Expected error: %s, given: %s", ErrRouteNotFound, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if table.Size() != 3 {
|
2019-07-02 01:36:22 +03:00
|
|
|
t.Errorf("invalid number of routes. expected: %d, given: %d", testTableSize, table.Size())
|
2019-07-02 00:57:27 +03:00
|
|
|
}
|
|
|
|
}
|
2019-07-02 01:15:45 +03:00
|
|
|
|
2019-07-02 01:19:35 +03:00
|
|
|
func TestList(t *testing.T) {
|
2019-07-02 01:15:45 +03:00
|
|
|
table, route := testSetup()
|
|
|
|
|
|
|
|
dest := []string{"one.svc", "two.svc", "three.svc"}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
routes, err := table.List()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error listing routes: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(routes) != len(dest) {
|
|
|
|
t.Errorf("incorrect number of routes listed. Expected: %d, Given: %d", len(dest), len(routes))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(routes) != table.Size() {
|
|
|
|
t.Errorf("mismatch number of routes and table size. Routes: %d, Size: %d", len(routes), table.Size())
|
|
|
|
}
|
|
|
|
}
|