2019-07-08 16:51:55 +01:00
|
|
|
package table
|
2019-07-01 22:57:27 +01:00
|
|
|
|
|
|
|
import "testing"
|
|
|
|
|
2019-07-01 23:15:45 +01:00
|
|
|
func testSetup() (Table, Route) {
|
2019-07-01 22:57:27 +01:00
|
|
|
table := NewTable()
|
|
|
|
|
2019-07-01 23:15:45 +01:00
|
|
|
route := Route{
|
2019-07-09 15:45:42 +01:00
|
|
|
Service: "dest.svc",
|
|
|
|
Gateway: "dest.gw",
|
|
|
|
Network: "dest.network",
|
|
|
|
Link: "det.link",
|
|
|
|
Metric: 10,
|
2019-07-01 22:57:27 +01:00
|
|
|
}
|
2019-07-01 23:15:45 +01:00
|
|
|
|
|
|
|
return table, route
|
2019-07-01 22:57:27 +01:00
|
|
|
}
|
|
|
|
|
2019-07-10 17:46:22 +01:00
|
|
|
func TestCreate(t *testing.T) {
|
2019-07-01 23:15:45 +01:00
|
|
|
table, route := testSetup()
|
2019-07-01 23:36:22 +01:00
|
|
|
testTableSize := table.Size()
|
2019-07-01 22:57:27 +01:00
|
|
|
|
2019-07-10 17:46:22 +01:00
|
|
|
if err := table.Create(route); err != nil {
|
2019-07-01 22:57:27 +01:00
|
|
|
t.Errorf("error adding route: %s", err)
|
|
|
|
}
|
2019-07-01 23:36:22 +01:00
|
|
|
testTableSize += 1
|
2019-07-01 22:57:27 +01:00
|
|
|
|
|
|
|
// adds new route for the original destination
|
|
|
|
route.Gateway = "dest.gw2"
|
|
|
|
|
2019-07-10 17:46:22 +01:00
|
|
|
if err := table.Create(route); err != nil {
|
2019-07-01 22:57:27 +01:00
|
|
|
t.Errorf("error adding route: %s", err)
|
|
|
|
}
|
2019-07-01 23:36:22 +01:00
|
|
|
testTableSize += 1
|
2019-07-01 22:57:27 +01:00
|
|
|
|
2019-07-01 23:36:22 +01:00
|
|
|
if table.Size() != testTableSize {
|
2019-07-09 15:45:42 +01:00
|
|
|
t.Errorf("invalid number of routes. Expected: %d, found: %d", testTableSize, table.Size())
|
2019-07-01 22:57:27 +01:00
|
|
|
}
|
|
|
|
|
2019-07-08 16:16:50 +01:00
|
|
|
// adding the same route under Insert policy must error
|
2019-07-10 17:46:22 +01:00
|
|
|
if err := table.Create(route); err != ErrDuplicateRoute {
|
2019-07-08 21:03:54 +01:00
|
|
|
t.Errorf("error adding route. Expected error: %s, found: %s", ErrDuplicateRoute, err)
|
2019-07-01 22:57:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDelete(t *testing.T) {
|
2019-07-01 23:15:45 +01:00
|
|
|
table, route := testSetup()
|
2019-07-01 23:36:22 +01:00
|
|
|
testTableSize := table.Size()
|
2019-07-01 22:57:27 +01:00
|
|
|
|
2019-07-10 17:46:22 +01:00
|
|
|
if err := table.Create(route); err != nil {
|
2019-07-01 22:57:27 +01:00
|
|
|
t.Errorf("error adding route: %s", err)
|
|
|
|
}
|
2019-07-01 23:36:22 +01:00
|
|
|
testTableSize += 1
|
2019-07-01 22:57:27 +01:00
|
|
|
|
|
|
|
// should fail to delete non-existant route
|
2019-07-09 15:45:42 +01:00
|
|
|
prevSvc := route.Service
|
|
|
|
route.Service = "randDest"
|
2019-07-01 22:57:27 +01:00
|
|
|
|
|
|
|
if err := table.Delete(route); err != ErrRouteNotFound {
|
2019-07-09 15:45:42 +01:00
|
|
|
t.Errorf("error deleting route. Expected: %s, found: %s", ErrRouteNotFound, err)
|
2019-07-01 22:57:27 +01:00
|
|
|
}
|
|
|
|
|
2019-07-01 23:36:22 +01:00
|
|
|
// we should be able to delete the existing route
|
2019-07-09 15:45:42 +01:00
|
|
|
route.Service = prevSvc
|
2019-07-01 22:57:27 +01:00
|
|
|
|
|
|
|
if err := table.Delete(route); err != nil {
|
|
|
|
t.Errorf("error deleting route: %s", err)
|
|
|
|
}
|
2019-07-01 23:36:22 +01:00
|
|
|
testTableSize -= 1
|
2019-07-01 22:57:27 +01:00
|
|
|
|
2019-07-01 23:36:22 +01:00
|
|
|
if table.Size() != testTableSize {
|
2019-07-09 15:45:42 +01:00
|
|
|
t.Errorf("invalid number of routes. Expected: %d, found: %d", testTableSize, table.Size())
|
2019-07-01 22:57:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdate(t *testing.T) {
|
2019-07-01 23:15:45 +01:00
|
|
|
table, route := testSetup()
|
2019-07-01 23:36:22 +01:00
|
|
|
testTableSize := table.Size()
|
2019-07-01 22:57:27 +01:00
|
|
|
|
2019-07-10 17:46:22 +01:00
|
|
|
if err := table.Create(route); err != nil {
|
2019-07-01 22:57:27 +01:00
|
|
|
t.Errorf("error adding route: %s", err)
|
|
|
|
}
|
2019-07-01 23:36:22 +01:00
|
|
|
testTableSize += 1
|
2019-07-01 22:57:27 +01:00
|
|
|
|
2019-07-01 23:36:22 +01:00
|
|
|
// change the metric of the original route
|
2019-07-01 22:57:27 +01:00
|
|
|
route.Metric = 200
|
|
|
|
|
|
|
|
if err := table.Update(route); err != nil {
|
|
|
|
t.Errorf("error updating route: %s", err)
|
|
|
|
}
|
|
|
|
|
2019-07-08 16:16:50 +01:00
|
|
|
// the size of the table should not change as we're only updating the metric of an existing route
|
2019-07-01 23:36:22 +01:00
|
|
|
if table.Size() != testTableSize {
|
2019-07-09 15:45:42 +01:00
|
|
|
t.Errorf("invalid number of routes. Expected: %d, found: %d", testTableSize, table.Size())
|
2019-07-01 22:57:27 +01:00
|
|
|
}
|
|
|
|
|
2019-07-08 21:03:54 +01:00
|
|
|
// this should error as the destination does not exist
|
2019-07-09 15:45:42 +01:00
|
|
|
route.Service = "rand.dest"
|
2019-07-01 22:57:27 +01:00
|
|
|
|
2019-07-08 21:03:54 +01:00
|
|
|
if err := table.Update(route); err != ErrRouteNotFound {
|
|
|
|
t.Errorf("error updating route. Expected error: %s, found: %s", ErrRouteNotFound, err)
|
2019-07-01 22:57:27 +01:00
|
|
|
}
|
|
|
|
|
2019-07-01 23:36:22 +01:00
|
|
|
if table.Size() != testTableSize {
|
2019-07-09 15:45:42 +01:00
|
|
|
t.Errorf("invalid number of routes. Expected: %d, found: %d", testTableSize, table.Size())
|
2019-07-01 22:57:27 +01:00
|
|
|
}
|
2019-07-08 21:03:54 +01:00
|
|
|
}
|
2019-07-01 22:57:27 +01:00
|
|
|
|
2019-07-08 21:03:54 +01:00
|
|
|
func TestList(t *testing.T) {
|
|
|
|
table, route := testSetup()
|
2019-07-01 22:57:27 +01:00
|
|
|
|
2019-07-09 15:45:42 +01:00
|
|
|
svc := []string{"one.svc", "two.svc", "three.svc"}
|
2019-07-01 22:57:27 +01:00
|
|
|
|
2019-07-09 15:45:42 +01:00
|
|
|
for i := 0; i < len(svc); i++ {
|
|
|
|
route.Service = svc[i]
|
2019-07-10 17:46:22 +01:00
|
|
|
if err := table.Create(route); err != nil {
|
2019-07-08 21:03:54 +01:00
|
|
|
t.Errorf("error adding route: %s", err)
|
|
|
|
}
|
2019-07-01 22:57:27 +01:00
|
|
|
}
|
|
|
|
|
2019-07-08 21:03:54 +01:00
|
|
|
routes, err := table.List()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error listing routes: %s", err)
|
|
|
|
}
|
2019-07-01 22:57:27 +01:00
|
|
|
|
2019-07-09 15:45:42 +01:00
|
|
|
if len(routes) != len(svc) {
|
|
|
|
t.Errorf("incorrect number of routes listed. Expected: %d, found: %d", len(svc), len(routes))
|
2019-07-01 22:57:27 +01:00
|
|
|
}
|
|
|
|
|
2019-07-08 21:03:54 +01:00
|
|
|
if len(routes) != table.Size() {
|
2019-07-09 15:45:42 +01:00
|
|
|
t.Errorf("mismatch number of routes and table size. Expected: %d, found: %d", len(routes), table.Size())
|
2019-07-01 22:57:27 +01:00
|
|
|
}
|
|
|
|
}
|
2019-07-01 23:15:45 +01:00
|
|
|
|
2019-07-08 21:03:54 +01:00
|
|
|
func TestLookup(t *testing.T) {
|
2019-07-01 23:15:45 +01:00
|
|
|
table, route := testSetup()
|
|
|
|
|
2019-07-09 15:45:42 +01:00
|
|
|
svc := []string{"svc1", "svc2", "svc3"}
|
2019-07-08 21:03:54 +01:00
|
|
|
net := []string{"net1", "net2", "net1"}
|
2019-07-09 15:45:42 +01:00
|
|
|
gw := []string{"gw1", "gw2", "gw3"}
|
2019-07-01 23:15:45 +01:00
|
|
|
|
2019-07-09 15:45:42 +01:00
|
|
|
for i := 0; i < len(svc); i++ {
|
|
|
|
route.Service = svc[i]
|
2019-07-08 21:03:54 +01:00
|
|
|
route.Network = net[i]
|
2019-07-09 15:45:42 +01:00
|
|
|
route.Gateway = gw[i]
|
2019-07-10 17:46:22 +01:00
|
|
|
if err := table.Create(route); err != nil {
|
2019-07-01 23:15:45 +01:00
|
|
|
t.Errorf("error adding route: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-08 21:03:54 +01:00
|
|
|
// return all routes
|
|
|
|
query := NewQuery()
|
|
|
|
|
|
|
|
routes, err := table.Lookup(query)
|
2019-07-01 23:15:45 +01:00
|
|
|
if err != nil {
|
2019-07-08 21:03:54 +01:00
|
|
|
t.Errorf("error looking up routes: %s", err)
|
2019-07-01 23:15:45 +01:00
|
|
|
}
|
|
|
|
|
2019-07-08 21:03:54 +01:00
|
|
|
if len(routes) != table.Size() {
|
2019-07-09 15:45:42 +01:00
|
|
|
t.Errorf("incorrect number of routes returned. Expected: %d, found: %d", table.Size(), len(routes))
|
2019-07-01 23:15:45 +01:00
|
|
|
}
|
|
|
|
|
2019-07-08 21:03:54 +01: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 {
|
2019-07-09 15:45:42 +01:00
|
|
|
t.Errorf("incorrect number of routes returned. Expected: %d, found: %d", 2, len(routes))
|
2019-07-08 21:03:54 +01:00
|
|
|
}
|
|
|
|
|
2019-07-09 15:45:42 +01:00
|
|
|
// query particular gateway
|
|
|
|
gateway := "gw1"
|
|
|
|
query = NewQuery(QueryGateway(gateway))
|
2019-07-08 21:03:54 +01:00
|
|
|
|
|
|
|
routes, err = table.Lookup(query)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error looking up routes: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(routes) != 1 {
|
2019-07-09 15:45:42 +01:00
|
|
|
t.Errorf("incorrect number of routes returned. Expected: %d, found: %d", 1, len(routes))
|
2019-07-08 21:03:54 +01:00
|
|
|
}
|
|
|
|
|
2019-07-09 15:45:42 +01:00
|
|
|
if routes[0].Gateway != gateway {
|
|
|
|
t.Errorf("incorrect route returned. Expected gateway: %s, found: %s", gateway, routes[0].Gateway)
|
2019-07-08 21:03:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// query particular route
|
|
|
|
network := "net1"
|
|
|
|
query = NewQuery(
|
2019-07-09 15:45:42 +01:00
|
|
|
QueryGateway(gateway),
|
2019-07-08 21:03:54 +01:00
|
|
|
QueryNetwork(network),
|
|
|
|
)
|
|
|
|
|
|
|
|
routes, err = table.Lookup(query)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error looking up routes: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(routes) != 1 {
|
2019-07-09 15:45:42 +01:00
|
|
|
t.Errorf("incorrect number of routes returned. Expected: %d, found: %d", 1, len(routes))
|
2019-07-08 21:03:54 +01:00
|
|
|
}
|
|
|
|
|
2019-07-09 15:45:42 +01:00
|
|
|
if routes[0].Gateway != gateway {
|
|
|
|
t.Errorf("incorrect route returned. Expected gateway: %s, found: %s", gateway, routes[0].Gateway)
|
2019-07-08 21:03:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if routes[0].Network != network {
|
|
|
|
t.Errorf("incorrect network returned. Expected network: %s, found: %s", network, routes[0].Network)
|
|
|
|
}
|
|
|
|
|
|
|
|
// bullshit route query
|
2019-07-09 15:45:42 +01:00
|
|
|
query = NewQuery(QueryService("foobar"))
|
2019-07-08 21:03:54 +01:00
|
|
|
|
|
|
|
routes, err = table.Lookup(query)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error looking up routes: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(routes) != 0 {
|
2019-07-09 15:45:42 +01:00
|
|
|
t.Errorf("incorrect number of routes returned. Expected: %d, found: %d", 0, len(routes))
|
2019-07-01 23:15:45 +01:00
|
|
|
}
|
|
|
|
}
|