feat(接口测试): 接口调试本地执行
This commit is contained in:
parent
7f4da59afe
commit
9b2c2552ce
|
@ -1,5 +1,6 @@
|
||||||
package io.metersphere.sdk.dto.api.task;
|
package io.metersphere.sdk.dto.api.task;
|
||||||
|
|
||||||
|
import io.metersphere.sdk.constants.ApiExecuteRunMode;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
import java.io.Serial;
|
import java.io.Serial;
|
||||||
|
@ -12,6 +13,7 @@ public class ApiRunModeConfigDTO implements Serializable {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 运行模式 串行/并行
|
* 运行模式 串行/并行
|
||||||
|
* {@link ApiExecuteRunMode}
|
||||||
*/
|
*/
|
||||||
private String runMode;
|
private String runMode;
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,7 @@ import io.metersphere.api.dto.request.ApiEditPosRequest;
|
||||||
import io.metersphere.api.service.debug.ApiDebugLogService;
|
import io.metersphere.api.service.debug.ApiDebugLogService;
|
||||||
import io.metersphere.api.service.debug.ApiDebugService;
|
import io.metersphere.api.service.debug.ApiDebugService;
|
||||||
import io.metersphere.sdk.constants.PermissionConstants;
|
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.annotation.Log;
|
||||||
import io.metersphere.system.log.constants.OperationLogType;
|
import io.metersphere.system.log.constants.OperationLogType;
|
||||||
import io.metersphere.system.security.CheckOwner;
|
import io.metersphere.system.security.CheckOwner;
|
||||||
|
@ -81,7 +82,7 @@ public class ApiDebugController {
|
||||||
@PostMapping("/debug")
|
@PostMapping("/debug")
|
||||||
@Operation(summary = "运行接口调试")
|
@Operation(summary = "运行接口调试")
|
||||||
@RequiresPermissions(PermissionConstants.PROJECT_API_DEBUG_EXECUTE)
|
@RequiresPermissions(PermissionConstants.PROJECT_API_DEBUG_EXECUTE)
|
||||||
public String debug(@Validated @RequestBody ApiDebugRunRequest request) {
|
public TaskRequestDTO debug(@Validated @RequestBody ApiDebugRunRequest request) {
|
||||||
return apiDebugService.debug(request);
|
return apiDebugService.debug(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -31,4 +31,6 @@ public class ApiDebugRunRequest {
|
||||||
private Object request;
|
private Object request;
|
||||||
@Schema(description = "项目ID")
|
@Schema(description = "项目ID")
|
||||||
private String projectId;
|
private String projectId;
|
||||||
|
@Schema(description = "是否是本地执行")
|
||||||
|
private Boolean frontendDebug = false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,6 +38,10 @@ public class ApiResourceRunRequest {
|
||||||
* 执行组件
|
* 执行组件
|
||||||
*/
|
*/
|
||||||
private AbstractMsTestElement testElement;
|
private AbstractMsTestElement testElement;
|
||||||
|
/**
|
||||||
|
* 是否是本地执行
|
||||||
|
*/
|
||||||
|
private Boolean frontendDebug = false;
|
||||||
/**
|
/**
|
||||||
* 新上传的文件ID
|
* 新上传的文件ID
|
||||||
* 创建时先按ID创建目录,再把文件放入目录
|
* 创建时先按ID创建目录,再把文件放入目录
|
||||||
|
|
|
@ -111,15 +111,12 @@ public class ApiExecuteService {
|
||||||
return reportId + "_" + testId;
|
return reportId + "_" + testId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void debug(ApiResourceRunRequest request, ApiParamConfig parameterConfig) {
|
public TaskRequestDTO debug(ApiResourceRunRequest request, ApiParamConfig parameterConfig) {
|
||||||
String reportId = request.getReportId();
|
|
||||||
String testId = request.getTestId();
|
|
||||||
|
|
||||||
TaskRequestDTO taskRequest = new TaskRequestDTO();
|
TaskRequestDTO taskRequest = new TaskRequestDTO();
|
||||||
BeanUtils.copyBean(taskRequest, request);
|
BeanUtils.copyBean(taskRequest, request);
|
||||||
taskRequest.setRealTime(true);
|
taskRequest.setRealTime(true);
|
||||||
taskRequest.setSaveResult(false);
|
taskRequest.setSaveResult(false);
|
||||||
taskRequest.setResourceId(testId);
|
taskRequest.setResourceId(request.getTestId());
|
||||||
setServerInfoParam(taskRequest);
|
setServerInfoParam(taskRequest);
|
||||||
|
|
||||||
// 设置执行文件参数
|
// 设置执行文件参数
|
||||||
|
@ -134,7 +131,7 @@ public class ApiExecuteService {
|
||||||
|
|
||||||
String executeScript = parseExecuteScript(request.getTestElement(), parameterConfig);
|
String executeScript = parseExecuteScript(request.getTestElement(), parameterConfig);
|
||||||
|
|
||||||
doDebug(reportId, testId, taskRequest, executeScript, request.getProjectId());
|
return doDebug(request, taskRequest, executeScript);
|
||||||
}
|
}
|
||||||
|
|
||||||
private GlobalParams getGlobalParam(ApiResourceRunRequest request) {
|
private GlobalParams getGlobalParam(ApiResourceRunRequest request) {
|
||||||
|
@ -148,24 +145,34 @@ public class ApiExecuteService {
|
||||||
/**
|
/**
|
||||||
* 发送执行任务
|
* 发送执行任务
|
||||||
*
|
*
|
||||||
* @param reportId 报告ID
|
|
||||||
* @param testId 资源ID
|
|
||||||
* @param taskRequest 执行参数
|
* @param taskRequest 执行参数
|
||||||
* @param executeScript 执行脚本
|
* @param executeScript 执行脚本
|
||||||
* @param projectId 项目ID
|
|
||||||
*/
|
*/
|
||||||
private void doDebug(String reportId,
|
private TaskRequestDTO doDebug(ApiResourceRunRequest request,
|
||||||
String testId,
|
|
||||||
TaskRequestDTO taskRequest,
|
TaskRequestDTO taskRequest,
|
||||||
String executeScript,
|
String executeScript) {
|
||||||
String projectId) {
|
String reportId = request.getReportId();
|
||||||
|
String testId = request.getTestId();
|
||||||
|
String projectId = request.getProjectId();
|
||||||
|
|
||||||
// 设置插件文件信息
|
// 设置插件文件信息
|
||||||
taskRequest.setPluginFiles(apiPluginService.getFileInfoByProjectId(projectId));
|
taskRequest.setPluginFiles(apiPluginService.getFileInfoByProjectId(projectId));
|
||||||
ApiRunModeConfigDTO runModeConfig = new ApiRunModeConfigDTO();
|
ApiRunModeConfigDTO runModeConfig = new ApiRunModeConfigDTO();
|
||||||
runModeConfig.setRunMode(ApiExecuteRunMode.BACKEND_DEBUG.name());
|
runModeConfig.setRunMode(ApiExecuteRunMode.BACKEND_DEBUG.name());
|
||||||
|
if (request.getFrontendDebug()) {
|
||||||
|
runModeConfig.setRunMode(ApiExecuteRunMode.FRONTEND_DEBUG.name());
|
||||||
|
}
|
||||||
taskRequest.setRunModeConfig(runModeConfig);
|
taskRequest.setRunModeConfig(runModeConfig);
|
||||||
|
|
||||||
|
// 将测试脚本缓存到 redis
|
||||||
|
String scriptRedisKey = getScriptRedisKey(reportId, testId);
|
||||||
|
stringRedisTemplate.opsForValue().set(scriptRedisKey, executeScript);
|
||||||
|
|
||||||
|
if (request.getFrontendDebug()) {
|
||||||
|
// 前端调试返回执行参数,由前端调用本地资源池执行
|
||||||
|
return taskRequest;
|
||||||
|
}
|
||||||
|
|
||||||
TestResourcePoolReturnDTO testResourcePoolDTO = getGetResourcePoolNodeDTO(projectId);
|
TestResourcePoolReturnDTO testResourcePoolDTO = getGetResourcePoolNodeDTO(projectId);
|
||||||
TestResourceNodeDTO testResourceNodeDTO = getProjectExecuteNode(testResourcePoolDTO);
|
TestResourceNodeDTO testResourceNodeDTO = getProjectExecuteNode(testResourcePoolDTO);
|
||||||
if (StringUtils.isNotBlank(testResourcePoolDTO.getServerUrl())) {
|
if (StringUtils.isNotBlank(testResourcePoolDTO.getServerUrl())) {
|
||||||
|
@ -174,14 +181,11 @@ public class ApiExecuteService {
|
||||||
}
|
}
|
||||||
taskRequest.setPoolSize(testResourceNodeDTO.getConcurrentNumber());
|
taskRequest.setPoolSize(testResourceNodeDTO.getConcurrentNumber());
|
||||||
|
|
||||||
// 将测试脚本缓存到 redis
|
|
||||||
String scriptRedisKey = getScriptRedisKey(reportId, testId);
|
|
||||||
stringRedisTemplate.opsForValue().set(scriptRedisKey, executeScript);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
String endpoint = TaskRunnerClient.getEndpoint(testResourceNodeDTO.getIp(), testResourceNodeDTO.getPort());
|
String endpoint = TaskRunnerClient.getEndpoint(testResourceNodeDTO.getIp(), testResourceNodeDTO.getPort());
|
||||||
LogUtils.info(String.format("开始发送请求【 %s 】到 %s 节点执行", testId, endpoint), reportId);
|
LogUtils.info(String.format("开始发送请求【 %s 】到 %s 节点执行", testId, endpoint), reportId);
|
||||||
TaskRunnerClient.debugApi(endpoint, taskRequest);
|
TaskRunnerClient.debugApi(endpoint, taskRequest);
|
||||||
|
return taskRequest;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
LogUtils.error(e);
|
LogUtils.error(e);
|
||||||
// 调用失败清理脚本
|
// 调用失败清理脚本
|
||||||
|
@ -237,7 +241,12 @@ public class ApiExecuteService {
|
||||||
taskRequest.setResourceId(testId);
|
taskRequest.setResourceId(testId);
|
||||||
taskRequest.setResourceType(ApiExecuteResourceType.API_DEBUG.name());
|
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;
|
return reportId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,6 +22,7 @@ import io.metersphere.project.domain.FileAssociation;
|
||||||
import io.metersphere.project.domain.FileMetadata;
|
import io.metersphere.project.domain.FileMetadata;
|
||||||
import io.metersphere.project.service.ProjectService;
|
import io.metersphere.project.service.ProjectService;
|
||||||
import io.metersphere.sdk.constants.DefaultRepositoryDir;
|
import io.metersphere.sdk.constants.DefaultRepositoryDir;
|
||||||
|
import io.metersphere.sdk.dto.api.task.TaskRequestDTO;
|
||||||
import io.metersphere.sdk.exception.MSException;
|
import io.metersphere.sdk.exception.MSException;
|
||||||
import io.metersphere.sdk.util.BeanUtils;
|
import io.metersphere.sdk.util.BeanUtils;
|
||||||
import io.metersphere.sdk.util.FileAssociationSourceUtil;
|
import io.metersphere.sdk.util.FileAssociationSourceUtil;
|
||||||
|
@ -204,7 +205,7 @@ public class ApiDebugService {
|
||||||
return apiFileResourceService.uploadTempFile(file);
|
return apiFileResourceService.uploadTempFile(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String debug(ApiDebugRunRequest request) {
|
public TaskRequestDTO debug(ApiDebugRunRequest request) {
|
||||||
String id = request.getId();
|
String id = request.getId();
|
||||||
String reportId = request.getReportId();
|
String reportId = request.getReportId();
|
||||||
|
|
||||||
|
@ -222,9 +223,7 @@ public class ApiDebugService {
|
||||||
|
|
||||||
// 设置使用脚本前后置的公共脚本信息
|
// 设置使用脚本前后置的公共脚本信息
|
||||||
apiCommonService.setEnableCommonScriptProcessorInfo(runRequest.getTestElement());
|
apiCommonService.setEnableCommonScriptProcessorInfo(runRequest.getTestElement());
|
||||||
|
return apiExecuteService.debug(runRequest, paramConfig);
|
||||||
apiExecuteService.debug(runRequest, paramConfig);
|
|
||||||
return runRequest.getReportId();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void checkModuleExist(String moduleId) {
|
public void checkModuleExist(String moduleId) {
|
||||||
|
|
|
@ -43,6 +43,7 @@ import io.metersphere.project.service.CustomFunctionService;
|
||||||
import io.metersphere.project.service.FileAssociationService;
|
import io.metersphere.project.service.FileAssociationService;
|
||||||
import io.metersphere.sdk.constants.DefaultRepositoryDir;
|
import io.metersphere.sdk.constants.DefaultRepositoryDir;
|
||||||
import io.metersphere.sdk.constants.PermissionConstants;
|
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.FileCenter;
|
||||||
import io.metersphere.sdk.file.FileRequest;
|
import io.metersphere.sdk.file.FileRequest;
|
||||||
import io.metersphere.sdk.util.BeanUtils;
|
import io.metersphere.sdk.util.BeanUtils;
|
||||||
|
@ -511,6 +512,12 @@ public class ApiDebugControllerTests extends BaseTest {
|
||||||
request.setRequest(getMsElementParam(msHTTPElement));
|
request.setRequest(getMsElementParam(msHTTPElement));
|
||||||
this.requestPostWithOk(DEBUG, request);
|
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();
|
MockMultipartFile file = getMockMultipartFile();
|
||||||
String fileId = doUploadTempFile(file);
|
String fileId = doUploadTempFile(file);
|
||||||
|
|
Loading…
Reference in New Issue