github.com/osrg/gobgp/v3@v3.30.0/internal/pkg/metrics/metrics_test.go (about) 1 package metrics 2 3 import ( 4 "context" 5 "testing" 6 "time" 7 8 "github.com/prometheus/client_golang/prometheus" 9 "github.com/stretchr/testify/assert" 10 apb "google.golang.org/protobuf/types/known/anypb" 11 12 api "github.com/osrg/gobgp/v3/api" 13 "github.com/osrg/gobgp/v3/pkg/server" 14 ) 15 16 func TestMetrics(test *testing.T) { 17 assert := assert.New(test) 18 s := server.NewBgpServer() 19 20 registry := prometheus.NewRegistry() 21 registry.MustRegister(NewBgpCollector(s)) 22 23 go s.Serve() 24 err := s.StartBgp(context.Background(), &api.StartBgpRequest{ 25 Global: &api.Global{ 26 Asn: 1, 27 RouterId: "1.1.1.1", 28 ListenPort: 10179, 29 }, 30 }) 31 assert.Nil(err) 32 defer s.StopBgp(context.Background(), &api.StopBgpRequest{}) 33 34 p1 := &api.Peer{ 35 Conf: &api.PeerConf{ 36 NeighborAddress: "127.0.0.1", 37 PeerAsn: 2, 38 }, 39 Transport: &api.Transport{ 40 PassiveMode: true, 41 }, 42 } 43 err = s.AddPeer(context.Background(), &api.AddPeerRequest{Peer: p1}) 44 assert.Nil(err) 45 46 t := server.NewBgpServer() 47 go t.Serve() 48 err = t.StartBgp(context.Background(), &api.StartBgpRequest{ 49 Global: &api.Global{ 50 Asn: 2, 51 RouterId: "2.2.2.2", 52 ListenPort: -1, 53 }, 54 }) 55 assert.Nil(err) 56 defer t.StopBgp(context.Background(), &api.StopBgpRequest{}) 57 58 p2 := &api.Peer{ 59 Conf: &api.PeerConf{ 60 NeighborAddress: "127.0.0.1", 61 PeerAsn: 1, 62 }, 63 Transport: &api.Transport{ 64 RemotePort: 10179, 65 }, 66 Timers: &api.Timers{ 67 Config: &api.TimersConfig{ 68 ConnectRetry: 1, 69 IdleHoldTimeAfterReset: 1, 70 }, 71 }, 72 } 73 74 ch := make(chan struct{}) 75 s.WatchEvent(context.Background(), &api.WatchEventRequest{Peer: &api.WatchEventRequest_Peer{}}, func(r *api.WatchEventResponse) { 76 if peer := r.GetPeer(); peer != nil { 77 if peer.Type == api.WatchEventResponse_PeerEvent_STATE && peer.Peer.State.SessionState == api.PeerState_ESTABLISHED { 78 close(ch) 79 } 80 } 81 }) 82 83 err = t.AddPeer(context.Background(), &api.AddPeerRequest{Peer: p2}) 84 assert.Nil(err) 85 <-ch 86 87 family := &api.Family{ 88 Afi: api.Family_AFI_IP, 89 Safi: api.Family_SAFI_UNICAST, 90 } 91 92 ctx, cancel := context.WithCancel(context.Background()) 93 ch = make(chan struct{}) 94 go func() { 95 for { 96 select { 97 case <-ctx.Done(): 98 ch <- struct{}{} 99 return 100 default: 101 nlri1, _ := apb.New(&api.IPAddressPrefix{ 102 Prefix: "10.1.0.0", 103 PrefixLen: 24, 104 }) 105 106 a1, _ := apb.New(&api.OriginAttribute{ 107 Origin: 0, 108 }) 109 a2, _ := apb.New(&api.NextHopAttribute{ 110 NextHop: "10.0.0.1", 111 }) 112 attrs := []*apb.Any{a1, a2} 113 114 t.AddPath(context.Background(), &api.AddPathRequest{ 115 TableType: api.TableType_GLOBAL, 116 Path: &api.Path{ 117 Family: family, 118 Nlri: nlri1, 119 Pattrs: attrs, 120 }, 121 }) 122 t.DeletePath(context.Background(), &api.DeletePathRequest{ 123 TableType: api.TableType_GLOBAL, 124 Path: &api.Path{ 125 Family: family, 126 Nlri: nlri1, 127 Pattrs: attrs, 128 }, 129 }) 130 } 131 } 132 }() 133 134 for i := 0; i < 100; i++ { 135 metrics, err := registry.Gather() 136 assert.Nil(err) 137 assert.NotEmpty(metrics) 138 time.Sleep(10 * time.Millisecond) 139 } 140 141 cancel() 142 <-ch 143 }