Merge changes from topic 'refactor_tempest'
[functest.git] / functest / opnfv_tests / openstack / rally / rally.py
index 59a6709..1502d58 100644 (file)
@@ -22,21 +22,20 @@ import uuid
 
 import pkg_resources
 import prettytable
 
 import pkg_resources
 import prettytable
-import yaml
-
-from functest.core import testcase
-from functest.energy import energy
-from functest.opnfv_tests.openstack.snaps import snaps_utils
-from functest.opnfv_tests.openstack.tempest import conf_utils
-from functest.utils.constants import CONST
-
 from snaps.config.flavor import FlavorConfig
 from snaps.config.image import ImageConfig
 from snaps.config.network import NetworkConfig, SubnetConfig
 from snaps.config.router import RouterConfig
 from snaps.config.flavor import FlavorConfig
 from snaps.config.image import ImageConfig
 from snaps.config.network import NetworkConfig, SubnetConfig
 from snaps.config.router import RouterConfig
-
 from snaps.openstack.create_flavor import OpenStackFlavor
 from snaps.openstack.utils import deploy_utils
 from snaps.openstack.create_flavor import OpenStackFlavor
 from snaps.openstack.utils import deploy_utils
+from xtesting.core import testcase
+from xtesting.energy import energy
+import yaml
+
+from functest.opnfv_tests.openstack.snaps import snaps_utils
+from functest.opnfv_tests.openstack.tempest import conf_utils
+from functest.utils import config
+from functest.utils import env
 
 LOGGER = logging.getLogger(__name__)
 
 
 LOGGER = logging.getLogger(__name__)
 
@@ -44,26 +43,23 @@ LOGGER = logging.getLogger(__name__)
 class RallyBase(testcase.TestCase):
     """Base class form Rally testcases implementation."""
 
 class RallyBase(testcase.TestCase):
     """Base class form Rally testcases implementation."""
 
+    # pylint: disable=too-many-instance-attributes
     TESTS = ['authenticate', 'glance', 'ceilometer', 'cinder', 'heat',
              'keystone', 'neutron', 'nova', 'quotas', 'vm', 'all']
     TESTS = ['authenticate', 'glance', 'ceilometer', 'cinder', 'heat',
              'keystone', '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')
-    GLANCE_IMAGE_USERNAME = CONST.__getattribute__('openstack_image_username')
-    GLANCE_IMAGE_EXTRA_PROPERTIES = {}
-    if hasattr(CONST, 'openstack_extra_properties'):
-        GLANCE_IMAGE_EXTRA_PROPERTIES = CONST.__getattribute__(
-            'openstack_extra_properties')
-    FLAVOR_NAME = CONST.__getattribute__('rally_flavor_name')
-    FLAVOR_ALT_NAME = CONST.__getattribute__('rally_flavor_alt_name')
-    FLAVOR_EXTRA_SPECS = None
+    GLANCE_IMAGE_NAME = getattr(config.CONF, 'openstack_image_name')
+    GLANCE_IMAGE_FILENAME = getattr(config.CONF, 'openstack_image_file_name')
+    GLANCE_IMAGE_PATH = os.path.join(getattr(
+        config.CONF, 'dir_functest_images'), GLANCE_IMAGE_FILENAME)
+    GLANCE_IMAGE_FORMAT = getattr(config.CONF, 'openstack_image_disk_format')
+    GLANCE_IMAGE_USERNAME = getattr(config.CONF, 'openstack_image_username')
+    GLANCE_IMAGE_EXTRA_PROPERTIES = getattr(
+        config.CONF, 'openstack_extra_properties', {})
+    FLAVOR_NAME = getattr(config.CONF, 'rally_flavor_name')
+    FLAVOR_ALT_NAME = getattr(config.CONF, 'rally_flavor_alt_name')
     FLAVOR_RAM = 512
     FLAVOR_RAM_ALT = 1024
     FLAVOR_RAM = 512
     FLAVOR_RAM_ALT = 1024
