6
6
import redis
7
7
import docker
8
8
import pytest
9
- import commands
10
9
import tarfile
11
- import StringIO
10
+ import io
12
11
import subprocess
12
+ import sys
13
+ if sys .version_info < (3 , 0 ):
14
+ import commands
13
15
14
16
from datetime import datetime
15
17
from swsscommon import swsscommon
21
23
from dvslib import dvs_policer
22
24
23
25
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 )
25
30
if rc :
26
31
raise RuntimeError ('Failed to run command: %s. rc=%d. output: %s' % (cmd , rc , output ))
27
32
@@ -138,17 +143,17 @@ def runcmd(self, cmd):
138
143
try :
139
144
out = subprocess .check_output ("ip netns exec %s %s" % (self .nsname , cmd ), stderr = subprocess .STDOUT , shell = True )
140
145
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 ( "------" )
144
149
return e .returncode
145
150
return 0
146
151
147
152
def runcmd_async (self , cmd ):
148
153
return subprocess .Popen ("ip netns exec %s %s" % (self .nsname , cmd ), shell = True )
149
154
150
155
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' )
152
157
153
158
class DockerVirtualSwitch (object ):
154
159
APP_DB_ID = 0
@@ -189,7 +194,10 @@ def __init__(self, name=None, imgname=None, keeptb=False, fakeplatform=None):
189
194
for ctn in self .client .containers .list ():
190
195
if ctn .name == name :
191
196
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 )
193
201
ctn_sw_id = output .split (':' )[1 ]
194
202
self .cleanup = False
195
203
if self .ctn == None :
@@ -200,7 +208,10 @@ def __init__(self, name=None, imgname=None, keeptb=False, fakeplatform=None):
200
208
if ctn .id == ctn_sw_id or ctn .name == ctn_sw_id :
201
209
ctn_sw_name = ctn .name
202
210
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 )
204
215
self .ctn_sw_pid = int (output )
205
216
206
217
# create virtual servers
@@ -216,7 +227,10 @@ def __init__(self, name=None, imgname=None, keeptb=False, fakeplatform=None):
216
227
else :
217
228
self .ctn_sw = self .client .containers .run ('debian:jessie' , privileged = True , detach = True ,
218
229
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 )
220
234
self .ctn_sw_pid = int (output )
221
235
222
236
# create virtual server
@@ -286,7 +300,7 @@ def check_ready(self, timeout=30):
286
300
# get process status
287
301
res = self .ctn .exec_run ("supervisorctl status" )
288
302
try :
289
- out = res .output
303
+ out = res .output . decode ( 'utf-8' )
290
304
except AttributeError :
291
305
out = res
292
306
for l in out .split ('\n ' ):
@@ -324,7 +338,7 @@ def net_cleanup(self):
324
338
325
339
res = self .ctn .exec_run ("ip link show" )
326
340
try :
327
- out = res .output
341
+ out = res .output . decode ( 'utf-8' )
328
342
except AttributeError :
329
343
out = res
330
344
for l in out .split ('\n ' ):
@@ -337,7 +351,7 @@ def net_cleanup(self):
337
351
m = re .compile ("(eth|lo|Bridge|Ethernet)" ).match (pname )
338
352
if not m :
339
353
self .ctn .exec_run ("ip link del {}" .format (pname ))
340
- print "remove extra link {}" .format (pname )
354
+ print ( "remove extra link {}" .format (pname ) )
341
355
return
342
356
343
357
def ctn_restart (self ):
@@ -391,19 +405,19 @@ def runcmd(self, cmd):
391
405
res = self .ctn .exec_run (cmd )
392
406
try :
393
407
exitcode = res .exit_code
394
- out = res .output
408
+ out = res .output . decode ( 'utf-8' )
395
409
except AttributeError :
396
410
exitcode = 0
397
411
out = res
398
412
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 ( "-----" )
402
416
403
417
return (exitcode , out )
404
418
405
419
def copy_file (self , path , filename ):
406
- tarstr = StringIO .StringIO ()
420
+ tarstr = io .StringIO ()
407
421
tar = tarfile .open (fileobj = tarstr , mode = "w" )
408
422
tar .add (filename , os .path .basename (filename ))
409
423
tar .close ()
@@ -439,13 +453,15 @@ def add_log_marker(self, file=None):
439
453
return marker
440
454
441
455
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 )
443
458
pubsub = r .pubsub ()
444
459
pubsub .psubscribe ("__keyspace@0__:%s*" % objpfx )
445
460
return pubsub
446
461
447
462
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 )
449
465
pubsub = r .pubsub ()
450
466
pubsub .psubscribe ("__keyspace@1__:ASIC_STATE:%s*" % objpfx )
451
467
return pubsub
@@ -457,7 +473,7 @@ def CountSubscribedObjects(self, pubsub, ignore=None, timeout=10):
457
473
while True and idle < timeout :
458
474
message = pubsub .get_message ()
459
475
if message :
460
- print message
476
+ print ( message )
461
477
if ignore :
462
478
fds = message ['channel' ].split (':' )
463
479
if fds [2 ] in ignore :
@@ -474,7 +490,8 @@ def CountSubscribedObjects(self, pubsub, ignore=None, timeout=10):
474
490
return (nadd , ndel )
475
491
476
492
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 )
478
495
479
496
addobjs = []
480
497
delobjs = []
@@ -484,7 +501,7 @@ def GetSubscribedAppDbObjects(self, pubsub, ignore=None, timeout=10):
484
501
while True and idle < timeout :
485
502
message = pubsub .get_message ()
486
503
if message :
487
- print message
504
+ print ( message )
488
505
key = message ['channel' ].split (':' , 1 )[1 ]
489
506
# In producer/consumer_state_table scenarios, every entry will
490
507
# show up twice for every push/pop operation, so skip the second
@@ -517,7 +534,8 @@ def GetSubscribedAppDbObjects(self, pubsub, ignore=None, timeout=10):
517
534
518
535
519
536
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 )
521
539
522
540
addobjs = []
523
541
delobjs = []
@@ -526,7 +544,7 @@ def GetSubscribedAsicDbObjects(self, pubsub, ignore=None, timeout=10):
526
544
while True and idle < timeout :
527
545
message = pubsub .get_message ()
528
546
if message :
529
- print message
547
+ print ( message )
530
548
key = message ['channel' ].split (':' , 1 )[1 ]
531
549
if ignore :
532
550
fds = message ['channel' ].split (':' )
@@ -548,7 +566,8 @@ def GetSubscribedAsicDbObjects(self, pubsub, ignore=None, timeout=10):
548
566
549
567
def SubscribeDbObjects (self , dbobjs ):
550
568
# 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 )
552
571
pubsub = r .pubsub ()
553
572
substr = ""
554
573
for db , obj in dbobjs :
@@ -860,7 +879,8 @@ def setReadOnlyAttr(self, obj, attr, val):
860
879
assert len (keys ) == 1
861
880
862
881
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 )
864
884
swRid = r .hget ("VIDTORID" , swVid )
865
885
866
886
assert swRid is not None
@@ -871,7 +891,8 @@ def setReadOnlyAttr(self, obj, attr, val):
871
891
fvp = swsscommon .FieldValuePairs ([(attr , val )])
872
892
key = "SAI_OBJECT_TYPE_SWITCH:" + swRid
873
893
874
- ntf .send ("set_ro" , key , fvp )
894
+ # explicit convert unicode string to str for python2
895
+ ntf .send ("set_ro" , str (key ), fvp )
875
896
876
897
def create_acl_table (self , table , type , ports ):
877
898
tbl = swsscommon .Table (self .cdb , "ACL_TABLE" )
0 commit comments