refactor(接口测试): 升级spring boot 3.0后 k8s API变更优化

This commit is contained in:
fit2-zhao 2023-01-30 13:38:38 +08:00 committed by fit2-zhao
parent 14a415013a
commit daeab567c6
3 changed files with 44 additions and 77 deletions

View File

@ -5,15 +5,17 @@ import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.dsl.ExecListener;
import io.fabric8.kubernetes.client.dsl.ExecWatch;
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.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.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class KubernetesApiExec {
@ -27,72 +29,49 @@ public class KubernetesApiExec {
if (CollectionUtils.isEmpty(nodePods)) {
MSException.throwException("Execution node not found");
}
Pod pod = nodePods.get(new Random().nextInt(nodePods.size()));
return pod;
return nodePods.get(new Random().nextInt(nodePods.size()));
}
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 + "");
return client.pods().inNamespace(namespace).withName(podName)
.readingInput(System.in)
.writingOutput(System.out)
.writingError(System.err)
.withTTY()
.usingListener(new SimpleListener())
.usingListener(new SimpleListener(runRequest))
.exec("sh", "-c", command);
}
private static String getQuery(String content) {
Pattern regex = Pattern.compile("\\{([^}]*)\\}");
Matcher matcher = regex.matcher(content);
StringBuilder sql = new StringBuilder();
while (matcher.find()) {
sql.append(matcher.group(1) + ",");
}
if (sql.length() > 0) {
sql.deleteCharAt(sql.length() - 1);
}
return sql.toString();
private static class SimpleListener implements ExecListener {
private JmeterRunRequestDTO runRequest;
SimpleListener(JmeterRunRequestDTO runRequest) {
this.runRequest = runRequest;
}
private static class SimpleListener implements ExecListener {
@Override
public void onOpen() {
LoggerUtil.info("The shell will remain open for 10 seconds.");
LoggerUtil.info("K8s命令执行监听 onOpen ", runRequest.getReportId());
}
@Override
public void onFailure(Throwable t, Response response) {
// todo k8s 执行api变了
try {
LoggerUtil.info("进入K8s onFailure");
LoggerUtil.info(response);
LoggerUtil.info(t);
LoggerUtil.info(t.getMessage());
LoggerUtil.info(response.body());
} catch (IOException e) {
throw new RuntimeException(e);
LoggerUtil.info("进入K8s onFailure处理");
if (runRequest != null) {
LoggerUtil.info("请求参数:", JSON.toJSONString(runRequest));
RemakeReportService apiScenarioReportService = CommonBeanFactory.getBean(RemakeReportService.class);
apiScenarioReportService.testEnded(runRequest, StringUtils.join("K8s执行异常", t.getMessage()));
} else {
MSException.throwException("K8S 节点执行错误:" + t.getMessage());
}
// List<String> value = response.request().url().queryParameterValues("command");
// 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());
LoggerUtil.error("K8S 节点执行错误:", t.getMessage());
}
@Override
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());
}
}
}

View File

@ -6,11 +6,12 @@ import io.metersphere.api.dto.MsgDTO;
import io.metersphere.base.domain.TestResource;
import io.metersphere.commons.constants.ApiRunMode;
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.LogUtil;
import io.metersphere.commons.utils.WebSocketUtil;
import io.metersphere.dto.JmeterRunRequestDTO;
import io.metersphere.service.RemakeReportService;
import io.metersphere.utils.LoggerUtil;
import io.metersphere.xpack.resourcepool.engine.provider.ClientCredential;
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("--retry 3"); // 设置重试次数3次
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) {
MsgDTO dto = new MsgDTO();
dto.setExecEnd(false);
@ -88,13 +89,15 @@ public class KubernetesTestEngine extends AbstractEngine {
LoggerUtil.debug("send. " + runRequest.getReportId());
WebSocketUtil.sendMessageSingle(dto);
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);
MSException.throwException(e);
}
}
@Override
public void stop() {
LoggerUtil.info("K8S执行STOP", runRequest.getReportId());
}
}

View File

@ -156,32 +156,17 @@ public class TestDataGenerator {
value = StringUtils.isNotBlank(v) ? v : value;
try {
if (object.has(FORMAT)) {
String propertyFormat = object.get(FORMAT).getAsString();
switch (propertyFormat) {
case "date-time":
value = DateTimeSource.getInstance().randomTimestamp(LocalDate.now()) + "";
break;
case "date":
value = DateTimeSource.getInstance().randomDate(LocalDate.now().getYear(), "yyyy-MM-dd");
break;
case "email":
value = InternetSource.getInstance().randomEmail(maxLength);
break;
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)) {
value = switch (object.get(FORMAT).getAsString()) {
case "date-time" -> DateTimeSource.getInstance().randomTimestamp(LocalDate.now()) + "";
case "date" -> DateTimeSource.getInstance().randomDate(LocalDate.now().getYear(), "yyyy-MM-dd");
case "email" -> InternetSource.getInstance().randomEmail(maxLength);
case "hostname" -> InternetSource.getInstance().randomDomain(maxLength);
case "ipv4" -> InternetSource.getInstance().randomPublicIpv4();
case "ipv6" -> InternetSource.getInstance().randomIpV6();
case "uri" -> InternetSource.getInstance().randomStaticUrl("jpg");
default -> value;
};
} else if (object.has(PATTERN)) {
String pattern = object.get(PATTERN).getAsString();
if (StringUtils.isNotEmpty(pattern)) {
Generex generex = new Generex(pattern);