Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package adjRIBIn
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/bio-routing/bio-rd/net"
"github.com/bio-routing/bio-rd/route"
)
type RTMockClient struct {
removePathParams struct {
pfx net.Prefix
path *route.Path
}
}
func NewRTMockClient() *RTMockClient {
return &RTMockClient{}
}
func (m *RTMockClient) AddPath(pfx net.Prefix, p *route.Path) error {
return nil
}
// RemovePath removes the path for prefix `pfx`
func (m *RTMockClient) RemovePath(pfx net.Prefix, p *route.Path) error {
m.removePathParams.pfx = pfx
m.removePathParams.path = p
return nil
}
func TestAddPath(t *testing.T) {
tests := []struct {
name string
routes []*route.Route
removePfx net.Prefix
removePath *route.Path
expected []*route.Route
}{
{
name: "Add route",
routes: []*route.Route{
route.NewRoute(net.NewPfx(strAddr("10.0.0.0"), 8), &route.Path{
Type: route.BGPPathType,
BGPPath: &route.BGPPath{
LocalPref: 100,
},
}),
},
removePfx: net.NewPfx(0, 0),
removePath: nil,
expected: []*route.Route{
route.NewRoute(net.NewPfx(strAddr("10.0.0.0"), 8), &route.Path{
Type: route.BGPPathType,
BGPPath: &route.BGPPath{
LocalPref: 100,
},
}),
},
},
{
name: "Overwrite routes",
routes: []*route.Route{
route.NewRoute(net.NewPfx(strAddr("10.0.0.0"), 8), &route.Path{
Type: route.BGPPathType,
BGPPath: &route.BGPPath{
LocalPref: 100,
},
}),
route.NewRoute(net.NewPfx(strAddr("10.0.0.0"), 8), &route.Path{
Type: route.BGPPathType,
BGPPath: &route.BGPPath{
LocalPref: 200,
},
}),
},
removePfx: net.NewPfx(strAddr("10.0.0.0"), 8),
removePath: &route.Path{
Type: route.BGPPathType,
BGPPath: &route.BGPPath{
LocalPref: 100,
},
},
expected: []*route.Route{
route.NewRoute(net.NewPfx(strAddr("10.0.0.0"), 8), &route.Path{
Type: route.BGPPathType,
BGPPath: &route.BGPPath{
LocalPref: 200,
},
}),
},
},
}
for _, test := range tests {
adjRIBIn := NewAdjRIBIn()
mc := NewRTMockClient()
adjRIBIn.ClientManager.Register(mc)
for _, route := range test.routes {
adjRIBIn.AddPath(route.Prefix(), route.Paths()[0])
}
if mc.removePathParams.pfx != test.removePfx {
t.Errorf("Test %q failed: Call to RemovePath did not propagate prefix properly: Got: %s Want: %s", test.name, mc.removePathParams.pfx.String(), test.removePfx.String())
}
assert.Equal(t, test.removePath, mc.removePathParams.path)
assert.Equal(t, test.expected, adjRIBIn.rt.Dump())
}
}
func TestRemovePath(t *testing.T) {
tests := []struct {
name string
routes []*route.Route
removePfx net.Prefix
removePath *route.Path
expected []*route.Route
wantPropagation bool
}{
{
name: "Remove an existing route",
routes: []*route.Route{
route.NewRoute(net.NewPfx(strAddr("10.0.0.0"), 8), &route.Path{
Type: route.BGPPathType,
BGPPath: &route.BGPPath{},
}),
route.NewRoute(net.NewPfx(strAddr("10.0.0.0"), 9), &route.Path{
Type: route.BGPPathType,
BGPPath: &route.BGPPath{},
}),
route.NewRoute(net.NewPfx(strAddr("10.128.0.0"), 9), &route.Path{
Type: route.BGPPathType,
BGPPath: &route.BGPPath{},
}),
},
removePfx: net.NewPfx(strAddr("10.0.0.0"), 8),
removePath: &route.Path{
Type: route.BGPPathType,
BGPPath: &route.BGPPath{},
},
expected: []*route.Route{
route.NewRoute(net.NewPfx(strAddr("10.0.0.0"), 9), &route.Path{
Type: route.BGPPathType,
BGPPath: &route.BGPPath{},
}),
route.NewRoute(net.NewPfx(strAddr("10.128.0.0"), 9), &route.Path{
Type: route.BGPPathType,
BGPPath: &route.BGPPath{},
}),
},
wantPropagation: true,
},
{
name: "Remove non existing route",
routes: []*route.Route{
route.NewRoute(net.NewPfx(strAddr("10.0.0.0"), 9), &route.Path{
Type: route.BGPPathType,
BGPPath: &route.BGPPath{},
}),
route.NewRoute(net.NewPfx(strAddr("10.128.0.0"), 9), &route.Path{
Type: route.BGPPathType,
BGPPath: &route.BGPPath{},
}),
},
removePfx: net.NewPfx(strAddr("10.0.0.0"), 8),
removePath: &route.Path{
Type: route.BGPPathType,
BGPPath: &route.BGPPath{},
},
expected: []*route.Route{
route.NewRoute(net.NewPfx(strAddr("10.0.0.0"), 9), &route.Path{
Type: route.BGPPathType,
BGPPath: &route.BGPPath{},
}),
route.NewRoute(net.NewPfx(strAddr("10.128.0.0"), 9), &route.Path{
Type: route.BGPPathType,
BGPPath: &route.BGPPath{},
}),
},
wantPropagation: false,
},
}
for _, test := range tests {
adjRIBIn := NewAdjRIBIn()
for _, route := range test.routes {
adjRIBIn.AddPath(route.Prefix(), route.Paths()[0])
}
mc := NewRTMockClient()
adjRIBIn.ClientManager.Register(mc)
adjRIBIn.RemovePath(test.removePfx, test.removePath)
if test.wantPropagation {
if mc.removePathParams.pfx != test.removePfx {
t.Errorf("Test %q failed: Call to RemovePath did not propagate prefix properly: Got: %s Want: %s", test.name, mc.removePathParams.pfx.String(), test.removePfx.String())
}
assert.Equal(t, test.removePath, mc.removePathParams.path)
t.Errorf("Test %q failed: Call to RemovePath propagated unexpectedly", test.name)
}
}
assert.Equal(t, test.expected, adjRIBIn.rt.Dump())
}
}
func strAddr(s string) uint32 {
ret, _ := net.StrToAddr(s)
return ret
}