Allow regex for blacklist scenarios/installers
[functest.git] / functest / opnfv_tests / openstack / rally / rally.py
index 46d6a57..d5acb1b 100644 (file)
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/env python
 #
 # Copyright (c) 2015 All rights reserved
 # This program and the accompanying materials
@@ -8,8 +8,12 @@
 # http://www.apache.org/licenses/LICENSE-2.0
 #
 
+from __future__ import division
+
 import json
+import logging
 import os
+import pkg_resources
 import re
 import subprocess
 import time
@@ -17,47 +21,50 @@ import time
 import iniparse
 import yaml
 
-from functest.core import testcase_base
+from functest.core import testcase
 from functest.utils.constants import CONST
-import functest.utils.functest_logger as ft_logger
-import functest.utils.functest_utils as ft_utils
 import functest.utils.openstack_utils as os_utils
 
-logger = ft_logger.Logger('Rally').getLogger()
+logger = logging.getLogger(__name__)
 
 
-class RallyBase(testcase_base.TestcaseBase):
+class RallyBase(testcase.OSGCTestCase):
     TESTS = ['authenticate', 'glance', 'cinder', 'heat', 'keystone',
-             'neutron', 'nova', 'quotas', 'requests', 'vm', 'all']
-    GLANCE_IMAGE_NAME = CONST.openstack_image_name
-    GLANCE_IMAGE_FILENAME = CONST.openstack_image_file_name
-    GLANCE_IMAGE_PATH = os.path.join(CONST.dir_functest_data,
-                                     GLANCE_IMAGE_FILENAME)
-    GLANCE_IMAGE_FORMAT = CONST.openstack_image_disk_format
+             'neutron', 'nova', 'quotas', 'vm', 'all']
+    GLANCE_IMAGE_NAME = CONST.__getattribute__('openstack_image_name')
+    GLANCE_IMAGE_FILENAME = CONST.__getattribute__('openstack_image_file_name')
+    GLANCE_IMAGE_PATH = os.path.join(
+        CONST.__getattribute__('dir_functest_images'),
+        GLANCE_IMAGE_FILENAME)
+    GLANCE_IMAGE_FORMAT = CONST.__getattribute__('openstack_image_disk_format')
     FLAVOR_NAME = "m1.tiny"
 
-    RALLY_DIR = os.path.join(CONST.dir_repo_functest, CONST.dir_rally)
-    RALLY_SCENARIO_DIR = os.path.join(RALLY_DIR, "scenario")
-    TEMPLATE_DIR = os.path.join(RALLY_SCENARIO_DIR, "templates")
-    SUPPORT_DIR = os.path.join(RALLY_SCENARIO_DIR, "support")
+    RALLY_DIR = pkg_resources.resource_filename(
+        'functest', 'opnfv_tests/openstack/rally')
+    RALLY_SCENARIO_DIR = pkg_resources.resource_filename(
+        'functest', 'opnfv_tests/openstack/rally/scenario')
+    TEMPLATE_DIR = pkg_resources.resource_filename(
+        'functest', 'opnfv_tests/openstack/rally/scenario/templates')
+    SUPPORT_DIR = pkg_resources.resource_filename(
+        'functest', 'opnfv_tests/openstack/rally/scenario/support')
     USERS_AMOUNT = 2
     TENANTS_AMOUNT = 3
     ITERATIONS_AMOUNT = 10
     CONCURRENCY = 4
