@@ -2,6 +2,7 @@ package routing
2
2
3
3
import (
4
4
"context"
5
+ "fmt"
5
6
"reflect"
6
7
"testing"
7
8
"time"
@@ -26,7 +27,8 @@ type PolicyTestCase struct {
26
27
allPeerDefinedSet * gobgpapi.DefinedSet
27
28
exportPolicyStatements []* gobgpapi.Statement
28
29
importPolicyStatements []* gobgpapi.Statement
29
- err error
30
+ addPolicyErr error
31
+ startBGPServerErr error
30
32
}
31
33
32
34
func Test_AddPolicies (t * testing.T ) {
@@ -36,6 +38,8 @@ func Test_AddPolicies(t *testing.T) {
36
38
& NetworkRoutingController {
37
39
clientset : fake .NewSimpleClientset (),
38
40
hostnameOverride : "node-1" ,
41
+ routerID : "10.0.0.0" ,
42
+ bgpPort : 10000 ,
39
43
bgpFullMeshMode : false ,
40
44
bgpEnableInternal : true ,
41
45
bgpServer : gobgp .NewBgpServer (),
@@ -121,6 +125,7 @@ func Test_AddPolicies(t *testing.T) {
121
125
MatchType : gobgpapi .MatchType_ANY ,
122
126
Name : "iBGPpeerset" ,
123
127
},
128
+ RpkiResult : - 1 ,
124
129
},
125
130
Actions : & gobgpapi.Actions {
126
131
RouteAction : gobgpapi .RouteAction_ACCEPT ,
@@ -139,19 +144,23 @@ func Test_AddPolicies(t *testing.T) {
139
144
MatchType : gobgpapi .MatchType_ANY ,
140
145
Name : "allpeerset" ,
141
146
},
147
+ RpkiResult : - 1 ,
142
148
},
143
149
Actions : & gobgpapi.Actions {
144
150
RouteAction : gobgpapi .RouteAction_REJECT ,
145
151
},
146
152
},
147
153
},
148
154
nil ,
155
+ nil ,
149
156
},
150
157
{
151
158
"has nodes, services with external peers" ,
152
159
& NetworkRoutingController {
153
160
clientset : fake .NewSimpleClientset (),
154
161
hostnameOverride : "node-1" ,
162
+ routerID : "10.0.0.0" ,
163
+ bgpPort : 10000 ,
155
164
bgpFullMeshMode : false ,
156
165
bgpEnableInternal : true ,
157
166
bgpServer : gobgp .NewBgpServer (),
@@ -253,6 +262,7 @@ func Test_AddPolicies(t *testing.T) {
253
262
MatchType : gobgpapi .MatchType_ANY ,
254
263
Name : "iBGPpeerset" ,
255
264
},
265
+ RpkiResult : - 1 ,
256
266
},
257
267
Actions : & gobgpapi.Actions {
258
268
RouteAction : gobgpapi .RouteAction_ACCEPT ,
@@ -269,6 +279,7 @@ func Test_AddPolicies(t *testing.T) {
269
279
MatchType : gobgpapi .MatchType_ANY ,
270
280
Name : "externalpeerset" ,
271
281
},
282
+ RpkiResult : - 1 ,
272
283
},
273
284
Actions : & gobgpapi.Actions {
274
285
RouteAction : gobgpapi .RouteAction_ACCEPT ,
@@ -287,19 +298,23 @@ func Test_AddPolicies(t *testing.T) {
287
298
MatchType : gobgpapi .MatchType_ANY ,
288
299
Name : "allpeerset" ,
289
300
},
301
+ RpkiResult : - 1 ,
290
302
},
291
303
Actions : & gobgpapi.Actions {
292
304
RouteAction : gobgpapi .RouteAction_REJECT ,
293
305
},
294
306
},
295
307
},
296
308
nil ,
309
+ nil ,
297
310
},
298
311
{
299
312
"has nodes, services with external peers and iBGP disabled" ,
300
313
& NetworkRoutingController {
301
314
clientset : fake .NewSimpleClientset (),
302
315
hostnameOverride : "node-1" ,
316
+ routerID : "10.0.0.0" ,
317
+ bgpPort : 10000 ,
303
318
bgpFullMeshMode : false ,
304
319
bgpEnableInternal : false ,
305
320
bgpServer : gobgp .NewBgpServer (),
@@ -401,6 +416,7 @@ func Test_AddPolicies(t *testing.T) {
401
416
MatchType : gobgpapi .MatchType_ANY ,
402
417
Name : "externalpeerset" ,
403
418
},
419
+ RpkiResult : - 1 ,
404
420
},
405
421
Actions : & gobgpapi.Actions {
406
422
RouteAction : gobgpapi .RouteAction_ACCEPT ,
@@ -419,24 +435,25 @@ func Test_AddPolicies(t *testing.T) {
419
435
MatchType : gobgpapi .MatchType_ANY ,
420
436
Name : "allpeerset" ,
421
437
},
438
+ RpkiResult : - 1 ,
422
439
},
423
440
Actions : & gobgpapi.Actions {
424
441
RouteAction : gobgpapi .RouteAction_REJECT ,
425
442
},
426
443
},
427
444
},
428
445
nil ,
446
+ nil ,
429
447
},
430
448
{
431
449
"prepends AS with external peers" ,
432
450
& NetworkRoutingController {
433
451
clientset : fake .NewSimpleClientset (),
434
452
hostnameOverride : "node-1" ,
453
+ routerID : "10.0.0.0" ,
454
+ bgpPort : 10000 ,
435
455
bgpEnableInternal : true ,
436
456
bgpFullMeshMode : false ,
437
- pathPrepend : true ,
438
- pathPrependCount : 5 ,
439
- pathPrependAS : "65100" ,
440
457
bgpServer : gobgp .NewBgpServer (),
441
458
activeNodes : make (map [string ]bool ),
442
459
podCidr : "172.20.0.0/24" ,
@@ -459,7 +476,9 @@ func Test_AddPolicies(t *testing.T) {
459
476
ObjectMeta : metav1.ObjectMeta {
460
477
Name : "node-1" ,
461
478
Annotations : map [string ]string {
462
- "kube-router.io/node.asn" : "100" ,
479
+ "kube-router.io/node.asn" : "100" ,
480
+ "kube-router.io/path-prepend.as" : "65100" ,
481
+ "kube-router.io/path-prepend.repeat-n" : "5" ,
463
482
},
464
483
},
465
484
Status : v1core.NodeStatus {
@@ -536,6 +555,7 @@ func Test_AddPolicies(t *testing.T) {
536
555
MatchType : gobgpapi .MatchType_ANY ,
537
556
Name : "iBGPpeerset" ,
538
557
},
558
+ RpkiResult : - 1 ,
539
559
},
540
560
Actions : & gobgpapi.Actions {
541
561
RouteAction : gobgpapi .RouteAction_ACCEPT ,
@@ -552,6 +572,7 @@ func Test_AddPolicies(t *testing.T) {
552
572
MatchType : gobgpapi .MatchType_ANY ,
553
573
Name : "externalpeerset" ,
554
574
},
575
+ RpkiResult : - 1 ,
555
576
},
556
577
Actions : & gobgpapi.Actions {
557
578
RouteAction : gobgpapi .RouteAction_ACCEPT ,
@@ -574,23 +595,25 @@ func Test_AddPolicies(t *testing.T) {
574
595
MatchType : gobgpapi .MatchType_ANY ,
575
596
Name : "allpeerset" ,
576
597
},
598
+ RpkiResult : - 1 ,
577
599
},
578
600
Actions : & gobgpapi.Actions {
579
601
RouteAction : gobgpapi .RouteAction_REJECT ,
580
602
},
581
603
},
582
604
},
583
605
nil ,
606
+ nil ,
584
607
},
585
608
{
586
609
"only prepends AS when both node annotations are present" ,
587
610
& NetworkRoutingController {
588
611
clientset : fake .NewSimpleClientset (),
589
612
hostnameOverride : "node-1" ,
613
+ routerID : "10.0.0.0" ,
614
+ bgpPort : 10000 ,
590
615
bgpEnableInternal : true ,
591
616
bgpFullMeshMode : false ,
592
- pathPrepend : false ,
593
- pathPrependAS : "65100" ,
594
617
bgpServer : gobgp .NewBgpServer (),
595
618
activeNodes : make (map [string ]bool ),
596
619
podCidr : "172.20.0.0/24" ,
@@ -613,7 +636,8 @@ func Test_AddPolicies(t *testing.T) {
613
636
ObjectMeta : metav1.ObjectMeta {
614
637
Name : "node-1" ,
615
638
Annotations : map [string ]string {
616
- "kube-router.io/node.asn" : "100" ,
639
+ "kube-router.io/node.asn" : "100" ,
640
+ "kube-router.io/path-prepend.as" : "65100" ,
617
641
},
618
642
},
619
643
Status : v1core.NodeStatus {
@@ -690,6 +714,7 @@ func Test_AddPolicies(t *testing.T) {
690
714
MatchType : gobgpapi .MatchType_ANY ,
691
715
Name : "iBGPpeerset" ,
692
716
},
717
+ RpkiResult : - 1 ,
693
718
},
694
719
Actions : & gobgpapi.Actions {
695
720
RouteAction : gobgpapi .RouteAction_ACCEPT ,
@@ -706,6 +731,7 @@ func Test_AddPolicies(t *testing.T) {
706
731
MatchType : gobgpapi .MatchType_ANY ,
707
732
Name : "externalpeerset" ,
708
733
},
734
+ RpkiResult : - 1 ,
709
735
},
710
736
Actions : & gobgpapi.Actions {
711
737
RouteAction : gobgpapi .RouteAction_ACCEPT ,
@@ -728,44 +754,46 @@ func Test_AddPolicies(t *testing.T) {
728
754
MatchType : gobgpapi .MatchType_ANY ,
729
755
Name : "allpeerset" ,
730
756
},
757
+ RpkiResult : - 1 ,
731
758
},
732
759
Actions : & gobgpapi.Actions {
733
760
RouteAction : gobgpapi .RouteAction_REJECT ,
734
761
},
735
762
},
736
763
},
737
764
nil ,
765
+ fmt .Errorf ("both %s and %s must be set" , pathPrependASNAnnotation , pathPrependRepeatNAnnotation ),
738
766
},
739
767
}
740
768
741
769
for _ , testcase := range testcases {
742
770
t .Run (testcase .name , func (t * testing.T ) {
743
- go testcase .nrc .bgpServer .Serve ()
744
- global := & gobgpapi.Global {
745
- As : 1 ,
746
- RouterId : "10.0.0.0" ,
747
- ListenPort : 10000 ,
748
- }
749
- err := testcase .nrc .bgpServer .StartBgp (context .Background (), & gobgpapi.StartBgpRequest {Global : global })
750
- if err != nil {
751
- t .Fatalf ("failed to start BGP server: %v" , err )
752
- }
753
- defer func () {
754
- if err := testcase .nrc .bgpServer .StopBgp (context .Background (), & gobgpapi.StopBgpRequest {}); err != nil {
755
- t .Fatalf ("failed to stop BGP server : %s" , err )
756
- }
757
- }()
758
-
759
771
startInformersForRoutes (testcase .nrc , testcase .nrc .clientset )
760
772
761
- if err = createNodes (testcase .nrc .clientset , testcase .existingNodes ); err != nil {
773
+ if err : = createNodes (testcase .nrc .clientset , testcase .existingNodes ); err != nil {
762
774
t .Errorf ("failed to create existing nodes: %v" , err )
763
775
}
764
776
765
- if err = createServices (testcase .nrc .clientset , testcase .existingServices ); err != nil {
777
+ if err : = createServices (testcase .nrc .clientset , testcase .existingServices ); err != nil {
766
778
t .Errorf ("failed to create existing nodes: %v" , err )
767
779
}
768
780
781
+ err := testcase .nrc .startBgpServer (false )
782
+ if ! reflect .DeepEqual (err , testcase .startBGPServerErr ) {
783
+ t .Logf ("expected err when invoking startBGPServer(): %v" , testcase .startBGPServerErr )
784
+ t .Logf ("actual err from startBGPServer() received: %v" , err )
785
+ t .Error ("unexpected error" )
786
+ }
787
+ // If the server was not expected to start we should stop here as the rest of the tests are unimportant
788
+ if testcase .startBGPServerErr != nil {
789
+ return
790
+ }
791
+ defer func () {
792
+ if err := testcase .nrc .bgpServer .StopBgp (context .Background (), & gobgpapi.StopBgpRequest {}); err != nil {
793
+ t .Fatalf ("failed to stop BGP server : %s" , err )
794
+ }
795
+ }()
796
+
769
797
// ClusterIPs and ExternalIPs
770
798
waitForListerWithTimeout (testcase .nrc .svcLister , time .Second * 10 , t )
771
799
@@ -777,21 +805,25 @@ func Test_AddPolicies(t *testing.T) {
777
805
nodeInformer := informerFactory .Core ().V1 ().Nodes ().Informer ()
778
806
testcase .nrc .nodeLister = nodeInformer .GetIndexer ()
779
807
err = testcase .nrc .AddPolicies ()
780
- if ! reflect .DeepEqual (err , testcase .err ) {
781
- t .Logf ("expected err %v" , testcase .err )
782
- t .Logf ("actual err %v" , err )
808
+ if ! reflect .DeepEqual (err , testcase .addPolicyErr ) {
809
+ t .Logf ("expected err when invoking AddPolicies(): %v" , testcase .addPolicyErr )
810
+ t .Logf ("actual err from AddPolicies() received: %v" , err )
783
811
t .Error ("unexpected error" )
784
812
}
813
+ // If we expect AddPolicies() to fail we should stop here as there is no point in further evaluating policies
814
+ if testcase .addPolicyErr != nil {
815
+ return
816
+ }
785
817
786
- err = testcase .nrc .bgpServer .ListDefinedSet (context .Background (), & gobgpapi. ListDefinedSetRequest {
787
- DefinedType : gobgpapi .DefinedType_PREFIX ,
788
- Name : "podcidrdefinedset" }, func (podDefinedSet * gobgpapi.DefinedSet ) {
789
- if ! reflect .DeepEqual (podDefinedSet , testcase .podDefinedSet ) {
790
- t .Logf ("expected pod defined set: %+v" , testcase .podDefinedSet )
791
- t .Logf ("actual pod defined set: %+v" , podDefinedSet )
792
- t .Error ("unexpected pod defined set" )
793
- }
794
- })
818
+ err = testcase .nrc .bgpServer .ListDefinedSet (context .Background (),
819
+ & gobgpapi. ListDefinedSetRequest { DefinedType : gobgpapi .DefinedType_PREFIX , Name : "podcidrdefinedset" } ,
820
+ func (podDefinedSet * gobgpapi.DefinedSet ) {
821
+ if ! reflect .DeepEqual (podDefinedSet , testcase .podDefinedSet ) {
822
+ t .Logf ("expected pod defined set: %+v" , testcase .podDefinedSet )
823
+ t .Logf ("actual pod defined set: %+v" , podDefinedSet )
824
+ t .Error ("unexpected pod defined set" )
825
+ }
826
+ })
795
827
if err != nil {
796
828
t .Fatalf ("error validating defined sets: %v" , err )
797
829
}
0 commit comments