-    if hasattr(CONST, 'flavor_extra_specs'):
-        FLAVOR_EXTRA_SPECS = CONST.__getattribute__('flavor_extra_specs')
+    FLAVOR_EXTRA_SPECS = getattr(config.CONF, 'flavor_extra_specs', None)
+    if FLAVOR_EXTRA_SPECS:
         FLAVOR_RAM = 1024
         FLAVOR_RAM_ALT = 2048
 
         FLAVOR_RAM = 1024
         FLAVOR_RAM_ALT = 2048
 
@@ -79,14 +75,14 @@ class RallyBase(testcase.TestCase):
     TENANTS_AMOUNT = 3
     ITERATIONS_AMOUNT = 10
     CONCURRENCY = 4
     TENANTS_AMOUNT = 3
     ITERATIONS_AMOUNT = 10
     CONCURRENCY = 4
-    RESULTS_DIR = os.path.join(CONST.__getattribute__('dir_results'), 'rally')
+    RESULTS_DIR = os.path.join(getattr(config.CONF, 'dir_results'), 'rally')
     BLACKLIST_FILE = os.path.join(RALLY_DIR, "blacklist.txt")
     TEMP_DIR = os.path.join(RALLY_DIR, "var")
 
     BLACKLIST_FILE = os.path.join(RALLY_DIR, "blacklist.txt")
     TEMP_DIR = os.path.join(RALLY_DIR, "var")
 
-    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')
+    RALLY_PRIVATE_NET_NAME = getattr(config.CONF, 'rally_network_name')
+    RALLY_PRIVATE_SUBNET_NAME = getattr(config.CONF, 'rally_subnet_name')
+    RALLY_PRIVATE_SUBNET_CIDR = getattr(config.CONF, 'rally_subnet_cidr')
+    RALLY_ROUTER_NAME = getattr(config.CONF, 'rally_router_name')
 
     def __init__(self, **kwargs):
         """Initialize RallyBase object."""
 
     def __init__(self, **kwargs):
         """Initialize RallyBase object."""
@@ -110,14 +106,15 @@ class RallyBase(testcase.TestCase):
         self.compute_cnt = 0
 
     def _build_task_args(self, test_file_name):
         self.compute_cnt = 0
 
     def _build_task_args(self, test_file_name):
+        """Build arguments for the Rally task."""
         task_args = {'service_list': [test_file_name]}
         task_args = {'service_list': [test_file_name]}
-        task_args['image_name'] = self.image_name
-        task_args['flavor_name'] = self.flavor_name
-        task_args['flavor_alt_name'] = self.flavor_alt_name
-        task_args['glance_image_location'] = self.GLANCE_IMAGE_PATH
-        task_args['glance_image_format'] = self.GLANCE_IMAGE_FORMAT
-        task_args['tmpl_dir'] = self.TEMPLATE_DIR
-        task_args['sup_dir'] = self.SUPPORT_DIR
+        task_args['image_name'] = str(self.image_name)
+        task_args['flavor_name'] = str(self.flavor_name)
+        task_args['flavor_alt_name'] = str(self.flavor_alt_name)
+        task_args['glance_image_location'] = str(self.GLANCE_IMAGE_PATH)
+        task_args['glance_image_format'] = str(self.GLANCE_IMAGE_FORMAT)
+        task_args['tmpl_dir'] = str(self.TEMPLATE_DIR)
+        task_args['sup_dir'] = str(self.SUPPORT_DIR)
         task_args['users_amount'] = self.USERS_AMOUNT
         task_args['tenants_amount'] = self.TENANTS_AMOUNT
         task_args['use_existing_users'] = False
         task_args['users_amount'] = self.USERS_AMOUNT
         task_args['tenants_amount'] = self.TENANTS_AMOUNT
         task_args['use_existing_users'] = False
