diff --git a/Dockerfile b/Dockerfile index 07f9e02988..162115dbac 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,11 +6,11 @@ ARG MS_VERSION=dev RUN mkdir -p /opt/apps && mkdir -p /opt/jmeter -COPY backend/target/backend-1.4.jar /opt/apps +COPY backend/target/backend-1.5.jar /opt/apps COPY backend/target/classes/jmeter/ /opt/jmeter/ -ENV JAVA_APP_JAR=/opt/apps/backend-1.4.jar +ENV JAVA_APP_JAR=/opt/apps/backend-1.5.jar ENV AB_OFF=true diff --git a/README-EN.md b/README-EN.md index 9d0d82f374..a949f3fc9a 100644 --- a/README-EN.md +++ b/README-EN.md @@ -17,8 +17,8 @@ MeterSphere is a one-stop open-source enterprise-class continuous testing platfo Only need two steps to install MeterSphere: What you need: - 1. Prepare a 64-bit Linux host with no less than 8 G RAM - 2. Log into root user and execute the command down below to install MeterSphere +1. Prepare a 64-bit Linux host with no less than 8 G RAM +2. Log into root user and execute the command down below to install MeterSphere ```sh curl -sSL https://github.com/metersphere/metersphere/releases/latest/download/quick_start.sh | sh diff --git a/backend/pom.xml b/backend/pom.xml index 723061e1f8..1e6add67e4 100644 --- a/backend/pom.xml +++ b/backend/pom.xml @@ -7,7 +7,7 @@ metersphere-server io.metersphere - 1.4 + 1.5 4.0.0 @@ -156,7 +156,7 @@ org.python jython-standalone - 2.7.2 + 2.7.0 diff --git a/backend/src/main/java/io/metersphere/api/jmeter/APIBackendListenerClient.java b/backend/src/main/java/io/metersphere/api/jmeter/APIBackendListenerClient.java index 5d13cc5f00..b572fb3c8f 100644 --- a/backend/src/main/java/io/metersphere/api/jmeter/APIBackendListenerClient.java +++ b/backend/src/main/java/io/metersphere/api/jmeter/APIBackendListenerClient.java @@ -138,7 +138,7 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl testResult.setTotal(queue.size()); // 一个脚本里可能包含多个场景(MsThreadGroup),所以要区分开,key: 场景Id final Map scenarios = new LinkedHashMap<>(); - queue.forEach(result -> { + queue.forEach(result -> { // 线程名称: <场景名> <场景Index>-<请求Index>, 例如:Scenario 2-1 String scenarioName = StringUtils.substringBeforeLast(result.getThreadName(), THREAD_SPLIT); String index = StringUtils.substringAfterLast(result.getThreadName(), THREAD_SPLIT); @@ -146,7 +146,12 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl ScenarioResult scenarioResult; if (!scenarios.containsKey(scenarioId)) { scenarioResult = new ScenarioResult(); - scenarioResult.setId(scenarioId); + try { + scenarioResult.setId(Integer.parseInt(scenarioId)); + } catch (Exception e) { + scenarioResult.setId(0); + LogUtil.error("场景ID转换异常: " + e.getMessage()); + } scenarioResult.setName(scenarioName); scenarios.put(scenarioId, scenarioResult); } else { @@ -203,13 +208,13 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl testPlanTestCaseService.updateTestCaseStates(ids, TestPlanTestCaseStatus.Failure.name()); } } catch (Exception e) { - LogUtil.error(e); + LogUtil.error(e.getMessage(), e); } } try { sendTask(report, testResult); } catch (Exception e) { - LogUtil.error(e); + LogUtil.error(e.getMessage(), e); } } diff --git a/backend/src/main/java/io/metersphere/api/jmeter/ScenarioResult.java b/backend/src/main/java/io/metersphere/api/jmeter/ScenarioResult.java index 331b9b8ab1..33b06b957c 100644 --- a/backend/src/main/java/io/metersphere/api/jmeter/ScenarioResult.java +++ b/backend/src/main/java/io/metersphere/api/jmeter/ScenarioResult.java @@ -8,7 +8,7 @@ import java.util.List; @Data public class ScenarioResult { - private String id; + private Integer id; private String name; diff --git a/backend/src/main/java/io/metersphere/api/parse/JmeterDocumentParser.java b/backend/src/main/java/io/metersphere/api/parse/JmeterDocumentParser.java index bbffeeaed5..4acf7f5690 100644 --- a/backend/src/main/java/io/metersphere/api/parse/JmeterDocumentParser.java +++ b/backend/src/main/java/io/metersphere/api/parse/JmeterDocumentParser.java @@ -50,7 +50,7 @@ public class JmeterDocumentParser { } return documentToBytes(document); } catch (Exception e) { - LogUtil.error(e); + LogUtil.error(e.getMessage(), e); return source; } } @@ -161,7 +161,9 @@ public class JmeterDocumentParser { break; case "Argument.value": String textContent = ele.getTextContent(); - ele.setTextContent(ScriptEngineUtils.calculate(textContent)); + if (StringUtils.startsWith(textContent, "@")) { + ele.setTextContent(ScriptEngineUtils.calculate(textContent)); + } break; default: break; diff --git a/backend/src/main/java/io/metersphere/api/service/APITestService.java b/backend/src/main/java/io/metersphere/api/service/APITestService.java index e0efbe7dea..9dcac1c29f 100644 --- a/backend/src/main/java/io/metersphere/api/service/APITestService.java +++ b/backend/src/main/java/io/metersphere/api/service/APITestService.java @@ -8,6 +8,7 @@ import io.metersphere.api.dto.scenario.request.dubbo.RegistryCenter; import io.metersphere.api.jmeter.JMeterService; import io.metersphere.api.parse.ApiImportParser; import io.metersphere.api.parse.ApiImportParserFactory; +import io.metersphere.api.parse.JmeterDocumentParser; import io.metersphere.base.domain.*; import io.metersphere.base.mapper.ApiTestFileMapper; import io.metersphere.base.mapper.ApiTestMapper; @@ -85,9 +86,9 @@ public class APITestService { return extApiTestMapper.listByIds(request.getIds()); } - public void create(SaveAPITestRequest request, List bodyFiles) { + public void create(SaveAPITestRequest request, MultipartFile file, List bodyFiles) { List bodyUploadIds = new ArrayList<>(request.getBodyUploadIds()); - ApiTest test = createTest(request); + ApiTest test = createTest(request, file); createBodyFiles(test, bodyUploadIds, bodyFiles); } private ApiTest createTest(SaveAPITestRequest request, MultipartFile file) { @@ -101,13 +102,17 @@ public class APITestService { return test; } - public void update(SaveAPITestRequest request, List bodyFiles) { + public void update(SaveAPITestRequest request, MultipartFile file, List bodyFiles) { + if (file == null) { + throw new IllegalArgumentException(Translator.get("file_cannot_be_null")); + } deleteFileByTestId(request.getId()); List bodyUploadIds = new ArrayList<>(request.getBodyUploadIds()); request.setBodyUploadIds(null); ApiTest test = updateTest(request); createBodyFiles(test, bodyUploadIds, bodyFiles); + saveFile(test.getId(), file); } private void createBodyFiles(ApiTest test, List bodyUploadIds, List bodyFiles) { @@ -126,7 +131,7 @@ public class APITestService { file.createNewFile(); FileUtil.copyStream(in, out); } catch (IOException e) { - LogUtil.error(e); + LogUtil.error(e.getMessage(), e); MSException.throwException(Translator.get("upload_fail")); } } @@ -169,7 +174,7 @@ public class APITestService { try { FileUtil.copyDir(sourceFile, new File(targetDir)); } catch (IOException e) { - LogUtil.error(e); + LogUtil.error(e.getMessage(), e); MSException.throwException(Translator.get("upload_fail")); } } @@ -212,14 +217,27 @@ public class APITestService { } } - public String run(SaveAPITestRequest request,String runMode) { + public String run(SaveAPITestRequest request) { + ApiTestFile file = getFileByTestId(request.getId()); + if (file == null) { + MSException.throwException(Translator.get("file_cannot_be_null")); + } + byte[] bytes = fileService.loadFileAsBytes(file.getFileId()); + // 解析 xml 处理 mock 数据 + bytes = JmeterDocumentParser.parse(bytes); + InputStream is = new ByteArrayInputStream(bytes); + APITestResult apiTest = get(request.getId()); if (SessionUtils.getUser() == null) { apiTest.setUserId(request.getUserId()); } String reportId = apiReportService.create(apiTest, request.getTriggerMode()); + /*if (request.getTriggerMode().equals("SCHEDULE")) { + List notice = noticeService.queryNotice(request.getId()); + mailService.sendHtml(reportId,notice,"api"); + }*/ changeStatus(request.getId(), APITestStatus.Running); - jMeterService.run(request.getId(), request.getName(), request.getScenarioDefinition(), null,runMode); + jMeterService.run(request.getId(), null, is); return reportId; } @@ -260,8 +278,6 @@ public class APITestService { } private ApiTest createTest(SaveAPITestRequest request) { - checkQuota(); - request.setBodyUploadIds(null); checkNameExist(request); final ApiTest test = new ApiTest(); test.setId(request.getId()); @@ -276,6 +292,14 @@ public class APITestService { return test; } + private void saveFile(String testId, MultipartFile file) { + final FileMetadata fileMetadata = fileService.saveFile(file); + ApiTestFile apiTestFile = new ApiTestFile(); + apiTestFile.setTestId(testId); + apiTestFile.setFileId(fileMetadata.getId()); + apiTestFileMapper.insert(apiTestFile); + } + private void deleteFileByTestId(String testId) { ApiTestFileExample ApiTestFileExample = new ApiTestFileExample(); ApiTestFileExample.createCriteria().andTestIdEqualTo(testId); @@ -287,12 +311,17 @@ public class APITestService { fileService.deleteFileByIds(fileIds); } } - private void saveFile(String testId, MultipartFile file) { - final FileMetadata fileMetadata = fileService.saveFile(file); - ApiTestFile apiTestFile = new ApiTestFile(); - apiTestFile.setTestId(testId); - apiTestFile.setFileId(fileMetadata.getId()); - apiTestFileMapper.insert(apiTestFile); + + private ApiTestFile getFileByTestId(String testId) { + ApiTestFileExample ApiTestFileExample = new ApiTestFileExample(); + ApiTestFileExample.createCriteria().andTestIdEqualTo(testId); + final List ApiTestFiles = apiTestFileMapper.selectByExample(ApiTestFileExample); + apiTestFileMapper.selectByExample(ApiTestFileExample); + if (!CollectionUtils.isEmpty(ApiTestFiles)) { + return ApiTestFiles.get(0); + } else { + return null; + } } public void updateSchedule(Schedule request) { @@ -312,17 +341,6 @@ public class APITestService { schedule.setType(ScheduleType.CRON.name()); return schedule; } - private ApiTestFile getFileByTestId(String testId) { - ApiTestFileExample ApiTestFileExample = new ApiTestFileExample(); - ApiTestFileExample.createCriteria().andTestIdEqualTo(testId); - final List ApiTestFiles = apiTestFileMapper.selectByExample(ApiTestFileExample); - apiTestFileMapper.selectByExample(ApiTestFileExample); - if (!CollectionUtils.isEmpty(ApiTestFiles)) { - return ApiTestFiles.get(0); - } else { - return null; - } - } private void addOrUpdateApiTestCronJob(Schedule request) { scheduleService.addOrUpdateCronJob(request, ApiTestJob.getJobKey(request.getResourceId()), ApiTestJob.getTriggerKey(request.getResourceId()), ApiTestJob.class); @@ -398,7 +416,10 @@ public class APITestService { return schedules; } - public String runDebug(SaveAPITestRequest request, List bodyFiles,String runMode) { + public String runDebug(SaveAPITestRequest request, MultipartFile file, List bodyFiles) { + if (file == null) { + throw new IllegalArgumentException(Translator.get("file_cannot_be_null")); + } updateTest(request); APITestResult apiTest = get(request.getId()); List bodyUploadIds = new ArrayList<>(request.getBodyUploadIds()); @@ -409,7 +430,17 @@ public class APITestService { } String reportId = apiReportService.createDebugReport(apiTest); - jMeterService.run(request.getId(), request.getName(), request.getScenarioDefinition(), reportId,runMode); + InputStream is = null; + try { + byte[] bytes = file.getBytes(); + // 解析 xml 处理 mock 数据 + bytes = JmeterDocumentParser.parse(bytes); + is = new ByteArrayInputStream(bytes); + } catch (IOException e) { + LogUtil.error(e.getMessage(), e); + } + + jMeterService.run(request.getId(), reportId, is); return reportId; } @@ -420,12 +451,10 @@ public class APITestService { } } - public void mergeCreate(SaveAPITestRequest request, List selectIds) { - ApiTest test = createTest(request); - selectIds.forEach(sourceId -> copyBodyFiles(test.getId(), sourceId)); - } - - public String getJMX(SaveAPITestRequest request) { - return jMeterService.getJMX(jMeterService.getHashTree(request.getId(), request.getName(), request.getScenarioDefinition())); + public void mergeCreate(SaveAPITestRequest request, MultipartFile file, List selectIds) { + ApiTest test = createTest(request, file); + selectIds.forEach(sourceId -> { + copyBodyFiles(test.getId(), sourceId); + }); } } diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiTestReportMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiTestReportMapper.xml index d7c92408b0..5cbc729bf8 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiTestReportMapper.xml +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiTestReportMapper.xml @@ -133,6 +133,7 @@ LEFT JOIN user ON user.id = r.user_id r.test_id = #{testId} + and r.status != 'Debug' ORDER BY r.update_time DESC diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestCaseMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestCaseMapper.xml index 53bb178dec..9a9a8b2d51 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestCaseMapper.xml +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestCaseMapper.xml @@ -258,7 +258,8 @@