refactor(接口测试): 升级spring boot 3.0后 k8s API变更优化
This commit is contained in:
parent
14a415013a
commit
daeab567c6
|
@ -5,15 +5,17 @@ import io.fabric8.kubernetes.client.KubernetesClient;
|
||||||
import io.fabric8.kubernetes.client.dsl.ExecListener;
|
import io.fabric8.kubernetes.client.dsl.ExecListener;
|
||||||
import io.fabric8.kubernetes.client.dsl.ExecWatch;
|
import io.fabric8.kubernetes.client.dsl.ExecWatch;
|
||||||
import io.metersphere.commons.exception.MSException;
|
import io.metersphere.commons.exception.MSException;
|
||||||
|
import io.metersphere.commons.utils.CommonBeanFactory;
|
||||||
|
import io.metersphere.commons.utils.JSON;
|
||||||
|
import io.metersphere.dto.JmeterRunRequestDTO;
|
||||||
|
import io.metersphere.service.RemakeReportService;
|
||||||
import io.metersphere.utils.LoggerUtil;
|
import io.metersphere.utils.LoggerUtil;
|
||||||
import io.metersphere.xpack.resourcepool.engine.provider.ClientCredential;
|
import io.metersphere.xpack.resourcepool.engine.provider.ClientCredential;
|
||||||
import org.apache.commons.collections4.CollectionUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.springframework.util.CollectionUtils;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.regex.Matcher;
|
|
||||||
import java.util.regex.Pattern;
|
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class KubernetesApiExec {
|
public class KubernetesApiExec {
|
||||||
|
@ -27,72 +29,49 @@ public class KubernetesApiExec {
|
||||||
if (CollectionUtils.isEmpty(nodePods)) {
|
if (CollectionUtils.isEmpty(nodePods)) {
|
||||||
MSException.throwException("Execution node not found");
|
MSException.throwException("Execution node not found");
|
||||||
}
|
}
|
||||||
Pod pod = nodePods.get(new Random().nextInt(nodePods.size()));
|
return nodePods.get(new Random().nextInt(nodePods.size()));
|
||||||
return pod;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ExecWatch newExecWatch(KubernetesClient client, String namespace, String podName, String command) {
|
public static ExecWatch newExecWatch(KubernetesClient client, String namespace, String podName, String command, JmeterRunRequestDTO runRequest) {
|
||||||
LoggerUtil.info("CURL 命令:【 " + command + " 】");
|
LoggerUtil.info("CURL 命令:【 " + command + " 】");
|
||||||
return client.pods().inNamespace(namespace).withName(podName)
|
return client.pods().inNamespace(namespace).withName(podName)
|
||||||
.readingInput(System.in)
|
.readingInput(System.in)
|
||||||
.writingOutput(System.out)
|
.writingOutput(System.out)
|
||||||
.writingError(System.err)
|
.writingError(System.err)
|
||||||
.withTTY()
|
.withTTY()
|
||||||
.usingListener(new SimpleListener())
|
.usingListener(new SimpleListener(runRequest))
|
||||||
.exec("sh", "-c", command);
|
.exec("sh", "-c", command);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String getQuery(String content) {
|
private static class SimpleListener implements ExecListener {
|
||||||
Pattern regex = Pattern.compile("\\{([^}]*)\\}");
|
private JmeterRunRequestDTO runRequest;
|
||||||
Matcher matcher = regex.matcher(content);
|
|
||||||
StringBuilder sql = new StringBuilder();
|
SimpleListener(JmeterRunRequestDTO runRequest) {
|
||||||
while (matcher.find()) {
|
this.runRequest = runRequest;
|
||||||
sql.append(matcher.group(1) + ",");
|
|
||||||
}
|
|
||||||
if (sql.length() > 0) {
|
|
||||||
sql.deleteCharAt(sql.length() - 1);
|
|
||||||
}
|
|
||||||
return sql.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class SimpleListener implements ExecListener {
|
|
||||||
@Override
|
@Override
|
||||||
public void onOpen() {
|
public void onOpen() {
|
||||||
LoggerUtil.info("The shell will remain open for 10 seconds.");
|
LoggerUtil.info("K8s命令执行监听 onOpen ", runRequest.getReportId());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(Throwable t, Response response) {
|
public void onFailure(Throwable t, Response response) {
|
||||||
// todo k8s 执行api变了
|
LoggerUtil.info("进入K8s onFailure处理");
|
||||||
try {
|
if (runRequest != null) {
|
||||||
LoggerUtil.info("进入K8s onFailure");
|
LoggerUtil.info("请求参数:", JSON.toJSONString(runRequest));
|
||||||
LoggerUtil.info(response);
|
RemakeReportService apiScenarioReportService = CommonBeanFactory.getBean(RemakeReportService.class);
|
||||||
LoggerUtil.info(t);
|
apiScenarioReportService.testEnded(runRequest, StringUtils.join("K8s执行异常:", t.getMessage()));
|
||||||
LoggerUtil.info(t.getMessage());
|
} else {
|
||||||
LoggerUtil.info(response.body());
|
MSException.throwException("K8S 节点执行错误:" + t.getMessage());
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
}
|
||||||
// List<String> value = response.request().url().queryParameterValues("command");
|
LoggerUtil.error("K8S 节点执行错误:", t.getMessage());
|
||||||
// if (CollectionUtils.isNotEmpty(value) && value.size() > 2 && value.get(2).startsWith("curl")) {
|
|
||||||
// String query = "{" + KubernetesApiExec.getQuery(value.get(2)) + "}";
|
|
||||||
// JmeterRunRequestDTO runRequest = JSON.parseObject(query, JmeterRunRequestDTO.class);
|
|
||||||
// if (runRequest != null) {
|
|
||||||
// RemakeReportService apiScenarioReportService = CommonBeanFactory.getBean(RemakeReportService.class);
|
|
||||||
// apiScenarioReportService.testEnded(runRequest, response.networkResponse().message());
|
|
||||||
// } else {
|
|
||||||
// MSException.throwException("K8S 节点执行错误:" + response.networkResponse().message());
|
|
||||||
// }
|
|
||||||
// } else {
|
|
||||||
// MSException.throwException("K8S 节点执行错误:" + response.networkResponse().message());
|
|
||||||
// }
|
|
||||||
// LoggerUtil.error("K8S 节点执行错误:" + JSON.toJSONString(value));
|
|
||||||
// LoggerUtil.error("K8S 节点执行错误:" + response.networkResponse());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onClose(int code, String reason) {
|
public void onClose(int code, String reason) {
|
||||||
LoggerUtil.info("The shell will now close.");
|
LoggerUtil.info(code + "_" + reason, runRequest.getReportId());
|
||||||
|
LoggerUtil.info("K8s命令执行监听 onClose ", runRequest.getReportId());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,11 +6,12 @@ import io.metersphere.api.dto.MsgDTO;
|
||||||
import io.metersphere.base.domain.TestResource;
|
import io.metersphere.base.domain.TestResource;
|
||||||
import io.metersphere.commons.constants.ApiRunMode;
|
import io.metersphere.commons.constants.ApiRunMode;
|
||||||
import io.metersphere.commons.constants.ExtendedParameter;
|
import io.metersphere.commons.constants.ExtendedParameter;
|
||||||
import io.metersphere.commons.exception.MSException;
|
import io.metersphere.commons.utils.CommonBeanFactory;
|
||||||
import io.metersphere.commons.utils.JSON;
|
import io.metersphere.commons.utils.JSON;
|
||||||
import io.metersphere.commons.utils.LogUtil;
|
import io.metersphere.commons.utils.LogUtil;
|
||||||
import io.metersphere.commons.utils.WebSocketUtil;
|
import io.metersphere.commons.utils.WebSocketUtil;
|
||||||
import io.metersphere.dto.JmeterRunRequestDTO;
|
import io.metersphere.dto.JmeterRunRequestDTO;
|
||||||
|
import io.metersphere.service.RemakeReportService;
|
||||||
import io.metersphere.utils.LoggerUtil;
|
import io.metersphere.utils.LoggerUtil;
|
||||||
import io.metersphere.xpack.resourcepool.engine.provider.ClientCredential;
|
import io.metersphere.xpack.resourcepool.engine.provider.ClientCredential;
|
||||||
import io.metersphere.xpack.resourcepool.engine.provider.KubernetesProvider;
|
import io.metersphere.xpack.resourcepool.engine.provider.KubernetesProvider;
|
||||||
|
@ -78,7 +79,7 @@ public class KubernetesTestEngine extends AbstractEngine {
|
||||||
command.append(StringUtils.SPACE).append("--max-time 120"); // 设置请求超时时间为120S
|
command.append(StringUtils.SPACE).append("--max-time 120"); // 设置请求超时时间为120S
|
||||||
command.append(StringUtils.SPACE).append("--retry 3"); // 设置重试次数3次
|
command.append(StringUtils.SPACE).append("--retry 3"); // 设置重试次数3次
|
||||||
command.append(StringUtils.SPACE).append("http://127.0.0.1:8082/jmeter/").append(path);
|
command.append(StringUtils.SPACE).append("http://127.0.0.1:8082/jmeter/").append(path);
|
||||||
KubernetesApiExec.newExecWatch(client, clientCredential.getNamespace(), pod.getMetadata().getName(), command.toString());
|
KubernetesApiExec.newExecWatch(client, clientCredential.getNamespace(), pod.getMetadata().getName(), command.toString(), runRequest);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
MsgDTO dto = new MsgDTO();
|
MsgDTO dto = new MsgDTO();
|
||||||
dto.setExecEnd(false);
|
dto.setExecEnd(false);
|
||||||
|
@ -88,13 +89,15 @@ public class KubernetesTestEngine extends AbstractEngine {
|
||||||
LoggerUtil.debug("send. " + runRequest.getReportId());
|
LoggerUtil.debug("send. " + runRequest.getReportId());
|
||||||
WebSocketUtil.sendMessageSingle(dto);
|
WebSocketUtil.sendMessageSingle(dto);
|
||||||
WebSocketUtil.onClose(runRequest.getReportId());
|
WebSocketUtil.onClose(runRequest.getReportId());
|
||||||
|
|
||||||
|
RemakeReportService remake = CommonBeanFactory.getBean(RemakeReportService.class);
|
||||||
|
remake.testEnded(runRequest, StringUtils.join("K8s执行异常:", e.getMessage()));
|
||||||
LoggerUtil.error("当前报告:【" + runRequest.getReportId() + "】资源:【" + runRequest.getTestId() + "】CURL失败:", e);
|
LoggerUtil.error("当前报告:【" + runRequest.getReportId() + "】资源:【" + runRequest.getTestId() + "】CURL失败:", e);
|
||||||
MSException.throwException(e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void stop() {
|
public void stop() {
|
||||||
|
LoggerUtil.info("K8S执行STOP:", runRequest.getReportId());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -156,32 +156,17 @@ public class TestDataGenerator {
|
||||||
value = StringUtils.isNotBlank(v) ? v : value;
|
value = StringUtils.isNotBlank(v) ? v : value;
|
||||||
try {
|
try {
|
||||||
if (object.has(FORMAT)) {
|
if (object.has(FORMAT)) {
|
||||||
String propertyFormat = object.get(FORMAT).getAsString();
|
value = switch (object.get(FORMAT).getAsString()) {
|
||||||
switch (propertyFormat) {
|
case "date-time" -> DateTimeSource.getInstance().randomTimestamp(LocalDate.now()) + "";
|
||||||
case "date-time":
|
case "date" -> DateTimeSource.getInstance().randomDate(LocalDate.now().getYear(), "yyyy-MM-dd");
|
||||||
value = DateTimeSource.getInstance().randomTimestamp(LocalDate.now()) + "";
|
case "email" -> InternetSource.getInstance().randomEmail(maxLength);
|
||||||
break;
|
case "hostname" -> InternetSource.getInstance().randomDomain(maxLength);
|
||||||
case "date":
|
case "ipv4" -> InternetSource.getInstance().randomPublicIpv4();
|
||||||
value = DateTimeSource.getInstance().randomDate(LocalDate.now().getYear(), "yyyy-MM-dd");
|
case "ipv6" -> InternetSource.getInstance().randomIpV6();
|
||||||
break;
|
case "uri" -> InternetSource.getInstance().randomStaticUrl("jpg");
|
||||||
case "email":
|
default -> value;
|
||||||
value = InternetSource.getInstance().randomEmail(maxLength);
|
};
|
||||||
break;
|
} else if (object.has(PATTERN)) {
|
||||||
case "hostname":
|
|
||||||
value = InternetSource.getInstance().randomDomain(maxLength);
|
|
||||||
break;
|
|
||||||
case "ipv4":
|
|
||||||
value = InternetSource.getInstance().randomPublicIpv4();
|
|
||||||
break;
|
|
||||||
case "ipv6":
|
|
||||||
value = InternetSource.getInstance().randomIpV6();
|
|
||||||
break;
|
|
||||||
case "uri":
|
|
||||||
value = InternetSource.getInstance().randomStaticUrl("jpg");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (object.has(PATTERN)) {
|
|
||||||
String pattern = object.get(PATTERN).getAsString();
|
String pattern = object.get(PATTERN).getAsString();
|
||||||
if (StringUtils.isNotEmpty(pattern)) {
|
if (StringUtils.isNotEmpty(pattern)) {
|
||||||
Generex generex = new Generex(pattern);
|
Generex generex = new Generex(pattern);
|
||||||
|
|
Loading…
Reference in New Issue