176e6818aSopenharmony_ci#!/usr/bin/env python3
276e6818aSopenharmony_ci# coding=utf-8
376e6818aSopenharmony_ci
476e6818aSopenharmony_ci#
576e6818aSopenharmony_ci# Copyright (c) 2022 Huawei Device Co., Ltd.
676e6818aSopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
776e6818aSopenharmony_ci# you may not use this file except in compliance with the License.
876e6818aSopenharmony_ci# You may obtain a copy of the License at
976e6818aSopenharmony_ci#
1076e6818aSopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
1176e6818aSopenharmony_ci#
1276e6818aSopenharmony_ci# Unless required by applicable law or agreed to in writing, software
1376e6818aSopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
1476e6818aSopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1576e6818aSopenharmony_ci# See the License for the specific language governing permissions and
1676e6818aSopenharmony_ci# limitations under the License.
1776e6818aSopenharmony_ci#
1876e6818aSopenharmony_ci
1976e6818aSopenharmony_ciimport copy
2076e6818aSopenharmony_ciimport os
2176e6818aSopenharmony_ciimport sys
2276e6818aSopenharmony_ci
2376e6818aSopenharmony_cifrom xdevice import get_plugin
2476e6818aSopenharmony_cifrom xdevice import ModeType
2576e6818aSopenharmony_cifrom xdevice import ConfigConst
2676e6818aSopenharmony_cifrom xdevice import DeviceLabelType
2776e6818aSopenharmony_cifrom xdevice import TestExecType
2876e6818aSopenharmony_cifrom xdevice import DeviceError
2976e6818aSopenharmony_cifrom xdevice import ParamError
3076e6818aSopenharmony_cifrom xdevice import ReportException
3176e6818aSopenharmony_cifrom xdevice import ExecuteTerminate
3276e6818aSopenharmony_cifrom xdevice import IDriver
3376e6818aSopenharmony_cifrom xdevice import platform_logger
3476e6818aSopenharmony_cifrom xdevice import Plugin
3576e6818aSopenharmony_cifrom xdevice import JsonParser
3676e6818aSopenharmony_cifrom xdevice import get_config_value
3776e6818aSopenharmony_cifrom xdevice import do_module_kit_setup
3876e6818aSopenharmony_cifrom xdevice import do_module_kit_teardown
3976e6818aSopenharmony_cifrom xdevice import get_filename_extension
4076e6818aSopenharmony_cifrom xdevice import get_file_absolute_path
4176e6818aSopenharmony_cifrom xdevice import get_kit_instances
4276e6818aSopenharmony_cifrom xdevice import check_result_report
4376e6818aSopenharmony_cifrom xdevice import check_mode
4476e6818aSopenharmony_cifrom xdevice import SuiteReporter
4576e6818aSopenharmony_cifrom xdevice import CaseEnd
4676e6818aSopenharmony_cifrom xdevice import Binder
4776e6818aSopenharmony_cifrom xdevice import HostDrivenTestType
4876e6818aSopenharmony_cifrom xdevice import Variables
4976e6818aSopenharmony_cifrom devicetest.constants import CKit
5076e6818aSopenharmony_cifrom devicetest.core.constants import DeviceTestMode
5176e6818aSopenharmony_cifrom devicetest.error import ErrorMessage
5276e6818aSopenharmony_ci
5376e6818aSopenharmony_ci__all__ = ["DeviceTestDriver", "DeviceTestSuiteDriver"]
5476e6818aSopenharmony_ciLOG = platform_logger("DeviceTest")
5576e6818aSopenharmony_ciPY_SUFFIX = ".py"
5676e6818aSopenharmony_ciPYD_SUFFIX = ".pyd"
5776e6818aSopenharmony_ciPYC_SUFFIX = ".pyc"
5876e6818aSopenharmony_ci
5976e6818aSopenharmony_ci
6076e6818aSopenharmony_cidef _get_dict_test_list(module_path, file_name):
6176e6818aSopenharmony_ci    test_list = []
6276e6818aSopenharmony_ci    for root, _, files in os.walk(module_path):
6376e6818aSopenharmony_ci        for _file in files:
6476e6818aSopenharmony_ci            if (_file.endswith(".py") or _file.endswith(".pyd")) \
6576e6818aSopenharmony_ci                    and file_name == os.path.splitext(_file)[0]:
6676e6818aSopenharmony_ci                test_list.append(os.path.join(root, _file))
6776e6818aSopenharmony_ci    return test_list
6876e6818aSopenharmony_ci
6976e6818aSopenharmony_ci
7076e6818aSopenharmony_cidef start_smart_perf(config, kits):
7176e6818aSopenharmony_ci    if not hasattr(config, ConfigConst.kits_in_module):
7276e6818aSopenharmony_ci        return
7376e6818aSopenharmony_ci    if CKit.smartperf not in config.get(ConfigConst.kits_in_module):
7476e6818aSopenharmony_ci        return
7576e6818aSopenharmony_ci    sp_kits = get_plugin(Plugin.TEST_KIT, CKit.smartperf)[0]
7676e6818aSopenharmony_ci    sp_kits.target_name = config.get("bundle_name", "")
7776e6818aSopenharmony_ci    param_config = config.get(ConfigConst.kits_params).get(CKit.smartperf, "")
7876e6818aSopenharmony_ci    sp_kits.__check_config__(param_config)
7976e6818aSopenharmony_ci    kits.insert(0, sp_kits)
8076e6818aSopenharmony_ci
8176e6818aSopenharmony_ci
8276e6818aSopenharmony_cidef handle_test_args(config, request):
8376e6818aSopenharmony_ci    pass
8476e6818aSopenharmony_ci
8576e6818aSopenharmony_ci
8676e6818aSopenharmony_cidef do_driver_execute(driver_obj: IDriver, request):
8776e6818aSopenharmony_ci    try:
8876e6818aSopenharmony_ci        # delete sys devicetest mode
8976e6818aSopenharmony_ci        if hasattr(sys, DeviceTestMode.MODE):
9076e6818aSopenharmony_ci            delattr(sys, DeviceTestMode.MODE)
9176e6818aSopenharmony_ci
9276e6818aSopenharmony_ci        # set self.config
9376e6818aSopenharmony_ci        driver_obj.config = request.config
9476e6818aSopenharmony_ci        driver_obj.config.devices = request.get_devices()
9576e6818aSopenharmony_ci        if request.get("exectype") == TestExecType.device_test and \
9676e6818aSopenharmony_ci                not driver_obj.config.devices:
9776e6818aSopenharmony_ci            err_msg = ErrorMessage.TestCase.Code_0203009
9876e6818aSopenharmony_ci            LOG.error(err_msg)
9976e6818aSopenharmony_ci            raise ParamError(err_msg)
10076e6818aSopenharmony_ci
10176e6818aSopenharmony_ci        # get source, json config and kits
10276e6818aSopenharmony_ci        if request.get_config_file():
10376e6818aSopenharmony_ci            source = request.get_config_file()
10476e6818aSopenharmony_ci            LOG.debug("Test config file path: %s" % source)
10576e6818aSopenharmony_ci        else:
10676e6818aSopenharmony_ci            source = request.get_source_string()
10776e6818aSopenharmony_ci            LOG.debug("Test String: %s" % source)
10876e6818aSopenharmony_ci
10976e6818aSopenharmony_ci        if not source:
11076e6818aSopenharmony_ci            err_msg = ErrorMessage.TestCase.Code_0203010.format(request.get_source_file())
11176e6818aSopenharmony_ci            LOG.error(err_msg)
11276e6818aSopenharmony_ci            raise ParamError(err_msg)
11376e6818aSopenharmony_ci
11476e6818aSopenharmony_ci        json_config = JsonParser(source)
11576e6818aSopenharmony_ci        kits = get_kit_instances(json_config, request.config.resource_path,
11676e6818aSopenharmony_ci                                 request.config.testcases_path)
11776e6818aSopenharmony_ci        start_smart_perf(driver_obj.config, kits)
11876e6818aSopenharmony_ci        test_name = request.get_module_name()
11976e6818aSopenharmony_ci        driver_obj.result = os.path.join(request.config.report_path, "result", "%s.xml" % test_name)
12076e6818aSopenharmony_ci
12176e6818aSopenharmony_ci        # set configs keys
12276e6818aSopenharmony_ci        configs = driver_obj._set_configs(json_config, kits, request)
12376e6818aSopenharmony_ci
12476e6818aSopenharmony_ci        # handle test args
12576e6818aSopenharmony_ci        handle_test_args(config=driver_obj.config, request=request)
12676e6818aSopenharmony_ci
12776e6818aSopenharmony_ci        # get test list
12876e6818aSopenharmony_ci        test_list = driver_obj._get_test_list(json_config, request, source)
12976e6818aSopenharmony_ci        if not test_list:
13076e6818aSopenharmony_ci            raise ParamError(ErrorMessage.TestCase.Code_0203011)
13176e6818aSopenharmony_ci        driver_obj._run(configs, test_list)
13276e6818aSopenharmony_ci    except (ReportException, ModuleNotFoundError, ExecuteTerminate,
13376e6818aSopenharmony_ci            SyntaxError, ValueError, AttributeError, TypeError,
13476e6818aSopenharmony_ci            KeyboardInterrupt, ParamError, DeviceError) \
13576e6818aSopenharmony_ci            as exception:
13676e6818aSopenharmony_ci        error_no = getattr(exception, "error_no", "00000")
13776e6818aSopenharmony_ci        LOG.exception(exception, exc_info=False, error_no=error_no)
13876e6818aSopenharmony_ci        driver_obj.error_message = exception
13976e6818aSopenharmony_ci        Binder.notify_stage(
14076e6818aSopenharmony_ci            CaseEnd(request.get_module_name(), "Failed", str(driver_obj.error_message)))
14176e6818aSopenharmony_ci    finally:
14276e6818aSopenharmony_ci        driver_obj._handle_finally(request)
14376e6818aSopenharmony_ci
14476e6818aSopenharmony_ci
14576e6818aSopenharmony_ci@Plugin(type=Plugin.DRIVER, id=HostDrivenTestType.device_test)
14676e6818aSopenharmony_ciclass DeviceTestDriver(IDriver):
14776e6818aSopenharmony_ci    """
14876e6818aSopenharmony_ci    DeviceTest is a Test that runs a host-driven test on given devices.
14976e6818aSopenharmony_ci    """
15076e6818aSopenharmony_ci    # test driver config
15176e6818aSopenharmony_ci    config = None
15276e6818aSopenharmony_ci    result = ""
15376e6818aSopenharmony_ci    error_message = ""
15476e6818aSopenharmony_ci    py_file = ""
15576e6818aSopenharmony_ci
15676e6818aSopenharmony_ci    def __init__(self):
15776e6818aSopenharmony_ci        self.linux_host = ""
15876e6818aSopenharmony_ci        self.linux_directory = ""
15976e6818aSopenharmony_ci
16076e6818aSopenharmony_ci    def __check_environment__(self, device_options):
16176e6818aSopenharmony_ci        pass
16276e6818aSopenharmony_ci
16376e6818aSopenharmony_ci    def __check_config__(self, config=None):
16476e6818aSopenharmony_ci        pass
16576e6818aSopenharmony_ci
16676e6818aSopenharmony_ci    def __init_nfs_server__(self, request=None):
16776e6818aSopenharmony_ci        pass
16876e6818aSopenharmony_ci
16976e6818aSopenharmony_ci    def __execute__(self, request):
17076e6818aSopenharmony_ci        do_driver_execute(self, request)
17176e6818aSopenharmony_ci
17276e6818aSopenharmony_ci    def _get_test_list(self, json_config, request, source):
17376e6818aSopenharmony_ci        test_list = get_config_value('py_file', json_config.get_driver(),
17476e6818aSopenharmony_ci                                     is_list=True)
17576e6818aSopenharmony_ci        if str(request.root.source.source_file).endswith(PYD_SUFFIX) or \
17676e6818aSopenharmony_ci                str(request.root.source.source_file).endswith(PY_SUFFIX):
17776e6818aSopenharmony_ci            test_list = [request.root.source.source_file]
17876e6818aSopenharmony_ci
17976e6818aSopenharmony_ci        if not test_list and os.path.exists(source):
18076e6818aSopenharmony_ci            dir_name, file_name = os.path.split(source)
18176e6818aSopenharmony_ci            file_name, _ = os.path.splitext(file_name)
18276e6818aSopenharmony_ci            test_list = _get_dict_test_list(os.path.dirname(source), file_name)
18376e6818aSopenharmony_ci
18476e6818aSopenharmony_ci        # check test list
18576e6818aSopenharmony_ci        testcase = request.get("testcase")
18676e6818aSopenharmony_ci        testcase_list = []
18776e6818aSopenharmony_ci        if testcase:
18876e6818aSopenharmony_ci            testcase_list = str(testcase).split(";")
18976e6818aSopenharmony_ci
19076e6818aSopenharmony_ci        checked_test_list = []
19176e6818aSopenharmony_ci        for _, test in enumerate(test_list):
19276e6818aSopenharmony_ci            if not os.path.exists(test):
19376e6818aSopenharmony_ci                try:
19476e6818aSopenharmony_ci                    absolute_file = get_file_absolute_path(test, [
19576e6818aSopenharmony_ci                        self.config.resource_path, self.config.testcases_path])
19676e6818aSopenharmony_ci                except ParamError as error:
19776e6818aSopenharmony_ci                    LOG.error(error, error_no=error.error_no)
19876e6818aSopenharmony_ci                    continue
19976e6818aSopenharmony_ci            else:
20076e6818aSopenharmony_ci                absolute_file = test
20176e6818aSopenharmony_ci
20276e6818aSopenharmony_ci            file_name = get_filename_extension(absolute_file)[0]
20376e6818aSopenharmony_ci            if not testcase_list or file_name in testcase_list:
20476e6818aSopenharmony_ci                checked_test_list.append(absolute_file)
20576e6818aSopenharmony_ci            else:
20676e6818aSopenharmony_ci                LOG.info("Test '%s' is ignored", absolute_file)
20776e6818aSopenharmony_ci        if checked_test_list:
20876e6818aSopenharmony_ci            LOG.info("Test list: {}".format(checked_test_list))
20976e6818aSopenharmony_ci        else:
21076e6818aSopenharmony_ci            err_msg = ErrorMessage.TestCase.Code_0203012
21176e6818aSopenharmony_ci            LOG.error(err_msg)
21276e6818aSopenharmony_ci            raise ParamError(err_msg)
21376e6818aSopenharmony_ci        if len(checked_test_list) > 1:
21476e6818aSopenharmony_ci            err_msg = ErrorMessage.TestCase.Code_0203013.format(request.get_module_name(), test_list)
21576e6818aSopenharmony_ci            LOG.error(err_msg)
21676e6818aSopenharmony_ci            raise ParamError(err_msg)
21776e6818aSopenharmony_ci        return checked_test_list
21876e6818aSopenharmony_ci
21976e6818aSopenharmony_ci    def _set_configs(self, json_config, kits, request):
22076e6818aSopenharmony_ci        configs = dict()
22176e6818aSopenharmony_ci        configs["testargs"] = self.config.testargs or {}
22276e6818aSopenharmony_ci        configs["testcases_path"] = self.config.testcases_path or ""
22376e6818aSopenharmony_ci        configs["request"] = request
22476e6818aSopenharmony_ci        configs["test_name"] = request.get_module_name()
22576e6818aSopenharmony_ci        configs["report_path"] = request.config.report_path
22676e6818aSopenharmony_ci        configs["execute"] = get_config_value(
22776e6818aSopenharmony_ci            'execute', json_config.get_driver(), False)
22876e6818aSopenharmony_ci
22976e6818aSopenharmony_ci        for device in self.config.devices:
23076e6818aSopenharmony_ci            do_module_kit_setup(request, kits)
23176e6818aSopenharmony_ci            if device.label == DeviceLabelType.ipcamera:
23276e6818aSopenharmony_ci                # add extra keys to configs for ipcamera device
23376e6818aSopenharmony_ci                self.__init_nfs_server__(request=request)
23476e6818aSopenharmony_ci                configs["linux_host"] = self.linux_host
23576e6818aSopenharmony_ci                configs["linux_directory"] = self.linux_directory
23676e6818aSopenharmony_ci                configs["kits"] = kits
23776e6818aSopenharmony_ci
23876e6818aSopenharmony_ci        return configs
23976e6818aSopenharmony_ci
24076e6818aSopenharmony_ci    def _handle_finally(self, request):
24176e6818aSopenharmony_ci        # do kit teardown
24276e6818aSopenharmony_ci        do_module_kit_teardown(request)
24376e6818aSopenharmony_ci
24476e6818aSopenharmony_ci        # close device connect
24576e6818aSopenharmony_ci        for device in self.config.devices:
24676e6818aSopenharmony_ci            if device.label == DeviceLabelType.ipcamera or device.label == \
24776e6818aSopenharmony_ci                    DeviceLabelType.watch_gt:
24876e6818aSopenharmony_ci                device.close()
24976e6818aSopenharmony_ci            if device.label == DeviceLabelType.phone:
25076e6818aSopenharmony_ci                device.close()
25176e6818aSopenharmony_ci
25276e6818aSopenharmony_ci        # check result report
25376e6818aSopenharmony_ci        report_name = request.root.source.test_name if \
25476e6818aSopenharmony_ci            not request.root.source.test_name.startswith("{") \
25576e6818aSopenharmony_ci            else "report"
25676e6818aSopenharmony_ci        module_name = request.get_module_name()
25776e6818aSopenharmony_ci        if Binder.session().mode != ModeType.decc:
25876e6818aSopenharmony_ci            self.result = check_result_report(
25976e6818aSopenharmony_ci                request.config.report_path, self.result, self.error_message,
26076e6818aSopenharmony_ci                report_name, module_name)
26176e6818aSopenharmony_ci        else:
26276e6818aSopenharmony_ci            tmp_list = copy.copy(SuiteReporter.get_report_result())
26376e6818aSopenharmony_ci            if self.result not in [report_path for report_path, _ in tmp_list]:
26476e6818aSopenharmony_ci                if not self.error_message:
26576e6818aSopenharmony_ci                    self.error_message = "Case not execute[01205]"
26676e6818aSopenharmony_ci                self.result = check_result_report(
26776e6818aSopenharmony_ci                    request.config.report_path, self.result,
26876e6818aSopenharmony_ci                    self.error_message, report_name, module_name)
26976e6818aSopenharmony_ci
27076e6818aSopenharmony_ci    def _run(self, configs, test_list):
27176e6818aSopenharmony_ci        # insert paths for loading _devicetest module and testcases
27276e6818aSopenharmony_ci        devicetest_module = os.path.join(Variables.modules_dir, "_devicetest")
27376e6818aSopenharmony_ci        if os.path.exists(devicetest_module):
27476e6818aSopenharmony_ci            sys.path.insert(1, devicetest_module)
27576e6818aSopenharmony_ci        if configs["testcases_path"]:
27676e6818aSopenharmony_ci            sys.path.insert(1, configs["testcases_path"])
27776e6818aSopenharmony_ci            sys.path.insert(1, os.path.dirname(configs["testcases_path"]))
27876e6818aSopenharmony_ci
27976e6818aSopenharmony_ci        # apply data to devicetest module about resource path
28076e6818aSopenharmony_ci        request = configs.get('request', None)
28176e6818aSopenharmony_ci        if request:
28276e6818aSopenharmony_ci            sys.ecotest_resource_path = request.config.resource_path
28376e6818aSopenharmony_ci
28476e6818aSopenharmony_ci        # run devicetest
28576e6818aSopenharmony_ci        from devicetest.main import DeviceTest
28676e6818aSopenharmony_ci        device_test = DeviceTest(test_list, configs, self.config.devices, self.result)
28776e6818aSopenharmony_ci        device_test.run()
28876e6818aSopenharmony_ci
28976e6818aSopenharmony_ci    def __result__(self):
29076e6818aSopenharmony_ci        if check_mode(ModeType.decc):
29176e6818aSopenharmony_ci            return self.result
29276e6818aSopenharmony_ci        return self.result if os.path.exists(self.result) else ""
29376e6818aSopenharmony_ci
29476e6818aSopenharmony_ci
29576e6818aSopenharmony_cidef _get_dict_testsuite(testsuite, config):
29676e6818aSopenharmony_ci    post_suffix = [PY_SUFFIX, PYD_SUFFIX, PYC_SUFFIX]
29776e6818aSopenharmony_ci    for suffix in post_suffix:
29876e6818aSopenharmony_ci        testsuite_file = "{}{}".format(testsuite, suffix)
29976e6818aSopenharmony_ci        if not os.path.exists(testsuite_file):
30076e6818aSopenharmony_ci            try:
30176e6818aSopenharmony_ci                absolute_file = get_file_absolute_path(testsuite_file, [
30276e6818aSopenharmony_ci                    config.resource_path, config.testcases_path])
30376e6818aSopenharmony_ci                return absolute_file
30476e6818aSopenharmony_ci            except ParamError as error:
30576e6818aSopenharmony_ci                LOG.error(error, error_no=error.error_no)
30676e6818aSopenharmony_ci                continue
30776e6818aSopenharmony_ci        else:
30876e6818aSopenharmony_ci            return testsuite_file
30976e6818aSopenharmony_ci    return None
31076e6818aSopenharmony_ci
31176e6818aSopenharmony_ci
31276e6818aSopenharmony_ci@Plugin(type=Plugin.DRIVER, id=HostDrivenTestType.device_testsuite)
31376e6818aSopenharmony_ciclass DeviceTestSuiteDriver(IDriver):
31476e6818aSopenharmony_ci    """
31576e6818aSopenharmony_ci    DeviceTestSuiteDriver is a Test that runs a host-driven test on given devices.
31676e6818aSopenharmony_ci    """
31776e6818aSopenharmony_ci    # test driver config
31876e6818aSopenharmony_ci    config = None
31976e6818aSopenharmony_ci    result = ""
32076e6818aSopenharmony_ci    error_message = ""
32176e6818aSopenharmony_ci    py_file = ""
32276e6818aSopenharmony_ci
32376e6818aSopenharmony_ci    def __init__(self):
32476e6818aSopenharmony_ci        pass
32576e6818aSopenharmony_ci
32676e6818aSopenharmony_ci    def __check_environment__(self, device_options):
32776e6818aSopenharmony_ci        pass
32876e6818aSopenharmony_ci
32976e6818aSopenharmony_ci    def __check_config__(self, config=None):
33076e6818aSopenharmony_ci        pass
33176e6818aSopenharmony_ci
33276e6818aSopenharmony_ci    def __init_nfs_server__(self, request=None):
33376e6818aSopenharmony_ci        pass
33476e6818aSopenharmony_ci
33576e6818aSopenharmony_ci    def __execute__(self, request):
33676e6818aSopenharmony_ci        do_driver_execute(self, request)
33776e6818aSopenharmony_ci
33876e6818aSopenharmony_ci    def _get_test_list(self, json_config, request, source):
33976e6818aSopenharmony_ci        testsuite = get_config_value('testsuite', json_config.get_driver(),
34076e6818aSopenharmony_ci                                     is_list=False)
34176e6818aSopenharmony_ci
34276e6818aSopenharmony_ci        if not testsuite and os.path.exists(source):
34376e6818aSopenharmony_ci            dir_name, file_name = os.path.split(source)
34476e6818aSopenharmony_ci            file_name, _ = os.path.splitext(file_name)
34576e6818aSopenharmony_ci            temp_testsuite = _get_dict_test_list(os.path.dirname(source), file_name)
34676e6818aSopenharmony_ci            if temp_testsuite:
34776e6818aSopenharmony_ci                testsuite = temp_testsuite[0]
34876e6818aSopenharmony_ci
34976e6818aSopenharmony_ci        if not testsuite:
35076e6818aSopenharmony_ci            err_msg = ErrorMessage.TestCase.Code_0203014
35176e6818aSopenharmony_ci            LOG.error(err_msg)
35276e6818aSopenharmony_ci            raise ParamError(err_msg)
35376e6818aSopenharmony_ci
35476e6818aSopenharmony_ci        checked_testsuite = None
35576e6818aSopenharmony_ci        if testsuite.endswith(PY_SUFFIX) or \
35676e6818aSopenharmony_ci                testsuite.endswith(PYD_SUFFIX) or \
35776e6818aSopenharmony_ci                testsuite.endswith(PYC_SUFFIX):
35876e6818aSopenharmony_ci            if not os.path.exists(testsuite):
35976e6818aSopenharmony_ci                try:
36076e6818aSopenharmony_ci                    checked_testsuite = get_file_absolute_path(testsuite, [
36176e6818aSopenharmony_ci                        self.config.resource_path, self.config.testcases_path])
36276e6818aSopenharmony_ci                except ParamError as error:
36376e6818aSopenharmony_ci                    LOG.debug(error, error_no=error.error_no)
36476e6818aSopenharmony_ci            else:
36576e6818aSopenharmony_ci                checked_testsuite = testsuite
36676e6818aSopenharmony_ci        else:
36776e6818aSopenharmony_ci            checked_testsuite = _get_dict_testsuite(testsuite, self.config)
36876e6818aSopenharmony_ci
36976e6818aSopenharmony_ci        if checked_testsuite:
37076e6818aSopenharmony_ci            LOG.info("Test suite list: {}".format(checked_testsuite))
37176e6818aSopenharmony_ci        else:
37276e6818aSopenharmony_ci            err_msg = ErrorMessage.TestCase.Code_0203012
37376e6818aSopenharmony_ci            LOG.error(err_msg)
37476e6818aSopenharmony_ci            raise ParamError(err_msg)
37576e6818aSopenharmony_ci        return checked_testsuite
37676e6818aSopenharmony_ci
37776e6818aSopenharmony_ci    def _set_configs(self, json_config, kits, request):
37876e6818aSopenharmony_ci        configs = dict()
37976e6818aSopenharmony_ci        configs["testargs"] = self.config.testargs or {}
38076e6818aSopenharmony_ci        configs["testcases_path"] = self.config.testcases_path or ""
38176e6818aSopenharmony_ci        configs["resource_path"] = self.config.resource_path or ""
38276e6818aSopenharmony_ci        configs["request"] = request
38376e6818aSopenharmony_ci        configs["device_log"] = request.config.device_log
38476e6818aSopenharmony_ci        configs["test_name"] = request.get_module_name()
38576e6818aSopenharmony_ci        configs["report_path"] = request.config.report_path
38676e6818aSopenharmony_ci        configs["suitecases"] = get_config_value(
38776e6818aSopenharmony_ci            'suitecases', json_config.get_driver(), True)
38876e6818aSopenharmony_ci        configs["listeners"] = request.listeners.copy()
38976e6818aSopenharmony_ci
39076e6818aSopenharmony_ci        do_module_kit_setup(request, kits)
39176e6818aSopenharmony_ci
39276e6818aSopenharmony_ci        return configs
39376e6818aSopenharmony_ci
39476e6818aSopenharmony_ci    def _handle_finally(self, request):
39576e6818aSopenharmony_ci        # do kit teardown
39676e6818aSopenharmony_ci        do_module_kit_teardown(request)
39776e6818aSopenharmony_ci
39876e6818aSopenharmony_ci        # close device connect
39976e6818aSopenharmony_ci        for device in self.config.devices:
40076e6818aSopenharmony_ci            if device.label == DeviceLabelType.phone:
40176e6818aSopenharmony_ci                device.close()
40276e6818aSopenharmony_ci
40376e6818aSopenharmony_ci        # check result report
40476e6818aSopenharmony_ci        report_name = request.root.source.test_name if \
40576e6818aSopenharmony_ci            not request.root.source.test_name.startswith("{") \
40676e6818aSopenharmony_ci            else "report"
40776e6818aSopenharmony_ci        module_name = request.get_module_name()
40876e6818aSopenharmony_ci        self.result = check_result_report(
40976e6818aSopenharmony_ci            request.config.report_path, self.result, self.error_message,
41076e6818aSopenharmony_ci            report_name, module_name)
41176e6818aSopenharmony_ci
41276e6818aSopenharmony_ci    def _run(self, configs, test_list):
41376e6818aSopenharmony_ci        if configs["testcases_path"]:
41476e6818aSopenharmony_ci            sys.path.insert(1, configs["testcases_path"])
41576e6818aSopenharmony_ci            sys.path.insert(1, os.path.dirname(configs["testcases_path"]))
41676e6818aSopenharmony_ci        request = configs.get('request', None)
41776e6818aSopenharmony_ci        if request:
41876e6818aSopenharmony_ci            sys.ecotest_resource_path = request.config.resource_path
41976e6818aSopenharmony_ci
42076e6818aSopenharmony_ci        # run AppTest
42176e6818aSopenharmony_ci        from devicetest.main import DeviceTestSuite
42276e6818aSopenharmony_ci        app_test = DeviceTestSuite(test_list, configs, self.config.devices)
42376e6818aSopenharmony_ci        app_test.run()
42476e6818aSopenharmony_ci
42576e6818aSopenharmony_ci    def __result__(self):
42676e6818aSopenharmony_ci        return self.result if os.path.exists(self.result) else ""
427