Ejemplo n.º 1
0
    def get(self, task_id):  # pylint: disable=no-self-use
        """ GET the result of the task id """
        try:
            uuid.UUID(task_id)
        except ValueError:
            return api_utils.result_handler(status=1, data='Invalid task id')

        task_handler = TasksHandler()
        try:
            task = task_handler.get_task_by_taskid(task_id)
        except ValueError:
            return api_utils.result_handler(status=1, data='No such task id')

        status = task.status
        LOGGER.debug('Task status is: %s', status)

        if status not in ['IN PROGRESS', 'FAIL', 'FINISHED']:
            return api_utils.result_handler(status=1,
                                            data='internal server error')
        if status == 'IN PROGRESS':
            result = {'status': status, 'result': ''}
        elif status == 'FAIL':
            result = {'status': status, 'error': task.error}
        else:
            result = {'status': status, 'result': json.loads(task.result)}

        return jsonify(result)
Ejemplo n.º 2
0
    def get(self, task_id):  # pylint: disable=no-self-use
        """ GET the log of the task id """
        try:
            uuid.UUID(task_id)
        except ValueError:
            return api_utils.result_handler(status=1, data='Invalid task id')

        task_handler = TasksHandler()
        try:
            task = task_handler.get_task_by_taskid(task_id)
        except ValueError:
            return api_utils.result_handler(status=1, data='No such task id')

        task_log_dir = CONST.__getattribute__('dir_results')

        try:
            with open(os.path.join(task_log_dir, '{}.log'.format(task_id)),
                      'r') as log_file:
                data = log_file.readlines()
        except OSError as err:
            if err.errno == errno.ENOENT:
                return api_utils.result_handler(status=1,
                                                data='Log file does not exist')

            return api_utils.result_handler(status=1,
                                            data='Error with log file')

        return_data = {'data': data}

        return api_utils.result_handler(status=task.status, data=return_data)
Ejemplo n.º 3
0
    def run_test_case(self, args):
        """ Run a testcase """
        try:
            case_name = args['testcase']
        except KeyError:
            return api_utils.result_handler(
                status=1, data='testcase name must be provided')

        testcase = Testcase().show(case_name)
        if not testcase:
            return api_utils.result_handler(
                status=1,
                data="The test case '%s' does not exist or is not supported" %
                case_name)

        task_id = str(uuid.uuid4())

        task_args = {'testcase': case_name, 'task_id': task_id}

        task_args.update(args.get('opts', {}))

        task_thread = thread.TaskThread(self._run, task_args, TasksHandler())
        task_thread.start()

        result = {'testcase': case_name, 'task_id': task_id}
        return jsonify({'result': result})
Ejemplo n.º 4
0
    def update_openrc(self, args):  # pylint: disable=no-self-use
        """ Used to update the OpenStack RC file """
        try:
            openrc_vars = args['openrc']
        except KeyError:
            return api_utils.result_handler(status=0,
                                            data='openrc must be provided')
        else:
            if not isinstance(openrc_vars, collections.Mapping):
                return api_utils.result_handler(status=0,
                                                data='args should be a dict')

        lines = ['export {}={}\n'.format(k, v) for k, v in openrc_vars.items()]

        rc_file = constants.ENV_FILE
        with open(rc_file, 'w') as creds_file:
            creds_file.writelines(lines)

        LOGGER.info("Sourcing the OpenStack RC file...")
        try:
            run_tests.Runner.source_envfile(rc_file)
        except Exception as err:  # pylint: disable=broad-except
            LOGGER.exception('Failed to source the OpenStack RC file')
            return api_utils.result_handler(status=0, data=str(err))

        return api_utils.result_handler(status=0,
                                        data='Update openrc successfully')
Ejemplo n.º 5
0
 def prepare(self, args):  # pylint: disable=no-self-use, unused-argument
     """ Prepare environment """
     try:
         ft_utils.execute_command("prepare_env start")
     except Exception as err:  # pylint: disable=broad-except
         return api_utils.result_handler(status=1, data=str(err))
     return api_utils.result_handler(status=0,
                                     data="Prepare env successfully")
Ejemplo n.º 6
0
 def _dispatch(self, args, action):
     """
     Dynamically load the classes with reflection and
     obtain corresponding methods
     """
     try:
         return getattr(self, action)(args)
     except AttributeError:
         api_utils.result_handler(status=1, data='No such action')
Ejemplo n.º 7
0
 def get(self, tier_name):  # pylint: disable=no-self-use
     """ GET all testcases within given tier """
     tier_info = Tier().show(tier_name)
     if not tier_info:
         return api_utils.result_handler(
             status=1,
             data="The tier with name '%s' does not exist." % tier_name)
     testcases = Tier().gettests(tier_name)
     result = {'tier': tier_name, 'testcases': testcases}
     return jsonify(result)
