Skip to content

Commit

Permalink
[vstest]: upgrade swss vs tests to python3
Browse files Browse the repository at this point in the history
this pr mainly contains the changes made by 2to3 conversion tool
  • Loading branch information
lguohan committed Jul 7, 2020
1 parent c05601c commit d7ff1ad
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 44 deletions.
56 changes: 35 additions & 21 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))

Expand Down Expand Up @@ -136,17 +141,17 @@ 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

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
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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'):
Expand Down Expand Up @@ -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'):
Expand All @@ -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):
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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(':')
Expand Down
2 changes: 1 addition & 1 deletion tests/dvslib/dvs_vlan.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
4 changes: 2 additions & 2 deletions tests/port_dpb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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()))

Expand Down
8 changes: 4 additions & 4 deletions tests/test_mirror_ipv6_separate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
Expand All @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions tests/test_nhg.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import re
import sys
import time
import json
import pytest
Expand Down Expand Up @@ -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
Expand All @@ -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"}
Expand Down
4 changes: 2 additions & 2 deletions tests/test_port_mac_learn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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")])
Expand Down
2 changes: 1 addition & 1 deletion tests/test_setro.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion tests/test_tunnel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
10 changes: 5 additions & 5 deletions tests/test_vrf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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))
Expand Down
10 changes: 5 additions & 5 deletions tests/test_warm_reboot.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,15 +661,15 @@ 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:
dvs.runcmd("ip neigh add {} dev {} lladdr {} nud reachable".format(ips[i], intfs[i/2], macs[i]))

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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit d7ff1ad

Please sign in to comment.