2019-07-26 01:19:05 +03:00
|
|
|
package router
|
2019-07-02 00:57:27 +03:00
|
|
|
|
2020-01-16 15:48:36 +03:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
)
|
2019-07-02 00:57:27 +03:00
|
|
|
|
2019-07-29 14:40:13 +03:00
|
|
|
func testSetup() (*table, Route) {
|
2020-06-30 11:53:49 +03:00
|
|
|
router := newRouter().(*router)
|
|
|
|
table := router.table
|
2019-07-02 00:57:27 +03:00
|
|
|
|
2019-07-02 01:15:45 +03:00
|
|
|
route := Route{
|
2019-07-09 17:45:42 +03:00
|
|
|
Service: "dest.svc",
|
2019-11-05 20:44:24 +03:00
|
|
|
Address: "dest.addr",
|
2019-07-09 17:45:42 +03:00
|
|
|
Gateway: "dest.gw",
|
|
|
|
Network: "dest.network",
|
2019-08-28 01:08:35 +03:00
|
|
|
Router: "src.router",
|
2019-07-09 17:45:42 +03:00
|
|
|
Link: "det.link",
|
|
|
|
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-10 19:46:22 +03:00
|
|
|
func TestCreate(t *testing.T) {
|
2019-07-02 01:15:45 +03:00
|
|
|
table, route := testSetup()
|
2019-07-02 00:57:27 +03:00
|
|
|
|
2019-07-10 19:46:22 +03:00
|
|
|
if err := table.Create(route); err != nil {
|
2019-07-02 00:57:27 +03:00
|
|
|
t.Errorf("error adding route: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// adds new route for the original destination
|
|
|
|
route.Gateway = "dest.gw2"
|
|
|
|
|
2019-07-10 19:46:22 +03:00
|
|
|
if err := table.Create(route); err != nil {
|
2019-07-02 00:57:27 +03:00
|
|
|
t.Errorf("error adding route: %s", err)
|
|
|
|
}
|
|
|
|
|
2019-07-08 18:16:50 +03:00
|
|
|
// adding the same route under Insert policy must error
|
2019-07-10 19:46:22 +03:00
|
|
|
if err := table.Create(route); err != ErrDuplicateRoute {
|
2019-07-08 23:03:54 +03:00
|
|
|
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()
|
2019-07-02 00:57:27 +03:00
|
|
|
|
2019-07-10 19:46:22 +03:00
|
|
|
if err := table.Create(route); err != nil {
|
2019-07-02 00:57:27 +03:00
|
|
|
t.Errorf("error adding route: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// should fail to delete non-existant route
|
2019-07-09 17:45:42 +03:00
|
|
|
prevSvc := route.Service
|
|
|
|
route.Service = "randDest"
|
2019-07-02 00:57:27 +03:00
|
|
|
|
|
|
|
if err := table.Delete(route); err != ErrRouteNotFound {
|
2019-07-09 17:45:42 +03:00
|
|
|
t.Errorf("error deleting route. Expected: %s, found: %s", ErrRouteNotFound, err)
|
2019-07-02 00:57:27 +03:00
|
|
|
}
|
|
|
|
|
2019-07-02 01:36:22 +03:00
|
|
|
// we should be able to delete the existing route
|
2019-07-09 17:45:42 +03:00
|
|
|
route.Service = prevSvc
|
2019-07-02 00:57:27 +03:00
|
|
|
|
|
|
|
if err := table.Delete(route); err != nil {
|
|
|
|
t.Errorf("error deleting route: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdate(t *testing.T) {
|
2019-07-02 01:15:45 +03:00
|
|
|
table, route := testSetup()
|
2019-07-02 00:57:27 +03:00
|
|
|
|
2019-07-10 19:46:22 +03:00
|
|
|
if err := table.Create(route); err != nil {
|
2019-07-02 00:57:27 +03:00
|
|
|
t.Errorf("error adding route: %s", err)
|
|
|
|
}
|
|
|
|
|
2019-07-02 01:36:22 +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)
|
|
|
|
}
|
|
|
|
|
2019-07-10 23:28:32 +03:00
|
|
|
// this should add a new route
|
2019-07-09 17:45:42 +03:00
|
|
|
route.Service = "rand.dest"
|
2019-07-02 00:57:27 +03:00
|
|
|
|
2019-07-10 23:28:32 +03:00
|
|
|
if err := table.Update(route); err != nil {
|
|
|
|
t.Errorf("error updating route: %s", err)
|
2019-07-02 00:57:27 +03:00
|
|
|
}
|
2019-07-08 23:03:54 +03:00
|
|
|
}
|
2019-07-02 00:57:27 +03:00
|
|
|
|
2019-07-08 23:03:54 +03:00
|
|
|
func TestList(t *testing.T) {
|
|
|
|
table, route := testSetup()
|
2019-07-02 00:57:27 +03:00
|
|
|
|
2019-07-09 17:45:42 +03:00
|
|
|
svc := []string{"one.svc", "two.svc", "three.svc"}
|
2019-07-02 00:57:27 +03:00
|
|
|
|
2019-07-09 17:45:42 +03:00
|
|
|
for i := 0; i < len(svc); i++ {
|
|
|
|
route.Service = svc[i]
|
2019-07-10 19:46:22 +03:00
|
|
|
if err := table.Create(route); err != nil {
|
2019-07-08 23:03:54 +03:00
|
|
|
t.Errorf("error adding route: %s", err)
|
|
|
|
}
|
2019-07-02 00:57:27 +03:00
|
|
|
}
|
|
|
|
|
2019-07-08 23:03:54 +03:00
|
|
|
routes, err := table.List()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error listing routes: %s", err)
|
|
|
|
}
|
2019-07-02 00:57:27 +03:00
|
|
|
|
2019-07-09 17:45:42 +03:00
|
|
|
if len(routes) != len(svc) {
|
|
|
|
t.Errorf("incorrect number of routes listed. Expected: %d, found: %d", len(svc), len(routes))
|
2019-07-02 00:57:27 +03:00
|
|
|
}
|
|
|
|
}
|
2019-07-02 01:15:45 +03:00
|
|
|
|
2019-07-29 20:57:40 +03:00
|
|
|
func TestQuery(t *testing.T) {
|
2019-07-02 01:15:45 +03:00
|
|
|
table, route := testSetup()
|
|
|
|
|
2020-01-16 15:48:36 +03:00
|
|
|
svc := []string{"svc1", "svc2", "svc3", "svc1"}
|
|
|
|
net := []string{"net1", "net2", "net1", "net3"}
|
|
|
|
gw := []string{"gw1", "gw2", "gw3", "gw3"}
|
|
|
|
rtr := []string{"rtr1", "rt2", "rt3", "rtr3"}
|
2019-07-02 01:15:45 +03:00
|
|
|
|
2019-07-09 17:45:42 +03:00
|
|
|
for i := 0; i < len(svc); i++ {
|
|
|
|
route.Service = svc[i]
|
2019-07-08 23:03:54 +03:00
|
|
|
route.Network = net[i]
|
2019-07-09 17:45:42 +03:00
|
|
|
route.Gateway = gw[i]
|
2019-08-28 01:08:35 +03:00
|
|
|
route.Router = rtr[i]
|
2019-07-10 19:46:22 +03:00
|
|
|
if err := table.Create(route); err != nil {
|
2019-07-02 01:15:45 +03:00
|
|
|
t.Errorf("error adding route: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-08 23:03:54 +03:00
|
|
|
// return all routes
|
2019-10-09 19:13:52 +03:00
|
|
|
routes, err := table.Query()
|
2019-07-02 01:15:45 +03:00
|
|
|
if err != nil {
|
2019-07-08 23:03:54 +03:00
|
|
|
t.Errorf("error looking up routes: %s", err)
|
2019-12-03 22:59:44 +03:00
|
|
|
} else if len(routes) == 0 {
|
|
|
|
t.Errorf("error looking up routes: not found")
|
2019-07-02 01:15:45 +03:00
|
|
|
}
|
|
|
|
|
2019-08-28 01:08:35 +03:00
|
|
|
// query routes particular network
|
|
|
|
network := "net1"
|
2019-07-08 23:03:54 +03:00
|
|
|
|
2019-10-09 19:13:52 +03:00
|
|
|
routes, err = table.Query(QueryNetwork(network))
|
2019-07-08 23:03:54 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error looking up routes: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(routes) != 2 {
|
2019-07-09 17:45:42 +03:00
|
|
|
t.Errorf("incorrect number of routes returned. Expected: %d, found: %d", 2, len(routes))
|
2019-07-08 23:03:54 +03:00
|
|
|
}
|
|
|
|
|
2019-08-28 01:08:35 +03:00
|
|
|
for _, route := range routes {
|
|
|
|
if route.Network != network {
|
|
|
|
t.Errorf("incorrect route returned. Expected network: %s, found: %s", network, route.Network)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// query routes for particular gateway
|
2019-07-09 17:45:42 +03:00
|
|
|
gateway := "gw1"
|
2019-07-08 23:03:54 +03:00
|
|
|
|
2019-10-09 19:13:52 +03:00
|
|
|
routes, err = table.Query(QueryGateway(gateway))
|
2019-07-08 23:03:54 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error looking up routes: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(routes) != 1 {
|
2019-07-09 17:45:42 +03:00
|
|
|
t.Errorf("incorrect number of routes returned. Expected: %d, found: %d", 1, len(routes))
|
2019-07-08 23:03:54 +03:00
|
|
|
}
|
|
|
|
|
2019-07-09 17:45:42 +03:00
|
|
|
if routes[0].Gateway != gateway {
|
|
|
|
t.Errorf("incorrect route returned. Expected gateway: %s, found: %s", gateway, routes[0].Gateway)
|
2019-07-08 23:03:54 +03:00
|
|
|
}
|
|
|
|
|
2019-08-28 01:08:35 +03:00
|
|
|
// query routes for particular router
|
|
|
|
router := "rtr1"
|
|
|
|
|
2019-10-09 19:13:52 +03:00
|
|
|
routes, err = table.Query(QueryRouter(router))
|
2019-08-28 01:08:35 +03:00
|
|
|
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 gateway and network
|
2019-10-09 19:13:52 +03:00
|
|
|
query := []QueryOption{
|
2019-07-09 17:45:42 +03:00
|
|
|
QueryGateway(gateway),
|
2019-07-08 23:03:54 +03:00
|
|
|
QueryNetwork(network),
|
2019-08-28 01:08:35 +03:00
|
|
|
QueryRouter(router),
|
2019-10-09 19:13:52 +03:00
|
|
|
}
|
2019-07-08 23:03:54 +03:00
|
|
|
|
2019-10-09 19:13:52 +03:00
|
|
|
routes, err = table.Query(query...)
|
2019-07-08 23:03:54 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error looking up routes: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(routes) != 1 {
|
2019-07-09 17:45:42 +03:00
|
|
|
t.Errorf("incorrect number of routes returned. Expected: %d, found: %d", 1, len(routes))
|
2019-07-08 23:03:54 +03:00
|
|
|
}
|
|
|
|
|
2019-07-09 17:45:42 +03:00
|
|
|
if routes[0].Gateway != gateway {
|
|
|
|
t.Errorf("incorrect route returned. Expected gateway: %s, found: %s", gateway, routes[0].Gateway)
|
2019-07-08 23:03:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if routes[0].Network != network {
|
|
|
|
t.Errorf("incorrect network returned. Expected network: %s, found: %s", network, routes[0].Network)
|
|
|
|
}
|
|
|
|
|
2019-08-28 01:08:35 +03:00
|
|
|
if routes[0].Router != router {
|
|
|
|
t.Errorf("incorrect route returned. Expected router: %s, found: %s", router, routes[0].Router)
|
|
|
|
}
|
|
|
|
|
|
|
|
// non-existen route query
|
2019-10-09 19:13:52 +03:00
|
|
|
routes, err = table.Query(QueryService("foobar"))
|
2019-07-26 01:19:05 +03:00
|
|
|
if err != ErrRouteNotFound {
|
|
|
|
t.Errorf("error looking up routes. Expected: %s, found: %s", ErrRouteNotFound, err)
|
2019-07-08 23:03:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(routes) != 0 {
|
2019-07-09 17:45:42 +03:00
|
|
|
t.Errorf("incorrect number of routes returned. Expected: %d, found: %d", 0, len(routes))
|
2019-07-02 01:15:45 +03:00
|
|
|
}
|
2020-01-16 15:48:36 +03:00
|
|
|
|
|
|
|
// query NO routes
|
|
|
|
query = []QueryOption{
|
|
|
|
QueryGateway(gateway),
|
|
|
|
QueryNetwork(network),
|
|
|
|
QueryStrategy(AdvertiseNone),
|
|
|
|
}
|
|
|
|
|
|
|
|
routes, err = table.Query(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))
|
|
|
|
}
|
|
|
|
|
|
|
|
// insert local routes to query
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
route.Link = "local"
|
|
|
|
route.Address = fmt.Sprintf("local.route.address-%d", i)
|
|
|
|
if err := table.Create(route); err != nil {
|
|
|
|
t.Errorf("error adding route: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// query local routes
|
|
|
|
query = []QueryOption{
|
|
|
|
QueryGateway("*"),
|
|
|
|
QueryNetwork("*"),
|
|
|
|
QueryStrategy(AdvertiseLocal),
|
|
|
|
}
|
|
|
|
|
|
|
|
routes, err = table.Query(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))
|
|
|
|
}
|
|
|
|
|
|
|
|
// add two different routes for svcX with different metric
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
route.Service = "svcX"
|
|
|
|
route.Address = fmt.Sprintf("svcX.route.address-%d", i)
|
|
|
|
route.Metric = int64(100 + i)
|
|
|
|
if err := table.Create(route); err != nil {
|
|
|
|
t.Errorf("error adding route: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// query best routes for svcX
|
|
|
|
query = []QueryOption{
|
|
|
|
QueryService("svcX"),
|
|
|
|
QueryStrategy(AdvertiseBest),
|
|
|
|
}
|
|
|
|
|
|
|
|
routes, err = table.Query(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))
|
|
|
|
}
|
2019-07-02 01:15:45 +03:00
|
|
|
}
|
2020-07-01 17:20:30 +03:00
|
|
|
|
|
|
|
func TestFallback(t *testing.T) {
|
|
|
|
|
|
|
|
r := &router{
|
|
|
|
subscribers: make(map[string]chan *Advert),
|
|
|
|
options: DefaultOptions(),
|
|
|
|
}
|
|
|
|
route := Route{
|
|
|
|
Service: "go.micro.service.foo",
|
|
|
|
Router: r.options.Id,
|
|
|
|
Link: DefaultLink,
|
|
|
|
Metric: DefaultLocalMetric,
|
|
|
|
}
|
|
|
|
r.table = newTable(func(s string) error {
|
|
|
|
r.table.Create(route)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
r.start()
|
|
|
|
|
|
|
|
rts, err := r.Lookup(QueryService("go.micro.service.foo"))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error looking up service %s", err)
|
|
|
|
}
|
|
|
|
if len(rts) != 1 {
|
|
|
|
t.Errorf("incorrect number of routes returned %d", len(rts))
|
|
|
|
}
|
|
|
|
|
|
|
|
// deleting from the table but the next query should invoke the fallback that we passed during new table creation
|
|
|
|
if err := r.table.Delete(route); err != nil {
|
|
|
|
t.Errorf("error deleting route %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rts, err = r.Lookup(QueryService("go.micro.service.foo"))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error looking up service %s", err)
|
|
|
|
}
|
|
|
|
if len(rts) != 1 {
|
|
|
|
t.Errorf("incorrect number of routes returned %d", len(rts))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|