• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# coding=utf-8
3
4#
5# Copyright (c) 2020-2024 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 re
20from collections import namedtuple
21
22__all__ = ["Error", "ErrorCategory", "ErrorMessage"]
23
24
25class ErrorCategory:
26    Environment = "Environment"
27    Framework = "Framework"
28    Script = "Script"
29
30
31class Error(namedtuple("Error", ["code", "error", "category", "suggestions"],
32                       defaults=[ErrorCategory.Framework, ""])):
33    """
34    错误码:
35    1~2位,01-99,插件编号:01-xdevice、02-devicetest、03-ohos、...
36    3~4位,01-99,保留位,可自由分配,如将环境问题细分为hdc问题、设备问题、...
37    5~7位,000-999,错误编号
38    示例:
39    [Framework-0101123] error message [Suggestions] fix suggestions
40    解析:
41    Framework是错误类别,0101123是错误代码,error message是错误信息,Suggestions是修改建议(可选)
42    """
43
44    __slots__ = ()
45
46    def __repr__(self):
47        msg = "[{}-{}] {}".format(self.category, self.code, self.error)
48        if self.suggestions:
49            msg += " [Suggestions] {}".format(self.suggestions)
50        return msg
51
52    def format(self, *args, **kwargs):
53        # 错误出现嵌套的情况,返回原始的错误
54        for arg in args:
55            arg_str = str(arg)
56            if re.search(r'\[.*[a-zA-Z0-9]+].*(?:\[Suggestions].*)?', arg_str) is not None:
57                return arg_str
58
59        return self.__str__().format(*args, **kwargs)
60
61
62class _CommonErr:
63    """Code_0101xxx,汇总常见的、未归类的问题"""
64    Code_0101001 = Error(**{"error": "Unsupported system environment",
65                            "code": "0101001"})
66    Code_0101002 = Error(**{"error": "File path does not exist, path: {}",
67                            "category": "{}",
68                            "code": "0101002"})
69    Code_0101003 = Error(**{"error": "Test kit {} does not exist",
70                            "code": "0101003",
71                            "suggestions": "1、kit名称错误;2、未安装kit依赖的模块"})
72    Code_0101004 = Error(**{"error": "Test kit {} has no attribute of device_name",
73                            "code": "0101004"})
74    Code_0101005 = Error(**{"error": "History report path does not exist, path: {}",
75                            "code": "0101005"})
76    Code_0101006 = Error(**{"error": "The combined parameters which key '{}' has no value",
77                            "category": ErrorCategory.Environment,
78                            "code": "0101006",
79                            "suggestions": "按照格式“key1:value1;key2:value2”使用组合参数"})
80    Code_0101007 = Error(**{"error": "no retry case exists",
81                            "code": "0101007"})
82    Code_0101008 = Error(**{"error": "session '{}' is invalid",
83                            "category": ErrorCategory.Environment,
84                            "code": "0101008"})
85    Code_0101009 = Error(**{"error": "no previous executed command",
86                            "category": ErrorCategory.Environment,
87                            "code": "0101009"})
88    Code_0101010 = Error(**{"error": "session '{}' has no executed command",
89                            "category": ErrorCategory.Environment,
90                            "code": "0101010"})
91    Code_0101011 = Error(**{"error": "wrong input task id '{}'",
92                            "category": ErrorCategory.Environment,
93                            "code": "0101011"})
94    Code_0101012 = Error(**{"error": "Unsupported command action '{}'",
95                            "category": ErrorCategory.Environment,
96                            "code": "0101012",
97                            "suggestions": "此方法用作处理run指令"})
98    Code_0101013 = Error(**{"error": "Unsupported execution type '{}'",
99                            "category": ErrorCategory.Environment,
100                            "code": "0101013",
101                            "suggestions": "当前支持设备测试device_test和主机测试host_test"})
102    Code_0101014 = Error(**{"error": "Test source '{}' or its json does not exist",
103                            "category": ErrorCategory.Environment,
104                            "code": "0101014",
105                            "suggestions": "1、确认是否存在对应的测试文件或用例json;2、检查user_config.xml的testcase路径配置;"
106                                           "3、确保xdevice框架程序工作目录为脚本工程目录"})
107    Code_0101015 = Error(**{"error": "Task file does not exist, file name: {}",
108                            "category": ErrorCategory.Environment,
109                            "code": "0101015",
110                            "suggestions": "需将任务json放在config目录下。如run acts全量运行acts测试用例,"
111                                           "需将acts.json放在config目录下"})
112    Code_0101016 = Error(**{"error": "No test driver to execute",
113                            "category": ErrorCategory.Script,
114                            "code": "0101016",
115                            "suggestions": "用例json需要配置测试驱动"})
116    Code_0101017 = Error(**{"error": "Test source '{}' has no test driver specified",
117                            "category": ErrorCategory.Environment,
118                            "code": "0101017",
119                            "suggestions": "用例json需要配置测试驱动"})
120    Code_0101018 = Error(**{"error": "Test source '{}' can't find the specified test driver '{}'",
121                            "category": ErrorCategory.Environment,
122                            "code": "0101018",
123                            "suggestions": "1、用例json驱动名称填写错误;2、驱动插件未安装"})
124    Code_0101019 = Error(**{"error": "Test source '{}' can't find the suitable test driver '{}'",
125                            "category": ErrorCategory.Environment,
126                            "code": "0101019",
127                            "suggestions": "1、用例json驱动名称填写错误;2、驱动插件未安装"})
128    Code_0101020 = Error(**{"error": "report path must be an empty folder",
129                            "category": ErrorCategory.Environment,
130                            "code": "0101020",
131                            "suggestions": "测试报告路径需为一个空文件夹"})
132    Code_0101021 = Error(**{"error": "Test source required {} devices, actually {} devices were found",
133                            "category": ErrorCategory.Environment,
134                            "code": "0101021",
135                            "suggestions": "测试用例的设备条件不满足"})
136    Code_0101022 = Error(**{"error": "no test file, list, dict, case or task were found",
137                            "category": ErrorCategory.Environment,
138                            "code": "0101022",
139                            "suggestions": "未发现用例"})
140    Code_0101023 = Error(**{"error": "test source is none",
141                            "category": ErrorCategory.Script,
142                            "code": "0101023"})
143    Code_0101024 = Error(**{"error": "Test file '{}' does not exist",
144                            "category": ErrorCategory.Environment,
145                            "code": "0101024"})
146    Code_0101025 = Error(**{"error": "RSA encryption error occurred, {}",
147                            "code": "0101025"})
148    Code_0101026 = Error(**{"error": "RSA decryption error occurred, {}",
149                            "code": "0101026"})
150    Code_0101027 = Error(**{"error": "Json file does not exist, file: {}",
151                            "code": "0101027"})
152    Code_0101028 = Error(**{"error": "Json file load error, file: {}, error: {}",
153                            "category": ErrorCategory.Script,
154                            "code": "0101028"})
155    Code_0101029 = Error(**{"error": "'{}' under '{}' should be dict",
156                            "category": ErrorCategory.Script,
157                            "code": "0101029"})
158    Code_0101030 = Error(**{"error": "'type' key does not exist in '{}' under '{}'",
159                            "category": ErrorCategory.Script,
160                            "code": "0101030"})
161    Code_0101031 = Error(**{"error": "The parameter {} {} is error",
162                            "category": ErrorCategory.Environment,
163                            "code": "0101031"})
164
165
166class _InterfaceImplementErr:
167    """Code_0102xxx,汇总接口实现的问题"""
168    Code_0102001 = Error(**{"error": "@Plugin must be specify type and id attributes. such as @Plugin('plugin_type') "
169                                     "or @Plugin(type='plugin_type', id='plugin_id')",
170                            "code": "0102001"})
171    Code_0102002 = Error(**{"error": "'{}' attribute is not allowed for plugin {}",
172                            "code": "0102002"})
173    Code_0102003 = Error(**{"error": "__init__ method must be no arguments for plugin {}",
174                            "code": "0102003"})
175    Code_0102004 = Error(**{"error": "'{}' method is not allowed for plugin {}",
176                            "code": "0102004"})
177    Code_0102005 = Error(**{"error": "{} plugin must be implement as {}",
178                            "code": "0102005"})
179    Code_0102006 = Error(**{"error": "Can not find the plugin {}",
180                            "code": "0102006"})
181    Code_0102007 = Error(**{"error": "Parser {} must be implement as IParser",
182                            "code": "0102007"})
183
184
185class _UserConfigErr:
186    """Code_0103xxx,汇总user_config.xml配置有误的问题"""
187    Code_0103001 = Error(**{"error": "user_config.xml does not exist",
188                            "category": ErrorCategory.Environment,
189                            "code": "0103001",
190                            "suggestions": "1、确保框架程序运行目录为工程根目录;2、工程根目录存在配置文件config/user_config.xml"})
191    Code_0103002 = Error(**{"error": "Parsing the user_config.xml failed, error: {}",
192                            "category": ErrorCategory.Environment,
193                            "code": "0103002",
194                            "suggestions": "检查user_config.xml配置文件的格式"})
195    Code_0103003 = Error(**{"error": "Parsing the user_config.xml from parameter(-env) failed, error: {}",
196                            "category": ErrorCategory.Environment,
197                            "code": "0103003",
198                            "suggestions": "检查-env运行参数配置内容的格式"})
199    Code_0103004 = Error(**{"error": "The alias of device {} is the same as that of device {}",
200                            "category": ErrorCategory.Environment,
201                            "code": "0103004",
202                            "suggestions": "设备别名不允许同名"})
203
204
205class ErrorMessage:
206    Common: _CommonErr = _CommonErr()
207    InterfaceImplement: _InterfaceImplementErr = _InterfaceImplementErr()
208    UserConfig: _UserConfigErr = _UserConfigErr()
209
210
211if __name__ == "__main__":
212    # 用法1,使用原始类,无需格式化报错内容
213    _err_101 = Error(**{"error": "error 101", "code": "101"})
214    print(_err_101)
215
216    # 用法2,使用构造方法,按format格式化报错内容
217    _err_102 = Error(**{"error": "{}, path: {}", "code": "102", "suggestions": "sss"})
218    print(_err_102.format("error 102", "test path"))
219    print(_err_102.code)
220
221    # 测试错误嵌套的情况
222    # 如:[Framework-102] [Framework-102] error 102, path: test path [Suggestions] sss, path: test path [Suggestions] sss
223    err_msg1 = "[Framework-102] error 102, path: test path"
224    print(_err_102.format(err_msg1, "test path1"))
225    err_msg1 = "[Framework-102] error 102, path: test path [Suggestions] sss"
226    print(_err_102.format(err_msg1, "test path2"))
227