github.com/osrg/gobgp/v3@v3.30.0/test/scenario_test/route_server_test2.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  from lib.exabgp import ExaBGPContainer
    32  
    33  
    34  class GoBGPTestBase(unittest.TestCase):
    35  
    36      wait_per_retry = 5
    37      retry_limit = 15
    38  
    39      @classmethod
    40      def setUpClass(cls):
    41          gobgp_ctn_image_name = parser_option.gobgp_image
    42          base.TEST_PREFIX = parser_option.test_prefix
    43  
    44          g1 = GoBGPContainer(name='g1', asn=65000, router_id='192.168.0.1',
    45                              ctn_image_name=gobgp_ctn_image_name,
    46                              log_level=parser_option.gobgp_log_level)
    47          g2 = GoBGPContainer(name='g2', asn=65001, router_id='192.168.0.2',
    48                              ctn_image_name=gobgp_ctn_image_name)
    49          e1 = ExaBGPContainer(name='e1', asn=65002, router_id='192.168.0.3')
    50  
    51          ctns = [g1, g2, e1]
    52          cls.clients = {cli.name: cli for cli in (g2, e1)}
    53  
    54          initial_wait_time = max(ctn.run() for ctn in ctns)
    55          time.sleep(initial_wait_time)
    56  
    57          for cli in list(cls.clients.values()):
    58              # Omit "passwd" to avoid a issue on ExaBGP version 4.0.5:
    59              # https://github.com/Exa-Networks/exabgp/issues/766
    60              g1.add_peer(cli, is_rs_client=True, passive=True, prefix_limit=10)
    61              cli.add_peer(g1)
    62  
    63          # advertise a route from route-server-clients
    64          g2.add_route('10.0.0.0/24')
    65          e1.add_route('10.0.1.0/24')
    66  
    67          cls.gobgp = g1
    68  
    69      # test each neighbor state is turned establish
    70      def test_01_neighbor_established(self):
    71          for cli in list(self.clients.values()):
    72              self.gobgp.wait_for(expected_state=BGP_FSM_ESTABLISHED, peer=cli)
    73  
    74      def test_02_add_neighbor(self):
    75          e2 = ExaBGPContainer(name='e2', asn=65001, router_id='192.168.0.4')
    76          time.sleep(e2.run())
    77          self.gobgp.add_peer(e2, is_rs_client=True)
    78          e2.add_peer(self.gobgp)
    79  
    80          self.gobgp.wait_for(expected_state=BGP_FSM_ESTABLISHED, peer=e2)
    81          self.clients[e2.name] = e2
    82  
    83      def test_03_check_neighbor_rib(self):
    84          rib = self.gobgp.get_local_rib(self.clients['e2'])
    85          self.assertEqual(len(rib), 1)
    86          self.assertEqual(len(rib[0]['paths']), 1)
    87          path = rib[0]['paths'][0]
    88          self.assertTrue(65001 not in path['aspath'])
    89  
    90      def test_04_withdraw_path(self):
    91          self.clients['g2'].local('gobgp global rib del 10.0.0.0/24')
    92          time.sleep(1)
    93          afisafis = self.gobgp.get_neighbor(self.clients['g2'])['afi_safis']
    94          advertised = 0
    95          for afisafi in afisafis:
    96              s = afisafi.get('state')
    97              advertised += s.get('advertised')
    98              self.assertEqual(s.get('accepted'), None)  # means info['accepted'] == 0
    99              self.assertEqual(s.get('received'), None)  # means info['received'] == 0
   100  
   101          self.assertEqual(advertised, 1)
   102  
   103  
   104  if __name__ == '__main__':
   105      output = local("which docker 2>&1 > /dev/null ; echo $?", capture=True)
   106      if int(output) != 0:
   107          print("docker not found")
   108          sys.exit(1)
   109  
   110      nose.main(argv=sys.argv, addplugins=[OptionParser()],
   111                defaultTest=sys.argv[0])