fix:jenkins选择资源池运行

This commit is contained in:
wenyann 2021-06-18 17:41:09 +08:00 committed by 刘瑞斌
parent 3bc0459f42
commit a9e7c67970
10 changed files with 71 additions and 36 deletions

View File

@ -278,11 +278,12 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl
reportTask.setExecutor(userName);
reportTask.setExecutionTime(executionTime);
reportTask.setExecutionEnvironment(name);
} else if (StringUtils.equalsAny(this.runMode, ApiRunMode.API_PLAN.name(), ApiRunMode.SCHEDULE_API_PLAN.name())) {
//用例定时jenkins
} else if (StringUtils.equalsAny(this.runMode, ApiRunMode.API_PLAN.name(), ApiRunMode.SCHEDULE_API_PLAN.name(), ApiRunMode.JENKINS_API_PLAN.name())) {
apiDefinitionService.addResult(testResult);
//测试计划定时任务-接口执行逻辑的话需要同步测试计划的报告数据
if (StringUtils.equalsAny(this.runMode, ApiRunMode.SCHEDULE_API_PLAN.name(), ApiRunMode.JENKINS_API_PLAN.name())) {
if (StringUtils.equals(this.runMode, ApiRunMode.SCHEDULE_API_PLAN.name())) {
apiDefinitionExecResultService.saveApiResultByScheduleTask(testResult, ApiRunMode.SCHEDULE_API_PLAN.name());
List<String> testPlanReportIdList = new ArrayList<>();
testPlanReportIdList.add(debugReportId);
@ -290,17 +291,25 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl
testPlanReportService.checkTestPlanStatus(testPlanReportId);
}
testPlanReportService.updateReport(testPlanReportIdList, ApiRunMode.SCHEDULE_API_PLAN.name(), ReportTriggerMode.SCHEDULE.name());
} else if (StringUtils.equals(this.runMode, ApiRunMode.JENKINS_API_PLAN.name())) {
apiDefinitionExecResultService.saveApiResult(testResult, ApiRunMode.JENKINS_API_PLAN.name(), TriggerMode.API.name());
List<String> testPlanReportIdList = new ArrayList<>();
testPlanReportIdList.add(debugReportId);
for (String testPlanReportId : testPlanReportIdList) { // 更新每个测试计划的状态
testPlanReportService.checkTestPlanStatus(testPlanReportId);
}
testPlanReportService.updateReport(testPlanReportIdList, ApiRunMode.JENKINS_API_PLAN.name(), TriggerMode.API.name());
} else {
apiDefinitionExecResultService.saveApiResult(testResult, ApiRunMode.API_PLAN.name(), TriggerMode.MANUAL.name());
}
} else if (StringUtils.equalsAny(this.runMode, ApiRunMode.SCENARIO.name(), ApiRunMode.SCENARIO_PLAN.name(), ApiRunMode.SCHEDULE_SCENARIO_PLAN.name(), ApiRunMode.SCHEDULE_SCENARIO.name())) {
} else if (StringUtils.equalsAny(this.runMode, ApiRunMode.SCENARIO.name(), ApiRunMode.SCENARIO_PLAN.name(), ApiRunMode.SCHEDULE_SCENARIO_PLAN.name(), ApiRunMode.SCHEDULE_SCENARIO.name(), ApiRunMode.JENKINS_SCENARIO_PLAN.name())) {
// 执行报告不需要存储由用户确认后在存储
testResult.setTestId(testId);
ApiScenarioReport scenarioReport = apiScenarioReportService.complete(testResult, this.runMode);
//环境
ApiScenarioWithBLOBs apiScenario = apiAutomationService.getDto(scenarioReport.getScenarioId());
String name = "";
if(apiScenario!= null ) {
if (apiScenario != null) {
String executionEnvironment = apiScenario.getScenarioDefinition();
JSONObject json = JSONObject.parseObject(executionEnvironment);
if (json != null && json.getString("environmentMap") != null && json.getString("environmentMap").length() > 2) {

View File

@ -916,7 +916,7 @@ public class ApiAutomationService {
APIScenarioReportResult report;
Map<String, String> planEnvMap = new HashMap<>();
//如果是测试计划页面触发的执行方式生成报告时createScenarioReport第二个参数需要特殊处理
if (StringUtils.equalsAny(request.getRunMode(), ApiRunMode.SCENARIO_PLAN.name(), ApiRunMode.SCHEDULE_SCENARIO_PLAN.name())) {
if (StringUtils.equalsAny(request.getRunMode(), ApiRunMode.SCENARIO_PLAN.name(), ApiRunMode.SCHEDULE_SCENARIO_PLAN.name(), ApiRunMode.JENKINS_SCENARIO_PLAN.name())) {
String testPlanScenarioId = item.getId();
if (request.getScenarioTestPlanIdMap() != null && request.getScenarioTestPlanIdMap().containsKey(item.getId())) {
testPlanScenarioId = request.getScenarioTestPlanIdMap().get(item.getId());
@ -1091,7 +1091,7 @@ public class ApiAutomationService {
if (reportIds != null) {
//如果是测试计划页面触发的执行方式生成报告时createScenarioReport第二个参数需要特殊处理
APIScenarioReportResult report = null;
if (StringUtils.equalsAny(request.getRunMode(), ApiRunMode.SCENARIO_PLAN.name(), ApiRunMode.SCHEDULE_SCENARIO_PLAN.name())) {
if (StringUtils.equalsAny(request.getRunMode(), ApiRunMode.SCENARIO_PLAN.name(), ApiRunMode.SCHEDULE_SCENARIO_PLAN.name(), ApiRunMode.JENKINS_SCENARIO_PLAN.name())) {
String testPlanScenarioId = item.getId();
if (request.getScenarioTestPlanIdMap() != null && request.getScenarioTestPlanIdMap().containsKey(item.getId())) {
testPlanScenarioId = request.getScenarioTestPlanIdMap().get(item.getId());

View File

@ -66,7 +66,7 @@ public class ApiScenarioReportService {
if (result != null) {
if (StringUtils.equals(runMode, ApiRunMode.SCENARIO_PLAN.name())) {
return updatePlanCase(result, runMode);
} else if (StringUtils.equals(runMode, ApiRunMode.SCHEDULE_SCENARIO_PLAN.name())) {
} else if (StringUtils.equalsAny(runMode, ApiRunMode.SCHEDULE_SCENARIO_PLAN.name(), ApiRunMode.JENKINS_SCENARIO_PLAN.name())) {
return updateSchedulePlanCase(result, runMode);
} else {
updateScenarioStatus(result.getTestId());
@ -319,7 +319,7 @@ public class ApiScenarioReportService {
// margeReport(result, scenarioIds, scenarioNames, runMode, projectId, userId, reportIds);
TestPlanReportService testPlanReportService = CommonBeanFactory.getBean(TestPlanReportService.class);
testPlanReportService.updateReport(testPlanReportIdList, ApiRunMode.SCHEDULE_SCENARIO_PLAN.name(), ReportTriggerMode.SCHEDULE.name());
testPlanReportService.updateReport(testPlanReportIdList, runMode, lastReport.getTriggerMode());
return lastReport;
}

View File

@ -1,5 +1,6 @@
package io.metersphere.commons.constants;
public enum ApiRunMode {
RUN, DEBUG, DEFINITION, SCENARIO, API_PLAN, JENKINS_API_PLAN, JENKINS, SCENARIO_PLAN, API, SCHEDULE_API_PLAN, SCHEDULE_SCENARIO,SCHEDULE_SCENARIO_PLAN, SCHEDULE_PERFORMANCE_TEST
RUN, DEBUG, DEFINITION, SCENARIO, API_PLAN, JENKINS_API_PLAN, JENKINS_SCENARIO_PLAN, JENKINS_PERFORMANCE_TEST, JENKINS,
SCENARIO_PLAN, API, SCHEDULE_API_PLAN, SCHEDULE_SCENARIO, SCHEDULE_SCENARIO_PLAN, SCHEDULE_PERFORMANCE_TEST
}

View File

@ -8,5 +8,6 @@ public enum ReportTriggerMode {
* 性能测试用例执行触发报告
*/
CASE,
TEST_PLAN_SCHEDULE
TEST_PLAN_SCHEDULE,
TEST_PLAN_API,
}

View File

@ -157,6 +157,8 @@ public class TestPlanController {
ApiRunConfigDTO api = new ApiRunConfigDTO();
api.setMode(testplanRunRequest.getMode());
api.setResourcePoolId(testplanRunRequest.getResourcePoolId());
api.setOnSampleError(true);
api.setReportType("iddReport");
String apiRunConfig = JSONObject.toJSONString(api);
testPlanService.run(testplanRunRequest.getTestPlanId(), testplanRunRequest.getProjectId(), testplanRunRequest.getUserId(), testplanRunRequest.getTriggerMode(), apiRunConfig);
}

View File

@ -44,7 +44,14 @@ public class TestPlanReportController {
public String sendTask(@PathVariable String planId) {
TestPlanReport report = testPlanReportService.getTestPlanReport(planId);
testPlanReportService.update(report);
return "sucess";
return "sucess";
}
@GetMapping("/status/{planId}")
public String getStatus(@PathVariable String planId) {
TestPlanReport report = testPlanReportService.getTestPlanReport(planId);
String status = report.getStatus();
return status;
}
@PostMapping("/delete")

View File

@ -6,7 +6,7 @@ import lombok.Data;
public class ApiRunConfigDTO {
private String mode;
private String reportType;
private String onSampleError;
private boolean onSampleError;
private String runWithinResourcePool;
private String resourcePoolId;
}

View File

@ -168,7 +168,6 @@ public class TestPlanReportService {
//更新TestPlan状态改为进行中
testPlan.setStatus(TestPlanStatus.Underway.name());
testPlanMapper.updateByPrimaryKeySelective(testPlan);
return testPlanReport;
}
@ -243,8 +242,7 @@ public class TestPlanReportService {
String issuesInfo = null;
//因为接口案例的定时任务是单个案例开线程运行 所以要检查是否都执行完成全部执行完成时才会进行统一整理
if (StringUtils.equals(ReportTriggerMode.SCHEDULE.name(), triggerMode)
&& StringUtils.equalsAny(resourceRunMode, ApiRunMode.SCHEDULE_API_PLAN.name())) {
if (StringUtils.equalsAny(triggerMode, ReportTriggerMode.SCHEDULE.name(), ReportTriggerMode.API.name()) && StringUtils.equalsAny(resourceRunMode, ApiRunMode.SCHEDULE_API_PLAN.name(), ApiRunMode.JENKINS_API_PLAN.name())) {
List<String> statusList = extTestPlanApiCaseMapper.getStatusByTestPlanId(testPlan.getId());
for (String status : statusList) {
if (status == null) {
@ -280,14 +278,14 @@ public class TestPlanReportService {
//只针对定时任务做处理
if (StringUtils.equals(ReportTriggerMode.SCHEDULE.name(), triggerMode)
&& StringUtils.equals(resourceRunMode, ApiRunMode.SCHEDULE_API_PLAN.name())) {
if (StringUtils.equalsAny(triggerMode, ReportTriggerMode.SCHEDULE.name(), ReportTriggerMode.API.name())
&& StringUtils.equalsAny(resourceRunMode, ApiRunMode.SCHEDULE_API_PLAN.name(), ApiRunMode.JENKINS_API_PLAN.name())) {
testPlanReport.setIsApiCaseExecuting(false);
} else if (StringUtils.equals(ReportTriggerMode.SCHEDULE.name(), triggerMode)
&& StringUtils.equals(resourceRunMode, ApiRunMode.SCHEDULE_SCENARIO_PLAN.name())) {
} else if (StringUtils.equalsAny(triggerMode, ReportTriggerMode.SCHEDULE.name(), ReportTriggerMode.API.name())
&& StringUtils.equalsAny(resourceRunMode, ApiRunMode.SCHEDULE_SCENARIO_PLAN.name(), ApiRunMode.SCHEDULE_SCENARIO_PLAN.name())) {
testPlanReport.setIsScenarioExecuting(false);
} else if (StringUtils.equals(ReportTriggerMode.SCHEDULE.name(), triggerMode)
&& StringUtils.equals(resourceRunMode, ApiRunMode.SCHEDULE_PERFORMANCE_TEST.name())) {
} else if (StringUtils.equalsAny(triggerMode, ReportTriggerMode.SCHEDULE.name(), ReportTriggerMode.API.name())
&& StringUtils.equalsAny(resourceRunMode, ApiRunMode.SCHEDULE_PERFORMANCE_TEST.name(), ApiRunMode.JENKINS_PERFORMANCE_TEST.name())) {
testPlanReport.setIsPerformanceExecuting(false);
} else {
testPlanReport.setIsPerformanceExecuting(false);
@ -299,8 +297,8 @@ public class TestPlanReportService {
component.afterBuild(testCaseReportMetricDTO);
});
if (StringUtils.equals(ReportTriggerMode.SCHEDULE.name(), triggerMode)
&& StringUtils.equals(resourceRunMode, ApiRunMode.SCHEDULE_PERFORMANCE_TEST.name())) {
if (StringUtils.equalsAny(triggerMode, ReportTriggerMode.SCHEDULE.name(), ReportTriggerMode.API.name())
&& StringUtils.equalsAny(resourceRunMode, ApiRunMode.SCHEDULE_PERFORMANCE_TEST.name(), ApiRunMode.JENKINS_PERFORMANCE_TEST.name())) {
//如果是性能测试作为触发由于延迟原因可能会出现报告已经结束但是状态还是进行中的状态
List<TestCaseReportStatusResultDTO> loadResult = testCaseReportMetricDTO.getExecuteResult().getLoadResult();
for (TestCaseReportStatusResultDTO dto : loadResult) {
@ -381,17 +379,16 @@ public class TestPlanReportService {
testPlan.setStatus(TestPlanStatus.Completed.name());
testPlanMapper.updateByPrimaryKeySelective(testPlan);
}
if (StringUtils.equals(report.getTriggerMode(), ReportTriggerMode.API.name()) || StringUtils.equals(report.getTriggerMode(), ReportTriggerMode.SCHEDULE.name())) {
if (StringUtils.equalsAny(report.getTriggerMode(), ReportTriggerMode.API.name(), ReportTriggerMode.SCHEDULE.name())) {
//发送通知
sendMessage(report);
}
} catch (Exception e) {
}
} else {
}
testPlanReportMapper.updateByPrimaryKey(report);
}
@ -408,10 +405,16 @@ public class TestPlanReportService {
String failedContext = "";
String subject = "";
String event = "";
if (StringUtils.equals(testPlanReport.getTriggerMode(), ReportTriggerMode.API.name())) {
successContext = "测试计划jenkins任务通知:'" + testPlan.getName() + "'执行成功" + "\n" + "请点击下面链接进入测试报告页面" + "\n" + url;
failedContext = "测试计划jenkins任务通知:'" + testPlan.getName() + "'执行失败" + "\n" + "请点击下面链接进入测试报告页面" + "\n" + url;
subject = Translator.get("task_notification_jenkins");
} else {
successContext = "测试计划定时任务通知:'" + testPlan.getName() + "'执行成功" + "\n" + "请点击下面链接进入测试报告页面" + "\n" + url;
failedContext = "测试计划定时任务通知:'" + testPlan.getName() + "'执行失败" + "\n" + "请点击下面链接进入测试报告页面" + "\n" + url;
subject = Translator.get("task_notification");
}
successContext = "测试计划定时任务通知:'" + testPlan.getName() + "'执行成功" + "\n" + "请点击下面链接进入测试报告页面" + "\n" + url;
failedContext = "测试计划定时任务通知:'" + testPlan.getName() + "'执行失败" + "\n" + "请点击下面链接进入测试报告页面" + "\n" + url;
subject = Translator.get("task_notification");
if (StringUtils.equals(TestPlanReportStatus.FAILED.name(), testPlanReport.getStatus())) {
event = NoticeConstants.Event.EXECUTE_FAILED;
@ -428,7 +431,7 @@ public class TestPlanReportService {
String successfulMailTemplate = "";
String errfoMailTemplate = "";
if (StringUtils.equals(testPlanReport.getTriggerMode(), ReportTriggerMode.SCHEDULE.name())) {
if (StringUtils.equalsAny(testPlanReport.getTriggerMode(), ReportTriggerMode.SCHEDULE.name(), ReportTriggerMode.API.name())) {
successfulMailTemplate = "TestPlanSuccessfulNotification";
errfoMailTemplate = "TestPlanFailedNotification";
}
@ -498,7 +501,7 @@ public class TestPlanReportService {
for (String string : updatePerformaneReportIDList) {
TestPlanLoadCaseEventDTO eventDTO = new TestPlanLoadCaseEventDTO();
eventDTO.setReportId(string);
eventDTO.setTriggerMode(ReportTriggerMode.SCHEDULE.name());
eventDTO.setTriggerMode(triggerMode);
eventDTO.setStatus(PerformanceTestStatus.Completed.name());
this.updatePerformanceTestStatus(eventDTO);
}
@ -516,7 +519,11 @@ public class TestPlanReportService {
public void updatePerformanceTestStatus(TestPlanLoadCaseEventDTO eventDTO) {
List<String> testPlanReportId = extTestPlanMapper.findIdByPerformanceReportId(eventDTO.getReportId());
this.updateReport(testPlanReportId, ApiRunMode.SCHEDULE_PERFORMANCE_TEST.name(), eventDTO.getTriggerMode());
if (StringUtils.equals(eventDTO.getTriggerMode(), ReportTriggerMode.SCHEDULE.name())) {
this.updateReport(testPlanReportId, ApiRunMode.SCHEDULE_PERFORMANCE_TEST.name(), eventDTO.getTriggerMode());
} else {
this.updateReport(testPlanReportId, ApiRunMode.JENKINS_PERFORMANCE_TEST.name(), eventDTO.getTriggerMode());
}
}
public void delete(List<String> testPlanReportIdList) {

View File

@ -1060,7 +1060,7 @@ public class TestPlanService {
performanceRequest.setId(caseID);
performanceRequest.setTestPlanLoadId(caseID);
if (StringUtils.equals(ReportTriggerMode.API.name(), triggerMode)) {
performanceRequest.setTriggerMode(ReportTriggerMode.API.name());
performanceRequest.setTriggerMode(ReportTriggerMode.TEST_PLAN_API.name());
} else {
performanceRequest.setTriggerMode(ReportTriggerMode.TEST_PLAN_SCHEDULE.name());
}
@ -1095,7 +1095,7 @@ public class TestPlanService {
if (!performaneReportIDList.isEmpty()) {
//性能测试时保存性能测试报告ID在结果返回时用于捕捉并进行
testPlanReportService.updatePerformanceInfo(testPlanReport, performaneReportIDList, ReportTriggerMode.SCHEDULE.name());
testPlanReportService.updatePerformanceInfo(testPlanReport, performaneReportIDList, triggerMode);
}
@ -1106,7 +1106,12 @@ public class TestPlanService {
String planCaseID = entry.getValue();
ApiTestCaseWithBLOBs blobs = apiTestCaseService.get(apiCaseID);
//需要更新这里来保证PlanCase的状态能正常更改
apiTestCaseService.run(blobs, UUID.randomUUID().toString(), planReportId, testPlanID, ApiRunMode.SCHEDULE_API_PLAN.name());
if (StringUtils.equals(triggerMode, ReportTriggerMode.API.name())) {
apiTestCaseService.run(blobs, UUID.randomUUID().toString(), planReportId, testPlanID, ApiRunMode.JENKINS_API_PLAN.name());
} else {
apiTestCaseService.run(blobs, UUID.randomUUID().toString(), planReportId, testPlanID, ApiRunMode.SCHEDULE_API_PLAN.name());
}
apiCaseIsExcuting = true;
}
if (apiCaseIsExcuting) {
@ -1122,8 +1127,11 @@ public class TestPlanService {
scenarioRequest.setProjectId(projectID);
if (StringUtils.equals(triggerMode, ReportTriggerMode.API.name())) {
scenarioRequest.setTriggerMode(ReportTriggerMode.API.name());
scenarioRequest.setRunMode(ApiRunMode.JENKINS_SCENARIO_PLAN.name());
} else {
scenarioRequest.setTriggerMode(ReportTriggerMode.SCHEDULE.name());
scenarioRequest.setRunMode(ApiRunMode.SCHEDULE_SCENARIO_PLAN.name());
}
scenarioRequest.setExecuteType(ExecuteType.Saved.name());
Map<String, Map<String, String>> testPlanScenarioIdMap = new HashMap<>();
@ -1131,7 +1139,7 @@ public class TestPlanService {
scenarioRequest.setTestPlanScenarioIDMap(testPlanScenarioIdMap);
scenarioRequest.setReportUserID(userId);
scenarioRequest.setTestPlanID(testPlanID);
scenarioRequest.setRunMode(ApiRunMode.SCHEDULE_SCENARIO_PLAN.name());
scenarioRequest.setTestPlanReportId(planReportId);
RunModeConfig runModeConfig = JSONObject.parseObject(apiRunConfig, RunModeConfig.class);
scenarioRequest.setConfig(runModeConfig);