@@ -140,6 +137,7 @@ class RallyBase(testcase.TestCase):
         return task_args
 
     def _prepare_test_list(self, test_name):
         return task_args
 
     def _prepare_test_list(self, test_name):
+        """Build the list of test cases to be executed."""
         test_yaml_file_name = 'opnfv-{}.yaml'.format(test_name)
         scenario_file_name = os.path.join(self.RALLY_SCENARIO_DIR,
                                           test_yaml_file_name)
         test_yaml_file_name = 'opnfv-{}.yaml'.format(test_name)
         scenario_file_name = os.path.join(self.RALLY_SCENARIO_DIR,
                                           test_yaml_file_name)
@@ -191,7 +189,7 @@ class RallyBase(testcase.TestCase):
                 return False
 
             for result in report.get('result'):
                 return False
 
             for result in report.get('result'):
-                if result is None or len(result.get('error')) > 0:
+                if result is None or result.get('error'):
                     return False
 
         return True
                     return False
 
         return True
@@ -219,8 +217,8 @@ class RallyBase(testcase.TestCase):
             with open(RallyBase.BLACKLIST_FILE, 'r') as black_list_file:
                 black_list_yaml = yaml.safe_load(black_list_file)
 
             with open(RallyBase.BLACKLIST_FILE, 'r') as black_list_file:
                 black_list_yaml = yaml.safe_load(black_list_file)
 
-            installer_type = CONST.__getattribute__('INSTALLER_TYPE')
-            deploy_scenario = CONST.__getattribute__('DEPLOY_SCENARIO')
+            installer_type = env.get('INSTALLER_TYPE')
+            deploy_scenario = env.get('DEPLOY_SCENARIO')
             if (bool(installer_type) and bool(deploy_scenario) and
                     'scenario' in black_list_yaml.keys()):
                 for item in black_list_yaml['scenario']:
             if (bool(installer_type) and bool(deploy_scenario) and
                     'scenario' in black_list_yaml.keys()):
                 for item in black_list_yaml['scenario']:
@@ -231,7 +229,7 @@ class RallyBase(testcase.TestCase):
                             in_it(installer_type, installers)):
                         tests = item['tests']
                         black_tests.extend(tests)
                             in_it(installer_type, installers)):
                         tests = item['tests']
                         black_tests.extend(tests)
-        except Exception:
+        except Exception:  # pylint: disable=broad-except
             LOGGER.debug("Scenario exclusion not applied.")
 
         return black_tests
             LOGGER.debug("Scenario exclusion not applied.")
 
         return black_tests
@@ -254,8 +252,8 @@ class RallyBase(testcase.TestCase):
             # match if regex pattern is set and found in the needle
             if pattern and re.search(pattern, needle) is not None:
                 return True
             # 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
+
+        return False
 
     def excl_func(self):
         """Exclude functionalities."""
 
     def excl_func(self):
         """Exclude functionalities."""
@@ -399,6 +397,7 @@ class RallyBase(testcase.TestCase):
             LOGGER.info('Test scenario: "{}" Failed.'.format(test_name) + "\n")
 
     def _append_summary(self, json_raw, test_name):
             LOGGER.info('Test scenario: "{}" Failed.'.format(test_name) + "\n")
 
     def _append_summary(self, json_raw, test_name):
+        """Update statistics summary info."""
         nb_tests = 0
         nb_success = 0
         overall_duration = 0.0
         nb_tests = 0
         nb_success = 0
         overall_duration = 0.0
@@ -421,6 +420,7 @@ class RallyBase(testcase.TestCase):
         self.summary.append(scenario_summary)
 
     def _prepare_env(self):
         self.summary.append(scenario_summary)
 
     def _prepare_env(self):
+        """Create resources needed by test scenarios."""
         LOGGER.debug('Validating the test name...')
         if self.test_name not in self.TESTS:
             raise Exception("Test name '%s' is invalid" % self.test_name)
         LOGGER.debug('Validating the test name...')
         if self.test_name not in self.TESTS:
             raise Exception("Test name '%s' is invalid" % self.test_name)
