diff options
author | IWASE Yusuke <iwase.yusuke0@gmail.com> | 2017-03-07 16:26:46 +0900 |
---|---|---|
committer | IWASE Yusuke <iwase.yusuke0@gmail.com> | 2017-03-16 16:14:20 +0900 |
commit | 03ba90ac0dd6ecea6c1e2b24956bcc73962d1ef7 (patch) | |
tree | d01d11e8808c17ed8e9b7437bbfede03845d82ea /test/lib/gobgp.py | |
parent | cd26f8c729b326623ad72eac5d136c03e01dfbd0 (diff) |
test/lib: pep8 and pylint improvements
Signed-off-by: IWASE Yusuke <iwase.yusuke0@gmail.com>
Diffstat (limited to 'test/lib/gobgp.py')
-rw-r--r-- | test/lib/gobgp.py | 122 |
1 files changed, 76 insertions, 46 deletions
diff --git a/test/lib/gobgp.py b/test/lib/gobgp.py index 8c0d9cdd..468bd0ae 100644 --- a/test/lib/gobgp.py +++ b/test/lib/gobgp.py @@ -13,22 +13,39 @@ # See the License for the specific language governing permissions and # limitations under the License. -from base import * +from __future__ import absolute_import + import json -import toml -import yaml from itertools import chain from threading import Thread -import socket import subprocess import os +from fabric import colors +from fabric.api import local +from fabric.utils import indent +import netaddr +import toml +import yaml + +from lib.base import ( + BGPContainer, + CmdBuffer, + BGP_ATTR_TYPE_AS_PATH, + BGP_ATTR_TYPE_NEXT_HOP, + BGP_ATTR_TYPE_MULTI_EXIT_DISC, + BGP_ATTR_TYPE_LOCAL_PREF, + BGP_ATTR_TYPE_MP_REACH_NLRI, +) + + def extract_path_attribute(path, typ): for a in path['attrs']: if a['type'] == typ: return a return None + class GoBGPContainer(BGPContainer): SHARED_VOLUME = '/root/shared_volume' @@ -45,6 +62,7 @@ class GoBGPContainer(BGPContainer): self.prefix_set = None self.neighbor_set = None self.bgp_set = None + self.statements = None self.default_policy = None self.zebra = zebra self.zapi_version = zapi_version @@ -81,26 +99,29 @@ class GoBGPContainer(BGPContainer): self._start_gobgp() return self.WAIT_FOR_BOOT - def _get_as_path(self, path): - asps = (p['as_paths'] for p in path['attrs'] if - p['type'] == BGP_ATTR_TYPE_AS_PATH and 'as_paths' in p - and p['as_paths'] != None) + @staticmethod + def _get_as_path(path): + asps = (p['as_paths'] for p in path['attrs'] + if p['type'] == BGP_ATTR_TYPE_AS_PATH and 'as_paths' in p and p['as_paths'] is not None) asps = chain.from_iterable(asps) asns = (asp['asns'] for asp in asps) return list(chain.from_iterable(asns)) - def _get_nexthop(self, path): + @staticmethod + def _get_nexthop(path): for p in path['attrs']: if p['type'] == BGP_ATTR_TYPE_NEXT_HOP or p['type'] == BGP_ATTR_TYPE_MP_REACH_NLRI: return p['nexthop'] - def _get_local_pref(self, path): + @staticmethod + def _get_local_pref(path): for p in path['attrs']: if p['type'] == BGP_ATTR_TYPE_LOCAL_PREF: return p['value'] return None - def _get_med(self, path): + @staticmethod + def _get_med(path): for p in path['attrs']: if p['type'] == BGP_ATTR_TYPE_MULTI_EXIT_DISC: return p['metric'] @@ -222,7 +243,7 @@ class GoBGPContainer(BGPContainer): self.statements = [] def set_prefix_set(self, ps): - if type(ps) is not list: + if not isinstance(ps, list): ps = [ps] self.prefix_set = ps @@ -232,7 +253,7 @@ class GoBGPContainer(BGPContainer): self.prefix_set.append(ps) def set_neighbor_set(self, ns): - if type(ns) is not list: + if not isinstance(ns, list): ns = [ns] self.neighbor_set = ns @@ -250,13 +271,20 @@ class GoBGPContainer(BGPContainer): self._create_config_zebra() def _create_config_bgp(self): - config = {'global': {'config': {'as': self.asn, 'router-id': self.router_id}, - 'route-selection-options':{ + config = { + 'global': { + 'config': { + 'as': self.asn, + 'router-id': self.router_id, + }, + 'route-selection-options': { 'config': { 'external-compare-router-id': True, }, }, - }} + }, + 'neighbors': [], + } if self.zebra and self.zapi_version == 2: config['global']['use-multiple-paths'] = {'config': {'enabled': True}} @@ -265,9 +293,9 @@ class GoBGPContainer(BGPContainer): afi_safi_list = [] version = netaddr.IPNetwork(info['neigh_addr']).version if version == 4: - afi_safi_list.append({'config':{'afi-safi-name': 'ipv4-unicast'}}) + afi_safi_list.append({'config': {'afi-safi-name': 'ipv4-unicast'}}) elif version == 6: - afi_safi_list.append({'config':{'afi-safi-name': 'ipv6-unicast'}}) + afi_safi_list.append({'config': {'afi-safi-name': 'ipv6-unicast'}}) else: Exception('invalid ip address version. {0}'.format(version)) @@ -283,18 +311,23 @@ class GoBGPContainer(BGPContainer): afi_safi_list.append({'config': {'afi-safi-name': 'ipv6-flowspec'}}) afi_safi_list.append({'config': {'afi-safi-name': 'l3vpn-ipv6-flowspec'}}) - n = {'config': - {'neighbor-address': info['neigh_addr'].split('/')[0], - 'peer-as': peer.asn, - 'auth-password': info['passwd'], - 'vrf': info['vrf'], - }, - 'afi-safis': afi_safi_list, - 'timers': {'config': { - 'connect-retry': 10, - }}, - 'transport': {'config': {}}, - } + n = { + 'config': { + 'neighbor-address': info['neigh_addr'].split('/')[0], + 'peer-as': peer.asn, + 'auth-password': info['passwd'], + 'vrf': info['vrf'], + }, + 'afi-safis': afi_safi_list, + 'timers': { + 'config': { + 'connect-retry': 10, + }, + }, + 'transport': { + 'config': {}, + }, + } if ':' in info['local_addr']: n['transport']['config']['local-address'] = info['local_addr'].split('/')[0] @@ -310,7 +343,7 @@ class GoBGPContainer(BGPContainer): if info['prefix_limit']: for v in afi_safi_list: - v['prefix-limit'] = {'config': {'max-prefixes': info['prefix_limit'], 'shutdown-threshold-pct': 80 }} + v['prefix-limit'] = {'config': {'max-prefixes': info['prefix_limit'], 'shutdown-threshold-pct': 80}} if info['graceful_restart'] is not None: n['graceful-restart'] = {'config': {'enabled': True, 'restart-time': 20}} @@ -324,11 +357,11 @@ class GoBGPContainer(BGPContainer): afi_safi['long-lived-graceful-restart'] = {'config': {'enabled': True, 'restart-time': 30}} if info['is_rr_client']: - clusterId = self.router_id + cluster_id = self.router_id if 'cluster_id' in info and info['cluster_id'] is not None: - clusterId = info['cluster_id'] - n['route-reflector'] = {'config' : {'route-reflector-client': True, - 'route-reflector-cluster-id': clusterId}} + cluster_id = info['cluster_id'] + n['route-reflector'] = {'config': {'route-reflector-client': True, + 'route-reflector-cluster-id': cluster_id}} if len(info.get('default-policy', [])) + len(info.get('policies', [])) > 0: n['apply-policy'] = {'config': {}} @@ -336,7 +369,7 @@ class GoBGPContainer(BGPContainer): for typ, p in info.get('policies', {}).iteritems(): n['apply-policy']['config']['{0}-policy-list'.format(typ)] = [p['name']] - def f(v): + def _f(v): if v == 'reject': return 'reject-route' elif v == 'accept': @@ -344,10 +377,7 @@ class GoBGPContainer(BGPContainer): raise Exception('invalid default policy type {0}'.format(v)) for typ, d in info.get('default-policy', {}).iteritems(): - n['apply-policy']['config']['default-{0}-policy'.format(typ)] = f(d) - - if 'neighbors' not in config: - config['neighbors'] = [] + n['apply-policy']['config']['default-{0}-policy'.format(typ)] = _f(d) config['neighbors'].append(n) @@ -372,9 +402,9 @@ class GoBGPContainer(BGPContainer): config['policy-definitions'] = policy_list if self.zebra: - config['zebra'] = {'config':{'enabled': True, - 'redistribute-route-type-list':['connect'], - 'version': self.zapi_version}} + config['zebra'] = {'config': {'enabled': True, + 'redistribute-route-type-list': ['connect'], + 'version': self.zapi_version}} with open('{0}/gobgpd.conf'.format(self.config_dir), 'w') as f: print colors.yellow('[{0}\'s new config]'.format(self.name)) @@ -405,8 +435,7 @@ class GoBGPContainer(BGPContainer): f.writelines(str(c)) def reload_config(self): - daemon = [] - daemon.append('gobgpd') + daemon = ['gobgpd'] if self.zebra: daemon.append('zebra') for d in daemon: @@ -428,7 +457,7 @@ class GoBGPContainer(BGPContainer): cmd = 'gobgp global '\ 'rib add match {0} then {1} -a {2}'.format(' '.join(v['matchs']), ' '.join(v['thens']), v['rf']) else: - raise Exception('unsupported route faily: {0}'.format(rf)) + raise Exception('unsupported route faily: {0}'.format(v['rf'])) self.local(cmd) @@ -449,6 +478,7 @@ class RawGoBGPContainer(GoBGPContainer): super(RawGoBGPContainer, self).__init__(name, asn, router_id, ctn_image_name, log_level, zebra, config_format) + def create_config(self): with open('{0}/gobgpd.conf'.format(self.config_dir), 'w') as f: print colors.yellow('[{0}\'s new config]'.format(self.name)) |