-
Notifications
You must be signed in to change notification settings - Fork 24
/
common.py
43 lines (36 loc) · 1.53 KB
/
common.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import os
import subprocess
import time
NOW = time.strftime('%Y%m%d%H%M%S')
class CommandRunner(object):
def __init__(self):
self.rc = 0
self.pid = 0
self.stdout = ''
self.rootdir = os.path.realpath(os.path.join(__file__, '../../../'))
def return_code_should_be(self, expected_rc):
if int(expected_rc) != self.rc:
raise AssertionError('Expected return code to be "%s" but was "%s".'
% (expected_rc, self.rc))
def return_code_should_not_be(self, expected_rc):
if int(expected_rc) == self.rc:
raise AssertionError('Expected return code not to be "%s".' % expected_rc)
def output_contains(self, s):
if s not in self.stdout:
raise AssertionError('Output does not contain "%s".' % s)
def output_does_not_contain(self, s):
if s in self.stdout:
raise AssertionError('Output contains "%s".' % s)
def run_command(self, command, detach=False):
process = subprocess.Popen(['/bin/bash', '-xc', command],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
if not detach:
stdout = process.communicate()[0].strip().decode()
self.rc = process.returncode
tmp = []
for x in stdout.split('\n'):
print(x)
if not x.startswith('+ '): # Remove debug lines that start with "+ "
tmp.append(x)
self.stdout = '\n'.join(tmp)