github.com/osrg/gobgp/v3@v3.30.0/test/scenario_test/route_server_ipv4_v6_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  
    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.quagga import QuaggaBGPContainer
    32  
    33  
    34  class GoBGPIPv6Test(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=65002, router_id='192.168.0.2',
    45                              ctn_image_name=gobgp_ctn_image_name,
    46                              log_level=parser_option.gobgp_log_level)
    47          q1 = QuaggaBGPContainer(name='q1', asn=65003, router_id='192.168.0.3')
    48          q2 = QuaggaBGPContainer(name='q2', asn=65004, router_id='192.168.0.4')
    49          q3 = QuaggaBGPContainer(name='q3', asn=65005, router_id='192.168.0.5')
    50          q4 = QuaggaBGPContainer(name='q4', asn=65006, router_id='192.168.0.6')
    51  
    52          ctns = [g1, q1, q2, q3, q4]
    53          v4 = [q1, q2]
    54          v6 = [q3, q4]
    55  
    56          initial_wait_time = max(ctn.run() for ctn in ctns)
    57          time.sleep(initial_wait_time)
    58  
    59          for ctn in v4:
    60              g1.add_peer(ctn, is_rs_client=True)
    61              ctn.add_peer(g1)
    62  
    63          for ctn in v6:
    64              g1.add_peer(ctn, is_rs_client=True, v6=True)
    65              ctn.add_peer(g1, v6=True)
    66  
    67          for idx, q in enumerate(v4):
    68              route = '10.0.{0}.0/24'.format(idx + 1)
    69              q.add_route(route)
    70  
    71          for idx, q in enumerate(v6):
    72              route = '2001:{0}::/96'.format(idx + 1)
    73              q.add_route(route, rf='ipv6')
    74  
    75          cls.gobgp = g1
    76          cls.quaggas = {'q1': q1, 'q2': q2, 'q3': q3, 'q4': q4}
    77          cls.ipv4s = {'q1': q1, 'q2': q2}
    78          cls.ipv6s = {'q3': q3, 'q4': q4}
    79  
    80      def check_gobgp_local_rib(self, ctns, rf):
    81          for rs_client in ctns.values():
    82              done = False
    83              for _ in range(self.retry_limit):
    84                  if done:
    85                      break
    86  
    87                  state = self.gobgp.get_neighbor_state(rs_client)
    88                  self.assertEqual(state, BGP_FSM_ESTABLISHED)
    89                  local_rib = self.gobgp.get_local_rib(rs_client, rf=rf)
    90                  local_rib = [p["prefix"] for p in local_rib]
    91                  if len(local_rib) < (len(ctns) - 1):
    92                      time.sleep(self.wait_per_retry)
    93                      continue
    94  
    95                  self.assertEqual(len(local_rib), (len(ctns) - 1))
    96  
    97                  for c in ctns.values():
    98                      if rs_client != c:
    99                          for r in c.routes:
   100                              self.assertTrue(r in local_rib)
   101  
   102                  done = True
   103              if done:
   104                  continue
   105              # should not reach here
   106              raise AssertionError
   107  
   108      def check_rs_client_rib(self, ctns, rf):
   109          for rs_client in ctns.values():
   110              done = False
   111              for _ in range(self.retry_limit):
   112                  if done:
   113                      break
   114                  global_rib = rs_client.get_global_rib(rf=rf)
   115                  global_rib = [p['prefix'] for p in global_rib]
   116                  if len(global_rib) < len(ctns):
   117                      time.sleep(self.wait_per_retry)
   118                      continue
   119  
   120                  self.assertEqual(len(global_rib), len(ctns))
   121  
   122                  for c in ctns.values():
   123                      for r in c.routes:
   124                          self.assertTrue(r in global_rib)
   125  
   126                  done = True
   127              if done:
   128                  continue
   129              # should not reach here
   130              raise AssertionError
   131  
   132      # test each neighbor state is turned establish
   133      def test_01_neighbor_established(self):
   134          for q in self.quaggas.values():
   135              self.gobgp.wait_for(expected_state=BGP_FSM_ESTABLISHED, peer=q)
   136  
   137      def test_02_check_ipv4_peer_rib(self):
   138          self.check_gobgp_local_rib(self.ipv4s, 'ipv4')
   139          self.check_rs_client_rib(self.ipv4s, 'ipv4')
   140  
   141      def test_03_check_ipv6_peer_rib(self):
   142          self.check_gobgp_local_rib(self.ipv6s, 'ipv6')
   143          self.check_rs_client_rib(self.ipv6s, 'ipv6')
   144  
   145      def test_04_add_in_policy_to_reject_all(self):
   146          for q in self.gobgp.peers.values():
   147              self.gobgp.local('gobgp neighbor {0} policy import set default reject'.format(q['neigh_addr'].split('/')[0]))
   148  
   149      def test_05_check_ipv4_peer_rib(self):
   150          self.check_gobgp_local_rib(self.ipv4s, 'ipv4')
   151          self.check_rs_client_rib(self.ipv4s, 'ipv4')
   152  
   153      def test_06_check_ipv6_peer_rib(self):
   154          self.check_gobgp_local_rib(self.ipv6s, 'ipv6')
   155          self.check_rs_client_rib(self.ipv6s, 'ipv6')
   156  
   157      def test_07_add_in_policy_to_reject_all(self):
   158          self.gobgp.local('gobgp neighbor all softresetin')
   159          time.sleep(1)
   160  
   161      def test_08_check_rib(self):
   162          for q in self.ipv4s.values():
   163              self.assertEqual(len(self.gobgp.get_adj_rib_out(q)), 0)
   164              self.assertEqual(len(q.get_global_rib()), len(q.routes))
   165  
   166          for q in self.ipv6s.values():
   167              self.assertEqual(len(self.gobgp.get_adj_rib_out(q, rf='ipv6')), 0)
   168              self.assertEqual(len(q.get_global_rib(rf='ipv6')), len(q.routes))
   169  
   170  
   171  if __name__ == '__main__':
   172      output = local("which docker 2>&1 > /dev/null ; echo $?", capture=True)
   173      if int(output) != 0:
   174          print("docker not found")
   175          sys.exit(1)
   176  
   177      nose.main(argv=sys.argv, addplugins=[OptionParser()],
   178                defaultTest=sys.argv[0])