fix(接口测试): 解决指令过长问题
This commit is contained in:
parent
fa05eb0217
commit
dd7a6143ff
|
@ -22,6 +22,7 @@ public class KubernetesExecEngine implements ApiEngine {
|
||||||
*/
|
*/
|
||||||
private final Object request;
|
private final Object request;
|
||||||
private final TestResourceDTO resource;
|
private final TestResourceDTO resource;
|
||||||
|
private final String scriptId;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 单调执行构造函数
|
* 单调执行构造函数
|
||||||
|
@ -29,9 +30,10 @@ public class KubernetesExecEngine implements ApiEngine {
|
||||||
* @param request
|
* @param request
|
||||||
* @param resource
|
* @param resource
|
||||||
*/
|
*/
|
||||||
public KubernetesExecEngine(TaskRequestDTO request, TestResourceDTO resource) {
|
public KubernetesExecEngine(TaskRequestDTO request, TestResourceDTO resource, String scriptId) {
|
||||||
this.request = request;
|
this.request = request;
|
||||||
this.resource = resource;
|
this.resource = resource;
|
||||||
|
this.scriptId = scriptId;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -40,9 +42,10 @@ public class KubernetesExecEngine implements ApiEngine {
|
||||||
* @param batchRequestDTO
|
* @param batchRequestDTO
|
||||||
* @param resource
|
* @param resource
|
||||||
*/
|
*/
|
||||||
public KubernetesExecEngine(TaskBatchRequestDTO batchRequestDTO, TestResourceDTO resource) {
|
public KubernetesExecEngine(TaskBatchRequestDTO batchRequestDTO, TestResourceDTO resource, String scriptId) {
|
||||||
this.resource = resource;
|
this.resource = resource;
|
||||||
this.request = batchRequestDTO;
|
this.request = batchRequestDTO;
|
||||||
|
this.scriptId = scriptId;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -51,21 +54,22 @@ public class KubernetesExecEngine implements ApiEngine {
|
||||||
* @param reportIds
|
* @param reportIds
|
||||||
* @param resource
|
* @param resource
|
||||||
*/
|
*/
|
||||||
public KubernetesExecEngine(List<String> reportIds, TestResourceDTO resource) {
|
public KubernetesExecEngine(List<String> reportIds, TestResourceDTO resource, String scriptId) {
|
||||||
this.resource = resource;
|
this.resource = resource;
|
||||||
this.request = reportIds;
|
this.request = reportIds;
|
||||||
|
this.scriptId = scriptId;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute(String command) {
|
public void execute(String command) {
|
||||||
// 初始化任务
|
// 初始化任务
|
||||||
LogUtils.info("CURL 命令:【 " + 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 {
|
try {
|
||||||
KubernetesProvider.exec(resource, command);
|
KubernetesProvider.exec(resource, request, scriptId, command);
|
||||||
} catch (HttpServerErrorException e) {
|
} catch (HttpServerErrorException e) {
|
||||||
handleHttpServerError(e);
|
handleHttpServerError(e);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|
|
@ -57,11 +57,20 @@ public class KubernetesProvider {
|
||||||
* @param command
|
* @param command
|
||||||
* @throws Exception
|
* @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;
|
ExecWatch execWatch = null;
|
||||||
try (KubernetesClient client = getKubernetesClient(resource)) {
|
try (KubernetesClient client = getKubernetesClient(resource)) {
|
||||||
Pod pod = getExecPod(client, resource);
|
Pod pod = getExecPod(client, resource);
|
||||||
LogUtils.info("当前执行 Pod:【 " + pod.getMetadata().getName() + " 】");
|
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())
|
execWatch = client.pods().inNamespace(client.getNamespace())
|
||||||
|
@ -69,8 +78,7 @@ public class KubernetesProvider {
|
||||||
.redirectingInput()
|
.redirectingInput()
|
||||||
.writingOutput(System.out)
|
.writingOutput(System.out)
|
||||||
.writingError(System.err)
|
.writingError(System.err)
|
||||||
.withTTY()
|
.withTTY().exec(SHELL_COMMAND, "-c", createFile + " && " + command + " && " + deleteFile);
|
||||||
.exec(SHELL_COMMAND, "-c", command);
|
|
||||||
|
|
||||||
// 等待命令执行完成,获取结果
|
// 等待命令执行完成,获取结果
|
||||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||||
|
@ -107,7 +115,7 @@ public class KubernetesProvider {
|
||||||
* @param runRequest
|
* @param runRequest
|
||||||
* @param command
|
* @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)) {
|
try (KubernetesClient client = getKubernetesClient(resource)) {
|
||||||
Pod pod = getExecPod(client, resource);
|
Pod pod = getExecPod(client, resource);
|
||||||
LogUtils.info("当前执行 Pod:【 " + pod.getMetadata().getName() + " 】");
|
LogUtils.info("当前执行 Pod:【 " + pod.getMetadata().getName() + " 】");
|
||||||
|
|
Loading…
Reference in New Issue