From b467d7dd5418a65100ba7b300d6244067b575793 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=AD=A6=E5=B3=B0?= Date: Mon, 17 Oct 2022 09:57:53 +0800 Subject: [PATCH 1/2] fix: parse result from tone by http --- app/__init__.py | 2 - app/sftp_client.py | 71 ------------------------------------ requirements.txt | 2 +- services/tone_job_service.py | 19 +++++----- 4 files changed, 10 insertions(+), 84 deletions(-) delete mode 100644 app/sftp_client.py diff --git a/app/__init__.py b/app/__init__.py index 8c1dde4..dbc9c41 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -5,7 +5,6 @@ from app.db import db from app.log import log from app.oss import alg_oss from app.redis import redis -from app.sftp_client import sftp_client from app.setting import STATIC_ROOT, STATICFILES_DIRS, OUTLINE_PATH @@ -14,7 +13,6 @@ def init_app(app): log.init_app(app) db.init_app(app) redis.init_app(app) - sftp_client.init_app(app) create_outline_files() init_blueprints(app) diff --git a/app/sftp_client.py b/app/sftp_client.py deleted file mode 100644 index cbae591..0000000 --- a/app/sftp_client.py +++ /dev/null @@ -1,71 +0,0 @@ -import logging - -import paramiko - -logger = logging.getLogger('sftp') - - -class SFTPClient(object): - - def init_app(self, app): - self.host = app.config['STORAGE_HOST'] - self.port = app.config['STORAGE_SFTP_PORT'] - self.user = app.config['STORAGE_USER'] - self.password = app.config['STORAGE_PASSWORD'] - - def upload_file(self, local_path, server_path, timeout=10): - """ - 上传文件,注意:不支持文件夹 - :param server_path: 远程路径,比如:/home/sdn/tmp.txt - :param local_path: 本地路径,比如:D:/text.txt - :param timeout: 超时时间(默认),必须是int类型 - :return: bool - """ - try: - t = paramiko.Transport((self.host, self.port)) - t.connect(username=self.user, password=self.password) - sftp = paramiko.SFTPClient.from_transport(t) - sftp.banner_timeout = timeout - self._mkdirs(server_path, sftp) - sftp.put(local_path, server_path) - t.close() - return True - except Exception as e: - logger.error(f'sftp upload file[{local_path}] failed! error:{e}') - return False - - def down_file(self, server_path, local_path, timeout=10): - """ - 下载文件,注意:不支持文件夹 - :param server_path: 远程路径,比如:/home/sdn/tmp.txt - :param local_path: 本地路径,比如:D:/text.txt - :param timeout: 超时时间(默认),必须是int类型 - :return: bool - """ - try: - t = paramiko.Transport((self.host, 22)) - t.banner_timeout = timeout - t.connect(username=self.user, password=self.password) - sftp = paramiko.SFTPClient.from_transport(t) - sftp.get(server_path, local_path, sftp) - t.close() - return True - except Exception as e: - logger.error(f'sftp download file[{local_path}] failed! error:{e}') - return False - - @staticmethod - def _mkdirs(server_path, sftp): - path_list = server_path.split("/")[0:-1] - for path_item in path_list: - if not path_item: - continue - try: - sftp.chdir(path_item) - except Exception as e: - logger.info(f'The directory does not exist, now create it.[{e}]') - sftp.mkdir(path_item) - sftp.chdir(path_item) - - -sftp_client = SFTPClient() diff --git a/requirements.txt b/requirements.txt index a19e341..cae743a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -11,4 +11,4 @@ openpyxl~=3.0.9 oss2~=2.15.0 PyJWT~=2.3.0 pyyaml -paramiko \ No newline at end of file +httplib2 \ No newline at end of file diff --git a/services/tone_job_service.py b/services/tone_job_service.py index 84f1928..cf6c1ef 100644 --- a/services/tone_job_service.py +++ b/services/tone_job_service.py @@ -2,6 +2,8 @@ import time import json import uuid +import requests + from app.conf import conf from app.log import log from common.enums import Tone_Job_State, Track_Result, Case_Result, Status_EN @@ -11,7 +13,6 @@ from models.plan_model import update_plan_status from services.const import ERROR_UN_EXISTED_TONE_JOB from common.tone.tone_request import get_res_from_tone from common.tone.api import TONE_JOB_QUERY, TONE_CREATE_JOB -from app.sftp_client import sftp_client async def create_job(data, task_id): @@ -51,7 +52,7 @@ async def save_job_result(data): tone_job = await ToneJob.query_obj_one(ToneJob.tone_job_id == data.get('job_id')) if not tone_job: return ERROR_UN_EXISTED_TONE_JOB, False - machine_info = await parse_result_file(data.get('machine_file'), data.get('job_id')) + machine_info = await parse_result_file(data.get('machine_file')) if machine_info: tone_job.server_info = machine_info else: @@ -80,6 +81,7 @@ async def save_func_detail(task_id, data): test_case_id = func_result.get('test_case_id') case = [task_case_result for task_case_result in run_result.get('case_infos') if int(task_case_result['tone_case_id']) == test_case_id] + statistic_json = await parse_result_file(func_result.get('statistic_file')) if len(case) == 1: case[0]['result'] = func_result.get('case_state') case[0]['statistics'] = func_result.get('result_statistics') @@ -95,7 +97,7 @@ async def save_func_detail(task_id, data): 'task_id': task_id }) continue - statistic_json = await parse_result_file(func_result.get('statistic_file'), tone_job_id) + statistic_json = await parse_result_file(func_result.get('statistic_file')) for case_result in func_result.get('case_result'): current, expect = [], [] if statistic_json: @@ -124,14 +126,11 @@ async def save_func_detail(task_id, data): await task.update() -async def parse_result_file(res_file, job_id): +async def parse_result_file(res_file): if res_file: - index = res_file.find('/' + str(job_id)) - oss_file = res_file[index + 1:] - filepath = 'common/static/' + str(uuid.uuid4()) + '.json' - sftp_client.down_file(oss_file, filepath) - with open(filepath, 'r') as f: - return json.load(f) + req = requests.get(res_file) + if req.status_code == 200: + return req.json() return None -- Gitee From 98d2acb0105d0bfab484543aac55513d8f85129e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=AD=A6=E5=B3=B0?= Date: Mon, 17 Oct 2022 10:33:18 +0800 Subject: [PATCH 2/2] fix: server ip for create tone job --- services/plan_service.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/services/plan_service.py b/services/plan_service.py index b4826b2..bef1f24 100644 --- a/services/plan_service.py +++ b/services/plan_service.py @@ -216,6 +216,8 @@ async def __start_auto_task(auto_tasks): if suite_name not in suite_dict: suite_dict[suite_name] = list() case_obj = {'test_case': case_name} + if task.device_ip == '-': + task.device_ip = '' if task.device_ip: case_obj['server'] = {'ip': task.device_ip, 'channel_type': 'toneagent'} if task.cluster: -- Gitee