-    RESULTS_DIR = os.path.join(CONST.dir_results, 'rally')
-    TEMPEST_CONF_FILE = os.path.join(CONST.dir_results,
+    RESULTS_DIR = os.path.join(CONST.__getattribute__('dir_results'), 'rally')
+    TEMPEST_CONF_FILE = os.path.join(CONST.__getattribute__('dir_results'),
                                      'tempest/tempest.conf')
     BLACKLIST_FILE = os.path.join(RALLY_DIR, "blacklist.txt")
     TEMP_DIR = os.path.join(RALLY_DIR, "var")
 
     CINDER_VOLUME_TYPE_NAME = "volume_test"
-    RALLY_PRIVATE_NET_NAME = CONST.rally_network_name
-    RALLY_PRIVATE_SUBNET_NAME = CONST.rally_subnet_name
-    RALLY_PRIVATE_SUBNET_CIDR = CONST.rally_subnet_cidr
-    RALLY_ROUTER_NAME = CONST.rally_router_name
+    RALLY_PRIVATE_NET_NAME = CONST.__getattribute__('rally_network_name')
+    RALLY_PRIVATE_SUBNET_NAME = CONST.__getattribute__('rally_subnet_name')
+    RALLY_PRIVATE_SUBNET_CIDR = CONST.__getattribute__('rally_subnet_cidr')
+    RALLY_ROUTER_NAME = CONST.__getattribute__('rally_router_name')
 
-    def __init__(self):
-        super(RallyBase, self).__init__()
+    def __init__(self, **kwargs):
+        super(RallyBase, self).__init__(**kwargs)
         self.mode = ''
         self.summary = []
         self.scenario_dir = ''
@@ -95,9 +102,6 @@ class RallyBase(testcase_base.TestcaseBase):
         else:
             task_args['netid'] = ''
 
-        # get keystone auth endpoint
-        task_args['request_url'] = CONST.OS_AUTH_URL or ''
-
         return task_args
 
     def _prepare_test_list(self, test_name):
@@ -182,22 +186,45 @@ class RallyBase(testcase_base.TestcaseBase):
             with open(RallyBase.BLACKLIST_FILE, 'r') as black_list_file:
                 black_list_yaml = yaml.safe_load(black_list_file)
 
-            installer_type = CONST.INSTALLER_TYPE
-            deploy_scenario = CONST.DEPLOY_SCENARIO
-            if (bool(installer_type) * bool(deploy_scenario)):
-                if 'scenario' in black_list_yaml.keys():
-                    for item in black_list_yaml['scenario']:
-                        scenarios = item['scenarios']
-                        installers = item['installers']
-                        if (deploy_scenario in scenarios and
-                                installer_type in installers):
-                            tests = item['tests']
-                            black_tests.extend(tests)
+            installer_type = CONST.__getattribute__('INSTALLER_TYPE')
+            deploy_scenario = CONST.__getattribute__('DEPLOY_SCENARIO')
+            if (bool(installer_type) and bool(deploy_scenario) and
+                    'scenario' in black_list_yaml.keys()):
+                for item in black_list_yaml['scenario']:
+                    scenarios = item['scenarios']
+                    installers = item['installers']
+                    in_it = RallyBase.in_iterable_re
+                    if (in_it(deploy_scenario, scenarios) and
+                            in_it(installer_type, installers)):
+                        tests = item['tests']
+                        black_tests.extend(tests)
         except Exception:
             logger.debug("Scenario exclusion not applied.")
 
         return black_tests
 
+    @staticmethod
+    def in_iterable_re(needle, haystack):
+        """
+        Check if given needle is in the iterable haystack, using regex.
+
+        :param needle: string to be matched
+        :param haystack: iterable of strings (optionally regex patterns)
+        :return: True if needle is eqial to any of the elements in haystack,
+                 or if a nonempty regex pattern in haystack is found in needle.
+        """
+
+        # match without regex
+        if needle in haystack:
+            return True
+
+        for pattern in haystack:
+            # match if regex pattern is set and found in the needle
+            if pattern and re.search(pattern, needle) is not None:
+                return True
+        else:
+            return False
+
     @staticmethod
     def excl_func():
         black_tests = []
@@ -231,6 +258,9 @@ class RallyBase(testcase_base.TestcaseBase):
         black_tests = list(set(RallyBase.excl_func() +
                            RallyBase.excl_scenario()))
 
+        if black_tests:
+            logger.debug("Blacklisted tests: " + str(black_tests))
+
         include = True
         for cases_line in cases_file:
             if include:
@@ -480,11 +510,12 @@ class RallyBase(testcase_base.TestcaseBase):
         total_duration_str2 = "{0:<10}".format(total_duration_str)
         total_nb_tests_str = "{0:<13}".format(total_nb_tests)
 
-        if len(self.summary):
-            success_rate = total_success / len(self.summary)
-        else:
-            success_rate = 100
-        success_rate = "{:0.2f}".format(success_rate)
+        try:
+            self.result = total_success / len(self.summary)
+        except ZeroDivisionError:
+            self.result = 100
+
+        success_rate = "{:0.2f}".format(self.result)
         success_rate_str = "{0:<10}".format(str(success_rate) + '%')
         report += ("+===================+============"
                    "+===============+===========+")
@@ -500,12 +531,10 @@ class RallyBase(testcase_base.TestcaseBase):
                                     'nb tests': total_nb_tests,
                                     'nb success': success_rate}})
 
-        self.criteria = ft_utils.check_success_rate(
-            self.case_name, success_rate)
         self.details = payload
 
-        logger.info("Rally '%s' success_rate is %s%%, is marked as %s"
-                    % (self.case_name, success_rate, self.criteria))
+        logger.info("Rally '%s' success_rate is %s%%"
+                    % (self.case_name, success_rate))
 
     def _clean_up(self):
         if self.volume_type:
@@ -526,19 +555,20 @@ class RallyBase(testcase_base.TestcaseBase):
             self._run_tests()
             self._generate_report()
             self._clean_up()
-            res = testcase_base.TestcaseBase.EX_OK
+            res = testcase.TestCase.EX_OK
         except Exception as e:
             logger.error('Error with run: %s' % e)
-            res = testcase_base.TestcaseBase.EX_RUN_ERROR
+            res = testcase.TestCase.EX_RUN_ERROR
 
         self.stop_time = time.time()
         return res
 
 
 class RallySanity(RallyBase):
-    def __init__(self):
-        super(RallySanity, self).__init__()
-        self.case_name = 'rally_sanity'
+    def __init__(self, **kwargs):
+        if "case_name" not in kwargs:
+            kwargs["case_name"] = "rally_sanity"
+        super(RallySanity, self).__init__(**kwargs)
         self.mode = 'sanity'
         self.test_name = 'all'
         self.smoke = True
@@ -546,9 +576,10 @@ class RallySanity(RallyBase):
 
 
 class RallyFull(RallyBase):
-    def __init__(self):
-        super(RallyFull, self).__init__()
-        self.case_name = 'rally_full'
+    def __init__(self, **kwargs):
+        if "case_name" not in kwargs:
+            kwargs["case_name"] = "rally_full"
+        super(RallyFull, self).__init__(**kwargs)
         self.mode = 'full'
         self.test_name = 'all'
         self.smoke = False