fix(接口测试): 修复接口用例调试时会一直生成tmp目录文件的问题

--bug=1017916 --user=宋天阳
【接口测试】接口case-请求体上传本地文件执行后,服务器中未删除,关联文件被删除但是目录文件夹未删除
https://www.tapd.cn/55049933/s/1260443
This commit is contained in:
song-tianyang 2022-10-13 13:43:24 +08:00 committed by 建国
parent 02e4f5e707
commit 828dba58ca
4 changed files with 26 additions and 39 deletions

View File

@ -30,10 +30,7 @@ import io.metersphere.base.mapper.ApiScenarioMapper;
import io.metersphere.commons.constants.*;
import io.metersphere.commons.enums.StorageEnums;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.FileUtils;
import io.metersphere.commons.utils.JSON;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.commons.utils.*;
import io.metersphere.constants.RunModeConstants;
import io.metersphere.environment.service.BaseEnvGroupProjectService;
import io.metersphere.environment.service.BaseEnvironmentService;
@ -43,8 +40,6 @@ import io.metersphere.metadata.service.FileMetadataService;
import io.metersphere.plugin.core.MsParameter;
import io.metersphere.plugin.core.MsTestElement;
import io.metersphere.request.BodyFile;
import io.metersphere.commons.utils.ApiFileUtil;
import io.metersphere.commons.utils.JSONUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
@ -139,7 +134,7 @@ public class ElementUtil {
fileId = file.getFileId();
if (fileMetadataService != null) {
FileMetadata fileMetadata = fileMetadataService.getFileMetadataById(fileId);
if (fileMetadata != null && StringUtils.equals(fileMetadata.getStorage(), StorageConstants.GIT.name())) {
if (fileMetadata != null && !StringUtils.equals(fileMetadata.getStorage(), StorageConstants.LOCAL.name())) {
isRepository = true;
}
}

View File

@ -15,31 +15,20 @@ import io.metersphere.api.dto.scenario.Body;
import io.metersphere.api.dto.scenario.environment.EnvironmentConfig;
import io.metersphere.api.jmeter.JMeterService;
import io.metersphere.api.jmeter.NewDriverManager;
import io.metersphere.service.definition.TcpApiParamService;
import io.metersphere.base.domain.ApiDefinitionExecResult;
import io.metersphere.base.domain.ApiDefinitionExecResultWithBLOBs;
import io.metersphere.base.domain.ApiDefinitionWithBLOBs;
import io.metersphere.base.domain.ApiTestCaseWithBLOBs;
import io.metersphere.base.domain.ApiTestEnvironmentWithBLOBs;
import io.metersphere.base.domain.TestPlanApiCase;
import io.metersphere.base.domain.TestPlanApiCaseExample;
import io.metersphere.base.domain.*;
import io.metersphere.base.mapper.ApiDefinitionExecResultMapper;
import io.metersphere.base.mapper.ApiDefinitionMapper;
import io.metersphere.base.mapper.ApiTestCaseMapper;
import io.metersphere.base.mapper.plan.TestPlanApiCaseMapper;
import io.metersphere.base.mapper.ext.ExtApiTestCaseMapper;
import io.metersphere.base.mapper.plan.TestPlanApiCaseMapper;
import io.metersphere.commons.constants.ApiRunMode;
import io.metersphere.commons.enums.ApiReportStatus;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.FileUtils;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.commons.utils.SessionUtils;
import io.metersphere.commons.utils.*;
import io.metersphere.dto.JmeterRunRequestDTO;
import io.metersphere.dto.MsExecResponseDTO;
import io.metersphere.environment.service.BaseEnvironmentService;
import io.metersphere.plugin.core.MsTestElement;
import io.metersphere.commons.utils.ApiDefinitionExecResultUtil;
import io.metersphere.commons.utils.JSONUtil;
import io.metersphere.service.definition.TcpApiParamService;
import io.metersphere.utils.LoggerUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
@ -51,13 +40,7 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.*;
@Service
@Transactional(rollbackFor = Exception.class)
@ -208,7 +191,7 @@ public class ApiExecuteService {
// 单接口调试生成tmp临时目录
requests.forEach(item -> {
Body body = item.getBody();
String tmpFilePath = "tmp/" + UUID.randomUUID().toString();
String tmpFilePath = "tmp/" + request.getReportId();
body.setTmpFilePath(tmpFilePath);
FileUtils.copyBdyFile(item.getId(), tmpFilePath);
FileUtils.createBodyFiles(tmpFilePath, bodyFiles);

View File

@ -4,10 +4,7 @@ package io.metersphere.api.jmeter;
import io.metersphere.api.exec.queue.PoolExecBlockingQueueUtil;
import io.metersphere.cache.JMeterEngineCache;
import io.metersphere.commons.constants.ApiRunMode;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.FileUtils;
import io.metersphere.commons.utils.FixedCapacityUtil;
import io.metersphere.commons.utils.JSON;
import io.metersphere.commons.utils.*;
import io.metersphere.constants.BackendListenerConstants;
import io.metersphere.constants.RunModeConstants;
import io.metersphere.dto.ResultDTO;
@ -24,6 +21,7 @@ import org.apache.jmeter.visualizers.backend.BackendListenerContext;
import java.io.File;
import java.io.Serializable;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
@ -65,7 +63,12 @@ public class MsApiBackendListener extends AbstractBackendListenerClient implemen
String console = FixedCapacityUtil.getJmeterLogger(dto.getReportId(), !StringUtils.equals(dto.getReportType(), RunModeConstants.SET_REPORT.toString()));
if (FileUtils.isFolderExists(dto.getReportId())) {
console += "\r\n" + "tmp folder " + FileUtils.BODY_FILE_DIR + File.separator + dto.getReportId() + " has deleted.";
console += "\r\n" + DateUtils.getTimeString(new Date()) + " INFO " +
"Tmp folder " + FileUtils.BODY_FILE_DIR + File.separator + dto.getReportId() + " has deleted.";
}
if (FileUtils.isFolderExists("tmp" + File.separator + dto.getReportId())) {
console += "\r\n" + DateUtils.getTimeString(new Date()) + " INFO " +
"Tmp folder " + FileUtils.BODY_FILE_DIR + File.separator + "tmp" + File.separator + dto.getReportId() + " has deleted.";
}
dto.setConsole(console);

View File

@ -281,6 +281,12 @@ public class FileUtils {
if (file.exists()) {
file.delete();
}
//删除临时目录中的文件
file = new File(BODY_FILE_DIR + File.separator + "tmp" + File.separator + requestId);
FileUtil.deleteContents(file);
if (file.exists()) {
file.delete();
}
}
public static String uploadFile(MultipartFile uploadFile, String path, String name) {
@ -378,7 +384,7 @@ public class FileUtils {
file.setName(arg.getPath());
if (arg.getPropertyAsBoolean("isRef") && fileMetadataService != null) {
FileMetadata fileMetadata = fileMetadataService.getFileMetadataById(arg.getPropertyAsString("fileId"));
if (fileMetadata != null && StringUtils.equalsAny(fileMetadata.getStorage(), StorageConstants.GIT.name(), StorageConstants.MINIO.name())) {
if (fileMetadata != null && !StringUtils.equals(fileMetadata.getStorage(), StorageConstants.LOCAL.name())) {
file.setStorage(fileMetadata.getStorage());
file.setFileId(arg.getPropertyAsString("fileId"));
file.setName(reportId + File.separator + fileMetadata.getName());
@ -396,7 +402,7 @@ public class FileUtils {
file.setName(source.getPropertyAsString("filename"));
if (source.getPropertyAsBoolean("isRef") && fileMetadataService != null) {
FileMetadata fileMetadata = fileMetadataService.getFileMetadataById(source.getPropertyAsString("fileId"));
if (fileMetadata != null && StringUtils.equalsAny(fileMetadata.getStorage(), StorageConstants.GIT.name(), StorageConstants.MINIO.name())) {
if (fileMetadata != null && !StringUtils.equals(fileMetadata.getStorage(), StorageConstants.LOCAL.name())) {
file.setStorage(fileMetadata.getStorage());
file.setFileId(source.getPropertyAsString("fileId"));
file.setName(reportId + File.separator + fileMetadata.getName());
@ -488,7 +494,7 @@ public class FileUtils {
for (HTTPFileArg arg : source.getHTTPFiles()) {
if (arg.getPropertyAsBoolean("isRef") && fileMetadataService != null) {
FileMetadata fileMetadata = fileMetadataService.getFileMetadataById(arg.getPropertyAsString("fileId"));
if (fileMetadata != null && StringUtils.equals(StorageConstants.GIT.name(), fileMetadata.getStorage())) {
if (fileMetadata != null && !StringUtils.equals(fileMetadata.getStorage(), StorageConstants.LOCAL.name())) {
list.add(fileMetadata);
arg.setPath(fileMetadata.getName());
arg.setName(fileMetadata.getName());
@ -501,7 +507,7 @@ public class FileUtils {
if (source != null && StringUtils.isNotEmpty(source.getPropertyAsString("filename"))) {
if (source.getPropertyAsBoolean("isRef") && fileMetadataService != null) {
FileMetadata fileMetadata = fileMetadataService.getFileMetadataById(source.getPropertyAsString("fileId"));
if (fileMetadata != null && StringUtils.equals(StorageConstants.GIT.name(), fileMetadata.getStorage())) {
if (fileMetadata != null && !StringUtils.equals(fileMetadata.getStorage(), StorageConstants.LOCAL.name())) {
list.add(fileMetadata);
source.setFilename(fileMetadata.getName());
}