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
19
21
from dvslib import dvs_lag
20
22
21
23
def 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 )
23
28
if rc :
24
29
raise RuntimeError ('Failed to run command: %s. rc=%d. output: %s' % (cmd , rc , output ))
25
30
@@ -136,17 +141,17 @@ def runcmd(self, cmd):
136
141
try :
137
142
out = subprocess .check_output ("ip netns exec %s %s" % (self .nsname , cmd ), stderr = subprocess .STDOUT , shell = True )
138
143
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 ( "------" )
142
147
return e .returncode
143
148
return 0
144
149
145
150
def runcmd_async (self , cmd ):
146
151
return subprocess .Popen ("ip netns exec %s %s" % (self .nsname , cmd ), shell = True )
147
152
148
153
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' )
150
155
151
156
class DockerVirtualSwitch (object ):
152
157
APP_DB_ID = 0
@@ -187,7 +192,10 @@ def __init__(self, name=None, imgname=None, keeptb=False, fakeplatform=None):
187
192
for ctn in self .client .containers .list ():
188
193
if ctn .name == name :
189
194
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 )
191
199
ctn_sw_id = output .split (':' )[1 ]
192
200
self .cleanup = False
193
201
if self .ctn == None :
@@ -198,7 +206,10 @@ def __init__(self, name=None, imgname=None, keeptb=False, fakeplatform=None):
198
206
if ctn .id == ctn_sw_id or ctn .name == ctn_sw_id :
199
207
ctn_sw_name = ctn .name
200
208
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 )
202
213
self .ctn_sw_pid = int (output )
203
214
204
215
# create virtual servers
@@ -214,7 +225,10 @@ def __init__(self, name=None, imgname=None, keeptb=False, fakeplatform=None):
214
225
else :
215
226
self .ctn_sw = self .client .containers .run ('debian:jessie' , privileged = True , detach = True ,
216
227
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 )
218
232
self .ctn_sw_pid = int (output )
219
233
220
234
# create virtual server
@@ -284,7 +298,7 @@ def check_ready(self, timeout=30):
284
298
# get process status
285
299
res = self .ctn .exec_run ("supervisorctl status" )
286
300
try :
287
- out = res .output
301
+ out = res .output . decode ( 'utf-8' )
288
302
except AttributeError :
289
303
out = res
290
304
for l in out .split ('\n ' ):
@@ -322,7 +336,7 @@ def net_cleanup(self):
322
336
323
337
res = self .ctn .exec_run ("ip link show" )
324
338
try :
325
- out = res .output
339
+ out = res .output . decode ( 'utf-8' )
326
340
except AttributeError :
327
341
out = res
328
342
for l in out .split ('\n ' ):
@@ -335,7 +349,7 @@ def net_cleanup(self):
335
349
m = re .compile ("(eth|lo|Bridge|Ethernet)" ).match (pname )
336
350
if not m :
337
351
self .ctn .exec_run ("ip link del {}" .format (pname ))
338
- print "remove extra link {}" .format (pname )
352
+ print ( "remove extra link {}" .format (pname ) )
339
353
return
340
354
341
355
def ctn_restart (self ):
@@ -389,19 +403,19 @@ def runcmd(self, cmd):
389
403
res = self .ctn .exec_run (cmd )
390
404
try :
391
405
exitcode = res .exit_code
392
- out = res .output
406
+ out = res .output . decode ( 'utf-8' )
393
407
except AttributeError :
394
408
exitcode = 0
395
409
out = res
396
410
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 ( "-----" )
400
414
401
415
return (exitcode , out )
402
416
403
417
def copy_file (self , path , filename ):
404
- tarstr = StringIO .StringIO ()
418
+ tarstr = io .StringIO ()
405
419
tar = tarfile .open (fileobj = tarstr , mode = "w" )
406
420
tar .add (filename , os .path .basename (filename ))
407
421
tar .close ()
@@ -455,7 +469,7 @@ def CountSubscribedObjects(self, pubsub, ignore=None, timeout=10):
455
469
while True and idle < timeout :
456
470
message = pubsub .get_message ()
457
471
if message :
458
- print message
472
+ print ( message )
459
473
if ignore :
460
474
fds = message ['channel' ].split (':' )
461
475
if fds [2 ] in ignore :
@@ -482,7 +496,7 @@ def GetSubscribedAppDbObjects(self, pubsub, ignore=None, timeout=10):
482
496
while True and idle < timeout :
483
497
message = pubsub .get_message ()
484
498
if message :
485
- print message
499
+ print ( message )
486
500
key = message ['channel' ].split (':' , 1 )[1 ]
487
501
# In producer/consumer_state_table scenarios, every entry will
488
502
# show up twice for every push/pop operation, so skip the second
@@ -524,7 +538,7 @@ def GetSubscribedAsicDbObjects(self, pubsub, ignore=None, timeout=10):
524
538
while True and idle < timeout :
525
539
message = pubsub .get_message ()
526
540
if message :
527
- print message
541
+ print ( message )
528
542
key = message ['channel' ].split (':' , 1 )[1 ]
529
543
if ignore :
530
544
fds = message ['channel' ].split (':' )
0 commit comments