-#!/usr/bin/python
+#!/usr/bin/env python
#
# Copyright (c) 2015 All rights reserved
# This program and the accompanying materials
# http://www.apache.org/licenses/LICENSE-2.0
#
+"""Tempest testcases implementation."""
+
+from __future__ import division
+
+import logging
import os
import re
import shutil
import subprocess
import time
+import uuid
+import os_client_config
+from xtesting.core import testcase
import yaml
-from functest.core import testcase
from functest.opnfv_tests.openstack.tempest import conf_utils
-from functest.utils.constants import CONST
-import functest.utils.functest_logger as ft_logger
-import functest.utils.functest_utils as ft_utils
+from functest.utils import config
+from functest.utils import env
-""" logging configuration """
-logger = ft_logger.Logger("Tempest").getLogger()
+LOGGER = logging.getLogger(__name__)
class TempestCommon(testcase.TestCase):
-
- def __init__(self, case_name=''):
- super(TempestCommon, self).__init__(case_name)
- self.MODE = ""
- self.OPTION = ""
- self.VERIFIER_ID = conf_utils.get_verifier_id()
- self.VERIFIER_REPO_DIR = conf_utils.get_verifier_repo_dir(
- self.VERIFIER_ID)
- self.DEPLOYMENT_ID = conf_utils.get_verifier_deployment_id()
- self.DEPLOYMENT_DIR = conf_utils.get_verifier_deployment_dir(
- self.VERIFIER_ID, self.DEPLOYMENT_ID)
- self.VERIFICATION_ID = None
+ # pylint: disable=too-many-instance-attributes
+ """TempestCommon testcases implementation class."""
+
+ TEMPEST_RESULTS_DIR = os.path.join(
+ getattr(config.CONF, 'dir_results'), 'tempest')
+
+ def __init__(self, **kwargs):
+ super(TempestCommon, self).__init__(**kwargs)
+ self.resources = TempestResourcesManager()
+ self.mode = ""
+ self.option = []
+ self.verifier_id = conf_utils.get_verifier_id()
+ self.verifier_repo_dir = conf_utils.get_verifier_repo_dir(
+ self.verifier_id)
+ self.deployment_id = conf_utils.get_verifier_deployment_id()
+ self.deployment_dir = conf_utils.get_verifier_deployment_dir(
+ self.verifier_id, self.deployment_id)
+ self.verification_id = None
+ self.res_dir = TempestCommon.TEMPEST_RESULTS_DIR
+ self.raw_list = os.path.join(self.res_dir, 'test_raw_list.txt')
+ self.list = os.path.join(self.res_dir, 'test_list.txt')
+ self.conf_file = None
@staticmethod
def read_file(filename):
+ """Read file and return content as a stripped list."""
with open(filename) as src:
return [line.strip() for line in src.readlines()]
- def generate_test_list(self, verifier_repo_dir):
- logger.debug("Generating test case list...")
- if self.MODE == 'defcore':
- shutil.copyfile(
- conf_utils.TEMPEST_DEFCORE, conf_utils.TEMPEST_RAW_LIST)
- elif self.MODE == 'custom':
+ @staticmethod
+ def get_verifier_result(verif_id):
+ """Retrieve verification results."""
+ result = {
+ 'num_tests': 0,
+ 'num_success': 0,
+ 'num_failures': 0,
+ 'num_skipped': 0
+ }
+ cmd = ["rally", "verify", "show", "--uuid", verif_id]
+ LOGGER.info("Showing result for a verification: '%s'.", cmd)
+ proc = subprocess.Popen(cmd,
+ stdout=subprocess.PIPE,
+ stderr=subprocess.STDOUT)
+ for line in proc.stdout:
+ new_line = line.replace(' ', '').split('|')
+ if 'Tests' in new_line:
+ break
+ LOGGER.info(line)
+ if 'Testscount' in new_line:
+ result['num_tests'] = int(new_line[2])
+ elif 'Success' in new_line:
+ result['num_success'] = int(new_line[2])
+ elif 'Skipped' in new_line:
+ result['num_skipped'] = int(new_line[2])
+ elif 'Failures' in new_line:
+ result['num_failures'] = int(new_line[2])
+ return result
+
+ @staticmethod
+ def backup_tempest_config(conf_file, res_dir):
+ """
+ Copy config file to tempest results directory
+ """
+ if not os.path.exists(res_dir):
+ os.makedirs(res_dir)
+ shutil.copyfile(conf_file,
+ os.path.join(res_dir, 'tempest.conf'))
+
+ def generate_test_list(self):
+ """Generate test list based on the test mode."""
+ LOGGER.debug("Generating test case list...")
+ if self.mode == 'custom':
if os.path.isfile(conf_utils.TEMPEST_CUSTOM):
shutil.copyfile(
- conf_utils.TEMPEST_CUSTOM, conf_utils.TEMPEST_RAW_LIST)
+ conf_utils.TEMPEST_CUSTOM, self.list)
else:
raise Exception("Tempest test list file %s NOT found."
% conf_utils.TEMPEST_CUSTOM)
else:
- if self.MODE == 'smoke':
- testr_mode = "smoke"
- elif self.MODE == 'feature_multisite':
- testr_mode = "'[Kk]ingbird'"
- elif self.MODE == 'full':
- testr_mode = ""
+ if self.mode == 'smoke':
+ testr_mode = r"'^tempest\.(api|scenario).*\[.*\bsmoke\b.*\]$'"
+ elif self.mode == 'full':
+ testr_mode = r"'^tempest\.'"
else:
- testr_mode = 'tempest.api.' + self.MODE
- cmd = ("cd {0};"
- "testr list-tests {1} > {2};"
- "cd -;".format(verifier_repo_dir,
- testr_mode,
- conf_utils.TEMPEST_RAW_LIST))
- ft_utils.execute_command(cmd)
+ testr_mode = self.mode
+ cmd = "(cd {0}; stestr list {1} >{2} 2>/dev/null)".format(
+ self.verifier_repo_dir, testr_mode, self.list)
+ output = subprocess.check_output(cmd, shell=True)
+ LOGGER.info("%s\n%s", cmd, output)
def apply_tempest_blacklist(self):
- logger.debug("Applying tempest blacklist...")
- cases_file = self.read_file(conf_utils.TEMPEST_RAW_LIST)
- result_file = open(conf_utils.TEMPEST_LIST, 'w')
+ """Exclude blacklisted test cases."""
+ LOGGER.debug("Applying tempest blacklist...")
+ if os.path.exists(self.raw_list):
+ os.remove(self.raw_list)
+ os.rename(self.list, self.raw_list)
+ cases_file = self.read_file(self.raw_list)
+ result_file = open(self.list, 'w')
black_tests = []
try:
- installer_type = CONST.INSTALLER_TYPE
- deploy_scenario = CONST.DEPLOY_SCENARIO
- if (bool(installer_type) * bool(deploy_scenario)):
+ installer_type = env.get('INSTALLER_TYPE')
+ deploy_scenario = env.get('DEPLOY_SCENARIO')
+ if bool(installer_type) * bool(deploy_scenario):
# if INSTALLER_TYPE and DEPLOY_SCENARIO are set we read the
# file
black_list_file = open(conf_utils.TEMPEST_BLACKLIST)
for test in tests:
black_tests.append(test)
break
- except Exception:
+ except Exception: # pylint: disable=broad-except
black_tests = []
- logger.debug("Tempest blacklist file does not exist.")
+ LOGGER.debug("Tempest blacklist file does not exist.")
for cases_line in cases_file:
for black_tests_line in black_tests:
result_file.close()
def run_verifier_tests(self):
- self.OPTION += (" --load-list {} --detailed"
- .format(conf_utils.TEMPEST_LIST))
-
- cmd_line = "rally verify start " + self.OPTION
- logger.info("Starting Tempest test suite: '%s'." % cmd_line)
-
- header = ("Tempest environment:\n"
- " SUT: %s\n Scenario: %s\n Node: %s\n Date: %s\n" %
- (CONST.INSTALLER_TYPE,
- CONST.DEPLOY_SCENARIO,
- CONST.NODE_NAME,
- time.strftime("%a %b %d %H:%M:%S %Z %Y")))
+ """Execute tempest test cases."""
+ cmd = ["rally", "verify", "start", "--load-list",
+ self.list]
+ cmd.extend(self.option)
+ LOGGER.info("Starting Tempest test suite: '%s'.", cmd)
f_stdout = open(
- os.path.join(conf_utils.TEMPEST_RESULTS_DIR, "tempest.log"), 'w+')
+ os.path.join(self.res_dir, "tempest.log"), 'w+')
f_stderr = open(
- os.path.join(conf_utils.TEMPEST_RESULTS_DIR,
+ os.path.join(self.res_dir,
"tempest-error.log"), 'w+')
- f_env = open(os.path.join(conf_utils.TEMPEST_RESULTS_DIR,
- "environment.log"), 'w+')
- f_env.write(header)
- p = subprocess.Popen(
- cmd_line, shell=True,
+ proc = subprocess.Popen(
+ cmd,
stdout=subprocess.PIPE,
stderr=f_stderr,
bufsize=1)
- with p.stdout:
- for line in iter(p.stdout.readline, b''):
- if re.search("\} tempest\.", line):
- logger.info(line.replace('\n', ''))
- elif re.search('Starting verification', line):
- logger.info(line.replace('\n', ''))
- first_pos = line.index("UUID=") + len("UUID=")
- last_pos = line.index(") for deployment")
- self.VERIFICATION_ID = line[first_pos:last_pos]
- logger.debug('Verication UUID: %s' % self.VERIFICATION_ID)
+ with proc.stdout:
+ for line in iter(proc.stdout.readline, b''):
+ if re.search(r"\} tempest\.", line):
+ LOGGER.info(line.replace('\n', ''))
+ elif re.search(r'(?=\(UUID=(.*)\))', line):
+ self.verification_id = re.search(
+ r'(?=\(UUID=(.*)\))', line).group(1)
+ LOGGER.info('Verification UUID: %s', self.verification_id)
f_stdout.write(line)
- p.wait()
+ proc.wait()
f_stdout.close()
f_stderr.close()
- f_env.close()
- def parse_verifier_result(self):
- if self.VERIFICATION_ID is None:
+ if self.verification_id is None:
raise Exception('Verification UUID not found')
- cmd_line = "rally verify show --uuid {}".format(self.VERIFICATION_ID)
- logger.info("Showing result for a verification: '%s'." % cmd_line)
- p = subprocess.Popen(cmd_line,
- shell=True,
- stdout=subprocess.PIPE,
- stderr=subprocess.STDOUT)
- for line in p.stdout:
- new_line = line.replace(' ', '').split('|')
- if 'Tests' in new_line:
- break
-
- logger.info(line)
- if 'Testscount' in new_line:
- num_tests = new_line[2]
- elif 'Success' in new_line:
- num_success = new_line[2]
- elif 'Skipped' in new_line:
- num_skipped = new_line[2]
- elif 'Failures' in new_line:
- num_failures = new_line[2]
-
+ def parse_verifier_result(self):
+ """Parse and save test results."""
+ stat = self.get_verifier_result(self.verification_id)
try:
- num_executed = int(num_tests) - int(num_skipped)
- success_rate = 100 * int(num_success) / int(num_executed)
- with open(os.path.join(conf_utils.TEMPEST_RESULTS_DIR,
+ num_executed = stat['num_tests'] - stat['num_skipped']
+ try:
+ self.result = 100 * stat['num_success'] / num_executed
+ except ZeroDivisionError:
+ self.result = 0
+ if stat['num_tests'] > 0:
+ LOGGER.info("All tests have been skipped")
+ else:
+ LOGGER.error("No test has been executed")
+ return
+
+ with open(os.path.join(self.res_dir,
"tempest.log"), 'r') as logfile:
output = logfile.read()
- error_logs = ""
- for match in re.findall('(.*?)[. ]*fail ', output):
- error_logs += match
- skipped_testcase = ""
- for match in re.findall('(.*?)[. ]*skip:', output):
- skipped_testcase += match
-
- self.details = {"tests": int(num_tests),
- "failures": int(num_failures),
- "errors": error_logs,
- "skipped": skipped_testcase}
- except Exception:
- success_rate = 0
-
- self.criteria = ft_utils.check_success_rate(
- self.case_name, success_rate)
- logger.info("Tempest %s success_rate is %s%%, is marked as %s"
- % (self.case_name, success_rate, self.criteria))
-
- def run(self):
-
+ success_testcases = []
+ for match in re.findall(r'.*\{\d{1,2}\} (.*?) \.{3} success ',
+ output):
+ success_testcases.append(match)
+ failed_testcases = []
+ for match in re.findall(r'.*\{\d{1,2}\} (.*?) \.{3} fail',
+ output):
+ failed_testcases.append(match)
+ skipped_testcases = []
+ for match in re.findall(r'.*\{\d{1,2}\} (.*?) \.{3} skip:',
+ output):
+ skipped_testcases.append(match)
+
+ self.details = {"tests_number": stat['num_tests'],
+ "success_number": stat['num_success'],
+ "skipped_number": stat['num_skipped'],
+ "failures_number": stat['num_failures'],
+ "success": success_testcases,
+ "skipped": skipped_testcases,
+ "failures": failed_testcases}
+ except Exception: # pylint: disable=broad-except
+ self.result = 0
+
+ LOGGER.info("Tempest %s success_rate is %s%%",
+ self.case_name, self.result)
+
+ def generate_report(self):
+ """Generate verification report."""
+ html_file = os.path.join(self.res_dir,
+ "tempest-report.html")
+ cmd = ["rally", "verify", "report", "--type", "html", "--uuid",
+ self.verification_id, "--to", html_file]
+ subprocess.Popen(cmd, stdout=subprocess.PIPE,
+ stderr=subprocess.STDOUT)
+
+ def configure(self, **kwargs): # pylint: disable=unused-argument
+ """
+ Create all openstack resources for tempest-based testcases and write
+ tempest.conf.
+ """
+ if not os.path.exists(self.res_dir):
+ os.makedirs(self.res_dir)
+ self.resources.create()
+ compute_cnt = len(self.resources.cloud.list_hypervisors())
+ self.conf_file = conf_utils.configure_verifier(self.deployment_dir)
+ conf_utils.configure_tempest_update_params(
+ self.conf_file, network_name=self.resources.network.id,
+ image_id=self.resources.image.id,
+ flavor_id=self.resources.flavor.id,
+ compute_cnt=compute_cnt,
+ image_alt_id=self.resources.image_alt.id,
+ flavor_alt_id=self.resources.flavor_alt.id)
+ self.backup_tempest_config(self.conf_file, self.res_dir)
+
+ def run(self, **kwargs):
self.start_time = time.time()
-
- if not os.path.exists(conf_utils.TEMPEST_RESULTS_DIR):
- os.makedirs(conf_utils.TEMPEST_RESULTS_DIR)
-
try:
- image_and_flavor = conf_utils.create_tempest_resources()
- conf_utils.configure_tempest(
- self.DEPLOYMENT_DIR,
- IMAGE_ID=image_and_flavor.get("image_id"),
- FLAVOR_ID=image_and_flavor.get("flavor_id"),
- MODE=self.MODE)
- self.generate_test_list(self.VERIFIER_REPO_DIR)
+ self.configure(**kwargs)
+ self.generate_test_list()
self.apply_tempest_blacklist()
self.run_verifier_tests()
self.parse_verifier_result()
+ self.generate_report()
res = testcase.TestCase.EX_OK
- except Exception as e:
- logger.error('Error with run: %s' % e)
+ except Exception: # pylint: disable=broad-except
+ LOGGER.exception('Error with run')
res = testcase.TestCase.EX_RUN_ERROR
-
+ finally:
+ self.resources.cleanup()
self.stop_time = time.time()
return res
class TempestSmokeSerial(TempestCommon):
-
- def __init__(self, case_name='tempest_smoke_serial'):
- TempestCommon.__init__(self, case_name)
- self.MODE = "smoke"
- self.OPTION = "--concurrency 1"
+ """Tempest smoke serial testcase implementation."""
+ def __init__(self, **kwargs):
+ if "case_name" not in kwargs:
+ kwargs["case_name"] = 'tempest_smoke_serial'
+ TempestCommon.__init__(self, **kwargs)
+ self.mode = "smoke"
+ self.option = ["--concurrency", "1"]
+
+
+class TempestNeutronTrunk(TempestCommon):
+ """Tempest neutron trunk testcase implementation."""
+ def __init__(self, **kwargs):
+ if "case_name" not in kwargs:
+ kwargs["case_name"] = 'neutron_trunk'
+ TempestCommon.__init__(self, **kwargs)
+ self.mode = "'neutron_tempest_plugin.(api|scenario).test_trunk'"
+ self.res_dir = os.path.join(
+ getattr(config.CONF, 'dir_results'), 'neutron_trunk')
+ self.raw_list = os.path.join(self.res_dir, 'test_raw_list.txt')
+ self.list = os.path.join(self.res_dir, 'test_list.txt')
+
+ def configure(self, **kwargs):
+ super(TempestNeutronTrunk, self).configure(**kwargs)
+ rconfig = conf_utils.ConfigParser.RawConfigParser()
+ rconfig.read(self.conf_file)
+ rconfig.set('network-feature-enabled', 'api_extensions', 'all')
+ with open(self.conf_file, 'wb') as config_file:
+ rconfig.write(config_file)
class TempestSmokeParallel(TempestCommon):
-
- def __init__(self, case_name='tempest_smoke_parallel'):
- TempestCommon.__init__(self, case_name)
- self.MODE = "smoke"
- self.OPTION = ""
+ """Tempest smoke parallel testcase implementation."""
+ def __init__(self, **kwargs):
+ if "case_name" not in kwargs:
+ kwargs["case_name"] = 'tempest_smoke_parallel'
+ TempestCommon.__init__(self, **kwargs)
+ self.mode = "smoke"
class TempestFullParallel(TempestCommon):
-
- def __init__(self, case_name="tempest_full_parallel"):
- TempestCommon.__init__(self, case_name)
- self.MODE = "full"
-
-
-class TempestMultisite(TempestCommon):
-
- def __init__(self, case_name="multisite"):
- TempestCommon.__init__(self, case_name)
- self.MODE = "feature_multisite"
- self.OPTION = "--concurrency 1"
- conf_utils.install_verifier_ext(CONST.dir_repo_kingbird)
+ """Tempest full parallel testcase implementation."""
+ def __init__(self, **kwargs):
+ if "case_name" not in kwargs:
+ kwargs["case_name"] = 'tempest_full_parallel'
+ TempestCommon.__init__(self, **kwargs)
+ self.mode = "full"
class TempestCustom(TempestCommon):
-
- def __init__(self, case_name="tempest_custom"):
- TempestCommon.__init__(self, case_name)
- self.MODE = "custom"
- self.OPTION = "--concurrency 1"
+ """Tempest custom testcase implementation."""
+ def __init__(self, **kwargs):
+ if "case_name" not in kwargs:
+ kwargs["case_name"] = 'tempest_custom'
+ TempestCommon.__init__(self, **kwargs)
+ self.mode = "custom"
+ self.option = ["--concurrency", "1"]
class TempestDefcore(TempestCommon):
-
- def __init__(self, case_name="tempest_defcore"):
- TempestCommon.__init__(self, case_name)
- self.MODE = "defcore"
- self.OPTION = "--concurrency 1"
+ """Tempest Defcore testcase implementation."""
+ def __init__(self, **kwargs):
+ if "case_name" not in kwargs:
+ kwargs["case_name"] = 'tempest_defcore'
+ TempestCommon.__init__(self, **kwargs)
+ self.mode = "defcore"
+ self.option = ["--concurrency", "1"]
+
+
+class TempestResourcesManager(object):
+ # pylint: disable=too-many-instance-attributes
+ """Tempest resource manager."""
+ def __init__(self):
+ self.guid = '-' + str(uuid.uuid4())
+ self.cloud = os_client_config.make_shade()
+ LOGGER.debug("cloud: %s", self.cloud)
+ self.domain = self.cloud.auth.get("project_domain_name", "Default")
+ LOGGER.debug("domain: %s", self.domain)
+ self.project = None
+ self.user = None
+ self.network = None
+ self.subnet = None
+ self.image = None
+ self.image_alt = None
+ self.flavor = None
+ self.flavor_alt = None
+
+ def _create_project(self):
+ """Create project for tests."""
+ self.project = self.cloud.create_project(
+ getattr(config.CONF, 'tempest_identity_tenant_name') + self.guid,
+ description=getattr(
+ config.CONF, 'tempest_identity_tenant_description'),
+ domain_id=self.domain)
+ LOGGER.debug("project: %s", self.project)
+
+ def _create_user(self):
+ """Create user for tests."""
+ self.user = self.cloud.create_user(
+ name=getattr(
+ config.CONF, 'tempest_identity_user_name') + self.guid,
+ password=getattr(config.CONF, 'tempest_identity_user_password'),
+ default_project=getattr(
+ config.CONF, 'tempest_identity_tenant_name') + self.guid,
+ domain_id=self.domain)
+ LOGGER.debug("user: %s", self.user)
+
+ def _create_network(self):
+ """Create network for tests."""
+ tempest_net_name = getattr(
+ config.CONF, 'tempest_private_net_name') + self.guid
+ provider = {}
+ if hasattr(config.CONF, 'tempest_network_type'):
+ provider["network_type"] = getattr(
+ config.CONF, 'tempest_network_type')
+ if hasattr(config.CONF, 'tempest_physical_network'):
+ provider["physical_network"] = getattr(
+ config.CONF, 'tempest_physical_network')
+ if hasattr(config.CONF, 'tempest_segmentation_id'):
+ provider["segmentation_id"] = getattr(
+ config.CONF, 'tempest_segmentation_id')
+ LOGGER.info(
+ "Creating network with name: '%s'", tempest_net_name)
+ self.network = self.cloud.create_network(
+ tempest_net_name, provider=provider)
+ LOGGER.debug("network: %s", self.network)
+
+ self.subnet = self.cloud.create_subnet(
+ self.network.id,
+ subnet_name=getattr(
+ config.CONF, 'tempest_private_subnet_name') + self.guid,
+ cidr=getattr(config.CONF, 'tempest_private_subnet_cidr'),
+ enable_dhcp=True,
+ dns_nameservers=[env.get('NAMESERVER')])
+ LOGGER.debug("subnet: %s", self.subnet)
+
+ def _create_image(self, name):
+ """Create image for tests"""
+ LOGGER.info("Creating image with name: '%s'", name)
+ meta = getattr(config.CONF, 'openstack_extra_properties', None)
+ image = self.cloud.create_image(
+ name, filename=getattr(config.CONF, 'openstack_image_url'),
+ is_public=True, meta=meta)
+ LOGGER.debug("image: %s", image)
+ return image
+
+ def _create_flavor(self, name):
+ """Create flavor for tests."""
+ flavor = self.cloud.create_flavor(
+ name, getattr(config.CONF, 'openstack_flavor_ram'),
+ getattr(config.CONF, 'openstack_flavor_vcpus'),
+ getattr(config.CONF, 'openstack_flavor_disk'))
+ self.cloud.set_flavor_specs(
+ flavor.id, getattr(config.CONF, 'flavor_extra_specs', {}))
+ LOGGER.debug("flavor: %s", flavor)
+ return flavor
+
+ def create(self, create_project=False):
+ """Create resources for Tempest test suite."""
+ if create_project:
+ self._create_project()
+ self._create_user()
+ self._create_network()
+
+ LOGGER.debug("Creating two images for Tempest suite")
+ self.image = self._create_image(
+ getattr(config.CONF, 'openstack_image_name') + self.guid)
+ self.image_alt = self._create_image(
+ getattr(config.CONF, 'openstack_image_name_alt') + self.guid)
+
+ LOGGER.info("Creating two flavors for Tempest suite")
+ self.flavor = self._create_flavor(
+ getattr(config.CONF, 'openstack_flavor_name') + self.guid)
+ self.flavor_alt = self._create_flavor(
+ getattr(config.CONF, 'openstack_flavor_name_alt') + self.guid)
+
+ def cleanup(self):
+ """
+ Cleanup all OpenStack objects. Should be called on completion.
+ """
+ self.cloud.delete_image(self.image)
+ self.cloud.delete_image(self.image_alt)
+ self.cloud.delete_network(self.network.id)
+ self.cloud.delete_flavor(self.flavor.id)
+ self.cloud.delete_flavor(self.flavor_alt.id)
+ if self.project:
+ self.cloud.delete_user(self.user.id)
+ self.cloud.delete_project(self.project.id)