From 61196140a6887fde8d60c033a39ec6c60b9e7dd7 Mon Sep 17 00:00:00 2001 From: Mark Stapp Date: Fri, 25 Sep 2020 10:29:54 -0400 Subject: [PATCH] tests: improve unicode handling Support more of the string/unicode interactions in various topotests. Signed-off-by: Mark Stapp --- tests/topotests/lib/bgp.py | 13 +++++++------ tests/topotests/lib/common_config.py | 18 +++++++++--------- tests/topotests/lib/ospf.py | 4 ++-- tests/topotests/lib/topotest.py | 7 +++++++ 4 files changed, 25 insertions(+), 17 deletions(-) diff --git a/tests/topotests/lib/bgp.py b/tests/topotests/lib/bgp.py index a3d846edbb..72b99eeba8 100644 --- a/tests/topotests/lib/bgp.py +++ b/tests/topotests/lib/bgp.py @@ -28,6 +28,7 @@ from lib import topotest from lib.topolog import logger from lib.topogen import TopoRouter, get_topogen +from lib.topotest import frr_unicode # Import common_config to use commomnly used APIs from lib.common_config import ( @@ -393,7 +394,7 @@ def __create_bgp_unicast_neighbor( # Generating IPs for verification network_list = generate_ips(network, no_of_network) for ip in network_list: - ip = str(ipaddress.ip_network(unicode(ip))) + ip = str(ipaddress.ip_network(frr_unicode(ip))) cmd = "network {}".format(ip) if del_action: @@ -1037,7 +1038,7 @@ def verify_router_id(tgen, topo, input_dict): logger.info("Checking router %s router-id", router) show_bgp_json = run_frr_cmd(rnode, "show bgp summary json", isjson=True) router_id_out = show_bgp_json["ipv4Unicast"]["routerId"] - router_id_out = ipaddress.IPv4Address(unicode(router_id_out)) + router_id_out = ipaddress.IPv4Address(frr_unicode(router_id_out)) # Once router-id is deleted, highest interface ip should become # router-id @@ -1045,7 +1046,7 @@ def verify_router_id(tgen, topo, input_dict): router_id = find_interface_with_greater_ip(topo, router) else: router_id = input_dict[router]["bgp"]["router_id"] - router_id = ipaddress.IPv4Address(unicode(router_id)) + router_id = ipaddress.IPv4Address(frr_unicode(router_id)) if router_id == router_id_out: logger.info("Found expected router-id %s for router %s", router_id, router) @@ -2286,7 +2287,7 @@ def verify_best_path_as_per_bgp_attribute( routes = generate_ips(_network, no_of_ip) for route in routes: - route = str(ipaddress.ip_network(unicode(route))) + route = str(ipaddress.ip_network(frr_unicode(route))) if route in sh_ip_bgp_json["routes"]: route_attributes = sh_ip_bgp_json["routes"][route] @@ -2604,7 +2605,7 @@ def verify_bgp_rib(tgen, addr_type, dut, input_dict, next_hop=None, aspath=None) ip_list = generate_ips(network, no_of_ip) for st_rt in ip_list: - st_rt = str(ipaddress.ip_network(unicode(st_rt))) + st_rt = str(ipaddress.ip_network(frr_unicode(st_rt))) _addr_type = validate_ip_address(st_rt) if _addr_type != addr_type: @@ -2742,7 +2743,7 @@ def verify_bgp_rib(tgen, addr_type, dut, input_dict, next_hop=None, aspath=None) ip_list = generate_ips(network, no_of_network) for st_rt in ip_list: - st_rt = str(ipaddress.ip_network(unicode(st_rt))) + st_rt = str(ipaddress.ip_network(frr_unicode(st_rt))) _addr_type = validate_ip_address(st_rt) if _addr_type != addr_type: diff --git a/tests/topotests/lib/common_config.py b/tests/topotests/lib/common_config.py index c6944c3568..4d46b72199 100644 --- a/tests/topotests/lib/common_config.py +++ b/tests/topotests/lib/common_config.py @@ -46,7 +46,7 @@ else: from lib.topolog import logger, logger_config from lib.topogen import TopoRouter, get_topogen -from lib.topotest import interface_set_status, version_cmp +from lib.topotest import interface_set_status, version_cmp, frr_unicode FRRCFG_FILE = "frr_json.conf" FRRCFG_BKUP_FILE = "frr_json_initial.conf" @@ -1150,10 +1150,10 @@ def generate_ips(network, no_of_ips): addr_type = validate_ip_address(start_ip) if addr_type == "ipv4": - start_ip = ipaddress.IPv4Address(unicode(start_ip)) + start_ip = ipaddress.IPv4Address(frr_unicode(start_ip)) step = 2 ** (32 - mask) if addr_type == "ipv6": - start_ip = ipaddress.IPv6Address(unicode(start_ip)) + start_ip = ipaddress.IPv6Address(frr_unicode(start_ip)) step = 2 ** (128 - mask) next_ip = start_ip @@ -2665,7 +2665,7 @@ def verify_rib( nh_found = False for st_rt in ip_list: - st_rt = str(ipaddress.ip_network(unicode(st_rt))) + st_rt = str(ipaddress.ip_network(frr_unicode(st_rt))) _addr_type = validate_ip_address(st_rt) if _addr_type != addr_type: @@ -2861,7 +2861,7 @@ def verify_rib( nh_found = False for st_rt in ip_list: - st_rt = str(ipaddress.ip_network(unicode(st_rt))) + st_rt = str(ipaddress.ip_network(frr_unicode(st_rt))) _addr_type = validate_ip_address(st_rt) if _addr_type != addr_type: @@ -3010,7 +3010,7 @@ def verify_fib_routes(tgen, addr_type, dut, input_dict, next_hop=None): nh_found = False for st_rt in ip_list: - st_rt = str(ipaddress.ip_network(unicode(st_rt))) + st_rt = str(ipaddress.ip_network(frr_unicode(st_rt))) #st_rt = str(ipaddr.IPNetwork(unicode(st_rt))) _addr_type = validate_ip_address(st_rt) @@ -3117,7 +3117,7 @@ def verify_fib_routes(tgen, addr_type, dut, input_dict, next_hop=None): for st_rt in ip_list: #st_rt = str(ipaddr.IPNetwork(unicode(st_rt))) - st_rt = str(ipaddress.ip_network(unicode(st_rt))) + st_rt = str(ipaddress.ip_network(frr_unicode(st_rt))) _addr_type = validate_ip_address(st_rt) if _addr_type != addr_type: @@ -3266,7 +3266,7 @@ def verify_fib_routes(tgen, addr_type, dut, input_dict, next_hop=None): nh_found = False for st_rt in ip_list: - st_rt = str(ipaddress.ip_network(unicode(st_rt))) + st_rt = str(ipaddress.ip_network(frr_unicode(st_rt))) _addr_type = validate_ip_address(st_rt) if _addr_type != addr_type: @@ -3371,7 +3371,7 @@ def verify_fib_routes(tgen, addr_type, dut, input_dict, next_hop=None): nh_found = False for st_rt in ip_list: - st_rt = str(ipaddress.ip_network(unicode(st_rt))) + st_rt = str(ipaddress.ip_network(frr_unicode(st_rt))) _addr_type = validate_ip_address(st_rt) if _addr_type != addr_type: diff --git a/tests/topotests/lib/ospf.py b/tests/topotests/lib/ospf.py index 3596525c5e..9d6b8fa691 100644 --- a/tests/topotests/lib/ospf.py +++ b/tests/topotests/lib/ospf.py @@ -23,7 +23,7 @@ import traceback from time import sleep from lib.topolog import logger import ipaddr - +from lib.topotest import frr_unicode # Import common_config to use commomnly used APIs from lib.common_config import (create_common_configuration, @@ -739,7 +739,7 @@ def verify_ospf_rib(tgen, dut, input_dict, next_hop=None, nh_found = False for st_rt in ip_list: - st_rt = str(ipaddr.IPNetwork(unicode(st_rt))) + st_rt = str(ipaddr.IPNetwork(frr_unicode(st_rt))) _addr_type = validate_ip_address(st_rt) if _addr_type != 'ipv4': diff --git a/tests/topotests/lib/topotest.py b/tests/topotests/lib/topotest.py index ba6d38ad42..5aa9c5dcb7 100644 --- a/tests/topotests/lib/topotest.py +++ b/tests/topotests/lib/topotest.py @@ -1735,3 +1735,10 @@ class LegacySwitch(OVSSwitch): def __init__(self, name, **params): OVSSwitch.__init__(self, name, failMode="standalone", **params) self.switchIP = None + +def frr_unicode(s): + '''Convert string to unicode, depending on python version''' + if sys.version_info[0] > 2: + return s + else: + return unicode(s) -- 2.39.5