13f085823Sopenharmony_ci#!/usr/bin/env python3
23f085823Sopenharmony_ci# coding=utf-8
33f085823Sopenharmony_ci
43f085823Sopenharmony_ci#
53f085823Sopenharmony_ci# Copyright (c) 2021 Huawei Device Co., Ltd.
63f085823Sopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
73f085823Sopenharmony_ci# you may not use this file except in compliance with the License.
83f085823Sopenharmony_ci# You may obtain a copy of the License at
93f085823Sopenharmony_ci#
103f085823Sopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
113f085823Sopenharmony_ci#
123f085823Sopenharmony_ci# Unless required by applicable law or agreed to in writing, software
133f085823Sopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
143f085823Sopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
153f085823Sopenharmony_ci# See the License for the specific language governing permissions and
163f085823Sopenharmony_ci# limitations under the License.
173f085823Sopenharmony_ci#
183f085823Sopenharmony_ci
193f085823Sopenharmony_ciimport os
203f085823Sopenharmony_ciimport platform
213f085823Sopenharmony_ciimport subprocess
223f085823Sopenharmony_ciimport tempfile
233f085823Sopenharmony_cifrom abc import ABCMeta
243f085823Sopenharmony_cifrom abc import abstractmethod
253f085823Sopenharmony_ciimport stat
263f085823Sopenharmony_ci
273f085823Sopenharmony_cifrom core.config.resource_manager import ResourceManager
283f085823Sopenharmony_ci
293f085823Sopenharmony_ci
303f085823Sopenharmony_ci##############################################################################
313f085823Sopenharmony_ci##############################################################################
323f085823Sopenharmony_ci
333f085823Sopenharmony_ciFLAGS = os.O_WRONLY | os.O_APPEND | os.O_CREAT
343f085823Sopenharmony_ciMODES = stat.S_IWUSR | stat.S_IRUSR
353f085823Sopenharmony_ci
363f085823Sopenharmony_ciDEVICE_TEST_PATH = "/%s/%s/" % ("data", "test")
373f085823Sopenharmony_ci
383f085823Sopenharmony_ci
393f085823Sopenharmony_cidef get_level_para_string(level_string):
403f085823Sopenharmony_ci    level_list = list(set(level_string.split(",")))
413f085823Sopenharmony_ci    level_para_string = ""
423f085823Sopenharmony_ci    for item in level_list:
433f085823Sopenharmony_ci        if not item.isdigit():
443f085823Sopenharmony_ci            continue
453f085823Sopenharmony_ci        item = item.strip(" ")
463f085823Sopenharmony_ci        level_para_string = (f"{level_para_string}Level{item},")
473f085823Sopenharmony_ci    level_para_string = level_para_string.strip(",")
483f085823Sopenharmony_ci    return level_para_string
493f085823Sopenharmony_ci
503f085823Sopenharmony_ci
513f085823Sopenharmony_cidef make_long_command_file(command, longcommand_path, filename):
523f085823Sopenharmony_ci    sh_file_name = '%s.sh' % filename
533f085823Sopenharmony_ci    file_path = os.path.join(longcommand_path, sh_file_name)
543f085823Sopenharmony_ci    try:
553f085823Sopenharmony_ci        with os.fdopen(os.open(file_path, FLAGS, MODES), 'a') as file_desc:
563f085823Sopenharmony_ci            file_desc.write(command)
573f085823Sopenharmony_ci    except(IOError, ValueError) as err_msg:
583f085823Sopenharmony_ci        print(f"Error for make long command file: {file_path}")
593f085823Sopenharmony_ci    return sh_file_name, file_path
603f085823Sopenharmony_ci
613f085823Sopenharmony_ci
623f085823Sopenharmony_cidef is_exist_target_in_device(device, path, target):
633f085823Sopenharmony_ci    command = "ls -l %s | grep %s" % (path, target)
643f085823Sopenharmony_ci    check_result = False
653f085823Sopenharmony_ci    stdout_info = device.shell_with_output(command)
663f085823Sopenharmony_ci    if stdout_info != "" and stdout_info.find(target) != -1:
673f085823Sopenharmony_ci        check_result = True
683f085823Sopenharmony_ci    return check_result
693f085823Sopenharmony_ci
703f085823Sopenharmony_ci
713f085823Sopenharmony_cidef receive_coverage_data(device, result_path, suite_file, file_name):
723f085823Sopenharmony_ci    file_dir = suite_file.split("distributedtest")[1].strip(os.sep).split(os.sep)[0]
733f085823Sopenharmony_ci    target_name = "obj"
743f085823Sopenharmony_ci    cxx_cov_path = os.path.abspath(os.path.join(
753f085823Sopenharmony_ci        result_path,
763f085823Sopenharmony_ci        "..",
773f085823Sopenharmony_ci        "coverage",
783f085823Sopenharmony_ci        "data",
793f085823Sopenharmony_ci        "cxx",
803f085823Sopenharmony_ci        file_name + '_' + file_dir + '_' + device.device_sn))
813f085823Sopenharmony_ci
823f085823Sopenharmony_ci    if is_exist_target_in_device(device, DEVICE_TEST_PATH, "obj"):
833f085823Sopenharmony_ci        if not os.path.exists(cxx_cov_path):
843f085823Sopenharmony_ci            os.makedirs(cxx_cov_path)
853f085823Sopenharmony_ci        device.shell(
863f085823Sopenharmony_ci            "cd %s; tar -czf %s.tar.gz %s" % (DEVICE_TEST_PATH, target_name, target_name))
873f085823Sopenharmony_ci        src_file_tar = os.path.join(DEVICE_TEST_PATH, "%s.tar.gz" % target_name)
883f085823Sopenharmony_ci        device.pull_file(src_file_tar, cxx_cov_path)
893f085823Sopenharmony_ci        tar_path = os.path.join(cxx_cov_path, "%s.tar.gz" % target_name)
903f085823Sopenharmony_ci        if platform.system() == "Windows":
913f085823Sopenharmony_ci            process = subprocess.Popen("tar -zxf %s -C %s" % (tar_path, cxx_cov_path), shell=True)
923f085823Sopenharmony_ci            process.communicate()
933f085823Sopenharmony_ci            os.remove(tar_path)
943f085823Sopenharmony_ci        else:
953f085823Sopenharmony_ci            subprocess.Popen("tar -zxf %s -C %s > /dev/null 2>&1" %
963f085823Sopenharmony_ci                             (tar_path, cxx_cov_path), shell=True)
973f085823Sopenharmony_ci            subprocess.Popen("rm -rf %s" % tar_path, shell=True)
983f085823Sopenharmony_ci
993f085823Sopenharmony_ci
1003f085823Sopenharmony_ci##############################################################################
1013f085823Sopenharmony_ci##############################################################################
1023f085823Sopenharmony_ci
1033f085823Sopenharmony_ci
1043f085823Sopenharmony_ciclass ITestDriver:
1053f085823Sopenharmony_ci    __metaclass__ = ABCMeta
1063f085823Sopenharmony_ci
1073f085823Sopenharmony_ci    @abstractmethod
1083f085823Sopenharmony_ci    def execute(self, suite_file, result_path, options, push_flag=False):
1093f085823Sopenharmony_ci        pass
1103f085823Sopenharmony_ci
1113f085823Sopenharmony_ci
1123f085823Sopenharmony_ci##############################################################################
1133f085823Sopenharmony_ci##############################################################################
1143f085823Sopenharmony_ci
1153f085823Sopenharmony_ci
1163f085823Sopenharmony_ciclass CppTestDriver(ITestDriver):
1173f085823Sopenharmony_ci    def __init__(self, device, hdc_tools):
1183f085823Sopenharmony_ci        self.device = device
1193f085823Sopenharmony_ci        self.hdc_tools = hdc_tools
1203f085823Sopenharmony_ci
1213f085823Sopenharmony_ci    def execute(self, suite_file, result_path, options, background=False):
1223f085823Sopenharmony_ci        case_dir, file_name = os.path.split(suite_file)
1233f085823Sopenharmony_ci        dst_dir = case_dir.split("distributedtest")[1].strip(os.sep)
1243f085823Sopenharmony_ci        os.makedirs(os.path.join(result_path, "temp", dst_dir), exist_ok=True)
1253f085823Sopenharmony_ci
1263f085823Sopenharmony_ci        long_command_path = tempfile.mkdtemp(
1273f085823Sopenharmony_ci            prefix="long_command_",
1283f085823Sopenharmony_ci            dir=os.path.join(result_path, "temp", dst_dir))
1293f085823Sopenharmony_ci        if not options.coverage:
1303f085823Sopenharmony_ci            command = "cd %s; rm -rf %s.xml; chmod +x *; ./%s" % (
1313f085823Sopenharmony_ci                DEVICE_TEST_PATH,
1323f085823Sopenharmony_ci                file_name,
1333f085823Sopenharmony_ci                file_name)
1343f085823Sopenharmony_ci        else:
1353f085823Sopenharmony_ci            coverage_outpath = options.coverage_outpath
1363f085823Sopenharmony_ci            strip_num = len(coverage_outpath.split("/")) - 1
1373f085823Sopenharmony_ci            command = "cd %s; rm -rf %s.xml; chmod +x *; GCOV_PREFIX=. " \
1383f085823Sopenharmony_ci                      "GCOV_PREFIX_STRIP=%s ./%s" % \
1393f085823Sopenharmony_ci                      (DEVICE_TEST_PATH,
1403f085823Sopenharmony_ci                       file_name,
1413f085823Sopenharmony_ci                       str(strip_num),
1423f085823Sopenharmony_ci                       file_name,
1433f085823Sopenharmony_ci                       )
1443f085823Sopenharmony_ci
1453f085823Sopenharmony_ci        print("command: %s" % command)
1463f085823Sopenharmony_ci        sh_file_name, file_path = make_long_command_file(command,
1473f085823Sopenharmony_ci                                                         long_command_path,
1483f085823Sopenharmony_ci                                                         file_name)
1493f085823Sopenharmony_ci        self.device.push_file(file_path, DEVICE_TEST_PATH)
1503f085823Sopenharmony_ci
1513f085823Sopenharmony_ci        # push resource files
1523f085823Sopenharmony_ci        resource_manager = ResourceManager()
1533f085823Sopenharmony_ci        resource_data_dic, resource_dir = \
1543f085823Sopenharmony_ci            resource_manager.get_resource_data_dic(suite_file)
1553f085823Sopenharmony_ci        resource_manager.process_preparer_data(resource_data_dic, resource_dir,
1563f085823Sopenharmony_ci                                               self.device)
1573f085823Sopenharmony_ci        if background:
1583f085823Sopenharmony_ci            sh_command = "nohup sh %s >%s 2>&1 &" % (
1593f085823Sopenharmony_ci                os.path.join(DEVICE_TEST_PATH, sh_file_name),
1603f085823Sopenharmony_ci                os.path.join(DEVICE_TEST_PATH, "agent.log"))
1613f085823Sopenharmony_ci        else:
1623f085823Sopenharmony_ci            sh_command = "sh %s" % (
1633f085823Sopenharmony_ci                os.path.join(DEVICE_TEST_PATH, sh_file_name))
1643f085823Sopenharmony_ci        self.device.shell(sh_command)
1653f085823Sopenharmony_ci
1663f085823Sopenharmony_ci        if options.coverage:
1673f085823Sopenharmony_ci            receive_coverage_data(self.device, result_path, suite_file, file_name)
1683f085823Sopenharmony_ci
1693f085823Sopenharmony_ci        if os.path.exists(long_command_path):
1703f085823Sopenharmony_ci            os.remove(long_command_path)
1713f085823Sopenharmony_ci
1723f085823Sopenharmony_ci
1733f085823Sopenharmony_ci##############################################################################
1743f085823Sopenharmony_ci##############################################################################
1753f085823Sopenharmony_ci
176