fix(接口测试) 使用临界控制器隔离引用场景修复同名变量问题
--bug=1009618 --user=赵勇 【接口测试】场景引用变量错误 https://www.tapd.cn/55049933/s/1096157
This commit is contained in:
parent
4a235bab40
commit
bfc9647498
|
@ -35,6 +35,7 @@ import org.apache.jmeter.config.Arguments;
|
|||
import org.apache.jmeter.config.CSVDataSet;
|
||||
import org.apache.jmeter.config.RandomVariableConfig;
|
||||
import org.apache.jmeter.modifiers.CounterConfig;
|
||||
import org.apache.jmeter.modifiers.JSR223PreProcessor;
|
||||
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy;
|
||||
import org.apache.jmeter.save.SaveService;
|
||||
import org.apache.jmeter.testelement.AbstractTestElement;
|
||||
|
@ -574,6 +575,23 @@ public class ElementUtil {
|
|||
return resourceId + "_" + ElementUtil.getFullIndexPath(parent, indexPath);
|
||||
}
|
||||
|
||||
public static JSR223PreProcessor argumentsToProcessor(Arguments arguments) {
|
||||
JSR223PreProcessor processor = new JSR223PreProcessor();
|
||||
processor.setEnabled(true);
|
||||
processor.setName("scene variable");
|
||||
processor.setProperty(TestElement.TEST_CLASS, JSR223PreProcessor.class.getName());
|
||||
processor.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI"));
|
||||
StringBuffer script = new StringBuffer();
|
||||
if (arguments != null) {
|
||||
for (int i = 0; i < arguments.getArguments().size(); ++i) {
|
||||
String argValue = arguments.getArgument(i).getValue();
|
||||
script.append("vars.put(\"" + arguments.getArgument(i).getName() + "\",\"" + argValue + "\");").append("\n");
|
||||
}
|
||||
processor.setProperty("script", script.toString());
|
||||
}
|
||||
return processor;
|
||||
}
|
||||
|
||||
public static void setBaseParams(AbstractTestElement sampler, MsTestElement parent, ParameterConfig config, String id, String indexPath) {
|
||||
sampler.setProperty("MS-ID", id);
|
||||
sampler.setProperty("MS-RESOURCE-ID", ElementUtil.getResourceId(id, config, parent, indexPath));
|
||||
|
|
|
@ -8,6 +8,7 @@ import com.fasterxml.jackson.core.type.TypeReference;
|
|||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.metersphere.api.dto.EnvironmentType;
|
||||
import io.metersphere.api.dto.definition.request.controller.MsCriticalSectionController;
|
||||
import io.metersphere.api.dto.definition.request.variable.ScenarioVariable;
|
||||
import io.metersphere.api.dto.mockconfig.MockConfigStaticData;
|
||||
import io.metersphere.api.dto.scenario.KeyValue;
|
||||
|
@ -158,14 +159,16 @@ public class MsScenario extends MsTestElement {
|
|||
if (CollectionUtils.isNotEmpty(this.getVariables())) {
|
||||
config.setVariables(this.variables);
|
||||
}
|
||||
final HashTree scenarioTree = MsCriticalSectionController.createHashTree(tree, this.getName());
|
||||
// 场景变量和环境变量
|
||||
Arguments arguments = arguments(config);
|
||||
if (arguments != null) {
|
||||
tree.add(ParameterConfig.valueSupposeMock(arguments));
|
||||
Arguments valueSupposeMock = ParameterConfig.valueSupposeMock(arguments);
|
||||
scenarioTree.add(ElementUtil.argumentsToProcessor(valueSupposeMock));
|
||||
}
|
||||
ElementUtil.addCsvDataSet(tree, variables, config, "shareMode.group");
|
||||
ElementUtil.addCounter(tree, variables, false);
|
||||
ElementUtil.addRandom(tree, variables);
|
||||
ElementUtil.addCsvDataSet(scenarioTree, variables, config, "shareMode.group");
|
||||
ElementUtil.addCounter(scenarioTree, variables, false);
|
||||
ElementUtil.addRandom(scenarioTree, variables);
|
||||
if (CollectionUtils.isNotEmpty(this.headers)) {
|
||||
config.setHeaders(this.headers);
|
||||
}
|
||||
|
@ -205,9 +208,9 @@ public class MsScenario extends MsTestElement {
|
|||
el.setParent(this);
|
||||
el.setMockEnvironment(this.isMockEnvironment());
|
||||
if (this.isEnvironmentEnable()) {
|
||||
el.toHashTree(tree, el.getHashTree(), newConfig);
|
||||
el.toHashTree(scenarioTree, el.getHashTree(), newConfig);
|
||||
} else {
|
||||
el.toHashTree(tree, el.getHashTree(), config);
|
||||
el.toHashTree(scenarioTree, el.getHashTree(), config);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
package io.metersphere.api.dto.definition.request.controller;
|
||||
|
||||
import io.metersphere.plugin.core.MsParameter;
|
||||
import io.metersphere.plugin.core.MsTestElement;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.jmeter.control.CriticalSectionController;
|
||||
import org.apache.jmeter.testelement.TestElement;
|
||||
import org.apache.jorphan.collections.HashTree;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MsCriticalSectionController extends MsTestElement {
|
||||
|
||||
@Override
|
||||
public void toHashTree(HashTree tree, List<MsTestElement> hashTree, MsParameter msParameter) {
|
||||
|
||||
}
|
||||
|
||||
public static HashTree createHashTree(HashTree tree, String name) {
|
||||
CriticalSectionController criticalSectionController = new CriticalSectionController();
|
||||
criticalSectionController.setName(StringUtils.isNotEmpty(name) ? "Csc_" + name : "Scenario Critical Section Controller");
|
||||
criticalSectionController.setLockName("global_lock");
|
||||
criticalSectionController.setEnabled(true);
|
||||
criticalSectionController.setProperty(TestElement.TEST_CLASS, CriticalSectionController.class.getName());
|
||||
criticalSectionController.setProperty(TestElement.GUI_CLASS, "CriticalSectionControllerGui");
|
||||
return tree.add(criticalSectionController);
|
||||
}
|
||||
}
|
|
@ -8,6 +8,7 @@ import io.metersphere.api.dto.scenario.environment.EnvironmentConfig;
|
|||
import io.metersphere.api.dto.shell.filter.ScriptFilter;
|
||||
import io.metersphere.plugin.core.MsParameter;
|
||||
import io.metersphere.plugin.core.MsTestElement;
|
||||
import io.metersphere.utils.JMeterVars;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
|
@ -35,8 +36,12 @@ public class MsJSR223PreProcessor extends MsTestElement {
|
|||
|
||||
@Override
|
||||
public void toHashTree(HashTree tree, List<MsTestElement> hashTree, MsParameter msParameter) {
|
||||
ScriptFilter.verify(this.getScriptLanguage(), this.getName(), script);
|
||||
ParameterConfig config = (ParameterConfig) msParameter;
|
||||
// 非导出操作,且不是启用状态则跳过执行
|
||||
if (!config.isOperating() && !this.isEnable()) {
|
||||
return;
|
||||
}
|
||||
ScriptFilter.verify(this.getScriptLanguage(), this.getName(), script);
|
||||
if (StringUtils.isEmpty(this.getEnvironmentId())) {
|
||||
if (config.getConfig() != null) {
|
||||
if (config.getProjectId() != null) {
|
||||
|
@ -54,19 +59,16 @@ public class MsJSR223PreProcessor extends MsTestElement {
|
|||
}
|
||||
}
|
||||
}
|
||||
//替换Metersphere环境变量
|
||||
script = StringUtils.replace(script, RunningParamKeys.API_ENVIRONMENT_ID, "\"" + RunningParamKeys.RUNNING_PARAMS_PREFIX + this.getEnvironmentId() + ".\"");
|
||||
|
||||
if(config.isOperating()){
|
||||
if (script.startsWith("io.metersphere.utils.JMeterVars.addVars")) {
|
||||
//替换环境变量
|
||||
if (StringUtils.isNotEmpty(script)) {
|
||||
script = StringUtils.replace(script, RunningParamKeys.API_ENVIRONMENT_ID, "\"" + RunningParamKeys.RUNNING_PARAMS_PREFIX + this.getEnvironmentId() + ".\"");
|
||||
}
|
||||
if (config.isOperating()) {
|
||||
if (script.startsWith(JMeterVars.class.getCanonicalName())) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 非导出操作,且不是启用状态则跳过执行
|
||||
if (!config.isOperating() && !this.isEnable()) {
|
||||
return;
|
||||
}
|
||||
final HashTree jsr223PreTree = tree.add(getJSR223PreProcessor());
|
||||
if (CollectionUtils.isNotEmpty(hashTree)) {
|
||||
hashTree.forEach(el -> {
|
||||
|
|
|
@ -687,11 +687,11 @@ public class ApiAutomationService {
|
|||
this.add("TCPSampler");
|
||||
}};
|
||||
|
||||
private void setElement(JSONObject element, Integer num, boolean enable, String versionName, boolean versionEnable) {
|
||||
private void setElement(JSONObject element, Integer num, Boolean enable, String versionName, Boolean versionEnable) {
|
||||
element.put("num", num);
|
||||
element.put("enable", enable);
|
||||
element.put("enable", enable == null ? false : enable);
|
||||
element.put("versionName", versionName);
|
||||
element.put("versionEnable", versionEnable);
|
||||
element.put("versionEnable", versionEnable == null ? false : versionEnable);
|
||||
}
|
||||
|
||||
private JSONObject setRequest(JSONObject element) {
|
||||
|
@ -1470,7 +1470,7 @@ public class ApiAutomationService {
|
|||
if (StringUtils.isNotEmpty(item.getScenarioDefinition())) {
|
||||
String jmx = generateJmx(item);
|
||||
if (StringUtils.isNotEmpty(jmx)) {
|
||||
ApiScenrioExportJmx scenrioExportJmx = new ApiScenrioExportJmx(item.getName(), apiTestService.updateJmxString(jmx,item.getProjectId()).getXml());
|
||||
ApiScenrioExportJmx scenrioExportJmx = new ApiScenrioExportJmx(item.getName(), apiTestService.updateJmxString(jmx, item.getProjectId()).getXml());
|
||||
JmxInfoDTO dto = apiTestService.updateJmxString(jmx, item.getProjectId());
|
||||
scenrioExportJmx.setId(item.getId());
|
||||
scenrioExportJmx.setVersion(item.getVersion());
|
||||
|
@ -1887,7 +1887,7 @@ public class ApiAutomationService {
|
|||
String testName = item.getName();
|
||||
MsTestPlan testPlan = new MsTestPlan();
|
||||
testPlan.setHashTree(new LinkedList<>());
|
||||
JmxInfoDTO dto = apiTestService.updateJmxString(generateJmx(item),item.getProjectId());
|
||||
JmxInfoDTO dto = apiTestService.updateJmxString(generateJmx(item), item.getProjectId());
|
||||
String name = item.getName() + ".jmx";
|
||||
dto.setId(item.getId());
|
||||
dto.setName(name);
|
||||
|
|
Loading…
Reference in New Issue