Move logics out of TempestCommon.__init__()
[functest.git] / functest / opnfv_tests / openstack / tempest / tempest.py
index 542ed9e..9d788de 100644 (file)
@@ -27,28 +27,42 @@ from functest.core import singlevm
 from functest.opnfv_tests.openstack.tempest import conf_utils
 from functest.utils import config
 from functest.utils import env
+from functest.utils import functest_utils
 
 LOGGER = logging.getLogger(__name__)
 
 
-class TempestCommon(singlevm.VmReady1):
+class TempestCommon(singlevm.VmReady2):
     # pylint: disable=too-many-instance-attributes
     """TempestCommon testcases implementation class."""
 
     visibility = 'public'
-    shared_network = True
     filename_alt = '/home/opnfv/functest/images/cirros-0.4.0-x86_64-disk.img'
 
     def __init__(self, **kwargs):
         if "case_name" not in kwargs:
             kwargs["case_name"] = 'tempest'
         super(TempestCommon, self).__init__(**kwargs)
-        self.verifier_id = conf_utils.get_verifier_id()
-        self.verifier_repo_dir = conf_utils.get_verifier_repo_dir(
-            self.verifier_id)
-        self.deployment_id = conf_utils.get_verifier_deployment_id()
-        self.deployment_dir = conf_utils.get_verifier_deployment_dir(
-            self.verifier_id, self.deployment_id)
+        assert self.orig_cloud
+        assert self.cloud
+        assert self.project
+        if self.orig_cloud.get_role("admin"):
+            self.role_name = "admin"
+        elif self.orig_cloud.get_role("Admin"):
+            self.role_name = "Admin"
+        else:
+            raise Exception("Cannot detect neither admin nor Admin")
+        self.orig_cloud.grant_role(
+            self.role_name, user=self.project.user.id,
+            project=self.project.project.id,
+            domain=self.project.domain.id)
+        self.orig_cloud.grant_role(
+            self.role_name, user=self.project.user.id,
+            domain=self.project.domain.id)
+        self.deployment_id = None
+        self.verifier_id = None
+        self.verifier_repo_dir = None
+        self.deployment_dir = None
         self.verification_id = None
         self.res_dir = os.path.join(
             getattr(config.CONF, 'dir_results'), self.case_name)
@@ -69,6 +83,9 @@ class TempestCommon(singlevm.VmReady1):
         except Exception:  # pylint: disable=broad-except
             pass
 
+    def create_network_resources(self):
+        pass
+
     def check_services(self):
         """Check the mandatory services."""
         for service in self.services:
@@ -91,6 +108,8 @@ class TempestCommon(singlevm.VmReady1):
     def check_requirements(self):
         self.check_services()
         self.check_extensions()
+        if self.is_skipped:
+            self.project.clean()
 
     @staticmethod
     def read_file(filename):
@@ -277,17 +296,16 @@ class TempestCommon(singlevm.VmReady1):
         html_file = os.path.join(self.res_dir,
                                  "tempest-report.html")
         cmd = ["rally", "verify", "report", "--type", "html", "--uuid",
-               self.verification_id, "--to", html_file]
-        subprocess.Popen(cmd, stdout=subprocess.PIPE,
-                         stderr=subprocess.STDOUT)
+               self.verification_id, "--to", str(html_file)]
+        subprocess.check_output(cmd)
 
     def update_rally_regex(self, rally_conf='/etc/rally/rally.conf'):
         """Set image name as tempest img_name_regex"""
         rconfig = configparser.RawConfigParser()
         rconfig.read(rally_conf)
