fix(接口测试): 修复node执行接口三方jar包加载失败问题

--bug=1019647 --user=赵勇 【接口测试】github#19700,接口场景加解密的jar包不使用资源池能正常使用,使用资源池报找不到jar包 https://www.tapd.cn/55049933/s/1296945
This commit is contained in:
fit2-zhao 2022-11-16 17:36:45 +08:00 committed by fit2-zhao
parent fe667a885d
commit 44a323a802
9 changed files with 25 additions and 8 deletions

View File

@ -231,7 +231,7 @@ public class ApiExecuteService {
runMode = ApiRunMode.API_PLAN.name();
}
// 加载自定义JAR
NewDriverManager.loadJar(request);
List<String> projectIds = NewDriverManager.loadJar(request);
HashTree hashTree = request.getTestElement().generateHashTree(config);
String jmx = request.getTestElement().getJmx(hashTree);
LoggerUtil.info("生成执行JMX内容【 " + jmx + "");
@ -246,6 +246,9 @@ public class ApiExecuteService {
this.put("userId", SessionUtils.getUser().getId());
this.put("userName", SessionUtils.getUser().getName());
}});
if (CollectionUtils.isNotEmpty(projectIds)) {
runRequest.getExtendedParameters().put(ExtendedParameter.PROJECT_ID, JSON.toJSONString(projectIds));
}
// 开始执行
if (StringUtils.isNotEmpty(request.getConfig().getResourcePoolId())) {
runRequest.setPool(GenerateHashTreeUtil.isResourcePool(request.getConfig().getResourcePoolId()));

View File

@ -4,7 +4,6 @@ import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.metersphere.api.dto.definition.request.MsTestPlan;
import io.metersphere.base.domain.TestResource;
import io.metersphere.commons.constants.ExtendedParameter;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.JSON;
import io.metersphere.commons.utils.LogUtil;
@ -60,7 +59,7 @@ public class KubernetesTestEngine extends AbstractEngine {
String path = "api/start";
if (runRequest.getHashTree() != null) {
path = "debug";
runRequest.getExtendedParameters().put(ExtendedParameter.JMX, new MsTestPlan().getJmx(runRequest.getHashTree()));
runRequest.setJmxScript(new MsTestPlan().getJmx(runRequest.getHashTree()));
runRequest.setHashTree(null);
LoggerUtil.info("进入DEBUG执行模式", runRequest.getReportId());
}

View File

@ -19,6 +19,7 @@ import io.metersphere.base.mapper.ext.ExtApiScenarioMapper;
import io.metersphere.base.mapper.plan.TestPlanApiScenarioMapper;
import io.metersphere.base.mapper.plan.ext.ExtTestPlanScenarioCaseMapper;
import io.metersphere.commons.constants.ApiRunMode;
import io.metersphere.commons.constants.ExtendedParameter;
import io.metersphere.commons.constants.ReportTriggerMode;
import io.metersphere.commons.enums.ApiReportStatus;
import io.metersphere.commons.exception.MSException;
@ -402,7 +403,7 @@ public class ApiScenarioExecuteService {
FileUtils.createBodyFiles(request.getScenarioFileIds(), scenarioFiles);
this.testElement(request);
// 加载自定义JAR
NewDriverManager.loadJar(request);
List<String> projectIds = NewDriverManager.loadJar(request);
HashTree hashTree = request.getTestElement().generateHashTree(config);
String runMode = StringUtils.isEmpty(request.getRunMode()) ? ApiRunMode.SCENARIO.name() : request.getRunMode();
JmeterRunRequestDTO runRequest = new JmeterRunRequestDTO(request.getId(), request.getId(), runMode, hashTree);
@ -417,6 +418,9 @@ public class ApiScenarioExecuteService {
BaseSystemConfigDTO baseInfo = systemParameterService.getBaseInfo();
runRequest.setPlatformUrl(GenerateHashTreeUtil.getPlatformUrl(baseInfo, runRequest, null));
}
if(CollectionUtils.isNotEmpty(projectIds)) {
runRequest.getExtendedParameters().put(ExtendedParameter.PROJECT_ID, JSON.toJSONString(projectIds));
}
jMeterService.run(runRequest);
return request.getId();
}

View File

@ -172,7 +172,7 @@ public class JMeterService {
ApiPoolDebugService apiPoolDebugService = CommonBeanFactory.getBean(ApiPoolDebugService.class);
if (apiPoolDebugService != null) {
List<TestResource> resources = GenerateHashTreeUtil.setPoolResource(request.getPoolId());
request.getExtendedParameters().put(ExtendedParameter.JMX, new MsTestPlan().getJmx(request.getHashTree()));
request.setJmxScript(new MsTestPlan().getJmx(request.getHashTree()));
request.setHashTree(null);
apiPoolDebugService.run(request, resources);
}

View File

@ -31,16 +31,19 @@ public class NewDriverManager {
return files.stream().map(FileMetadata::getPath).collect(Collectors.toList());
}
public static void loadJar(RunDefinitionRequest request) {
public static List<String> loadJar(RunDefinitionRequest request) {
// 加载自定义JAR
MsTestPlan testPlan = (MsTestPlan) request.getTestElement();
List<String> projectIds = new ArrayList<>();
projectIds.add(request.getProjectId());
if (MapUtils.isNotEmpty(request.getEnvironmentMap())) {
request.getEnvironmentMap().forEach((k, v) -> {
projectIds.add(k);
if (!projectIds.contains(k)) {
projectIds.add(k);
}
});
}
testPlan.setJarPaths(getJars(projectIds));
return projectIds;
}
}

View File

@ -1,7 +1,7 @@
package io.metersphere.commons.constants;
public class ExtendedParameter {
public static final String JMX = "JMX";
public static final String SYNC_STATUS = "SYN_RES";
public static final String SAVE_RESULT = "SAVE_RESULT";
public static final String PROJECT_ID = "projectId";
}

View File

@ -58,6 +58,12 @@ public class JmeterRunRequestDTO {
* 执行脚本
*/
private HashTree hashTree;
/**
* Node 执行脚本
*/
private String jmxScript;
/**
* 并发数
*/

View File

@ -31,6 +31,7 @@ public class CustomizeFunctionUtil {
JMeterContext context = JMeterContextService.getContext();
if (StringUtils.isNotEmpty(pathStr) && context != null) {
List<String> jarPaths = JsonUtils.parseObject(pathStr, List.class);
LoggerUtil.info(testPlan.getName() + "加载JAR:", jarPaths);
if (CollectionUtils.isNotEmpty(jarPaths)) {
// 初始化类加载器
GroovyClassLoader loader = MsClassLoader.getDynamic(jarPaths);

View File

@ -36,6 +36,7 @@ public class ShiroUtils {
filterChainDefinitionMap.put("/api/jmeter/download", "anon");
filterChainDefinitionMap.put("/api/jmeter/download/files", "anon");
filterChainDefinitionMap.put("/api/jmeter/download/jar", "anon");
filterChainDefinitionMap.put("/api/jmeter/download/jar/**", "anon");
filterChainDefinitionMap.put("/api/jmeter/download/plug/jar", "anon");
// for swagger