Temporarily disable shelve
[functest.git] / functest / opnfv_tests / openstack / tempest / tempest.py
index ce53dde..216709f 100644 (file)
@@ -35,13 +35,14 @@ class TempestCommon(singlevm.VmReady1):
     # pylint: disable=too-many-instance-attributes
     """TempestCommon testcases implementation class."""
 
-    TEMPEST_RESULTS_DIR = os.path.join(
-        getattr(config.CONF, 'dir_results'), 'tempest')
+    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.mode = ""
-        self.option = []
         self.verifier_id = conf_utils.get_verifier_id()
         self.verifier_repo_dir = conf_utils.get_verifier_repo_dir(
             self.verifier_id)
@@ -49,12 +50,47 @@ class TempestCommon(singlevm.VmReady1):
         self.deployment_dir = conf_utils.get_verifier_deployment_dir(
             self.verifier_id, self.deployment_id)
         self.verification_id = None
-        self.res_dir = TempestCommon.TEMPEST_RESULTS_DIR
+        self.res_dir = os.path.join(
+            getattr(config.CONF, 'dir_results'), self.case_name)
         self.raw_list = os.path.join(self.res_dir, 'test_raw_list.txt')
         self.list = os.path.join(self.res_dir, 'test_list.txt')
         self.conf_file = None
         self.image_alt = None
         self.flavor_alt = None
+        self.services = []
+        try:
+            self.services = kwargs['run']['args']['services']
+        except Exception:  # pylint: disable=broad-except
+            pass
+        self.neutron_extensions = []
+        try:
+            self.neutron_extensions = kwargs['run']['args'][
+                'neutron_extensions']
+        except Exception:  # pylint: disable=broad-except
+            pass
+
+    def check_services(self):
+        """Check the mandatory services."""
+        for service in self.services:
+            try:
+                self.cloud.search_services(service)[0]
+            except Exception:  # pylint: disable=broad-except
+                self.is_skipped = True
+                break
+
+    def check_extensions(self):
+        """Check the mandatory network extensions."""
+        extensions = self.cloud.get_network_extensions()
+        for network_extension in self.neutron_extensions:
+            if network_extension not in extensions:
+                LOGGER.warning(
+                    "Cannot find Neutron extension: %s", network_extension)
+                self.is_skipped = True
+                break
+
+    def check_requirements(self):
+        self.check_services()
+        self.check_extensions()
 
     @staticmethod
     def read_file(filename):
@@ -77,10 +113,10 @@ class TempestCommon(singlevm.VmReady1):
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.STDOUT)
         for line in proc.stdout:
+            LOGGER.info(line.rstrip())
             new_line = line.replace(' ', '').split('|')
             if 'Tests' in new_line:
                 break
-            LOGGER.info(line)
             if 'Testscount' in new_line:
                 result['num_tests'] = int(new_line[2])
             elif 'Success' in new_line:
@@ -101,11 +137,11 @@ class TempestCommon(singlevm.VmReady1):
         shutil.copyfile(conf_file,
                         os.path.join(res_dir, 'tempest.conf'))
 
-    def generate_test_list(self):
+    def generate_test_list(self, **kwargs):
         """Generate test list based on the test mode."""
         LOGGER.debug("Generating test case list...")
         self.backup_tempest_config(self.conf_file, '/etc')
-        if self.mode == 'custom':
+        if kwargs.get('mode') == 'custom':
             if os.path.isfile(conf_utils.TEMPEST_CUSTOM):
                 shutil.copyfile(
                     conf_utils.TEMPEST_CUSTOM, self.list)
@@ -113,13 +149,9 @@ class TempestCommon(singlevm.VmReady1):
                 raise Exception("Tempest test list file %s NOT found."
                                 % conf_utils.TEMPEST_CUSTOM)
         else:
