Allow regex for blacklist scenarios/installers
[functest.git] / functest / opnfv_tests / openstack / rally / rally.py
index 40f8386..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
@@ -13,6 +13,7 @@ from __future__ import division
 import json
 import logging
 import os
+import pkg_resources
 import re
 import subprocess
 import time
@@ -27,35 +28,40 @@ import functest.utils.openstack_utils as os_utils
 logger = logging.getLogger(__name__)
 
 
-class RallyBase(testcase.TestCase):
+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_images,
-                                     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, **kwargs):
         super(RallyBase, self).__init__(**kwargs)
@@ -96,9 +102,6 @@ class RallyBase(testcase.TestCase):
         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):
@@ -183,22 +186,45 @@ class RallyBase(testcase.TestCase):
             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 = []
@@ -232,6 +258,9 @@ class RallyBase(testcase.TestCase):
         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: