github.com/osrg/gobgp/v3@v3.30.0/test/scenario_test/route_server_softreset_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      wait_per_retry = 5
    36      retry_limit = 15
    37  
    38      @classmethod
    39      def setUpClass(cls):
    40          gobgp_ctn_image_name = parser_option.gobgp_image
    41          base.TEST_PREFIX = parser_option.test_prefix
    42  
    43          g1 = GoBGPContainer(name='g1', asn=65000, router_id='192.168.0.1',
    44                              ctn_image_name=gobgp_ctn_image_name,
    45                              log_level=parser_option.gobgp_log_level)
    46          g2 = GoBGPContainer(name='g2', asn=65001, router_id='192.168.0.2',
    47                              ctn_image_name=gobgp_ctn_image_name,
    48                              log_level=parser_option.gobgp_log_level)
    49          g3 = GoBGPContainer(name='g3', asn=65002, router_id='192.168.0.3',
    50                              ctn_image_name=gobgp_ctn_image_name,
    51                              log_level=parser_option.gobgp_log_level)
    52          g4 = GoBGPContainer(name='g4', asn=65003, router_id='192.168.0.4',
    53                              ctn_image_name=gobgp_ctn_image_name,
    54                              log_level=parser_option.gobgp_log_level)
    55          ctns = [g1, g2, g3, g4]
    56  
    57          # advertise a route from route-server-clients
    58          cls.clients = {}
    59          for cli in [g2, g3, g4]:
    60              cls.clients[cli.name] = cli
    61          initial_wait_time = max(ctn.run() for ctn in ctns)
    62  
    63          time.sleep(initial_wait_time)
    64  
    65          for cli in cls.clients.values():
    66              g1.add_peer(cli, is_rs_client=True, passwd='passwd', passive=True, prefix_limit=10)
    67              cli.add_peer(g1, passwd='passwd')
    68  
    69          cls.gobgp = g1
    70  
    71      # test each neighbor state is turned establish
    72      def test_01_neighbor_established(self):
    73          for cli in self.clients.values():
    74              self.gobgp.wait_for(expected_state=BGP_FSM_ESTABLISHED, peer=cli)
    75  
    76      def test_02_softresetin_test1(self):
    77          g1 = self.gobgp
    78          g2 = self.clients['g2']
    79          g3 = self.clients['g3']
    80  
    81          p1 = {'ip-prefix': '10.0.10.0/24'}
    82          p2 = {'ip-prefix': '10.0.20.0/24'}
    83  
    84          ps0 = {'prefix-set-name': 'ps0', 'prefix-list': [p1, p2]}
    85          g1.set_prefix_set(ps0)
    86  
    87          st0 = {'conditions': {'match-prefix-set': {'prefix-set': 'ps0'}},
    88                 'actions': {'route-disposition': 'accept-route'}}
    89  
    90          pol0 = {'name': 'pol0', 'statements': [st0]}
    91  
    92          _filename = g1.add_policy(pol0, g3, 'import', 'reject')
    93  
    94          g3.add_route('10.0.10.0/24')
    95          g3.add_route('10.0.20.0/24')
    96  
    97          time.sleep(1)
    98  
    99          num = g2.get_neighbor(g1)['state']['messages']['received'].get('update', 0)
   100          num = g2.get_neighbor(g1)['state']['messages']['received'].get('update', 0)
   101  
   102          ps0 = {'prefix-set-name': 'ps0', 'prefix-list': [p1]}
   103          g1.set_prefix_set(ps0)
   104          g1.create_config()
   105          # this will cause g1 to do softresetin for all neighbors (policy is changed)
   106          g1.reload_config()
   107  
   108          time.sleep(1)
   109  
   110          num2 = g2.get_neighbor(g1)['state']['messages']['received'].get('update', 0)
   111          self.assertEqual((num + 1), num2)
   112  
   113          g3.softreset(g1, type='out')
   114  
   115          time.sleep(1)
   116  
   117          num3 = g2.get_neighbor(g1)['state']['messages']['received'].get('update', 0)
   118          self.assertEqual(num2, num3)
   119  
   120      def test_03_softresetin_test2(self):
   121          g1 = self.gobgp
   122          g2 = self.clients['g2']
   123  
   124          g2.add_route('10.0.10.0/24')
   125          time.sleep(1)
   126  
   127          num = g2.get_neighbor(g1)['state']['messages']['received'].get('update', 0)
   128          time.sleep(3)
   129  
   130          g1.local('gobgp n all softresetin')
   131          time.sleep(3)
   132  
   133          num1 = g2.get_neighbor(g1)['state']['messages']['received'].get('update', 0)
   134  
   135          self.assertEqual(num, num1)
   136  
   137  
   138  if __name__ == '__main__':
   139      output = local("which docker 2>&1 > /dev/null ; echo $?", capture=True)
   140      if int(output) != 0:
   141          print("docker not found")
   142          sys.exit(1)
   143  
   144      nose.main(argv=sys.argv, addplugins=[OptionParser()],
   145                defaultTest=sys.argv[0])