-            if self.mode == 'smoke':
-                testr_mode = r"'^tempest\.(api|scenario).*\[.*\bsmoke\b.*\]$'"
-            elif self.mode == 'full':
-                testr_mode = r"'^tempest\.'"
-            else:
-                testr_mode = self.mode
-            cmd = "(cd {0}; stestr list {1} >{2} 2>/dev/null)".format(
+            testr_mode = kwargs.get(
+                'mode', r'^tempest\.(api|scenario).*\[.*\bsmoke\b.*\]$')
+            cmd = "(cd {0}; stestr list '{1}' >{2} 2>/dev/null)".format(
                 self.verifier_repo_dir, testr_mode, self.list)
             output = subprocess.check_output(cmd, shell=True)
             LOGGER.info("%s\n%s", cmd, output)
@@ -164,41 +196,40 @@ class TempestCommon(singlevm.VmReady1):
                 result_file.write(str(cases_line) + '\n')
         result_file.close()
 
-    def run_verifier_tests(self):
+    def run_verifier_tests(self, **kwargs):
         """Execute tempest test cases."""
         cmd = ["rally", "verify", "start", "--load-list",
                self.list]
-        cmd.extend(self.option)
+        cmd.extend(kwargs.get('option', []))
         LOGGER.info("Starting Tempest test suite: '%s'.", cmd)
 
         f_stdout = open(
             os.path.join(self.res_dir, "tempest.log"), 'w+')
-        f_stderr = open(
-            os.path.join(self.res_dir,
-                         "tempest-error.log"), 'w+')
 
         proc = subprocess.Popen(
             cmd,
             stdout=subprocess.PIPE,
-            stderr=f_stderr,
+            stderr=subprocess.STDOUT,
             bufsize=1)
 
         with proc.stdout:
             for line in iter(proc.stdout.readline, b''):
                 if re.search(r"\} tempest\.", line):
-                    LOGGER.info(line.replace('\n', ''))
+                    LOGGER.info(line.rstrip())
                 elif re.search(r'(?=\(UUID=(.*)\))', line):
                     self.verification_id = re.search(
                         r'(?=\(UUID=(.*)\))', line).group(1)
-                    LOGGER.info('Verification UUID: %s', self.verification_id)
                 f_stdout.write(line)
         proc.wait()
-
         f_stdout.close()
-        f_stderr.close()
 
         if self.verification_id is None:
             raise Exception('Verification UUID not found')
+        LOGGER.info('Verification UUID: %s', self.verification_id)
+
+        shutil.copy(
+            "{}/tempest.log".format(self.deployment_dir),
+            "{}/tempest.debug.log".format(self.res_dir))
 
     def parse_verifier_result(self):
         """Parse and save test results."""
@@ -216,7 +247,7 @@ class TempestCommon(singlevm.VmReady1):
                     return
 
             with open(os.path.join(self.res_dir,
-                                   "tempest-error.log"), 'r') as logfile:
+                                   "tempest.log"), 'r') as logfile:
                 output = logfile.read()
 
             success_testcases = []
@@ -254,6 +285,57 @@ class TempestCommon(singlevm.VmReady1):
         subprocess.Popen(cmd, stdout=subprocess.PIPE,
                          stderr=subprocess.STDOUT)
 
