2020-07-27 15:22:00 +03:00
|
|
|
package registry
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/micro/go-micro/v3/router"
|
|
|
|
)
|
|
|
|
|
|
|
|
func testSetup() (*table, router.Route) {
|
2020-08-21 11:23:01 +03:00
|
|
|
table := newTable()
|
2020-07-27 15:22:00 +03:00
|
|
|
|
|
|
|
route := router.Route{
|
|
|
|
Service: "dest.svc",
|
|
|
|
Address: "dest.addr",
|
|
|
|
Gateway: "dest.gw",
|
|
|
|
Network: "dest.network",
|
|
|
|
Router: "src.router",
|
|
|
|
Link: "det.link",
|
|
|
|
Metric: 10,
|
|
|
|
}
|
|
|
|
|
|
|
|
return table, route
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreate(t *testing.T) {
|
|
|
|
table, route := testSetup()
|
|
|
|
|
|
|
|
if err := table.Create(route); err != nil {
|
2020-08-15 01:51:52 +03:00
|
|
|
t.Fatalf("error adding route: %s", err)
|
2020-07-27 15:22:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// adds new route for the original destination
|
|
|
|
route.Gateway = "dest.gw2"
|
|
|
|
|
|
|
|
if err := table.Create(route); err != nil {
|
2020-08-15 01:51:52 +03:00
|
|
|
t.Fatalf("error adding route: %s", err)
|
2020-07-27 15:22:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// adding the same route under Insert policy must error
|
|
|
|
if err := table.Create(route); err != router.ErrDuplicateRoute {
|
2020-08-15 01:51:52 +03:00
|
|
|
t.Fatalf("error adding route. Expected error: %s, found: %s", router.ErrDuplicateRoute, err)
|
2020-07-27 15:22:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDelete(t *testing.T) {
|
|
|
|
table, route := testSetup()
|
|
|
|
|
|
|
|
if err := table.Create(route); err != nil {
|
2020-08-15 01:51:52 +03:00
|
|
|
t.Fatalf("error adding route: %s", err)
|
2020-07-27 15:22:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// should fail to delete non-existant route
|
|
|
|
prevSvc := route.Service
|
|
|
|
route.Service = "randDest"
|
|
|
|
|
|
|
|
if err := table.Delete(route); err != router.ErrRouteNotFound {
|
2020-08-15 01:51:52 +03:00
|
|
|
t.Fatalf("error deleting route. Expected: %s, found: %s", router.ErrRouteNotFound, err)
|
2020-07-27 15:22:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// we should be able to delete the existing route
|
|
|
|
route.Service = prevSvc
|
|
|
|
|
|
|
|
if err := table.Delete(route); err != nil {
|
2020-08-15 01:51:52 +03:00
|
|
|
t.Fatalf("error deleting route: %s", err)
|
2020-07-27 15:22:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdate(t *testing.T) {
|
|
|
|
table, route := testSetup()
|
|
|
|
|
|
|
|
if err := table.Create(route); err != nil {
|
2020-08-15 01:51:52 +03:00
|
|
|
t.Fatalf("error adding route: %s", err)
|
2020-07-27 15:22:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// change the metric of the original route
|
|
|
|
route.Metric = 200
|
|
|
|
|
|
|
|
if err := table.Update(route); err != nil {
|
2020-08-15 01:51:52 +03:00
|
|
|
t.Fatalf("error updating route: %s", err)
|
2020-07-27 15:22:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// this should add a new route
|
|
|
|
route.Service = "rand.dest"
|
|
|
|
|
|
|
|
if err := table.Update(route); err != nil {
|
2020-08-15 01:51:52 +03:00
|
|
|
t.Fatalf("error updating route: %s", err)
|
2020-07-27 15:22:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestList(t *testing.T) {
|
|
|
|
table, route := testSetup()
|
|
|
|
|
|
|
|
svc := []string{"one.svc", "two.svc", "three.svc"}
|
|
|
|
|
|
|
|
for i := 0; i < len(svc); i++ {
|
|
|
|
route.Service = svc[i]
|
|
|
|
if err := table.Create(route); err != nil {
|
2020-08-15 01:51:52 +03:00
|
|
|
t.Fatalf("error adding route: %s", err)
|
2020-07-27 15:22:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-23 15:10:48 +03:00
|
|
|
routes, err := table.Read()
|
2020-07-27 15:22:00 +03:00
|
|
|
if err != nil {
|
2020-08-15 01:51:52 +03:00
|
|
|
t.Fatalf("error listing routes: %s", err)
|
2020-07-27 15:22:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(routes) != len(svc) {
|
2020-08-15 01:51:52 +03:00
|
|
|
t.Fatalf("incorrect number of routes listed. Expected: %d, found: %d", len(svc), len(routes))
|
2020-07-27 15:22:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQuery(t *testing.T) {
|
|
|
|
table, route := testSetup()
|
|
|
|
|
2020-08-21 11:23:01 +03:00
|
|
|
if err := table.Create(route); err != nil {
|
|
|
|
t.Fatalf("error adding route: %s", err)
|
2020-07-27 15:22:00 +03:00
|
|
|
}
|
|
|
|
|
2020-08-23 15:10:48 +03:00
|
|
|
rt, err := table.Read(router.ReadService(route.Service))
|
2020-07-27 15:22:00 +03:00
|
|
|
if err != nil {
|
2020-08-21 11:23:01 +03:00
|
|
|
t.Fatal("Expected a route got err", err)
|
2020-07-27 15:22:00 +03:00
|
|
|
}
|
|
|
|
|
2020-08-21 11:23:01 +03:00
|
|
|
if len(rt) != 1 {
|
|
|
|
t.Fatalf("Expected one route got %d", len(rt))
|
2020-07-27 15:22:00 +03:00
|
|
|
}
|
|
|
|
|
2020-08-21 11:23:01 +03:00
|
|
|
if rt[0].Hash() != route.Hash() {
|
|
|
|
t.Fatal("Mismatched routes received")
|
2020-07-27 15:22:00 +03:00
|
|
|
}
|
|
|
|
}
|