Skip to content
Snippets Groups Projects
Commit bb07a834 authored by cedi's avatar cedi
Browse files

Add unit-tests for netlink_path

parent 673eafda
No related branches found
No related tags found
No related merge requests found
......@@ -4,12 +4,20 @@ import (
"fmt"
bnet "github.com/bio-routing/bio-rd/net"
log "github.com/sirupsen/logrus"
"github.com/vishvananda/netlink"
)
// ProtoBio is the protocol number constant for bio from /etc/iproute2/rt_protos
const ProtoBio = 45
const (
ProtoUnspec = 0 // unspec (from /etc/iproute2/rt_protos)
ProtoRedirect = 1 // redirect (from /etc/iproute2/rt_protos)
ProtoKernel = 2 // kernel (from /etc/iproute2/rt_protos)
ProtoBoot = 3 // boot (from /etc/iproute2/rt_protos)
ProtoStatic = 4 // static (from /etc/iproute2/rt_protos)
ProtoZebra = 11 // zebra (from /etc/iproute2/rt_protos)
ProtoBird = 12 // bird (from /etc/iproute2/rt_protos)
ProtoDHCP = 16 // dhcp (from /etc/iproute2/rt_protos)
ProtoBio = 45 // bio
)
// NetlinkPath represents a path learned via Netlink of a route
type NetlinkPath struct {
......@@ -65,9 +73,6 @@ func NewNlPathFromRoute(r *netlink.Route, kernel bool) (*NetlinkPath, error) {
dst = bnet.NewPfxFromIPNet(r.Dst)
}
log.Warnf("IPFromBytes: %v goes to %v", r.Src, src)
log.Warnf("IPFromBytes: %v goes to %v", r.Dst, dst)
nextHop, _ := bnet.IPFromBytes(r.Gw)
return &NetlinkPath{
......
package route
import (
"fmt"
"testing"
bnet "github.com/bio-routing/bio-rd/net"
"github.com/stretchr/testify/assert"
"github.com/vishvananda/netlink"
)
func TestPathNextHop(t *testing.T) {
......@@ -321,3 +323,182 @@ func TestPathsContains(t *testing.T) {
}
}
}
func TestNewNlPath(t *testing.T) {
tests := []struct {
name string
source *Path
expected *NetlinkPath
}{
{
name: "BGPPath",
source: &Path{
Type: BGPPathType,
BGPPath: &BGPPath{
NextHop: bnet.IPv4(123),
},
},
expected: &NetlinkPath{
NextHop: bnet.IPv4(123),
Protocol: ProtoBio,
},
},
}
for _, test := range tests {
var converted *NetlinkPath
switch test.source.Type {
case BGPPathType:
converted = NewNlPathFromBgpPath(test.source.BGPPath)
default:
assert.Fail(t, fmt.Sprintf("Source-type %d is not supported", test.source.Type))
}
assert.Equalf(t, test.expected, converted, test.name)
}
}
func TestNewNlPathFromNetlinkRoute(t *testing.T) {
tests := []struct {
name string
source *netlink.Route
expected *NetlinkPath
expectError bool
}{
{
name: "Simple",
source: &netlink.Route{
Dst: bnet.NewPfx(bnet.IPv4FromOctets(10, 0, 0, 0), 8).GetIPNet(),
Src: bnet.IPv4(456).Bytes(),
Gw: bnet.IPv4(789).Bytes(),
Protocol: ProtoKernel,
Priority: 1,
Table: 254,
Type: 1,
},
expected: &NetlinkPath{
Dst: bnet.NewPfx(bnet.IPv4FromOctets(10, 0, 0, 0), 8),
Src: bnet.IPv4(456),
NextHop: bnet.IPv4(789),
Protocol: ProtoKernel,
Priority: 1,
Table: 254,
Type: 1,
Kernel: true,
},
expectError: false,
},
{
name: "No source, no destination",
source: &netlink.Route{
Gw: bnet.IPv4(789).Bytes(),
Protocol: ProtoKernel,
Priority: 1,
Table: 254,
Type: 1,
},
expected: &NetlinkPath{},
expectError: true,
},
{
name: "No source but destination",
source: &netlink.Route{
Dst: bnet.NewPfx(bnet.IPv4FromOctets(10, 0, 0, 0), 8).GetIPNet(),
Gw: bnet.IPv4(789).Bytes(),
Protocol: ProtoKernel,
Priority: 1,
Table: 254,
Type: 1,
},
expected: &NetlinkPath{
Dst: bnet.NewPfx(bnet.IPv4FromOctets(10, 0, 0, 0), 8),
Src: bnet.IPv4FromOctets(0, 0, 0, 0),
NextHop: bnet.IPv4(789),
Protocol: ProtoKernel,
Priority: 1,
Table: 254,
Type: 1,
Kernel: true,
},
expectError: false,
},
{
name: "Source but no destination",
source: &netlink.Route{
Src: bnet.IPv4(456).Bytes(),
Gw: bnet.IPv4(789).Bytes(),
Protocol: ProtoKernel,
Priority: 1,
Table: 254,
Type: 1,
},
expected: &NetlinkPath{
Dst: bnet.NewPfx(bnet.IPv4FromOctets(0, 0, 0, 0), 0),
Src: bnet.IPv4(456),
NextHop: bnet.IPv4(789),
Protocol: ProtoKernel,
Priority: 1,
Table: 254,
Type: 1,
Kernel: true,
},
expectError: false,
},
{
name: "No source but destination IPv6",
source: &netlink.Route{
Dst: bnet.NewPfx(bnet.IPv6(2001, 0), 48).GetIPNet(),
Gw: bnet.IPv6(2001, 2).Bytes(),
Protocol: ProtoKernel,
Priority: 1,
Table: 254,
Type: 1,
},
expected: &NetlinkPath{
Dst: bnet.NewPfx(bnet.IPv6(2001, 0), 48),
Src: bnet.IPv6FromBlocks(0, 0, 0, 0, 0, 0, 0, 0),
NextHop: bnet.IPv6(2001, 2),
Protocol: ProtoKernel,
Priority: 1,
Table: 254,
Type: 1,
Kernel: true,
},
expectError: false,
},
{
name: "Source but no destination IPv6",
source: &netlink.Route{
Src: bnet.IPv6(2001, 0).Bytes(),
Gw: bnet.IPv6(2001, 2).Bytes(),
Protocol: ProtoKernel,
Priority: 1,
Table: 254,
Type: 1,
},
expected: &NetlinkPath{
Dst: bnet.NewPfx(bnet.IPv6FromBlocks(0, 0, 0, 0, 0, 0, 0, 0), 0),
Src: bnet.IPv6(2001, 0),
NextHop: bnet.IPv6(2001, 2),
Protocol: ProtoKernel,
Priority: 1,
Table: 254,
Type: 1,
Kernel: true,
},
expectError: false,
},
}
for _, test := range tests {
converted, err := NewNlPathFromRoute(test.source, true)
if test.expectError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equalf(t, test.expected, converted, test.name)
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment