diff --git a/network/router/default_table.go b/network/router/default_table.go index ad4510dd..3297c028 100644 --- a/network/router/default_table.go +++ b/network/router/default_table.go @@ -138,6 +138,12 @@ func (t *table) Update(r Route) error { return ErrRouteNotFound } + if _, ok := t.m[destAddr][sum]; !ok && r.Policy == AddIfNotExists { + t.m[destAddr][sum] = r + go t.sendEvent(&Event{Type: CreateEvent, Route: r}) + return nil + } + // if the route has been found update it if _, ok := t.m[destAddr][sum]; ok { t.m[destAddr][sum] = r @@ -248,7 +254,12 @@ func (t *table) Size() int { t.RLock() defer t.RUnlock() - return len(t.m) + size := 0 + for dest, _ := range t.m { + size += len(t.m[dest]) + } + + return size } // String returns debug information diff --git a/network/router/default_table_test.go b/network/router/default_table_test.go new file mode 100644 index 00000000..93f28730 --- /dev/null +++ b/network/router/default_table_test.go @@ -0,0 +1,193 @@ +package router + +import "testing" + +func TestNewTable(t *testing.T) { + table := NewTable() + + if table.Size() != 0 { + t.Errorf("new table should be empty") + } +} + +func TestAdd(t *testing.T) { + table := NewTable() + + if table.Size() != 0 { + t.Errorf("new table should be empty") + } + + route := Route{ + Destination: "dest.svc", + Gateway: "dest.gw", + Router: "dest.router", + Network: "dest.network", + Metric: 10, + } + + if err := table.Add(route); err != nil { + t.Errorf("error adding route: %s", err) + } + + if table.Size() != 1 { + t.Errorf("invalid number of routes. expected: 1, given: %d", table.Size()) + } + + // 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) + } + + if table.Size() != 2 { + t.Errorf("invalid number of routes. expected: 2, given: %d", table.Size()) + } + + // overrides an existing route: the size of the table does not change + route.Metric = 100 + route.Policy = OverrideIfExists + + if err := table.Add(route); err != nil { + t.Errorf("error adding route: %s", err) + } + + if table.Size() != 2 { + t.Errorf("invalid number of routes. expected: 2, given: %d", table.Size()) + } + + // dont add new route if it already exists + route.Policy = IgnoreIfExists + + if err := table.Add(route); err != nil { + t.Errorf("error adding route: %s", err) + } + + if table.Size() != 2 { + t.Errorf("invalid number of routes. expected: 2, given: %d", table.Size()) + } + + // adding the same route under this policy should error + 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) { + table := NewTable() + + if table.Size() != 0 { + t.Errorf("new table should be empty") + } + + route := Route{ + Destination: "dest.svc", + Gateway: "dest.gw", + Router: "dest.router", + Network: "dest.network", + Metric: 10, + } + + if err := table.Add(route); err != nil { + t.Errorf("error adding route: %s", err) + } + + if table.Size() != 1 { + t.Errorf("invalid number of routes. expected: 1, given: %d", table.Size()) + } + + // should fail to delete non-existant route + oldDest := route.Destination + route.Destination = "randDest" + + if err := table.Delete(route); err != ErrRouteNotFound { + t.Errorf("error deleting route. Expected error: %s, given: %s", ErrRouteNotFound, err) + } + + if table.Size() != 1 { + t.Errorf("invalid number of routes. expected: %d, given: %d", 1, table.Size()) + } + + // we should be able to delete the routes now + route.Destination = oldDest + + if err := table.Delete(route); err != nil { + t.Errorf("error deleting route: %s", err) + } + + if table.Size() != 0 { + t.Errorf("invalid number of routes. expected: %d, given: %d", 0, table.Size()) + } +} + +func TestUpdate(t *testing.T) { + table := NewTable() + + if table.Size() != 0 { + t.Errorf("new table should be empty") + } + + route := Route{ + Destination: "dest.svc", + Gateway: "dest.gw", + Router: "dest.router", + Network: "dest.network", + Metric: 10, + } + + if err := table.Add(route); err != nil { + t.Errorf("error adding route: %s", err) + } + + if table.Size() != 1 { + t.Errorf("invalid number of routes. expected: 1, given: %d", table.Size()) + } + + route.Metric = 200 + + if err := table.Update(route); err != nil { + t.Errorf("error updating route: %s", err) + } + + if table.Size() != 1 { + t.Errorf("invalid number of routes. expected: 1, given: %d", table.Size()) + } + + // this should add a new route; we are hashing routes on + route.Destination = "new.dest" + + if err := table.Update(route); err != nil { + t.Errorf("error updating route: %s", err) + } + + // NOTE: default policy is AddIfNotExists so the new route will be added here + if table.Size() != 2 { + t.Errorf("invalid number of routes. expected: 2, given: %d", table.Size()) + } + + // this should add a new route; we are hashing routes on + route.Gateway = "new.gw" + + if err := table.Update(route); err != nil { + t.Errorf("error updating route: %s", err) + } + + // NOTE: default policy is AddIfNotExists so the new route will be added here + if table.Size() != 3 { + 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 + 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 { + t.Errorf("invalid number of routes. expected: 3, given: %d", table.Size()) + } +} diff --git a/network/router/route.go b/network/router/route.go index 1f45b688..50526533 100644 --- a/network/router/route.go +++ b/network/router/route.go @@ -18,10 +18,10 @@ var ( type RoutePolicy int const ( - // OverrideIfExists overrides route if it already exists - OverrideIfExists RoutePolicy = iota // AddIfNotExist adds the route if it does not exist - AddIfNotExists + AddIfNotExists RoutePolicy = iota + // OverrideIfExists overrides route if it already exists + OverrideIfExists // IgnoreIfExists instructs to not modify existing route IgnoreIfExists )