Skip to content
Snippets Groups Projects
routing_table_test.go 2.86 KiB
Newer Older
  • Learn to ignore specific revisions
  • Oliver Herms's avatar
    Oliver Herms committed
    package rt
    
    import (
    	"testing"
    
    	net "github.com/bio-routing/bio-rd/net"
    	"github.com/stretchr/testify/assert"
    )
    
    func TestNew(t *testing.T) {
    	l := New()
    	if l == nil {
    		t.Errorf("New() returned nil")
    	}
    }
    
    func TestRemovePath(t *testing.T) {
    	tests := []struct {
    		name     string
    		routes   []*Route
    		remove   []*Route
    		expected []*Route
    	}{
    		{
    			name: "Remove a path that is the only one for a prefix",
    			routes: []*Route{
    				NewRoute(net.NewPfx(strAddr("10.0.0.0"), 8), []*Path{
    					{
    						Type:    BGPPathType,
    						BGPPath: &BGPPath{},
    					},
    				}),
    
    Oliver Herms's avatar
    Oliver Herms committed
    				NewRoute(net.NewPfx(strAddr("10.0.0.0"), 9), []*Path{
    
    Oliver Herms's avatar
    Oliver Herms committed
    					{
    						Type:    BGPPathType,
    						BGPPath: &BGPPath{},
    					},
    
    Oliver Herms's avatar
    Oliver Herms committed
    				}),
    				NewRoute(net.NewPfx(strAddr("10.128.0.0"), 9), []*Path{
    
    Oliver Herms's avatar
    Oliver Herms committed
    					{
    						Type:    BGPPathType,
    						BGPPath: &BGPPath{},
    					},
    
    Oliver Herms's avatar
    Oliver Herms committed
    				}),
    
    Oliver Herms's avatar
    Oliver Herms committed
    			},
    			remove: []*Route{
    
    Oliver Herms's avatar
    Oliver Herms committed
    				NewRoute(net.NewPfx(strAddr("10.0.0.0"), 8), []*Path{
    
    Oliver Herms's avatar
    Oliver Herms committed
    					{
    						Type:    BGPPathType,
    						BGPPath: &BGPPath{},
    					},
    
    Oliver Herms's avatar
    Oliver Herms committed
    				}),
    
    Oliver Herms's avatar
    Oliver Herms committed
    			},
    			expected: []*Route{
    
    Oliver Herms's avatar
    Oliver Herms committed
    				NewRoute(net.NewPfx(strAddr("10.0.0.0"), 9), []*Path{
    
    Oliver Herms's avatar
    Oliver Herms committed
    					{
    						Type:    BGPPathType,
    						BGPPath: &BGPPath{},
    					},
    
    Oliver Herms's avatar
    Oliver Herms committed
    				}),
    				NewRoute(net.NewPfx(strAddr("10.128.0.0"), 9), []*Path{
    
    Oliver Herms's avatar
    Oliver Herms committed
    					{
    						Type:    BGPPathType,
    						BGPPath: &BGPPath{},
    					},
    
    Oliver Herms's avatar
    Oliver Herms committed
    				}),
    
    Oliver Herms's avatar
    Oliver Herms committed
    			},
    		},
    		{
    			name: "Remove a path that is one of two for a prefix",
    			routes: []*Route{
    
    Oliver Herms's avatar
    Oliver Herms committed
    				NewRoute(net.NewPfx(strAddr("10.0.0.0"), 8), []*Path{
    
    Oliver Herms's avatar
    Oliver Herms committed
    					{
    						Type: BGPPathType,
    						BGPPath: &BGPPath{
    							LocalPref: 1000,
    						},
    					},
    					{
    						Type: BGPPathType,
    						BGPPath: &BGPPath{
    							LocalPref: 2000,
    						},
    					},
    
    Oliver Herms's avatar
    Oliver Herms committed
    				}),
    				NewRoute(net.NewPfx(strAddr("10.0.0.0"), 9), []*Path{
    
    Oliver Herms's avatar
    Oliver Herms committed
    					{
    						Type:    BGPPathType,
    						BGPPath: &BGPPath{},
    					},
    
    Oliver Herms's avatar
    Oliver Herms committed
    				}),
    				NewRoute(net.NewPfx(strAddr("10.128.0.0"), 9), []*Path{
    
    Oliver Herms's avatar
    Oliver Herms committed
    					{
    						Type:    BGPPathType,
    						BGPPath: &BGPPath{},
    					},
    
    Oliver Herms's avatar
    Oliver Herms committed
    				}),
    
    Oliver Herms's avatar
    Oliver Herms committed
    			},
    			remove: []*Route{
    
    Oliver Herms's avatar
    Oliver Herms committed
    				NewRoute(net.NewPfx(strAddr("10.0.0.0"), 8), []*Path{
    
    Oliver Herms's avatar
    Oliver Herms committed
    					{
    						Type: BGPPathType,
    						BGPPath: &BGPPath{
    							LocalPref: 1000,
    						},
    					},
    
    Oliver Herms's avatar
    Oliver Herms committed
    				}),
    
    Oliver Herms's avatar
    Oliver Herms committed
    			},
    			expected: []*Route{
    
    Oliver Herms's avatar
    Oliver Herms committed
    				NewRoute(net.NewPfx(strAddr("10.0.0.0"), 8), []*Path{
    
    Oliver Herms's avatar
    Oliver Herms committed
    					{
    						Type: BGPPathType,
    						BGPPath: &BGPPath{
    							LocalPref: 2000,
    						},
    					},
    
    Oliver Herms's avatar
    Oliver Herms committed
    				}),
    				NewRoute(net.NewPfx(strAddr("10.0.0.0"), 9), []*Path{
    
    Oliver Herms's avatar
    Oliver Herms committed
    					{
    						Type:    BGPPathType,
    						BGPPath: &BGPPath{},
    					},
    
    Oliver Herms's avatar
    Oliver Herms committed
    				}),
    				NewRoute(net.NewPfx(strAddr("10.128.0.0"), 9), []*Path{
    
    Oliver Herms's avatar
    Oliver Herms committed
    					{
    						Type:    BGPPathType,
    						BGPPath: &BGPPath{},
    					},
    
    Oliver Herms's avatar
    Oliver Herms committed
    				}),
    
    Oliver Herms's avatar
    Oliver Herms committed
    			},
    		},
    	}
    
    	for _, test := range tests {
    		lpm := New()
    		for _, route := range test.routes {
    			lpm.Insert(route)
    		}
    
    		for _, route := range test.remove {
    			lpm.RemovePath(route)
    		}
    
    		res := lpm.Dump()
    		assert.Equal(t, test.expected, res)
    	}
    }
    
    func strAddr(s string) uint32 {
    	ret, _ := net.StrToAddr(s)
    	return ret
    }