3 # Copyright (c) 2015 All rights reserved
4 # This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
11 """Tempest testcases implementation."""
13 from __future__ import division
23 import os_client_config
24 from xtesting.core import testcase
27 from functest.opnfv_tests.openstack.tempest import conf_utils
28 from functest.utils import config
29 from functest.utils import env
31 LOGGER = logging.getLogger(__name__)
34 class TempestCommon(testcase.TestCase):
35 # pylint: disable=too-many-instance-attributes
36 """TempestCommon testcases implementation class."""
38 TEMPEST_RESULTS_DIR = os.path.join(
39 getattr(config.CONF, 'dir_results'), 'tempest')
41 def __init__(self, **kwargs):
42 super(TempestCommon, self).__init__(**kwargs)
43 self.resources = TempestResourcesManager()
46 self.verifier_id = conf_utils.get_verifier_id()
47 self.verifier_repo_dir = conf_utils.get_verifier_repo_dir(
49 self.deployment_id = conf_utils.get_verifier_deployment_id()
50 self.deployment_dir = conf_utils.get_verifier_deployment_dir(
51 self.verifier_id, self.deployment_id)
52 self.verification_id = None
53 self.res_dir = TempestCommon.TEMPEST_RESULTS_DIR
54 self.raw_list = os.path.join(self.res_dir, 'test_raw_list.txt')
55 self.list = os.path.join(self.res_dir, 'test_list.txt')
59 def read_file(filename):
60 """Read file and return content as a stripped list."""
61 with open(filename) as src:
62 return [line.strip() for line in src.readlines()]
65 def get_verifier_result(verif_id):
66 """Retrieve verification results."""
73 cmd = ["rally", "verify", "show", "--uuid", verif_id]
74 LOGGER.info("Showing result for a verification: '%s'.", cmd)
75 proc = subprocess.Popen(cmd,
76 stdout=subprocess.PIPE,
77 stderr=subprocess.STDOUT)
78 for line in proc.stdout:
79 new_line = line.replace(' ', '').split('|')
80 if 'Tests' in new_line:
83 if 'Testscount' in new_line:
84 result['num_tests'] = int(new_line[2])
85 elif 'Success' in new_line:
86 result['num_success'] = int(new_line[2])
87 elif 'Skipped' in new_line:
88 result['num_skipped'] = int(new_line[2])
89 elif 'Failures' in new_line:
90 result['num_failures'] = int(new_line[2])
94 def backup_tempest_config(conf_file, res_dir):
96 Copy config file to tempest results directory
98 if not os.path.exists(res_dir):
100 shutil.copyfile(conf_file,
101 os.path.join(res_dir, 'tempest.conf'))
103 def generate_test_list(self):
104 """Generate test list based on the test mode."""
105 LOGGER.debug("Generating test case list...")
106 if self.mode == 'custom':
107 if os.path.isfile(conf_utils.TEMPEST_CUSTOM):
109 conf_utils.TEMPEST_CUSTOM, self.list)
111 raise Exception("Tempest test list file %s NOT found."
112 % conf_utils.TEMPEST_CUSTOM)
114 if self.mode == 'smoke':
115 testr_mode = r"'^tempest\.(api|scenario).*\[.*\bsmoke\b.*\]$'"
116 elif self.mode == 'full':
117 testr_mode = r"'^tempest\.'"
119 testr_mode = self.mode
120 cmd = "(cd {0}; stestr list {1} >{2} 2>/dev/null)".format(
121 self.verifier_repo_dir, testr_mode, self.list)
122 output = subprocess.check_output(cmd, shell=True)
123 LOGGER.info("%s\n%s", cmd, output)
125 def apply_tempest_blacklist(self):
126 """Exclude blacklisted test cases."""
127 LOGGER.debug("Applying tempest blacklist...")
128 if os.path.exists(self.raw_list):
129 os.remove(self.raw_list)
130 os.rename(self.list, self.raw_list)
131 cases_file = self.read_file(self.raw_list)
132 result_file = open(self.list, 'w')
135 installer_type = env.get('INSTALLER_TYPE')
136 deploy_scenario = env.get('DEPLOY_SCENARIO')
137 if bool(installer_type) * bool(deploy_scenario):
138 # if INSTALLER_TYPE and DEPLOY_SCENARIO are set we read the
140 black_list_file = open(conf_utils.TEMPEST_BLACKLIST)
141 black_list_yaml = yaml.safe_load(black_list_file)
142 black_list_file.close()
143 for item in black_list_yaml:
144 scenarios = item['scenarios']
145 installers = item['installers']
146 if (deploy_scenario in scenarios and
147 installer_type in installers):
148 tests = item['tests']
150 black_tests.append(test)
152 except Exception: # pylint: disable=broad-except
154 LOGGER.debug("Tempest blacklist file does not exist.")
156 for cases_line in cases_file:
157 for black_tests_line in black_tests:
158 if black_tests_line in cases_line:
161 result_file.write(str(cases_line) + '\n')
164 def run_verifier_tests(self):
165 """Execute tempest test cases."""
166 cmd = ["rally", "verify", "start", "--load-list",
168 cmd.extend(self.option)
169 LOGGER.info("Starting Tempest test suite: '%s'.", cmd)
172 os.path.join(self.res_dir, "tempest.log"), 'w+')
174 os.path.join(self.res_dir,
175 "tempest-error.log"), 'w+')
177 proc = subprocess.Popen(
179 stdout=subprocess.PIPE,
184 for line in iter(proc.stdout.readline, b''):
185 if re.search(r"\} tempest\.", line):
186 LOGGER.info(line.replace('\n', ''))
187 elif re.search(r'(?=\(UUID=(.*)\))', line):
188 self.verification_id = re.search(
189 r'(?=\(UUID=(.*)\))', line).group(1)
190 LOGGER.info('Verification UUID: %s', self.verification_id)
197 if self.verification_id is None:
198 raise Exception('Verification UUID not found')
200 def parse_verifier_result(self):
201 """Parse and save test results."""
202 stat = self.get_verifier_result(self.verification_id)
204 num_executed = stat['num_tests'] - stat['num_skipped']
206 self.result = 100 * stat['num_success'] / num_executed
207 except ZeroDivisionError:
209 if stat['num_tests'] > 0:
210 LOGGER.info("All tests have been skipped")
212 LOGGER.error("No test has been executed")
215 with open(os.path.join(self.res_dir,
216 "tempest.log"), 'r') as logfile:
217 output = logfile.read()
219 success_testcases = []
220 for match in re.findall(r'.*\{0\} (.*?)[. ]*success ', output):
221 success_testcases.append(match)
222 failed_testcases = []
223 for match in re.findall(r'.*\{0\} (.*?)[. ]*fail', output):
224 failed_testcases.append(match)
225 skipped_testcases = []
226 for match in re.findall(r'.*\{0\} (.*?)[. ]*skip:', output):
227 skipped_testcases.append(match)
229 self.details = {"tests_number": stat['num_tests'],
230 "success_number": stat['num_success'],
231 "skipped_number": stat['num_skipped'],
232 "failures_number": stat['num_failures'],
233 "success": success_testcases,
234 "skipped": skipped_testcases,
235 "failures": failed_testcases}
236 except Exception: # pylint: disable=broad-except
239 LOGGER.info("Tempest %s success_rate is %s%%",
240 self.case_name, self.result)
242 def generate_report(self):
243 """Generate verification report."""
244 html_file = os.path.join(self.res_dir,
245 "tempest-report.html")
246 cmd = ["rally", "verify", "report", "--type", "html", "--uuid",
247 self.verification_id, "--to", html_file]
248 subprocess.Popen(cmd, stdout=subprocess.PIPE,
249 stderr=subprocess.STDOUT)
251 def configure(self, **kwargs): # pylint: disable=unused-argument
253 Create all openstack resources for tempest-based testcases and write
256 if not os.path.exists(self.res_dir):
257 os.makedirs(self.res_dir)
258 self.resources.create()
259 compute_cnt = len(self.resources.cloud.list_hypervisors())
260 self.conf_file = conf_utils.configure_verifier(self.deployment_dir)
261 conf_utils.configure_tempest_update_params(
262 self.conf_file, network_name=self.resources.network.id,
263 image_id=self.resources.image.id,
264 flavor_id=self.resources.flavor.id,
265 compute_cnt=compute_cnt,
266 image_alt_id=self.resources.image_alt.id,
267 flavor_alt_id=self.resources.flavor_alt.id)
268 self.backup_tempest_config(self.conf_file, self.res_dir)
270 def run(self, **kwargs):
271 self.start_time = time.time()
273 self.configure(**kwargs)
274 self.generate_test_list()
275 self.apply_tempest_blacklist()
276 self.run_verifier_tests()
277 self.parse_verifier_result()
278 self.generate_report()
279 res = testcase.TestCase.EX_OK
280 except Exception: # pylint: disable=broad-except
281 LOGGER.exception('Error with run')
282 res = testcase.TestCase.EX_RUN_ERROR
284 self.resources.cleanup()
285 self.stop_time = time.time()
289 class TempestSmokeSerial(TempestCommon):
290 """Tempest smoke serial testcase implementation."""
291 def __init__(self, **kwargs):
292 if "case_name" not in kwargs:
293 kwargs["case_name"] = 'tempest_smoke_serial'
294 TempestCommon.__init__(self, **kwargs)
296 self.option = ["--concurrency", "1"]
299 class TempestNeutronTrunk(TempestCommon):
300 """Tempest neutron trunk testcase implementation."""
301 def __init__(self, **kwargs):
302 if "case_name" not in kwargs:
303 kwargs["case_name"] = 'neutron_trunk'
304 TempestCommon.__init__(self, **kwargs)
305 self.mode = "'neutron_tempest_plugin.(api|scenario).test_trunk'"
306 self.res_dir = os.path.join(
307 getattr(config.CONF, 'dir_results'), 'neutron_trunk')
308 self.raw_list = os.path.join(self.res_dir, 'test_raw_list.txt')
309 self.list = os.path.join(self.res_dir, 'test_list.txt')
311 def configure(self, **kwargs):
312 super(TempestNeutronTrunk, self).configure(**kwargs)
313 rconfig = conf_utils.ConfigParser.RawConfigParser()
314 rconfig.read(self.conf_file)
315 rconfig.set('network-feature-enabled', 'api_extensions', 'all')
316 with open(self.conf_file, 'wb') as config_file:
317 rconfig.write(config_file)
320 class TempestSmokeParallel(TempestCommon):
321 """Tempest smoke parallel testcase implementation."""
322 def __init__(self, **kwargs):
323 if "case_name" not in kwargs:
324 kwargs["case_name"] = 'tempest_smoke_parallel'
325 TempestCommon.__init__(self, **kwargs)
329 class TempestFullParallel(TempestCommon):
330 """Tempest full parallel testcase implementation."""
331 def __init__(self, **kwargs):
332 if "case_name" not in kwargs:
333 kwargs["case_name"] = 'tempest_full_parallel'
334 TempestCommon.__init__(self, **kwargs)
338 class TempestCustom(TempestCommon):
339 """Tempest custom testcase implementation."""
340 def __init__(self, **kwargs):
341 if "case_name" not in kwargs:
342 kwargs["case_name"] = 'tempest_custom'
343 TempestCommon.__init__(self, **kwargs)
345 self.option = ["--concurrency", "1"]
348 class TempestDefcore(TempestCommon):
349 """Tempest Defcore testcase implementation."""
350 def __init__(self, **kwargs):
351 if "case_name" not in kwargs:
352 kwargs["case_name"] = 'tempest_defcore'
353 TempestCommon.__init__(self, **kwargs)
354 self.mode = "defcore"
355 self.option = ["--concurrency", "1"]
358 class TempestResourcesManager(object):
359 # pylint: disable=too-many-instance-attributes
360 """Tempest resource manager."""
362 self.guid = '-' + str(uuid.uuid4())
363 self.cloud = os_client_config.make_shade()
364 self.domain_id = self.cloud.auth["project_domain_id"]
370 self.image_alt = None
372 self.flavor_alt = None
374 def _create_project(self):
375 """Create project for tests."""
376 self.project = self.cloud.create_project(
377 getattr(config.CONF, 'tempest_identity_tenant_name') + self.guid,
379 config.CONF, 'tempest_identity_tenant_description'),
380 domain_id=self.domain_id)
381 LOGGER.debug("project: %s", self.project)
383 def _create_user(self):
384 """Create user for tests."""
385 self.user = self.cloud.create_user(
387 config.CONF, 'tempest_identity_user_name') + self.guid,
388 password=getattr(config.CONF, 'tempest_identity_user_password'),
389 default_project=getattr(
390 config.CONF, 'tempest_identity_tenant_name') + self.guid,
391 domain_id=self.domain_id)
392 LOGGER.debug("user: %s", self.user)
394 def _create_network(self):
395 """Create network for tests."""
396 tempest_net_name = getattr(
397 config.CONF, 'tempest_private_net_name') + self.guid
399 if hasattr(config.CONF, 'tempest_network_type'):
400 provider["network_type"] = getattr(
401 config.CONF, 'tempest_network_type')
402 if hasattr(config.CONF, 'tempest_physical_network'):
403 provider["physical_network"] = getattr(
404 config.CONF, 'tempest_physical_network')
405 if hasattr(config.CONF, 'tempest_segmentation_id'):
406 provider["segmentation_id"] = getattr(
407 config.CONF, 'tempest_segmentation_id')
409 "Creating network with name: '%s'", tempest_net_name)
410 self.network = self.cloud.create_network(
411 tempest_net_name, provider=provider)
412 LOGGER.debug("network: %s", self.network)
414 self.subnet = self.cloud.create_subnet(
417 config.CONF, 'tempest_private_subnet_name') + self.guid,
418 cidr=getattr(config.CONF, 'tempest_private_subnet_cidr'),
420 dns_nameservers=[env.get('NAMESERVER')])
421 LOGGER.debug("subnet: %s", self.subnet)
423 def _create_image(self, name):
424 """Create image for tests"""
425 LOGGER.info("Creating image with name: '%s'", name)
426 image = self.cloud.create_image(
427 name, filename=getattr(config.CONF, 'openstack_image_url'),
429 LOGGER.debug("image: %s", image)
432 def _create_flavor(self, name):
433 """Create flavor for tests."""
434 flavor = self.cloud.create_flavor(
435 name, getattr(config.CONF, 'openstack_flavor_ram'),
436 getattr(config.CONF, 'openstack_flavor_vcpus'),
437 getattr(config.CONF, 'openstack_flavor_disk'))
438 self.cloud.set_flavor_specs(
439 flavor.id, getattr(config.CONF, 'flavor_extra_specs', {}))
440 LOGGER.debug("flavor: %s", flavor)
443 def create(self, create_project=False):
444 """Create resources for Tempest test suite."""
446 self._create_project()
448 self._create_network()
450 LOGGER.debug("Creating two images for Tempest suite")
451 self.image = self._create_image(
452 getattr(config.CONF, 'openstack_image_name') + self.guid)
453 self.image_alt = self._create_image(
454 getattr(config.CONF, 'openstack_image_name_alt') + self.guid)
456 LOGGER.info("Creating two flavors for Tempest suite")
457 self.flavor = self._create_flavor(
458 getattr(config.CONF, 'openstack_flavor_name') + self.guid)
459 self.flavor_alt = self._create_flavor(
460 getattr(config.CONF, 'openstack_flavor_name_alt') + self.guid)
464 Cleanup all OpenStack objects. Should be called on completion.
466 self.cloud.delete_image(self.image)
467 self.cloud.delete_image(self.image_alt)
468 self.cloud.delete_network(self.network.id)
469 self.cloud.delete_flavor(self.flavor.id)
470 self.cloud.delete_flavor(self.flavor_alt.id)
472 self.cloud.delete_user(self.user.id)
473 self.cloud.delete_project(self.project.id)