github.com/osrg/gobgp@v2.0.0+incompatible/test/scenario_test/vrf_neighbor_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  from __future__ import absolute_import
    17  
    18  import sys
    19  import time
    20  import unittest
    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 BGP_FSM_ESTABLISHED
    29  from lib.gobgp import GoBGPContainer
    30  
    31  
    32  class GoBGPTestBase(unittest.TestCase):
    33  
    34      @classmethod
    35      def setUpClass(cls):
    36          gobgp_ctn_image_name = parser_option.gobgp_image
    37          base.TEST_PREFIX = parser_option.test_prefix
    38  
    39          g1 = GoBGPContainer(name='g1', asn=65001, router_id='192.168.0.1',
    40                              ctn_image_name=gobgp_ctn_image_name,
    41                              log_level=parser_option.gobgp_log_level,
    42                              config_format='yaml')
    43          g2 = GoBGPContainer(name='g2', asn=65002, router_id='192.168.0.2',
    44                              ctn_image_name=gobgp_ctn_image_name,
    45                              log_level=parser_option.gobgp_log_level,
    46                              config_format='yaml')
    47          g3 = GoBGPContainer(name='g3', asn=65003, router_id='192.168.0.3',
    48                              ctn_image_name=gobgp_ctn_image_name,
    49                              log_level=parser_option.gobgp_log_level,
    50                              config_format='yaml')
    51          g4 = GoBGPContainer(name='g4', asn=65004, router_id='192.168.0.4',
    52                              ctn_image_name=gobgp_ctn_image_name,
    53                              log_level=parser_option.gobgp_log_level,
    54                              config_format='yaml')
    55          g5 = GoBGPContainer(name='g5', asn=65005, router_id='192.168.0.5',
    56                              ctn_image_name=gobgp_ctn_image_name,
    57                              log_level=parser_option.gobgp_log_level,
    58                              config_format='yaml')
    59          g6 = GoBGPContainer(name='g6', asn=65006, router_id='192.168.0.6',
    60                              ctn_image_name=gobgp_ctn_image_name,
    61                              log_level=parser_option.gobgp_log_level,
    62                              config_format='yaml')
    63          g7 = GoBGPContainer(name='g7', asn=65007, router_id='192.168.0.7',
    64                              ctn_image_name=gobgp_ctn_image_name,
    65                              log_level=parser_option.gobgp_log_level,
    66                              config_format='yaml')
    67  
    68          ctns = [g1, g2, g3, g4, g5, g6, g7]
    69  
    70          initial_wait_time = max(ctn.run() for ctn in ctns)
    71  
    72          time.sleep(initial_wait_time)
    73  
    74          g4.local("gobgp vrf add red rd 10:10 rt both 10:10")
    75          g4.local("gobgp vrf add blue rd 20:20 rt both 20:20")
    76  
    77          g5.local("gobgp vrf add red rd 10:10 rt both 10:10")
    78          g5.local("gobgp vrf add blue rd 20:20 rt both 20:20")
    79  
    80          g1.add_peer(g4)
    81          g4.add_peer(g1, vrf='red')
    82  
    83          g2.add_peer(g4)
    84          g4.add_peer(g2, vrf='red')
    85  
    86          g3.add_peer(g4)
    87          g4.add_peer(g3, vrf='blue')
    88  
    89          g4.add_peer(g5, vpn=True)
    90          g5.add_peer(g4, vpn=True)
    91  
    92          g5.add_peer(g6, vrf='red')
    93          g6.add_peer(g5)
    94  
    95          g5.add_peer(g7, vrf='blue')
    96          g7.add_peer(g5)
    97  
    98          cls.g1 = g1
    99          cls.g2 = g2
   100          cls.g3 = g3
   101          cls.g4 = g4
   102          cls.g5 = g5
   103          cls.g6 = g6
   104          cls.g7 = g7
   105  
   106      # test each neighbor state is turned establish
   107      def test_01_neighbor_established(self):
   108          self.g4.wait_for(expected_state=BGP_FSM_ESTABLISHED, peer=self.g1)
   109          self.g4.wait_for(expected_state=BGP_FSM_ESTABLISHED, peer=self.g2)
   110          self.g4.wait_for(expected_state=BGP_FSM_ESTABLISHED, peer=self.g3)
   111          self.g4.wait_for(expected_state=BGP_FSM_ESTABLISHED, peer=self.g5)
   112          self.g5.wait_for(expected_state=BGP_FSM_ESTABLISHED, peer=self.g6)
   113          self.g5.wait_for(expected_state=BGP_FSM_ESTABLISHED, peer=self.g7)
   114  
   115      def test_02_inject_from_vrf_red(self):
   116          self.g1.local('gobgp global rib add 10.0.0.0/24')
   117  
   118          time.sleep(1)
   119  
   120          dst = self.g2.get_global_rib('10.0.0.0/24')
   121          self.assertEqual(len(dst), 1)
   122          self.assertEqual(len(dst[0]['paths']), 1)
   123          path = dst[0]['paths'][0]
   124          self.assertEqual([self.g4.asn, self.g1.asn], path['aspath'])
   125  
   126          dst = self.g3.get_global_rib('10.0.0.0/24')
   127          self.assertEqual(len(dst), 0)
   128  
   129          dst = self.g4.get_global_rib(rf='vpnv4')
   130          self.assertEqual(len(dst), 1)
   131          self.assertEqual(len(dst[0]['paths']), 1)
   132          path = dst[0]['paths'][0]
   133          self.assertEqual([self.g1.asn], path['aspath'])
   134  
   135          dst = self.g6.get_global_rib('10.0.0.0/24')
   136          self.assertEqual(len(dst), 1)
   137          self.assertEqual(len(dst[0]['paths']), 1)
   138          path = dst[0]['paths'][0]
   139          self.assertEqual([self.g5.asn, self.g4.asn, self.g1.asn], path['aspath'])
   140  
   141          dst = self.g7.get_global_rib('10.0.0.0/24')
   142          self.assertEqual(len(dst), 0)
   143  
   144      def test_03_inject_from_vrf_blue(self):
   145          self.g3.local('gobgp global rib add 10.0.0.0/24')
   146  
   147          time.sleep(1)
   148  
   149          dst = self.g2.get_global_rib('10.0.0.0/24')
   150          self.assertEqual(len(dst), 1)
   151          self.assertEqual(len(dst[0]['paths']), 1)
   152          path = dst[0]['paths'][0]
   153          self.assertEqual([self.g4.asn, self.g1.asn], path['aspath'])
   154  
   155          dst = self.g4.get_global_rib(rf='vpnv4')
   156          self.assertEqual(len(dst), 2)
   157  
   158          dst = self.g6.get_global_rib('10.0.0.0/24')
   159          self.assertEqual(len(dst), 1)
   160          self.assertEqual(len(dst[0]['paths']), 1)
   161          path = dst[0]['paths'][0]
   162          self.assertEqual([self.g5.asn, self.g4.asn, self.g1.asn], path['aspath'])
   163  
   164          dst = self.g7.get_global_rib('10.0.0.0/24')
   165          self.assertEqual(len(dst), 1)
   166          self.assertEqual(len(dst[0]['paths']), 1)
   167          path = dst[0]['paths'][0]
   168          self.assertEqual([self.g5.asn, self.g4.asn, self.g3.asn], path['aspath'])
   169  
   170  
   171  if __name__ == '__main__':
   172      output = local("which docker 2>&1 > /dev/null ; echo $?", capture=True)
   173      if int(output) is not 0:
   174          print "docker not found"
   175          sys.exit(1)
   176  
   177      nose.main(argv=sys.argv, addplugins=[OptionParser()],
   178                defaultTest=sys.argv[0])