github.com/osrg/gobgp/v3@v3.30.0/test/scenario_test/aspath_test.py (about) 1 # Copyright (C) 2017 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 ( 30 BGP_FSM_ESTABLISHED, 31 assert_several_times, 32 local, 33 ) 34 from lib.gobgp import GoBGPContainer 35 from lib.quagga import QuaggaBGPContainer 36 37 38 class GoBGPTestBase(unittest.TestCase): 39 40 @classmethod 41 def setUpClass(cls): 42 gobgp_ctn_image_name = parser_option.gobgp_image 43 base.TEST_PREFIX = parser_option.test_prefix 44 45 g1 = GoBGPContainer(name='g1', asn=65001, router_id='192.168.0.1', 46 ctn_image_name=gobgp_ctn_image_name, 47 log_level=parser_option.gobgp_log_level) 48 q1 = QuaggaBGPContainer(name='q1', asn=65002, router_id='192.168.0.2') 49 g2 = GoBGPContainer(name='g2', asn=65001, router_id='192.168.0.3', 50 ctn_image_name=gobgp_ctn_image_name, 51 log_level=parser_option.gobgp_log_level) 52 ctns = [g1, g2, q1] 53 54 initial_wait_time = max(ctn.run() for ctn in ctns) 55 56 time.sleep(initial_wait_time) 57 58 g1.add_peer(q1) 59 q1.add_peer(g1) 60 61 q1.add_peer(g2) 62 g2.add_peer(q1) 63 64 g1.add_route('10.0.0.0/24') 65 66 cls.g1 = g1 67 cls.g2 = g2 68 cls.q1 = q1 69 cls.ctns = {n.name: n for n in ctns} 70 71 # test each neighbor state is turned establish 72 def test_01_neighbor_established(self): 73 self.g1.wait_for(expected_state=BGP_FSM_ESTABLISHED, peer=self.q1) 74 self.q1.wait_for(expected_state=BGP_FSM_ESTABLISHED, peer=self.g2) 75 76 def test_02_check_reject_as_loop(self): 77 def f(): 78 r = self.g2.get_neighbor(self.q1) 79 self.assertTrue('afi_safis' in r) 80 received = 0 81 for afisafi in r['afi_safis']: 82 self.assertTrue('state' in afisafi) 83 s = afisafi.get('state') 84 self.assertTrue('received' in s) 85 received += s.get('received') 86 # hacky. 'accepted' is zero so the key was deleted due to 87 # omitempty tag in bgp_configs.go. 88 self.assertFalse(s.get('accepted'), None) 89 self.assertEqual(received, 1) 90 91 assert_several_times(f) 92 93 def test_03_update_peer(self): 94 self.g2.update_peer(self.q1, allow_as_in=10) 95 96 self.g2.wait_for(expected_state=BGP_FSM_ESTABLISHED, peer=self.q1) 97 98 def test_04_check_accept_as_loop(self): 99 def f(): 100 r = self.g2.get_neighbor(self.q1) 101 self.assertTrue('afi_safis' in r) 102 received = 0 103 accepted = 0 104 for afisafi in r['afi_safis']: 105 self.assertTrue('state' in afisafi) 106 s = afisafi.get('state') 107 self.assertTrue('received' in s) 108 received += s.get('received') 109 accepted += s.get('accepted') 110 self.assertEqual(received, 1) 111 self.assertEqual(accepted, 1) 112 113 assert_several_times(f) 114 115 def test_05_check_remove_private_as_peer_all(self): 116 g3 = GoBGPContainer(name='g3', asn=100, router_id='192.168.0.4', 117 ctn_image_name=parser_option.gobgp_image, 118 log_level=parser_option.gobgp_log_level) 119 g4 = GoBGPContainer(name='g4', asn=200, router_id='192.168.0.5', 120 ctn_image_name=parser_option.gobgp_image, 121 log_level=parser_option.gobgp_log_level) 122 time.sleep(max(ctn.run() for ctn in [g3, g4])) 123 124 self.ctns['g3'] = g3 125 self.ctns['g4'] = g4 126 127 self.g2.add_peer(g3) 128 g3.add_peer(self.g2) 129 130 g3.add_peer(g4, remove_private_as='all') 131 g4.add_peer(g3) 132 133 self.g2.wait_for(expected_state=BGP_FSM_ESTABLISHED, peer=g3) 134 g3.wait_for(expected_state=BGP_FSM_ESTABLISHED, peer=g4) 135 136 def f(): 137 rib = g4.get_global_rib() 138 self.assertEqual(len(rib), 1) 139 self.assertEqual(len(rib[0]['paths']), 1) 140 self.assertEqual(rib[0]['paths'][0]['aspath'], [100]) 141 142 assert_several_times(f) 143 144 def test_06_check_remove_private_as_peer_replace(self): 145 g3 = self.ctns['g3'] 146 g4 = self.ctns['g4'] 147 g3.update_peer(g4, remove_private_as='replace') 148 149 g3.wait_for(expected_state=BGP_FSM_ESTABLISHED, peer=g4) 150 151 def f(): 152 rib = g4.get_global_rib() 153 self.assertEqual(rib[0]['paths'][0]['aspath'], [100, 100, 100, 100]) 154 155 assert_several_times(f) 156 157 def test_07_check_replace_peer_as(self): 158 g5 = GoBGPContainer(name='g5', asn=100, router_id='192.168.0.6', 159 ctn_image_name=parser_option.gobgp_image, 160 log_level=parser_option.gobgp_log_level) 161 time.sleep(g5.run()) 162 163 g4 = self.ctns['g4'] 164 g4.add_peer(g5, replace_peer_as=True) 165 g5.add_peer(g4) 166 167 g4.wait_for(expected_state=BGP_FSM_ESTABLISHED, peer=g5) 168 169 def f(): 170 rib = g5.get_global_rib() 171 self.assertEqual(rib[0]['paths'][0]['aspath'], [200, 200, 200, 200, 200]) 172 173 assert_several_times(f) 174 175 176 if __name__ == '__main__': 177 output = local("which docker 2>&1 > /dev/null ; echo $?", capture=True) 178 if int(output) != 0: 179 print("docker not found") 180 sys.exit(1) 181 182 nose.main(argv=sys.argv, addplugins=[OptionParser()], 183 defaultTest=sys.argv[0])