micro/router/registry/table_test.go

132 lines
2.8 KiB
Go
Raw Normal View History

package registry
2019-07-02 00:57:27 +03:00
import (
"testing"
"github.com/micro/go-micro/v3/router"
)
2019-07-02 00:57:27 +03:00
func testSetup() (*table, router.Route) {
table := newTable()
2019-07-02 00:57:27 +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,
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
}
func TestCreate(t *testing.T) {
2019-07-02 01:15:45 +03:00
table, route := testSetup()
2019-07-02 00:57:27 +03:00
if err := table.Create(route); err != nil {
2020-08-15 01:51:52 +03:00
t.Fatalf("error adding route: %s", err)
2019-07-02 00:57:27 +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)
2019-07-02 00:57:27 +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)
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
if err := table.Create(route); err != nil {
2020-08-15 01:51:52 +03:00
t.Fatalf("error adding route: %s", err)
2019-07-02 00:57:27 +03:00
}
// should fail to delete non-existant route
prevSvc := route.Service
route.Service = "randDest"
2019-07-02 00:57:27 +03:00
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)
2019-07-02 00:57:27 +03:00
}
// we should be able to delete the existing route
route.Service = prevSvc
2019-07-02 00:57:27 +03:00
if err := table.Delete(route); err != nil {
2020-08-15 01:51:52 +03:00
t.Fatalf("error deleting route: %s", err)
2019-07-02 00:57:27 +03:00
}
}
func TestUpdate(t *testing.T) {
2019-07-02 01:15:45 +03:00
table, route := testSetup()
2019-07-02 00:57:27 +03:00
if err := table.Create(route); err != nil {
2020-08-15 01:51:52 +03:00
t.Fatalf("error adding route: %s", err)
2019-07-02 00:57:27 +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 {
2020-08-15 01:51:52 +03:00
t.Fatalf("error updating route: %s", err)
2019-07-02 00:57:27 +03:00
}
// this should add a new route
route.Service = "rand.dest"
2019-07-02 00:57:27 +03:00
if err := table.Update(route); err != nil {
2020-08-15 01:51:52 +03:00
t.Fatalf("error updating route: %s", err)
2019-07-02 00:57:27 +03:00
}
}
2019-07-02 00:57:27 +03:00
func TestList(t *testing.T) {
table, route := testSetup()
2019-07-02 00:57:27 +03:00
svc := []string{"one.svc", "two.svc", "three.svc"}
2019-07-02 00:57:27 +03:00
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)
}
2019-07-02 00:57:27 +03:00
}
routes, err := table.List()
if err != nil {
2020-08-15 01:51:52 +03:00
t.Fatalf("error listing routes: %s", err)
}
2019-07-02 00:57:27 +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))
2019-07-02 00:57:27 +03:00
}
}
2019-07-02 01:15:45 +03:00
func TestQuery(t *testing.T) {
2019-07-02 01:15:45 +03:00
table, route := testSetup()
if err := table.Create(route); err != nil {
t.Fatalf("error adding route: %s", err)
}
rt, err := table.Query(route.Service)
if err != nil {
t.Fatal("Expected a route got err", err)
}
if len(rt) != 1 {
t.Fatalf("Expected one route got %d", len(rt))
}
if rt[0].Hash() != route.Hash() {
t.Fatal("Mismatched routes received")
}
}