Make CLI tests python2.6 compatible

- adds setUpClass method to tempest.test.BaseTestCase
  to provide it instead of unittest.TestCase
- adds replacement for subprocess.check_output and CalledProcessError
- also ensures closing of devnull provided to check_output

Change-Id: I90382f8553baabc592a2bf7008d591d83f89f416
This commit is contained in:
Pavel Sedlák 2013-04-16 16:47:40 +02:00
parent 0ad0a61dfa
commit 1053bd3deb
2 changed files with 24 additions and 3 deletions

View File

@ -87,6 +87,15 @@ class ClientTestBase(tempest.test.BaseTestCase):
flags = creds + ' ' + flags
return self.cmd(cmd, action, flags, params, fail_ok)
def check_output(self, cmd, **kwargs):
# substitutes subprocess.check_output which is not in python2.6
kwargs['stdout'] = subprocess.PIPE
proc = subprocess.Popen(cmd, **kwargs)
output = proc.communicate()[0]
if proc.returncode != 0:
raise CommandFailed(proc.returncode, cmd, output)
return output
def cmd(self, cmd, action, flags='', params='', fail_ok=False,
merge_stderr=False):
"""Executes specified command for the given action."""
@ -96,10 +105,10 @@ class ClientTestBase(tempest.test.BaseTestCase):
cmd = shlex.split(cmd)
try:
if merge_stderr:
result = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
result = self.check_output(cmd, stderr=subprocess.STDOUT)
else:
devnull = open('/dev/null', 'w')
result = subprocess.check_output(cmd, stderr=devnull)
with open('/dev/null', 'w') as devnull:
result = self.check_output(cmd, stderr=devnull)
except subprocess.CalledProcessError, e:
LOG.error("command output:\n%s" % e.output)
raise
@ -110,3 +119,10 @@ class ClientTestBase(tempest.test.BaseTestCase):
for item in items:
for field in field_names:
self.assertIn(field, item)
class CommandFailed(subprocess.CalledProcessError):
# adds output attribute for python2.6
def __init__(self, returncode, cmd, output):
super(CommandFailed, self).__init__(returncode, cmd)
self.output = output

View File

@ -56,6 +56,11 @@ class BaseTestCase(testtools.TestCase,
#NOTE(afazekas): inspection workaround
BaseTestCase.config = config.TempestConfig()
@classmethod
def setUpClass(cls):
if hasattr(super(BaseTestCase, cls), 'setUpClass'):
super(BaseTestCase, cls).setUpClass()
class TestCase(BaseTestCase):
"""Base test case class for all Tempest tests