diff --git a/api-test/backend/src/main/java/io/metersphere/api/exec/engine/KubernetesTestEngine.java b/api-test/backend/src/main/java/io/metersphere/api/exec/engine/KubernetesTestEngine.java index 87c441941a..0a817a9d52 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/exec/engine/KubernetesTestEngine.java +++ b/api-test/backend/src/main/java/io/metersphere/api/exec/engine/KubernetesTestEngine.java @@ -2,7 +2,6 @@ package io.metersphere.api.exec.engine; import io.fabric8.kubernetes.api.model.Pod; import io.fabric8.kubernetes.client.KubernetesClient; -import io.metersphere.api.dto.definition.request.MsTestPlan; import io.metersphere.base.domain.TestResource; import io.metersphere.commons.constants.ApiRunMode; import io.metersphere.commons.constants.ExtendedParameter; @@ -66,7 +65,6 @@ public class KubernetesTestEngine extends AbstractEngine { } else if (!runRequest.isDebug()) { runRequest.getExtendedParameters().put(ExtendedParameter.SAVE_RESULT, true); } - runRequest.setJmxScript(new MsTestPlan().getJmx(runRequest.getHashTree())); runRequest.setHashTree(null); LoggerUtil.info("进入DEBUG执行模式", runRequest.getReportId()); } diff --git a/api-test/backend/src/main/java/io/metersphere/api/jmeter/JMeterService.java b/api-test/backend/src/main/java/io/metersphere/api/jmeter/JMeterService.java index 83f6c973ff..b6db672028 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/jmeter/JMeterService.java +++ b/api-test/backend/src/main/java/io/metersphere/api/jmeter/JMeterService.java @@ -146,11 +146,18 @@ public class JMeterService { // 如果是K8S调用 if (request.getPool().isK8s()) { 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()); final Engine engine = EngineFactory.createApiEngine(request); engine.start(); } catch (Exception e) { remakeReportService.testEnded(request, e.getMessage()); + String key = StringUtils.join(request.getReportId(), "-", request.getTestId()); + redisTemplate.delete(key); LoggerUtil.error("调用K8S执行请求[ " + request.getTestId() + " ]失败:", request.getReportId(), e); } } else if ((MapUtils.isNotEmpty(request.getExtendedParameters()) @@ -168,13 +175,18 @@ public class JMeterService { ApiPoolDebugService apiPoolDebugService = CommonBeanFactory.getBean(ApiPoolDebugService.class); if (apiPoolDebugService != null) { List resources = GenerateHashTreeUtil.setPoolResource(request.getPoolId()); - request.setJmxScript(new MsTestPlan().getJmx(request.getHashTree())); - request.setHashTree(null); + if (request.getHashTree() != null) { + String key = StringUtils.join(request.getReportId(), "-", request.getTestId()); + redisTemplate.opsForValue().set(key, new MsTestPlan().getJmx(request.getHashTree())); + request.setHashTree(null); + } apiPoolDebugService.run(request, resources); } } catch (Exception e) { LoggerUtil.error(e); remakeReportService.remake(request); + String key = StringUtils.join(request.getReportId(), "-", request.getTestId()); + redisTemplate.delete(key); LoggerUtil.error("发送请求[ " + request.getTestId() + " ] 执行失败,进行数据回滚:", request.getReportId(), e); MSException.throwException("调用资源池执行失败,请检查资源池是否配置正常"); } diff --git a/api-test/backend/src/main/java/io/metersphere/controller/ApiJMeterFileController.java b/api-test/backend/src/main/java/io/metersphere/controller/ApiJMeterFileController.java index 8d52eb4849..1c98eba0e3 100644 --- a/api-test/backend/src/main/java/io/metersphere/controller/ApiJMeterFileController.java +++ b/api-test/backend/src/main/java/io/metersphere/controller/ApiJMeterFileController.java @@ -3,6 +3,9 @@ package io.metersphere.controller; import io.metersphere.api.dto.BodyFileRequest; import io.metersphere.api.jmeter.JMeterThreadUtils; 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.MediaType; import org.springframework.http.ResponseEntity; @@ -17,6 +20,8 @@ public class ApiJMeterFileController { @Resource private ApiJMeterFileService apiJmeterFileService; + @Resource + private RedisTemplate redisTemplate; @GetMapping("stop/{name}") public String stop(@PathVariable String name) { @@ -68,4 +73,12 @@ public class ApiJMeterFileController { .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + request.getReportId() + ".zip\"") .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() : ""; + } } diff --git a/framework/sdk-parent/sdk/src/main/java/io/metersphere/commons/utils/ShiroUtils.java b/framework/sdk-parent/sdk/src/main/java/io/metersphere/commons/utils/ShiroUtils.java index dab3d60f45..c117f71453 100644 --- a/framework/sdk-parent/sdk/src/main/java/io/metersphere/commons/utils/ShiroUtils.java +++ b/framework/sdk-parent/sdk/src/main/java/io/metersphere/commons/utils/ShiroUtils.java @@ -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/plug/jar", "anon"); + filterChainDefinitionMap.put("/api/jmeter/get-script", "anon"); // for swagger filterChainDefinitionMap.put("/swagger-ui.html", "anon");