From 5dbf55c866c6e049e9e3681df6a7099342fbbf71 Mon Sep 17 00:00:00 2001 From: song-tianyang Date: Fri, 10 Feb 2023 16:08:07 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):?= =?UTF-8?q?=20=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=E8=BF=87=E7=A8=8B=E4=BC=98=E5=8C=96=EF=BC=9A=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E5=AF=B9=E8=AF=81=E4=B9=A6=E6=96=87=E4=BB=B6=E7=9A=84=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E4=BB=A5=E5=8F=8A=E5=AF=B9=E8=B0=83=E8=AF=95=E6=97=B6?= =?UTF-8?q?=E4=B8=B4=E6=97=B6=E6=96=87=E4=BB=B6=E7=9A=84=E5=88=A0=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 接口测试执行过程优化:增加对证书文件的获取以及对调试时临时文件的删除 --- .../request/sampler/MsHTTPSamplerProxy.java | 4 ++-- .../api/jmeter/MsDebugListener.java | 2 ++ .../commons/constants/ElementConstants.java | 5 +++-- .../metersphere/commons/utils/ApiFileUtil.java | 17 +++++++++++++++++ .../commons/utils/HashTreeUtil.java | 4 ++++ .../metersphere/commons/utils/FileUtils.java | 18 ++++++++++++++++++ .../metadata/service/FileMetadataService.java | 5 ++++- 7 files changed, 50 insertions(+), 5 deletions(-) diff --git a/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java b/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java index 297b36658b..7ae434d36e 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java +++ b/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java @@ -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())); } diff --git a/api-test/backend/src/main/java/io/metersphere/api/jmeter/MsDebugListener.java b/api-test/backend/src/main/java/io/metersphere/api/jmeter/MsDebugListener.java index 74e4161053..fb71c42bf7 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/jmeter/MsDebugListener.java +++ b/api-test/backend/src/main/java/io/metersphere/api/jmeter/MsDebugListener.java @@ -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(); } diff --git a/api-test/backend/src/main/java/io/metersphere/commons/constants/ElementConstants.java b/api-test/backend/src/main/java/io/metersphere/commons/constants/ElementConstants.java index a69764f558..522be02675 100644 --- a/api-test/backend/src/main/java/io/metersphere/commons/constants/ElementConstants.java +++ b/api-test/backend/src/main/java/io/metersphere/commons/constants/ElementConstants.java @@ -54,9 +54,10 @@ public class ElementConstants { public static final String BEANSHELL = "beanshell"; public static final String IS_REF = "isRef"; public static final String FILE_ID = "fileId"; - + 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"; } 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 e06dc01d70..2626533c16 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 @@ -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 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 fileList) { if (testElement == null) { return; diff --git a/api-test/backend/src/main/java/io/metersphere/commons/utils/HashTreeUtil.java b/api-test/backend/src/main/java/io/metersphere/commons/utils/HashTreeUtil.java index df77b06b57..7b6b13c007 100644 --- a/api-test/backend/src/main/java/io/metersphere/commons/utils/HashTreeUtil.java +++ b/api-test/backend/src/main/java/io/metersphere/commons/utils/HashTreeUtil.java @@ -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( 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 8eaa3c412a..1ee66105b4 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 @@ -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 getZipJar() { List jarFiles = new LinkedList<>(); // jar 包 diff --git a/framework/sdk-parent/sdk/src/main/java/io/metersphere/metadata/service/FileMetadataService.java b/framework/sdk-parent/sdk/src/main/java/io/metersphere/metadata/service/FileMetadataService.java index 382b56c035..dc4ea0239b 100644 --- a/framework/sdk-parent/sdk/src/main/java/io/metersphere/metadata/service/FileMetadataService.java +++ b/framework/sdk-parent/sdk/src/main/java/io/metersphere/metadata/service/FileMetadataService.java @@ -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)); } });