github.com/osrg/gobgp@v2.0.0+incompatible/test/scenario_test/evpn_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 from itertools import combinations 19 import sys 20 import time 21 import unittest 22 23 from fabric.api import local 24 import nose 25 26 from lib.noseplugin import OptionParser, parser_option 27 28 from lib import base 29 from lib.base import ( 30 BGP_FSM_ESTABLISHED, 31 BGP_ATTR_TYPE_EXTENDED_COMMUNITIES, 32 ) 33 from lib.gobgp import GoBGPContainer 34 35 36 def get_mac_mobility_sequence(pattr): 37 for ecs in [ 38 p['value'] for p in pattr 39 if 'type' in p and p['type'] == BGP_ATTR_TYPE_EXTENDED_COMMUNITIES]: 40 for ec in [e for e in ecs if 'type' in e and e['type'] == 6]: 41 if ec['subtype'] == 0: 42 if 'sequence' not in ec: 43 return 0 44 else: 45 return ec['sequence'] 46 return -1 47 48 49 class GoBGPTestBase(unittest.TestCase): 50 51 @classmethod 52 def setUpClass(cls): 53 gobgp_ctn_image_name = parser_option.gobgp_image 54 base.TEST_PREFIX = parser_option.test_prefix 55 56 g1 = GoBGPContainer(name='g1', asn=65000, router_id='192.168.0.1', 57 ctn_image_name=gobgp_ctn_image_name, 58 log_level=parser_option.gobgp_log_level) 59 g2 = GoBGPContainer(name='g2', asn=65000, router_id='192.168.0.2', 60 ctn_image_name=gobgp_ctn_image_name, 61 log_level=parser_option.gobgp_log_level) 62 ctns = [g1, g2] 63 64 initial_wait_time = max(ctn.run() for ctn in ctns) 65 66 time.sleep(initial_wait_time) 67 g1.local("gobgp vrf add vrf1 rd 10:10 rt both 10:10") 68 g2.local("gobgp vrf add vrf1 rd 10:10 rt both 10:10") 69 70 for a, b in combinations(ctns, 2): 71 a.add_peer(b, vpn=True, passwd='evpn') 72 b.add_peer(a, vpn=True, passwd='evpn') 73 74 cls.g1 = g1 75 cls.g2 = g2 76 77 # test each neighbor state is turned establish 78 def test_01_neighbor_established(self): 79 self.g1.wait_for(expected_state=BGP_FSM_ESTABLISHED, peer=self.g2) 80 81 def test_02_add_evpn_route(self): 82 self.g1.local('gobgp global rib add ' 83 '-a evpn macadv 11:22:33:44:55:66 10.0.0.1 esi AS 1 1 1 etag 1000 label 1000 ' 84 'rd 10:10 rt 10:10') 85 grib = self.g1.get_global_rib(rf='evpn') 86 self.assertEqual(len(grib), 1) 87 dst = grib[0] 88 self.assertEqual(len(dst['paths']), 1) 89 path = dst['paths'][0] 90 self.assertEqual(path['nexthop'], '0.0.0.0') 91 92 interval = 1 93 timeout = int(30 / interval) 94 done = False 95 for _ in range(timeout): 96 if done: 97 break 98 grib = self.g2.get_global_rib(rf='evpn') 99 100 if len(grib) < 1: 101 time.sleep(interval) 102 continue 103 104 self.assertEqual(len(grib), 1) 105 dst = grib[0] 106 self.assertEqual(len(dst['paths']), 1) 107 path = dst['paths'][0] 108 n_addrs = [i[1].split('/')[0] for i in self.g1.ip_addrs] 109 self.assertTrue(path['nexthop'] in n_addrs) 110 done = True 111 112 def test_03_check_mac_mobility(self): 113 self.g2.local('gobgp global rib add ' 114 '-a evpn macadv 11:22:33:44:55:66 10.0.0.1 esi AS 2 1 1 etag 1000 label 1000 ' 115 'rd 10:20 rt 10:10') 116 117 time.sleep(3) 118 119 grib = self.g1.get_global_rib(rf='evpn') 120 self.assertEqual(len(grib), 1) 121 dst = grib[0] 122 self.assertEqual(len(dst['paths']), 1) 123 path = dst['paths'][0] 124 n_addrs = [i[1].split('/')[0] for i in self.g2.ip_addrs] 125 self.assertTrue(path['nexthop'] in n_addrs) 126 self.assertEqual(get_mac_mobility_sequence(path['attrs']), 0) 127 128 def test_04_check_mac_mobility_again(self): 129 self.g1.local('gobgp global rib add ' 130 '-a evpn macadv 11:22:33:44:55:66 10.0.0.1 esi AS 3 1 1 etag 1000 label 1000 ' 131 'rd 10:20 rt 10:10') 132 133 time.sleep(3) 134 135 grib = self.g2.get_global_rib(rf='evpn') 136 self.assertEqual(len(grib), 1) 137 dst = grib[0] 138 self.assertEqual(len(dst['paths']), 1) 139 path = dst['paths'][0] 140 n_addrs = [i[1].split('/')[0] for i in self.g1.ip_addrs] 141 self.assertTrue(path['nexthop'] in n_addrs) 142 self.assertEqual(get_mac_mobility_sequence(path['attrs']), 1) 143 144 145 if __name__ == '__main__': 146 output = local("which docker 2>&1 > /dev/null ; echo $?", capture=True) 147 if int(output) is not 0: 148 print "docker not found" 149 sys.exit(1) 150 151 nose.main(argv=sys.argv, addplugins=[OptionParser()], 152 defaultTest=sys.argv[0])