From d7ff1ad363da96819cff650643a25ec3649bc69e Mon Sep 17 00:00:00 2001 From: Guohan Lu Date: Sat, 27 Jun 2020 09:31:27 +0000 Subject: [PATCH] [vstest]: upgrade swss vs tests to python3 this pr mainly contains the changes made by 2to3 conversion tool --- tests/conftest.py | 56 +++++++++++++++++++----------- tests/dvslib/dvs_vlan.py | 2 +- tests/port_dpb.py | 4 +-- tests/test_mirror_ipv6_separate.py | 8 ++--- tests/test_nhg.py | 8 +++-- tests/test_port_mac_learn.py | 4 +-- tests/test_setro.py | 2 +- tests/test_tunnel.py | 2 +- tests/test_vrf.py | 10 +++--- tests/test_warm_reboot.py | 10 +++--- 10 files changed, 62 insertions(+), 44 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index fac32b321768..30ead8dd7087 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -6,10 +6,12 @@ import redis import docker import pytest -import commands import tarfile -import StringIO +import io import subprocess +import sys +if sys.version_info < (3, 0): + import commands from datetime import datetime from swsscommon import swsscommon @@ -19,7 +21,10 @@ from dvslib import dvs_lag def ensure_system(cmd): - (rc, output) = commands.getstatusoutput(cmd) + if sys.version_info < (3, 0): + (rc, output) = commands.getstatusoutput(cmd) + else: + (rc, output) = subprocess.getstatusoutput(cmd) if rc: raise RuntimeError('Failed to run command: %s. rc=%d. output: %s' % (cmd, rc, output)) @@ -136,9 +141,9 @@ def runcmd(self, cmd): try: out = subprocess.check_output("ip netns exec %s %s" % (self.nsname, cmd), stderr=subprocess.STDOUT, shell=True) except subprocess.CalledProcessError as e: - print "------rc={} for cmd: {}------".format(e.returncode, e.cmd) - print e.output.rstrip() - print "------" + print("------rc={} for cmd: {}------".format(e.returncode, e.cmd)) + print(e.output.rstrip()) + print("------") return e.returncode return 0 @@ -146,7 +151,7 @@ def runcmd_async(self, cmd): return subprocess.Popen("ip netns exec %s %s" % (self.nsname, cmd), shell=True) def runcmd_output(self, cmd): - return subprocess.check_output("ip netns exec %s %s" % (self.nsname, cmd), shell=True) + return subprocess.check_output("ip netns exec %s %s" % (self.nsname, cmd), shell=True).decode('utf-8') class DockerVirtualSwitch(object): APP_DB_ID = 0 @@ -187,7 +192,10 @@ def __init__(self, name=None, imgname=None, keeptb=False, fakeplatform=None): for ctn in self.client.containers.list(): if ctn.name == name: self.ctn = ctn - (status, output) = commands.getstatusoutput("docker inspect --format '{{.HostConfig.NetworkMode}}' %s" % name) + if sys.version_info < (3, 0): + (status, output) = commands.getstatusoutput("docker inspect --format '{{.HostConfig.NetworkMode}}' %s" % name) + else: + (status, output) = subprocess.getstatusoutput("docker inspect --format '{{.HostConfig.NetworkMode}}' %s" % name) ctn_sw_id = output.split(':')[1] self.cleanup = False if self.ctn == None: @@ -198,7 +206,10 @@ def __init__(self, name=None, imgname=None, keeptb=False, fakeplatform=None): if ctn.id == ctn_sw_id or ctn.name == ctn_sw_id: ctn_sw_name = ctn.name - (status, output) = commands.getstatusoutput("docker inspect --format '{{.State.Pid}}' %s" % ctn_sw_name) + if sys.version_info < (3, 0): + (status, output) = commands.getstatusoutput("docker inspect --format '{{.State.Pid}}' %s" % ctn_sw_name) + else: + (status, output) = subprocess.getstatusoutput("docker inspect --format '{{.State.Pid}}' %s" % ctn_sw_name) self.ctn_sw_pid = int(output) # create virtual servers @@ -214,7 +225,10 @@ def __init__(self, name=None, imgname=None, keeptb=False, fakeplatform=None): else: self.ctn_sw = self.client.containers.run('debian:jessie', privileged=True, detach=True, command="bash", stdin_open=True) - (status, output) = commands.getstatusoutput("docker inspect --format '{{.State.Pid}}' %s" % self.ctn_sw.name) + if sys.version_info < (3, 0): + (status, output) = commands.getstatusoutput("docker inspect --format '{{.State.Pid}}' %s" % self.ctn_sw.name) + else: + (status, output) = subprocess.getstatusoutput("docker inspect --format '{{.State.Pid}}' %s" % self.ctn_sw.name) self.ctn_sw_pid = int(output) # create virtual server @@ -284,7 +298,7 @@ def check_ready(self, timeout=30): # get process status res = self.ctn.exec_run("supervisorctl status") try: - out = res.output + out = res.output.decode('utf-8') except AttributeError: out = res for l in out.split('\n'): @@ -322,7 +336,7 @@ def net_cleanup(self): res = self.ctn.exec_run("ip link show") try: - out = res.output + out = res.output.decode('utf-8') except AttributeError: out = res for l in out.split('\n'): @@ -335,7 +349,7 @@ def net_cleanup(self): m = re.compile("(eth|lo|Bridge|Ethernet)").match(pname) if not m: self.ctn.exec_run("ip link del {}".format(pname)) - print "remove extra link {}".format(pname) + print("remove extra link {}".format(pname)) return def ctn_restart(self): @@ -389,19 +403,19 @@ def runcmd(self, cmd): res = self.ctn.exec_run(cmd) try: exitcode = res.exit_code - out = res.output + out = res.output.decode('utf-8') except AttributeError: exitcode = 0 out = res if exitcode != 0: - print "-----rc={} for cmd {}-----".format(exitcode, cmd) - print out.rstrip() - print "-----" + print("-----rc={} for cmd {}-----".format(exitcode, cmd)) + print(out.rstrip()) + print("-----") return (exitcode, out) def copy_file(self, path, filename): - tarstr = StringIO.StringIO() + tarstr = io.StringIO() tar = tarfile.open(fileobj=tarstr, mode="w") tar.add(filename, os.path.basename(filename)) tar.close() @@ -455,7 +469,7 @@ def CountSubscribedObjects(self, pubsub, ignore=None, timeout=10): while True and idle < timeout: message = pubsub.get_message() if message: - print message + print(message) if ignore: fds = message['channel'].split(':') if fds[2] in ignore: @@ -482,7 +496,7 @@ def GetSubscribedAppDbObjects(self, pubsub, ignore=None, timeout=10): while True and idle < timeout: message = pubsub.get_message() if message: - print message + print(message) key = message['channel'].split(':', 1)[1] # In producer/consumer_state_table scenarios, every entry will # show up twice for every push/pop operation, so skip the second @@ -524,7 +538,7 @@ def GetSubscribedAsicDbObjects(self, pubsub, ignore=None, timeout=10): while True and idle < timeout: message = pubsub.get_message() if message: - print message + print(message) key = message['channel'].split(':', 1)[1] if ignore: fds = message['channel'].split(':') diff --git a/tests/dvslib/dvs_vlan.py b/tests/dvslib/dvs_vlan.py index b22ac9cf1218..adca18b0e72b 100644 --- a/tests/dvslib/dvs_vlan.py +++ b/tests/dvslib/dvs_vlan.py @@ -1,4 +1,4 @@ -from dvs_database import DVSDatabase +from .dvs_database import DVSDatabase class DVSVlan(object): def __init__(self, adb, cdb, sdb, cntrdb, appdb): diff --git a/tests/port_dpb.py b/tests/port_dpb.py index 5a5217ca6077..87d19b755e1a 100644 --- a/tests/port_dpb.py +++ b/tests/port_dpb.py @@ -87,7 +87,7 @@ def get_oid(self): return self._oid def print_port(self): - print "Port: %s Lanes: %s Speed: %d, Index: %d"%(self._name, self._lanes, self._speed, self._index) + print("Port: %s Lanes: %s Speed: %d, Index: %d"%(self._name, self._lanes, self._speed, self._index)) def port_merge(self, child_ports): child_ports.sort(key=lambda x: x.get_port_num()) @@ -218,7 +218,7 @@ def verify_asic_db(self): (status, fvs) = self._asic_db_ptbl.get(self.get_oid()) assert(status == True) fvs_dict = self.get_fvs_dict(fvs) - if (fvs_dict.has_key("SAI_PORT_ATTR_HW_LANE_LIST")): + if ("SAI_PORT_ATTR_HW_LANE_LIST" in fvs_dict): assert(fvs_dict['SAI_PORT_ATTR_HW_LANE_LIST'] == self.get_lanes_asic_db_str()) assert(fvs_dict['SAI_PORT_ATTR_SPEED'] == str(self.get_speed())) diff --git a/tests/test_mirror_ipv6_separate.py b/tests/test_mirror_ipv6_separate.py index 6696b26d2145..966d1217c906 100644 --- a/tests/test_mirror_ipv6_separate.py +++ b/tests/test_mirror_ipv6_separate.py @@ -284,7 +284,7 @@ def test_CreateMirrorIngressAndEgress(self, dvs, testlog): self.create_acl_table(ingress_table, ports, "MIRROR") # Check that the table has been created - table_ids = asic_db.wait_for_n_keys("ASIC_STATE:SAI_OBJECT_TYPE_ACL_TABLE", + table_ids = asic_db.wait_for_n_keys("ASIC_STATE:SAI_OBJECT_TYPE_ACL_TABLE", len(asic_db.default_acl_tables) + 1) table_entries = [oid for oid in table_ids if oid not in asic_db.default_acl_tables] original_entry = table_entries[0] @@ -293,7 +293,7 @@ def test_CreateMirrorIngressAndEgress(self, dvs, testlog): self.create_acl_table(duplicate_ingress_table, ports, "MIRROR") # Check that there is still only one table, and that it is the original table - table_ids = asic_db.wait_for_n_keys("ASIC_STATE:SAI_OBJECT_TYPE_ACL_TABLE", + table_ids = asic_db.wait_for_n_keys("ASIC_STATE:SAI_OBJECT_TYPE_ACL_TABLE", len(asic_db.default_acl_tables) + 1) table_entries = [oid for oid in table_ids if oid not in asic_db.default_acl_tables] assert table_entries[0] == original_entry @@ -305,14 +305,14 @@ def test_CreateMirrorIngressAndEgress(self, dvs, testlog): self.create_acl_table(egress_table, ports, "MIRROR", "egress") # Check that there are two tables - asic_db.wait_for_n_keys("ASIC_STATE:SAI_OBJECT_TYPE_ACL_TABLE", + asic_db.wait_for_n_keys("ASIC_STATE:SAI_OBJECT_TYPE_ACL_TABLE", len(asic_db.default_acl_tables) + 2) # Attempt to create another MIRROR table with egress ACLs self.create_acl_table(duplicate_egress_table, ports, "MIRROR", "egress") # Check that there are still only two tables - asic_db.wait_for_n_keys("ASIC_STATE:SAI_OBJECT_TYPE_ACL_TABLE", + asic_db.wait_for_n_keys("ASIC_STATE:SAI_OBJECT_TYPE_ACL_TABLE", len(asic_db.default_acl_tables) + 2) self.remove_acl_table(ingress_table) diff --git a/tests/test_nhg.py b/tests/test_nhg.py index 4a12590d9af5..91692addd449 100644 --- a/tests/test_nhg.py +++ b/tests/test_nhg.py @@ -1,5 +1,6 @@ import os import re +import sys import time import json import pytest @@ -206,7 +207,7 @@ def asic_route_nhg_fvs(k): if not fvs: return None - print fvs + print(fvs) nhgid = fvs.get("SAI_ROUTE_ENTRY_ATTR_NEXT_HOP_ID") if nhgid is None: return None @@ -216,7 +217,10 @@ def asic_route_nhg_fvs(k): MAX_ECMP_COUNT = 512 MAX_PORT_COUNT = 10 - IP_INTEGER_BASE = int(ipaddress.IPv4Address(unicode("2.2.2.0"))) + if sys.version_info < (3, 0): + IP_INTEGER_BASE = int(ipaddress.IPv4Address(unicode("2.2.2.0"))) + else: + IP_INTEGER_BASE = int(ipaddress.IPv4Address(str("2.2.2.0"))) config_db = dvs.get_config_db() fvs = {"NULL": "NULL"} diff --git a/tests/test_port_mac_learn.py b/tests/test_port_mac_learn.py index b2f1dccceb1b..6731ed7b54cf 100644 --- a/tests/test_port_mac_learn.py +++ b/tests/test_port_mac_learn.py @@ -60,7 +60,7 @@ def check_learn_mode_in_asicdb(self, interface_oid, learn_mode): return True else: return False - + def test_PortMacLearnMode(self, dvs, testlog): self.setup_db(dvs) @@ -126,7 +126,7 @@ def test_PortchannelMacLearnMode(self, dvs, testlog): ("mtu", "9100")]) tbl.set("PortChannel001", fvs) time.sleep(1) - + # create vlan tbl = swsscommon.Table(self.cdb, "VLAN") fvs = swsscommon.FieldValuePairs([("vlanid", "3")]) diff --git a/tests/test_setro.py b/tests/test_setro.py index 896d0c1facb8..6000f0fb8bfa 100644 --- a/tests/test_setro.py +++ b/tests/test_setro.py @@ -36,7 +36,7 @@ def test_SetReadOnlyAttribute(self, dvs, testlog): key = "SAI_OBJECT_TYPE_SWITCH:" + swRid - print key + print(key) ntf.send("set_ro", key, fvp) diff --git a/tests/test_tunnel.py b/tests/test_tunnel.py index bd8273e703d4..d3d5811adb58 100644 --- a/tests/test_tunnel.py +++ b/tests/test_tunnel.py @@ -134,7 +134,7 @@ def remove_and_test_tunnel(self, db, asicdb, tunnel_name): status, fvs = tunnel_table.get(tunnel_sai_obj) # get overlay loopback interface oid to check if it is deleted with the tunnel - overlay_infs_id = {f:v for f,v in fvs}["SAI_TUNNEL_ATTR_OVERLAY_INTERFACE"] + overlay_infs_id = {f:v for f,v in fvs}["SAI_TUNNEL_ATTR_OVERLAY_INTERFACE"] ps = swsscommon.ProducerStateTable(db, self.APP_TUNNEL_DECAP_TABLE_NAME) ps.set(tunnel_name, create_fvs(), 'DEL') diff --git a/tests/test_vrf.py b/tests/test_vrf.py index 6b04ce63f273..f68d80775be6 100644 --- a/tests/test_vrf.py +++ b/tests/test_vrf.py @@ -44,7 +44,7 @@ def is_vrf_attributes_correct(self, db, table, key, expected_attributes): assert status, "Got an error when get a key" # filter the fake 'NULL' attribute out - fvs = filter(lambda x : x != ('NULL', 'NULL'), fvs) + fvs = [x for x in fvs if x != ('NULL', 'NULL')] attr_keys = {entry[0] for entry in fvs} assert attr_keys == set(expected_attributes.keys()) @@ -78,7 +78,7 @@ def vrf_create(self, dvs, vrf_name, attributes, expected_attributes): assert len(intf_entries) == 1 assert intf_entries[0] == vrf_name exp_attr = {} - for an in xrange(len(attributes)): + for an in range(len(attributes)): exp_attr[attributes[an][0]] = attributes[an][1] self.is_vrf_attributes_correct(self.pdb, "VRF_TABLE", vrf_name, exp_attr) @@ -142,7 +142,7 @@ def boolean_gen(self): def mac_addr_gen(self): - ns = [random.randint(0, 255) for _ in xrange(6)] + ns = [random.randint(0, 255) for _ in range(6)] ns[0] &= 0xfe mac = ':'.join("%02x" % n for n in ns) return mac, mac.upper() @@ -177,13 +177,13 @@ def test_VRFMgr_Comprehensive(self, dvs, testlog): random.seed(int(time.clock())) - for n in xrange(2**len(attributes)): + for n in range(2**len(attributes)): # generate testcases for all combinations of attributes req_attr = [] exp_attr = {} vrf_name = "Vrf_%d" % n bmask = 0x1 - for an in xrange(len(attributes)): + for an in range(len(attributes)): if (bmask & n) > 0: req_res, exp_res = attributes[an][2]() req_attr.append((attributes[an][0], req_res)) diff --git a/tests/test_warm_reboot.py b/tests/test_warm_reboot.py index 8d327873f3ab..87f02272e0ea 100644 --- a/tests/test_warm_reboot.py +++ b/tests/test_warm_reboot.py @@ -661,7 +661,7 @@ def test_swss_neighbor_syncup(self, dvs, testlog): # use "change" if neighbor is in FAILED state for i in range(0, len(ips), 2): (rc, output) = dvs.runcmd(['sh', '-c', "ip -4 neigh | grep {}".format(ips[i])]) - print output + print(output) if output: dvs.runcmd("ip neigh change {} dev {} lladdr {} nud reachable".format(ips[i], intfs[i/2], macs[i])) else: @@ -669,7 +669,7 @@ def test_swss_neighbor_syncup(self, dvs, testlog): for i in range(0, len(v6ips), 2): (rc, output) = dvs.runcmd(['sh', '-c', "ip -6 neigh | grep {}".format(v6ips[i])]) - print output + print(output) if output: dvs.runcmd("ip -6 neigh change {} dev {} lladdr {} nud reachable".format(v6ips[i], intfs[i/2], macs[i])) else: @@ -995,9 +995,9 @@ def test_swss_port_state_syncup(self, dvs, testlog): # appDB port table operation orchStateCount = 0 for message in pubsubMessages: - print message + print(message) key = message['channel'].split(':', 1)[1] - print key + print(key) if message['data'] != 'hset' and message['data'] != 'del': continue if key.find(swsscommon.APP_PORT_TABLE_NAME)==0: @@ -2040,7 +2040,7 @@ def test_system_warmreboot_neighbor_syncup(self, dvs, testlog): # waited 10 above already i = 10 while (not kernel_restore_neighs_done(restoretbl)): - print "Waiting for kernel neighbors restore process done: {} seconds".format(i) + print("Waiting for kernel neighbors restore process done: {} seconds".format(i)) time.sleep(10) i += 10