alice-lg/pkg/store/neighbors_store_test.go

177 lines
3.8 KiB
Go
Raw Normal View History

2021-10-20 18:36:30 +00:00
package store
2017-06-29 14:24:08 +02:00
import (
2021-12-08 14:15:55 +01:00
"context"
"sort"
2017-06-29 14:24:08 +02:00
"testing"
2021-03-22 17:35:20 +01:00
"github.com/alice-lg/alice-lg/pkg/api"
2021-12-08 14:15:55 +01:00
"github.com/alice-lg/alice-lg/pkg/config"
"github.com/alice-lg/alice-lg/pkg/store/backends/memory"
2017-06-29 14:24:08 +02:00
)
2021-10-22 22:17:04 +02:00
// Make a store and populate it with data
2021-10-15 21:24:24 +02:00
func makeTestNeighborsStore() *NeighborsStore {
2021-12-08 14:15:55 +01:00
be := memory.NewNeighborsBackend()
2017-06-29 14:24:08 +02:00
2021-12-08 14:15:55 +01:00
cfg := &config.Config{
Server: config.ServerConfig{
NeighborsStoreRefreshInterval: 1,
2017-06-29 14:24:08 +02:00
},
2021-12-08 14:15:55 +01:00
Sources: []*config.SourceConfig{
{
ID: "rs1",
Name: "rs1",
},
{
ID: "rs2",
Name: "rs2",
},
2017-06-29 14:24:08 +02:00
},
}
2021-12-08 14:15:55 +01:00
rs1 := api.Neighbors{
&api.Neighbor{
ID: "ID2233_AS2342",
ASN: 2342,
Description: "PEER AS2342 192.9.23.42 Customer Peer 1",
2017-06-29 14:24:08 +02:00
},
2021-12-08 14:15:55 +01:00
&api.Neighbor{
ID: "ID2233_AS2343",
ASN: 2343,
Description: "PEER AS2343 192.9.23.43 Different Peer 1",
},
&api.Neighbor{
ID: "ID2233_AS2344",
ASN: 2344,
Description: "PEER AS2344 192.9.23.44 3rd Peer from the sun",
2017-06-29 14:24:08 +02:00
},
2021-12-08 16:16:39 +01:00
&api.Neighbor{
ID: "ID163_AS31078",
ASN: 31078,
Description: "PEER AS31078 1.2.3.4 Peer Peer",
},
&api.Neighbor{
ID: "ID7254_AS31334",
ASN: 31078,
Description: "PEER AS31334 4.3.2.1 Peer",
},
2017-06-29 14:24:08 +02:00
}
2021-12-08 14:15:55 +01:00
rs2 := api.Neighbors{
&api.Neighbor{
ID: "ID2233_AS2342",
ASN: 2342,
Description: "PEER AS2342 192.9.23.42 Customer Peer 1",
2017-06-29 14:24:08 +02:00
},
2021-12-08 14:15:55 +01:00
&api.Neighbor{
ID: "ID2233_AS4223",
ASN: 4223,
Description: "PEER AS4223 192.9.42.23 Cloudfoo Inc.",
},
2017-06-29 14:24:08 +02:00
}
2021-12-08 14:15:55 +01:00
be.SetNeighbors(context.Background(), "rs1", rs1)
be.SetNeighbors(context.Background(), "rs2", rs2)
2021-12-08 14:15:55 +01:00
// Create store
store := NewNeighborsStore(cfg, be)
2022-11-10 10:05:04 +01:00
store.sources.RefreshSuccess("rs1")
store.sources.RefreshSuccess("rs2")
2021-12-08 14:15:55 +01:00
return store
}
func TestGetNeighborsMapAt(t *testing.T) {
2021-10-15 21:24:24 +02:00
store := makeTestNeighborsStore()
2017-06-29 14:24:08 +02:00
neighbors, err := store.GetNeighborsMapAt(context.Background(), "rs1")
2021-12-08 14:15:55 +01:00
if err != nil {
t.Fatal(err)
}
neighbor := neighbors["ID2233_AS2343"]
2021-10-20 20:26:37 +00:00
if neighbor.ID != "ID2233_AS2343" {
t.Error("unexpected neighbor:", neighbor)
2017-06-29 14:24:08 +02:00
}
}
func TestGetNeighbors(t *testing.T) {
2021-10-15 21:24:24 +02:00
store := makeTestNeighborsStore()
2021-12-08 14:15:55 +01:00
neighbors, err := store.GetNeighborsAt(context.Background(), "rs2")
if err != nil {
t.Fatal(err)
}
if len(neighbors) != 2 {
t.Error("Expected 2 neighbors, got:", len(neighbors))
}
2018-10-16 18:27:54 +02:00
sort.Sort(neighbors)
2021-10-20 20:26:37 +00:00
if neighbors[0].ID != "ID2233_AS2342" {
t.Error("Expected neighbor: ID2233_AS2342, got:",
neighbors[0])
}
2017-06-29 14:24:08 +02:00
2021-12-08 14:15:55 +01:00
neighbors, err = store.GetNeighborsAt(context.Background(), "rs3")
2021-12-08 16:16:39 +01:00
if err == nil {
t.Error("Unknown source should have yielded zero results")
}
2022-07-11 10:36:17 +02:00
t.Log(neighbors)
2017-06-29 14:24:08 +02:00
}
2021-10-15 21:24:24 +02:00
func TestNeighborLookup(t *testing.T) {
store := makeTestNeighborsStore()
2021-12-08 14:15:55 +01:00
results, err := store.LookupNeighbors(context.Background(), "Cloudfoo")
if err != nil {
t.Fatal(err)
}
// Peer should be present at RS2
2021-10-15 21:24:24 +02:00
neighbors, ok := results["rs2"]
if !ok {
t.Error("Lookup on rs2 unsuccessful.")
}
2021-10-15 21:24:24 +02:00
if len(neighbors) > 1 {
t.Error("Lookup should match exact 1 peer.")
}
2021-10-15 21:24:24 +02:00
n := neighbors[0]
2021-10-20 20:26:37 +00:00
if n.ID != "ID2233_AS4223" {
t.Error("Wrong peer in lookup response")
}
2017-06-29 14:24:08 +02:00
}
2019-10-07 18:21:28 +02:00
func TestNeighborFilter(t *testing.T) {
2021-12-08 14:15:55 +01:00
ctx := context.Background()
2021-10-15 21:24:24 +02:00
store := makeTestNeighborsStore()
2019-10-07 18:21:28 +02:00
filter := api.NeighborFilterFromQueryString("asn=2342")
2021-12-08 14:15:55 +01:00
neighbors, err := store.FilterNeighbors(ctx, filter)
if err != nil {
t.Fatal(err)
}
2019-10-07 18:21:28 +02:00
if len(neighbors) != 2 {
t.Error("Expected two results")
}
filter = api.NeighborFilterFromQueryString("")
2021-12-08 14:15:55 +01:00
neighbors, err = store.FilterNeighbors(ctx, filter)
if err != nil {
t.Fatal(err)
}
2019-10-07 18:21:28 +02:00
if len(neighbors) != 0 {
t.Error("Expected empty result set")
}
}
2022-07-11 10:36:17 +02:00
func TestReMatchASLookup(t *testing.T) {
if !ReMatchASLookup.MatchString("AS2342") {
t.Error("should be ASN")
}
if ReMatchASLookup.MatchString("Goo") {
t.Error("should not be ASN")
}
}