refactor(接口测试): 接口测试执行过程优化:增加对证书文件的获取以及对调试时临时文件的删除

接口测试执行过程优化:增加对证书文件的获取以及对调试时临时文件的删除
This commit is contained in:
song-tianyang 2023-02-10 16:08:07 +08:00 committed by 建国
parent 89ab0222c3
commit a122001c75
7 changed files with 50 additions and 5 deletions

View File

@ -494,8 +494,8 @@ public class MsHTTPSamplerProxy extends MsTestElement {
keystoreConfig.setProperty("endIndex", -1);
keystoreConfig.setProperty("preload", true);
keystoreConfig.setProperty("startIndex", 0);
keystoreConfig.setProperty("MS-KEYSTORE-FILE-PATH", msKeyStore.getPath());
keystoreConfig.setProperty("MS-KEYSTORE-FILE-PASSWORD", msKeyStore.getPassword());
keystoreConfig.setProperty(ElementConstants.MS_KEYSTORE_FILE_PATH, msKeyStore.getPath());
keystoreConfig.setProperty(ElementConstants.MS_KEYSTORE_FILE_PASSWORD, msKeyStore.getPassword());
httpSamplerTree.add(keystoreConfig);
config.getKeyStoreMap().put(this.getProjectId(), new MsKeyStore(msKeyStore.getPath(), msKeyStore.getPassword()));
}

View File

@ -111,6 +111,8 @@ public class MsDebugListener extends AbstractListenerElement implements SampleLi
LoggerUtil.debug("send. " + this.getName());
WebSocketUtil.sendMessageSingle(dto);
PoolExecBlockingQueueUtil.offer(this.getName());
//删除可能出现的临时文件
FileUtils.deleteBodyTmpFiles(this.getName());
JvmUtil.memoryInfo();
}

View File

@ -58,5 +58,6 @@ public class ElementConstants {
public static final String RESOURCE_ID = "resourceId";
public static final String FILENAME = "filename";
public static final String COVER = "COVER";
public static final String MS_KEYSTORE_FILE_PATH = "MS-KEYSTORE-FILE-PATH";
public static final String MS_KEYSTORE_FILE_PASSWORD = "MS-KEYSTORE-FILE-PASSWORD";
}

View File

@ -16,6 +16,7 @@ import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.config.CSVDataSet;
import org.apache.jmeter.config.KeystoreConfig;
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy;
import org.apache.jmeter.protocol.http.util.HTTPFileArg;
import org.apache.jmeter.testelement.TestElement;
@ -137,6 +138,8 @@ public class ApiFileUtil extends FileUtils {
getAttachmentBodyFileByHttp(key, reportId, isLocal, fileList);
} else if (key instanceof CSVDataSet) {
getAttachmentBodyFileByCsv(key, reportId, isLocal, fileList);
} else if (key instanceof KeystoreConfig) {
getAttachmentBodyFileByKeystoreConfig(key, fileList);
}
if (node != null) {
formatFilePathForNode(node, reportId, isLocal, fileList);
@ -152,6 +155,20 @@ public class ApiFileUtil extends FileUtils {
}
}
public static void getAttachmentBodyFileByKeystoreConfig(Object tree, List<AttachmentBodyFile> bodyFileList) {
KeystoreConfig source = (KeystoreConfig) tree;
if (StringUtils.isNotEmpty(source.getPropertyAsString(ElementConstants.MS_KEYSTORE_FILE_PATH))) {
String filePath = source.getPropertyAsString(ElementConstants.MS_KEYSTORE_FILE_PATH);
//判断本地文件
AttachmentBodyFile attachmentBodyFile = new AttachmentBodyFile();
attachmentBodyFile.setFileStorage(StorageConstants.LOCAL.name());
attachmentBodyFile.setName(filePath);
attachmentBodyFile.setFilePath(filePath);
bodyFileList.add(attachmentBodyFile);
}
}
public static void getAttachmentBodyFileByHttp(Object testElement, String reportId, boolean isLocal, List<AttachmentBodyFile> fileList) {
if (testElement == null) {
return;

View File

@ -17,6 +17,7 @@ import io.metersphere.environment.service.BaseEnvironmentService;
import io.metersphere.metadata.service.FileMetadataService;
import io.metersphere.request.BodyFile;
import io.metersphere.service.ExtErrorReportLibraryService;
import io.metersphere.utils.LoggerUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
@ -251,10 +252,13 @@ public class HashTreeUtil {
repositoryFileMap.put(bodyFile.getFileMetadataId(), bodyFile.getName());
}
}
LoggerUtil.info("本次执行[" + runRequest.getReportId() + "]需要下载[" + repositoryFileMap.size() + "]个文件,开始下载......");
FileMetadataService fileMetadataService = CommonBeanFactory.getBean(FileMetadataService.class);
if (fileMetadataService != null) {
fileMetadataService.downloadApiExecuteFilesByIds(repositoryFileMap.keySet());
}
LoggerUtil.info("本次执行[" + runRequest.getReportId() + "]需要下载[" + repositoryFileMap.size() + "]个文件,下载结束。");
}
public static void downFile(

View File

@ -499,6 +499,24 @@ public class FileUtils {
return file.isDirectory();
}
public static void deleteBodyTmpFiles(String reportId) {
if (StringUtils.isNotEmpty(reportId)) {
String executeTmpFolder = StringUtils.join(
BODY_FILE_DIR,
File.separator,
"tmp",
File.separator,
reportId
);
try {
FileUtils.deleteDir(executeTmpFolder);
} catch (Exception e) {
LoggerUtil.error("删除[" + reportId + "]执行中产生的临时文件失败!", e);
}
}
}
public List<Object> getZipJar() {
List<Object> jarFiles = new LinkedList<>();
// jar

View File

@ -22,6 +22,7 @@ import io.metersphere.metadata.utils.MetadataUtils;
import io.metersphere.metadata.vo.*;
import io.metersphere.request.OrderRequest;
import io.metersphere.request.QueryProjectFileRequest;
import io.metersphere.utils.LoggerUtil;
import io.metersphere.utils.TemporaryFileUtil;
import jakarta.annotation.Resource;
import org.apache.commons.collections.CollectionUtils;
@ -545,10 +546,12 @@ public class FileMetadataService {
//检查是否存在已下载的文件
fileMetadataWithBLOBList.forEach(fileMetadata -> {
File file = temporaryFileUtil.getFile(fileMetadata.getProjectId(), fileMetadata.getUpdateTime(), fileMetadata.getName());
if (file != null && file.exists() && file.isFile()) {
if (file != null) {
LoggerUtil.info("文件【" + fileMetadata.getUpdateTime() + "_" + fileMetadata.getName() + "】在执行目录【" + fileMetadata.getProjectId() + "】已找到,无需下载");
FileInfoDTO fileInfoDTO = new FileInfoDTO(fileMetadata.getId(), fileMetadata.getName(), fileMetadata.getProjectId(), fileMetadata.getUpdateTime(), fileMetadata.getStorage(), fileMetadata.getPath(), FileUtils.fileToByte(file));
fileInfoDTOList.add(fileInfoDTO);
} else {
LoggerUtil.info("文件【" + fileMetadata.getUpdateTime() + "_" + fileMetadata.getName() + "】在执行目录【" + fileMetadata.getProjectId() + "】未找到,需要下载");
downloadFileRequest.add(this.genFileRequest(fileMetadata));
}
});