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 """Rally testcases implementation."""
13 from __future__ import division
25 from snaps.config.flavor import FlavorConfig
26 from snaps.config.image import ImageConfig
27 from snaps.config.network import NetworkConfig, SubnetConfig
28 from snaps.config.router import RouterConfig
29 from snaps.openstack.create_flavor import OpenStackFlavor
30 from snaps.openstack.utils import deploy_utils
31 from xtesting.core import testcase
32 from xtesting.energy import energy
35 from functest.opnfv_tests.openstack.snaps import snaps_utils
36 from functest.opnfv_tests.openstack.tempest import conf_utils
37 from functest.utils import config
38 from functest.utils import env
40 LOGGER = logging.getLogger(__name__)
43 class RallyBase(testcase.TestCase):
44 """Base class form Rally testcases implementation."""
46 # pylint: disable=too-many-instance-attributes
47 TESTS = ['authenticate', 'glance', 'cinder', 'heat',
48 'keystone', 'neutron', 'nova', 'quotas', 'vm', 'all']
49 GLANCE_IMAGE_NAME = getattr(config.CONF, 'openstack_image_name')
50 GLANCE_IMAGE_FILENAME = getattr(config.CONF, 'openstack_image_file_name')
51 GLANCE_IMAGE_PATH = os.path.join(getattr(
52 config.CONF, 'dir_functest_images'), GLANCE_IMAGE_FILENAME)
53 GLANCE_IMAGE_FORMAT = getattr(config.CONF, 'openstack_image_disk_format')
54 GLANCE_IMAGE_USERNAME = getattr(config.CONF, 'openstack_image_username')
55 GLANCE_IMAGE_EXTRA_PROPERTIES = getattr(
56 config.CONF, 'image_properties', {})
57 FLAVOR_NAME = getattr(config.CONF, 'rally_flavor_name')
58 FLAVOR_ALT_NAME = getattr(config.CONF, 'rally_flavor_alt_name')
61 FLAVOR_EXTRA_SPECS = getattr(config.CONF, 'flavor_extra_specs', None)
62 if FLAVOR_EXTRA_SPECS:
66 RALLY_DIR = pkg_resources.resource_filename(
67 'functest', 'opnfv_tests/openstack/rally')
68 RALLY_SCENARIO_DIR = pkg_resources.resource_filename(
69 'functest', 'opnfv_tests/openstack/rally/scenario')
70 TEMPLATE_DIR = pkg_resources.resource_filename(
71 'functest', 'opnfv_tests/openstack/rally/scenario/templates')
72 SUPPORT_DIR = pkg_resources.resource_filename(
73 'functest', 'opnfv_tests/openstack/rally/scenario/support')
76 ITERATIONS_AMOUNT = 10
78 RESULTS_DIR = os.path.join(getattr(config.CONF, 'dir_results'), 'rally')
79 BLACKLIST_FILE = os.path.join(RALLY_DIR, "blacklist.txt")
80 TEMP_DIR = os.path.join(RALLY_DIR, "var")
82 RALLY_PRIVATE_NET_NAME = getattr(config.CONF, 'rally_network_name')
83 RALLY_PRIVATE_SUBNET_NAME = getattr(config.CONF, 'rally_subnet_name')
84 RALLY_PRIVATE_SUBNET_CIDR = getattr(config.CONF, 'rally_subnet_cidr')
85 RALLY_ROUTER_NAME = getattr(config.CONF, 'rally_router_name')
87 def __init__(self, **kwargs):
88 """Initialize RallyBase object."""
89 super(RallyBase, self).__init__(**kwargs)
90 self.os_creds = kwargs.get('os_creds') or snaps_utils.get_credentials()
91 self.guid = '-' + str(uuid.uuid4())
95 self.scenario_dir = ''
96 self.image_name = None
97 self.ext_net_name = None
98 self.priv_net_id = None
99 self.flavor_name = None
100 self.flavor_alt_name = None
102 self.test_name = None
103 self.start_time = None
108 def _build_task_args(self, test_file_name):
109 """Build arguments for the Rally task."""
110 task_args = {'service_list': [test_file_name]}
111 task_args['image_name'] = str(self.image_name)
112 task_args['flavor_name'] = str(self.flavor_name)
113 task_args['flavor_alt_name'] = str(self.flavor_alt_name)
114 task_args['glance_image_location'] = str(self.GLANCE_IMAGE_PATH)
115 task_args['glance_image_format'] = str(self.GLANCE_IMAGE_FORMAT)
116 task_args['tmpl_dir'] = str(self.TEMPLATE_DIR)
117 task_args['sup_dir'] = str(self.SUPPORT_DIR)
118 task_args['users_amount'] = self.USERS_AMOUNT
119 task_args['tenants_amount'] = self.TENANTS_AMOUNT
120 task_args['use_existing_users'] = False
121 task_args['iterations'] = self.ITERATIONS_AMOUNT
122 task_args['concurrency'] = self.CONCURRENCY
123 task_args['smoke'] = self.smoke
125 ext_net = self.ext_net_name
127 task_args['floating_network'] = str(ext_net)
129 task_args['floating_network'] = ''
131 net_id = self.priv_net_id
133 task_args['netid'] = str(net_id)
135 task_args['netid'] = ''
139 def _prepare_test_list(self, test_name):
140 """Build the list of test cases to be executed."""
141 test_yaml_file_name = 'opnfv-{}.yaml'.format(test_name)
142 scenario_file_name = os.path.join(self.RALLY_SCENARIO_DIR,
145 if not os.path.exists(scenario_file_name):
146 scenario_file_name = os.path.join(self.scenario_dir,
149 if not os.path.exists(scenario_file_name):
150 raise Exception("The scenario '%s' does not exist."
151 % scenario_file_name)
153 LOGGER.debug('Scenario fetched from : %s', scenario_file_name)
154 test_file_name = os.path.join(self.TEMP_DIR, test_yaml_file_name)
156 if not os.path.exists(self.TEMP_DIR):
157 os.makedirs(self.TEMP_DIR)
159 self._apply_blacklist(scenario_file_name, test_file_name)
160 return test_file_name
163 def get_task_id(cmd_raw):
165 Get task id from command rally result.
168 :return: task_id as string
170 taskid_re = re.compile('^Task +(.*): started$')
171 for line in cmd_raw.splitlines(True):
173 match = taskid_re.match(line)
175 return match.group(1)
179 def task_succeed(json_raw):
181 Parse JSON from rally JSON results.
186 rally_report = json.loads(json_raw)
187 for report in rally_report:
188 if report is None or report.get('result') is None:
191 for result in report.get('result'):
192 if result is None or result.get('error'):
197 def _migration_supported(self):
198 """Determine if migration is supported."""
199 if self.compute_cnt > 1:
205 def get_cmd_output(proc):
206 """Get command stdout."""
208 for line in proc.stdout:
214 """Exclude scenario."""
217 with open(RallyBase.BLACKLIST_FILE, 'r') as black_list_file:
218 black_list_yaml = yaml.safe_load(black_list_file)
220 installer_type = env.get('INSTALLER_TYPE')
221 deploy_scenario = env.get('DEPLOY_SCENARIO')
222 if (bool(installer_type) and bool(deploy_scenario) and
223 'scenario' in black_list_yaml.keys()):
224 for item in black_list_yaml['scenario']:
225 scenarios = item['scenarios']
226 installers = item['installers']
227 in_it = RallyBase.in_iterable_re
228 if (in_it(deploy_scenario, scenarios) and
229 in_it(installer_type, installers)):
230 tests = item['tests']
231 black_tests.extend(tests)
232 except Exception: # pylint: disable=broad-except
233 LOGGER.debug("Scenario exclusion not applied.")
238 def in_iterable_re(needle, haystack):
240 Check if given needle is in the iterable haystack, using regex.
242 :param needle: string to be matched
243 :param haystack: iterable of strings (optionally regex patterns)
244 :return: True if needle is eqial to any of the elements in haystack,
245 or if a nonempty regex pattern in haystack is found in needle.
247 # match without regex
248 if needle in haystack:
251 for pattern in haystack:
252 # match if regex pattern is set and found in the needle
253 if pattern and re.search(pattern, needle) is not None:
259 """Exclude functionalities."""
264 with open(RallyBase.BLACKLIST_FILE, 'r') as black_list_file:
265 black_list_yaml = yaml.safe_load(black_list_file)
267 if not self._migration_supported():
268 func_list.append("no_migration")
270 if 'functionality' in black_list_yaml.keys():
271 for item in black_list_yaml['functionality']:
272 functions = item['functions']
273 for func in func_list:
274 if func in functions:
275 tests = item['tests']
276 black_tests.extend(tests)
277 except Exception: # pylint: disable=broad-except
278 LOGGER.debug("Functionality exclusion not applied.")
282 def _apply_blacklist(self, case_file_name, result_file_name):
283 """Apply blacklist."""
284 LOGGER.debug("Applying blacklist...")
285 cases_file = open(case_file_name, 'r')
286 result_file = open(result_file_name, 'w')
288 black_tests = list(set(self.excl_func() +
289 self.excl_scenario()))
292 LOGGER.debug("Blacklisted tests: " + str(black_tests))
295 for cases_line in cases_file:
297 for black_tests_line in black_tests:
298 if re.search(black_tests_line,
299 cases_line.strip().rstrip(':')):
303 result_file.write(str(cases_line))
305 if cases_line.isspace():
312 def file_is_empty(file_name):
313 """Determine is a file is empty."""
315 if os.stat(file_name).st_size > 0:
317 except Exception: # pylint: disable=broad-except
322 def _run_task(self, test_name):
324 LOGGER.info('Starting test scenario "%s" ...', test_name)
326 task_file = os.path.join(self.RALLY_DIR, 'task.yaml')
327 if not os.path.exists(task_file):
328 LOGGER.error("Task file '%s' does not exist.", task_file)
329 raise Exception("Task file '%s' does not exist.", task_file)
331 file_name = self._prepare_test_list(test_name)
332 if self.file_is_empty(file_name):
333 LOGGER.info('No tests for scenario "%s"', test_name)
336 cmd = (["rally", "task", "start", "--abort-on-sla-failure", "--task",
337 task_file, "--task-args",
338 str(self._build_task_args(test_name))])
339 LOGGER.debug('running command: %s', cmd)
341 proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
342 stderr=subprocess.STDOUT)
343 output = self.get_cmd_output(proc)
344 task_id = self.get_task_id(output)
346 LOGGER.debug('task_id : %s', task_id)
349 LOGGER.error('Failed to retrieve task_id, validating task...')
350 cmd = (["rally", "task", "validate", "--task", task_file,
351 "--task-args", str(self._build_task_args(test_name))])
352 LOGGER.debug('running command: %s', cmd)
353 proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
354 stderr=subprocess.STDOUT)
355 output = self.get_cmd_output(proc)
356 LOGGER.error("Task validation result:" + "\n" + output)
359 # check for result directory and create it otherwise
360 if not os.path.exists(self.RESULTS_DIR):
361 LOGGER.debug('%s does not exist, we create it.',
363 os.makedirs(self.RESULTS_DIR)
365 # get and save rally operation JSON result
366 cmd = (["rally", "task", "detailed", task_id])
367 LOGGER.debug('running command: %s', cmd)
368 proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
369 stderr=subprocess.STDOUT)
370 json_detailed = self.get_cmd_output(proc)
371 LOGGER.info('%s', json_detailed)
373 cmd = (["rally", "task", "results", task_id])
374 LOGGER.debug('running command: %s', cmd)
375 proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
376 stderr=subprocess.STDOUT)
377 json_results = self.get_cmd_output(proc)
378 self._append_summary(json_results, test_name)
379 report_json_name = 'opnfv-{}.json'.format(test_name)
380 report_json_dir = os.path.join(self.RESULTS_DIR, report_json_name)
381 with open(report_json_dir, 'w') as r_file:
382 LOGGER.debug('saving json file')
383 r_file.write(json_results)
385 # write html report file
386 report_html_name = 'opnfv-{}.html'.format(test_name)
387 report_html_dir = os.path.join(self.RESULTS_DIR, report_html_name)
388 cmd = (["rally", "task", "report", task_id, "--out", report_html_dir])
389 LOGGER.debug('running command: %s', cmd)
390 subprocess.Popen(cmd, stdout=subprocess.PIPE,
391 stderr=subprocess.STDOUT)
393 # parse JSON operation result
394 if self.task_succeed(json_results):
395 LOGGER.info('Test scenario: "{}" OK.'.format(test_name) + "\n")
397 LOGGER.info('Test scenario: "{}" Failed.'.format(test_name) + "\n")
399 def _append_summary(self, json_raw, test_name):
400 """Update statistics summary info."""
403 overall_duration = 0.0
405 rally_report = json.loads(json_raw)
406 for report in rally_report:
407 if report.get('full_duration'):
408 overall_duration += report.get('full_duration')
410 if report.get('result'):
411 for result in report.get('result'):
413 if not result.get('error'):
416 scenario_summary = {'test_name': test_name,
417 'overall_duration': overall_duration,
418 'nb_tests': nb_tests,
419 'nb_success': nb_success}
420 self.summary.append(scenario_summary)
422 def _prepare_env(self):
423 """Create resources needed by test scenarios."""
424 LOGGER.debug('Validating the test name...')
425 if self.test_name not in self.TESTS:
426 raise Exception("Test name '%s' is invalid" % self.test_name)
428 network_name = self.RALLY_PRIVATE_NET_NAME + self.guid
429 subnet_name = self.RALLY_PRIVATE_SUBNET_NAME + self.guid
430 router_name = self.RALLY_ROUTER_NAME + self.guid
431 self.image_name = self.GLANCE_IMAGE_NAME + self.guid
432 self.flavor_name = self.FLAVOR_NAME + self.guid
433 self.flavor_alt_name = self.FLAVOR_ALT_NAME + self.guid
434 self.ext_net_name = snaps_utils.get_ext_net_name(self.os_creds)
435 self.compute_cnt = snaps_utils.get_active_compute_cnt(self.os_creds)
437 LOGGER.debug("Creating image '%s'...", self.image_name)
438 image_creator = deploy_utils.create_image(
439 self.os_creds, ImageConfig(
440 name=self.image_name,
441 image_file=self.GLANCE_IMAGE_PATH,
442 img_format=self.GLANCE_IMAGE_FORMAT,
443 image_user=self.GLANCE_IMAGE_USERNAME,
445 extra_properties=self.GLANCE_IMAGE_EXTRA_PROPERTIES))
446 if image_creator is None:
447 raise Exception("Failed to create image")
448 self.creators.append(image_creator)
450 LOGGER.debug("Creating network '%s'...", network_name)
452 rally_network_type = getattr(config.CONF, 'rally_network_type', None)
453 rally_physical_network = getattr(
454 config.CONF, 'rally_physical_network', None)
455 rally_segmentation_id = getattr(
456 config.CONF, 'rally_segmentation_id', None)
458 network_creator = deploy_utils.create_network(
459 self.os_creds, NetworkConfig(
462 network_type=rally_network_type,
463 physical_network=rally_physical_network,
464 segmentation_id=rally_segmentation_id,
465 subnet_settings=[SubnetConfig(
467 cidr=self.RALLY_PRIVATE_SUBNET_CIDR,
468 dns_nameservers=[env.get('NAMESERVER')])]))
469 if network_creator is None:
470 raise Exception("Failed to create private network")
471 self.priv_net_id = network_creator.get_network().id
472 self.creators.append(network_creator)
474 LOGGER.debug("Creating router '%s'...", router_name)
475 router_creator = deploy_utils.create_router(
476 self.os_creds, RouterConfig(
478 external_gateway=self.ext_net_name,
479 internal_subnets=[subnet_name]))
480 if router_creator is None:
481 raise Exception("Failed to create router")
482 self.creators.append(router_creator)
484 LOGGER.debug("Creating flavor '%s'...", self.flavor_name)
485 flavor_creator = OpenStackFlavor(
486 self.os_creds, FlavorConfig(
487 name=self.flavor_name, ram=self.FLAVOR_RAM, disk=1, vcpus=1,
488 metadata=self.FLAVOR_EXTRA_SPECS))
489 if flavor_creator is None or flavor_creator.create() is None:
490 raise Exception("Failed to create flavor")
491 self.creators.append(flavor_creator)
493 LOGGER.debug("Creating flavor '%s'...", self.flavor_alt_name)
494 flavor_alt_creator = OpenStackFlavor(
495 self.os_creds, FlavorConfig(
496 name=self.flavor_alt_name, ram=self.FLAVOR_RAM_ALT, disk=1,
497 vcpus=1, metadata=self.FLAVOR_EXTRA_SPECS))
498 if flavor_alt_creator is None or flavor_alt_creator.create() is None:
499 raise Exception("Failed to create flavor")
500 self.creators.append(flavor_alt_creator)
502 def _run_tests(self):
504 if self.test_name == 'all':
505 for test in self.TESTS:
506 if test == 'all' or test == 'vm':
510 self._run_task(self.test_name)
512 def _generate_report(self):
513 """Generate test execution summary report."""
519 res_table = prettytable.PrettyTable(
521 field_names=['Module', 'Duration', 'nb. Test Run', 'Success'])
522 res_table.align['Module'] = "l"
523 res_table.align['Duration'] = "r"
524 res_table.align['Success'] = "r"
526 # for each scenario we draw a row for the table
527 for item in self.summary:
528 total_duration += item['overall_duration']
529 total_nb_tests += item['nb_tests']
530 total_nb_success += item['nb_success']
532 success_avg = 100 * item['nb_success'] / item['nb_tests']
533 except ZeroDivisionError:
535 success_str = str("{:0.2f}".format(success_avg)) + '%'
536 duration_str = time.strftime("%M:%S",
537 time.gmtime(item['overall_duration']))
538 res_table.add_row([item['test_name'], duration_str,
539 item['nb_tests'], success_str])
540 payload.append({'module': item['test_name'],
541 'details': {'duration': item['overall_duration'],
542 'nb tests': item['nb_tests'],
543 'success': success_str}})
545 total_duration_str = time.strftime("%H:%M:%S",
546 time.gmtime(total_duration))
548 self.result = 100 * total_nb_success / total_nb_tests
549 except ZeroDivisionError:
551 success_rate = "{:0.2f}".format(self.result)
552 success_rate_str = str(success_rate) + '%'
553 res_table.add_row(["", "", "", ""])
554 res_table.add_row(["TOTAL:", total_duration_str, total_nb_tests,
557 LOGGER.info("Rally Summary Report:\n\n%s\n", res_table.get_string())
558 LOGGER.info("Rally '%s' success_rate is %s%%",
559 self.case_name, success_rate)
560 payload.append({'summary': {'duration': total_duration,
561 'nb tests': total_nb_tests,
562 'nb success': success_rate}})
563 self.details = payload
566 """Cleanup all OpenStack objects. Should be called on completion."""
567 for creator in reversed(self.creators):
570 except Exception as exc: # pylint: disable=broad-except
571 LOGGER.error('Unexpected error cleaning - %s', exc)
573 @energy.enable_recording
574 def run(self, **kwargs):
576 self.start_time = time.time()
578 conf_utils.create_rally_deployment()
581 self._generate_report()
582 res = testcase.TestCase.EX_OK
583 except Exception as exc: # pylint: disable=broad-except
584 LOGGER.error('Error with run: %s', exc)
585 res = testcase.TestCase.EX_RUN_ERROR
589 self.stop_time = time.time()
593 class RallySanity(RallyBase):
594 """Rally sanity testcase implementation."""
596 def __init__(self, **kwargs):
597 """Initialize RallySanity object."""
598 if "case_name" not in kwargs:
599 kwargs["case_name"] = "rally_sanity"
600 super(RallySanity, self).__init__(**kwargs)
602 self.test_name = 'all'
604 self.scenario_dir = os.path.join(self.RALLY_SCENARIO_DIR, 'sanity')
607 class RallyFull(RallyBase):
608 """Rally full testcase implementation."""
610 def __init__(self, **kwargs):
611 """Initialize RallyFull object."""
612 if "case_name" not in kwargs:
613 kwargs["case_name"] = "rally_full"
614 super(RallyFull, self).__init__(**kwargs)
616 self.test_name = 'all'
618 self.scenario_dir = os.path.join(self.RALLY_SCENARIO_DIR, 'full')