fix(接口测试): 修复资源池执行时从三方仓库获取不到文件问题

--bug=1021667 --user=赵勇 【接口测试】场景关联系统中的csv文件,使用local资源池执行,执行失败 https://www.tapd.cn/55049933/s/1324369
This commit is contained in:
fit2-zhao 2023-01-09 15:24:42 +08:00 committed by fit2-zhao
parent 493f678643
commit 5e2486ddad
4 changed files with 68 additions and 29 deletions

View File

@ -8,6 +8,7 @@ import io.metersphere.metadata.service.FileManagerService;
import io.metersphere.metadata.service.FileMetadataService;
import io.metersphere.metadata.vo.FileRequest;
import io.metersphere.request.BodyFile;
import io.metersphere.utils.LoggerUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
@ -90,6 +91,7 @@ public class ApiFileUtil extends FileUtils {
request.setProjectId(path);
request.setFileName(requestId);
request.setStorage(StorageConstants.MINIO.name());
LoggerUtil.info("开始从MinIO处理文件", path);
return request;
}

View File

@ -7,6 +7,7 @@ import io.metersphere.api.dto.definition.request.assertions.MsAssertionRegex;
import io.metersphere.api.dto.definition.request.assertions.MsAssertions;
import io.metersphere.api.dto.scenario.environment.EnvironmentConfig;
import io.metersphere.base.domain.ApiTestEnvironmentWithBLOBs;
import io.metersphere.base.domain.FileMetadata;
import io.metersphere.commons.constants.ElementConstants;
import io.metersphere.commons.constants.StorageConstants;
import io.metersphere.dto.FileInfoDTO;
@ -16,6 +17,7 @@ import io.metersphere.metadata.service.FileMetadataService;
import io.metersphere.request.BodyFile;
import io.metersphere.service.ExtErrorReportLibraryService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.extractor.JSR223PostProcessor;
import org.apache.jmeter.modifiers.JSR223PreProcessor;
@ -250,11 +252,16 @@ public class HashTreeUtil {
if (fileMetadataService != null) {
Map<String, byte[]> multipartFiles = new LinkedHashMap<>();
List<FileInfoDTO> fileInfoDTOList = fileMetadataService.downloadFileByIds(repositoryFileMap.keySet());
fileInfoDTOList.forEach(repositoryFile -> {
if (repositoryFile.getFileByte() != null) {
multipartFiles.put(FileUtils.BODY_FILE_DIR + File.separator + repositoryFileMap.get(repositoryFile.getId()), repositoryFile.getFileByte());
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();
@ -263,4 +270,54 @@ public class HashTreeUtil {
}
}
}
public static void downFile(
List<BodyFile> files,
Map<String, byte[]> multipartFiles,
FileMetadataService fileMetadataService) {
if (CollectionUtils.isNotEmpty(files)) {
Map<String, String> repositoryFileMap = new HashMap<>();
List<String> processFiles = new ArrayList<>();
for (BodyFile bodyFile : files) {
// 调试附件处理
if (StringUtils.isNotBlank(bodyFile.getRefResourceId())) {
FileMetadata fileMetadata = fileMetadataService.getFileMetadataById(bodyFile.getRefResourceId());
if (fileMetadata != null) {
bodyFile.setFileId(bodyFile.getRefResourceId());
bodyFile.setStorage(fileMetadata.getStorage());
processFiles.add(bodyFile.getFileId());
repositoryFileMap.put(bodyFile.getFileId(), bodyFile.getName());
}
} else if (StringUtils.isNotBlank(bodyFile.getFileId())) {
repositoryFileMap.put(bodyFile.getFileId(), bodyFile.getName());
} else {
File file = new File(bodyFile.getName());
if (file != null && file.exists()) {
byte[] fileByte = FileUtils.fileToByte(file);
if (fileByte != null) {
multipartFiles.put(file.getAbsolutePath(), fileByte);
}
}
}
}
List<FileInfoDTO> fileList = fileMetadataService.downloadFileByIds(repositoryFileMap.keySet());
if (CollectionUtils.isNotEmpty(fileList)) {
// 处理返回文件
fileList.forEach(repositoryFile -> {
if (ArrayUtils.isNotEmpty(repositoryFile.getFileByte())) {
String path = StringUtils.join(
FileUtils.BODY_FILE_DIR,
File.separator,
repositoryFileMap.get(repositoryFile.getId()));
// 调试文件路径
String key = processFiles.contains(repositoryFile.getId())
? repositoryFileMap.get(repositoryFile.getId())
: path;
multipartFiles.put(key, repositoryFile.getFileByte());
}
});
}
}
}
}

View File

@ -9,9 +9,7 @@ import io.metersphere.base.mapper.ApiExecutionQueueDetailMapper;
import io.metersphere.base.mapper.ApiScenarioMapper;
import io.metersphere.base.mapper.plan.TestPlanApiScenarioMapper;
import io.metersphere.commons.constants.ApiRunMode;
import io.metersphere.commons.constants.StorageConstants;
import io.metersphere.commons.utils.*;
import io.metersphere.dto.FileInfoDTO;
import io.metersphere.dto.JmeterRunRequestDTO;
import io.metersphere.environment.service.BaseEnvGroupProjectService;
import io.metersphere.metadata.service.FileMetadataService;
@ -218,30 +216,7 @@ public class ApiJMeterFileService {
// 获取附件
List<BodyFile> files = new LinkedList<>();
ApiFileUtil.getExecuteFiles(hashTree, reportId, files);
if (CollectionUtils.isNotEmpty(files)) {
Map<String, String> repositoryFileMap = new HashMap<>();
for (BodyFile bodyFile : files) {
if (StringUtils.equals(bodyFile.getStorage(), StorageConstants.GIT.name())
&& StringUtils.isNotBlank(bodyFile.getFileId())) {
repositoryFileMap.put(bodyFile.getFileId(), bodyFile.getName());
} else {
File file = new File(bodyFile.getName());
if (file != null && file.exists()) {
byte[] fileByte = FileUtils.fileToByte(file);
if (fileByte != null) {
multipartFiles.put(file.getAbsolutePath(), fileByte);
}
}
}
}
List<FileInfoDTO> fileInfoDTOList = fileMetadataService.downloadFileByIds(repositoryFileMap.keySet());
fileInfoDTOList.forEach(repositoryFile -> {
if (repositoryFile.getFileByte() != null) {
multipartFiles.put(FileUtils.BODY_FILE_DIR + File.separator + repositoryFileMap.get(repositoryFile.getId()), repositoryFile.getFileByte());
}
});
}
HashTreeUtil.downFile(files, multipartFiles, fileMetadataService);
return multipartFiles;
}
@ -305,6 +280,9 @@ public class ApiJMeterFileService {
public byte[] zipFilesToByteArray(BodyFileRequest request) {
Map<String, byte[]> files = new LinkedHashMap<>();
if (CollectionUtils.isNotEmpty(request.getBodyFiles())) {
LoggerUtil.info("开始从三方仓库下载文件");
HashTreeUtil.downFile(request.getBodyFiles(), files, fileMetadataService);
LoggerUtil.info("从三方仓库下载文件");
for (BodyFile bodyFile : request.getBodyFiles()) {
File file = new File(bodyFile.getName());
if (!file.exists()) {

View File

@ -15,6 +15,8 @@ public class BodyFile {
private String fileType;
// 正常/已删除
private String status;
// 调试附件处理
private String refResourceId;
public boolean isRef() {
return StringUtils.equals(storage, StorageConstants.FILE_REF.name()) && StringUtils.isNotEmpty(fileId);