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
22 from six.moves import configparser
23 from xtesting.core import testcase
26 from functest.core import singlevm
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(singlevm.VmReady2):
35 # pylint: disable=too-many-instance-attributes
36 """TempestCommon testcases implementation class."""
40 filename_alt = '/home/opnfv/functest/images/cirros-0.4.0-x86_64-disk.img'
42 def __init__(self, **kwargs):
43 if "case_name" not in kwargs:
44 kwargs["case_name"] = 'tempest'
45 super(TempestCommon, self).__init__(**kwargs)
46 assert self.orig_cloud
49 if self.orig_cloud.get_role("admin"):
51 elif self.orig_cloud.get_role("Admin"):
54 raise Exception("Cannot detect neither admin nor Admin")
55 self.orig_cloud.grant_role(
56 role_name, user=self.project.user.id,
57 project=self.project.project.id,
58 domain=self.project.domain.id)
61 OS_USERNAME=self.project.user.name,
62 OS_PROJECT_NAME=self.project.project.name,
63 OS_PROJECT_ID=self.project.project.id,
64 OS_PASSWORD=self.project.password)
65 self.deployment_id = conf_utils.create_rally_deployment(
67 if not self.deployment_id:
68 raise Exception("Deployment create failed")
69 self.verifier_id = conf_utils.create_verifier()
70 if not self.verifier_id:
71 raise Exception("Verifier create failed")
72 self.verifier_repo_dir = conf_utils.get_verifier_repo_dir(
74 self.deployment_dir = conf_utils.get_verifier_deployment_dir(
75 self.verifier_id, self.deployment_id)
76 self.verification_id = None
77 self.res_dir = os.path.join(
78 getattr(config.CONF, 'dir_results'), self.case_name)
79 self.raw_list = os.path.join(self.res_dir, 'test_raw_list.txt')
80 self.list = os.path.join(self.res_dir, 'test_list.txt')
83 self.flavor_alt = None
86 self.services = kwargs['run']['args']['services']
87 except Exception: # pylint: disable=broad-except
89 self.neutron_extensions = []
91 self.neutron_extensions = kwargs['run']['args'][
93 except Exception: # pylint: disable=broad-except
96 def check_services(self):
97 """Check the mandatory services."""
98 for service in self.services:
100 self.cloud.search_services(service)[0]
101 except Exception: # pylint: disable=broad-except
102 self.is_skipped = True
105 def check_extensions(self):
106 """Check the mandatory network extensions."""
107 extensions = self.cloud.get_network_extensions()
108 for network_extension in self.neutron_extensions:
109 if network_extension not in extensions:
111 "Cannot find Neutron extension: %s", network_extension)
112 self.is_skipped = True
115 def check_requirements(self):
116 self.check_services()
117 self.check_extensions()
122 def read_file(filename):
123 """Read file and return content as a stripped list."""
124 with open(filename) as src:
125 return [line.strip() for line in src.readlines()]
128 def get_verifier_result(verif_id):
129 """Retrieve verification results."""
136 cmd = ["rally", "verify", "show", "--uuid", verif_id]
137 LOGGER.info("Showing result for a verification: '%s'.", cmd)
138 proc = subprocess.Popen(cmd,
139 stdout=subprocess.PIPE,
140 stderr=subprocess.STDOUT)
141 for line in proc.stdout:
142 LOGGER.info(line.rstrip())
143 new_line = line.replace(' ', '').split('|')
144 if 'Tests' in new_line:
146 if 'Testscount' in new_line:
147 result['num_tests'] = int(new_line[2])
148 elif 'Success' in new_line:
149 result['num_success'] = int(new_line[2])
150 elif 'Skipped' in new_line:
151 result['num_skipped'] = int(new_line[2])
152 elif 'Failures' in new_line:
153 result['num_failures'] = int(new_line[2])
157 def backup_tempest_config(conf_file, res_dir):
159 Copy config file to tempest results directory
161 if not os.path.exists(res_dir):
163 shutil.copyfile(conf_file,
164 os.path.join(res_dir, 'tempest.conf'))
166 def generate_test_list(self, **kwargs):
167 """Generate test list based on the test mode."""
168 LOGGER.debug("Generating test case list...")
169 self.backup_tempest_config(self.conf_file, '/etc')
170 if kwargs.get('mode') == 'custom':
171 if os.path.isfile(conf_utils.TEMPEST_CUSTOM):
173 conf_utils.TEMPEST_CUSTOM, self.list)
175 raise Exception("Tempest test list file %s NOT found."
176 % conf_utils.TEMPEST_CUSTOM)
178 testr_mode = kwargs.get(
179 'mode', r'^tempest\.(api|scenario).*\[.*\bsmoke\b.*\]$')
180 cmd = "(cd {0}; stestr list '{1}' >{2} 2>/dev/null)".format(
181 self.verifier_repo_dir, testr_mode, self.list)
182 output = subprocess.check_output(cmd, shell=True)
183 LOGGER.info("%s\n%s", cmd, output)
184 os.remove('/etc/tempest.conf')
186 def apply_tempest_blacklist(self):
187 """Exclude blacklisted test cases."""
188 LOGGER.debug("Applying tempest blacklist...")
189 if os.path.exists(self.raw_list):
190 os.remove(self.raw_list)
191 os.rename(self.list, self.raw_list)
192 cases_file = self.read_file(self.raw_list)
193 result_file = open(self.list, 'w')
196 deploy_scenario = env.get('DEPLOY_SCENARIO')
197 if bool(deploy_scenario):
198 # if DEPLOY_SCENARIO is set we read the file
199 black_list_file = open(conf_utils.TEMPEST_BLACKLIST)
200 black_list_yaml = yaml.safe_load(black_list_file)
201 black_list_file.close()
202 for item in black_list_yaml:
203 scenarios = item['scenarios']
204 if deploy_scenario in scenarios:
205 tests = item['tests']
207 black_tests.append(test)
209 except Exception: # pylint: disable=broad-except
211 LOGGER.debug("Tempest blacklist file does not exist.")
213 for cases_line in cases_file:
214 for black_tests_line in black_tests:
215 if black_tests_line in cases_line:
218 result_file.write(str(cases_line) + '\n')
221 def run_verifier_tests(self, **kwargs):
222 """Execute tempest test cases."""
223 cmd = ["rally", "verify", "start", "--load-list",
225 cmd.extend(kwargs.get('option', []))
226 LOGGER.info("Starting Tempest test suite: '%s'.", cmd)
229 os.path.join(self.res_dir, "tempest.log"), 'w+')
231 proc = subprocess.Popen(
233 stdout=subprocess.PIPE,
234 stderr=subprocess.STDOUT,
238 for line in iter(proc.stdout.readline, b''):
239 if re.search(r"\} tempest\.", line):
240 LOGGER.info(line.rstrip())
241 elif re.search(r'(?=\(UUID=(.*)\))', line):
242 self.verification_id = re.search(
243 r'(?=\(UUID=(.*)\))', line).group(1)
248 if self.verification_id is None:
249 raise Exception('Verification UUID not found')
250 LOGGER.info('Verification UUID: %s', self.verification_id)
253 "{}/tempest.log".format(self.deployment_dir),
254 "{}/tempest.debug.log".format(self.res_dir))
256 def parse_verifier_result(self):
257 """Parse and save test results."""
258 stat = self.get_verifier_result(self.verification_id)
260 num_executed = stat['num_tests'] - stat['num_skipped']
262 self.result = 100 * stat['num_success'] / num_executed
263 except ZeroDivisionError:
265 if stat['num_tests'] > 0:
266 LOGGER.info("All tests have been skipped")
268 LOGGER.error("No test has been executed")
271 with open(os.path.join(self.res_dir,
272 "tempest.log"), 'r') as logfile:
273 output = logfile.read()
275 success_testcases = []
276 for match in re.findall(r'.*\{\d{1,2}\} (.*?) \.{3} success ',
278 success_testcases.append(match)
279 failed_testcases = []
280 for match in re.findall(r'.*\{\d{1,2}\} (.*?) \.{3} fail',
282 failed_testcases.append(match)
283 skipped_testcases = []
284 for match in re.findall(r'.*\{\d{1,2}\} (.*?) \.{3} skip:',
286 skipped_testcases.append(match)
288 self.details = {"tests_number": stat['num_tests'],
289 "success_number": stat['num_success'],
290 "skipped_number": stat['num_skipped'],
291 "failures_number": stat['num_failures'],
292 "success": success_testcases,
293 "skipped": skipped_testcases,
294 "failures": failed_testcases}
295 except Exception: # pylint: disable=broad-except
298 LOGGER.info("Tempest %s success_rate is %s%%",
299 self.case_name, self.result)
301 def generate_report(self):
302 """Generate verification report."""
303 html_file = os.path.join(self.res_dir,
304 "tempest-report.html")
305 cmd = ["rally", "verify", "report", "--type", "html", "--uuid",
306 self.verification_id, "--to", html_file]
307 subprocess.Popen(cmd, stdout=subprocess.PIPE,
308 stderr=subprocess.STDOUT)
310 def update_rally_regex(self, rally_conf='/etc/rally/rally.conf'):
311 """Set image name as tempest img_name_regex"""
312 rconfig = configparser.RawConfigParser()
313 rconfig.read(rally_conf)
314 if not rconfig.has_section('openstack'):
315 rconfig.add_section('openstack')
316 rconfig.set('openstack', 'img_name_regex', '^{}$'.format(
318 with open(rally_conf, 'wb') as config_file:
319 rconfig.write(config_file)
321 def update_default_role(self, rally_conf='/etc/rally/rally.conf'):
322 """Detect and update the default role if required"""
323 role = self.get_default_role(self.cloud)
326 rconfig = configparser.RawConfigParser()
327 rconfig.read(rally_conf)
328 if not rconfig.has_section('openstack'):
329 rconfig.add_section('openstack')
330 rconfig.set('openstack', 'swift_operator_role', role.name)
331 with open(rally_conf, 'wb') as config_file:
332 rconfig.write(config_file)
334 def update_rally_logs(self, rally_conf='/etc/rally/rally.conf'):
335 """Print rally logs in res dir"""
336 if not os.path.exists(self.res_dir):
337 os.makedirs(self.res_dir)
338 rconfig = configparser.RawConfigParser()
339 rconfig.read(rally_conf)
340 rconfig.set('DEFAULT', 'log-file', 'rally.log')
341 rconfig.set('DEFAULT', 'log_dir', self.res_dir)
342 with open(rally_conf, 'wb') as config_file:
343 rconfig.write(config_file)
346 def clean_rally_conf(rally_conf='/etc/rally/rally.conf'):
347 """Clean Rally config"""
348 rconfig = configparser.RawConfigParser()
349 rconfig.read(rally_conf)
350 if rconfig.has_option('openstack', 'img_name_regex'):
351 rconfig.remove_option('openstack', 'img_name_regex')
352 if rconfig.has_option('openstack', 'swift_operator_role'):
353 rconfig.remove_option('openstack', 'swift_operator_role')
354 if rconfig.has_option('DEFAULT', 'log-file'):
355 rconfig.remove_option('DEFAULT', 'log-file')
356 if rconfig.has_option('DEFAULT', 'log_dir'):
357 rconfig.remove_option('DEFAULT', 'log_dir')
358 with open(rally_conf, 'wb') as config_file:
359 rconfig.write(config_file)
361 def update_scenario_section(self):
362 """Update scenario section in tempest.conf"""
363 rconfig = configparser.RawConfigParser()
364 rconfig.read(self.conf_file)
366 config.CONF, '{}_image'.format(self.case_name), self.filename)
367 if not rconfig.has_section('scenario'):
368 rconfig.add_section('scenario')
369 rconfig.set('scenario', 'img_file', os.path.basename(filename))
370 rconfig.set('scenario', 'img_dir', os.path.dirname(filename))
371 rconfig.set('scenario', 'img_disk_format', getattr(
372 config.CONF, '{}_image_format'.format(self.case_name),
374 extra_properties = self.extra_properties.copy()
375 extra_properties.update(
376 getattr(config.CONF, '{}_extra_properties'.format(
377 self.case_name), {}))
379 'scenario', 'img_properties',
380 conf_utils.convert_dict_to_ini(extra_properties))
381 with open(self.conf_file, 'wb') as config_file:
382 rconfig.write(config_file)
384 def configure(self, **kwargs): # pylint: disable=unused-argument
386 Create all openstack resources for tempest-based testcases and write
389 if not os.path.exists(self.res_dir):
390 os.makedirs(self.res_dir)
391 compute_cnt = len(self.cloud.list_hypervisors())
393 self.image_alt = self.publish_image_alt()
394 self.flavor_alt = self.create_flavor_alt()
395 LOGGER.debug("flavor: %s", self.flavor_alt)
397 self.conf_file = conf_utils.configure_verifier(self.deployment_dir)
398 if not self.conf_file:
399 raise Exception("Tempest verifier configuring failed")
400 conf_utils.configure_tempest_update_params(
401 self.conf_file, network_name=self.network.name,
402 image_id=self.image.id,
403 flavor_id=self.flavor.id,
404 compute_cnt=compute_cnt,
405 image_alt_id=self.image_alt.id,
406 flavor_alt_id=self.flavor_alt.id,
407 domain_name=self.cloud.auth.get("project_domain_name", "Default"))
408 self.update_scenario_section()
409 self.backup_tempest_config(self.conf_file, self.res_dir)
411 def run(self, **kwargs):
412 self.start_time = time.time()
414 assert super(TempestCommon, self).run(
415 **kwargs) == testcase.TestCase.EX_OK
416 if not os.path.exists(self.res_dir):
417 os.makedirs(self.res_dir)
418 self.update_rally_regex()
419 self.update_default_role()
420 self.update_rally_logs()
421 shutil.copy("/etc/rally/rally.conf", self.res_dir)
422 self.configure(**kwargs)
423 self.generate_test_list(**kwargs)
424 self.apply_tempest_blacklist()
425 self.run_verifier_tests(**kwargs)
426 self.parse_verifier_result()
427 self.generate_report()
428 res = testcase.TestCase.EX_OK
429 except Exception: # pylint: disable=broad-except
430 LOGGER.exception('Error with run')
432 res = testcase.TestCase.EX_RUN_ERROR
433 self.stop_time = time.time()
438 Cleanup all OpenStack objects. Should be called on completion.
440 self.clean_rally_conf()
442 self.cloud.delete_image(self.image_alt)
444 self.orig_cloud.delete_flavor(self.flavor_alt.id)
445 super(TempestCommon, self).clean()