fixed tests, added db init flag
This commit is contained in:
parent
8d53d7e301
commit
1c73eb4802
@ -9,6 +9,7 @@ import (
|
|||||||
"github.com/alice-lg/alice-lg/pkg/http"
|
"github.com/alice-lg/alice-lg/pkg/http"
|
||||||
"github.com/alice-lg/alice-lg/pkg/store"
|
"github.com/alice-lg/alice-lg/pkg/store"
|
||||||
"github.com/alice-lg/alice-lg/pkg/store/backends/memory"
|
"github.com/alice-lg/alice-lg/pkg/store/backends/memory"
|
||||||
|
"github.com/alice-lg/alice-lg/pkg/store/backends/postgres"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -19,7 +20,10 @@ func main() {
|
|||||||
"config", "/etc/alice-lg/alice.conf",
|
"config", "/etc/alice-lg/alice.conf",
|
||||||
"Alice looking glass configuration file",
|
"Alice looking glass configuration file",
|
||||||
)
|
)
|
||||||
|
dbInitFlag := flag.Bool(
|
||||||
|
"db-init", false,
|
||||||
|
"Initialize the database. Clears all data.",
|
||||||
|
)
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
// Load configuration
|
// Load configuration
|
||||||
@ -28,9 +32,32 @@ func main() {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setup local routes store
|
// Setup local routes store and use backend from configuration
|
||||||
neighborsBackend := memory.NewNeighborsBackend()
|
var (
|
||||||
routesBackend := memory.NewRoutesBackend()
|
neighborsBackend store.NeighborsStoreBackend = memory.NewNeighborsBackend()
|
||||||
|
routesBackend store.RoutesStoreBackend = memory.NewRoutesBackend()
|
||||||
|
)
|
||||||
|
if cfg.Server.StoreBackend == "postgres" {
|
||||||
|
pool, err := postgres.Connect(ctx, cfg.Postgres)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
m := postgres.NewManager(pool)
|
||||||
|
|
||||||
|
// Initialize db if required
|
||||||
|
if *dbInitFlag {
|
||||||
|
if err := m.Initialize(ctx); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
log.Println("database initialized")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
go m.Start(ctx)
|
||||||
|
|
||||||
|
neighborsBackend = postgres.NewNeighborsBackend(pool)
|
||||||
|
routesBackend = postgres.NewRoutesBackend(pool)
|
||||||
|
}
|
||||||
|
|
||||||
neighborsStore := store.NewNeighborsStore(cfg, neighborsBackend)
|
neighborsStore := store.NewNeighborsStore(cfg, neighborsBackend)
|
||||||
routesStore := store.NewRoutesStore(neighborsStore, cfg, routesBackend)
|
routesStore := store.NewRoutesStore(neighborsStore, cfg, routesBackend)
|
||||||
|
2
go.sum
2
go.sum
@ -171,6 +171,7 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
|
|||||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||||
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
|
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
|
||||||
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
||||||
|
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
github.com/vishvananda/netlink v0.0.0-20170802012344-a95659537721/go.mod h1:+SR5DhBJrl6ZM7CoCKvpw5BKroDKQ+PJqOg65H/2ktk=
|
github.com/vishvananda/netlink v0.0.0-20170802012344-a95659537721/go.mod h1:+SR5DhBJrl6ZM7CoCKvpw5BKroDKQ+PJqOg65H/2ktk=
|
||||||
github.com/vishvananda/netns v0.0.0-20170707011535-86bef332bfc3/go.mod h1:ZjcWmFBXmLKZu9Nxj3WKYEafiSqer2rnvPr0en9UNpI=
|
github.com/vishvananda/netns v0.0.0-20170707011535-86bef332bfc3/go.mod h1:ZjcWmFBXmLKZu9Nxj3WKYEafiSqer2rnvPr0en9UNpI=
|
||||||
@ -303,6 +304,7 @@ gopkg.in/ini.v1 v1.42.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
|
|||||||
gopkg.in/yaml.v2 v2.0.0-20170721122051-25c4ec802a7d/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
|
gopkg.in/yaml.v2 v2.0.0-20170721122051-25c4ec802a7d/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
|
||||||
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
|
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
|
||||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||||
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||||
|
@ -71,13 +71,12 @@ func (b *NeighborsBackend) GetNeighborsAt(
|
|||||||
return ret, nil
|
return ret, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetNeighborAt retrieves all neighbors for a source
|
// GetNeighborsMapAt retrieves all neighbors for a source
|
||||||
// identified by its ID.
|
// identified by its ID and returns a map.
|
||||||
func (b *NeighborsBackend) GetNeighborAt(
|
func (b *NeighborsBackend) GetNeighborsMapAt(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
sourceID string,
|
sourceID string,
|
||||||
neighborID string,
|
) (map[string]*api.Neighbor, error) {
|
||||||
) (*api.Neighbor, error) {
|
|
||||||
b.Lock()
|
b.Lock()
|
||||||
defer b.Unlock()
|
defer b.Unlock()
|
||||||
|
|
||||||
@ -87,7 +86,7 @@ func (b *NeighborsBackend) GetNeighborAt(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Copy neighbors map
|
// Copy neighbors map
|
||||||
result := make(map[string]*Neighbor)
|
result := make(map[string]*api.Neighbor)
|
||||||
for k, v := range neighbors {
|
for k, v := range neighbors {
|
||||||
result[k] = v
|
result[k] = v
|
||||||
}
|
}
|
||||||
|
@ -40,7 +40,6 @@ type NeighborsStoreBackend interface {
|
|||||||
GetNeighborsMapAt(
|
GetNeighborsMapAt(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
sourceID string,
|
sourceID string,
|
||||||
neighborID string,
|
|
||||||
) (map[string]*api.Neighbor, error)
|
) (map[string]*api.Neighbor, error)
|
||||||
|
|
||||||
// CountNeighborsAt retrieves the current number of
|
// CountNeighborsAt retrieves the current number of
|
||||||
|
@ -154,9 +154,14 @@ func (s *RoutesStore) updateSource(
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
neighbors, err := s.neighbors.GetNeighborsMapAt(ctx, src.ID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// Prepare imported routes for lookup
|
// Prepare imported routes for lookup
|
||||||
imported := s.routesToLookupRoutes(ctx, "imported", src, res.Imported)
|
imported := s.routesToLookupRoutes(ctx, "imported", src, neighbors, res.Imported)
|
||||||
filtered := s.routesToLookupRoutes(ctx, "filtered", src, res.Filtered)
|
filtered := s.routesToLookupRoutes(ctx, "filtered", src, neighbors, res.Filtered)
|
||||||
lookupRoutes := append(imported, filtered...)
|
lookupRoutes := append(imported, filtered...)
|
||||||
|
|
||||||
if err = s.backend.SetRoutes(ctx, src.ID, lookupRoutes); err != nil {
|
if err = s.backend.SetRoutes(ctx, src.ID, lookupRoutes); err != nil {
|
||||||
@ -188,11 +193,10 @@ func (s *RoutesStore) routesToLookupRoutes(
|
|||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
state string,
|
state string,
|
||||||
src *config.SourceConfig,
|
src *config.SourceConfig,
|
||||||
|
neighbors map[string]*api.Neighbor,
|
||||||
routes api.Routes,
|
routes api.Routes,
|
||||||
) api.LookupRoutes {
|
) api.LookupRoutes {
|
||||||
lookupRoutes := make(api.LookupRoutes, 0, len(routes))
|
lookupRoutes := make(api.LookupRoutes, 0, len(routes))
|
||||||
neighbors := s.neighbors.GetNeighborsMapAt(ctx, src.ID)
|
|
||||||
|
|
||||||
for _, route := range routes {
|
for _, route := range routes {
|
||||||
neighbor, ok := neighbors[route.NeighborID]
|
neighbor, ok := neighbors[route.NeighborID]
|
||||||
if !ok {
|
if !ok {
|
||||||
|
@ -41,8 +41,16 @@ func importRoutes(
|
|||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
// Prepare imported routes for lookup
|
// Prepare imported routes for lookup
|
||||||
imported := s.routesToLookupRoutes(ctx, "imported", src, res.Imported)
|
neighbors := map[string]*api.Neighbor{
|
||||||
filtered := s.routesToLookupRoutes(ctx, "filtered", src, res.Filtered)
|
"ID163_AS31078": &api.Neighbor{
|
||||||
|
ID: "ID163_AS31078",
|
||||||
|
},
|
||||||
|
"ID7254_AS31334": &api.Neighbor{
|
||||||
|
ID: "ID7254_AS31334",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
imported := s.routesToLookupRoutes(ctx, "imported", src, neighbors, res.Imported)
|
||||||
|
filtered := s.routesToLookupRoutes(ctx, "filtered", src, neighbors, res.Filtered)
|
||||||
lookupRoutes := append(imported, filtered...)
|
lookupRoutes := append(imported, filtered...)
|
||||||
|
|
||||||
if err := s.backend.SetRoutes(ctx, src.ID, lookupRoutes); err != nil {
|
if err := s.backend.SetRoutes(ctx, src.ID, lookupRoutes); err != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user