fix(接口测试): 修复k8s调试大场景不执行问题

--bug=1019898 --user=赵勇 【接口测试】禁用本地执行,使用k8s资源池执行接口后,场景中调试/生成报告场景失败 https://www.tapd.cn/55049933/s/1303262
This commit is contained in:
fit2-zhao 2022-11-23 19:38:24 +08:00 committed by fit2-zhao
parent cc9283817a
commit 9a62918734
4 changed files with 28 additions and 4 deletions

View File

@ -2,7 +2,6 @@ package io.metersphere.api.exec.engine;
import io.fabric8.kubernetes.api.model.Pod; import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.client.KubernetesClient; import io.fabric8.kubernetes.client.KubernetesClient;
import io.metersphere.api.dto.definition.request.MsTestPlan;
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;
@ -66,7 +65,6 @@ public class KubernetesTestEngine extends AbstractEngine {
} else if (!runRequest.isDebug()) { } else if (!runRequest.isDebug()) {
runRequest.getExtendedParameters().put(ExtendedParameter.SAVE_RESULT, true); runRequest.getExtendedParameters().put(ExtendedParameter.SAVE_RESULT, true);
} }
runRequest.setJmxScript(new MsTestPlan().getJmx(runRequest.getHashTree()));
runRequest.setHashTree(null); runRequest.setHashTree(null);
LoggerUtil.info("进入DEBUG执行模式", runRequest.getReportId()); LoggerUtil.info("进入DEBUG执行模式", runRequest.getReportId());
} }

View File

@ -146,11 +146,18 @@ public class JMeterService {
// 如果是K8S调用 // 如果是K8S调用
if (request.getPool().isK8s()) { if (request.getPool().isK8s()) {
try { try {
// 缓存调试脚本
if (request.getHashTree() != null) {
String key = StringUtils.join(request.getReportId(), "-", request.getTestId());
redisTemplate.opsForValue().set(key, new MsTestPlan().getJmx(request.getHashTree()));
}
LoggerUtil.info("开始发送请求[ " + request.getTestId() + " ] 到K8S节点执行", request.getReportId()); LoggerUtil.info("开始发送请求[ " + request.getTestId() + " ] 到K8S节点执行", request.getReportId());
final Engine engine = EngineFactory.createApiEngine(request); final Engine engine = EngineFactory.createApiEngine(request);
engine.start(); engine.start();
} catch (Exception e) { } catch (Exception e) {
remakeReportService.testEnded(request, e.getMessage()); remakeReportService.testEnded(request, e.getMessage());
String key = StringUtils.join(request.getReportId(), "-", request.getTestId());
redisTemplate.delete(key);
LoggerUtil.error("调用K8S执行请求[ " + request.getTestId() + " ]失败:", request.getReportId(), e); LoggerUtil.error("调用K8S执行请求[ " + request.getTestId() + " ]失败:", request.getReportId(), e);
} }
} else if ((MapUtils.isNotEmpty(request.getExtendedParameters()) } else if ((MapUtils.isNotEmpty(request.getExtendedParameters())
@ -168,13 +175,18 @@ public class JMeterService {
ApiPoolDebugService apiPoolDebugService = CommonBeanFactory.getBean(ApiPoolDebugService.class); ApiPoolDebugService apiPoolDebugService = CommonBeanFactory.getBean(ApiPoolDebugService.class);
if (apiPoolDebugService != null) { if (apiPoolDebugService != null) {
List<TestResource> resources = GenerateHashTreeUtil.setPoolResource(request.getPoolId()); List<TestResource> resources = GenerateHashTreeUtil.setPoolResource(request.getPoolId());
request.setJmxScript(new MsTestPlan().getJmx(request.getHashTree())); if (request.getHashTree() != null) {
String key = StringUtils.join(request.getReportId(), "-", request.getTestId());
redisTemplate.opsForValue().set(key, new MsTestPlan().getJmx(request.getHashTree()));
request.setHashTree(null); request.setHashTree(null);
}
apiPoolDebugService.run(request, resources); apiPoolDebugService.run(request, resources);
} }
} catch (Exception e) { } catch (Exception e) {
LoggerUtil.error(e); LoggerUtil.error(e);
remakeReportService.remake(request); remakeReportService.remake(request);
String key = StringUtils.join(request.getReportId(), "-", request.getTestId());
redisTemplate.delete(key);
LoggerUtil.error("发送请求[ " + request.getTestId() + " ] 执行失败,进行数据回滚:", request.getReportId(), e); LoggerUtil.error("发送请求[ " + request.getTestId() + " ] 执行失败,进行数据回滚:", request.getReportId(), e);
MSException.throwException("调用资源池执行失败,请检查资源池是否配置正常"); MSException.throwException("调用资源池执行失败,请检查资源池是否配置正常");
} }

View File

@ -3,6 +3,9 @@ package io.metersphere.controller;
import io.metersphere.api.dto.BodyFileRequest; import io.metersphere.api.dto.BodyFileRequest;
import io.metersphere.api.jmeter.JMeterThreadUtils; import io.metersphere.api.jmeter.JMeterThreadUtils;
import io.metersphere.service.ApiJMeterFileService; import io.metersphere.service.ApiJMeterFileService;
import io.metersphere.utils.LoggerUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
@ -17,6 +20,8 @@ public class ApiJMeterFileController {
@Resource @Resource
private ApiJMeterFileService apiJmeterFileService; private ApiJMeterFileService apiJmeterFileService;
@Resource
private RedisTemplate<String, Object> redisTemplate;
@GetMapping("stop/{name}") @GetMapping("stop/{name}")
public String stop(@PathVariable String name) { public String stop(@PathVariable String name) {
@ -68,4 +73,12 @@ public class ApiJMeterFileController {
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + request.getReportId() + ".zip\"") .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + request.getReportId() + ".zip\"")
.body(bytes); .body(bytes);
} }
@GetMapping("get-script")
public String getScript(@RequestParam("reportId") String reportId, @RequestParam("testId") String testId) {
String key = StringUtils.join(reportId, "-", testId);
LoggerUtil.info("获取执行脚本", key);
Object script = redisTemplate.opsForValue().get(key);
redisTemplate.delete(key);
return script != null ? script.toString() : "";
}
} }

View File

@ -39,6 +39,7 @@ public class ShiroUtils {
filterChainDefinitionMap.put("/api/jmeter/download/jar", "anon"); filterChainDefinitionMap.put("/api/jmeter/download/jar", "anon");
filterChainDefinitionMap.put("/api/jmeter/download/jar/**", "anon"); filterChainDefinitionMap.put("/api/jmeter/download/jar/**", "anon");
filterChainDefinitionMap.put("/api/jmeter/download/plug/jar", "anon"); filterChainDefinitionMap.put("/api/jmeter/download/plug/jar", "anon");
filterChainDefinitionMap.put("/api/jmeter/get-script", "anon");
// for swagger // for swagger
filterChainDefinitionMap.put("/swagger-ui.html", "anon"); filterChainDefinitionMap.put("/swagger-ui.html", "anon");