Add py3 support in tempest and rally
[functest.git] / functest / api / resources / v1 / testcases.py
index 7cc70bb..4334062 100644 (file)
 Resources to handle testcase related requests
 """
 
+import ConfigParser
 import logging
 import os
 import re
-import pkg_resources
 import socket
 import uuid
 
-import ConfigParser
 from flask import jsonify
 from flasgger.utils import swag_from
+import pkg_resources
 
 from functest.api.base import ApiResource
 from functest.api.common import api_utils, thread
 from functest.cli.commands.cli_testcase import Testcase
 from functest.api.database.v1.handlers import TasksHandler
-from functest.utils.constants import CONST
+from functest.utils import config
+from functest.utils import env
 import functest.utils.functest_utils as ft_utils
 
 LOGGER = logging.getLogger(__name__)
@@ -115,45 +116,42 @@ class V1Testcase(ApiResource):
         case_name = args.get('testcase')
         self._update_logging_ini(args.get('task_id'))
 
-        if not os.path.isfile(CONST.__getattribute__('env_active')):
-            raise Exception("Functest environment is not ready.")
+        try:
+            cmd = "run_tests -t {}".format(case_name)
+            runner = ft_utils.execute_command(cmd)
+        except Exception:  # pylint: disable=broad-except
+            result = 'FAIL'
+            LOGGER.exception("Running test case %s failed!", case_name)
+        if runner == os.EX_OK:
+            result = 'PASS'
         else:
-            try:
-                cmd = "run_tests -t {}".format(case_name)
-                runner = ft_utils.execute_command(cmd)
-            except Exception:  # pylint: disable=broad-except
-                result = 'FAIL'
-                LOGGER.exception("Running test case %s failed!", case_name)
-            if runner == os.EX_OK:
-                result = 'PASS'
-            else:
-                result = 'FAIL'
-
-            env_info = {
-                'installer': CONST.__getattribute__('INSTALLER_TYPE'),
-                'scenario': CONST.__getattribute__('DEPLOY_SCENARIO'),
-                'build_tag': CONST.__getattribute__('BUILD_TAG'),
-                'ci_loop': CONST.__getattribute__('CI_LOOP')
-            }
-            result = {
-                'task_id': args.get('task_id'),
-                'testcase': case_name,
-                'env_info': env_info,
-                'result': result
-            }
-
-            return {'result': result}
+            result = 'FAIL'
+
+        env_info = {
+            'installer': env.get('INSTALLER_TYPE'),
+            'scenario': env.get('DEPLOY_SCENARIO'),
+            'build_tag': env.get('BUILD_TAG'),
+            'ci_loop': env.get('CI_LOOP')
+        }
+        result = {
+            'task_id': args.get('task_id'),
+            'testcase': case_name,
+            'env_info': env_info,
+            'result': result
+        }
+
+        return {'result': result}
 
     def _update_logging_ini(self, task_id):  # pylint: disable=no-self-use
         """ Update the log file for each task"""
-        config = ConfigParser.RawConfigParser()
-        config.read(
-            pkg_resources.resource_filename('functest', 'ci/logging.ini'))
-        log_path = os.path.join(CONST.__getattribute__('dir_results'),
+        rconfig = ConfigParser.RawConfigParser()
+        rconfig.read(
+            pkg_resources.resource_filename('xtesting', 'ci/logging.ini'))
+        log_path = os.path.join(getattr(config.CONF, 'dir_results'),
                                 '{}.log'.format(task_id))
-        config.set('handler_file', 'args', '("{}",)'.format(log_path))
+        rconfig.set('handler_file', 'args', '("{}",)'.format(log_path))
 
         with open(
             pkg_resources.resource_filename(
-                'functest', 'ci/logging.ini'), 'wb') as configfile:
-            config.write(configfile)
+                'xtesting', 'ci/logging.ini'), 'wb') as configfile:
+            rconfig.write(configfile)