Remove Ceilometer test scenarios
[functest.git] / functest / opnfv_tests / openstack / rally / rally.py
1 #!/usr/bin/env python
2 #
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
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10
11 """Rally testcases implementation."""
12
13 from __future__ import division
14
15 import json
16 import logging
17 import os
18 import re
19 import subprocess
20 import time
21 import uuid
22
23 import pkg_resources
24 import prettytable
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
33 import yaml
34
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
39
40 LOGGER = logging.getLogger(__name__)
41
42
43 class RallyBase(testcase.TestCase):
44     """Base class form Rally testcases implementation."""
45
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')
59     FLAVOR_RAM = 512
60     FLAVOR_RAM_ALT = 1024
61     FLAVOR_EXTRA_SPECS = getattr(config.CONF, 'flavor_extra_specs', None)
62     if FLAVOR_EXTRA_SPECS:
63         FLAVOR_RAM = 1024
64         FLAVOR_RAM_ALT = 2048
65
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')
74     USERS_AMOUNT = 2
75     TENANTS_AMOUNT = 3
76     ITERATIONS_AMOUNT = 10
77     CONCURRENCY = 4
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")
81
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')
86
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())
92         self.creators = []
93         self.mode = ''
94         self.summary = []
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
101         self.smoke = None
102         self.test_name = None
103         self.start_time = None
104         self.result = None
105         self.details = None
106         self.compute_cnt = 0
107
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
124
125         ext_net = self.ext_net_name
126         if ext_net:
127             task_args['floating_network'] = str(ext_net)
128         else:
129             task_args['floating_network'] = ''
130
131         net_id = self.priv_net_id
132         if net_id:
133             task_args['netid'] = str(net_id)
134         else:
135             task_args['netid'] = ''
136
137         return task_args
138
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,
143                                           test_yaml_file_name)
144
145         if not os.path.exists(scenario_file_name):
146             scenario_file_name = os.path.join(self.scenario_dir,
147                                               test_yaml_file_name)
148
149             if not os.path.exists(scenario_file_name):
150                 raise Exception("The scenario '%s' does not exist."
151                                 % scenario_file_name)
152
153         LOGGER.debug('Scenario fetched from : %s', scenario_file_name)
154         test_file_name = os.path.join(self.TEMP_DIR, test_yaml_file_name)
155
156         if not os.path.exists(self.TEMP_DIR):
157             os.makedirs(self.TEMP_DIR)
158
159         self._apply_blacklist(scenario_file_name, test_file_name)
160         return test_file_name
161
162     @staticmethod
163     def get_task_id(cmd_raw):
164         """
165         Get task id from command rally result.
166
167         :param cmd_raw:
168         :return: task_id as string
169         """
170         taskid_re = re.compile('^Task +(.*): started$')
171         for line in cmd_raw.splitlines(True):
172             line = line.strip()
173             match = taskid_re.match(line)
174             if match:
175                 return match.group(1)
176         return None
177
178     @staticmethod
179     def task_succeed(json_raw):
180         """
181         Parse JSON from rally JSON results.
182
183         :param json_raw:
184         :return: Bool
185         """
186         rally_report = json.loads(json_raw)
187         for report in rally_report:
188             if report is None or report.get('result') is None:
189                 return False
190
191             for result in report.get('result'):
192                 if result is None or result.get('error'):
193                     return False
194
195         return True
196
197     def _migration_supported(self):
198         """Determine if migration is supported."""
199         if self.compute_cnt > 1:
200             return True
201
202         return False
203
204     @staticmethod
205     def get_cmd_output(proc):
206         """Get command stdout."""
207         result = ""
208         for line in proc.stdout:
209             result += line
210         return result
211
212     @staticmethod
213     def excl_scenario():
214         """Exclude scenario."""
215         black_tests = []
216         try:
217             with open(RallyBase.BLACKLIST_FILE, 'r') as black_list_file:
218                 black_list_yaml = yaml.safe_load(black_list_file)
219
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.")
234
235         return black_tests
236
237     @staticmethod
238     def in_iterable_re(needle, haystack):
239         """
240         Check if given needle is in the iterable haystack, using regex.
241
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.
246         """
247         # match without regex
248         if needle in haystack:
249             return True
250
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:
254                 return True
255
256         return False
257
258     def excl_func(self):
259         """Exclude functionalities."""
260         black_tests = []
261         func_list = []
262
263         try:
264             with open(RallyBase.BLACKLIST_FILE, 'r') as black_list_file:
265                 black_list_yaml = yaml.safe_load(black_list_file)
266
267             if not self._migration_supported():
268                 func_list.append("no_migration")
269
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.")
279
280         return black_tests
281
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')
287
288         black_tests = list(set(self.excl_func() +
289                                self.excl_scenario()))
290
291         if black_tests:
292             LOGGER.debug("Blacklisted tests: " + str(black_tests))
293
294         include = True
295         for cases_line in cases_file:
296             if include:
297                 for black_tests_line in black_tests:
298                     if re.search(black_tests_line,
299                                  cases_line.strip().rstrip(':')):
300                         include = False
301                         break
302                 else:
303                     result_file.write(str(cases_line))
304             else:
305                 if cases_line.isspace():
306                     include = True
307
308         cases_file.close()
309         result_file.close()
310
311     @staticmethod
312     def file_is_empty(file_name):
313         """Determine is a file is empty."""
314         try:
315             if os.stat(file_name).st_size > 0:
316                 return False
317         except Exception:  # pylint: disable=broad-except
318             pass
319
320         return True
321
322     def _run_task(self, test_name):
323         """Run a task."""
324         LOGGER.info('Starting test scenario "%s" ...', test_name)
325
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)
330
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)
334             return
335
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)
340
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)
345
346         LOGGER.debug('task_id : %s', task_id)
347
348         if task_id is None:
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)
357             return
358
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.',
362                          self.RESULTS_DIR)
363             os.makedirs(self.RESULTS_DIR)
364
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)
372
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)
384
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)
392
393         # parse JSON operation result
394         if self.task_succeed(json_results):
395             LOGGER.info('Test scenario: "{}" OK.'.format(test_name) + "\n")
396         else:
397             LOGGER.info('Test scenario: "{}" Failed.'.format(test_name) + "\n")
398
399     def _append_summary(self, json_raw, test_name):
400         """Update statistics summary info."""
401         nb_tests = 0
402         nb_success = 0
403         overall_duration = 0.0
404
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')
409
410             if report.get('result'):
411                 for result in report.get('result'):
412                     nb_tests += 1
413                     if not result.get('error'):
414                         nb_success += 1
415
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)
421
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)
427
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)
436
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,
444                 public=True,
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)
449
450         LOGGER.debug("Creating network '%s'...", network_name)
451
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)
457
458         network_creator = deploy_utils.create_network(
459             self.os_creds, NetworkConfig(
460                 name=network_name,
461                 shared=True,
462                 network_type=rally_network_type,
463                 physical_network=rally_physical_network,
464                 segmentation_id=rally_segmentation_id,
465                 subnet_settings=[SubnetConfig(
466                     name=subnet_name,
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)
473
474         LOGGER.debug("Creating router '%s'...", router_name)
475         router_creator = deploy_utils.create_router(
476             self.os_creds, RouterConfig(
477                 name=router_name,
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)
483
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)
492
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)
501
502     def _run_tests(self):
503         """Execute tests."""
504         if self.test_name == 'all':
505             for test in self.TESTS:
506                 if test == 'all' or test == 'vm':
507                     continue
508                 self._run_task(test)
509         else:
510             self._run_task(self.test_name)
511
512     def _generate_report(self):
513         """Generate test execution summary report."""
514         total_duration = 0.0
515         total_nb_tests = 0
516         total_nb_success = 0
517         payload = []
518
519         res_table = prettytable.PrettyTable(
520             padding_width=2,
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"
525
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']
531             try:
532                 success_avg = 100 * item['nb_success'] / item['nb_tests']
533             except ZeroDivisionError:
534                 success_avg = 0
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}})
544
545         total_duration_str = time.strftime("%H:%M:%S",
546                                            time.gmtime(total_duration))
547         try:
548             self.result = 100 * total_nb_success / total_nb_tests
549         except ZeroDivisionError:
550             self.result = 100
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,
555                            success_rate_str])
556
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
564
565     def _clean_up(self):
566         """Cleanup all OpenStack objects. Should be called on completion."""
567         for creator in reversed(self.creators):
568             try:
569                 creator.clean()
570             except Exception as exc:  # pylint: disable=broad-except
571                 LOGGER.error('Unexpected error cleaning - %s', exc)
572
573     @energy.enable_recording
574     def run(self, **kwargs):
575         """Run testcase."""
576         self.start_time = time.time()
577         try:
578             conf_utils.create_rally_deployment()
579             self._prepare_env()
580             self._run_tests()
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
586         finally:
587             self._clean_up()
588
589         self.stop_time = time.time()
590         return res
591
592
593 class RallySanity(RallyBase):
594     """Rally sanity testcase implementation."""
595
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)
601         self.mode = 'sanity'
602         self.test_name = 'all'
603         self.smoke = True
604         self.scenario_dir = os.path.join(self.RALLY_SCENARIO_DIR, 'sanity')
605
606
607 class RallyFull(RallyBase):
608     """Rally full testcase implementation."""
609
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)
615         self.mode = 'full'
616         self.test_name = 'all'
617         self.smoke = False
618         self.scenario_dir = os.path.join(self.RALLY_SCENARIO_DIR, 'full')