fix(接口测试): 环境引用公共脚本,执行时没有获取最新的脚本内容

--bug=1045655 --user=陈建星 [项目管理]github #32810环境的前置里引入了全局脚本,全局脚本变更后,环境里的未变更 https://www.tapd.cn/55049933/s/1596334
This commit is contained in:
AgAngle 2024-10-23 16:17:27 +08:00 committed by Craftsman
parent b08abe6691
commit be4870155e
3 changed files with 98 additions and 1 deletions

View File

@ -139,7 +139,7 @@ public class MsCommonElementConverter extends AbstractJmeterElementConverter<MsC
if (BooleanUtils.isTrue(processorConfig.getEnableGlobal()) && envInfo != null) {
EnvironmentConfig envConfig = envInfo.getConfig();
EnvProcessorConfig envProcessorConfig = isPre ? envConfig.getPreProcessorConfig() : envConfig.getPostProcessorConfig();
addEnvProcessors(envProcessorConfig, beforeStepProcessors, afterStepProcessors, protocol, true);
addEnvProcessors(envProcessorConfig, beforeStepProcessors, afterStepProcessors, protocol, isPre);
}
Function<Class<?>, MsProcessorConverter<MsProcessor>> getConverterFunc =

View File

@ -22,6 +22,9 @@ import io.metersphere.project.domain.CustomFunctionBlob;
import io.metersphere.project.domain.FileAssociation;
import io.metersphere.project.domain.FileMetadata;
import io.metersphere.project.dto.CommonScriptInfo;
import io.metersphere.project.dto.environment.EnvironmentInfoDTO;
import io.metersphere.project.dto.environment.MsEnvAssertionConfig;
import io.metersphere.project.dto.environment.processors.*;
import io.metersphere.project.service.CustomFunctionService;
import io.metersphere.project.service.FileAssociationService;
import io.metersphere.project.service.FileMetadataService;
@ -29,6 +32,7 @@ import io.metersphere.sdk.constants.ApplicationNumScope;
import io.metersphere.sdk.constants.ExecStatus;
import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.sdk.util.JSON;
import io.metersphere.sdk.util.LogUtils;
import io.metersphere.system.domain.ExecTask;
import io.metersphere.system.domain.ExecTaskItem;
import io.metersphere.system.uid.IDGenerator;
@ -254,6 +258,87 @@ public class ApiCommonService {
setEnableCommonScriptInfo(commonScriptInfos);
}
/**
* 设置环境前后置的公共脚本信息
*
* @param envConfig
*/
public void setEnvCommonScriptInfo(EnvironmentInfoDTO envConfig) {
if (envConfig == null || envConfig.getConfig() == null) {
return;
}
try {
// 获取脚本
List<ScriptProcessor> scriptsProcessors = getEnableCommonScriptProcessors(envConfig.getConfig().getPreProcessorConfig());
scriptsProcessors.addAll(getEnableCommonScriptProcessors(envConfig.getConfig().getPostProcessorConfig()));
// 获取断言
List<MsScriptAssertion> scriptAssertions = getEnableCommonScriptAssertion(envConfig.getConfig().getAssertionConfig());
List<CommonScriptInfo> commonScriptInfos = scriptsProcessors.stream()
.map(ScriptProcessor::getCommonScriptInfo)
.collect(Collectors.toList());
List<CommonScriptInfo> assertionsCommonScriptInfos = scriptAssertions.stream()
.map(MsScriptAssertion::getCommonScriptInfo)
.toList();
commonScriptInfos.addAll(assertionsCommonScriptInfos);
// 设置最新的公共脚本信息
setEnableCommonScriptInfo(commonScriptInfos);
} catch (Exception e) {
LogUtils.error(e);
}
}
/**
* 获取环境使用公共脚本的前后置
*
* @param envProcessorConfig
* @return
*/
private List<ScriptProcessor> getEnableCommonScriptProcessors(EnvProcessorConfig envProcessorConfig) {
if (envProcessorConfig == null) {
return new ArrayList<>(0);
}
ApiEnvProcessorConfig apiProcessorConfig = envProcessorConfig.getApiProcessorConfig();
ApiEnvPlanProcessorConfig planProcessorConfig = apiProcessorConfig.getPlanProcessorConfig();
ApiEnvScenarioProcessorConfig scenarioProcessorConfig = apiProcessorConfig.getScenarioProcessorConfig();
ApiEnvRequestProcessorConfig requestProcessorConfig = apiProcessorConfig.getRequestProcessorConfig();
List<MsProcessor> processors = new ArrayList<>();
processors.addAll(planProcessorConfig.getProcessors());
processors.addAll(scenarioProcessorConfig.getProcessors());
processors.addAll(requestProcessorConfig.getProcessors());
// 获取使用公共脚本的前后置
return processors.stream()
.filter(processor -> processor instanceof ScriptProcessor)
.map(processor -> (ScriptProcessor) processor)
.filter(ScriptProcessor::isEnableCommonScript)
.filter(ScriptProcessor::isValid)
.collect(Collectors.toList());
}
/**
* 获取使用公共脚本的前后置
*
* @param envAssertionConfig
* @return
*/
private List<MsScriptAssertion> getEnableCommonScriptAssertion(MsEnvAssertionConfig envAssertionConfig) {
if (envAssertionConfig == null || CollectionUtils.isEmpty(envAssertionConfig.getAssertions())) {
return List.of();
}
return envAssertionConfig.getAssertions()
.stream()
.filter(assertion -> assertion instanceof MsScriptAssertion)
.map(msAssertion -> (MsScriptAssertion) msAssertion)
.filter(MsScriptAssertion::isEnableCommonScript)
.filter(MsScriptAssertion::isValid)
.toList();
}
private void setEnableCommonScriptInfo(List<CommonScriptInfo> commonScriptInfos) {
List<String> commonScriptIds = commonScriptInfos.stream()
.map(CommonScriptInfo::getId)

View File

@ -4,6 +4,7 @@ import io.metersphere.api.config.JmeterProperties;
import io.metersphere.api.config.KafkaConfig;
import io.metersphere.api.controller.result.ApiResultCode;
import io.metersphere.api.dto.ApiParamConfig;
import io.metersphere.api.dto.ApiScenarioParamConfig;
import io.metersphere.api.dto.debug.ApiDebugRunRequest;
import io.metersphere.api.dto.debug.ApiResourceRunRequest;
import io.metersphere.api.dto.request.controller.MsScriptElement;
@ -18,6 +19,7 @@ import io.metersphere.project.domain.FileMetadata;
import io.metersphere.project.domain.ProjectApplication;
import io.metersphere.project.dto.CommonScriptInfo;
import io.metersphere.project.dto.customfunction.request.CustomFunctionRunRequest;
import io.metersphere.project.dto.environment.EnvironmentInfoDTO;
import io.metersphere.project.dto.environment.GlobalParams;
import io.metersphere.project.dto.environment.GlobalParamsDTO;
import io.metersphere.project.service.*;
@ -648,6 +650,16 @@ public class ApiExecuteService {
public String parseExecuteScript(AbstractMsTestElement msTestElement, ParameterConfig config) {
// 解析生成脚本
TestElementParser defaultParser = TestElementParserFactory.getDefaultParser();
// 环境获取最新的公共脚本信息
if (config instanceof ApiScenarioParamConfig apiScenarioParamConfig) {
Map<String, EnvironmentInfoDTO> projectEnvMap = apiScenarioParamConfig.getProjectEnvMap();
if (projectEnvMap != null) {
projectEnvMap.values().forEach(apiCommonService::setEnvCommonScriptInfo);
}
apiCommonService.setEnvCommonScriptInfo(apiScenarioParamConfig.getEnvConfig());
} else if (config instanceof ApiParamConfig apiParamConfig) {
apiCommonService.setEnvCommonScriptInfo(apiParamConfig.getEnvConfig());
}
return defaultParser.parse(msTestElement, config);
}