Add unit tests

This commit is contained in:
Mayuresh Gaitonde
2020-12-17 17:25:53 -08:00
parent 3702339f44
commit 6be4d69d02
705 changed files with 120529 additions and 150051 deletions

View File

@@ -17,9 +17,11 @@ package apiutil
import (
"encoding/json"
"fmt"
"net"
"time"
"github.com/golang/protobuf/ptypes"
api "github.com/osrg/gobgp/api"
"github.com/osrg/gobgp/pkg/packet/bgp"
)
@@ -49,9 +51,10 @@ func NewDestination(dst *api.Destination) *Destination {
for _, p := range dst.Paths {
nlri, _ := GetNativeNlri(p)
attrs, _ := GetNativePathAttributes(p)
t, _ := ptypes.Timestamp(p.Age)
l = append(l, &Path{
Nlri: nlri,
Age: p.Age,
Age: t.Unix(),
Best: p.Best,
Attrs: attrs,
Stale: p.Stale,
@@ -64,10 +67,11 @@ func NewDestination(dst *api.Destination) *Destination {
}
func NewPath(nlri bgp.AddrPrefixInterface, isWithdraw bool, attrs []bgp.PathAttributeInterface, age time.Time) *api.Path {
t, _ := ptypes.TimestampProto(age)
return &api.Path{
AnyNlri: MarshalNLRI(nlri),
AnyPattrs: MarshalPathAttributes(attrs),
Age: age.Unix(),
Nlri: MarshalNLRI(nlri),
Pattrs: MarshalPathAttributes(attrs),
Age: t,
IsWithdraw: isWithdraw,
Family: ToApiFamily(nlri.AFI(), nlri.SAFI()),
Identifier: nlri.PathIdentifier(),
@@ -87,17 +91,20 @@ func getNLRI(family bgp.RouteFamily, buf []byte) (bgp.AddrPrefixInterface, error
}
func GetNativeNlri(p *api.Path) (bgp.AddrPrefixInterface, error) {
if len(p.Nlri) > 0 {
return getNLRI(ToRouteFamily(p.Family), p.Nlri)
if p.Family == nil {
return nil, fmt.Errorf("family cannot be nil")
}
return UnmarshalNLRI(ToRouteFamily(p.Family), p.AnyNlri)
if len(p.NlriBinary) > 0 {
return getNLRI(ToRouteFamily(p.Family), p.NlriBinary)
}
return UnmarshalNLRI(ToRouteFamily(p.Family), p.Nlri)
}
func GetNativePathAttributes(p *api.Path) ([]bgp.PathAttributeInterface, error) {
pattrsLen := len(p.Pattrs)
pattrsLen := len(p.PattrsBinary)
if pattrsLen > 0 {
pattrs := make([]bgp.PathAttributeInterface, 0, pattrsLen)
for _, attr := range p.Pattrs {
for _, attr := range p.PattrsBinary {
a, err := bgp.GetPathAttribute(attr)
if err != nil {
return nil, err
@@ -110,7 +117,7 @@ func GetNativePathAttributes(p *api.Path) ([]bgp.PathAttributeInterface, error)
}
return pattrs, nil
}
return UnmarshalPathAttributes(p.AnyPattrs)
return UnmarshalPathAttributes(p.Pattrs)
}
func ToRouteFamily(f *api.Family) bgp.RouteFamily {