-        if not rconfig.has_section('tempest'):
-            rconfig.add_section('tempest')
-        rconfig.set('tempest', 'img_name_regex', '^{}$'.format(
+        if not rconfig.has_section('openstack'):
+            rconfig.add_section('openstack')
+        rconfig.set('openstack', 'img_name_regex', '^{}$'.format(
             self.image.name))
         with open(rally_conf, 'wb') as config_file:
             rconfig.write(config_file)
@@ -299,9 +317,9 @@ class TempestCommon(singlevm.VmReady1):
             return
         rconfig = configparser.RawConfigParser()
         rconfig.read(rally_conf)
-        if not rconfig.has_section('tempest'):
-            rconfig.add_section('tempest')
-        rconfig.set('tempest', 'swift_operator_role', role.name)
+        if not rconfig.has_section('openstack'):
+            rconfig.add_section('openstack')
+        rconfig.set('openstack', 'swift_operator_role', role.name)
         with open(rally_conf, 'wb') as config_file:
             rconfig.write(config_file)
 
@@ -311,6 +329,8 @@ class TempestCommon(singlevm.VmReady1):
             os.makedirs(self.res_dir)
         rconfig = configparser.RawConfigParser()
         rconfig.read(rally_conf)
+        rconfig.set('DEFAULT', 'debug', True)
+        rconfig.set('DEFAULT', 'use_stderr', False)
         rconfig.set('DEFAULT', 'log-file', 'rally.log')
         rconfig.set('DEFAULT', 'log_dir', self.res_dir)
         with open(rally_conf, 'wb') as config_file:
@@ -321,10 +341,14 @@ class TempestCommon(singlevm.VmReady1):
         """Clean Rally config"""
         rconfig = configparser.RawConfigParser()
         rconfig.read(rally_conf)
-        if rconfig.has_option('tempest', 'img_name_regex'):
-            rconfig.remove_option('tempest', 'img_name_regex')
-        if rconfig.has_option('tempest', 'swift_operator_role'):
-            rconfig.remove_option('tempest', 'swift_operator_role')
+        if rconfig.has_option('openstack', 'img_name_regex'):
+            rconfig.remove_option('openstack', 'img_name_regex')
+        if rconfig.has_option('openstack', 'swift_operator_role'):
+            rconfig.remove_option('openstack', 'swift_operator_role')
+        if rconfig.has_option('DEFAULT', 'use_stderr'):
+            rconfig.remove_option('DEFAULT', 'use_stderr')
+        if rconfig.has_option('DEFAULT', 'debug'):
+            rconfig.remove_option('DEFAULT', 'debug')
         if rconfig.has_option('DEFAULT', 'log-file'):
             rconfig.remove_option('DEFAULT', 'log-file')
         if rconfig.has_option('DEFAULT', 'log_dir'):
@@ -332,6 +356,33 @@ class TempestCommon(singlevm.VmReady1):
         with open(rally_conf, 'wb') as config_file:
             rconfig.write(config_file)
 
+    def update_scenario_section(self):
+        """Update scenario section in tempest.conf"""
+        rconfig = configparser.RawConfigParser()
+        rconfig.read(self.conf_file)
+        filename = getattr(
+            config.CONF, '{}_image'.format(self.case_name), self.filename)
+        if not rconfig.has_section('scenario'):
+            rconfig.add_section('scenario')
+        rconfig.set('scenario', 'img_file', os.path.basename(filename))
+        rconfig.set('scenario', 'img_dir', os.path.dirname(filename))
+        rconfig.set('scenario', 'img_disk_format', getattr(
+            config.CONF, '{}_image_format'.format(self.case_name),
+            self.image_format))
+        extra_properties = self.extra_properties.copy()
+        if env.get('IMAGE_PROPERTIES'):
+            extra_properties.update(
+                functest_utils.convert_ini_to_dict(
+                    env.get('IMAGE_PROPERTIES')))
+        extra_properties.update(
+            getattr(config.CONF, '{}_extra_properties'.format(
+                self.case_name), {}))
+        rconfig.set(
+            'scenario', 'img_properties',
+            functest_utils.convert_dict_to_ini(extra_properties))
+        with open(self.conf_file, 'wb') as config_file:
+            rconfig.write(config_file)
+
     def configure(self, **kwargs):  # pylint: disable=unused-argument
         """
         Create all openstack resources for tempest-based testcases and write
@@ -339,20 +390,48 @@ class TempestCommon(singlevm.VmReady1):
         """
         if not os.path.exists(self.res_dir):
             os.makedirs(self.res_dir)
-        compute_cnt = len(self.cloud.list_hypervisors())
+        environ = dict(
+            os.environ,
+            OS_USERNAME=self.project.user.name,
+            OS_PROJECT_NAME=self.project.project.name,
+            OS_PROJECT_ID=self.project.project.id,
+            OS_PASSWORD=self.project.password)
+        try:
+            del environ['OS_TENANT_NAME']
+            del environ['OS_TENANT_ID']
+        except Exception:  # pylint: disable=broad-except
+            pass
+        self.deployment_id = conf_utils.create_rally_deployment(
+            environ=environ)
+        if not self.deployment_id:
+            raise Exception("Deployment create failed")
+        self.verifier_id = conf_utils.create_verifier()
+        if not self.verifier_id:
+            raise Exception("Verifier create failed")
+        self.verifier_repo_dir = conf_utils.get_verifier_repo_dir(
+            self.verifier_id)
+        self.deployment_dir = conf_utils.get_verifier_deployment_dir(
+            self.verifier_id, self.deployment_id)
+
+        compute_cnt = len(self.orig_cloud.list_hypervisors())
 
         self.image_alt = self.publish_image_alt()
         self.flavor_alt = self.create_flavor_alt()
         LOGGER.debug("flavor: %s", self.flavor_alt)
 
         self.conf_file = conf_utils.configure_verifier(self.deployment_dir)
+        if not self.conf_file:
+            raise Exception("Tempest verifier configuring failed")
         conf_utils.configure_tempest_update_params(
-            self.conf_file, network_name=self.network.name,
+            self.conf_file,
             image_id=self.image.id,
             flavor_id=self.flavor.id,
             compute_cnt=compute_cnt,
             image_alt_id=self.image_alt.id,
-            flavor_alt_id=self.flavor_alt.id)
+            flavor_alt_id=self.flavor_alt.id,
+            admin_role_name=self.role_name, cidr=self.cidr,
+            domain_id=self.project.domain.id)
+        self.update_scenario_section()
         self.backup_tempest_config(self.conf_file, self.res_dir)
 
     def run(self, **kwargs):