@@ -525,6 +525,173 @@ func Test_advertiseRoute(t *testing.T) {
525525	}
526526}
527527
528+ func  Test_OnNodeUpdate (t  * testing.T ) {
529+ 	testcases  :=  []struct  {
530+ 		name         string 
531+ 		nrc          * NetworkRoutingController 
532+ 		nodeEvents   []* watchers.NodeUpdate 
533+ 		activeNodes  map [string ]bool 
534+ 	}{
535+ 		{
536+ 			"node add event" ,
537+ 			& NetworkRoutingController {
538+ 				activeNodes :          make (map [string ]bool ),
539+ 				bgpServer :            gobgp .NewBgpServer (),
540+ 				defaultNodeAsnNumber : 1 ,
541+ 				clientset :            fake .NewSimpleClientset (),
542+ 			},
543+ 			[]* watchers.NodeUpdate {
544+ 				{
545+ 					Node : & v1core.Node {
546+ 						ObjectMeta : metav1.ObjectMeta {
547+ 							Name : "node-1" ,
548+ 						},
549+ 						Status : v1core.NodeStatus {
550+ 							Addresses : []v1core.NodeAddress {
551+ 								{
552+ 									Type :    v1core .NodeInternalIP ,
553+ 									Address : "10.0.0.1" ,
554+ 								},
555+ 							},
556+ 						},
557+ 					},
558+ 					Op : watchers .ADD ,
559+ 				},
560+ 			},
561+ 			map [string ]bool {
562+ 				"10.0.0.1" : true ,
563+ 			},
564+ 		},
565+ 		{
566+ 			"add multiple nodes" ,
567+ 			& NetworkRoutingController {
568+ 				activeNodes :          make (map [string ]bool ),
569+ 				bgpServer :            gobgp .NewBgpServer (),
570+ 				defaultNodeAsnNumber : 1 ,
571+ 				clientset :            fake .NewSimpleClientset (),
572+ 			},
573+ 			[]* watchers.NodeUpdate {
574+ 				{
575+ 					Node : & v1core.Node {
576+ 						ObjectMeta : metav1.ObjectMeta {
577+ 							Name : "node-1" ,
578+ 						},
579+ 						Status : v1core.NodeStatus {
580+ 							Addresses : []v1core.NodeAddress {
581+ 								{
582+ 									Type :    v1core .NodeInternalIP ,
583+ 									Address : "10.0.0.1" ,
584+ 								},
585+ 							},
586+ 						},
587+ 					},
588+ 					Op : watchers .ADD ,
589+ 				},
590+ 				{
591+ 					Node : & v1core.Node {
592+ 						ObjectMeta : metav1.ObjectMeta {
593+ 							Name : "node-2" ,
594+ 						},
595+ 						Status : v1core.NodeStatus {
596+ 							Addresses : []v1core.NodeAddress {
597+ 								{
598+ 									Type :    v1core .NodeExternalIP ,
599+ 									Address : "1.1.1.1" ,
600+ 								},
601+ 							},
602+ 						},
603+ 					},
604+ 					Op : watchers .ADD ,
605+ 				},
606+ 			},
607+ 			map [string ]bool {
608+ 				"10.0.0.1" : true ,
609+ 				"1.1.1.1" :  true ,
610+ 			},
611+ 		},
612+ 		{
613+ 			"add and then delete nodes" ,
614+ 			& NetworkRoutingController {
615+ 				activeNodes :          make (map [string ]bool ),
616+ 				bgpServer :            gobgp .NewBgpServer (),
617+ 				defaultNodeAsnNumber : 1 ,
618+ 				clientset :            fake .NewSimpleClientset (),
619+ 			},
620+ 			[]* watchers.NodeUpdate {
621+ 				{
622+ 					Node : & v1core.Node {
623+ 						ObjectMeta : metav1.ObjectMeta {
624+ 							Name : "node-1" ,
625+ 						},
626+ 						Status : v1core.NodeStatus {
627+ 							Addresses : []v1core.NodeAddress {
628+ 								{
629+ 									Type :    v1core .NodeInternalIP ,
630+ 									Address : "10.0.0.1" ,
631+ 								},
632+ 							},
633+ 						},
634+ 					},
635+ 					Op : watchers .ADD ,
636+ 				},
637+ 				{
638+ 					Node : & v1core.Node {
639+ 						ObjectMeta : metav1.ObjectMeta {
640+ 							Name : "node-1" ,
641+ 						},
642+ 						Status : v1core.NodeStatus {
643+ 							Addresses : []v1core.NodeAddress {
644+ 								{
645+ 									Type :    v1core .NodeInternalIP ,
646+ 									Address : "10.0.0.1" ,
647+ 								},
648+ 							},
649+ 						},
650+ 					},
651+ 					Op : watchers .REMOVE ,
652+ 				},
653+ 			},
654+ 			map [string ]bool {},
655+ 		},
656+ 	}
657+ 
658+ 	for  _ , testcase  :=  range  testcases  {
659+ 		t .Run (testcase .name , func (t  * testing.T ) {
660+ 			t .Log (testcase .name )
661+ 			go  testcase .nrc .bgpServer .Serve ()
662+ 			err  :=  testcase .nrc .bgpServer .Start (& config.Global {
663+ 				Config : config.GlobalConfig {
664+ 					As :       1 ,
665+ 					RouterId : "10.0.0.0" ,
666+ 					Port :     10000 ,
667+ 				},
668+ 			})
669+ 			if  err  !=  nil  {
670+ 				t .Fatalf ("failed to start BGP server: %v" , err )
671+ 			}
672+ 			defer  testcase .nrc .bgpServer .Stop ()
673+ 
674+ 			for  _ , nodeEvent  :=  range  testcase .nodeEvents  {
675+ 				testcase .nrc .OnNodeUpdate (nodeEvent )
676+ 			}
677+ 
678+ 			neighbors  :=  testcase .nrc .bgpServer .GetNeighbor ("" , false )
679+ 			for  _ , neighbor  :=  range  neighbors  {
680+ 				_ , exists  :=  testcase .activeNodes [neighbor .Config .NeighborAddress ]
681+ 				if  ! exists  {
682+ 					t .Errorf ("expected neighbor: %v doesn't exist" , neighbor .Config .NeighborAddress )
683+ 				}
684+ 			}
685+ 
686+ 			if  ! reflect .DeepEqual (testcase .nrc .activeNodes , testcase .activeNodes ) {
687+ 				t .Logf ("actual active nodes: %v" , testcase .nrc .activeNodes )
688+ 				t .Logf ("expected active nodes: %v" , testcase .activeNodes )
689+ 				t .Errorf ("did not get expected activeNodes" )
690+ 			}
691+ 		})
692+ 	}
693+ }
694+ 
528695func  createServices (clientset  kubernetes.Interface , svcs  []* v1core.Service ) error  {
529696	for  _ , svc  :=  range  svcs  {
530697		_ , err  :=  clientset .CoreV1 ().Services ("default" ).Create (svc )
0 commit comments