fix(接口测试): 解决指令过长问题

This commit is contained in:
fit2-zhao 2024-10-11 17:41:39 +08:00 committed by Craftsman
parent fa05eb0217
commit dd7a6143ff
2 changed files with 22 additions and 10 deletions

View File

@ -22,6 +22,7 @@ public class KubernetesExecEngine implements ApiEngine {
*/
private final Object request;
private final TestResourceDTO resource;
private final String scriptId;
/**
* 单调执行构造函数
@ -29,9 +30,10 @@ public class KubernetesExecEngine implements ApiEngine {
* @param request
* @param resource
*/
public KubernetesExecEngine(TaskRequestDTO request, TestResourceDTO resource) {
public KubernetesExecEngine(TaskRequestDTO request, TestResourceDTO resource, String scriptId) {
this.request = request;
this.resource = resource;
this.scriptId = scriptId;
}
/**
@ -40,9 +42,10 @@ public class KubernetesExecEngine implements ApiEngine {
* @param batchRequestDTO
* @param resource
*/
public KubernetesExecEngine(TaskBatchRequestDTO batchRequestDTO, TestResourceDTO resource) {
public KubernetesExecEngine(TaskBatchRequestDTO batchRequestDTO, TestResourceDTO resource, String scriptId) {
this.resource = resource;
this.request = batchRequestDTO;
this.scriptId = scriptId;
}
/**
@ -51,21 +54,22 @@ public class KubernetesExecEngine implements ApiEngine {
* @param reportIds
* @param resource
*/
public KubernetesExecEngine(List<String> reportIds, TestResourceDTO resource) {
public KubernetesExecEngine(List<String> reportIds, TestResourceDTO resource, String scriptId) {
this.resource = resource;
this.request = reportIds;
this.scriptId = scriptId;
}
@Override
public void execute(String command) {
// 初始化任务
LogUtils.info("CURL 命令:【 " + command + "");
this.runApi(command);
this.runApi(command, request);
}
private void runApi(String command) {
private void runApi(String command, Object request) {
try {
KubernetesProvider.exec(resource, command);
KubernetesProvider.exec(resource, request, scriptId, command);
} catch (HttpServerErrorException e) {
handleHttpServerError(e);
} catch (Exception e) {

View File

@ -57,11 +57,20 @@ public class KubernetesProvider {
* @param command
* @throws Exception
*/
public static void exec(TestResourceDTO resource, String command) throws Exception {
protected static void exec(TestResourceDTO resource, Object runRequest, String scriptId, String command) throws Exception {
// 防止执行非法命令
if (StringUtils.isEmpty(command) || !StringUtils.contains(command, scriptId)) {
throw new MSException("Invalid command: " + command);
}
ExecWatch execWatch = null;
try (KubernetesClient client = getKubernetesClient(resource)) {
Pod pod = getExecPod(client, resource);
LogUtils.info("当前执行 Pod" + pod.getMetadata().getName() + "");
// 创建文件
String createFile = "echo -e \"" + JSON.toJSONString(runRequest) + "\" > " + scriptId;
// 删除文件
String deleteFile = "rm -f " + scriptId;
// 同步执行命令
execWatch = client.pods().inNamespace(client.getNamespace())
@ -69,8 +78,7 @@ public class KubernetesProvider {
.redirectingInput()
.writingOutput(System.out)
.writingError(System.err)
.withTTY()
.exec(SHELL_COMMAND, "-c", command);
.withTTY().exec(SHELL_COMMAND, "-c", createFile + " && " + command + " && " + deleteFile);
// 等待命令执行完成获取结果
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
@ -107,7 +115,7 @@ public class KubernetesProvider {
* @param runRequest
* @param command
*/
public static void exec(TestResourceDTO resource, Object runRequest, String command) {
public static void execCommand(TestResourceDTO resource, Object runRequest, String command) {
try (KubernetesClient client = getKubernetesClient(resource)) {
Pod pod = getExecPod(client, resource);
LogUtils.info("当前执行 Pod" + pod.getMetadata().getName() + "");