summaryrefslogtreecommitdiff
path: root/tempest_lib
diff options
context:
space:
mode:
authorMatthew Treinish <mtreinish@kortar.org>2014-08-28 10:20:29 -0400
committerMatthew Treinish <mtreinish@kortar.org>2014-08-28 10:28:14 -0400
commit622d56b7a50a8715ba5f656c91d250726bea8d81 (patch)
tree181a7c4989e127fa102ffb3f2639f4d275dc1f98 /tempest_lib
parent88372882bb645d6461d0d32397186ab98692e4fc (diff)
downloadtempest-lib-622d56b7a50a8715ba5f656c91d250726bea8d81.tar.gz
Clean up and move cli test base
This commit moves the migrated cli testing framework and adapts it to be an independent tool. Part of this is removing the config references and reworking the test class to be a regular class for calling cli args.
Diffstat (limited to 'tempest_lib')
-rw-r--r--tempest_lib/cli/__init__.py0
-rw-r--r--tempest_lib/cli/base.py192
-rw-r--r--tempest_lib/cli/output_parser.py171
3 files changed, 363 insertions, 0 deletions
diff --git a/tempest_lib/cli/__init__.py b/tempest_lib/cli/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tempest_lib/cli/__init__.py
diff --git a/tempest_lib/cli/base.py b/tempest_lib/cli/base.py
new file mode 100644
index 0000000..7b9b3bf
--- /dev/null
+++ b/tempest_lib/cli/base.py
@@ -0,0 +1,192 @@
+# Copyright 2013 OpenStack Foundation
+# All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+import functools
+import os
+import shlex
+import subprocess
+
+import testtools
+
+import tempest_lib.cli.output_parser
+from tempest_lib import exceptions
+from tempest_lib.openstack.common import log as logging
+from tempest_lib.openstack.common import versionutils
+
+
+LOG = logging.getLogger(__name__)
+
+
+def execute(cmd, action, flags='', params='', fail_ok=False,
+ merge_stderr=False, cli_dir='/usr/bin'):
+ """Executes specified command for the given action."""
+ cmd = ' '.join([os.path.join(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 CLIClientBase(object):
+ def __init__(self, username='', password='', tenant_name='', uri='',
+ cli_dir='', *args, **kwargs):
+ super(CLIClientBase, self).__init__()
+ self.parser = tempest_lib.cli.output_parser
+ self.cli_dir = cli_dir if cli_dir else '/usr/bin'
+ self.username = username
+ self.tenant_name = tenant_name
+ self.password = password
+ self.uri = uri
+
+ def nova(self, action, flags='', params='', admin=True, fail_ok=False,
+ endpoint_type='publicURL'):
+ """Executes nova command for the given action."""
+ flags += ' --endpoint-type %s' % endpoint_type
+ return self.cmd_with_auth(
+ 'nova', action, flags, params, admin, fail_ok)
+
+ def nova_manage(self, action, flags='', params='', fail_ok=False,
+ merge_stderr=False):
+ """Executes nova-manage command for the given action."""
+ return execute(
+ 'nova-manage', action, flags, params, fail_ok, merge_stderr)
+
+ def keystone(self, action, flags='', params='', admin=True, fail_ok=False):
+ """Executes keystone command for the given action."""
+ return self.cmd_with_auth(
+ 'keystone', action, flags, params, admin, fail_ok)
+
+ def glance(self, action, flags='', params='', admin=True, fail_ok=False,
+ endpoint_type='publicURL'):
+ """Executes glance command for the given action."""
+ flags += ' --os-endpoint-type %s' % endpoint_type
+ return self.cmd_with_auth(
+ 'glance', action, flags, params, admin, fail_ok)
+
+ def ceilometer(self, action, flags='', params='', admin=True,
+ fail_ok=False, endpoint_type='publicURL'):
+ """Executes ceilometer command for the given action."""
+ flags += ' --os-endpoint-type %s' % endpoint_type
+ return self.cmd_with_auth(
+ 'ceilometer', action, flags, params, admin, fail_ok)
+
+ def heat(self, action, flags='', params='', admin=True,
+ fail_ok=False, endpoint_type='publicURL'):
+ """Executes heat command for the given action."""
+ flags += ' --os-endpoint-type %s' % endpoint_type
+ return self.cmd_with_auth(
+ 'heat', action, flags, params, admin, fail_ok)
+
+ def cinder(self, action, flags='', params='', admin=True, fail_ok=False,
+ endpoint_type='publicURL'):
+ """Executes cinder command for the given action."""
+ flags += ' --endpoint-type %s' % endpoint_type
+ return self.cmd_with_auth(
+ 'cinder', action, flags, params, admin, fail_ok)
+
+ def swift(self, action, flags='', params='', admin=True, fail_ok=False,
+ endpoint_type='publicURL'):
+ """Executes swift command for the given action."""
+ flags += ' --os-endpoint-type %s' % endpoint_type
+ return self.cmd_with_auth(
+ 'swift', action, flags, params, admin, fail_ok)
+
+ def neutron(self, action, flags='', params='', admin=True, fail_ok=False,
+ endpoint_type='publicURL'):
+ """Executes neutron command for the given action."""
+ flags += ' --endpoint-type %s' % endpoint_type
+ return self.cmd_with_auth(
+ 'neutron', action, flags, params, admin, fail_ok)
+
+ def sahara(self, action, flags='', params='', admin=True,
+ fail_ok=False, merge_stderr=True, endpoint_type='publicURL'):
+ """Executes sahara command for the given action."""
+ flags += ' --endpoint-type %s' % endpoint_type
+ return self.cmd_with_auth(
+ 'sahara', action, flags, params, admin, fail_ok, merge_stderr)
+
+ def cmd_with_auth(self, cmd, action, flags='', params='',
+ fail_ok=False, merge_stderr=False):
+ """Executes given command with auth attributes appended."""
+ # TODO(jogo) make admin=False work
+ creds = ('--os-username %s --os-tenant-name %s --os-password %s '
+ '--os-auth-url %s' %
+ (self.username,
+ self.tenant_name,
+ self.password,
+ self.uri))
+ flags = creds + ' ' + flags
+ return execute(cmd, action, flags, params, fail_ok, merge_stderr,
+ self.cli_dir)
+
+ def assertTableStruct(self, items, field_names):
+ """Verify that all items has keys listed in field_names."""
+ for item in items:
+ for field in field_names:
+ self.assertIn(field, item)
+
+ def assertFirstLineStartsWith(self, lines, beginning):
+ self.assertTrue(lines[0].startswith(beginning),
+ msg=('Beginning of first line has invalid content: %s'
+ % lines[:3]))
diff --git a/tempest_lib/cli/output_parser.py b/tempest_lib/cli/output_parser.py
new file mode 100644
index 0000000..80234a3
--- /dev/null
+++ b/tempest_lib/cli/output_parser.py
@@ -0,0 +1,171 @@
+# Copyright 2013 OpenStack Foundation
+# All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+"""Collection of utilities for parsing CLI clients output."""
+
+import re
+
+from tempest import exceptions
+from tempest.openstack.common import log as logging
+
+
+LOG = logging.getLogger(__name__)
+
+
+delimiter_line = re.compile('^\+\-[\+\-]+\-\+$')
+
+
+def details_multiple(output_lines, with_label=False):
+ """Return list of dicts with item details from cli output tables.
+
+ If with_label is True, key '__label' is added to each items dict.
+ For more about 'label' see OutputParser.tables().
+ """
+ items = []
+ tables_ = tables(output_lines)
+ for table_ in tables_:
+ if 'Property' not in table_['headers'] \
+ or 'Value' not in table_['headers']:
+ raise exceptions.InvalidStructure()
+ item = {}
+ for value in table_['values']:
+ item[value[0]] = value[1]
+ if with_label:
+ item['__label'] = table_['label']
+ items.append(item)
+ return items
+
+
+def details(output_lines, with_label=False):
+ """Return dict with details of first item (table) found in output."""
+ items = details_multiple(output_lines, with_label)
+ return items[0]
+
+
+def listing(output_lines):
+ """Return list of dicts with basic item info parsed from cli output.
+ """
+
+ items = []
+ table_ = table(output_lines)
+ for row in table_['values']:
+ item = {}
+ for col_idx, col_key in enumerate(table_['headers']):
+ item[col_key] = row[col_idx]
+ items.append(item)
+ return items
+
+
+def tables(output_lines):
+ """Find all ascii-tables in output and parse them.
+
+ Return list of tables parsed from cli output as dicts.
+ (see OutputParser.table())
+
+ And, if found, label key (separated line preceding the table)
+ is added to each tables dict.
+ """
+ tables_ = []
+
+ table_ = []
+ label = None
+
+ start = False
+ header = False
+
+ if not isinstance(output_lines, list):
+ output_lines = output_lines.split('\n')
+
+ for line in output_lines:
+ if delimiter_line.match(line):
+ if not start:
+ start = True
+ elif not header:
+ # we are after head area
+ header = True
+ else:
+ # table ends here
+ start = header = None
+ table_.append(line)
+
+ parsed = table(table_)
+ parsed['label'] = label
+ tables_.append(parsed)
+
+ table_ = []
+ label = None
+ continue
+ if start:
+ table_.append(line)
+ else:
+ if label is None:
+ label = line
+ else:
+ LOG.warn('Invalid line between tables: %s' % line)
+ if len(table_) > 0:
+ LOG.warn('Missing end of table')
+
+ return tables_
+
+
+def table(output_lines):
+ """Parse single table from cli output.
+
+ Return dict with list of column names in 'headers' key and
+ rows in 'values' key.
+ """
+ table_ = {'headers': [], 'values': []}
+ columns = None
+
+ if not isinstance(output_lines, list):
+ output_lines = output_lines.split('\n')
+
+ if not output_lines[-1]:
+ # skip last line if empty (just newline at the end)
+ output_lines = output_lines[:-1]
+
+ for line in output_lines:
+ if delimiter_line.match(line):
+ columns = _table_columns(line)
+ continue
+ if '|' not in line:
+ LOG.warn('skipping invalid table line: %s' % line)
+ continue
+ row = []
+ for col in columns:
+ row.append(line[col[0]:col[1]].strip())
+ if table_['headers']:
+ table_['values'].append(row)
+ else:
+ table_['headers'] = row
+
+ return table_
+
+
+def _table_columns(first_table_row):
+ """Find column ranges in output line.
+
+ Return list of tuples (start,end) for each column
+ detected by plus (+) characters in delimiter line.
+ """
+ positions = []
+ start = 1 # there is '+' at 0
+ while start < len(first_table_row):
+ end = first_table_row.find('+', start)
+ if end == -1:
+ break
+ positions.append((start, end))
+ start = end + 1
+ return positions