From 1053bd3deba39584e3fbe9abaeff8555c3c8286a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20Sedl=C3=A1k?= Date: Tue, 16 Apr 2013 16:47:40 +0200 Subject: [PATCH] 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 --- cli/__init__.py | 22 +++++++++++++++++++--- tempest/test.py | 5 +++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/cli/__init__.py b/cli/__init__.py index 7a92260884..a3038d236a 100644 --- a/cli/__init__.py +++ b/cli/__init__.py @@ -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 diff --git a/tempest/test.py b/tempest/test.py index ccb225106b..b0038e0aad 100644 --- a/tempest/test.py +++ b/tempest/test.py @@ -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