66import redis
77import docker
88import pytest
9- import commands
109import tarfile
11- import StringIO
10+ import io
1211import subprocess
12+ import sys
13+ if sys .version_info < (3 , 0 ):
14+ import commands
1315
1416from datetime import datetime
1517from swsscommon import swsscommon
1921from dvslib import dvs_lag
2022
2123def ensure_system (cmd ):
22- (rc , output ) = commands .getstatusoutput (cmd )
24+ if sys .version_info < (3 , 0 ):
25+ (rc , output ) = commands .getstatusoutput (cmd )
26+ else :
27+ (rc , output ) = subprocess .getstatusoutput (cmd )
2328 if rc :
2429 raise RuntimeError ('Failed to run command: %s. rc=%d. output: %s' % (cmd , rc , output ))
2530
@@ -136,17 +141,17 @@ def runcmd(self, cmd):
136141 try :
137142 out = subprocess .check_output ("ip netns exec %s %s" % (self .nsname , cmd ), stderr = subprocess .STDOUT , shell = True )
138143 except subprocess .CalledProcessError as e :
139- print "------rc={} for cmd: {}------" .format (e .returncode , e .cmd )
140- print e .output .rstrip ()
141- print "------"
144+ print ( "------rc={} for cmd: {}------" .format (e .returncode , e .cmd ) )
145+ print ( e .output .rstrip () )
146+ print ( "------" )
142147 return e .returncode
143148 return 0
144149
145150 def runcmd_async (self , cmd ):
146151 return subprocess .Popen ("ip netns exec %s %s" % (self .nsname , cmd ), shell = True )
147152
148153 def runcmd_output (self , cmd ):
149- return subprocess .check_output ("ip netns exec %s %s" % (self .nsname , cmd ), shell = True )
154+ return subprocess .check_output ("ip netns exec %s %s" % (self .nsname , cmd ), shell = True ). decode ( 'utf-8' )
150155
151156class DockerVirtualSwitch (object ):
152157 APP_DB_ID = 0
@@ -187,7 +192,10 @@ def __init__(self, name=None, imgname=None, keeptb=False, fakeplatform=None):
187192 for ctn in self .client .containers .list ():
188193 if ctn .name == name :
189194 self .ctn = ctn
190- (status , output ) = commands .getstatusoutput ("docker inspect --format '{{.HostConfig.NetworkMode}}' %s" % name )
195+ if sys .version_info < (3 , 0 ):
196+ (status , output ) = commands .getstatusoutput ("docker inspect --format '{{.HostConfig.NetworkMode}}' %s" % name )
197+ else :
198+ (status , output ) = subprocess .getstatusoutput ("docker inspect --format '{{.HostConfig.NetworkMode}}' %s" % name )
191199 ctn_sw_id = output .split (':' )[1 ]
192200 self .cleanup = False
193201 if self .ctn == None :
@@ -198,7 +206,10 @@ def __init__(self, name=None, imgname=None, keeptb=False, fakeplatform=None):
198206 if ctn .id == ctn_sw_id or ctn .name == ctn_sw_id :
199207 ctn_sw_name = ctn .name
200208
201- (status , output ) = commands .getstatusoutput ("docker inspect --format '{{.State.Pid}}' %s" % ctn_sw_name )
209+ if sys .version_info < (3 , 0 ):
210+ (status , output ) = commands .getstatusoutput ("docker inspect --format '{{.State.Pid}}' %s" % ctn_sw_name )
211+ else :
212+ (status , output ) = subprocess .getstatusoutput ("docker inspect --format '{{.State.Pid}}' %s" % ctn_sw_name )
202213 self .ctn_sw_pid = int (output )
203214
204215 # create virtual servers
@@ -214,7 +225,10 @@ def __init__(self, name=None, imgname=None, keeptb=False, fakeplatform=None):
214225 else :
215226 self .ctn_sw = self .client .containers .run ('debian:jessie' , privileged = True , detach = True ,
216227 command = "bash" , stdin_open = True )
217- (status , output ) = commands .getstatusoutput ("docker inspect --format '{{.State.Pid}}' %s" % self .ctn_sw .name )
228+ if sys .version_info < (3 , 0 ):
229+ (status , output ) = commands .getstatusoutput ("docker inspect --format '{{.State.Pid}}' %s" % self .ctn_sw .name )
230+ else :
231+ (status , output ) = subprocess .getstatusoutput ("docker inspect --format '{{.State.Pid}}' %s" % self .ctn_sw .name )
218232 self .ctn_sw_pid = int (output )
219233
220234 # create virtual server
@@ -284,7 +298,7 @@ def check_ready(self, timeout=30):
284298 # get process status
285299 res = self .ctn .exec_run ("supervisorctl status" )
286300 try :
287- out = res .output
301+ out = res .output . decode ( 'utf-8' )
288302 except AttributeError :
289303 out = res
290304 for l in out .split ('\n ' ):
@@ -322,7 +336,7 @@ def net_cleanup(self):
322336
323337 res = self .ctn .exec_run ("ip link show" )
324338 try :
325- out = res .output
339+ out = res .output . decode ( 'utf-8' )
326340 except AttributeError :
327341 out = res
328342 for l in out .split ('\n ' ):
@@ -335,7 +349,7 @@ def net_cleanup(self):
335349 m = re .compile ("(eth|lo|Bridge|Ethernet)" ).match (pname )
336350 if not m :
337351 self .ctn .exec_run ("ip link del {}" .format (pname ))
338- print "remove extra link {}" .format (pname )
352+ print ( "remove extra link {}" .format (pname ) )
339353 return
340354
341355 def ctn_restart (self ):
@@ -389,19 +403,19 @@ def runcmd(self, cmd):
389403 res = self .ctn .exec_run (cmd )
390404 try :
391405 exitcode = res .exit_code
392- out = res .output
406+ out = res .output . decode ( 'utf-8' )
393407 except AttributeError :
394408 exitcode = 0
395409 out = res
396410 if exitcode != 0 :
397- print "-----rc={} for cmd {}-----" .format (exitcode , cmd )
398- print out .rstrip ()
399- print "-----"
411+ print ( "-----rc={} for cmd {}-----" .format (exitcode , cmd ) )
412+ print ( out .rstrip () )
413+ print ( "-----" )
400414
401415 return (exitcode , out )
402416
403417 def copy_file (self , path , filename ):
404- tarstr = StringIO .StringIO ()
418+ tarstr = io .StringIO ()
405419 tar = tarfile .open (fileobj = tarstr , mode = "w" )
406420 tar .add (filename , os .path .basename (filename ))
407421 tar .close ()
@@ -455,7 +469,7 @@ def CountSubscribedObjects(self, pubsub, ignore=None, timeout=10):
455469 while True and idle < timeout :
456470 message = pubsub .get_message ()
457471 if message :
458- print message
472+ print ( message )
459473 if ignore :
460474 fds = message ['channel' ].split (':' )
461475 if fds [2 ] in ignore :
@@ -482,7 +496,7 @@ def GetSubscribedAppDbObjects(self, pubsub, ignore=None, timeout=10):
482496 while True and idle < timeout :
483497 message = pubsub .get_message ()
484498 if message :
485- print message
499+ print ( message )
486500 key = message ['channel' ].split (':' , 1 )[1 ]
487501 # In producer/consumer_state_table scenarios, every entry will
488502 # show up twice for every push/pop operation, so skip the second
@@ -524,7 +538,7 @@ def GetSubscribedAsicDbObjects(self, pubsub, ignore=None, timeout=10):
524538 while True and idle < timeout :
525539 message = pubsub .get_message ()
526540 if message :
527- print message
541+ print ( message )
528542 key = message ['channel' ].split (':' , 1 )[1 ]
529543 if ignore :
530544 fds = message ['channel' ].split (':' )
0 commit comments