github.com/osrg/gobgp@v2.0.0+incompatible/test/scenario_test/route_server_as2_test.py (about) 1 # Copyright (C) 2015 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 from __future__ import absolute_import 17 18 import unittest 19 import sys 20 import time 21 22 from fabric.api import local 23 import nose 24 25 from lib.noseplugin import OptionParser, parser_option 26 27 from lib import base 28 from lib.base import ( 29 BGP_FSM_IDLE, 30 BGP_FSM_ESTABLISHED, 31 ) 32 from lib.gobgp import GoBGPContainer 33 from lib.exabgp import ExaBGPContainer 34 35 36 class GoBGPTestBase(unittest.TestCase): 37 38 wait_per_retry = 5 39 retry_limit = 10 40 41 @classmethod 42 def setUpClass(cls): 43 gobgp_ctn_image_name = parser_option.gobgp_image 44 base.TEST_PREFIX = parser_option.test_prefix 45 46 g1 = GoBGPContainer(name='g1', asn=65000, router_id='192.168.0.1', 47 ctn_image_name=gobgp_ctn_image_name, 48 log_level=parser_option.gobgp_log_level) 49 50 rs_clients = [ 51 ExaBGPContainer(name='q{0}'.format(i + 1), asn=(65001 + i), 52 router_id='192.168.0.{0}'.format(i + 2)) 53 for i in range(4)] 54 ctns = [g1] + rs_clients 55 56 initial_wait_time = max(ctn.run() for ctn in ctns) 57 time.sleep(initial_wait_time) 58 59 for i, rs_client in enumerate(rs_clients): 60 g1.add_peer(rs_client, is_rs_client=True) 61 as2 = False 62 if i > 1: 63 as2 = True 64 rs_client.add_peer(g1, as2=as2) 65 66 # advertise a route from route-server-clients 67 for idx, rs_client in enumerate(rs_clients): 68 route = '10.0.{0}.0/24'.format(idx + 1) 69 rs_client.add_route(route) 70 if idx < 2: 71 route = '10.0.10.0/24' 72 rs_client.add_route(route) 73 74 cls.gobgp = g1 75 cls.quaggas = {x.name: x for x in rs_clients} 76 77 # test each neighbor state is turned establish 78 def test_01_neighbor_established(self): 79 for q in self.quaggas.itervalues(): 80 self.gobgp.wait_for(expected_state=BGP_FSM_ESTABLISHED, peer=q) 81 82 def test_02_check_gobgp_local_rib(self): 83 for rs_client in self.quaggas.itervalues(): 84 done = False 85 for _ in range(self.retry_limit): 86 if done: 87 break 88 89 state = self.gobgp.get_neighbor_state(rs_client) 90 self.assertEqual(state, BGP_FSM_ESTABLISHED) 91 local_rib = self.gobgp.get_local_rib(rs_client) 92 local_rib = [p['prefix'] for p in local_rib] 93 if len(local_rib) < (len(self.quaggas) - 1): 94 time.sleep(self.wait_per_retry) 95 continue 96 97 self.assertEqual(len(local_rib), 4) 98 done = True 99 100 if done: 101 continue 102 # should not reach here 103 raise AssertionError 104 105 def test_03_stop_q2_and_check_neighbor_status(self): 106 q2 = self.quaggas['q2'] 107 q2.remove() 108 self.gobgp.wait_for(expected_state=BGP_FSM_IDLE, peer=q2) 109 110 111 if __name__ == '__main__': 112 output = local("which docker 2>&1 > /dev/null ; echo $?", capture=True) 113 if int(output) is not 0: 114 print "docker not found" 115 sys.exit(1) 116 117 nose.main(argv=sys.argv, addplugins=[OptionParser()], 118 defaultTest=sys.argv[0])