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 traceback
2076e6818aSopenharmony_ci
2176e6818aSopenharmony_cifrom devicetest.core.exception import TestPrepareError
2276e6818aSopenharmony_cifrom devicetest.core.result_upload import UploadResultHandler
2376e6818aSopenharmony_cifrom devicetest.error import ErrorMessage
2476e6818aSopenharmony_cifrom devicetest.log.logger import DeviceTestLog as Log
2576e6818aSopenharmony_cifrom devicetest.runner.test_runner import TestRunner
2676e6818aSopenharmony_cifrom devicetest.runner.test_runner import TestSuiteRunner
2776e6818aSopenharmony_ci
2876e6818aSopenharmony_ci
2976e6818aSopenharmony_ciclass DeviceTest:
3076e6818aSopenharmony_ci
3176e6818aSopenharmony_ci    def __init__(self, test_list, configs, devices, result_file):
3276e6818aSopenharmony_ci
3376e6818aSopenharmony_ci        self.test_list = test_list or []
3476e6818aSopenharmony_ci        self.configs = configs or {}
3576e6818aSopenharmony_ci        self.devices = devices or []
3676e6818aSopenharmony_ci        self.result_file = result_file
3776e6818aSopenharmony_ci        self.upload_result_handler = UploadResultHandler(self.result_file)
3876e6818aSopenharmony_ci
3976e6818aSopenharmony_ci    def run(self):
4076e6818aSopenharmony_ci        try:
4176e6818aSopenharmony_ci            test_runner = TestRunner()
4276e6818aSopenharmony_ci            test_runner.init_pipeline_runner(
4376e6818aSopenharmony_ci                self.test_list, self.configs, self.devices, self.upload_result_handler)
4476e6818aSopenharmony_ci            self.upload_result_handler.set_test_runner(test_runner)
4576e6818aSopenharmony_ci            test_runner.run()
4676e6818aSopenharmony_ci
4776e6818aSopenharmony_ci        except TestPrepareError as err:
4876e6818aSopenharmony_ci            Log.error(err)
4976e6818aSopenharmony_ci
5076e6818aSopenharmony_ci        except Exception as err:
5176e6818aSopenharmony_ci            Log.error(ErrorMessage.Common.Code_0201017)
5276e6818aSopenharmony_ci            Log.error(err, exc_info=True)
5376e6818aSopenharmony_ci        finally:
5476e6818aSopenharmony_ci            self.upload_result_handler.upload_suitereporter()
5576e6818aSopenharmony_ci
5676e6818aSopenharmony_ci
5776e6818aSopenharmony_ciclass DeviceTestSuite:
5876e6818aSopenharmony_ci
5976e6818aSopenharmony_ci    def __init__(self, test_list, configs, devices):
6076e6818aSopenharmony_ci        self.test_list = test_list or []
6176e6818aSopenharmony_ci        self.configs = configs or {}
6276e6818aSopenharmony_ci        self.devices = devices or []
6376e6818aSopenharmony_ci
6476e6818aSopenharmony_ci    def run(self):
6576e6818aSopenharmony_ci        try:
6676e6818aSopenharmony_ci            test_runner = TestSuiteRunner(self.test_list, self.configs, self.devices)
6776e6818aSopenharmony_ci            test_runner.run()
6876e6818aSopenharmony_ci
6976e6818aSopenharmony_ci        except Exception as err:
7076e6818aSopenharmony_ci            Log.debug(traceback.format_exc())
7176e6818aSopenharmony_ci            Log.error("Failed to instantiate the test runner.")
7276e6818aSopenharmony_ci            Log.error(err)
73