Merge "Define create_snapshot() and clean() in TestCase"
[functest.git] / functest / opnfv_tests / openstack / tempest / tempest.py
index d0119c9..cb8e9b4 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,6 +8,9 @@
 # http://www.apache.org/licenses/LICENSE-2.0
 #
 
+from __future__ import division
+
+import logging
 import os
 import re
 import shutil
@@ -16,20 +19,19 @@ import time
 
 import yaml
 
-from functest.core import testcase_base
+from functest.core import testcase
 from functest.opnfv_tests.openstack.tempest import conf_utils
 from functest.utils.constants import CONST
-import functest.utils.functest_logger as ft_logger
 import functest.utils.functest_utils as ft_utils
 
 """ logging configuration """
-logger = ft_logger.Logger("Tempest").getLogger()
+logger = logging.getLogger(__name__)
 
 
-class TempestCommon(testcase_base.TestCase):
+class TempestCommon(testcase.OSGCTestCase):
 
-    def __init__(self):
-        super(TempestCommon, self).__init__()
+    def __init__(self, **kwargs):
+        super(TempestCommon, self).__init__(**kwargs)
         self.MODE = ""
         self.OPTION = ""
         self.VERIFIER_ID = conf_utils.get_verifier_id()
@@ -79,8 +81,8 @@ class TempestCommon(testcase_base.TestCase):
         result_file = open(conf_utils.TEMPEST_LIST, 'w')
         black_tests = []
         try:
-            installer_type = CONST.INSTALLER_TYPE
-            deploy_scenario = CONST.DEPLOY_SCENARIO
+            installer_type = CONST.__getattribute__('INSTALLER_TYPE')
+            deploy_scenario = CONST.__getattribute__('DEPLOY_SCENARIO')
             if (bool(installer_type) * bool(deploy_scenario)):
                 # if INSTALLER_TYPE and DEPLOY_SCENARIO are set we read the
                 # file
@@ -117,9 +119,9 @@ class TempestCommon(testcase_base.TestCase):
 
         header = ("Tempest environment:\n"
                   "  SUT: %s\n  Scenario: %s\n  Node: %s\n  Date: %s\n" %
-                  (CONST.INSTALLER_TYPE,
-                   CONST.DEPLOY_SCENARIO,
-                   CONST.NODE_NAME,
+                  (CONST.__getattribute__('INSTALLER_TYPE'),
+                   CONST.__getattribute__('DEPLOY_SCENARIO'),
+                   CONST.__getattribute__('NODE_NAME'),
                    time.strftime("%a %b %d %H:%M:%S %Z %Y")))
 
         f_stdout = open(
@@ -146,7 +148,7 @@ class TempestCommon(testcase_base.TestCase):
                     first_pos = line.index("UUID=") + len("UUID=")
                     last_pos = line.index(") for deployment")
                     self.VERIFICATION_ID = line[first_pos:last_pos]
-                    logger.debug('Verication UUID: %s' % self.VERIFICATION_ID)
+                    logger.debug('Verification UUID: %s', self.VERIFICATION_ID)
                 f_stdout.write(line)
         p.wait()
 
@@ -181,7 +183,13 @@ class TempestCommon(testcase_base.TestCase):
 
         try:
             num_executed = int(num_tests) - int(num_skipped)
-            success_rate = 100 * int(num_success) / int(num_executed)
+            try:
+                self.result = 100 * int(num_success) / int(num_executed)
+            except ZeroDivisionError:
+                logger.error("No test has been executed")
+                self.result = 0
+                return
+
             with open(os.path.join(conf_utils.TEMPEST_RESULTS_DIR,
                                    "tempest.log"), 'r') as logfile:
                 output = logfile.read()
@@ -198,21 +206,17 @@ class TempestCommon(testcase_base.TestCase):
                             "errors": error_logs,
                             "skipped": skipped_testcase}
         except Exception:
-            success_rate = 0
+            self.result = 0
 
-        self.criteria = ft_utils.check_success_rate(
-            self.case_name, success_rate)
-        logger.info("Tempest %s success_rate is %s%%, is marked as %s"
-                    % (self.case_name, success_rate, self.criteria))
+        logger.info("Tempest %s success_rate is %s%%"
+                    % (self.case_name, self.result))
 
     def run(self):
 
         self.start_time = time.time()
-
-        if not os.path.exists(conf_utils.TEMPEST_RESULTS_DIR):
-            os.makedirs(conf_utils.TEMPEST_RESULTS_DIR)
-
         try:
+            if not os.path.exists(conf_utils.TEMPEST_RESULTS_DIR):
+                os.makedirs(conf_utils.TEMPEST_RESULTS_DIR)
             image_and_flavor = conf_utils.create_tempest_resources()
             conf_utils.configure_tempest(
                 self.DEPLOYMENT_DIR,
@@ -223,10 +227,10 @@ class TempestCommon(testcase_base.TestCase):
             self.apply_tempest_blacklist()
             self.run_verifier_tests()
             self.parse_verifier_result()
-            res = testcase_base.TestCase.EX_OK
+            res = testcase.TestCase.EX_OK
         except Exception as e:
             logger.error('Error with run: %s' % e)
-            res = testcase_base.TestCase.EX_RUN_ERROR
+            res = testcase.TestCase.EX_RUN_ERROR
 
         self.stop_time = time.time()
         return res
@@ -234,53 +238,60 @@ class TempestCommon(testcase_base.TestCase):
 
 class TempestSmokeSerial(TempestCommon):
 
-    def __init__(self):
-        TempestCommon.__init__(self)
-        self.case_name = "tempest_smoke_serial"
+    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 TempestSmokeParallel(TempestCommon):
 
-    def __init__(self):
-        TempestCommon.__init__(self)
-        self.case_name = "tempest_smoke_parallel"
+    def __init__(self, **kwargs):
+        if "case_name" not in kwargs:
+            kwargs["case_name"] = 'tempest_smoke_parallel'
+        TempestCommon.__init__(self, **kwargs)
         self.MODE = "smoke"
         self.OPTION = ""
 
 
 class TempestFullParallel(TempestCommon):
 
-    def __init__(self):
-        TempestCommon.__init__(self)
-        self.case_name = "tempest_full_parallel"
+    def __init__(self, **kwargs):
+        if "case_name" not in kwargs:
+            kwargs["case_name"] = 'tempest_full_parallel'
+        TempestCommon.__init__(self, **kwargs)
         self.MODE = "full"
 
 
 class TempestMultisite(TempestCommon):
 
-    def __init__(self):
-        TempestCommon.__init__(self)
-        self.case_name = "multisite"
+    def __init__(self, **kwargs):
+        if "case_name" not in kwargs:
+            kwargs["case_name"] = 'multisite'
+        TempestCommon.__init__(self, **kwargs)
         self.MODE = "feature_multisite"
         self.OPTION = "--concurrency 1"
-        conf_utils.install_verifier_ext(CONST.dir_repo_kingbird)
+        conf_utils.install_verifier_ext(
+            CONST.__getattribute__('dir_repo_kingbird'))
 
 
 class TempestCustom(TempestCommon):
 
-    def __init__(self):
-        TempestCommon.__init__(self)
-        self.case_name = "tempest_custom"
+    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):
 
-    def __init__(self):
-        TempestCommon.__init__(self)
-        self.case_name = "tempest_defcore"
+    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"