Ejemplo n.º 8
0
    def update_hosts(self, hosts_info):  # pylint: disable=no-self-use
        """ Update hosts info """

        if not isinstance(hosts_info, dict):
            return api_utils.result_handler(
                status=1, data='Error, args should be a dict')

        for key, value in hosts_info.items():
            if key:
                try:
                    IPy.IP(value)
                except Exception:  # pylint: disable=broad-except
                    return api_utils.result_handler(
                        status=1, data='The IP %s is invalid' % value)
            else:
                return api_utils.result_handler(status=1,
                                                data='Domain name is absent')

        try:
            functest_flag = "# SUT hosts info for Functest"
            hosts_list = ('\n{} {} {}'.format(ip, host_name, functest_flag)
                          for host_name, ip in hosts_info.items())

            with open("/etc/hosts", 'r') as file_hosts:
                origin_lines = [
                    line for line in file_hosts if functest_flag not in line
                ]

            with open("/etc/hosts", 'w') as file_hosts:
                file_hosts.writelines(origin_lines)
                file_hosts.write(functest_flag)
                file_hosts.writelines(hosts_list)
        except Exception:  # pylint: disable=broad-except
            return api_utils.result_handler(
                status=1, data='Error when updating hosts info')
        else:
            return api_utils.result_handler(
                status=0, data='Update hosts info successfully')
Ejemplo n.º 9
0
 def get(self, tier_name):  # pylint: disable=no-self-use
     """ GET the info of one tier """
     tier_info = Tier().show(tier_name)
     if not tier_info:
         return api_utils.result_handler(
             status=1,
             data="The tier with name '%s' does not exist." % tier_name)
     tier_info.__dict__.pop('name')
     tier_info.__dict__.pop('tests_array')
     tier_info.__dict__.pop('skipped_tests_array')
     testcases = Tier().gettests(tier_name)
     result = {'tier': tier_name, 'testcases': testcases}
     result.update(tier_info.__dict__)
     return jsonify(result)
Ejemplo n.º 10
0
    def get(self, task_id):  # pylint: disable=no-self-use
        """ GET the log of the task id """
        try:
            uuid.UUID(task_id)
        except ValueError:
            return api_utils.result_handler(status=1, data='Invalid task id')

        task_handler = TasksHandler()
        try:
            task = task_handler.get_task_by_taskid(task_id)
        except ValueError:
            return api_utils.result_handler(status=1, data='No such task id')

        task_log_dir = getattr(config.CONF, 'dir_results')
        # pylint: disable=maybe-no-member
        index = int(self._get_args().get('index', 0))

        try:
            with open(os.path.join(task_log_dir, '{}.log'.format(task_id)),
                      'r') as log_file:
                log_file.seek(index)
                data = log_file.readlines()
                index = log_file.tell()
        except OSError as err:
            if err.errno == errno.ENOENT:
                return api_utils.result_handler(status=1,
                                                data='Log file does not exist')

            return api_utils.result_handler(status=1,
                                            data='Error with log file')

        return_data = {'data': data, 'index': index}

        switcher = {'IN PROGRESS': 0, 'FAIL': 1, 'FINISHED': 2}

        return api_utils.result_handler(status=switcher.get(task.status),
                                        data=return_data)
Ejemplo n.º 11
0
    def get(self, testcase_name):  # pylint: disable=no-self-use
        """ GET the info of one testcase"""
        testcase = Testcase().show(testcase_name)
        if not testcase:
            return api_utils.result_handler(
                status=1,
                data="The test case '%s' does not exist or is not supported" %
                testcase_name)

        testcase_info = api_utils.change_obj_to_dict(testcase)
        dependency_dict = api_utils.change_obj_to_dict(
            testcase_info.get('dependency'))
        testcase_info.pop('name')
        testcase_info.pop('dependency')
        result = {'testcase': testcase_name}
        result.update(testcase_info)
        result.update({'dependency': dependency_dict})
        return jsonify(result)
Ejemplo n.º 12
0
    def run_test_case(self, args):
        """ Run a testcase """
        try:
            case_name = args['testcase']
        except KeyError:
            return api_utils.result_handler(
                status=1, data='testcase name must be provided')

        task_id = str(uuid.uuid4())

        task_args = {'testcase': case_name, 'task_id': task_id}

        task_args.update(args.get('opts', {}))

        task_thread = thread.TaskThread(self._run, task_args, TasksHandler())
        task_thread.start()

        results = {'testcase': case_name, 'task_id': task_id}
        return jsonify(results)