+    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(
+            self.image.name))
+        with open(rally_conf, 'wb') as config_file:
+            rconfig.write(config_file)
+
+    def update_default_role(self, rally_conf='/etc/rally/rally.conf'):
+        """Detect and update the default role if required"""
+        role = self.get_default_role(self.cloud)
+        if not role:
+            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)
+        with open(rally_conf, 'wb') as config_file:
+            rconfig.write(config_file)
+
+    def update_rally_logs(self, rally_conf='/etc/rally/rally.conf'):
+        """Print rally logs in res dir"""
+        if not os.path.exists(self.res_dir):
+            os.makedirs(self.res_dir)
+        rconfig = configparser.RawConfigParser()
+        rconfig.read(rally_conf)
+        rconfig.set('DEFAULT', 'log-file', 'rally.log')
+        rconfig.set('DEFAULT', 'log_dir', self.res_dir)
+        with open(rally_conf, 'wb') as config_file:
+            rconfig.write(config_file)
+
+    @staticmethod
+    def clean_rally_conf(rally_conf='/etc/rally/rally.conf'):
+        """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('DEFAULT', 'log-file'):
+            rconfig.remove_option('DEFAULT', 'log-file')
+        if rconfig.has_option('DEFAULT', 'log_dir'):
+            rconfig.remove_option('DEFAULT', 'log_dir')
+        with open(rally_conf, '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
@@ -263,33 +345,13 @@ class TempestCommon(singlevm.VmReady1):
             os.makedirs(self.res_dir)
         compute_cnt = len(self.cloud.list_hypervisors())
 
-        LOGGER.info("Creating two images for Tempest suite")
-
-        meta = getattr(
-            config.CONF, '{}_extra_properties'.format(self.case_name), None)
-        self.image_alt = self.cloud.create_image(
-            '{}-img_alt_{}'.format(self.case_name, self.guid),
-            filename=getattr(
-                config.CONF, '{}_image'.format(self.case_name),
-                self.filename),
-            meta=meta)
-        LOGGER.debug("image_alt: %s", self.image_alt)
-
-        self.flavor_alt = self.orig_cloud.create_flavor(
-            '{}-flavor_alt_{}'.format(self.case_name, self.guid),
-            getattr(config.CONF, '{}_flavor_ram'.format(self.case_name),
-                    self.flavor_ram),
-            getattr(config.CONF, '{}_flavor_vcpus'.format(self.case_name),
-                    self.flavor_vcpus),
-            getattr(config.CONF, '{}_flavor_disk'.format(self.case_name),
-                    self.flavor_disk))
+        self.image_alt = self.publish_image_alt()
+        self.flavor_alt = self.create_flavor_alt()
         LOGGER.debug("flavor: %s", self.flavor_alt)
-        self.orig_cloud.set_flavor_specs(
-            self.flavor_alt.id, getattr(config.CONF, 'flavor_extra_specs', {}))
 
         self.conf_file = conf_utils.configure_verifier(self.deployment_dir)
         conf_utils.configure_tempest_update_params(
-            self.conf_file, network_name=self.network.id,
+            self.conf_file, network_name=self.network.name,
             image_id=self.image.id,
             flavor_id=self.flavor.id,
             compute_cnt=compute_cnt,
@@ -300,16 +362,22 @@ class TempestCommon(singlevm.VmReady1):
     def run(self, **kwargs):
         self.start_time = time.time()
         try:
-            super(TempestCommon, self).run(**kwargs)
+            assert super(TempestCommon, self).run(
+                **kwargs) == testcase.TestCase.EX_OK
+            self.update_rally_regex()
+            self.update_default_role()
+            self.update_rally_logs()
+            shutil.copy("/etc/rally/rally.conf", self.res_dir)
             self.configure(**kwargs)
-            self.generate_test_list()
+            self.generate_test_list(**kwargs)
             self.apply_tempest_blacklist()
-            self.run_verifier_tests()
+            self.run_verifier_tests(**kwargs)
             self.parse_verifier_result()
             self.generate_report()
             res = testcase.TestCase.EX_OK
         except Exception:  # pylint: disable=broad-except
             LOGGER.exception('Error with run')
+            self.result = 0
             res = testcase.TestCase.EX_RUN_ERROR
         self.stop_time = time.time()
         return res
@@ -318,88 +386,9 @@ class TempestCommon(singlevm.VmReady1):
         """
         Cleanup all OpenStack objects. Should be called on completion.
         """
+        self.clean_rally_conf()
+        if self.image_alt:
+            self.cloud.delete_image(self.image_alt)
+        if self.flavor_alt:
+            self.orig_cloud.delete_flavor(self.flavor_alt.id)
         super(TempestCommon, self).clean()
-        self.cloud.delete_image(self.image_alt)
-        self.orig_cloud.delete_flavor(self.flavor_alt.id)
-
-
-class TempestSmokeSerial(TempestCommon):
-    """Tempest smoke serial testcase implementation."""
-    def __init__(self, **kwargs):
-        if "case_name" not in kwargs:
-            kwargs["case_name"] = 'tempest_smoke_serial'
-        TempestCommon.__init__(self, **kwargs)
-        self.mode = "smoke"
-        self.option = ["--concurrency", "1"]
-
-
-class TempestNeutronTrunk(TempestCommon):
-    """Tempest neutron trunk testcase implementation."""
-    def __init__(self, **kwargs):
-        if "case_name" not in kwargs:
-            kwargs["case_name"] = 'neutron_trunk'
-        TempestCommon.__init__(self, **kwargs)
-        self.mode = "'neutron_tempest_plugin.(api|scenario).test_trunk'"
-        self.res_dir = os.path.join(
-            getattr(config.CONF, 'dir_results'), 'neutron_trunk')
-        self.raw_list = os.path.join(self.res_dir, 'test_raw_list.txt')
-        self.list = os.path.join(self.res_dir, 'test_list.txt')
-
-    def configure(self, **kwargs):
-        super(TempestNeutronTrunk, self).configure(**kwargs)
-        rconfig = configparser.RawConfigParser()
-        rconfig.read(self.conf_file)
-        rconfig.set('network-feature-enabled', 'api_extensions', 'all')
-        with open(self.conf_file, 'wb') as config_file:
-            rconfig.write(config_file)
-
-
-class TempestBarbican(TempestCommon):
-    """Tempest Barbican testcase implementation."""
-    def __init__(self, **kwargs):
-        if "case_name" not in kwargs:
-            kwargs["case_name"] = 'barbican'
-        TempestCommon.__init__(self, **kwargs)
-        self.mode = "'barbican_tempest_plugin.tests.(api|scenario)'"
-        self.res_dir = os.path.join(
-            getattr(config.CONF, 'dir_results'), 'barbican')
-        self.raw_list = os.path.join(self.res_dir, 'test_raw_list.txt')
-        self.list = os.path.join(self.res_dir, 'test_list.txt')
-
-
-class TempestSmokeParallel(TempestCommon):
-    """Tempest smoke parallel testcase implementation."""
-    def __init__(self, **kwargs):
-        if "case_name" not in kwargs:
-            kwargs["case_name"] = 'tempest_smoke_parallel'
-        TempestCommon.__init__(self, **kwargs)
-        self.mode = "smoke"
-
-
-class TempestFullParallel(TempestCommon):
-    """Tempest full parallel testcase implementation."""
-    def __init__(self, **kwargs):
-        if "case_name" not in kwargs:
-            kwargs["case_name"] = 'tempest_full_parallel'
-        TempestCommon.__init__(self, **kwargs)
-        self.mode = "full"
-
-
-class TempestCustom(TempestCommon):
-    """Tempest custom testcase implementation."""
-    def __init__(self, **kwargs):
-        if "case_name" not in kwargs:
-            kwargs["case_name"] = 'tempest_custom'
-        TempestCommon.__init__(self, **kwargs)
-        self.mode = "custom"
-        self.option = ["--concurrency", "1"]
-
-
-class TempestDefcore(TempestCommon):
-    """Tempest Defcore testcase implementation."""
-    def __init__(self, **kwargs):
-        if "case_name" not in kwargs:
-            kwargs["case_name"] = 'tempest_defcore'
-        TempestCommon.__init__(self, **kwargs)
-        self.mode = "defcore"
-        self.option = ["--concurrency", "1"]