修复认证设备信息或result_content包含符号“<”或“>”,会导致结果xml格式错误的问题

Signed-off-by: deveco_xdevice <liguangjie1@huawei.com>
This commit is contained in:
deveco_xdevice 2024-11-14 15:00:18 +08:00
parent 9d6664ecae
commit e0f46bf452
5 changed files with 22 additions and 8 deletions

View File

@ -17,7 +17,6 @@
# #
import os import os
from ast import literal_eval
from xml.dom import minidom from xml.dom import minidom
from xml.etree import ElementTree from xml.etree import ElementTree
@ -125,7 +124,7 @@ class ReportHandler:
# 用例测试结果的拓展内容 # 用例测试结果的拓展内容
result_content = result_info.get('result_content') result_content = result_info.get('result_content')
if result_content: if result_content:
testcase.set("result_content", literal_eval(str(result_content))) testcase.set("result_content", f"<![CDATA[{result_content}]]>")
testsuite = ElementTree.Element('testsuite') testsuite = ElementTree.Element('testsuite')
testsuite.set("modulename", test_name) testsuite.set("modulename", test_name)

View File

@ -16,10 +16,10 @@
# limitations under the License. # limitations under the License.
# #
import json
import os import os
import shutil import shutil
import time import time
from ast import literal_eval
from xml.etree import ElementTree from xml.etree import ElementTree
from _core.constants import CaseResult from _core.constants import CaseResult
@ -58,7 +58,7 @@ def update_report_xml(report_xml, props):
return return
for k, v in props.items(): for k, v in props.items():
if k == ReportConstant.devices: if k == ReportConstant.devices:
v = literal_eval(str(v)) v = f"<![CDATA[{json.dumps(v, separators=(',', ':'))}]]>"
root.set(k, v) root.set(k, v)
result_fd = os.open(report_xml, os.O_CREAT | os.O_WRONLY | os.O_TRUNC, FilePermission.mode_644) result_fd = os.open(report_xml, os.O_CREAT | os.O_WRONLY | os.O_TRUNC, FilePermission.mode_644)
with os.fdopen(result_fd, mode="w", encoding="utf-8") as result_file: with os.fdopen(result_fd, mode="w", encoding="utf-8") as result_file:

View File

@ -31,6 +31,7 @@ from _core.report.reporter_helper import DataHelper
from _core.report.reporter_helper import ReportConstant from _core.report.reporter_helper import ReportConstant
from _core.utils import check_mode from _core.utils import check_mode
from _core.utils import get_filename_extension from _core.utils import get_filename_extension
from _core.utils import parse_xml_cdata
LOG = platform_logger("Upload") LOG = platform_logger("Upload")
@ -111,7 +112,7 @@ class Uploader:
case_id, result, error, start, end, report_path, result_content = upload_param case_id, result, error, start, end, report_path, result_content = upload_param
case = {"caseid": case_id, "result": result, "error": error, case = {"caseid": case_id, "result": result, "error": error,
"start": start, "end": end, "report": report_path, "start": start, "end": end, "report": report_path,
"result_content": result_content} "result_content": parse_xml_cdata(result_content)}
LOG.info("Case info: %s", case) LOG.info("Case info: %s", case)
upload_suite.append(case) upload_suite.append(case)

View File

@ -25,7 +25,6 @@ import re
import shutil import shutil
import time import time
import stat import stat
from ast import literal_eval
from importlib import util from importlib import util
from xml.etree import ElementTree from xml.etree import ElementTree
@ -42,6 +41,7 @@ from _core.utils import calculate_elapsed_time
from _core.utils import calculate_percent from _core.utils import calculate_percent
from _core.utils import copy_folder from _core.utils import copy_folder
from _core.utils import get_filename_extension from _core.utils import get_filename_extension
from _core.utils import parse_xml_cdata
from _core.utils import show_current_environment from _core.utils import show_current_environment
from _core.report.encrypt import check_pub_key_exist from _core.report.encrypt import check_pub_key_exist
from _core.report.encrypt import do_rsa_encrypt from _core.report.encrypt import do_rsa_encrypt
@ -376,8 +376,9 @@ class ResultReporter(IReporter):
if devices_str == "": if devices_str == "":
return [] return []
try: try:
devices = literal_eval(devices_str) devices = json.loads(parse_xml_cdata(devices_str))
except SyntaxError: except Exception as e:
LOG.warning(f"parse devices from xml failed, {e}")
return [] return []
# 汇总测试设备信息 # 汇总测试设备信息
for device in devices: for device in devices:

View File

@ -852,3 +852,16 @@ def check_uitest_version(uitest_version_info: str, base_version: tuple) -> bool:
version = tuple(version_list[i].split(".")) version = tuple(version_list[i].split("."))
return version > base_version return version > base_version
return True return True
def parse_xml_cdata(content: str) -> str:
"""
提取CDATA标签里面的内容
:param content: 内容
:return: 返回content或者移除CDATA的内容
"""
if content and '<![CDATA[' in content:
ret = re.search(r'<!\[CDATA\[(.*)]]', content)
if ret is not None:
return ret.group(1)
return content