From 11ab0ef7c538a7e5183bbac99d8aa9bfaf0e7d6e Mon Sep 17 00:00:00 2001 From: Sebastian Lohff Date: Wed, 27 May 2020 00:48:51 +0200 Subject: [PATCH 1/2] Add vrf_name to BGP Session API --- protocols/bgp/api/session.pb.go | 73 ++++++++++++++++++--------------- protocols/bgp/api/session.proto | 1 + 2 files changed, 42 insertions(+), 32 deletions(-) diff --git a/protocols/bgp/api/session.pb.go b/protocols/bgp/api/session.pb.go index 0b693a07..6291b09a 100644 --- a/protocols/bgp/api/session.pb.go +++ b/protocols/bgp/api/session.pb.go @@ -70,6 +70,7 @@ type Session struct { Stats *SessionStats `protobuf:"bytes,6,opt,name=stats,proto3" json:"stats,omitempty"` EstablishedSince uint64 `protobuf:"varint,7,opt,name=established_since,json=establishedSince,proto3" json:"established_since,omitempty"` Description string `protobuf:"bytes,8,opt,name=description,proto3" json:"description,omitempty"` + VrfName string `protobuf:"bytes,9,opt,name=vrf_name,json=vrfName,proto3" json:"vrf_name,omitempty"` XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` XXX_sizecache int32 `json:"-"` @@ -156,6 +157,13 @@ func (m *Session) GetDescription() string { return "" } +func (m *Session) GetVrfName() string { + if m != nil { + return m.VrfName + } + return "" +} + type SessionStats struct { MessagesIn uint64 `protobuf:"varint,1,opt,name=messages_in,json=messagesIn,proto3" json:"messages_in,omitempty"` MessagesOut uint64 `protobuf:"varint,2,opt,name=messages_out,json=messagesOut,proto3" json:"messages_out,omitempty"` @@ -246,36 +254,37 @@ func init() { } var fileDescriptor_5b53032c0bb76d75 = []byte{ - // 484 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x93, 0xc1, 0x6e, 0xd3, 0x40, - 0x10, 0x86, 0x71, 0xeb, 0x38, 0xe9, 0x38, 0x69, 0xdc, 0x15, 0x20, 0x03, 0x07, 0x4c, 0x2e, 0x44, - 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4c, 0x8f, 0x80, 0xde, 0x1b, 0x97, 0x9a, 0xcf, - 0x8f, 0xa6, 0xbb, 0xbd, 0x61, 0x8f, 0x61, 0x74, 0x5f, 0xe7, 0x0d, 0xb9, 0x80, 0x7c, 0xde, 0x15, - 0xec, 0x35, 0xcc, 0xed, 0x9f, 0x0a, 0x69, 0xd3, 0x62, 0x89, 0xf2, 0x01, 0x85, 0x0b, 0xc9, 0xe7, - 0xe7, 0x9d, 0xcc, 0x7b, 0x75, 0x00, 0xca, 0x5d, 0xa3, 0x5b, 0x83, 0xc2, 0x85, 0x76, 0x04, 0x57, - 0xbd, 0x3a, 0x00, 0xf1, 0x7b, 0x0f, 0x06, 0x43, 0xf0, 0xb6, 0x57, 0x3f, 0xbd, 0xfb, 0x9a, 0xfd, - 0xe7, 0x67, 0x52, 0x04, 0x4e, 0x7a, 0xff, 0x2b, 0x00, 0x00, 0xff, 0xff, 0x87, 0xfb, 0xe6, 0x6a, - 0x60, 0x03, 0x00, 0x00, + // 503 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x93, 0x3f, 0x8f, 0xd3, 0x4c, + 0x10, 0xc6, 0x5f, 0x5f, 0x1c, 0x3b, 0x19, 0x27, 0x17, 0xdf, 0xea, 0x05, 0x19, 0x28, 0x30, 0x69, + 0x88, 0x74, 0xc2, 0x86, 0x43, 0xa2, 0xa3, 0x38, 0x8e, 0x2b, 0xd2, 0x70, 0xc8, 0xe9, 0x68, 0x22, + 0xff, 0x99, 0x38, 0x8b, 0xec, 0x5d, 0xcb, 0xb3, 0x89, 0xf8, 0xb4, 0x34, 0x7c, 0x11, 0xb4, 0x6b, + 0x27, 0x58, 0x20, 0x21, 0xd1, 0xed, 0x3c, 0xcf, 0xef, 0x99, 0xcc, 0x68, 0x62, 0x78, 0x5f, 0x72, + 0xb5, 0x3f, 0x64, 0x51, 0x2e, 0xeb, 0x38, 0xe3, 0x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0xc0, + 0x0d, 0xad, 0x95, 0x9d, 0xf8, 0x03, 0x63, 0xa3, 0x75, 0x16, 0x82, 0x57, 0x20, 0xe5, 0x2d, 0x6f, + 0x14, 0x97, 0x22, 0x98, 0x84, 0xd6, 0x6a, 0x9a, 0x0c, 0x25, 0xbd, 0xc6, 0xb1, 0xdd, 0x6d, 0x45, + 0x5a, 0x63, 0x30, 0x35, 0xb6, 0x7b, 0x6c, 0x77, 0x9f, 0xd2, 0x1a, 0x97, 0x5f, 0x61, 0x6c, 0xe6, + 0x64, 0x33, 0x98, 0x7c, 0xe4, 0x94, 0x66, 0x15, 0x16, 0xfe, 0x7f, 0x6c, 0x02, 0xf6, 0xba, 0xa8, + 0xd0, 0xb7, 0x98, 0x07, 0xee, 0x9d, 0x14, 0x02, 0x73, 0xe5, 0x5f, 0x30, 0x00, 0xe7, 0x36, 0x57, + 0xfc, 0x88, 0xfe, 0x48, 0x07, 0x1e, 0x1a, 0x14, 0x1b, 0x14, 0xca, 0xb7, 0xd9, 0x15, 0xcc, 0x75, + 0x75, 0x27, 0xc5, 0x8e, 0xb7, 0x35, 0x16, 0xfe, 0x98, 0x2d, 0xc0, 0xbb, 0xff, 0x35, 0xab, 0xef, + 0x2c, 0x7f, 0x58, 0x30, 0x1b, 0x6e, 0xcb, 0x9e, 0x83, 0x57, 0x23, 0x51, 0x5a, 0x22, 0x6d, 0xb9, + 0x30, 0x37, 0xb6, 0x13, 0x38, 0x49, 0x6b, 0xc1, 0x5e, 0xc0, 0xec, 0x0c, 0xc8, 0x83, 0x32, 0x07, + 0xb5, 0x93, 0x73, 0xe8, 0xe1, 0xa0, 0xd8, 0xff, 0x30, 0xde, 0x55, 0x69, 0x43, 0xe6, 0x76, 0x76, + 0xd2, 0x15, 0xec, 0x25, 0x2c, 0xf4, 0xff, 0x0d, 0x69, 0xdb, 0x62, 0x8e, 0xfc, 0x88, 0x85, 0xb9, + 0x9f, 0x9d, 0x5c, 0x76, 0x72, 0xd2, 0xab, 0x03, 0x90, 0xd7, 0x8d, 0x6c, 0x15, 0x16, 0xe6, 0x9e, + 0x67, 0x70, 0xdd, 0xab, 0x03, 0x10, 0xbf, 0xf5, 0xa0, 0x33, 0x04, 0xef, 0x7b, 0xf5, 0xc3, 0x9b, + 0x2f, 0xf1, 0x3f, 0x7e, 0x41, 0x99, 0x63, 0xa4, 0xb7, 0x3f, 0x03, 0x00, 0x00, 0xff, 0xff, 0x0d, + 0xb3, 0xa5, 0xc6, 0x7b, 0x03, 0x00, 0x00, } diff --git a/protocols/bgp/api/session.proto b/protocols/bgp/api/session.proto index 9ca2f2d3..fa9be04a 100644 --- a/protocols/bgp/api/session.proto +++ b/protocols/bgp/api/session.proto @@ -23,6 +23,7 @@ message Session { SessionStats stats = 6; uint64 established_since = 7; string description = 8; + string vrf_name = 9; } message SessionStats { From 855dbd43b7306e31a196b38e481809dd32cc3c33 Mon Sep 17 00:00:00 2001 From: Sebastian Lohff Date: Sat, 23 May 2020 22:01:46 +0200 Subject: [PATCH 2/2] Implement ListSessions of BGP API flaps + routes imported in the stats are still missing. --- protocols/bgp/metrics/bgp_peer_metrics.go | 24 ++ protocols/bgp/server/bgp_api.go | 72 ++++- protocols/bgp/server/bgp_api_test.go | 318 ++++++++++++++++++++++ 3 files changed, 413 insertions(+), 1 deletion(-) diff --git a/protocols/bgp/metrics/bgp_peer_metrics.go b/protocols/bgp/metrics/bgp_peer_metrics.go index 8cad4d3a..2eebe9e2 100644 --- a/protocols/bgp/metrics/bgp_peer_metrics.go +++ b/protocols/bgp/metrics/bgp_peer_metrics.go @@ -1,9 +1,11 @@ package metrics import ( + "fmt" "time" bnet "github.com/bio-routing/bio-rd/net" + "github.com/bio-routing/bio-rd/protocols/bgp/api" ) const ( @@ -48,3 +50,25 @@ type BGPPeerMetrics struct { // AddressFamilies provides metrics on AFI/SAFI level AddressFamilies []*BGPAddressFamilyMetrics } + +// GetStateAsProto returns the state of this peer to be used by the BGP API +func (m *BGPPeerMetrics) GetStateAsProto() api.Session_State { + switch m.State { + case StateDown: + return api.Session_Active // substitution + case StateIdle: + return api.Session_Idle + case StateConnect: + return api.Session_Connect + case StateActive: + return api.Session_Active + case StateOpenSent: + return api.Session_OpenSent + case StateOpenConfirm: + return api.Session_OpenConfirmed + case StateEstablished: + return api.Session_Established + default: + panic(fmt.Sprintf("Unknown state: %v", m.State)) + } +} diff --git a/protocols/bgp/server/bgp_api.go b/protocols/bgp/server/bgp_api.go index 242c7fb0..8aee92e0 100644 --- a/protocols/bgp/server/bgp_api.go +++ b/protocols/bgp/server/bgp_api.go @@ -5,7 +5,9 @@ import ( "fmt" "github.com/bio-routing/bio-rd/protocols/bgp/api" + "github.com/bio-routing/bio-rd/protocols/bgp/metrics" "github.com/bio-routing/bio-rd/route" + "github.com/pkg/errors" bnet "github.com/bio-routing/bio-rd/net" routeapi "github.com/bio-routing/bio-rd/route/api" @@ -22,8 +24,76 @@ func NewBGPAPIServer(s BGPServer) *BGPAPIServer { } } +// ListSessions lists all sessions the BGP server currently has func (s *BGPAPIServer) ListSessions(ctx context.Context, in *api.ListSessionsRequest) (*api.ListSessionsResponse, error) { - return nil, fmt.Errorf("Not implemented yet") + bgpMetrics, err := s.srv.Metrics() + if err != nil { + return nil, errors.Wrap(err, "Could not get peer metrics") + } + + sessions := make([]*api.Session, 0) + for _, peerIP := range s.srv.GetPeers() { + peer := s.srv.GetPeerConfig(peerIP) + // find metrics for peer + var peerMetrics *metrics.BGPPeerMetrics + for _, peerMetricsEntry := range bgpMetrics.Peers { + if *peerMetricsEntry.IP == *peer.PeerAddress { + peerMetrics = peerMetricsEntry + break + } + } + if peerMetrics == nil { + return nil, fmt.Errorf("Could not find metrics for neighbor %s", peer.PeerAddress) + } + + if in.Filter != nil { + if in.Filter.NeighborIp != nil { + filterNeighbor := bnet.IPFromProtoIP(in.Filter.NeighborIp) + if *filterNeighbor != *peerIP { + continue + } + } + if in.Filter.VrfName != "" { + if in.Filter.VrfName != peerMetrics.VRF { + continue + } + } + } + + estSince := peerMetrics.Since.Unix() + if estSince < 0 { + // time not set, peer probably not up + estSince = 0 + } + var routesReceived, routesSent uint64 + for _, afiPeerMetrics := range peerMetrics.AddressFamilies { + routesReceived += afiPeerMetrics.RoutesReceived + routesSent += afiPeerMetrics.RoutesSent + } + + session := &api.Session{ + LocalAddress: peer.LocalAddress.ToProto(), + NeighborAddress: peer.PeerAddress.ToProto(), + LocalAsn: peer.LocalAS, + PeerAsn: peer.PeerAS, + Status: peerMetrics.GetStateAsProto(), + Stats: &api.SessionStats{ + MessagesIn: peerMetrics.UpdatesReceived, + MessagesOut: peerMetrics.UpdatesSent, + RoutesReceived: routesReceived, + RoutesExported: routesSent, + }, + EstablishedSince: uint64(estSince), + Description: peer.Description, + VrfName: peerMetrics.VRF, + } + sessions = append(sessions, session) + } + + resp := &api.ListSessionsResponse{ + Sessions: sessions, + } + return resp, nil } // DumpRIBIn dumps the RIB in of a peer for a given AFI/SAFI diff --git a/protocols/bgp/server/bgp_api_test.go b/protocols/bgp/server/bgp_api_test.go index 5ae69c0e..68740581 100644 --- a/protocols/bgp/server/bgp_api_test.go +++ b/protocols/bgp/server/bgp_api_test.go @@ -16,6 +16,7 @@ import ( "github.com/bio-routing/bio-rd/routingtable/adjRIBIn" "github.com/bio-routing/bio-rd/routingtable/adjRIBOut" "github.com/bio-routing/bio-rd/routingtable/filter" + "github.com/bio-routing/bio-rd/routingtable/vrf" "github.com/stretchr/testify/assert" "google.golang.org/grpc" "google.golang.org/grpc/test/bufconn" @@ -358,3 +359,320 @@ func TestDumpRIBInOut(t *testing.T) { assert.Equal(t, expected, results, test.name) } } + +func TestListSessions(t *testing.T) { + vrf1, _ := vrf.New("majestic-cat", 0) + vrf2, _ := vrf.New("glorious-seagull", 1) + + tests := []struct { + name string + apisrv *BGPAPIServer + req *api.ListSessionsRequest + expected *api.ListSessionsResponse + wantFail bool + }{ + { + name: "Simple ListSessions, without filter", + apisrv: &BGPAPIServer{ + srv: &bgpServer{ + peers: &peerManager{ + peers: map[bnet.IP]*peer{ + bnet.IPv4FromOctets(10, 0, 0, 0): { + config: &PeerConfig{ + PeerAS: 65100, + LocalAS: 65000, + LocalAddress: bnet.IPv4FromOctets(172, 0, 0, 0).Ptr(), + PeerAddress: bnet.IPv4FromOctets(10, 0, 0, 0).Ptr(), + }, + peerASN: 65100, + localASN: 65000, + addr: bnet.IPv4FromOctets(10, 0, 0, 0).Ptr(), + vrf: vrf1, + }, + }, + }, + }, + }, + req: &api.ListSessionsRequest{}, + expected: &api.ListSessionsResponse{ + Sessions: []*api.Session{ + { + LocalAddress: bnet.IPv4FromOctets(172, 0, 0, 0).ToProto(), + NeighborAddress: bnet.IPv4FromOctets(10, 0, 0, 0).ToProto(), + PeerAsn: 65100, + LocalAsn: 65000, + Status: api.Session_Active, + Stats: &api.SessionStats{}, + VrfName: "majestic-cat", + }, + }, + }, + wantFail: false, + }, + { + name: "ListSessions with two peers without filter", + apisrv: &BGPAPIServer{ + srv: &bgpServer{ + peers: &peerManager{ + peers: map[bnet.IP]*peer{ + bnet.IPv4FromOctets(10, 0, 0, 0): { + config: &PeerConfig{ + PeerAS: 65100, + LocalAS: 65000, + LocalAddress: bnet.IPv4FromOctets(172, 0, 0, 0).Ptr(), + PeerAddress: bnet.IPv4FromOctets(10, 0, 0, 0).Ptr(), + }, + peerASN: 65100, + localASN: 65000, + addr: bnet.IPv4FromOctets(10, 0, 0, 0).Ptr(), + vrf: vrf1, + }, + bnet.IPv4FromOctets(192, 168, 0, 0): { + config: &PeerConfig{ + PeerAS: 64999, + LocalAS: 65000, + LocalAddress: bnet.IPv4FromOctets(172, 0, 0, 0).Ptr(), + PeerAddress: bnet.IPv4FromOctets(192, 168, 0, 0).Ptr(), + }, + peerASN: 64999, + localASN: 65000, + addr: bnet.IPv4FromOctets(192, 168, 0, 0).Ptr(), + vrf: vrf1, + }, + }, + }, + }, + }, + req: &api.ListSessionsRequest{}, + expected: &api.ListSessionsResponse{ + Sessions: []*api.Session{ + { + LocalAddress: bnet.IPv4FromOctets(172, 0, 0, 0).ToProto(), + NeighborAddress: bnet.IPv4FromOctets(10, 0, 0, 0).ToProto(), + PeerAsn: 65100, + LocalAsn: 65000, + Status: api.Session_Active, + Stats: &api.SessionStats{}, + VrfName: "majestic-cat", + }, + { + LocalAddress: bnet.IPv4FromOctets(172, 0, 0, 0).ToProto(), + NeighborAddress: bnet.IPv4FromOctets(192, 168, 0, 0).ToProto(), + PeerAsn: 64999, + LocalAsn: 65000, + Status: api.Session_Active, + Stats: &api.SessionStats{}, + VrfName: "majestic-cat", + }, + }, + }, + wantFail: false, + }, + { + name: "ListSession with two peers and filter for vrf", + apisrv: &BGPAPIServer{ + srv: &bgpServer{ + peers: &peerManager{ + peers: map[bnet.IP]*peer{ + bnet.IPv4FromOctets(10, 0, 0, 0): { + config: &PeerConfig{ + PeerAS: 65100, + LocalAS: 65000, + LocalAddress: bnet.IPv4FromOctets(172, 0, 0, 0).Ptr(), + PeerAddress: bnet.IPv4FromOctets(10, 0, 0, 0).Ptr(), + }, + peerASN: 65100, + localASN: 65000, + addr: bnet.IPv4FromOctets(10, 0, 0, 0).Ptr(), + vrf: vrf2, + }, + bnet.IPv4FromOctets(192, 168, 0, 0): { + config: &PeerConfig{ + PeerAS: 64999, + LocalAS: 65000, + LocalAddress: bnet.IPv4FromOctets(172, 0, 0, 0).Ptr(), + PeerAddress: bnet.IPv4FromOctets(192, 168, 0, 0).Ptr(), + }, + peerASN: 64999, + localASN: 65000, + addr: bnet.IPv4FromOctets(192, 168, 0, 0).Ptr(), + vrf: vrf1, + }, + }, + }, + }, + }, + req: &api.ListSessionsRequest{ + Filter: &api.SessionFilter{ + VrfName: "glorious-seagull", + }, + }, + expected: &api.ListSessionsResponse{ + Sessions: []*api.Session{ + { + LocalAddress: bnet.IPv4FromOctets(172, 0, 0, 0).ToProto(), + NeighborAddress: bnet.IPv4FromOctets(10, 0, 0, 0).ToProto(), + PeerAsn: 65100, + LocalAsn: 65000, + Status: api.Session_Active, + Stats: &api.SessionStats{}, + VrfName: "glorious-seagull", + }, + }, + }, + wantFail: false, + }, + { + name: "ListSession with two peers and filter for neighbor", + apisrv: &BGPAPIServer{ + srv: &bgpServer{ + peers: &peerManager{ + peers: map[bnet.IP]*peer{ + bnet.IPv4FromOctets(10, 0, 0, 0): { + config: &PeerConfig{ + PeerAS: 65100, + LocalAS: 65000, + LocalAddress: bnet.IPv4FromOctets(172, 0, 0, 0).Ptr(), + PeerAddress: bnet.IPv4FromOctets(10, 0, 0, 0).Ptr(), + }, + peerASN: 65100, + localASN: 65000, + addr: bnet.IPv4FromOctets(10, 0, 0, 0).Ptr(), + vrf: vrf1, + }, + bnet.IPv4FromOctets(192, 168, 0, 0): { + config: &PeerConfig{ + PeerAS: 64999, + LocalAS: 65000, + LocalAddress: bnet.IPv4FromOctets(172, 0, 0, 0).Ptr(), + PeerAddress: bnet.IPv4FromOctets(192, 168, 0, 0).Ptr(), + }, + peerASN: 64999, + localASN: 65000, + addr: bnet.IPv4FromOctets(192, 168, 0, 0).Ptr(), + vrf: vrf1, + }, + }, + }, + }, + }, + req: &api.ListSessionsRequest{ + Filter: &api.SessionFilter{ + NeighborIp: bnet.IPv4FromOctets(10, 0, 0, 0).ToProto(), + }, + }, + expected: &api.ListSessionsResponse{ + Sessions: []*api.Session{ + { + LocalAddress: bnet.IPv4FromOctets(172, 0, 0, 0).ToProto(), + NeighborAddress: bnet.IPv4FromOctets(10, 0, 0, 0).ToProto(), + PeerAsn: 65100, + LocalAsn: 65000, + Status: api.Session_Active, + Stats: &api.SessionStats{}, + VrfName: "majestic-cat", + }, + }, + }, + wantFail: false, + }, + { + name: "ListSession with routes for stats", + apisrv: &BGPAPIServer{ + srv: &bgpServer{ + peers: &peerManager{ + peers: map[bnet.IP]*peer{ + bnet.IPv4FromOctets(10, 0, 0, 0): { + ipv4: &peerAddressFamily{}, + ipv6: &peerAddressFamily{}, + fsms: []*FSM{ + 0: { + ribsInitialized: true, + ipv4Unicast: &fsmAddressFamily{ + adjRIBIn: &routingtable.RTMockClient{FakeRouteCount: 3}, + adjRIBOut: &routingtable.RTMockClient{FakeRouteCount: 2}, + }, + ipv6Unicast: &fsmAddressFamily{ + adjRIBIn: &routingtable.RTMockClient{FakeRouteCount: 10}, + adjRIBOut: &routingtable.RTMockClient{FakeRouteCount: 12}, + }, + counters: fsmCounters{ + updatesReceived: 23, + updatesSent: 42, + }, + }, + }, + config: &PeerConfig{ + PeerAS: 65100, + LocalAS: 65000, + LocalAddress: bnet.IPv4FromOctets(172, 0, 0, 0).Ptr(), + PeerAddress: bnet.IPv4FromOctets(10, 0, 0, 0).Ptr(), + }, + peerASN: 65100, + localASN: 65000, + addr: bnet.IPv4FromOctets(10, 0, 0, 0).Ptr(), + vrf: vrf1, + }, + }, + }, + }, + }, + req: &api.ListSessionsRequest{}, + expected: &api.ListSessionsResponse{ + Sessions: []*api.Session{ + { + LocalAddress: bnet.IPv4FromOctets(172, 0, 0, 0).ToProto(), + NeighborAddress: bnet.IPv4FromOctets(10, 0, 0, 0).ToProto(), + PeerAsn: 65100, + LocalAsn: 65000, + Status: api.Session_Active, + Stats: &api.SessionStats{ + RoutesReceived: 13, + RoutesExported: 14, + MessagesIn: 23, + MessagesOut: 42, + }, + VrfName: "majestic-cat", + }, + }, + }, + wantFail: false, + }, + } + + for _, test := range tests { + testSrv := test.apisrv.srv.(*bgpServer) + testSrv.metrics = &metricsService{testSrv} + bufSize := 1024 * 1024 + lis := bufconn.Listen(bufSize) + s := grpc.NewServer() + api.RegisterBgpServiceServer(s, test.apisrv) + go func() { + if err := s.Serve(lis); err != nil { + log.Fatalf("Server exited with error: %v", err) + } + }() + + ctx := context.Background() + conn, err := grpc.DialContext(ctx, "bufnet", grpc.WithDialer(func(string, time.Duration) (net.Conn, error) { + return lis.Dial() + }), grpc.WithInsecure()) + if err != nil { + t.Fatalf("Failed to dial bufnet: %v", err) + } + defer conn.Close() + + client := api.NewBgpServiceClient(conn) + neighborResp, err := client.ListSessions(ctx, test.req) + if err != nil { + t.Fatalf("ListSessions call failed: %v", err) + } + assert.Equal(t, test.expected, neighborResp) + } + + // As tests seem to share state we need to clean up the vrf here + vrf1.Unregister() + vrf1.Dispose() + vrf2.Unregister() + vrf2.Dispose() +}