From 430784015f0f675f1ae5cb2d8e28a3e15e96da21 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Wed, 1 Feb 2023 10:57:49 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):?= =?UTF-8?q?=20=E5=9C=BA=E6=99=AF=E5=8F=98=E9=87=8F=E5=BD=93=E6=89=80?= =?UTF-8?q?=E6=9C=89=E4=BC=98=E5=85=88=E8=A7=84=E5=88=99=E4=B8=8D=E9=80=89?= =?UTF-8?q?=E6=97=B6=EF=BC=8C=E9=BB=98=E8=AE=A4=E8=BF=87=E7=A8=8B=E5=8F=98?= =?UTF-8?q?=E9=87=8F=E4=BC=98=E5=85=88=E5=B9=B6=E5=85=BC=E5=AE=B9=E5=8E=86?= =?UTF-8?q?=E5=8F=B2=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1022046 --user=赵勇 【接口测试】github#21416,1.10.11 升级1.20.18 ,接口自动化a场景引用b场景 修改b场景里面的场景变量保存后,a场景调试 刚刚修改的变量不生效 https://www.tapd.cn/55049933/s/1329562 --- .../dto/definition/request/ElementUtil.java | 34 +++++++++++++++++++ .../dto/definition/request/MsScenario.java | 1 + .../api/exec/engine/KubernetesTestEngine.java | 11 +++--- .../metersphere/api/jmeter/JMeterService.java | 4 +++ .../commons/constants/ElementConstants.java | 1 + .../service/ApiJMeterFileService.java | 4 +++ 6 files changed, 51 insertions(+), 4 deletions(-) diff --git a/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/ElementUtil.java b/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/ElementUtil.java index 8feda609b7..c9b7ee56ce 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/ElementUtil.java +++ b/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/ElementUtil.java @@ -966,4 +966,38 @@ public class ElementUtil { } return null; } + + /** + * 过程变量覆盖处理 + * + * @param tree + */ + public static void coverArguments(HashTree tree) { + Map process = new HashMap<>(); + coverArguments(tree, process); + } + + public static void coverArguments(HashTree tree, Map process) { + for (Object key : tree.keySet()) { + HashTree node = tree.get(key); + if (key instanceof Arguments) { + Arguments arguments = (Arguments) key; + if (arguments.getProperty(ElementConstants.COVER) == null) { + continue; + } + for (int i = 0; i < arguments.getArguments().size(); ++i) { + String argKey = arguments.getArgument(i).getName(); + String argValue = arguments.getArgument(i).getValue(); + if (process.containsKey(argKey)) { + arguments.getArgument(i).setValue(process.get(argKey)); + } else { + process.put(argKey, argValue); + } + } + } + if (node != null) { + coverArguments(node, process); + } + } + } } diff --git a/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/MsScenario.java b/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/MsScenario.java index 18fbcd82c0..b440f3ba4f 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/MsScenario.java +++ b/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/MsScenario.java @@ -127,6 +127,7 @@ public class MsScenario extends MsTestElement { if (BooleanUtils.isTrue(this.variableEnable) || BooleanUtils.isTrue(this.mixEnable)) { scenarioTree.add(ElementUtil.argumentsToUserParameters(valueSupposeMock)); } else if (config != null && (this.isAllEnable() || config.isApi())) { + valueSupposeMock.setProperty(ElementConstants.COVER, true); scenarioTree.add(valueSupposeMock); } } 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 c686f5a1c7..06775df445 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 @@ -3,6 +3,7 @@ package io.metersphere.api.exec.engine; import io.fabric8.kubernetes.api.model.Pod; import io.fabric8.kubernetes.client.KubernetesClient; import io.metersphere.api.dto.MsgDTO; +import io.metersphere.api.dto.definition.request.ElementUtil; import io.metersphere.base.domain.TestResource; import io.metersphere.commons.constants.ApiRunMode; import io.metersphere.commons.constants.ExtendedParameter; @@ -24,6 +25,8 @@ import java.util.Set; public class KubernetesTestEngine extends AbstractEngine { private JmeterRunRequestDTO runRequest; private final String DEBUG_ERROR = "DEBUG_ERROR"; + private final String EXEC_URL = "api/start"; + private final String DEBUG_URL = "debug"; // 初始化API调用 public KubernetesTestEngine(JmeterRunRequestDTO runRequest) { @@ -60,9 +63,9 @@ public class KubernetesTestEngine extends AbstractEngine { .append(StringUtils.LF).append("Pod信息:【 ") .append(JSON.toJSONString(pod.getMetadata())).append(" 】"); LoggerUtil.info(logMsg); - String path = "api/start"; - if (runRequest.getHashTree() != null) { - path = "debug"; + boolean isDebug = runRequest.getHashTree() != null; + if (isDebug) { + ElementUtil.coverArguments(runRequest.getHashTree()); if (runRequest.isDebug() && !StringUtils.equalsAny(runRequest.getRunMode(), ApiRunMode.DEFINITION.name())) { runRequest.getExtendedParameters().put(ExtendedParameter.SAVE_RESULT, true); } else if (!runRequest.isDebug()) { @@ -77,7 +80,7 @@ public class KubernetesTestEngine extends AbstractEngine { command.append(StringUtils.SPACE).append("--connect-timeout 30"); // 设置连接超时时间为30S 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); + command.append(StringUtils.SPACE).append("http://127.0.0.1:8082/jmeter/").append(isDebug ? DEBUG_URL : EXEC_URL); KubernetesApiExec.newExecWatch(client, clientCredential.getNamespace(), pod.getMetadata().getName(), command.toString()); } catch (Exception e) { MsgDTO dto = new MsgDTO(); 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 d760b70268..ddf9512e90 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 @@ -1,6 +1,7 @@ package io.metersphere.api.jmeter; +import io.metersphere.api.dto.definition.request.ElementUtil; import io.metersphere.api.dto.definition.request.MsTestPlan; import io.metersphere.api.exec.engine.EngineFactory; import io.metersphere.api.exec.queue.ExecThreadPoolExecutor; @@ -149,6 +150,7 @@ public class JMeterService { try { // 缓存调试脚本 if (request.getHashTree() != null) { + ElementUtil.coverArguments(request.getHashTree()); String key = StringUtils.join(request.getReportId(), "-", request.getTestId()); redisTemplate.opsForValue().set(key, new MsTestPlan().getJmx(request.getHashTree())); } @@ -177,6 +179,7 @@ public class JMeterService { if (apiPoolDebugService != null) { List resources = GenerateHashTreeUtil.setPoolResource(request.getPoolId()); if (request.getHashTree() != null) { + ElementUtil.coverArguments(request.getHashTree()); String key = StringUtils.join(request.getReportId(), "-", request.getTestId()); redisTemplate.opsForValue().set(key, new MsTestPlan().getJmx(request.getHashTree())); request.setHashTree(null); @@ -228,6 +231,7 @@ public class JMeterService { if (request.getPool().isPool() && StringUtils.isNotBlank(request.getRunMode())) { this.runNode(request); } else if (request.getHashTree() != null) { + ElementUtil.coverArguments(request.getHashTree()); //解析hashTree,是否含有文件库文件 HashTreeUtil.initRepositoryFiles(request); execThreadPoolExecutor.addTask(request); diff --git a/api-test/backend/src/main/java/io/metersphere/commons/constants/ElementConstants.java b/api-test/backend/src/main/java/io/metersphere/commons/constants/ElementConstants.java index e0ac55559d..0f96f91834 100644 --- a/api-test/backend/src/main/java/io/metersphere/commons/constants/ElementConstants.java +++ b/api-test/backend/src/main/java/io/metersphere/commons/constants/ElementConstants.java @@ -56,4 +56,5 @@ public class ElementConstants { public static final String FILE_ID = "fileId"; public static final String RESOURCE_ID = "resourceId"; public static final String FILENAME = "filename"; + public static final String COVER = "COVER"; } diff --git a/api-test/backend/src/main/java/io/metersphere/service/ApiJMeterFileService.java b/api-test/backend/src/main/java/io/metersphere/service/ApiJMeterFileService.java index 4b979cd5ee..1f4af4f9bb 100644 --- a/api-test/backend/src/main/java/io/metersphere/service/ApiJMeterFileService.java +++ b/api-test/backend/src/main/java/io/metersphere/service/ApiJMeterFileService.java @@ -2,6 +2,7 @@ package io.metersphere.service; import io.metersphere.api.dto.BodyFileRequest; import io.metersphere.api.dto.EnvironmentType; +import io.metersphere.api.dto.definition.request.ElementUtil; import io.metersphere.api.dto.definition.request.MsTestPlan; import io.metersphere.api.exec.api.ApiCaseSerialService; import io.metersphere.base.domain.*; @@ -126,6 +127,9 @@ public class ApiJMeterFileService { } hashTree = GenerateHashTreeUtil.generateHashTree(scenario, planEnvMap, runRequest); } + if (hashTree != null) { + ElementUtil.coverArguments(hashTree); + } return zipFilesToByteArray((reportId + "_" + remoteTestId), hashTree); }