@@ -449,19 +449,11 @@ class RallyBase(testcase.TestCase):
 
         LOGGER.debug("Creating network '%s'...", network_name)
 
 
         LOGGER.debug("Creating network '%s'...", network_name)
 
-        rally_network_type = None
-        rally_physical_network = None
-        rally_segmentation_id = None
-
-        if hasattr(CONST, 'rally_network_type'):
-            rally_network_type = CONST.__getattribute__(
-                'rally_network_type')
-        if hasattr(CONST, 'rally_physical_network'):
-            rally_physical_network = CONST.__getattribute__(
-                'rally_physical_network')
-        if hasattr(CONST, 'rally_segmentation_id'):
-            rally_segmentation_id = CONST.__getattribute__(
-                'rally_segmentation_id')
+        rally_network_type = getattr(config.CONF, 'rally_network_type', None)
+        rally_physical_network = getattr(
+            config.CONF, 'rally_physical_network', None)
+        rally_segmentation_id = getattr(
+            config.CONF, 'rally_segmentation_id', None)
 
         network_creator = deploy_utils.create_network(
             self.os_creds, NetworkConfig(
 
         network_creator = deploy_utils.create_network(
             self.os_creds, NetworkConfig(
@@ -472,8 +464,8 @@ class RallyBase(testcase.TestCase):
                 segmentation_id=rally_segmentation_id,
                 subnet_settings=[SubnetConfig(
                     name=subnet_name,
                 segmentation_id=rally_segmentation_id,
                 subnet_settings=[SubnetConfig(
                     name=subnet_name,
-                    cidr=self.RALLY_PRIVATE_SUBNET_CIDR)
-                ]))
+                    cidr=self.RALLY_PRIVATE_SUBNET_CIDR,
+                    dns_nameservers=[env.get('NAMESERVER')])]))
         if network_creator is None:
             raise Exception("Failed to create private network")
         self.priv_net_id = network_creator.get_network().id
         if network_creator is None:
             raise Exception("Failed to create private network")
         self.priv_net_id = network_creator.get_network().id
@@ -508,6 +500,7 @@ class RallyBase(testcase.TestCase):
         self.creators.append(flavor_alt_creator)
 
     def _run_tests(self):
         self.creators.append(flavor_alt_creator)
 
     def _run_tests(self):
+        """Execute tests."""
         if self.test_name == 'all':
             for test in self.TESTS:
                 if test == 'all' or test == 'vm':
         if self.test_name == 'all':
             for test in self.TESTS:
                 if test == 'all' or test == 'vm':
@@ -517,6 +510,7 @@ class RallyBase(testcase.TestCase):
             self._run_task(self.test_name)
 
     def _generate_report(self):
             self._run_task(self.test_name)
 
     def _generate_report(self):
+        """Generate test execution summary report."""
         total_duration = 0.0
         total_nb_tests = 0
         total_nb_success = 0
         total_duration = 0.0
         total_nb_tests = 0
         total_nb_success = 0
@@ -569,11 +563,12 @@ class RallyBase(testcase.TestCase):
         self.details = payload
 
     def _clean_up(self):
         self.details = payload
 
     def _clean_up(self):
+        """Cleanup all OpenStack objects. Should be called on completion."""
         for creator in reversed(self.creators):
             try:
                 creator.clean()
         for creator in reversed(self.creators):
             try:
                 creator.clean()
-            except Exception as e:
-                LOGGER.error('Unexpected error cleaning - %s', e)
+            except Exception as exc:  # pylint: disable=broad-except
+                LOGGER.error('Unexpected error cleaning - %s', exc)
 
     @energy.enable_recording
     def run(self, **kwargs):
 
     @energy.enable_recording
     def run(self, **kwargs):