refactor(接口测试): 构造器优化

This commit is contained in:
fit2-zhao 2024-10-15 12:29:25 +08:00 committed by Craftsman
parent 3a2789c154
commit d4ff0d46b6
2 changed files with 11 additions and 42 deletions

View File

@ -22,7 +22,6 @@ public class KubernetesExecEngine implements ApiEngine {
*/ */
private final Object request; private final Object request;
private final TestResourceDTO resource; private final TestResourceDTO resource;
private final String optToken;
/** /**
* 单调执行构造函数 * 单调执行构造函数
@ -30,10 +29,9 @@ public class KubernetesExecEngine implements ApiEngine {
* @param request * @param request
* @param resource * @param resource
*/ */
public KubernetesExecEngine(TaskRequestDTO request, TestResourceDTO resource, String optToken) { public KubernetesExecEngine(TaskRequestDTO request, TestResourceDTO resource) {
this.request = request; this.request = request;
this.resource = resource; this.resource = resource;
this.optToken = optToken;
} }
/** /**
@ -42,10 +40,9 @@ public class KubernetesExecEngine implements ApiEngine {
* @param batchRequestDTO * @param batchRequestDTO
* @param resource * @param resource
*/ */
public KubernetesExecEngine(TaskBatchRequestDTO batchRequestDTO, TestResourceDTO resource, String optToken) { public KubernetesExecEngine(TaskBatchRequestDTO batchRequestDTO, TestResourceDTO resource) {
this.resource = resource; this.resource = resource;
this.request = batchRequestDTO; this.request = batchRequestDTO;
this.optToken = optToken;
} }
/** /**
@ -54,10 +51,9 @@ public class KubernetesExecEngine implements ApiEngine {
* @param reportIds * @param reportIds
* @param resource * @param resource
*/ */
public KubernetesExecEngine(List<String> reportIds, TestResourceDTO resource, String optToken) { public KubernetesExecEngine(List<String> reportIds, TestResourceDTO resource) {
this.resource = resource; this.resource = resource;
this.request = reportIds; this.request = reportIds;
this.optToken = optToken;
} }
@Override @Override
@ -67,9 +63,9 @@ public class KubernetesExecEngine implements ApiEngine {
this.runApi(path, request); this.runApi(path, request);
} }
private void runApi(String apiPath, Object request) { private void runApi(String command, Object request) {
try { try {
KubernetesProvider.exec(resource, request, apiPath, optToken); KubernetesProvider.exec(resource, request, command);
} catch (HttpServerErrorException e) { } catch (HttpServerErrorException e) {
handleHttpServerError(e); handleHttpServerError(e);
} catch (Exception e) { } catch (Exception e) {

View File

@ -29,7 +29,6 @@ public class KubernetesProvider {
private static final String RUNNING_PHASE = "Running"; private static final String RUNNING_PHASE = "Running";
private static final String SHELL_COMMAND = "sh"; private static final String SHELL_COMMAND = "sh";
private static final String LOCAL_URL = "http://127.0.0.1:8000";
private static final int TIMEOUT = 120000; private static final int TIMEOUT = 120000;
public static KubernetesClient getKubernetesClient(TestResourceDTO credential) { public static KubernetesClient getKubernetesClient(TestResourceDTO credential) {
@ -70,9 +69,9 @@ public class KubernetesProvider {
* 执行命令 * 执行命令
* *
* @param resource * @param resource
* @param apiPath * @param command
*/ */
protected static void exec(TestResourceDTO resource, Object runRequest, String apiPath, String optToken) { protected static void exec(TestResourceDTO resource, Object runRequest, String command) {
KubernetesClient client = getKubernetesClient(resource); KubernetesClient client = getKubernetesClient(resource);
try { try {
if (runRequest instanceof TaskBatchRequestDTO request) { if (runRequest instanceof TaskBatchRequestDTO request) {
@ -94,19 +93,19 @@ public class KubernetesProvider {
.toList(); .toList();
LogUtils.info("Sending batch tasks to pod {} for execution:\n{}", pod.getMetadata().getName(), taskKeys); LogUtils.info("Sending batch tasks to pod {} for execution:\n{}", pod.getMetadata().getName(), taskKeys);
executeCommandOnPod(client, pod, subTaskRequest, apiPath, optToken); executeCommandOnPod(client, pod, subTaskRequest, command);
} }
} else if (runRequest instanceof TaskRequestDTO) { } else if (runRequest instanceof TaskRequestDTO) {
// 随机一个 Pod 执行 // 随机一个 Pod 执行
Pod pod = getExecPod(client, resource); Pod pod = getExecPod(client, resource);
LogUtils.info("Executing task on pod: {}", pod.getMetadata().getName()); LogUtils.info("Executing task on pod: {}", pod.getMetadata().getName());
executeCommandOnPod(client, pod, runRequest, apiPath, optToken); executeCommandOnPod(client, pod, runRequest, command);
} else { } else {
// 发送给每一个 Pod // 发送给每一个 Pod
LogUtils.info("Stop tasks [{}] on Pods", runRequest); LogUtils.info("Stop tasks [{}] on Pods", runRequest);
List<Pod> nodesList = getPods(client, resource); List<Pod> nodesList = getPods(client, resource);
for (Pod pod : nodesList) { for (Pod pod : nodesList) {
executeCommandOnPod(client, pod, runRequest, apiPath, optToken); executeCommandOnPod(client, pod, runRequest, command);
} }
} }
} catch (Exception e) { } catch (Exception e) {
@ -141,10 +140,8 @@ public class KubernetesProvider {
/** /**
* Executes the curl command on a given Kubernetes pod. * Executes the curl command on a given Kubernetes pod.
*/ */
private static void executeCommandOnPod(KubernetesClient client, Pod pod, Object runRequest, String apiPath, String optToken) { private static void executeCommandOnPod(KubernetesClient client, Pod pod, Object runRequest, String command) {
try { try {
String command = buildCurlCommand(apiPath, runRequest, optToken);
LogUtils.info("Executing command on pod {}: 【{}】", pod.getMetadata().getName(), command); LogUtils.info("Executing command on pod {}: 【{}】", pod.getMetadata().getName(), command);
// Execute the command on the pod // Execute the command on the pod
@ -223,28 +220,4 @@ public class KubernetesProvider {
return result; return result;
} }
private static String buildCurlCommand(String path, Object request, String optToken) {
return String.format("""
curl -H "Accept: application/json" \
-H "Content-type: application/json" \
-H "otp-token: %s" \
-X POST -d '%s' \
--connect-timeout %d \
--max-time %d \
--retry-max-time %d \
--retry %d \
%s%s
""",
optToken, // otp-token
JSON.toFormatJSONString(request), // 请求体
30, // 连接超时
120, // 最大时间
3, // 最大重试时间
3, // 重试次数
LOCAL_URL, // 本地 URL
path // 具体 API 路径
);
}
} }