refactor(接口测试): 场景变量当所有优先规则不选时,默认过程变量优先并兼容历史数据
--bug=1022046 --user=赵勇 【接口测试】github#21416,1.10.11 升级1.20.18 ,接口自动化a场景引用b场景 修改b场景里面的场景变量保存后,a场景调试 刚刚修改的变量不生效 https://www.tapd.cn/55049933/s/1329562
This commit is contained in:
parent
865b99ddd4
commit
884fb9e5a8
|
@ -966,4 +966,38 @@ public class ElementUtil {
|
|||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 过程变量覆盖处理
|
||||
*
|
||||
* @param tree
|
||||
*/
|
||||
public static void coverArguments(HashTree tree) {
|
||||
Map<String, String> process = new HashMap<>();
|
||||
coverArguments(tree, process);
|
||||
}
|
||||
|
||||
public static void coverArguments(HashTree tree, Map<String, String> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
@ -25,6 +26,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) {
|
||||
|
@ -61,9 +64,10 @@ 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()) {
|
||||
|
@ -73,12 +77,13 @@ public class KubernetesTestEngine extends AbstractEngine {
|
|||
LoggerUtil.info("进入DEBUG执行模式", runRequest.getReportId());
|
||||
}
|
||||
// 拼接CURL执行命令
|
||||
StringBuffer command = new StringBuffer("curl -H \"Accept: application/json\" -H \"Content-type: application/json\" -X POST -d").append(StringUtils.SPACE);
|
||||
StringBuffer command = new StringBuffer("curl -H \"Accept: application/json\" -H \"Content-type: application/json\" -X POST -d");
|
||||
command.append(StringUtils.SPACE);
|
||||
command.append("'").append(JSON.toJSONString(runRequest)).append("'"); // 请求参数
|
||||
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(), runRequest);
|
||||
} catch (Exception e) {
|
||||
MsgDTO dto = new MsgDTO();
|
||||
|
|
|
@ -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;
|
||||
|
@ -27,6 +28,8 @@ import io.metersphere.jmeter.LocalRunner;
|
|||
import io.metersphere.service.ApiPoolDebugService;
|
||||
import io.metersphere.service.RemakeReportService;
|
||||
import io.metersphere.utils.LoggerUtil;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
import org.apache.commons.collections.MapUtils;
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
|
@ -42,8 +45,6 @@ import org.springframework.http.ResponseEntity;
|
|||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import jakarta.annotation.Resource;
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
|
@ -154,6 +155,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()));
|
||||
}
|
||||
|
@ -180,6 +182,8 @@ public class JMeterService {
|
|||
try {
|
||||
List<TestResource> 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);
|
||||
|
@ -230,6 +234,8 @@ 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);
|
||||
|
|
|
@ -56,4 +56,6 @@ 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";
|
||||
|
||||
}
|
||||
|
|
|
@ -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.*;
|
||||
|
@ -23,6 +24,7 @@ import org.apache.jorphan.collections.HashTree;
|
|||
import org.springframework.stereotype.Service;
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
@ -126,6 +128,9 @@ public class ApiJMeterFileService {
|
|||
}
|
||||
hashTree = GenerateHashTreeUtil.generateHashTree(scenario, planEnvMap, runRequest);
|
||||
}
|
||||
if (hashTree != null) {
|
||||
ElementUtil.coverArguments(hashTree);
|
||||
}
|
||||
return zipFilesToByteArray((reportId + "_" + remoteTestId), hashTree);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue