Skip to content

Commit 82d36c4

Browse files
authored
support swss vstest in python3
2 parents 2ebd44e + ff04e6d commit 82d36c4

17 files changed

+247
-233
lines changed

tests/README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ SWSS, Redis, and all the other required components run inside a virtual switch D
1414
```
1515
sudo modprobe team
1616
sudo apt install net-tools ethtool vlan
17-
sudo pip install docker zipp==2.2.1 pytest==4.6.9 flaky redis
17+
sudo pip3 install docker zipp==2.2.1 pytest==4.6.9 flaky redis distro==1.4.0
1818
```
19-
3. Install `python-swsscommon_1.0.0_amd64.deb`. You will need to install all the dependencies as well in the following order:
19+
3. Install `python3-swsscommon_1.0.0_amd64.deb`. You will need to install all the dependencies as well in the following order:
2020
2121
```
2222
sudo dpkg -i libnl-3-200_3.5.0-1_amd64.deb
@@ -30,7 +30,10 @@ SWSS, Redis, and all the other required components run inside a virtual switch D
3030
3131
You can find the dependencies [here](https://sonic-jenkins.westus2.cloudapp.azure.com/job/vs/job/buildimage-vs-all/lastSuccessfulBuild/artifact/target/debs/stretch/), and get this package by:
3232
- [Building it from scratch](https://github.com/Azure/sonic-swss-common)
33-
- [Downloading the latest build from Jenkins](https://sonic-jenkins.westus2.cloudapp.azure.com/job/common/job/sonic-swss-common-build/lastSuccessfulBuild/artifact/target/)
33+
- Downloading the latest build from Jenkins
34+
- [Debian](https://sonic-jenkins.westus2.cloudapp.azure.com/job/common/job/sonic-swss-common-build/lastSuccessfulBuild/artifact/target/)
35+
- [Ubuntu 18.04](https://sonic-jenkins.westus2.cloudapp.azure.com/job/common/job/sonic-swss-common-build-ubuntu/lastSuccessfulBuild/artifact/target/)
36+
- [Ubuntu 20.04](https://sonic-jenkins.westus2.cloudapp.azure.com/job/common/job/sonic-swss-common-build-ubuntu-20_04/lastSuccessfulBuild/artifact/target/)
3437
4. Load the `docker-sonic-vs.gz` file into docker. You can get the image by:
3538
- [Building it from scratch](https://github.com/Azure/sonic-buildimage)
3639
- [Downloading the latest build from Jenkins](https://sonic-jenkins.westus2.cloudapp.azure.com/job/vs/job/buildimage-vs-all/lastSuccessfulBuild/artifact/target/)

tests/conftest.py

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
import redis
77
import docker
88
import pytest
9-
import commands
109
import tarfile
11-
import StringIO
10+
import io
1211
import subprocess
12+
import sys
13+
if sys.version_info < (3, 0):
14+
import commands
1315

1416
from datetime import datetime
1517
from swsscommon import swsscommon
@@ -21,7 +23,10 @@
2123
from dvslib import dvs_policer
2224

2325
def ensure_system(cmd):
24-
(rc, output) = commands.getstatusoutput(cmd)
26+
if sys.version_info < (3, 0):
27+
(rc, output) = commands.getstatusoutput(cmd)
28+
else:
29+
(rc, output) = subprocess.getstatusoutput(cmd)
2530
if rc:
2631
raise RuntimeError('Failed to run command: %s. rc=%d. output: %s' % (cmd, rc, output))
2732

@@ -138,17 +143,17 @@ def runcmd(self, cmd):
138143
try:
139144
out = subprocess.check_output("ip netns exec %s %s" % (self.nsname, cmd), stderr=subprocess.STDOUT, shell=True)
140145
except subprocess.CalledProcessError as e:
141-
print "------rc={} for cmd: {}------".format(e.returncode, e.cmd)
142-
print e.output.rstrip()
143-
print "------"
146+
print("------rc={} for cmd: {}------".format(e.returncode, e.cmd))
147+
print(e.output.rstrip())
148+
print("------")
144149
return e.returncode
145150
return 0
146151

147152
def runcmd_async(self, cmd):
148153
return subprocess.Popen("ip netns exec %s %s" % (self.nsname, cmd), shell=True)
149154

150155
def runcmd_output(self, cmd):
151-
return subprocess.check_output("ip netns exec %s %s" % (self.nsname, cmd), shell=True)
156+
return subprocess.check_output("ip netns exec %s %s" % (self.nsname, cmd), shell=True).decode('utf-8')
152157

153158
class DockerVirtualSwitch(object):
154159
APP_DB_ID = 0
@@ -189,7 +194,10 @@ def __init__(self, name=None, imgname=None, keeptb=False, fakeplatform=None):
189194
for ctn in self.client.containers.list():
190195
if ctn.name == name:
191196
self.ctn = ctn
192-
(status, output) = commands.getstatusoutput("docker inspect --format '{{.HostConfig.NetworkMode}}' %s" % name)
197+
if sys.version_info < (3, 0):
198+
(status, output) = commands.getstatusoutput("docker inspect --format '{{.HostConfig.NetworkMode}}' %s" % name)
199+
else:
200+
(status, output) = subprocess.getstatusoutput("docker inspect --format '{{.HostConfig.NetworkMode}}' %s" % name)
193201
ctn_sw_id = output.split(':')[1]
194202
self.cleanup = False
195203
if self.ctn == None:
@@ -200,7 +208,10 @@ def __init__(self, name=None, imgname=None, keeptb=False, fakeplatform=None):
200208
if ctn.id == ctn_sw_id or ctn.name == ctn_sw_id:
201209
ctn_sw_name = ctn.name
202210

203-
(status, output) = commands.getstatusoutput("docker inspect --format '{{.State.Pid}}' %s" % ctn_sw_name)
211+
if sys.version_info < (3, 0):
212+
(status, output) = commands.getstatusoutput("docker inspect --format '{{.State.Pid}}' %s" % ctn_sw_name)
213+
else:
214+
(status, output) = subprocess.getstatusoutput("docker inspect --format '{{.State.Pid}}' %s" % ctn_sw_name)
204215
self.ctn_sw_pid = int(output)
205216

206217
# create virtual servers
@@ -216,7 +227,10 @@ def __init__(self, name=None, imgname=None, keeptb=False, fakeplatform=None):
216227
else:
217228
self.ctn_sw = self.client.containers.run('debian:jessie', privileged=True, detach=True,
218229
command="bash", stdin_open=True)
219-
(status, output) = commands.getstatusoutput("docker inspect --format '{{.State.Pid}}' %s" % self.ctn_sw.name)
230+
if sys.version_info < (3, 0):
231+
(status, output) = commands.getstatusoutput("docker inspect --format '{{.State.Pid}}' %s" % self.ctn_sw.name)
232+
else:
233+
(status, output) = subprocess.getstatusoutput("docker inspect --format '{{.State.Pid}}' %s" % self.ctn_sw.name)
220234
self.ctn_sw_pid = int(output)
221235

222236
# create virtual server
@@ -286,7 +300,7 @@ def check_ready(self, timeout=30):
286300
# get process status
287301
res = self.ctn.exec_run("supervisorctl status")
288302
try:
289-
out = res.output
303+
out = res.output.decode('utf-8')
290304
except AttributeError:
291305
out = res
292306
for l in out.split('\n'):
@@ -324,7 +338,7 @@ def net_cleanup(self):
324338

325339
res = self.ctn.exec_run("ip link show")
326340
try:
327-
out = res.output
341+
out = res.output.decode('utf-8')
328342
except AttributeError:
329343
out = res
330344
for l in out.split('\n'):
@@ -337,7 +351,7 @@ def net_cleanup(self):
337351
m = re.compile("(eth|lo|Bridge|Ethernet)").match(pname)
338352
if not m:
339353
self.ctn.exec_run("ip link del {}".format(pname))
340-
print "remove extra link {}".format(pname)
354+
print("remove extra link {}".format(pname))
341355
return
342356

343357
def ctn_restart(self):
@@ -391,19 +405,19 @@ def runcmd(self, cmd):
391405
res = self.ctn.exec_run(cmd)
392406
try:
393407
exitcode = res.exit_code
394-
out = res.output
408+
out = res.output.decode('utf-8')
395409
except AttributeError:
396410
exitcode = 0
397411
out = res
398412
if exitcode != 0:
399-
print "-----rc={} for cmd {}-----".format(exitcode, cmd)
400-
print out.rstrip()
401-
print "-----"
413+
print("-----rc={} for cmd {}-----".format(exitcode, cmd))
414+
print(out.rstrip())
415+
print("-----")
402416

403417
return (exitcode, out)
404418

405419
def copy_file(self, path, filename):
406-
tarstr = StringIO.StringIO()
420+
tarstr = io.StringIO()
407421
tar = tarfile.open(fileobj=tarstr, mode="w")
408422
tar.add(filename, os.path.basename(filename))
409423
tar.close()
@@ -439,13 +453,15 @@ def add_log_marker(self, file=None):
439453
return marker
440454

441455
def SubscribeAppDbObject(self, objpfx):
442-
r = redis.Redis(unix_socket_path=self.redis_sock, db=swsscommon.APPL_DB)
456+
r = redis.Redis(unix_socket_path=self.redis_sock, db=swsscommon.APPL_DB,
457+
encoding="utf-8", decode_responses=True)
443458
pubsub = r.pubsub()
444459
pubsub.psubscribe("__keyspace@0__:%s*" % objpfx)
445460
return pubsub
446461

447462
def SubscribeAsicDbObject(self, objpfx):
448-
r = redis.Redis(unix_socket_path=self.redis_sock, db=swsscommon.ASIC_DB)
463+
r = redis.Redis(unix_socket_path=self.redis_sock, db=swsscommon.ASIC_DB,
464+
encoding="utf-8", decode_responses=True)
449465
pubsub = r.pubsub()
450466
pubsub.psubscribe("__keyspace@1__:ASIC_STATE:%s*" % objpfx)
451467
return pubsub
@@ -457,7 +473,7 @@ def CountSubscribedObjects(self, pubsub, ignore=None, timeout=10):
457473
while True and idle < timeout:
458474
message = pubsub.get_message()
459475
if message:
460-
print message
476+
print(message)
461477
if ignore:
462478
fds = message['channel'].split(':')
463479
if fds[2] in ignore:
@@ -474,7 +490,8 @@ def CountSubscribedObjects(self, pubsub, ignore=None, timeout=10):
474490
return (nadd, ndel)
475491

476492
def GetSubscribedAppDbObjects(self, pubsub, ignore=None, timeout=10):
477-
r = redis.Redis(unix_socket_path=self.redis_sock, db=swsscommon.APPL_DB)
493+
r = redis.Redis(unix_socket_path=self.redis_sock, db=swsscommon.APPL_DB,
494+
encoding="utf-8", decode_responses=True)
478495

479496
addobjs = []
480497
delobjs = []
@@ -484,7 +501,7 @@ def GetSubscribedAppDbObjects(self, pubsub, ignore=None, timeout=10):
484501
while True and idle < timeout:
485502
message = pubsub.get_message()
486503
if message:
487-
print message
504+
print(message)
488505
key = message['channel'].split(':', 1)[1]
489506
# In producer/consumer_state_table scenarios, every entry will
490507
# show up twice for every push/pop operation, so skip the second
@@ -517,7 +534,8 @@ def GetSubscribedAppDbObjects(self, pubsub, ignore=None, timeout=10):
517534

518535

519536
def GetSubscribedAsicDbObjects(self, pubsub, ignore=None, timeout=10):
520-
r = redis.Redis(unix_socket_path=self.redis_sock, db=swsscommon.ASIC_DB)
537+
r = redis.Redis(unix_socket_path=self.redis_sock, db=swsscommon.ASIC_DB,
538+
encoding="utf-8", decode_responses=True)
521539

522540
addobjs = []
523541
delobjs = []
@@ -526,7 +544,7 @@ def GetSubscribedAsicDbObjects(self, pubsub, ignore=None, timeout=10):
526544
while True and idle < timeout:
527545
message = pubsub.get_message()
528546
if message:
529-
print message
547+
print(message)
530548
key = message['channel'].split(':', 1)[1]
531549
if ignore:
532550
fds = message['channel'].split(':')
@@ -548,7 +566,8 @@ def GetSubscribedAsicDbObjects(self, pubsub, ignore=None, timeout=10):
548566

549567
def SubscribeDbObjects(self, dbobjs):
550568
# assuming all the db object pairs are in the same db instance
551-
r = redis.Redis(unix_socket_path=self.redis_sock)
569+
r = redis.Redis(unix_socket_path=self.redis_sock, encoding="utf-8",
570+
decode_responses=True)
552571
pubsub = r.pubsub()
553572
substr = ""
554573
for db, obj in dbobjs:
@@ -860,7 +879,8 @@ def setReadOnlyAttr(self, obj, attr, val):
860879
assert len(keys) == 1
861880

862881
swVid = keys[0]
863-
r = redis.Redis(unix_socket_path=self.redis_sock, db=swsscommon.ASIC_DB)
882+
r = redis.Redis(unix_socket_path=self.redis_sock, db=swsscommon.ASIC_DB,
883+
encoding="utf-8", decode_responses=True)
864884
swRid = r.hget("VIDTORID", swVid)
865885

866886
assert swRid is not None
@@ -871,7 +891,8 @@ def setReadOnlyAttr(self, obj, attr, val):
871891
fvp = swsscommon.FieldValuePairs([(attr, val)])
872892
key = "SAI_OBJECT_TYPE_SWITCH:" + swRid
873893

874-
ntf.send("set_ro", key, fvp)
894+
# explicit convert unicode string to str for python2
895+
ntf.send("set_ro", str(key), fvp)
875896

876897
def create_acl_table(self, table, type, ports):
877898
tbl = swsscommon.Table(self.cdb, "ACL_TABLE")

tests/dvslib/dvs_vlan.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from dvs_database import DVSDatabase
1+
from .dvs_database import DVSDatabase
22

33
class DVSVlan(object):
44
def __init__(self, adb, cdb, sdb, cntrdb, appdb):

tests/port_dpb.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from swsscommon import swsscommon
2-
import redis
32
import time
43
import os
54
import pytest
@@ -26,7 +25,7 @@ def __init__(self, dvs, name = None):
2625
self._app_db_ptbl = swsscommon.Table(self._app_db, swsscommon.APP_PORT_TABLE_NAME)
2726
self._asic_db = swsscommon.DBConnector(swsscommon.ASIC_DB, dvs.redis_sock, 0)
2827
self._asic_db_ptbl = swsscommon.Table(self._asic_db, "ASIC_STATE:SAI_OBJECT_TYPE_PORT")
29-
self._counters_db = redis.Redis(unix_socket_path=self._dvs.redis_sock, db=swsscommon.COUNTERS_DB)
28+
self._counters_db = dvs.get_counters_db()
3029
self._dvs_asic_db = dvs.get_asic_db()
3130

3231
def set_name(self, name):
@@ -87,7 +86,7 @@ def get_oid(self):
8786
return self._oid
8887

8988
def print_port(self):
90-
print "Port: %s Lanes: %s Speed: %d, Index: %d"%(self._name, self._lanes, self._speed, self._index)
89+
print("Port: %s Lanes: %s Speed: %d, Index: %d"%(self._name, self._lanes, self._speed, self._index))
9190

9291
def port_merge(self, child_ports):
9392
child_ports.sort(key=lambda x: x.get_port_num())
@@ -111,7 +110,7 @@ def port_split(self, child_ports):
111110
child_port_list = []
112111
port_num = self.get_port_num()
113112
num_lanes = len(self._lanes)
114-
offset = num_lanes/child_ports;
113+
offset = num_lanes//child_ports
115114
lanes_per_child = offset
116115
for i in range(child_ports):
117116
child_port_num = port_num + (i * offset)
@@ -120,7 +119,7 @@ def port_split(self, child_ports):
120119
child_port_lanes = []
121120
for j in range(lanes_per_child):
122121
child_port_lanes.append(self._lanes[(i*offset)+j])
123-
child_port_speed = self._speed/child_ports
122+
child_port_speed = self._speed//child_ports
124123
child_port_index = self._index
125124

126125
child_port = Port(self._dvs, child_port_name)
@@ -172,7 +171,11 @@ def exists_in_app_db(self):
172171
return status
173172

174173
def sync_oid(self):
175-
self._oid = self._counters_db.hget("COUNTERS_PORT_NAME_MAP", self.get_name())
174+
fvs = dict(self._counters_db.get_entry("COUNTERS_PORT_NAME_MAP", ""))
175+
try:
176+
self._oid = fvs[self.get_name()]
177+
except KeyError:
178+
self._oid = None
176179

177180
"""
178181
Expectation of the caller is that the port does exist in ASIC DB.
@@ -218,7 +221,7 @@ def verify_asic_db(self):
218221
(status, fvs) = self._asic_db_ptbl.get(self.get_oid())
219222
assert(status == True)
220223
fvs_dict = self.get_fvs_dict(fvs)
221-
if (fvs_dict.has_key("SAI_PORT_ATTR_HW_LANE_LIST")):
224+
if "SAI_PORT_ATTR_HW_LANE_LIST" in fvs_dict:
222225
assert(fvs_dict['SAI_PORT_ATTR_HW_LANE_LIST'] == self.get_lanes_asic_db_str())
223226
assert(fvs_dict['SAI_PORT_ATTR_SPEED'] == str(self.get_speed()))
224227

0 commit comments

Comments
 (0)