github.com/osrg/gobgp/v3@v3.30.0/test/scenario_test/vrf_neighbor_test.py (about) 1 # Copyright (C) 2016 Nippon Telegraph and Telephone Corporation. 2 # 3 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # you may not use this file except in compliance with the License. 5 # You may obtain a copy of the License at 6 # 7 # http://www.apache.org/licenses/LICENSE-2.0 8 # 9 # Unless required by applicable law or agreed to in writing, software 10 # distributed under the License is distributed on an "AS IS" BASIS, 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 12 # implied. 13 # See the License for the specific language governing permissions and 14 # limitations under the License. 15 16 17 import sys 18 import time 19 import unittest 20 21 import collections 22 collections.Callable = collections.abc.Callable 23 24 import nose 25 26 from lib.noseplugin import OptionParser, parser_option 27 28 from lib import base 29 from lib.base import BGP_FSM_ESTABLISHED, local 30 from lib.gobgp import GoBGPContainer 31 32 33 class GoBGPTestBase(unittest.TestCase): 34 35 @classmethod 36 def setUpClass(cls): 37 gobgp_ctn_image_name = parser_option.gobgp_image 38 base.TEST_PREFIX = parser_option.test_prefix 39 40 g1 = GoBGPContainer(name='g1', asn=65001, router_id='192.168.0.1', 41 ctn_image_name=gobgp_ctn_image_name, 42 log_level=parser_option.gobgp_log_level, 43 config_format='yaml') 44 g2 = GoBGPContainer(name='g2', asn=65002, router_id='192.168.0.2', 45 ctn_image_name=gobgp_ctn_image_name, 46 log_level=parser_option.gobgp_log_level, 47 config_format='yaml') 48 g3 = GoBGPContainer(name='g3', asn=65003, router_id='192.168.0.3', 49 ctn_image_name=gobgp_ctn_image_name, 50 log_level=parser_option.gobgp_log_level, 51 config_format='yaml') 52 g4 = GoBGPContainer(name='g4', asn=65004, router_id='192.168.0.4', 53 ctn_image_name=gobgp_ctn_image_name, 54 log_level=parser_option.gobgp_log_level, 55 config_format='yaml') 56 g5 = GoBGPContainer(name='g5', asn=65005, router_id='192.168.0.5', 57 ctn_image_name=gobgp_ctn_image_name, 58 log_level=parser_option.gobgp_log_level, 59 config_format='yaml') 60 g6 = GoBGPContainer(name='g6', asn=65006, router_id='192.168.0.6', 61 ctn_image_name=gobgp_ctn_image_name, 62 log_level=parser_option.gobgp_log_level, 63 config_format='yaml') 64 g7 = GoBGPContainer(name='g7', asn=65007, router_id='192.168.0.7', 65 ctn_image_name=gobgp_ctn_image_name, 66 log_level=parser_option.gobgp_log_level, 67 config_format='yaml') 68 69 ctns = [g1, g2, g3, g4, g5, g6, g7] 70 71 initial_wait_time = max(ctn.run() for ctn in ctns) 72 73 time.sleep(initial_wait_time) 74 75 g4.local("gobgp vrf add red rd 10:10 rt both 10:10") 76 g4.local("gobgp vrf add blue rd 20:20 rt both 20:20") 77 78 g5.local("gobgp vrf add red rd 10:10 rt both 10:10") 79 g5.local("gobgp vrf add blue rd 20:20 rt both 20:20") 80 81 g1.add_peer(g4) 82 g4.add_peer(g1, vrf='red') 83 84 g2.add_peer(g4) 85 g4.add_peer(g2, vrf='red') 86 87 g3.add_peer(g4) 88 g4.add_peer(g3, vrf='blue') 89 90 g4.add_peer(g5, vpn=True) 91 g5.add_peer(g4, vpn=True) 92 93 g5.add_peer(g6, vrf='red') 94 g6.add_peer(g5) 95 96 g5.add_peer(g7, vrf='blue') 97 g7.add_peer(g5) 98 99 cls.g1 = g1 100 cls.g2 = g2 101 cls.g3 = g3 102 cls.g4 = g4 103 cls.g5 = g5 104 cls.g6 = g6 105 cls.g7 = g7 106 107 # test each neighbor state is turned establish 108 def test_01_neighbor_established(self): 109 self.g4.wait_for(expected_state=BGP_FSM_ESTABLISHED, peer=self.g1) 110 self.g4.wait_for(expected_state=BGP_FSM_ESTABLISHED, peer=self.g2) 111 self.g4.wait_for(expected_state=BGP_FSM_ESTABLISHED, peer=self.g3) 112 self.g4.wait_for(expected_state=BGP_FSM_ESTABLISHED, peer=self.g5) 113 self.g5.wait_for(expected_state=BGP_FSM_ESTABLISHED, peer=self.g6) 114 self.g5.wait_for(expected_state=BGP_FSM_ESTABLISHED, peer=self.g7) 115 116 def test_02_inject_from_vrf_red(self): 117 self.g1.local('gobgp global rib add 10.0.0.0/24') 118 119 time.sleep(1) 120 121 dst = self.g2.get_global_rib('10.0.0.0/24') 122 self.assertEqual(len(dst), 1) 123 self.assertEqual(len(dst[0]['paths']), 1) 124 path = dst[0]['paths'][0] 125 self.assertEqual([self.g4.asn, self.g1.asn], path['aspath']) 126 127 dst = self.g3.get_global_rib('10.0.0.0/24') 128 self.assertEqual(len(dst), 0) 129 130 dst = self.g4.get_global_rib(rf='vpnv4') 131 self.assertEqual(len(dst), 1) 132 self.assertEqual(len(dst[0]['paths']), 1) 133 path = dst[0]['paths'][0] 134 self.assertEqual([self.g1.asn], path['aspath']) 135 136 dst = self.g6.get_global_rib('10.0.0.0/24') 137 self.assertEqual(len(dst), 1) 138 self.assertEqual(len(dst[0]['paths']), 1) 139 path = dst[0]['paths'][0] 140 self.assertEqual([self.g5.asn, self.g4.asn, self.g1.asn], path['aspath']) 141 142 dst = self.g7.get_global_rib('10.0.0.0/24') 143 self.assertEqual(len(dst), 0) 144 145 def test_03_inject_from_vrf_blue(self): 146 self.g3.local('gobgp global rib add 10.0.0.0/24') 147 148 time.sleep(1) 149 150 dst = self.g2.get_global_rib('10.0.0.0/24') 151 self.assertEqual(len(dst), 1) 152 self.assertEqual(len(dst[0]['paths']), 1) 153 path = dst[0]['paths'][0] 154 self.assertEqual([self.g4.asn, self.g1.asn], path['aspath']) 155 156 dst = self.g4.get_global_rib(rf='vpnv4') 157 self.assertEqual(len(dst), 2) 158 159 dst = self.g6.get_global_rib('10.0.0.0/24') 160 self.assertEqual(len(dst), 1) 161 self.assertEqual(len(dst[0]['paths']), 1) 162 path = dst[0]['paths'][0] 163 self.assertEqual([self.g5.asn, self.g4.asn, self.g1.asn], path['aspath']) 164 165 dst = self.g7.get_global_rib('10.0.0.0/24') 166 self.assertEqual(len(dst), 1) 167 self.assertEqual(len(dst[0]['paths']), 1) 168 path = dst[0]['paths'][0] 169 self.assertEqual([self.g5.asn, self.g4.asn, self.g3.asn], path['aspath']) 170 171 172 if __name__ == '__main__': 173 output = local("which docker 2>&1 > /dev/null ; echo $?", capture=True) 174 if int(output) != 0: 175 print("docker not found") 176 sys.exit(1) 177 178 nose.main(argv=sys.argv, addplugins=[OptionParser()], 179 defaultTest=sys.argv[0])