From 9b2c2552ce86ba020d32cccd2c4b8b6b957e6d0a Mon Sep 17 00:00:00 2001 From: AgAngle <1323481023@qq.com> Date: Mon, 26 Feb 2024 21:35:18 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E8=B0=83=E8=AF=95=E6=9C=AC=E5=9C=B0=E6=89=A7?= =?UTF-8?q?=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sdk/dto/api/task/ApiRunModeConfigDTO.java | 2 + .../controller/debug/ApiDebugController.java | 3 +- .../api/dto/debug/ApiDebugRunRequest.java | 2 + .../api/dto/debug/ApiResourceRunRequest.java | 4 ++ .../api/service/ApiExecuteService.java | 47 +++++++++++-------- .../api/service/debug/ApiDebugService.java | 7 ++- .../controller/ApiDebugControllerTests.java | 7 +++ 7 files changed, 48 insertions(+), 24 deletions(-) diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/task/ApiRunModeConfigDTO.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/task/ApiRunModeConfigDTO.java index 830039294c..5dfcf4ea8a 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/task/ApiRunModeConfigDTO.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/task/ApiRunModeConfigDTO.java @@ -1,5 +1,6 @@ package io.metersphere.sdk.dto.api.task; +import io.metersphere.sdk.constants.ApiExecuteRunMode; import lombok.Data; import java.io.Serial; @@ -12,6 +13,7 @@ public class ApiRunModeConfigDTO implements Serializable { /** * 运行模式 串行/并行 + * {@link ApiExecuteRunMode} */ private String runMode; diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/controller/debug/ApiDebugController.java b/backend/services/api-test/src/main/java/io/metersphere/api/controller/debug/ApiDebugController.java index ac0b11ba3f..5c3ffb65b1 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/controller/debug/ApiDebugController.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/controller/debug/ApiDebugController.java @@ -6,6 +6,7 @@ import io.metersphere.api.dto.request.ApiEditPosRequest; import io.metersphere.api.service.debug.ApiDebugLogService; import io.metersphere.api.service.debug.ApiDebugService; import io.metersphere.sdk.constants.PermissionConstants; +import io.metersphere.sdk.dto.api.task.TaskRequestDTO; import io.metersphere.system.log.annotation.Log; import io.metersphere.system.log.constants.OperationLogType; import io.metersphere.system.security.CheckOwner; @@ -81,7 +82,7 @@ public class ApiDebugController { @PostMapping("/debug") @Operation(summary = "运行接口调试") @RequiresPermissions(PermissionConstants.PROJECT_API_DEBUG_EXECUTE) - public String debug(@Validated @RequestBody ApiDebugRunRequest request) { + public TaskRequestDTO debug(@Validated @RequestBody ApiDebugRunRequest request) { return apiDebugService.debug(request); } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/debug/ApiDebugRunRequest.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/debug/ApiDebugRunRequest.java index 55614ad18c..f1c13b7a6c 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/dto/debug/ApiDebugRunRequest.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/debug/ApiDebugRunRequest.java @@ -31,4 +31,6 @@ public class ApiDebugRunRequest { private Object request; @Schema(description = "项目ID") private String projectId; + @Schema(description = "是否是本地执行") + private Boolean frontendDebug = false; } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/debug/ApiResourceRunRequest.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/debug/ApiResourceRunRequest.java index 04178304e6..135a065124 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/dto/debug/ApiResourceRunRequest.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/debug/ApiResourceRunRequest.java @@ -38,6 +38,10 @@ public class ApiResourceRunRequest { * 执行组件 */ private AbstractMsTestElement testElement; + /** + * 是否是本地执行 + */ + private Boolean frontendDebug = false; /** * 新上传的文件ID * 创建时先按ID创建目录,再把文件放入目录 diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiExecuteService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiExecuteService.java index f6d7338186..ef9b2c2a5b 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiExecuteService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiExecuteService.java @@ -111,15 +111,12 @@ public class ApiExecuteService { return reportId + "_" + testId; } - public void debug(ApiResourceRunRequest request, ApiParamConfig parameterConfig) { - String reportId = request.getReportId(); - String testId = request.getTestId(); - + public TaskRequestDTO debug(ApiResourceRunRequest request, ApiParamConfig parameterConfig) { TaskRequestDTO taskRequest = new TaskRequestDTO(); BeanUtils.copyBean(taskRequest, request); taskRequest.setRealTime(true); taskRequest.setSaveResult(false); - taskRequest.setResourceId(testId); + taskRequest.setResourceId(request.getTestId()); setServerInfoParam(taskRequest); // 设置执行文件参数 @@ -134,7 +131,7 @@ public class ApiExecuteService { String executeScript = parseExecuteScript(request.getTestElement(), parameterConfig); - doDebug(reportId, testId, taskRequest, executeScript, request.getProjectId()); + return doDebug(request, taskRequest, executeScript); } private GlobalParams getGlobalParam(ApiResourceRunRequest request) { @@ -148,24 +145,34 @@ public class ApiExecuteService { /** * 发送执行任务 * - * @param reportId 报告ID - * @param testId 资源ID * @param taskRequest 执行参数 * @param executeScript 执行脚本 - * @param projectId 项目ID */ - private void doDebug(String reportId, - String testId, - TaskRequestDTO taskRequest, - String executeScript, - String projectId) { + private TaskRequestDTO doDebug(ApiResourceRunRequest request, + TaskRequestDTO taskRequest, + String executeScript) { + String reportId = request.getReportId(); + String testId = request.getTestId(); + String projectId = request.getProjectId(); // 设置插件文件信息 taskRequest.setPluginFiles(apiPluginService.getFileInfoByProjectId(projectId)); ApiRunModeConfigDTO runModeConfig = new ApiRunModeConfigDTO(); runModeConfig.setRunMode(ApiExecuteRunMode.BACKEND_DEBUG.name()); + if (request.getFrontendDebug()) { + runModeConfig.setRunMode(ApiExecuteRunMode.FRONTEND_DEBUG.name()); + } taskRequest.setRunModeConfig(runModeConfig); + // 将测试脚本缓存到 redis + String scriptRedisKey = getScriptRedisKey(reportId, testId); + stringRedisTemplate.opsForValue().set(scriptRedisKey, executeScript); + + if (request.getFrontendDebug()) { + // 前端调试返回执行参数,由前端调用本地资源池执行 + return taskRequest; + } + TestResourcePoolReturnDTO testResourcePoolDTO = getGetResourcePoolNodeDTO(projectId); TestResourceNodeDTO testResourceNodeDTO = getProjectExecuteNode(testResourcePoolDTO); if (StringUtils.isNotBlank(testResourcePoolDTO.getServerUrl())) { @@ -174,14 +181,11 @@ public class ApiExecuteService { } taskRequest.setPoolSize(testResourceNodeDTO.getConcurrentNumber()); - // 将测试脚本缓存到 redis - String scriptRedisKey = getScriptRedisKey(reportId, testId); - stringRedisTemplate.opsForValue().set(scriptRedisKey, executeScript); - try { String endpoint = TaskRunnerClient.getEndpoint(testResourceNodeDTO.getIp(), testResourceNodeDTO.getPort()); LogUtils.info(String.format("开始发送请求【 %s 】到 %s 节点执行", testId, endpoint), reportId); TaskRunnerClient.debugApi(endpoint, taskRequest); + return taskRequest; } catch (Exception e) { LogUtils.error(e); // 调用失败清理脚本 @@ -237,7 +241,12 @@ public class ApiExecuteService { taskRequest.setResourceId(testId); taskRequest.setResourceType(ApiExecuteResourceType.API_DEBUG.name()); - doDebug(reportId, testId, taskRequest, executeScript, runRequest.getProjectId()); + ApiResourceRunRequest apiRunRequest = new ApiResourceRunRequest(); + apiRunRequest.setTestId(testId); + apiRunRequest.setReportId(reportId); + apiRunRequest.setProjectId(runRequest.getProjectId()); + apiRunRequest.setFrontendDebug(false); + doDebug(apiRunRequest, taskRequest, executeScript); return reportId; } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/debug/ApiDebugService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/debug/ApiDebugService.java index 44150758b9..f963742b1c 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/debug/ApiDebugService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/debug/ApiDebugService.java @@ -22,6 +22,7 @@ import io.metersphere.project.domain.FileAssociation; import io.metersphere.project.domain.FileMetadata; import io.metersphere.project.service.ProjectService; import io.metersphere.sdk.constants.DefaultRepositoryDir; +import io.metersphere.sdk.dto.api.task.TaskRequestDTO; import io.metersphere.sdk.exception.MSException; import io.metersphere.sdk.util.BeanUtils; import io.metersphere.sdk.util.FileAssociationSourceUtil; @@ -204,7 +205,7 @@ public class ApiDebugService { return apiFileResourceService.uploadTempFile(file); } - public String debug(ApiDebugRunRequest request) { + public TaskRequestDTO debug(ApiDebugRunRequest request) { String id = request.getId(); String reportId = request.getReportId(); @@ -222,9 +223,7 @@ public class ApiDebugService { // 设置使用脚本前后置的公共脚本信息 apiCommonService.setEnableCommonScriptProcessorInfo(runRequest.getTestElement()); - - apiExecuteService.debug(runRequest, paramConfig); - return runRequest.getReportId(); + return apiExecuteService.debug(runRequest, paramConfig); } public void checkModuleExist(String moduleId) { diff --git a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDebugControllerTests.java b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDebugControllerTests.java index 6cf8d67aaa..3947bc3510 100644 --- a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDebugControllerTests.java +++ b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDebugControllerTests.java @@ -43,6 +43,7 @@ import io.metersphere.project.service.CustomFunctionService; import io.metersphere.project.service.FileAssociationService; import io.metersphere.sdk.constants.DefaultRepositoryDir; import io.metersphere.sdk.constants.PermissionConstants; +import io.metersphere.sdk.dto.api.task.TaskRequestDTO; import io.metersphere.sdk.file.FileCenter; import io.metersphere.sdk.file.FileRequest; import io.metersphere.sdk.util.BeanUtils; @@ -511,6 +512,12 @@ public class ApiDebugControllerTests extends BaseTest { request.setRequest(getMsElementParam(msHTTPElement)); this.requestPostWithOk(DEBUG, request); + // 测试本地调试 + request.setFrontendDebug(true); + MvcResult mvcResult = this.requestPostWithOkAndReturn(DEBUG, request); + TaskRequestDTO taskRequestDTO = getResultData(mvcResult, TaskRequestDTO.class); + Assertions.assertEquals(taskRequestDTO.getReportId(), request.getReportId()); + // 测试请求体 MockMultipartFile file = getMockMultipartFile(); String fileId = doUploadTempFile(file);