2020-07-27 15:22:00 +03:00
|
|
|
package registry
|
2019-07-02 00:57:27 +03:00
|
|
|
|
2020-01-16 15:48:36 +03:00
|
|
|
import (
|
|
|
|
"testing"
|
2020-07-27 15:22:00 +03:00
|
|
|
|
|
|
|
"github.com/micro/go-micro/v3/router"
|
2020-01-16 15:48:36 +03:00
|
|
|
)
|
2019-07-02 00:57:27 +03:00
|
|
|
|
2020-07-27 15:22:00 +03:00
|
|
|
func testSetup() (*table, router.Route) {
|
2020-08-21 11:23:01 +03:00
|
|
|
table := newTable()
|
2019-07-02 00:57:27 +03:00
|
|
|
|
2020-07-27 15:22:00 +03:00
|
|
|
route := router.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 {
|
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"
|
|
|
|
|
2019-07-10 19:46:22 +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
|
|
|
}
|
|
|
|
|
2019-07-08 18:16:50 +03:00
|
|
|
// adding the same route under Insert policy must error
|
2020-07-27 15:22:00 +03:00
|
|
|
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
|
|
|
|
2019-07-10 19:46:22 +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
|
2019-07-09 17:45:42 +03:00
|
|
|
prevSvc := route.Service
|
|
|
|
route.Service = "randDest"
|
2019-07-02 00:57:27 +03:00
|
|
|
|
2020-07-27 15:22:00 +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
|
|
|
}
|
|
|
|
|
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 {
|
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
|
|
|
|
2019-07-10 19:46:22 +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
|
|
|
}
|
|
|
|
|
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 {
|
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-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 {
|
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-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 {
|
2020-08-15 01:51:52 +03:00
|
|
|
t.Fatalf("error adding route: %s", err)
|
2019-07-08 23:03:54 +03:00
|
|
|
}
|
2019-07-02 00:57:27 +03:00
|
|
|
}
|
|
|
|
|
2020-08-23 15:10:48 +03:00
|
|
|
routes, err := table.Read()
|
2019-07-08 23:03:54 +03:00
|
|
|
if err != nil {
|
2020-08-15 01:51:52 +03:00
|
|
|
t.Fatalf("error listing routes: %s", err)
|
2019-07-08 23:03:54 +03:00
|
|
|
}
|
2019-07-02 00:57:27 +03:00
|
|
|
|
2019-07-09 17:45:42 +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
|
|
|
|
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-08-21 11:23:01 +03:00
|
|
|
if err := table.Create(route); err != nil {
|
|
|
|
t.Fatalf("error adding route: %s", err)
|
2020-07-01 17:20:30 +03:00
|
|
|
}
|
|
|
|
|
2020-08-23 15:10:48 +03:00
|
|
|
rt, err := table.Read(router.ReadService(route.Service))
|
2020-07-01 17:20:30 +03:00
|
|
|
if err != nil {
|
2020-08-21 11:23:01 +03:00
|
|
|
t.Fatal("Expected a route got err", err)
|
2020-07-01 17:20:30 +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-01 17:20:30 +03:00
|
|
|
}
|
|
|
|
|
2020-08-21 11:23:01 +03:00
|
|
|
if rt[0].Hash() != route.Hash() {
|
|
|
|
t.Fatal("Mismatched routes received")
|
2020-07-02 20:29:11 +03:00
|
|
|
}
|
|
|
|
}
|