feat(接口测试): 单个执行支持k8s调用

This commit is contained in:
fit2-zhao 2024-10-11 10:03:56 +08:00 committed by Craftsman
parent d3fb9bf135
commit 97c6ddea80
4 changed files with 51 additions and 12 deletions

View File

@ -3,6 +3,7 @@ package io.metersphere.api.engine;
import io.metersphere.engine.ApiEngine;
import io.metersphere.sdk.dto.api.task.TaskBatchRequestDTO;
import io.metersphere.sdk.dto.api.task.TaskRequestDTO;
import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.util.LogUtils;
import io.metersphere.system.dto.pool.TestResourceDTO;
@ -43,6 +44,7 @@ public class KubernetesExecEngine implements ApiEngine {
} catch (Exception e) {
LogUtils.error("K8S 执行异常:", e);
rollbackOnFailure(); // 错误处理逻辑
throw new MSException("K8S 节点执行错误:" + e.getMessage(), e);
}
}

View File

@ -77,6 +77,7 @@ public class KubernetesProvider {
if (runRequest != null) {
LogUtils.info("请求参数:{}", JSON.toJSONString(runRequest));
// TODO: Add proper error handling based on response or task request details
throw new MSException("K8S 节点执行错误:" + t.getMessage(), t);
} else {
throw new MSException("K8S 节点执行错误:" + t.getMessage(), t);
}

View File

@ -10,6 +10,7 @@ import io.metersphere.api.dto.request.controller.MsScriptElement;
import io.metersphere.api.parser.TestElementParser;
import io.metersphere.api.parser.TestElementParserFactory;
import io.metersphere.api.utils.ApiDataUtils;
import io.metersphere.engine.EngineFactory;
import io.metersphere.engine.MsHttpClient;
import io.metersphere.plugin.api.dto.ParameterConfig;
import io.metersphere.plugin.api.spi.AbstractMsTestElement;
@ -31,6 +32,7 @@ import io.metersphere.sdk.util.*;
import io.metersphere.system.config.MinioProperties;
import io.metersphere.system.controller.handler.ResultHolder;
import io.metersphere.system.domain.TestResourcePool;
import io.metersphere.system.dto.pool.TestResourceDTO;
import io.metersphere.system.dto.pool.TestResourceNodeDTO;
import io.metersphere.system.dto.pool.TestResourcePoolReturnDTO;
import io.metersphere.system.service.*;
@ -213,19 +215,38 @@ public class ApiExecuteService {
// 获取资源池
TestResourcePoolReturnDTO testResourcePoolDTO = getGetResourcePoolNodeDTO(taskInfo.getRunModeConfig(), taskInfo.getProjectId());
TestResourceNodeDTO testResourceNodeDTO = getNextExecuteNode(testResourcePoolDTO);
if (StringUtils.isNotBlank(testResourcePoolDTO.getServerUrl())) {
// 如果资源池配置了当前站点则使用资源池的
taskInfo.setMsUrl(testResourcePoolDTO.getServerUrl());
}
taskInfo.setPoolSize(testResourceNodeDTO.getConcurrentNumber());
String endpoint = MsHttpClient.getEndpoint(testResourceNodeDTO.getIp(), testResourceNodeDTO.getPort());
LogUtils.info("开始发送请求【 {}_{} 】到 {} 节点执行", taskItem.getReportId(), taskItem.getResourceId(), endpoint);
if (StringUtils.equalsAny(taskInfo.getRunMode(), ApiExecuteRunMode.FRONTEND_DEBUG.name(), ApiExecuteRunMode.BACKEND_DEBUG.name())) {
MsHttpClient.debugApi(endpoint, taskRequest);
// 判断是否为 K8S 资源池
boolean isK8SResourcePool = StringUtils.equals(testResourcePoolDTO.getType(), ResourcePoolTypeEnum.K8S.name());
boolean isDebugMode = StringUtils.equalsAny(taskInfo.getRunMode(), ApiExecuteRunMode.FRONTEND_DEBUG.name(), ApiExecuteRunMode.BACKEND_DEBUG.name());
if (isK8SResourcePool) {
TestResourceDTO testResourceDTO = new TestResourceDTO();
BeanUtils.copyBean(testResourceDTO, testResourcePoolDTO.getTestResourceReturnDTO());
taskInfo.setPoolSize(testResourceDTO.getConcurrentNumber());
LogUtils.info("开始发送请求【 {}_{} 】到 K8S 资源池执行", taskItem.getReportId(), taskItem.getResourceId());
if (isDebugMode) {
EngineFactory.debugApi(taskRequest, testResourceDTO);
} else {
EngineFactory.runApi(taskRequest, testResourceDTO);
}
} else {
MsHttpClient.runApi(endpoint, taskRequest);
TestResourceNodeDTO testResourceNodeDTO = getNextExecuteNode(testResourcePoolDTO);
taskInfo.setPoolSize(testResourceNodeDTO.getConcurrentNumber());
String endpoint = MsHttpClient.getEndpoint(testResourceNodeDTO.getIp(), testResourceNodeDTO.getPort());
LogUtils.info("开始发送请求【 {}_{} 】到 {} 节点执行", taskItem.getReportId(), taskItem.getResourceId(), endpoint);
if (isDebugMode) {
MsHttpClient.debugApi(endpoint, taskRequest);
} else {
MsHttpClient.runApi(endpoint, taskRequest);
}
}
} catch (HttpServerErrorException e) {

View File

@ -24,7 +24,11 @@ public class KubernetesExecTests extends BaseTest {
public void debugApi() throws Exception {
TaskRequestDTO request = new TaskRequestDTO();
TestResourceDTO resource = new TestResourceDTO();
EngineFactory.debugApi(request, resource);
try{
EngineFactory.debugApi(request, resource);
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
@Test
@ -32,7 +36,11 @@ public class KubernetesExecTests extends BaseTest {
public void runApi() throws Exception {
TaskRequestDTO request = new TaskRequestDTO();
TestResourceDTO resource = new TestResourceDTO();
EngineFactory.runApi(request, resource);
try {
EngineFactory.runApi(request, resource);
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
@ -41,7 +49,11 @@ public class KubernetesExecTests extends BaseTest {
public void batchRunApi() throws Exception {
TaskBatchRequestDTO request = new TaskBatchRequestDTO();
TestResourceDTO resource = new TestResourceDTO();
EngineFactory.batchRunApi(request, resource);
try {
EngineFactory.batchRunApi(request, resource);
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
@ -50,8 +62,11 @@ public class KubernetesExecTests extends BaseTest {
public void stop() throws Exception {
List<String> request = new ArrayList<>();
TestResourceDTO resource = new TestResourceDTO();
EngineFactory.stopApi(request, resource);
try {
EngineFactory.stopApi(request, resource);
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}