refactor(接口测试): 禁用本地执行功能开源

--task=1010912 --user=赵勇 【开源计划】接口测试... https://www.tapd.cn/55049933/s/1323587
This commit is contained in:
fit2-zhao 2023-01-06 15:04:26 +08:00 committed by fit2-zhao
parent 20abce6d9b
commit ce1aae2e4f
12 changed files with 119 additions and 74 deletions

View File

@ -23,7 +23,7 @@ import io.metersphere.jmeter.JMeterBase;
import io.metersphere.jmeter.LocalRunner;
import io.metersphere.service.RemakeReportService;
import io.metersphere.utils.LoggerUtil;
import io.metersphere.xpack.api.service.ApiPoolDebugService;
import io.metersphere.service.ApiPoolDebugService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.ArrayUtils;
@ -59,6 +59,8 @@ public class JMeterService {
private RemakeReportService remakeReportService;
@Resource
private ExecThreadPoolExecutor execThreadPoolExecutor;
@Resource
private ApiPoolDebugService apiPoolDebugService;
@PostConstruct
private void init() {
@ -172,16 +174,13 @@ public class JMeterService {
private synchronized void nodeDebug(JmeterRunRequestDTO request) {
try {
ApiPoolDebugService apiPoolDebugService = CommonBeanFactory.getBean(ApiPoolDebugService.class);
if (apiPoolDebugService != null) {
List<TestResource> resources = GenerateHashTreeUtil.setPoolResource(request.getPoolId());
if (request.getHashTree() != null) {
String key = StringUtils.join(request.getReportId(), "-", request.getTestId());
redisTemplate.opsForValue().set(key, new MsTestPlan().getJmx(request.getHashTree()));
request.setHashTree(null);
}
apiPoolDebugService.run(request, resources);
List<TestResource> resources = GenerateHashTreeUtil.setPoolResource(request.getPoolId());
if (request.getHashTree() != null) {
String key = StringUtils.join(request.getReportId(), "-", request.getTestId());
redisTemplate.opsForValue().set(key, new MsTestPlan().getJmx(request.getHashTree()));
request.setHashTree(null);
}
apiPoolDebugService.run(request, resources);
} catch (Exception e) {
LoggerUtil.error(e);
remakeReportService.remake(request);
@ -263,9 +262,6 @@ public class JMeterService {
}
public void verifyPool(String projectId, RunModeConfigDTO runModeConfigDTO) {
ApiPoolDebugService debugService = CommonBeanFactory.getBean(ApiPoolDebugService.class);
if (debugService != null) {
debugService.verifyPool(projectId, runModeConfigDTO);
}
apiPoolDebugService.verifyPool(projectId, runModeConfigDTO);
}
}

View File

@ -100,7 +100,6 @@ import { getCurrentProjectID } from 'metersphere-frontend/src/utils/token';
import { getSystemBaseSetting } from 'metersphere-frontend/src/api/system';
import EnvSelectPopover from '@/business/automation/scenario/EnvSelectPopover';
import { getApiCaseEnvironments } from '@/api/api-test-case';
import { hasLicense } from 'metersphere-frontend/src/utils/permission';
export default {
name: 'ApiRunMode',
@ -155,9 +154,7 @@ export default {
this.runModeVisible = true;
this.getResourcePools();
this.getWsProjects();
if (hasLicense()) {
this.query();
}
this.query();
this.showPopover();
this.runConfig.environmentType = ENV_TYPE.JSON;
},

View File

@ -115,7 +115,6 @@ import EnvPopover from '@/business/automation/scenario/EnvPopover';
import { getMaintainer, getOwnerProjects, getProjectConfig } from '@/api/project';
import { getTestResourcePools } from '@/api/test-resource-pool';
import { getSystemBaseSetting } from 'metersphere-frontend/src/api/system';
import { hasLicense } from 'metersphere-frontend/src/utils/permission';
function defaultCustomValidate() {
return { pass: true };
@ -322,9 +321,7 @@ export default {
this.activeName = 'first';
this.getResourcePools();
this.getWsProjects();
if (hasLicense()) {
this.query();
}
this.query();
this.runConfig.environmentType = ENV_TYPE.JSON;
},
findSchedule() {

View File

@ -0,0 +1,81 @@
package io.metersphere.service;
import io.metersphere.base.domain.TestResource;
import io.metersphere.commons.constants.ApiRunMode;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.JSON;
import io.metersphere.dto.*;
import io.metersphere.utils.LoggerUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.util.List;
@Service
public class ApiPoolDebugService {
public static final String BASE_URL = "http://%s:%d";
public static final String POOL = "POOL";
@Resource
private RestTemplate restTemplate;
public static final String SAVE_RESULT = "SAVE_RESULT";
@Resource
private SystemParameterService systemParameterService;
@Resource
private BaseProjectApplicationService baseProjectApplicationService;
public void run(JmeterRunRequestDTO request, List<TestResource> resources) throws MSException {
try {
if (request.isDebug() && !StringUtils.equalsAny(request.getRunMode(), ApiRunMode.DEFINITION.name())) {
request.getExtendedParameters().put(SAVE_RESULT, true);
} else if (!request.isDebug()) {
request.getExtendedParameters().put(SAVE_RESULT, true);
}
String uri = null;
int index = (int) (Math.random() * resources.size());
String configuration = resources.get(index).getConfiguration();
if (StringUtils.isNotEmpty(configuration)) {
NodeDTO node = JSON.parseObject(configuration, NodeDTO.class);
uri = String.format(BASE_URL + "/jmeter/debug", node.getIp(), node.getPort());
}
if (StringUtils.isEmpty(uri)) {
LoggerUtil.info("url为空", request.getReportId());
MSException.throwException("请在【项目设置-应用管理-接口测试】中选择资源池");
}
LoggerUtil.info("开始发送请求【 " + request.getTestId() + " 】到 " + uri + " 节点执行", request.getReportId());
ResponseEntity<String> result = restTemplate.postForEntity(uri, request, String.class);
if (result == null || !StringUtils.equals("SUCCESS", result.getBody())) {
LoggerUtil.error("发送请求[ " + request.getTestId() + " ] 到" + uri + " 节点执行失败", request.getReportId());
LoggerUtil.info(result);
MSException.throwException("请在【项目设置-应用管理-接口测试】中选择资源池");
}
} catch (Exception e) {
LoggerUtil.error("发送请求[ " + request.getTestId() + " ] 执行失败,进行数据回滚:", request.getReportId(), e);
MSException.throwException("请在【项目设置-应用管理-接口测试】中选择资源池");
}
}
/**
* 检查是否禁用了本地执行
*
* @param projectId
* @param runConfig
*/
public void verifyPool(String projectId, RunModeConfigDTO runConfig) {
if (runConfig != null && StringUtils.isEmpty(runConfig.getResourcePoolId())) {
BaseSystemConfigDTO configDTO = systemParameterService.getBaseInfo();
LoggerUtil.info("校验项目为:【" + projectId + "", runConfig.getReportId());
if (StringUtils.equals(configDTO.getRunMode(), POOL)) {
ProjectConfig config = baseProjectApplicationService.getProjectConfig(projectId);
if (config == null || !config.getPoolEnable() || StringUtils.isEmpty(config.getResourcePoolId())) {
MSException.throwException("请在【项目设置-应用管理-接口测试】中选择资源池");
}
runConfig = runConfig == null ? new RunModeConfigDTO() : runConfig;
runConfig.setResourcePoolId(config.getResourcePoolId());
}
}
}
}

View File

@ -1,13 +0,0 @@
package io.metersphere.xpack.api.service;
import io.metersphere.base.domain.TestResource;
import io.metersphere.dto.JmeterRunRequestDTO;
import io.metersphere.dto.RunModeConfigDTO;
import java.util.List;
public interface ApiPoolDebugService {
public void run(JmeterRunRequestDTO request, List<TestResource> resources);
public void verifyPool(String projectId, RunModeConfigDTO runConfig);
}

View File

@ -97,7 +97,7 @@
<!-- 接口测试资源池 -->
<app-manage-item :title="$t('pj.api_run_pool_title')" :prepend-span="8" :middle-span="12"
:append-span="4" v-if="isPool && isXpack">
:append-span="4" v-if="isPool">
<template #middle>
<el-select v-model="config.resourcePoolId"
size="mini"

View File

@ -22,7 +22,7 @@
<el-form-item :label="$t('commons.help_documentation')" prop="docUrl">
<el-input v-model="formInline.docUrl" placeholder="https://metersphere.io/docs/index.html"/>
</el-form-item>
<el-form-item :label="$t('system.api_default_run')" prop="runMode" v-if="hasLicense()">
<el-form-item :label="$t('system.api_default_run')" prop="runMode">
<el-switch active-value="LOCAL" inactive-value="POOL" v-model="formInline.runMode" @change="modeChange"/>
</el-form-item>
</el-col>
@ -41,7 +41,6 @@
<script>
import {getSystemBaseSetting, saveSystemBaseSetting} from "../../../api/system";
import {hasLicense} from 'metersphere-frontend/src/utils/permission';
export default {
name: "BaseSetting",
@ -80,7 +79,6 @@ export default {
this.query()
},
methods: {
hasLicense,
query() {
this.loading = getSystemBaseSetting().then(res => {
if(!res.data.runMode) {

View File

@ -49,7 +49,7 @@ import io.metersphere.request.ScheduleRequest;
import io.metersphere.service.*;
import io.metersphere.utils.DiscoveryUtil;
import io.metersphere.utils.LoggerUtil;
import io.metersphere.xpack.api.service.ApiPoolDebugService;
import io.metersphere.service.ApiPoolDebugService;
import io.metersphere.xpack.track.dto.IssuesDao;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
@ -65,8 +65,6 @@ import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
@ -156,6 +154,8 @@ public class TestPlanService {
private TestResourcePoolMapper testResourcePoolMapper;
@Resource
private TestPlanReportMapper testPlanReportMapper;
@Resource
private ApiPoolDebugService apiPoolDebugService;
public synchronized TestPlan addTestPlan(AddTestPlanRequest testPlan) {
if (getTestPlanByName(testPlan.getName()).size() > 0) {
@ -913,10 +913,7 @@ public class TestPlanService {
public void verifyPool(String projectId, RunModeConfigDTO runConfig) {
// 检查是否禁用了本地执行
ApiPoolDebugService debugService = CommonBeanFactory.getBean(ApiPoolDebugService.class);
if (debugService != null) {
debugService.verifyPool(projectId, runConfig);
}
apiPoolDebugService.verifyPool(projectId, runConfig);
}
/**
@ -1393,7 +1390,7 @@ public class TestPlanService {
TestPlanSimpleReportDTO report = buildPlanReport(planId, true);
report.setLang(lang);
TestPlanExtReportDTO extReport = getExtInfoByPlanId(planId);
if(extReport != null) {
if (extReport != null) {
BeanUtils.copyBean(report, extReport);
}
render(report, response);
@ -1402,7 +1399,7 @@ public class TestPlanService {
public void exportPlanDbReport(String reportId, String lang, HttpServletResponse response) throws UnsupportedEncodingException, JsonProcessingException {
TestPlanSimpleReportDTO report = testPlanReportService.getReport(reportId);
TestPlanExtReportDTO extReport = getExtInfoByReportId(reportId);
if(extReport != null) {
if (extReport != null) {
BeanUtils.copyBean(report, extReport);
}
Set<String> serviceIdSet = DiscoveryUtil.getServiceIdSet();
@ -1983,14 +1980,14 @@ public class TestPlanService {
Set<String> serviceIdSet = DiscoveryUtil.getServiceIdSet();
if (serviceIdSet.contains(MicroServiceName.API_TEST)) {
List<ApiDefinitionExecResultWithBLOBs> apiDefinitionLists = planTestPlanApiCaseService.selectExtForPlanReport(reportId);
if(CollectionUtils.isNotEmpty(apiDefinitionLists)){
if (CollectionUtils.isNotEmpty(apiDefinitionLists)) {
ApiDefinitionExecResultWithBLOBs apiDefinition = apiDefinitionLists.get(0);
convertEnvConfig(apiDefinition.getEnvConfig(), testPlanExtReportDTO);
getResourcePool(apiDefinition.getActuator(), testPlanExtReportDTO);
return testPlanExtReportDTO;
}
List<ApiScenarioReportWithBLOBs> apiScenarioLists = planTestPlanApiCaseService.selectExtForPlanScenarioReport(reportId);
if(CollectionUtils.isNotEmpty(apiScenarioLists)){
if (CollectionUtils.isNotEmpty(apiScenarioLists)) {
ApiScenarioReportWithBLOBs apiScenario = apiScenarioLists.get(0);
convertEnvConfig(apiScenario.getEnvConfig(), testPlanExtReportDTO);
getResourcePool(apiScenario.getActuator(), testPlanExtReportDTO);
@ -1999,7 +1996,7 @@ public class TestPlanService {
}
if (serviceIdSet.contains(MicroServiceName.UI_TEST)) {
List<UiScenarioReportWithBLOBs> apiDefinitionLists = planTestPlanUiScenarioCaseService.selectExtForPlanReport(reportId);
if(CollectionUtils.isNotEmpty(apiDefinitionLists)){
if (CollectionUtils.isNotEmpty(apiDefinitionLists)) {
UiScenarioReportWithBLOBs apiDefinition = apiDefinitionLists.get(0);
convertEnvConfig(apiDefinition.getEnvConfig(), testPlanExtReportDTO);
getResourcePool(apiDefinition.getActuator(), testPlanExtReportDTO);
@ -2029,12 +2026,12 @@ public class TestPlanService {
}
private void convertEnvConfig(String envConfig, TestPlanExtReportDTO testPlanExtReportDTO) throws JsonProcessingException {
if(StringUtils.isEmpty(envConfig)){
if (StringUtils.isEmpty(envConfig)) {
return;
}
EnvConfig env = objectMapper.readValue(envConfig, EnvConfig.class);
if(StringUtils.isNotEmpty(env.getMode())){
if(RunMode.RUN_MODE_SERIAL.getCode().equals(env.getMode())){
if (StringUtils.isNotEmpty(env.getMode())) {
if (RunMode.RUN_MODE_SERIAL.getCode().equals(env.getMode())) {
testPlanExtReportDTO.setRunMode(RunMode.RUN_MODE_SERIAL.getDesc());
} else if (RunMode.RUN_MODE_PARALLEL.getCode().equals(env.getMode())) {
testPlanExtReportDTO.setRunMode(RunMode.RUN_MODE_PARALLEL.getDesc());
@ -2042,8 +2039,8 @@ public class TestPlanService {
}
}
private void getResourcePool(String actuator, TestPlanExtReportDTO testPlanExtReportDTO){
if(StringUtils.isEmpty(actuator)){
private void getResourcePool(String actuator, TestPlanExtReportDTO testPlanExtReportDTO) {
if (StringUtils.isEmpty(actuator)) {
return;
}
TestResourcePool testResourcePool = testResourcePoolMapper.selectByPrimaryKey(actuator);
@ -2052,21 +2049,21 @@ public class TestPlanService {
public TestPlanExtReportDTO getExtInfoByPlanId(String planId) throws JsonProcessingException {
String reportId = testPlanReportService.getLastReportByPlanId(planId);
if(StringUtils.isEmpty(reportId)){
if (StringUtils.isEmpty(reportId)) {
return null;
}
TestPlanExtReportDTO testPlanExtReportDTO = new TestPlanExtReportDTO();
Set<String> serviceIdSet = DiscoveryUtil.getServiceIdSet();
if (serviceIdSet.contains(MicroServiceName.API_TEST)) {
List<ApiDefinitionExecResultWithBLOBs> apiDefinitionLists = planTestPlanApiCaseService.selectExtForPlanReport(reportId);
if(CollectionUtils.isNotEmpty(apiDefinitionLists)){
if (CollectionUtils.isNotEmpty(apiDefinitionLists)) {
ApiDefinitionExecResultWithBLOBs apiDefinition = apiDefinitionLists.get(0);
convertEnvConfig(apiDefinition.getEnvConfig(), testPlanExtReportDTO);
getResourcePool(apiDefinition.getActuator(), testPlanExtReportDTO);
return testPlanExtReportDTO;
}
List<ApiScenarioReportWithBLOBs> apiScenarioLists = planTestPlanApiCaseService.selectExtForPlanScenarioReport(reportId);
if(CollectionUtils.isNotEmpty(apiScenarioLists)){
if (CollectionUtils.isNotEmpty(apiScenarioLists)) {
ApiScenarioReportWithBLOBs apiScenario = apiScenarioLists.get(0);
convertEnvConfig(apiScenario.getEnvConfig(), testPlanExtReportDTO);
getResourcePool(apiScenario.getActuator(), testPlanExtReportDTO);
@ -2118,12 +2115,12 @@ public class TestPlanService {
}
private void convertPlanEnvConfig(String envConfig, TestPlanExtReportDTO testPlanExtReportDTO) throws JsonProcessingException {
if(StringUtils.isEmpty(envConfig)){
if (StringUtils.isEmpty(envConfig)) {
return;
}
PlanEnvConfig env = objectMapper.readValue(envConfig, PlanEnvConfig.class);
if(StringUtils.isNotEmpty(env.getRunMode())){
if(RunMode.RUN_MODE_SERIAL.getCode().equals(env.getRunMode())){
if (StringUtils.isNotEmpty(env.getRunMode())) {
if (RunMode.RUN_MODE_SERIAL.getCode().equals(env.getRunMode())) {
testPlanExtReportDTO.setRunMode(RunMode.RUN_MODE_SERIAL.getDesc());
} else if (RunMode.RUN_MODE_PARALLEL.getCode().equals(env.getRunMode())) {
testPlanExtReportDTO.setRunMode(RunMode.RUN_MODE_PARALLEL.getDesc());

View File

@ -281,9 +281,7 @@ export default {
this.testType = testType;
this.getResourcePools();
this.getWsProjects();
if(hasLicense()) {
this.query();
}
this.query();
},
query() {
this.loading = true;

View File

@ -298,9 +298,7 @@ export default {
this.getResourcePools();
this.getWsProjects();
this.showPopover();
if(hasLicense()) {
this.query();
}
this.query();
},
query() {
this.loading = true;

View File

@ -368,9 +368,7 @@ export default {
return param;
},
open(row) {
if (this.isHasLicense) {
this.query();
}
this.query();
this.planId = row.id;
//
let paramTestId = row.id;

View File

@ -274,9 +274,7 @@ export default {
this.testType = testType;
this.getResourcePools();
this.getWsProjects();
if(hasLicense()) {
this.query();
}
this.query();
},
query() {
this.loading = true;