Skip to content
Snippets Groups Projects
Commit 1ee4258a authored by Oliver Herms's avatar Oliver Herms
Browse files

Added some tests

parent db54622f
No related branches found
No related tags found
No related merge requests found
...@@ -2,11 +2,11 @@ package route ...@@ -2,11 +2,11 @@ package route
import ( import (
"fmt" "fmt"
"log"
bnet "github.com/bio-routing/bio-rd/net" bnet "github.com/bio-routing/bio-rd/net"
) )
// Path represents a network path
type Path struct { type Path struct {
Type uint8 Type uint8
StaticPath *StaticPath StaticPath *StaticPath
...@@ -46,6 +46,7 @@ func (p *Path) Select(q *Path) int8 { ...@@ -46,6 +46,7 @@ func (p *Path) Select(q *Path) int8 {
panic("Unknown path type") panic("Unknown path type")
} }
// ECMP checks if path p and q are equal enough to be considered for ECMP usage
func (p *Path) ECMP(q *Path) bool { func (p *Path) ECMP(q *Path) bool {
switch p.Type { switch p.Type {
case BGPPathType: case BGPPathType:
...@@ -59,6 +60,7 @@ func (p *Path) ECMP(q *Path) bool { ...@@ -59,6 +60,7 @@ func (p *Path) ECMP(q *Path) bool {
panic("Unknown path type") panic("Unknown path type")
} }
// Equal checks if paths p and q are equal
func (p *Path) Equal(q *Path) bool { func (p *Path) Equal(q *Path) bool {
if p == nil || q == nil { if p == nil || q == nil {
return false return false
...@@ -162,9 +164,7 @@ func (p *Path) NextHop() bnet.IP { ...@@ -162,9 +164,7 @@ func (p *Path) NextHop() bnet.IP {
return p.StaticPath.NextHop return p.StaticPath.NextHop
case NetlinkPathType: case NetlinkPathType:
return p.NetlinkPath.NextHop return p.NetlinkPath.NextHop
default:
log.Panic("Type %d not implemented (yet)", p.Type)
} }
return bnet.IP{} panic("Unknown path type")
} }
...@@ -3,9 +3,169 @@ package route ...@@ -3,9 +3,169 @@ package route
import ( import (
"testing" "testing"
bnet "github.com/bio-routing/bio-rd/net"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestPathNextHop(t *testing.T) {
tests := []struct {
name string
p *Path
expected bnet.IP
}{
{
name: "BGP Path",
p: &Path{
Type: BGPPathType,
BGPPath: &BGPPath{
NextHop: bnet.IPv4(123),
},
},
expected: bnet.IPv4(123),
},
{
name: "Static Path",
p: &Path{
Type: StaticPathType,
StaticPath: &StaticPath{
NextHop: bnet.IPv4(456),
},
},
expected: bnet.IPv4(456),
},
{
name: "Netlink Path",
p: &Path{
Type: NetlinkPathType,
NetlinkPath: &NetlinkPath{
NextHop: bnet.IPv4(1000),
},
},
expected: bnet.IPv4(1000),
},
}
for _, test := range tests {
res := test.p.NextHop()
assert.Equal(t, test.expected, res, test.name)
}
}
func TestPathCopy(t *testing.T) {
tests := []struct {
name string
p *Path
expected *Path
}{
{
name: "nil test",
},
}
for _, test := range tests {
res := test.p.Copy()
assert.Equal(t, test.expected, res, test.name)
}
}
func TestEqual(t *testing.T) {
tests := []struct {
name string
p *Path
q *Path
expected bool
}{
{
name: "Different types",
p: &Path{Type: 100},
q: &Path{Type: 200},
expected: false,
},
}
for _, test := range tests {
res := test.p.Equal(test.q)
assert.Equalf(t, test.expected, res, test.name)
}
}
func TestSelect(t *testing.T) {
tests := []struct {
name string
p *Path
q *Path
expected int8
}{
{
name: "All nil",
expected: 0,
},
{
name: "p nil",
q: &Path{},
expected: -1,
},
{
name: "q nil",
p: &Path{},
expected: 1,
},
{
name: "p > q",
p: &Path{Type: 20},
q: &Path{Type: 10},
expected: 1,
},
{
name: "p < q",
p: &Path{Type: 10},
q: &Path{Type: 20},
expected: -1,
},
{
name: "Static",
p: &Path{
Type: StaticPathType,
StaticPath: &StaticPath{},
},
q: &Path{
Type: StaticPathType,
StaticPath: &StaticPath{},
},
expected: 0,
},
{
name: "BGP",
p: &Path{
Type: BGPPathType,
BGPPath: &BGPPath{},
},
q: &Path{
Type: BGPPathType,
BGPPath: &BGPPath{},
},
expected: 0,
},
{
name: "Netlink",
p: &Path{
Type: NetlinkPathType,
NetlinkPath: &NetlinkPath{},
},
q: &Path{
Type: NetlinkPathType,
NetlinkPath: &NetlinkPath{},
},
expected: 0,
},
}
for _, test := range tests {
res := test.p.Select(test.q)
assert.Equalf(t, test.expected, res, "Test %q", test.name)
}
}
func TestPathsDiff(t *testing.T) { func TestPathsDiff(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
......
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