diff --git a/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/MsTestPlan.java b/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/MsTestPlan.java index f1d8b5f080..d87bc41d74 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/MsTestPlan.java +++ b/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/MsTestPlan.java @@ -4,11 +4,14 @@ import com.alibaba.excel.util.StringUtils; import io.metersphere.commons.constants.ApiTestConstants; import io.metersphere.commons.constants.ElementConstants; import io.metersphere.commons.utils.JSON; +import io.metersphere.dto.ProjectJarConfig; +import io.metersphere.enums.JmxFileMetadataColumns; import io.metersphere.plugin.core.MsParameter; import io.metersphere.plugin.core.MsTestElement; import lombok.Data; import lombok.EqualsAndHashCode; import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.collections.MapUtils; import org.apache.jmeter.config.Arguments; import org.apache.jmeter.save.SaveService; import org.apache.jmeter.testelement.TestElement; @@ -16,6 +19,7 @@ import org.apache.jmeter.testelement.TestPlan; import org.apache.jorphan.collections.HashTree; import java.util.List; +import java.util.Map; @Data @EqualsAndHashCode(callSuper = true) @@ -28,6 +32,9 @@ public class MsTestPlan extends MsTestElement { private boolean serializeThreadGroups = false; + // 资源池调用的时候需要的jar配置 + private Map> poolJarsMap; + @Override public void toHashTree(HashTree tree, List hashTree, MsParameter msParameter) { ParameterConfig config = (ParameterConfig) msParameter; @@ -50,6 +57,9 @@ public class MsTestPlan extends MsTestElement { if (CollectionUtils.isNotEmpty(projectJarIds)) { testPlan.setProperty(ApiTestConstants.JAR_PATH, JSON.toJSONString(projectJarIds)); } + if (MapUtils.isNotEmpty(poolJarsMap)) { + testPlan.setProperty(JmxFileMetadataColumns.JAR_PATH_CONFIG.name(), JSON.toJSONString(poolJarsMap)); + } testPlan.setUserDefinedVariables(new Arguments()); return testPlan; } diff --git a/api-test/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseSerialService.java b/api-test/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseSerialService.java index c7bdb96474..4dfc77404a 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseSerialService.java +++ b/api-test/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseSerialService.java @@ -28,6 +28,7 @@ import io.metersphere.commons.enums.ApiReportStatus; import io.metersphere.commons.utils.*; import io.metersphere.constants.RunModeConstants; import io.metersphere.dto.JmeterRunRequestDTO; +import io.metersphere.dto.ProjectJarConfig; import io.metersphere.dto.ResultDTO; import io.metersphere.environment.service.BaseEnvironmentService; import io.metersphere.plugin.core.MsTestElement; @@ -131,14 +132,13 @@ public class ApiCaseSerialService { if (caseWithBLOBs != null) { HashTree jmeterHashTree = new HashTree(); MsTestPlan testPlan = new MsTestPlan(); - - if (!runRequest.getPool().isPool()) { - // 获取自定义JAR - String projectId = caseWithBLOBs.getProjectId(); - testPlan.setProjectJarIds(NewDriverManager.getJars(new ArrayList<>() {{ - this.add(projectId); - }}, runRequest.getPool()).keySet().stream().toList()); - } + // 获取自定义JAR + String projectId = caseWithBLOBs.getProjectId(); + Map> jars = NewDriverManager.getJars(new ArrayList<>() {{ + this.add(projectId); + }}, runRequest.getPool()); + testPlan.setProjectJarIds(jars.keySet().stream().toList()); + testPlan.setPoolJarsMap(jars); testPlan.setHashTree(new LinkedList<>()); MsThreadGroup group = new MsThreadGroup(); group.setLabel(caseWithBLOBs.getName()); diff --git a/api-test/backend/src/main/java/io/metersphere/api/jmeter/NewDriverManager.java b/api-test/backend/src/main/java/io/metersphere/api/jmeter/NewDriverManager.java index 5200c35ffe..3fe4b20afb 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/jmeter/NewDriverManager.java +++ b/api-test/backend/src/main/java/io/metersphere/api/jmeter/NewDriverManager.java @@ -14,6 +14,7 @@ import io.metersphere.dto.ProjectJarConfig; import io.metersphere.jmeter.ProjectClassLoader; import io.metersphere.metadata.service.FileMetadataService; import io.metersphere.utils.JarConfigUtils; +import io.metersphere.utils.LocalPathUtil; import io.metersphere.vo.BooleanPool; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.MapUtils; @@ -70,7 +71,7 @@ public class NewDriverManager { byte[] bytes = new byte[0]; // 兼容历史数据 bytes = fileMetadataService.getContent(s.getId()); - ApiFileUtil.createFile(StringUtils.join(ApiFileUtil.LOCAL_JAR, + ApiFileUtil.createFile(StringUtils.join(LocalPathUtil.JAR_PATH, File.separator, key, File.separator, diff --git a/api-test/backend/src/main/java/io/metersphere/commons/config/UtilsConfig.java b/api-test/backend/src/main/java/io/metersphere/commons/config/UtilsConfig.java index 6f6b5bcce4..fe96913fa7 100644 --- a/api-test/backend/src/main/java/io/metersphere/commons/config/UtilsConfig.java +++ b/api-test/backend/src/main/java/io/metersphere/commons/config/UtilsConfig.java @@ -1,6 +1,5 @@ package io.metersphere.commons.config; -import io.metersphere.commons.utils.FileUtils; import io.metersphere.utils.LocalPathUtil; import io.metersphere.utils.TemporaryFileUtil; import org.springframework.context.annotation.Bean; @@ -8,13 +7,14 @@ import org.springframework.context.annotation.Configuration; @Configuration public class UtilsConfig { + + static { + LocalPathUtil.JAR_PATH += LocalPathUtil.MS; + LocalPathUtil.PLUGIN_PATH += LocalPathUtil.MS; + } + @Bean public TemporaryFileUtil temporaryFileUtil() { return new TemporaryFileUtil(TemporaryFileUtil.MS_FILE_FOLDER); } - - @Bean - public void localPathUtil() { - LocalPathUtil.prePath = FileUtils.LOCAL_JAR; - } } diff --git a/api-test/backend/src/main/java/io/metersphere/commons/utils/ApiFileUtil.java b/api-test/backend/src/main/java/io/metersphere/commons/utils/ApiFileUtil.java index 2626533c16..ada5354b63 100644 --- a/api-test/backend/src/main/java/io/metersphere/commons/utils/ApiFileUtil.java +++ b/api-test/backend/src/main/java/io/metersphere/commons/utils/ApiFileUtil.java @@ -10,6 +10,7 @@ import io.metersphere.enums.JmxFileMetadataColumns; import io.metersphere.metadata.service.FileManagerService; import io.metersphere.metadata.service.FileMetadataService; import io.metersphere.metadata.vo.FileRequest; +import io.metersphere.utils.LocalPathUtil; import io.metersphere.utils.LoggerUtil; import io.metersphere.utils.TemporaryFileUtil; import org.apache.commons.collections.CollectionUtils; @@ -36,7 +37,7 @@ public class ApiFileUtil extends FileUtils { infoDTOS.forEach(item -> { value.forEach(config -> { if (StringUtils.equals(item.getId(), config.getId())) { - createFile(StringUtils.join(ApiFileUtil.LOCAL_JAR, + createFile(StringUtils.join(LocalPathUtil.JAR_PATH, File.separator, key, File.separator, diff --git a/api-test/backend/src/main/java/io/metersphere/commons/utils/GenerateHashTreeUtil.java b/api-test/backend/src/main/java/io/metersphere/commons/utils/GenerateHashTreeUtil.java index 6c58afae78..3694123c93 100644 --- a/api-test/backend/src/main/java/io/metersphere/commons/utils/GenerateHashTreeUtil.java +++ b/api-test/backend/src/main/java/io/metersphere/commons/utils/GenerateHashTreeUtil.java @@ -13,10 +13,7 @@ import io.metersphere.base.mapper.TestResourcePoolMapper; import io.metersphere.commons.constants.ElementConstants; import io.metersphere.commons.constants.ResourcePoolTypeEnum; import io.metersphere.constants.RunModeConstants; -import io.metersphere.dto.BaseSystemConfigDTO; -import io.metersphere.dto.JmeterRunRequestDTO; -import io.metersphere.dto.ResultDTO; -import io.metersphere.dto.RunModeConfigDTO; +import io.metersphere.dto.*; import io.metersphere.environment.service.BaseEnvGroupProjectService; import io.metersphere.plugin.core.MsTestElement; import io.metersphere.service.ApiExecutionQueueService; @@ -112,18 +109,6 @@ public class GenerateHashTreeUtil { HashTree jmeterHashTree = new HashTree(); MsTestPlan testPlan = new MsTestPlan(); - if (!runRequest.getPool().isPool()) { - // 获取自定义JAR - String projectId = item.getProjectId(); - List projectIds = new ArrayList<>(); - projectIds.add(projectId); - if (MapUtils.isNotEmpty(planEnvMap)) { - planEnvMap.forEach((k, v) -> { - projectIds.add(k); - }); - } - testPlan.setProjectJarIds(NewDriverManager.getJars(projectIds, runRequest.getPool()).keySet().stream().toList()); - } testPlan.setHashTree(new LinkedList<>()); try { MsThreadGroup group = new MsThreadGroup(); @@ -139,6 +124,27 @@ public class GenerateHashTreeUtil { } else { setScenarioEnv(scenario, item); } + // 获取自定义JAR + String currentProjectId = item.getProjectId(); + List projectIds = new ArrayList<>(); + projectIds.add(currentProjectId); + if (MapUtils.isNotEmpty(planEnvMap)) { + planEnvMap.forEach((projectId, env) -> { + if (!projectIds.contains(projectId)) { + projectIds.add(projectId); + } + }); + } + if (MapUtils.isNotEmpty(scenario.getEnvironmentMap())) { + scenario.getEnvironmentMap().forEach((projectId, env) -> { + if (!projectIds.contains(projectId)) { + projectIds.add(projectId); + } + }); + } + Map> jarsMap = NewDriverManager.getJars(projectIds, runRequest.getPool()); + testPlan.setProjectJarIds(jarsMap.keySet().stream().toList()); + testPlan.setPoolJarsMap(jarsMap); String data = definition; // 失败重试 if (runRequest.isRetryEnable() && runRequest.getRetryNum() > 0) { diff --git a/api-test/backend/src/main/java/io/metersphere/controller/ApiJMeterFileController.java b/api-test/backend/src/main/java/io/metersphere/controller/ApiJMeterFileController.java index 8f06ee670d..f67439fb7f 100644 --- a/api-test/backend/src/main/java/io/metersphere/controller/ApiJMeterFileController.java +++ b/api-test/backend/src/main/java/io/metersphere/controller/ApiJMeterFileController.java @@ -31,15 +31,6 @@ public class ApiJMeterFileController { return JMeterThreadUtils.stop(name); } - @GetMapping("download/jar") - public ResponseEntity downloadJmeterFiles() { - byte[] bytes = apiJmeterFileService.downloadJmeterJar(); - return ResponseEntity.ok() - .contentType(MediaType.parseMediaType("application/octet-stream")) - .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + UUID.randomUUID().toString() + ".zip\"") - .body(bytes); - } - @PostMapping("download/jar") public ResponseEntity downloadJmeterFiles(@RequestBody Map> jarConfigs) { byte[] bytes = apiJmeterFileService.downloadJmeterJar(jarConfigs); diff --git a/api-test/backend/src/main/java/io/metersphere/service/ApiJMeterFileService.java b/api-test/backend/src/main/java/io/metersphere/service/ApiJMeterFileService.java index 3a707c6e2b..95064b6da7 100644 --- a/api-test/backend/src/main/java/io/metersphere/service/ApiJMeterFileService.java +++ b/api-test/backend/src/main/java/io/metersphere/service/ApiJMeterFileService.java @@ -21,6 +21,7 @@ import io.metersphere.environment.service.BaseEnvGroupProjectService; import io.metersphere.metadata.service.FileMetadataService; import io.metersphere.request.BodyFile; import io.metersphere.utils.LoggerUtil; +import io.metersphere.vo.BooleanPool; import jakarta.annotation.Resource; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.MapUtils; @@ -71,6 +72,9 @@ public class ApiJMeterFileService { JmeterRunRequestDTO runRequest = new JmeterRunRequestDTO(remoteTestId, reportId, runMode); runRequest.setReportType(reportType); runRequest.setQueueId(queueId); + BooleanPool booleanPool = new BooleanPool(); + booleanPool.setK8s(true); + runRequest.setPool(booleanPool); ApiScenarioWithBLOBs scenario = null; if (StringUtils.equalsAny(runMode, ApiRunMode.SCENARIO_PLAN.name(), ApiRunMode.JENKINS_SCENARIO_PLAN.name(), ApiRunMode.SCHEDULE_SCENARIO_PLAN.name())) { @@ -140,19 +144,6 @@ public class ApiJMeterFileService { return zipFilesToByteArray((reportId + "_" + remoteTestId), reportId, hashTree); } - public byte[] downloadJmeterJar() { - Map files = new HashMap<>(); - // 获取JAR - Map jarFiles = this.getJar(null); - if (!MapUtils.isEmpty(jarFiles)) { - for (String k : jarFiles.keySet()) { - byte[] v = jarFiles.get(k); - files.put(k, v); - } - } - return listBytesToZip(files); - } - public byte[] downloadJmeterJar(Map> map) { Map files = new HashMap<>(); if (MapUtils.isNotEmpty(map)) { @@ -180,7 +171,7 @@ public class ApiJMeterFileService { Map files = new HashMap<>(); if (CollectionUtils.isNotEmpty(pluginIds)) { // 获取JAR - Map jarFiles = this.getPlugJar(pluginIds); + Map jarFiles = this.getPluginJar(pluginIds); if (MapUtils.isNotEmpty(jarFiles)) { for (String k : jarFiles.keySet()) { byte[] v = jarFiles.get(k); @@ -191,30 +182,7 @@ public class ApiJMeterFileService { return listBytesToZip(files); } - private Map getJar(String projectId) { - Map jarFiles = new LinkedHashMap<>(); - FileMetadataService jarConfigService = CommonBeanFactory.getBean(FileMetadataService.class); - if (jarConfigService != null) { - List files = jarConfigService.getJar(new ArrayList<>() {{ - this.add(projectId); - }}); - files.forEach(path -> { - File file = new File(path); - if (file.isDirectory() && !path.endsWith("/")) { - file = new File(path + "/"); - } - byte[] fileByte = FileUtils.fileToByte(file); - if (fileByte != null) { - jarFiles.put(file.getName(), fileByte); - } - }); - return jarFiles; - } else { - return new HashMap<>(); - } - } - - public Map getPlugJar(List pluginIds) { + public Map getPluginJar(List pluginIds) { Map jarFiles = new LinkedHashMap<>(); PluginExample example = new PluginExample(); example.createCriteria().andPluginIdIn(pluginIds).andScenarioNotEqualTo(PluginScenario.platform.name()); diff --git a/framework/sdk-parent/jmeter/src/main/java/io/metersphere/enums/JmxFileMetadataColumns.java b/framework/sdk-parent/jmeter/src/main/java/io/metersphere/enums/JmxFileMetadataColumns.java index 485848a7a4..5b6f0e6080 100644 --- a/framework/sdk-parent/jmeter/src/main/java/io/metersphere/enums/JmxFileMetadataColumns.java +++ b/framework/sdk-parent/jmeter/src/main/java/io/metersphere/enums/JmxFileMetadataColumns.java @@ -5,5 +5,6 @@ public enum JmxFileMetadataColumns { REF_FILE_UPDATE_TIME, REF_FILE_PROJECT_ID, REF_FILE_ATTACH_INFO, - REF_FILE_NAME + REF_FILE_NAME, + JAR_PATH_CONFIG, } diff --git a/framework/sdk-parent/jmeter/src/main/java/io/metersphere/jmeter/ProjectClassLoader.java b/framework/sdk-parent/jmeter/src/main/java/io/metersphere/jmeter/ProjectClassLoader.java index 8335186983..e2a19d472f 100644 --- a/framework/sdk-parent/jmeter/src/main/java/io/metersphere/jmeter/ProjectClassLoader.java +++ b/framework/sdk-parent/jmeter/src/main/java/io/metersphere/jmeter/ProjectClassLoader.java @@ -20,7 +20,7 @@ public class ProjectClassLoader { public static void initClassLoader(List projectIds) { // 读取所有JAR路径 for (String projectId : projectIds) { - List jarPaths = JarConfigUtils.walk(LocalPathUtil.prePath + File.separator + projectId); + List jarPaths = JarConfigUtils.walk(LocalPathUtil.JAR_PATH + File.separator + projectId); if (CollectionUtils.isNotEmpty(jarPaths)) { LoggerUtil.info("加载JAR-PATH:" + JsonUtils.toJSONString(jarPaths), projectId); // 初始化类加载器 @@ -48,7 +48,7 @@ public class ProjectClassLoader { public static void initClassLoader() { // 读取所有JAR路径 - List projectIds = JarConfigUtils.getFileNames(LocalPathUtil.prePath + File.separator); + List projectIds = JarConfigUtils.getFileNames(LocalPathUtil.JAR_PATH + File.separator); LoggerUtil.info("初始化所有JAR:" + JsonUtils.toJSONString(projectIds)); if (CollectionUtils.isNotEmpty(projectIds)) { initClassLoader(projectIds); diff --git a/framework/sdk-parent/jmeter/src/main/java/io/metersphere/utils/CustomizeFunctionUtil.java b/framework/sdk-parent/jmeter/src/main/java/io/metersphere/utils/CustomizeFunctionUtil.java index 18a2afa71b..439ed36e3e 100644 --- a/framework/sdk-parent/jmeter/src/main/java/io/metersphere/utils/CustomizeFunctionUtil.java +++ b/framework/sdk-parent/jmeter/src/main/java/io/metersphere/utils/CustomizeFunctionUtil.java @@ -31,7 +31,7 @@ public class CustomizeFunctionUtil { if (StringUtils.isNotEmpty(pathStr) && context != null) { List projectIds = JsonUtils.parseObject(pathStr, List.class); LoggerUtil.info("加载JAR-PROJECT-ID:" + projectIds, testPlan.getName()); - LoggerUtil.info("PRE-PATH:" + LocalPathUtil.prePath, testPlan.getName()); + LoggerUtil.info("PRE-PATH:" + LocalPathUtil.JAR_PATH, testPlan.getName()); if (CollectionUtils.isNotEmpty(projectIds)) { // 读取所有JAR路径 List jarPaths = JarConfigUtils.findPathByProjectIds(projectIds); diff --git a/framework/sdk-parent/jmeter/src/main/java/io/metersphere/utils/JarConfigUtils.java b/framework/sdk-parent/jmeter/src/main/java/io/metersphere/utils/JarConfigUtils.java index 830525a749..a10894d17d 100644 --- a/framework/sdk-parent/jmeter/src/main/java/io/metersphere/utils/JarConfigUtils.java +++ b/framework/sdk-parent/jmeter/src/main/java/io/metersphere/utils/JarConfigUtils.java @@ -20,7 +20,7 @@ import java.util.stream.Stream; public class JarConfigUtils { public static Map> getJarConfigs(List projectIds, Map> jarConfigMap) { - String localPath = LocalPathUtil.prePath; + String localPath = LocalPathUtil.JAR_PATH; Map> jarConfigsMap = new HashMap<>(); projectIds.forEach(item -> { List jarConfigs = new ArrayList<>(); @@ -109,7 +109,7 @@ public class JarConfigUtils { List jarPaths = new ArrayList<>(); if (CollectionUtils.isNotEmpty(projectIds)) { projectIds.forEach(item -> { - jarPaths.addAll(walk(LocalPathUtil.prePath + File.separator + item)); + jarPaths.addAll(walk(LocalPathUtil.JAR_PATH + File.separator + item)); }); } return jarPaths; diff --git a/framework/sdk-parent/jmeter/src/main/java/io/metersphere/utils/LocalPathUtil.java b/framework/sdk-parent/jmeter/src/main/java/io/metersphere/utils/LocalPathUtil.java index 5921a66ee5..99fc8a9766 100644 --- a/framework/sdk-parent/jmeter/src/main/java/io/metersphere/utils/LocalPathUtil.java +++ b/framework/sdk-parent/jmeter/src/main/java/io/metersphere/utils/LocalPathUtil.java @@ -1,5 +1,20 @@ package io.metersphere.utils; +import org.apache.commons.lang3.StringUtils; + +import java.io.File; + public class LocalPathUtil { - public static String prePath; + public static final String NODE = "node"; + public static final String MS = "ms"; + public static final String PRE_PATH = File.separator + "opt" + + File.separator + "metersphere" + + File.separator + "data" + + File.separator + "api-folder" + + File.separator; + + public static String JAR_PATH = + StringUtils.join(PRE_PATH, "jar", File.separator); + + public static String PLUGIN_PATH = StringUtils.join(PRE_PATH, "plugin", File.separator); } diff --git a/framework/sdk-parent/sdk/src/main/java/io/metersphere/commons/utils/FileUtils.java b/framework/sdk-parent/sdk/src/main/java/io/metersphere/commons/utils/FileUtils.java index 1ee66105b4..1dd32da6c6 100644 --- a/framework/sdk-parent/sdk/src/main/java/io/metersphere/commons/utils/FileUtils.java +++ b/framework/sdk-parent/sdk/src/main/java/io/metersphere/commons/utils/FileUtils.java @@ -33,7 +33,6 @@ public class FileUtils { public static final String UI_IMAGE_DIR = "/opt/metersphere/data/image/ui/screenshots"; public static final String ATTACHMENT_DIR = "/opt/metersphere/data/attachment"; public static final String ATTACHMENT_TMP_DIR = "/opt/metersphere/data/attachment/tmp"; - public static final String LOCAL_JAR = "/opt/metersphere/data/local-jar/jar"; public static void validateFileName(String fileName) { if (StringUtils.isNotEmpty(fileName) && StringUtils.contains(fileName, "." + File.separator)) {