refactor(接口测试): 优化本地执行时附件的处理逻辑,不再进行本地附件拷贝操作

优化本地执行时附件的处理逻辑,不再进行本地附件拷贝操作
This commit is contained in:
song-tianyang 2023-02-09 18:40:03 +08:00 committed by fit2-zhao
parent 9aa0c08f22
commit 420f0c9128
5 changed files with 48 additions and 52 deletions

View File

@ -162,7 +162,7 @@ public class JMeterService {
if (request.getHashTree() != null) { if (request.getHashTree() != null) {
ElementUtil.coverArguments(request.getHashTree()); ElementUtil.coverArguments(request.getHashTree());
//解析HashTree里的文件信息 //解析HashTree里的文件信息
List<AttachmentBodyFile> attachmentBodyFileList = ApiFileUtil.getExecuteFileForNode(request.getHashTree(), request.getReportId()); List<AttachmentBodyFile> attachmentBodyFileList = ApiFileUtil.getExecuteFile(request.getHashTree(), request.getReportId(), false);
if (CollectionUtils.isNotEmpty(attachmentBodyFileList)) { if (CollectionUtils.isNotEmpty(attachmentBodyFileList)) {
redisTemplateService.setIfAbsent(JmxFileUtil.getExecuteFileKeyInRedis(request.getReportId()), JmxFileUtil.getRedisJmxFileString(attachmentBodyFileList)); redisTemplateService.setIfAbsent(JmxFileUtil.getExecuteFileKeyInRedis(request.getReportId()), JmxFileUtil.getRedisJmxFileString(attachmentBodyFileList));
} }
@ -197,7 +197,7 @@ public class JMeterService {
//解析HashTree里的文件信息 //解析HashTree里的文件信息
List<AttachmentBodyFile> attachmentBodyFileList = ApiFileUtil.getExecuteFileForNode(request.getHashTree(), request.getReportId()); List<AttachmentBodyFile> attachmentBodyFileList = ApiFileUtil.getExecuteFile(request.getHashTree(), request.getReportId(), false);
if (CollectionUtils.isNotEmpty(attachmentBodyFileList)) { if (CollectionUtils.isNotEmpty(attachmentBodyFileList)) {
redisTemplateService.setIfAbsent(JmxFileUtil.getExecuteFileKeyInRedis(request.getReportId()), JmxFileUtil.getRedisJmxFileString(attachmentBodyFileList)); redisTemplateService.setIfAbsent(JmxFileUtil.getExecuteFileKeyInRedis(request.getReportId()), JmxFileUtil.getRedisJmxFileString(attachmentBodyFileList));
} }

View File

@ -16,7 +16,6 @@ import io.metersphere.service.RedisTemplateService;
import io.metersphere.service.TestResultService; import io.metersphere.service.TestResultService;
import io.metersphere.utils.LoggerUtil; import io.metersphere.utils.LoggerUtil;
import io.metersphere.utils.RetryResultUtil; import io.metersphere.utils.RetryResultUtil;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.samplers.SampleResult; import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.services.FileServer; import org.apache.jmeter.services.FileServer;
@ -37,7 +36,6 @@ public class MsApiBackendListener extends AbstractBackendListenerClient implemen
// 当前场景报告/用例结果状态 // 当前场景报告/用例结果状态
private ResultVO resultVO; private ResultVO resultVO;
@Resource
private RedisTemplateService redisTemplateService; private RedisTemplateService redisTemplateService;
/** /**
@ -54,6 +52,9 @@ public class MsApiBackendListener extends AbstractBackendListenerClient implemen
if (testResultService == null) { if (testResultService == null) {
testResultService = CommonBeanFactory.getBean(TestResultService.class); testResultService = CommonBeanFactory.getBean(TestResultService.class);
} }
if (redisTemplateService == null) {
redisTemplateService = CommonBeanFactory.getBean(RedisTemplateService.class);
}
resultVO = new ResultVO(); resultVO = new ResultVO();
super.setupTest(context); super.setupTest(context);
} }

View File

@ -12,6 +12,7 @@ import io.metersphere.metadata.service.FileMetadataService;
import io.metersphere.metadata.vo.FileRequest; import io.metersphere.metadata.vo.FileRequest;
import io.metersphere.request.BodyFile; import io.metersphere.request.BodyFile;
import io.metersphere.utils.LoggerUtil; import io.metersphere.utils.LoggerUtil;
import io.metersphere.utils.TemporaryFileUtil;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -29,6 +30,7 @@ import java.util.List;
public class ApiFileUtil extends FileUtils { public class ApiFileUtil extends FileUtils {
private static FileManagerService fileManagerService; private static FileManagerService fileManagerService;
private static FileMetadataService fileMetadataService; private static FileMetadataService fileMetadataService;
private static TemporaryFileUtil temporaryFileUtil;
public static String getFilePath(BodyFile file) { public static String getFilePath(BodyFile file) {
String type = StringUtils.isNotEmpty(file.getFileType()) ? file.getFileType().toLowerCase() : null; String type = StringUtils.isNotEmpty(file.getFileType()) ? file.getFileType().toLowerCase() : null;
@ -111,13 +113,16 @@ public class ApiFileUtil extends FileUtils {
} }
} }
public static List<AttachmentBodyFile> getExecuteFileForNode(HashTree tree, String reportId) { public static List<AttachmentBodyFile> getExecuteFile(HashTree tree, String reportId, boolean isLocal) {
if (temporaryFileUtil == null) {
temporaryFileUtil = CommonBeanFactory.getBean(TemporaryFileUtil.class);
}
List<AttachmentBodyFile> fileList = new ArrayList<>(); List<AttachmentBodyFile> fileList = new ArrayList<>();
formatFilePathForNode(tree, reportId, fileList); formatFilePathForNode(tree, reportId, isLocal, fileList);
return fileList; return fileList;
} }
private static void formatFilePathForNode(HashTree tree, String reportId, List<AttachmentBodyFile> fileList) { private static void formatFilePathForNode(HashTree tree, String reportId, boolean isLocal, List<AttachmentBodyFile> fileList) {
if (tree != null) { if (tree != null) {
if (fileMetadataService == null) { if (fileMetadataService == null) {
fileMetadataService = CommonBeanFactory.getBean(FileMetadataService.class); fileMetadataService = CommonBeanFactory.getBean(FileMetadataService.class);
@ -129,35 +134,35 @@ public class ApiFileUtil extends FileUtils {
} }
HashTree node = tree.get(key); HashTree node = tree.get(key);
if (key instanceof HTTPSamplerProxy) { if (key instanceof HTTPSamplerProxy) {
getAttachmentBodyFileByHttp(key, reportId, fileList); getAttachmentBodyFileByHttp(key, reportId, isLocal, fileList);
} else if (key instanceof CSVDataSet) { } else if (key instanceof CSVDataSet) {
getAttachmentBodyFileByCsv(key, reportId, fileList); getAttachmentBodyFileByCsv(key, reportId, isLocal, fileList);
} }
if (node != null) { if (node != null) {
formatFilePathForNode(node, reportId, fileList); formatFilePathForNode(node, reportId, isLocal, fileList);
} }
} }
} }
} }
public static void getAttachmentBodyFileByCsv(Object tree, String reportId, List<AttachmentBodyFile> bodyFileList) { public static void getAttachmentBodyFileByCsv(Object tree, String reportId, boolean isLocal, List<AttachmentBodyFile> bodyFileList) {
CSVDataSet source = (CSVDataSet) tree; CSVDataSet source = (CSVDataSet) tree;
if (StringUtils.isNotEmpty(source.getPropertyAsString(ElementConstants.FILENAME))) { if (StringUtils.isNotEmpty(source.getPropertyAsString(ElementConstants.FILENAME))) {
getAttachmentFileByTestElement(source, reportId, bodyFileList); getAttachmentFileByTestElement(source, reportId, isLocal, bodyFileList);
} }
} }
public static void getAttachmentBodyFileByHttp(Object testElement, String reportId, List<AttachmentBodyFile> fileList) { public static void getAttachmentBodyFileByHttp(Object testElement, String reportId, boolean isLocal, List<AttachmentBodyFile> fileList) {
if (testElement == null) { if (testElement == null) {
return; return;
} }
HTTPSamplerProxy source = (HTTPSamplerProxy) testElement; HTTPSamplerProxy source = (HTTPSamplerProxy) testElement;
for (HTTPFileArg httpFileArg : source.getHTTPFiles()) { for (HTTPFileArg httpFileArg : source.getHTTPFiles()) {
getAttachmentFileByTestElement(httpFileArg, reportId, fileList); getAttachmentFileByTestElement(httpFileArg, reportId, isLocal, fileList);
} }
} }
private static void getAttachmentFileByTestElement(TestElement testElement, String reportId, List<AttachmentBodyFile> bodyFileList) { private static void getAttachmentFileByTestElement(TestElement testElement, String reportId, boolean isLocal, List<AttachmentBodyFile> bodyFileList) {
if (testElement == null) { if (testElement == null) {
return; return;
} }
@ -186,7 +191,7 @@ public class ApiFileUtil extends FileUtils {
} }
bodyFileList.add(attachmentBodyFile); bodyFileList.add(attachmentBodyFile);
testElement.setProperty(ElementConstants.FILENAME, path); if (!isLocal) {
testElement.setProperty(JmxFileMetadataColumns.REF_FILE_STORAGE.name(), fileMetadata.getStorage()); testElement.setProperty(JmxFileMetadataColumns.REF_FILE_STORAGE.name(), fileMetadata.getStorage());
testElement.setProperty(JmxFileMetadataColumns.REF_FILE_NAME.name(), fileMetadata.getName()); testElement.setProperty(JmxFileMetadataColumns.REF_FILE_NAME.name(), fileMetadata.getName());
testElement.setProperty(JmxFileMetadataColumns.REF_FILE_UPDATE_TIME.name(), fileMetadata.getUpdateTime()); testElement.setProperty(JmxFileMetadataColumns.REF_FILE_UPDATE_TIME.name(), fileMetadata.getUpdateTime());
@ -194,7 +199,11 @@ public class ApiFileUtil extends FileUtils {
if (StringUtils.isNotBlank(fileMetadata.getAttachInfo())) { if (StringUtils.isNotBlank(fileMetadata.getAttachInfo())) {
testElement.setProperty(JmxFileMetadataColumns.REF_FILE_ATTACH_INFO.name(), fileMetadata.getAttachInfo()); testElement.setProperty(JmxFileMetadataColumns.REF_FILE_ATTACH_INFO.name(), fileMetadata.getAttachInfo());
} }
} else {
path = temporaryFileUtil.generateFilePath(attachmentBodyFile.getProjectId(), attachmentBodyFile.getFileUpdateTime(), attachmentBodyFile.getName());
}
testElement.setProperty(ElementConstants.FILENAME, path);
if (testElement instanceof HTTPFileArg) { if (testElement instanceof HTTPFileArg) {
((HTTPFileArg) testElement).setPath(path); ((HTTPFileArg) testElement).setPath(path);
} }

View File

@ -10,6 +10,7 @@ import io.metersphere.base.domain.ApiTestEnvironmentWithBLOBs;
import io.metersphere.base.domain.FileMetadata; import io.metersphere.base.domain.FileMetadata;
import io.metersphere.commons.constants.ElementConstants; import io.metersphere.commons.constants.ElementConstants;
import io.metersphere.commons.constants.StorageConstants; import io.metersphere.commons.constants.StorageConstants;
import io.metersphere.dto.AttachmentBodyFile;
import io.metersphere.dto.FileInfoDTO; import io.metersphere.dto.FileInfoDTO;
import io.metersphere.dto.JmeterRunRequestDTO; import io.metersphere.dto.JmeterRunRequestDTO;
import io.metersphere.environment.service.BaseEnvironmentService; import io.metersphere.environment.service.BaseEnvironmentService;
@ -27,7 +28,10 @@ import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import java.io.File; import java.io.File;
import java.util.*; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/** /**
* @author song.tianyang * @author song.tianyang
@ -238,36 +242,18 @@ public class HashTreeUtil {
if (runRequest.getPool().isPool() || runRequest.getPool().isK8s()) { if (runRequest.getPool().isPool() || runRequest.getPool().isK8s()) {
return; return;
} }
List<BodyFile> files = new LinkedList<>(); List<AttachmentBodyFile> downloadFileList = ApiFileUtil.getExecuteFile(runRequest.getHashTree(), runRequest.getReportId(), true);
ApiFileUtil.getExecuteFiles(runRequest.getHashTree(), runRequest.getReportId(), files);
if (CollectionUtils.isNotEmpty(files)) {
Map<String, String> repositoryFileMap = new HashMap<>(); Map<String, String> repositoryFileMap = new HashMap<>();
for (BodyFile bodyFile : files) { for (AttachmentBodyFile bodyFile : downloadFileList) {
if (!StringUtils.equals(bodyFile.getStorage(), StorageConstants.LOCAL.name()) if (!StringUtils.equals(bodyFile.getFileStorage(), StorageConstants.LOCAL.name())
&& StringUtils.isNotBlank(bodyFile.getFileId())) { && StringUtils.isNotBlank(bodyFile.getFileMetadataId())) {
repositoryFileMap.put(bodyFile.getFileId(), bodyFile.getName()); repositoryFileMap.put(bodyFile.getFileMetadataId(), bodyFile.getName());
} }
} }
FileMetadataService fileMetadataService = CommonBeanFactory.getBean(FileMetadataService.class); FileMetadataService fileMetadataService = CommonBeanFactory.getBean(FileMetadataService.class);
if (fileMetadataService != null) { if (fileMetadataService != null) {
Map<String, byte[]> multipartFiles = new LinkedHashMap<>(); fileMetadataService.downloadApiExecuteFilesByIds(repositoryFileMap.keySet());
List<FileInfoDTO> fileInfoDTOList = fileMetadataService.downloadApiExecuteFilesByIds(repositoryFileMap.keySet());
fileInfoDTOList.forEach(dto -> {
if (ArrayUtils.isNotEmpty(dto.getFileByte())) {
String key = StringUtils.join(
FileUtils.BODY_FILE_DIR,
File.separator,
repositoryFileMap.get(dto.getId()));
multipartFiles.put(key, dto.getFileByte());
}
});
for (Map.Entry<String, byte[]> fileEntry : multipartFiles.entrySet()) {
String fileName = fileEntry.getKey();
byte[] fileBytes = fileEntry.getValue();
FileUtils.createFile(fileName, fileBytes);
}
}
} }
} }

View File

@ -248,7 +248,7 @@ public class ApiJMeterFileService {
Map<String, byte[]> multipartFiles = this.getMultipartFiles(testId, hashTree); Map<String, byte[]> multipartFiles = this.getMultipartFiles(testId, hashTree);
转为解析jmx中附件节点赋予相关信息(例如文件关联类型路径更新时间等),并将文件信息存储在redis中为了进行连接ms下载时的安全校验 转为解析jmx中附件节点赋予相关信息(例如文件关联类型路径更新时间等),并将文件信息存储在redis中为了进行连接ms下载时的安全校验
*/ */
List<AttachmentBodyFile> attachmentBodyFileList = ApiFileUtil.getExecuteFileForNode(hashTree, reportId); List<AttachmentBodyFile> attachmentBodyFileList = ApiFileUtil.getExecuteFile(hashTree, reportId, false);
if (CollectionUtils.isNotEmpty(attachmentBodyFileList)) { if (CollectionUtils.isNotEmpty(attachmentBodyFileList)) {
redisTemplateService.setIfAbsent(JmxFileUtil.getExecuteFileKeyInRedis(reportId), JmxFileUtil.getRedisJmxFileString(attachmentBodyFileList)); redisTemplateService.setIfAbsent(JmxFileUtil.getExecuteFileKeyInRedis(reportId), JmxFileUtil.getRedisJmxFileString(attachmentBodyFileList));
} }