feat(接口测试): 动态处理函数包

This commit is contained in:
fit2-zhao 2024-02-19 13:50:15 +08:00 committed by Craftsman
parent 2c123ff830
commit bbfb7d146e
5 changed files with 45 additions and 1 deletions

View File

@ -54,4 +54,8 @@ public class LocalRepositoryDir {
public static String getSystemCacheDir() {
return SYSTEM_CACHE_DIR;
}
public static String getFuncJarDir() {
return ROOT_DIR + "/api/func-jar";
}
}

View File

@ -66,6 +66,12 @@ public class TaskRequestDTO implements Serializable {
* id 为插件的 id + 更新时间戳
*/
private List<ApiExecuteFileInfo> pluginFiles;
/**
* 接口测试函数包
*/
private List<ApiExecuteFileInfo> funcJars;
/**
* 误报规则
*/

View File

@ -1,5 +1,6 @@
package io.metersphere.api.parser.jmeter.processor;
import io.metersphere.plugin.api.constants.ElementProperty;
import io.metersphere.project.constants.ScriptLanguageType;
import io.metersphere.project.api.processor.ScriptProcessor;
import io.metersphere.api.parser.jmeter.constants.JmeterAlias;
@ -17,6 +18,7 @@ public abstract class ScriptProcessorConverter extends MsProcessorConverter<Scri
public static final String ENV_VARIABLE_EXPRESSION = "${__metersphere_env_id}";
public static final String MS_RUNNING_ENV_PREFIX = "MS.ENV.";
public static void parse(TestElement testElement, ScriptProcessor scriptProcessor) {
// 脚本安全校验
ScriptFilter.verify(scriptProcessor.getScriptLanguage(), scriptProcessor.getName(), scriptProcessor.getScript());
@ -38,9 +40,10 @@ public abstract class ScriptProcessorConverter extends MsProcessorConverter<Scri
testElement.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass(JmeterAlias.TEST_BEAN_GUI));
testElement.setProperty(JmeterProperty.SCRIPT_LANGUAGE, scriptProcessor.getScriptLanguage());
testElement.setProperty(JmeterProperty.SCRIPT, scriptProcessor.getScript());
testElement.setProperty(ElementProperty.PROJECT_ID.name(), scriptProcessor.getProjectId());
}
public static boolean isJSR233(ScriptProcessor scriptProcessor) {
return !StringUtils.equals(scriptProcessor.getScriptLanguage(), ScriptLanguageType.BEANSHELL.getValue());
return !StringUtils.equals(scriptProcessor.getScriptLanguage(), ScriptLanguageType.BEANSHELL.getValue());
}
}

View File

@ -10,6 +10,7 @@ import io.metersphere.api.parser.TestElementParser;
import io.metersphere.api.parser.TestElementParserFactory;
import io.metersphere.plugin.api.dto.ParameterConfig;
import io.metersphere.plugin.api.spi.AbstractMsTestElement;
import io.metersphere.project.domain.FileMetadata;
import io.metersphere.project.domain.ProjectApplication;
import io.metersphere.project.dto.customfunction.request.CustomFunctionRunRequest;
import io.metersphere.project.service.FileAssociationService;
@ -299,6 +300,27 @@ public class ApiExecuteService {
}
taskRequest.setRefFiles(refFiles);
// 获取函数jar包
List<FileMetadata> fileMetadataList = fileManagementService.findJarByProjectId(List.of(taskRequest.getProjectId()));
taskRequest.setFuncJars(fileMetadataList.stream()
.map(file -> {
String fileName = file.getName() + "." + file.getType();
ApiExecuteFileInfo tempFileInfo = getApiExecuteFileInfo(file.getId(), fileName, file.getProjectId(), file.getStorage());
if (StorageType.isGit(file.getStorage())) {
// 设置Git信息
tempFileInfo.setFileMetadataRepositoryDTO(fileManagementService.getFileMetadataRepositoryDTO(file.getId()));
tempFileInfo.setFileModuleRepositoryDTO(fileManagementService.getFileModuleRepositoryDTO(file.getModuleId()));
}
return tempFileInfo;
}).toList());
// TODO 当前项目没有包分两种情况1 之前存在被删除2 一直不存在
// 为了兼容1 这种情况需要初始化一条空的数据由执行机去做卸载
if (CollectionUtils.isEmpty(taskRequest.getFuncJars())) {
ApiExecuteFileInfo tempFileInfo = new ApiExecuteFileInfo();
tempFileInfo.setProjectId(request.getProjectId());
taskRequest.setFuncJars(List.of(tempFileInfo));
}
}
private static ApiExecuteFileInfo getApiExecuteFileInfo(String fileId, String fileName, String projectId) {

View File

@ -231,4 +231,13 @@ public class FileManagementService {
byte[] fileBytes = this.getFile(fileMetadata);
return TempFileUtils.compressPic(fileBytes);
}
public List<FileMetadata> findJarByProjectId(List<String> projectIds) {
FileMetadataExample example = new FileMetadataExample();
example.createCriteria().andProjectIdIn(projectIds)
.andEnableEqualTo(true)
.andTypeEqualTo("jar")
.andLatestEqualTo(true);
return fileMetadataMapper.selectByExample(example);
}
}