• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# coding=utf-8
3
4#
5# Copyright (c) 2021 Huawei Device Co., Ltd.
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#     http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18
19import os
20import tempfile
21from abc import ABCMeta
22from abc import abstractmethod
23
24from core.config.resource_manager import ResourceManager
25
26
27##############################################################################
28##############################################################################
29
30DEVICE_TEST_PATH = "/%s/%s/" % ("data", "test")
31
32
33def get_level_para_string(level_string):
34    level_list = list(set(level_string.split(",")))
35    level_para_string = ""
36    for item in level_list:
37        if not item.isdigit():
38            continue
39        item = item.strip(" ")
40        level_para_string += ("Level%s," % item)
41    level_para_string = level_para_string.strip(",")
42    return level_para_string
43
44
45def make_long_command_file(command, longcommand_path, filename):
46    sh_file_name = '%s.sh' % filename
47    file_path = os.path.join(longcommand_path, sh_file_name)
48    try:
49        with open(file_path, "a") as file_desc:
50            file_desc.write(command)
51    except(IOError, ValueError) as err_msg:
52        print("Error for make long command file: ", err_msg)
53    return sh_file_name, file_path
54
55
56##############################################################################
57##############################################################################
58
59
60class ITestDriver:
61    __metaclass__ = ABCMeta
62
63    @abstractmethod
64    def execute(self, suite_file, push_flag=False):
65        pass
66
67
68##############################################################################
69##############################################################################
70
71
72class CppTestDriver(ITestDriver):
73    def __init__(self, device):
74        self.device = device
75
76    def execute(self, suite_file, background=False):
77        file_name = os.path.basename(suite_file)
78
79        long_command_path = tempfile.mkdtemp(prefix="long_command_",
80            dir=os.path.join(os.environ.get('PYTEST_RESULTPATH'), "temp"))
81        command = "cd %s; rm -rf %s.xml; chmod +x *; ./%s" % (
82            DEVICE_TEST_PATH,
83            file_name,
84            file_name)
85
86        print("command: %s" % command)
87        sh_file_name, file_path = make_long_command_file(command,
88            long_command_path,
89            file_name)
90        self.device.push_file(file_path, DEVICE_TEST_PATH)
91
92        # push resource files
93        resource_manager = ResourceManager()
94        resource_data_dic, resource_dir = \
95            resource_manager.get_resource_data_dic(suite_file)
96        resource_manager.process_preparer_data(resource_data_dic, resource_dir,
97                                               self.device)
98        if background:
99            sh_command = "nohup sh %s >%s 2>&1 &" % (
100                os.path.join(DEVICE_TEST_PATH, sh_file_name),
101                os.path.join(DEVICE_TEST_PATH, "agent.log"))
102        else:
103            sh_command = "sh %s" % (
104                os.path.join(DEVICE_TEST_PATH, sh_file_name))
105
106        return self.device.shell(sh_command)
107
108
109##############################################################################
110##############################################################################
111
112