fix(接口测试): 脚本设置环境变量报错
This commit is contained in:
parent
d540191a12
commit
0c365ad88f
|
@ -41,7 +41,7 @@ public class ApiExecuteResourceController {
|
||||||
@GetMapping("script")
|
@GetMapping("script")
|
||||||
public String getScript(@RequestParam("reportId") String reportId, @RequestParam("testId") String testId) {
|
public String getScript(@RequestParam("reportId") String reportId, @RequestParam("testId") String testId) {
|
||||||
String key = apiExecuteService.getScriptRedisKey(reportId, testId);
|
String key = apiExecuteService.getScriptRedisKey(reportId, testId);
|
||||||
LogUtils.info("获取执行脚本: ", key);
|
LogUtils.info("获取执行脚本: {}", key);
|
||||||
String script = stringRedisTemplate.opsForValue().get(key);
|
String script = stringRedisTemplate.opsForValue().get(key);
|
||||||
stringRedisTemplate.delete(key);
|
stringRedisTemplate.delete(key);
|
||||||
apiReportService.updateReportStatus(reportId, ApiReportStatus.RERUNNING.name());
|
apiReportService.updateReportStatus(reportId, ApiReportStatus.RERUNNING.name());
|
||||||
|
|
|
@ -45,12 +45,13 @@ public class JmeterTestElementParser implements TestElementParser {
|
||||||
name = msTestElement.getName();
|
name = msTestElement.getName();
|
||||||
final HashTree testPlanTree = hashTree.add(testPlan);
|
final HashTree testPlanTree = hashTree.add(testPlan);
|
||||||
final HashTree groupTree = testPlanTree.add(getThreadGroup());
|
final HashTree groupTree = testPlanTree.add(getThreadGroup());
|
||||||
// 添加 debugSampler
|
|
||||||
groupTree.add(getDebugSampler());
|
|
||||||
|
|
||||||
// 解析 msTestElement
|
// 解析 msTestElement
|
||||||
JmeterElementConverterRegister.getConverter(msTestElement.getClass()).toHashTree(groupTree, msTestElement, config);
|
JmeterElementConverterRegister.getConverter(msTestElement.getClass()).toHashTree(groupTree, msTestElement, config);
|
||||||
|
|
||||||
|
// 添加 debugSampler,放最后才能采集到变量信息
|
||||||
|
groupTree.add(getDebugSampler());
|
||||||
|
|
||||||
return getJmx(hashTree);
|
return getJmx(hashTree);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -53,7 +53,7 @@ public class MsCommentScriptElementConverter extends AbstractJmeterElementConver
|
||||||
} else {
|
} else {
|
||||||
scriptElement = new BeanShellSampler();
|
scriptElement = new BeanShellSampler();
|
||||||
}
|
}
|
||||||
ScriptProcessorConverter.parse(scriptElement, scriptProcessor);
|
ScriptProcessorConverter.parse(scriptElement, scriptProcessor, config);
|
||||||
// 添加公共脚本的参数
|
// 添加公共脚本的参数
|
||||||
Optional.ofNullable(ScriptProcessorConverter.getScriptArguments(scriptProcessor))
|
Optional.ofNullable(ScriptProcessorConverter.getScriptArguments(scriptProcessor))
|
||||||
.ifPresent(hashTree::add);
|
.ifPresent(hashTree::add);
|
||||||
|
|
|
@ -22,6 +22,7 @@ public class ScenarioScriptProcessorConverter extends ScriptProcessorConverter {
|
||||||
if (!needParse(scriptProcessor, config) || !scriptProcessor.isValid()) {
|
if (!needParse(scriptProcessor, config) || !scriptProcessor.isValid()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
EnvScenarioScriptProcessor scenarioScriptProcessor = (EnvScenarioScriptProcessor) scriptProcessor;
|
EnvScenarioScriptProcessor scenarioScriptProcessor = (EnvScenarioScriptProcessor) scriptProcessor;
|
||||||
Boolean associateScenarioResult = scenarioScriptProcessor.getAssociateScenarioResult();
|
Boolean associateScenarioResult = scenarioScriptProcessor.getAssociateScenarioResult();
|
||||||
|
|
||||||
|
@ -32,7 +33,7 @@ public class ScenarioScriptProcessorConverter extends ScriptProcessorConverter {
|
||||||
processor = new BeanShellSampler();
|
processor = new BeanShellSampler();
|
||||||
}
|
}
|
||||||
|
|
||||||
parse(processor, scriptProcessor);
|
parse(processor, scriptProcessor, config);
|
||||||
|
|
||||||
// 添加公共脚本的参数
|
// 添加公共脚本的参数
|
||||||
Optional.ofNullable(getScriptArguments(scriptProcessor))
|
Optional.ofNullable(getScriptArguments(scriptProcessor))
|
||||||
|
|
|
@ -25,7 +25,7 @@ public class ScriptPostProcessorConverter extends ScriptProcessorConverter {
|
||||||
} else {
|
} else {
|
||||||
processor = new BeanShellPostProcessor();
|
processor = new BeanShellPostProcessor();
|
||||||
}
|
}
|
||||||
parse(processor, scriptProcessor);
|
parse(processor, scriptProcessor, config);
|
||||||
|
|
||||||
// 添加公共脚本的参数
|
// 添加公共脚本的参数
|
||||||
Optional.ofNullable(getScriptArguments(scriptProcessor))
|
Optional.ofNullable(getScriptArguments(scriptProcessor))
|
||||||
|
|
|
@ -25,7 +25,7 @@ public class ScriptPreProcessorConverter extends ScriptProcessorConverter {
|
||||||
} else {
|
} else {
|
||||||
processor = new BeanShellPreProcessor();
|
processor = new BeanShellPreProcessor();
|
||||||
}
|
}
|
||||||
parse(processor, scriptProcessor);
|
parse(processor, scriptProcessor, config);
|
||||||
|
|
||||||
// 添加公共脚本的参数
|
// 添加公共脚本的参数
|
||||||
Optional.ofNullable(getScriptArguments(scriptProcessor))
|
Optional.ofNullable(getScriptArguments(scriptProcessor))
|
||||||
|
|
|
@ -1,13 +1,16 @@
|
||||||
package io.metersphere.api.parser.jmeter.processor;
|
package io.metersphere.api.parser.jmeter.processor;
|
||||||
|
|
||||||
|
import io.metersphere.api.dto.ApiParamConfig;
|
||||||
import io.metersphere.api.parser.jmeter.JmeterTestElementParserHelper;
|
import io.metersphere.api.parser.jmeter.JmeterTestElementParserHelper;
|
||||||
import io.metersphere.api.parser.jmeter.constants.JmeterAlias;
|
import io.metersphere.api.parser.jmeter.constants.JmeterAlias;
|
||||||
import io.metersphere.api.parser.jmeter.constants.JmeterProperty;
|
import io.metersphere.api.parser.jmeter.constants.JmeterProperty;
|
||||||
import io.metersphere.plugin.api.constants.ElementProperty;
|
import io.metersphere.plugin.api.constants.ElementProperty;
|
||||||
|
import io.metersphere.plugin.api.dto.ParameterConfig;
|
||||||
import io.metersphere.project.api.KeyValueParam;
|
import io.metersphere.project.api.KeyValueParam;
|
||||||
import io.metersphere.project.api.processor.ScriptProcessor;
|
import io.metersphere.project.api.processor.ScriptProcessor;
|
||||||
import io.metersphere.project.constants.ScriptLanguageType;
|
import io.metersphere.project.constants.ScriptLanguageType;
|
||||||
import io.metersphere.project.dto.CommonScriptInfo;
|
import io.metersphere.project.dto.CommonScriptInfo;
|
||||||
|
import io.metersphere.project.dto.environment.EnvironmentInfoDTO;
|
||||||
import org.apache.commons.collections.CollectionUtils;
|
import org.apache.commons.collections.CollectionUtils;
|
||||||
import org.apache.commons.lang3.BooleanUtils;
|
import org.apache.commons.lang3.BooleanUtils;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
@ -27,7 +30,7 @@ public abstract class ScriptProcessorConverter extends MsProcessorConverter<Scri
|
||||||
public static final String ENV_VARIABLE_EXPRESSION = "${__metersphere_env_id}";
|
public static final String ENV_VARIABLE_EXPRESSION = "${__metersphere_env_id}";
|
||||||
public static final String MS_RUNNING_ENV_PREFIX = "MS.ENV.";
|
public static final String MS_RUNNING_ENV_PREFIX = "MS.ENV.";
|
||||||
|
|
||||||
public static void parse(TestElement testElement, ScriptProcessor scriptProcessor) {
|
public static void parse(TestElement testElement, ScriptProcessor scriptProcessor, ParameterConfig config) {
|
||||||
// 脚本安全校验
|
// 脚本安全校验
|
||||||
ScriptFilter.verify(scriptProcessor.getScriptLanguage(), scriptProcessor.getName(), scriptProcessor.getScript());
|
ScriptFilter.verify(scriptProcessor.getScriptLanguage(), scriptProcessor.getName(), scriptProcessor.getScript());
|
||||||
|
|
||||||
|
@ -35,11 +38,15 @@ public abstract class ScriptProcessorConverter extends MsProcessorConverter<Scri
|
||||||
String name = StringUtils.isEmpty(scriptProcessor.getName()) ? scriptProcessor.getClass().getSimpleName() : scriptProcessor.getName();
|
String name = StringUtils.isEmpty(scriptProcessor.getName()) ? scriptProcessor.getClass().getSimpleName() : scriptProcessor.getName();
|
||||||
testElement.setName(name);
|
testElement.setName(name);
|
||||||
|
|
||||||
// todo 替换环境变量
|
// 设置环境变量
|
||||||
// String evnId = scriptProcessor.getEnvironmentId();
|
ApiParamConfig apiParamConfig = (ApiParamConfig) config;
|
||||||
// if (StringUtils.isNotEmpty(scriptProcessor.getScript())) {
|
EnvironmentInfoDTO envConfig = apiParamConfig.getEnvConfig(scriptProcessor.getProjectId());
|
||||||
// scriptProcessor.setScript(StringUtils.replace(scriptProcessor.getScript(), ENV_VARIABLE_EXPRESSION, "\"" + MS_RUNNING_ENV_PREFIX + evnId + ".\""));
|
if (envConfig != null) {
|
||||||
// }
|
String envId = envConfig.getId();
|
||||||
|
if (StringUtils.isNotEmpty(scriptProcessor.getScript())) {
|
||||||
|
scriptProcessor.setScript(StringUtils.replace(scriptProcessor.getScript(), ENV_VARIABLE_EXPRESSION, "\"" + MS_RUNNING_ENV_PREFIX + envId + ".\""));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// python 和 js cache 打开
|
// python 和 js cache 打开
|
||||||
boolean cacheKey = StringUtils.equalsAny(scriptProcessor.getScriptLanguage(), ScriptLanguageType.PYTHON.name(), ScriptLanguageType.JAVASCRIPT.name());
|
boolean cacheKey = StringUtils.equalsAny(scriptProcessor.getScriptLanguage(), ScriptLanguageType.PYTHON.name(), ScriptLanguageType.JAVASCRIPT.name());
|
||||||
|
|
|
@ -30,7 +30,7 @@ public class ScriptAssertionConverter extends AssertionConverter<MsScriptAsserti
|
||||||
assertion = new BeanShellAssertion();
|
assertion = new BeanShellAssertion();
|
||||||
}
|
}
|
||||||
ScriptProcessor scriptProcessor = BeanUtils.copyBean(new ScriptProcessor(), msAssertion);
|
ScriptProcessor scriptProcessor = BeanUtils.copyBean(new ScriptProcessor(), msAssertion);
|
||||||
ScriptProcessorConverter.parse(assertion, scriptProcessor);
|
ScriptProcessorConverter.parse(assertion, scriptProcessor, config);
|
||||||
|
|
||||||
// 添加公共脚本的参数
|
// 添加公共脚本的参数
|
||||||
Optional.ofNullable(ScriptProcessorConverter.getScriptArguments(scriptProcessor))
|
Optional.ofNullable(ScriptProcessorConverter.getScriptArguments(scriptProcessor))
|
||||||
|
|
|
@ -29,7 +29,7 @@ public class VariableAssertionConverter extends AssertionConverter<MsVariableAss
|
||||||
.filter(this::isValid)
|
.filter(this::isValid)
|
||||||
.forEach(variableAssertionItem -> {
|
.forEach(variableAssertionItem -> {
|
||||||
if (needParse(variableAssertionItem, config)) {
|
if (needParse(variableAssertionItem, config)) {
|
||||||
JSR223Assertion jsr223Assertion = parse2JSR233Assertion(variableAssertionItem);
|
JSR223Assertion jsr223Assertion = parse2JSR233Assertion(variableAssertionItem, config);
|
||||||
jsr223Assertion.setEnabled(variableAssertionItem.getEnable());
|
jsr223Assertion.setEnabled(variableAssertionItem.getEnable());
|
||||||
if (BooleanUtils.isFalse(globalEnable)) {
|
if (BooleanUtils.isFalse(globalEnable)) {
|
||||||
// 如果整体禁用,则禁用
|
// 如果整体禁用,则禁用
|
||||||
|
@ -45,7 +45,7 @@ public class VariableAssertionConverter extends AssertionConverter<MsVariableAss
|
||||||
return BooleanUtils.isTrue(variableAssertionItem.getEnable()) || config.getParseDisabledElement();
|
return BooleanUtils.isTrue(variableAssertionItem.getEnable()) || config.getParseDisabledElement();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static JSR223Assertion parse2JSR233Assertion(MsVariableAssertion.VariableAssertionItem variableAssertionItem) {
|
private static JSR223Assertion parse2JSR233Assertion(MsVariableAssertion.VariableAssertionItem variableAssertionItem, ParameterConfig config) {
|
||||||
ScriptProcessor scriptProcessor = new ScriptProcessor();
|
ScriptProcessor scriptProcessor = new ScriptProcessor();
|
||||||
scriptProcessor.setScript(parse2BeanshellJSR233Script(variableAssertionItem));
|
scriptProcessor.setScript(parse2BeanshellJSR233Script(variableAssertionItem));
|
||||||
|
|
||||||
|
@ -57,7 +57,7 @@ public class VariableAssertionConverter extends AssertionConverter<MsVariableAss
|
||||||
|
|
||||||
scriptProcessor.setScriptLanguage(ScriptLanguageType.GROOVY.name());
|
scriptProcessor.setScriptLanguage(ScriptLanguageType.GROOVY.name());
|
||||||
JSR223Assertion jsr223Assertion = new JSR223Assertion();
|
JSR223Assertion jsr223Assertion = new JSR223Assertion();
|
||||||
ScriptProcessorConverter.parse(jsr223Assertion, scriptProcessor);
|
ScriptProcessorConverter.parse(jsr223Assertion, scriptProcessor, config);
|
||||||
return jsr223Assertion;
|
return jsr223Assertion;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue