Stop hardcoding table sizes; increment as you move on.
This commit is contained in:
parent
8f22e61a8b
commit
0e1fcc4f28
@ -2,6 +2,7 @@ package router
|
|||||||
|
|
||||||
import "testing"
|
import "testing"
|
||||||
|
|
||||||
|
// creates routing table and test route
|
||||||
func testSetup() (Table, Route) {
|
func testSetup() (Table, Route) {
|
||||||
table := NewTable()
|
table := NewTable()
|
||||||
|
|
||||||
@ -18,14 +19,12 @@ func testSetup() (Table, Route) {
|
|||||||
|
|
||||||
func TestAdd(t *testing.T) {
|
func TestAdd(t *testing.T) {
|
||||||
table, route := testSetup()
|
table, route := testSetup()
|
||||||
|
testTableSize := table.Size()
|
||||||
|
|
||||||
if err := table.Add(route); err != nil {
|
if err := table.Add(route); err != nil {
|
||||||
t.Errorf("error adding route: %s", err)
|
t.Errorf("error adding route: %s", err)
|
||||||
}
|
}
|
||||||
|
testTableSize += 1
|
||||||
if table.Size() != 1 {
|
|
||||||
t.Errorf("invalid number of routes. expected: 1, given: %d", table.Size())
|
|
||||||
}
|
|
||||||
|
|
||||||
// adds new route for the original destination
|
// adds new route for the original destination
|
||||||
route.Gateway = "dest.gw2"
|
route.Gateway = "dest.gw2"
|
||||||
@ -33,12 +32,10 @@ func TestAdd(t *testing.T) {
|
|||||||
if err := table.Add(route); err != nil {
|
if err := table.Add(route); err != nil {
|
||||||
t.Errorf("error adding route: %s", err)
|
t.Errorf("error adding route: %s", err)
|
||||||
}
|
}
|
||||||
|
testTableSize += 1
|
||||||
|
|
||||||
if table.Size() != 2 {
|
// overrides an existing route
|
||||||
t.Errorf("invalid number of routes. expected: 2, given: %d", table.Size())
|
// NOTE: the size of the table should not change
|
||||||
}
|
|
||||||
|
|
||||||
// overrides an existing route: the size of the table does not change
|
|
||||||
route.Metric = 100
|
route.Metric = 100
|
||||||
route.Policy = OverrideIfExists
|
route.Policy = OverrideIfExists
|
||||||
|
|
||||||
@ -46,22 +43,23 @@ func TestAdd(t *testing.T) {
|
|||||||
t.Errorf("error adding route: %s", err)
|
t.Errorf("error adding route: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if table.Size() != 2 {
|
if table.Size() != testTableSize {
|
||||||
t.Errorf("invalid number of routes. expected: 2, given: %d", table.Size())
|
t.Errorf("invalid number of routes. expected: %d, given: %d", testTableSize, table.Size())
|
||||||
}
|
}
|
||||||
|
|
||||||
// dont add new route if it already exists
|
// dont add new route if it already exists
|
||||||
|
// NOTE: The size of the table should not change
|
||||||
route.Policy = IgnoreIfExists
|
route.Policy = IgnoreIfExists
|
||||||
|
|
||||||
if err := table.Add(route); err != nil {
|
if err := table.Add(route); err != nil {
|
||||||
t.Errorf("error adding route: %s", err)
|
t.Errorf("error adding route: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if table.Size() != 2 {
|
if table.Size() != testTableSize {
|
||||||
t.Errorf("invalid number of routes. expected: 2, given: %d", table.Size())
|
t.Errorf("invalid number of routes. expected: %d, given: %d", testTableSize, table.Size())
|
||||||
}
|
}
|
||||||
|
|
||||||
// adding the same route under this policy should error
|
// adding the same route under AddIfNotExists policy must error
|
||||||
route.Policy = AddIfNotExists
|
route.Policy = AddIfNotExists
|
||||||
|
|
||||||
if err := table.Add(route); err != ErrDuplicateRoute {
|
if err := table.Add(route); err != ErrDuplicateRoute {
|
||||||
@ -71,85 +69,83 @@ func TestAdd(t *testing.T) {
|
|||||||
|
|
||||||
func TestDelete(t *testing.T) {
|
func TestDelete(t *testing.T) {
|
||||||
table, route := testSetup()
|
table, route := testSetup()
|
||||||
|
testTableSize := table.Size()
|
||||||
|
|
||||||
if err := table.Add(route); err != nil {
|
if err := table.Add(route); err != nil {
|
||||||
t.Errorf("error adding route: %s", err)
|
t.Errorf("error adding route: %s", err)
|
||||||
}
|
}
|
||||||
|
testTableSize += 1
|
||||||
if table.Size() != 1 {
|
|
||||||
t.Errorf("invalid number of routes. expected: 1, given: %d", table.Size())
|
|
||||||
}
|
|
||||||
|
|
||||||
// should fail to delete non-existant route
|
// should fail to delete non-existant route
|
||||||
oldDest := route.Destination
|
prevDest := route.Destination
|
||||||
route.Destination = "randDest"
|
route.Destination = "randDest"
|
||||||
|
|
||||||
if err := table.Delete(route); err != ErrRouteNotFound {
|
if err := table.Delete(route); err != ErrRouteNotFound {
|
||||||
t.Errorf("error deleting route. Expected error: %s, given: %s", ErrRouteNotFound, err)
|
t.Errorf("error deleting route. Expected error: %s, given: %s", ErrRouteNotFound, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if table.Size() != 1 {
|
// we should be able to delete the existing route
|
||||||
t.Errorf("invalid number of routes. expected: %d, given: %d", 1, table.Size())
|
route.Destination = prevDest
|
||||||
}
|
|
||||||
|
|
||||||
// we should be able to delete the routes now
|
|
||||||
route.Destination = oldDest
|
|
||||||
|
|
||||||
if err := table.Delete(route); err != nil {
|
if err := table.Delete(route); err != nil {
|
||||||
t.Errorf("error deleting route: %s", err)
|
t.Errorf("error deleting route: %s", err)
|
||||||
}
|
}
|
||||||
|
testTableSize -= 1
|
||||||
|
|
||||||
if table.Size() != 0 {
|
if table.Size() != testTableSize {
|
||||||
t.Errorf("invalid number of routes. expected: %d, given: %d", 0, table.Size())
|
t.Errorf("invalid number of routes. expected: %d, given: %d", testTableSize, table.Size())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUpdate(t *testing.T) {
|
func TestUpdate(t *testing.T) {
|
||||||
table, route := testSetup()
|
table, route := testSetup()
|
||||||
|
testTableSize := table.Size()
|
||||||
|
|
||||||
if err := table.Add(route); err != nil {
|
if err := table.Add(route); err != nil {
|
||||||
t.Errorf("error adding route: %s", err)
|
t.Errorf("error adding route: %s", err)
|
||||||
}
|
}
|
||||||
|
testTableSize += 1
|
||||||
|
|
||||||
if table.Size() != 1 {
|
// change the metric of the original route
|
||||||
t.Errorf("invalid number of routes. expected: 1, given: %d", table.Size())
|
// NOTE: this should NOT change the size of the table
|
||||||
}
|
|
||||||
|
|
||||||
route.Metric = 200
|
route.Metric = 200
|
||||||
|
|
||||||
if err := table.Update(route); err != nil {
|
if err := table.Update(route); err != nil {
|
||||||
t.Errorf("error updating route: %s", err)
|
t.Errorf("error updating route: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if table.Size() != 1 {
|
if table.Size() != testTableSize {
|
||||||
t.Errorf("invalid number of routes. expected: 1, given: %d", table.Size())
|
t.Errorf("invalid number of routes. expected: %d, given: %d", testTableSize, table.Size())
|
||||||
}
|
}
|
||||||
|
|
||||||
// this should add a new route; we are hashing routes on <destination, gateway, network>
|
// NOTE: routing table routes on <destination, gateway, network>
|
||||||
|
// this should add a new route
|
||||||
route.Destination = "new.dest"
|
route.Destination = "new.dest"
|
||||||
|
|
||||||
if err := table.Update(route); err != nil {
|
if err := table.Update(route); err != nil {
|
||||||
t.Errorf("error updating route: %s", err)
|
t.Errorf("error updating route: %s", err)
|
||||||
}
|
}
|
||||||
|
testTableSize += 1
|
||||||
|
|
||||||
// NOTE: default policy is AddIfNotExists so the new route will be added here
|
// NOTE: default policy is AddIfNotExists so the new route will be added here
|
||||||
if table.Size() != 2 {
|
if table.Size() != testTableSize {
|
||||||
t.Errorf("invalid number of routes. expected: 2, given: %d", table.Size())
|
t.Errorf("invalid number of routes. expected: %d, given: %d", testTableSize, table.Size())
|
||||||
}
|
}
|
||||||
|
|
||||||
// this should add a new route; we are hashing routes on <destination, gateway, network>
|
// NOTE: we are hashing routes on <destination, gateway, network>
|
||||||
|
// this should add a new route
|
||||||
route.Gateway = "new.gw"
|
route.Gateway = "new.gw"
|
||||||
|
|
||||||
if err := table.Update(route); err != nil {
|
if err := table.Update(route); err != nil {
|
||||||
t.Errorf("error updating route: %s", err)
|
t.Errorf("error updating route: %s", err)
|
||||||
}
|
}
|
||||||
|
testTableSize += 1
|
||||||
|
|
||||||
// NOTE: default policy is AddIfNotExists so the new route will be added here
|
if table.Size() != testTableSize {
|
||||||
if table.Size() != 3 {
|
t.Errorf("invalid number of routes. expected: %d, given: %d", testTableSize, table.Size())
|
||||||
t.Errorf("invalid number of routes. expected: 3, given: %d", table.Size())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// this should NOTE add a new route; we are setting the policy to IgnoreIfExists
|
// this should NOT add a new route as we are setting the policy to IgnoreIfExists
|
||||||
route.Destination = "rand.dest"
|
route.Destination = "rand.dest"
|
||||||
route.Policy = IgnoreIfExists
|
route.Policy = IgnoreIfExists
|
||||||
|
|
||||||
@ -158,7 +154,7 @@ func TestUpdate(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if table.Size() != 3 {
|
if table.Size() != 3 {
|
||||||
t.Errorf("invalid number of routes. expected: 3, given: %d", table.Size())
|
t.Errorf("invalid number of routes. expected: %d, given: %d", testTableSize, table.Size())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user