Newer
Older
package route
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewRoute(t *testing.T) {
tests := []struct {
name string
path *Path
expected *Route
}{
{
name: "BGP Path",
pfx: bnet.NewPfx(bnet.IPv4FromOctets(10, 0, 0, 0), 8),
path: &Path{
Type: BGPPathType,
BGPPath: &BGPPath{},
},
expected: &Route{
pfx: bnet.NewPfx(bnet.IPv4FromOctets(10, 0, 0, 0), 8),
paths: []*Path{
&Path{
Type: BGPPathType,
BGPPath: &BGPPath{},
},
},
},
},
{
name: "Empty Path",
pfx: bnet.NewPfx(bnet.IPv4FromOctets(10, 0, 0, 0), 8),
pfx: bnet.NewPfx(bnet.IPv4FromOctets(10, 0, 0, 0), 8),
paths: []*Path{},
},
},
}
for _, test := range tests {
res := NewRoute(test.pfx, test.path)
assert.Equal(t, test.expected, res)
}
}
func TestPrefix(t *testing.T) {
tests := []struct {
name string
route *Route
}{
{
name: "Prefix",
route: &Route{
pfx: bnet.NewPfx(bnet.IPv4FromOctets(10, 0, 0, 0), 8),
expected: bnet.NewPfx(bnet.IPv4FromOctets(10, 0, 0, 0), 8),
},
}
for _, test := range tests {
res := test.route.Prefix()
assert.Equal(t, test.expected, res)
}
}
func TestAddr(t *testing.T) {
tests := []struct {
name string
route *Route
}{
{
name: "Prefix",
route: &Route{
pfx: bnet.NewPfx(bnet.IPv4FromOctets(10, 0, 0, 0), 8),
},
}
for _, test := range tests {
res := test.route.Addr()
assert.Equal(t, test.expected, res)
}
}
func TestPfxlen(t *testing.T) {
tests := []struct {
name string
route *Route
expected uint8
}{
{
name: "Prefix",
route: &Route{
pfx: bnet.NewPfx(bnet.IPv4FromOctets(10, 0, 0, 0), 8),
},
expected: 8,
},
}
for _, test := range tests {
res := test.route.Pfxlen()
assert.Equal(t, test.expected, res)
}
}
func TestAddPath(t *testing.T) {
tests := []struct {
name string
route *Route
newPath *Path
expected *Route
}{
{
name: "Regular BGP path",
route: NewRoute(bnet.NewPfx(bnet.IPv4FromOctets(10, 0, 0, 0), 8), &Path{
Type: BGPPathType,
BGPPath: &BGPPath{},
}),
newPath: &Path{
Type: BGPPathType,
BGPPath: &BGPPath{},
},
expected: &Route{
pfx: bnet.NewPfx(bnet.IPv4FromOctets(10, 0, 0, 0), 8),
paths: []*Path{
{
Type: BGPPathType,
BGPPath: &BGPPath{},
},
{
Type: BGPPathType,
BGPPath: &BGPPath{},
},
},
},
},
{
name: "Nil path",
route: NewRoute(bnet.NewPfx(bnet.IPv4FromOctets(10, 0, 0, 0), 8), &Path{
Type: BGPPathType,
BGPPath: &BGPPath{},
}),
newPath: nil,
expected: &Route{
pfx: bnet.NewPfx(bnet.IPv4FromOctets(10, 0, 0, 0), 8),
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
paths: []*Path{
{
Type: BGPPathType,
BGPPath: &BGPPath{},
},
},
},
},
}
for _, test := range tests {
test.route.AddPath(test.newPath)
assert.Equal(t, test.expected, test.route)
}
}
func TestRouteRemovePath(t *testing.T) {
tests := []struct {
name string
paths []*Path
remove *Path
expected []*Path
}{
{
name: "Remove middle",
paths: []*Path{
{
Type: BGPPathType,
BGPPath: &BGPPath{
LocalPref: 100,
},
},
{
Type: BGPPathType,
BGPPath: &BGPPath{
LocalPref: 200,
},
},
{
Type: BGPPathType,
BGPPath: &BGPPath{
LocalPref: 300,
},
},
},
remove: &Path{
Type: BGPPathType,
BGPPath: &BGPPath{
LocalPref: 200,
},
},
expected: []*Path{
{
Type: BGPPathType,
BGPPath: &BGPPath{
LocalPref: 100,
},
},
{
Type: BGPPathType,
BGPPath: &BGPPath{
LocalPref: 300,
},
},
},
},
{
name: "Remove non-existent",
paths: []*Path{
{
Type: BGPPathType,
BGPPath: &BGPPath{
LocalPref: 10,
},
},
{
Type: BGPPathType,
BGPPath: &BGPPath{
LocalPref: 20,
},
},
},
remove: &Path{
Type: BGPPathType,
BGPPath: &BGPPath{
LocalPref: 50,
},
},
expected: []*Path{
{
Type: BGPPathType,
BGPPath: &BGPPath{
LocalPref: 10,
},
},
{
Type: BGPPathType,
BGPPath: &BGPPath{
LocalPref: 20,
},
},
},
},
}
for _, test := range tests {
res := removePath(test.paths, test.remove)
assert.Equal(t, test.expected, res)
}
}
func TestCopy(t *testing.T) {
tests := []struct {
name string
route *Route
expected *Route
}{
{
name: "",
route: &Route{
pfx: bnet.NewPfx(bnet.IPv4(1000), 8),
ecmpPaths: 2,
paths: []*Path{
{
Type: 100,
},
},
},
expected: &Route{
pfx: bnet.NewPfx(bnet.IPv4(1000), 8),
ecmpPaths: 2,
paths: []*Path{
{
Type: 100,
},
},
},
},
Christoph Petrausch
committed
{
name: "",
},
}
for _, test := range tests {
res := test.route.Copy()
assert.Equal(t, test.expected, res)
}
}
Christoph Petrausch
committed
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
func TestECMPPathCount(t *testing.T) {
var r *Route
assert.Equal(t, uint(0), r.ECMPPathCount())
r = &Route{}
assert.Equal(t, uint(0), r.ECMPPathCount())
r.ecmpPaths = 12
assert.Equal(t, uint(12), r.ECMPPathCount())
}
func TestBestPath(t *testing.T) {
tests := []struct {
route *Route
expected *Path
}{
{
route: nil,
expected: nil,
},
{
route: &Route{},
expected: nil,
},
{
route: &Route{
paths: []*Path{
{
Type: StaticPathType,
StaticPath: &StaticPath{
Christoph Petrausch
committed
},
},
},
},
expected: &Path{
Type: StaticPathType,
StaticPath: &StaticPath{
Christoph Petrausch
committed
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
},
},
},
}
for _, tc := range tests {
assert.Equal(t, tc.expected, tc.route.BestPath())
}
}
func TestECMPPaths(t *testing.T) {
tests := []struct {
route *Route
expected []*Path
}{
{
route: nil,
expected: nil,
},
{
route: &Route{},
expected: nil,
},
{
route: &Route{
ecmpPaths: 2,
paths: []*Path{
{
Type: StaticPathType,
StaticPath: &StaticPath{
Christoph Petrausch
committed
},
},
{
Type: StaticPathType,
StaticPath: &StaticPath{
Christoph Petrausch
committed
},
},
},
},
expected: []*Path{
{
Type: StaticPathType,
StaticPath: &StaticPath{
Christoph Petrausch
committed
},
},
{
Type: StaticPathType,
StaticPath: &StaticPath{