Add min_client_version decorator for CLI tests

This adds the min_client_version decorator to be used in the CLI tests.
The decorator is used skip CLI tests where the command being tested
isn't available in the version of the client we're testing against.

The cmd method is pulled out of the ClientTestBase class so it can be
used by the decorator and is renamed so it does not conflict with the
cmd argument passed to it.

To start using the decorator and show how it works, two known CLI tests
are decorated for minimum version checks on their clients.

Part of blueprint minversion-check-for-cli-tests

Change-Id: I8a0ee390048870d450772fa064d0dc2daa2b7036
This commit is contained in:
Matt Riedemann
2014-07-30 14:57:55 -07:00
parent 708643641c
commit 8040220404

View File

@@ -13,14 +13,18 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import functools
import os import os
import shlex import shlex
import subprocess import subprocess
import testtools
import tempest.cli.output_parser import tempest.cli.output_parser
from tempest import config from tempest import config
from tempest import exceptions from tempest import exceptions
from tempest.openstack.common import log as logging from tempest.openstack.common import log as logging
from tempest.openstack.common import versionutils
import tempest.test import tempest.test
@@ -29,6 +33,65 @@ LOG = logging.getLogger(__name__)
CONF = config.CONF CONF = config.CONF
def execute(cmd, action, flags='', params='', fail_ok=False,
merge_stderr=False):
"""Executes specified command for the given action."""
cmd = ' '.join([os.path.join(CONF.cli.cli_dir, cmd),
flags, action, params])
LOG.info("running: '%s'" % cmd)
cmd = shlex.split(cmd.encode('utf-8'))
result = ''
result_err = ''
stdout = subprocess.PIPE
stderr = subprocess.STDOUT if merge_stderr else subprocess.PIPE
proc = subprocess.Popen(cmd, stdout=stdout, stderr=stderr)
result, result_err = proc.communicate()
if not fail_ok and proc.returncode != 0:
raise exceptions.CommandFailed(proc.returncode,
cmd,
result,
result_err)
return result
def check_client_version(client, version):
"""Checks if the client's version is compatible with the given version
@param client: The client to check.
@param version: The version to compare against.
@return: True if the client version is compatible with the given version
parameter, False otherwise.
"""
current_version = execute(client, '', params='--version',
merge_stderr=True)
if not current_version.strip():
raise exceptions.TempestException('"%s --version" output was empty' %
client)
return versionutils.is_compatible(version, current_version,
same_major=False)
def min_client_version(*args, **kwargs):
"""A decorator to skip tests if the client used isn't of the right version.
@param client: The client command to run. For python-novaclient, this is
'nova', for python-cinderclient this is 'cinder', etc.
@param version: The minimum version required to run the CLI test.
"""
def decorator(func):
@functools.wraps(func)
def wrapper(*func_args, **func_kwargs):
if not check_client_version(kwargs['client'], kwargs['version']):
msg = "requires %s client version >= %s" % (kwargs['client'],
kwargs['version'])
raise testtools.TestCase.skipException(msg)
return func(*func_args, **func_kwargs)
return wrapper
return decorator
class ClientTestBase(tempest.test.BaseTestCase): class ClientTestBase(tempest.test.BaseTestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
@@ -50,7 +113,7 @@ class ClientTestBase(tempest.test.BaseTestCase):
def nova_manage(self, action, flags='', params='', fail_ok=False, def nova_manage(self, action, flags='', params='', fail_ok=False,
merge_stderr=False): merge_stderr=False):
"""Executes nova-manage command for the given action.""" """Executes nova-manage command for the given action."""
return self.cmd( return execute(
'nova-manage', action, flags, params, fail_ok, merge_stderr) 'nova-manage', action, flags, params, fail_ok, merge_stderr)
def keystone(self, action, flags='', params='', admin=True, fail_ok=False): def keystone(self, action, flags='', params='', admin=True, fail_ok=False):
@@ -114,28 +177,7 @@ class ClientTestBase(tempest.test.BaseTestCase):
CONF.identity.admin_password, CONF.identity.admin_password,
CONF.identity.uri)) CONF.identity.uri))
flags = creds + ' ' + flags flags = creds + ' ' + flags
return self.cmd(cmd, action, flags, params, fail_ok, merge_stderr) return execute(cmd, action, flags, params, fail_ok, merge_stderr)
def cmd(self, cmd, action, flags='', params='', fail_ok=False,
merge_stderr=False):
"""Executes specified command for the given action."""
cmd = ' '.join([os.path.join(CONF.cli.cli_dir, cmd),
flags, action, params])
LOG.info("running: '%s'" % cmd)
cmd = shlex.split(cmd.encode('utf-8'))
result = ''
result_err = ''
stdout = subprocess.PIPE
stderr = subprocess.STDOUT if merge_stderr else subprocess.PIPE
proc = subprocess.Popen(
cmd, stdout=stdout, stderr=stderr)
result, result_err = proc.communicate()
if not fail_ok and proc.returncode != 0:
raise exceptions.CommandFailed(proc.returncode,
cmd,
result,
result_err)
return result
def assertTableStruct(self, items, field_names): def assertTableStruct(self, items, field_names):
"""Verify that all items has keys listed in field_names.""" """Verify that all items has keys listed in field_names."""