feat(接口测试): 接口调试本地执行

This commit is contained in:
AgAngle 2024-02-26 21:35:18 +08:00 committed by Craftsman
parent 7f4da59afe
commit 9b2c2552ce
7 changed files with 48 additions and 24 deletions

View File

@ -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;

View File

@ -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);
}

View File

@ -31,4 +31,6 @@ public class ApiDebugRunRequest {
private Object request;
@Schema(description = "项目ID")
private String projectId;
@Schema(description = "是否是本地执行")
private Boolean frontendDebug = false;
}

View File

@ -38,6 +38,10 @@ public class ApiResourceRunRequest {
* 执行组件
*/
private AbstractMsTestElement testElement;
/**
* 是否是本地执行
*/
private Boolean frontendDebug = false;
/**
* 新上传的文件ID
* 创建时先按ID创建目录再把文件放入目录

View File

@ -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;
}

View File

@ -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) {

View File

@ -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);