feat(接口测试): 增加dns生成

This commit is contained in:
wxg0103 2024-03-19 19:38:51 +08:00 committed by Craftsman
parent 4adc3c3355
commit 1ea17a964c
5 changed files with 97 additions and 12 deletions

View File

@ -22,6 +22,7 @@ import io.metersphere.project.api.KeyValueEnableParam;
import io.metersphere.project.api.KeyValueParam; import io.metersphere.project.api.KeyValueParam;
import io.metersphere.project.dto.environment.EnvironmentInfoDTO; import io.metersphere.project.dto.environment.EnvironmentInfoDTO;
import io.metersphere.project.dto.environment.GlobalParams; import io.metersphere.project.dto.environment.GlobalParams;
import io.metersphere.project.dto.environment.host.Host;
import io.metersphere.project.dto.environment.http.HttpConfig; import io.metersphere.project.dto.environment.http.HttpConfig;
import io.metersphere.project.dto.environment.http.HttpConfigPathMatchRule; import io.metersphere.project.dto.environment.http.HttpConfigPathMatchRule;
import io.metersphere.project.dto.environment.http.SelectModule; import io.metersphere.project.dto.environment.http.SelectModule;
@ -32,10 +33,7 @@ import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.config.Arguments; import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.http.control.AuthManager; import org.apache.jmeter.protocol.http.control.*;
import org.apache.jmeter.protocol.http.control.Authorization;
import org.apache.jmeter.protocol.http.control.Header;
import org.apache.jmeter.protocol.http.control.HeaderManager;
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy; import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy;
import org.apache.jmeter.save.SaveService; import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement; import org.apache.jmeter.testelement.TestElement;
@ -61,6 +59,9 @@ public class MsHTTPElementConverter extends AbstractJmeterElementConverter<MsHTT
public static final String URL_ENCODE = "${__urlencode(%s)}"; public static final String URL_ENCODE = "${__urlencode(%s)}";
public static final String COOKIE = "Cookie"; public static final String COOKIE = "Cookie";
public static final String HTTP = "http://";
public static final String HTTPS = "https://";
@Override @Override
public void toHashTree(HashTree tree, MsHTTPElement msHTTPElement, ParameterConfig config) { public void toHashTree(HashTree tree, MsHTTPElement msHTTPElement, ParameterConfig config) {
if (BooleanUtils.isFalse(msHTTPElement.getEnable())) { if (BooleanUtils.isFalse(msHTTPElement.getEnable())) {
@ -97,6 +98,9 @@ public class MsHTTPElementConverter extends AbstractJmeterElementConverter<MsHTT
// 处理请求头 // 处理请求头
HeaderManager httpHeader = getHttpHeader(msHTTPElement, apiParamConfig, httpConfig); HeaderManager httpHeader = getHttpHeader(msHTTPElement, apiParamConfig, httpConfig);
Optional.ofNullable(httpHeader).ifPresent(httpTree::add); Optional.ofNullable(httpHeader).ifPresent(httpTree::add);
//处理host
DNSCacheManager dnsCacheManager = getEnvDns(msHTTPElement.getName(), envConfig, httpConfig);
Optional.ofNullable(dnsCacheManager).ifPresent(httpTree::add);
HTTPAuthConfig authConfig = msHTTPElement.getAuthConfig(); HTTPAuthConfig authConfig = msHTTPElement.getAuthConfig();
@ -107,6 +111,7 @@ public class MsHTTPElementConverter extends AbstractJmeterElementConverter<MsHTT
parseChild(httpTree, msHTTPElement, config); parseChild(httpTree, msHTTPElement, config);
} }
/** /**
* 设置超时时间等配置 * 设置超时时间等配置
* *
@ -310,6 +315,7 @@ public class MsHTTPElementConverter extends AbstractJmeterElementConverter<MsHTT
return headerManager; return headerManager;
} }
private void setHeaderMap(Map<String, String> headerMap, List<? extends KeyValueEnableParam> headers) { private void setHeaderMap(Map<String, String> headerMap, List<? extends KeyValueEnableParam> headers) {
if (CollectionUtils.isEmpty(headers)) { if (CollectionUtils.isEmpty(headers)) {
return; return;
@ -436,4 +442,42 @@ public class MsHTTPElementConverter extends AbstractJmeterElementConverter<MsHTT
}); });
return stringBuffer.substring(0, stringBuffer.length() - 1); return stringBuffer.substring(0, stringBuffer.length() - 1);
} }
private DNSCacheManager getEnvDns(String name , EnvironmentInfoDTO envConfig, HttpConfig httpConfig) {
if (envConfig == null ||
envConfig.getConfig() == null ||
envConfig.getConfig().getHostConfig() == null ||
BooleanUtils.isFalse(envConfig.getConfig().getHostConfig().getEnable()) ||
httpConfig == null) {
return null;
}
String domain = httpConfig.getHostname().trim();
List<Host> hosts = new ArrayList<>();
envConfig.getConfig().getHostConfig().getHosts().forEach(host -> {
if (StringUtils.isNotBlank(host.getDomain())) {
String hostDomain = host.getDomain().trim().replace(HTTP, StringUtils.EMPTY).replace(HTTPS, StringUtils.EMPTY);
if (StringUtils.equals(hostDomain, domain)) {
host.setDomain(hostDomain); // 域名去掉协议
hosts.add(host);
}
}
});
if (CollectionUtils.isNotEmpty(hosts)) {
return dnsCacheManager(name + "DNSCacheManager", hosts);
}
return null;
}
private static DNSCacheManager dnsCacheManager(String name, List<Host> hosts) {
DNSCacheManager dnsCacheManager = new DNSCacheManager();
dnsCacheManager.setEnabled(true);
dnsCacheManager.setName(name);
dnsCacheManager.setProperty(TestElement.TEST_CLASS, DNSCacheManager.class.getName());
dnsCacheManager.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("DNSCachePanel"));
dnsCacheManager.setCustomResolver(false);
dnsCacheManager.setClearEachIteration(true);
hosts.forEach(host -> dnsCacheManager.addHost(host.getDomain(), host.getIp()));
return dnsCacheManager;
}
} }

View File

@ -20,15 +20,21 @@ import io.metersphere.api.service.definition.ApiTestCaseService;
import io.metersphere.api.utils.ApiDataUtils; import io.metersphere.api.utils.ApiDataUtils;
import io.metersphere.plugin.api.spi.AbstractMsTestElement; import io.metersphere.plugin.api.spi.AbstractMsTestElement;
import io.metersphere.project.domain.ProjectVersion; import io.metersphere.project.domain.ProjectVersion;
import io.metersphere.project.dto.environment.EnvironmentConfig;
import io.metersphere.project.dto.environment.host.Host;
import io.metersphere.project.dto.environment.host.HostConfig;
import io.metersphere.project.dto.environment.http.HttpConfig;
import io.metersphere.project.dto.filemanagement.FileInfo; import io.metersphere.project.dto.filemanagement.FileInfo;
import io.metersphere.project.mapper.ProjectVersionMapper; import io.metersphere.project.mapper.ProjectVersionMapper;
import io.metersphere.project.service.FileAssociationService; import io.metersphere.project.service.FileAssociationService;
import io.metersphere.sdk.constants.*; import io.metersphere.sdk.constants.*;
import io.metersphere.sdk.domain.Environment; import io.metersphere.sdk.domain.Environment;
import io.metersphere.sdk.domain.EnvironmentBlob;
import io.metersphere.sdk.domain.EnvironmentExample; import io.metersphere.sdk.domain.EnvironmentExample;
import io.metersphere.sdk.file.FileCenter; import io.metersphere.sdk.file.FileCenter;
import io.metersphere.sdk.file.FileRequest; import io.metersphere.sdk.file.FileRequest;
import io.metersphere.sdk.file.MinioRepository; import io.metersphere.sdk.file.MinioRepository;
import io.metersphere.sdk.mapper.EnvironmentBlobMapper;
import io.metersphere.sdk.mapper.EnvironmentMapper; import io.metersphere.sdk.mapper.EnvironmentMapper;
import io.metersphere.sdk.util.BeanUtils; import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.sdk.util.CommonBeanFactory; import io.metersphere.sdk.util.CommonBeanFactory;
@ -119,6 +125,8 @@ public class ApiTestCaseControllerTests extends BaseTest {
private ApiTestCaseFollowerMapper apiTestCaseFollowerMapper; private ApiTestCaseFollowerMapper apiTestCaseFollowerMapper;
@Resource @Resource
private EnvironmentMapper environmentMapper; private EnvironmentMapper environmentMapper;
@Resource
private EnvironmentBlobMapper environmentBlobMapper;
private static String fileMetadataId; private static String fileMetadataId;
@Resource @Resource
private SqlSessionFactory sqlSessionFactory; private SqlSessionFactory sqlSessionFactory;
@ -426,7 +434,40 @@ public class ApiTestCaseControllerTests extends BaseTest {
resultHolder.getCode() == MsHttpResultCode.SUCCESS.getCode()); resultHolder.getCode() == MsHttpResultCode.SUCCESS.getCode());
// 测试执行 // 测试执行
request.setEnvironmentId("envId"); Environment environment = new Environment();
environment.setId("test-host");
environment.setProjectId(DEFAULT_PROJECT_ID);
environment.setName("test-host");
environment.setPos(0L);
environment.setCreateTime(System.currentTimeMillis());
environment.setUpdateTime(System.currentTimeMillis());
environment.setCreateUser("admin");
environment.setUpdateUser("admin");
environment.setMock(false);
environmentMapper.insert(environment);
EnvironmentConfig environmentConfig = new EnvironmentConfig();
List<HttpConfig> httpConfigs = new ArrayList<>();
HttpConfig httpConfig = new HttpConfig();
httpConfig.setHostname("www.aa.com");
httpConfig.setType("NONE");
httpConfig.setProtocol("HTTP");
httpConfigs.add(httpConfig);
environmentConfig.setHttpConfig(httpConfigs);
HostConfig hostConfig = new HostConfig();
hostConfig.setEnable(true);
List<Host> hosts = new ArrayList<>();
Host host = new Host();
host.setIp("172.0.0.1");
host.setDomain("www.aa.com");
hosts.add(host);
hostConfig.setHosts(hosts);
environmentConfig.setHostConfig(hostConfig);
EnvironmentBlob environmentBlob = new EnvironmentBlob();
environmentBlob.setId(environment.getId());
environmentBlob.setConfig(JSON.toJSONBytes(environmentConfig));
environmentBlobMapper.insert(environmentBlob);
request.setEnvironmentId("test-host");
mvcResult = this.requestPostAndReturn(RUN_POST, request); mvcResult = this.requestPostAndReturn(RUN_POST, request);
resultHolder = JSON.parseObject(mvcResult.getResponse().getContentAsString(Charset.defaultCharset()), ResultHolder.class); resultHolder = JSON.parseObject(mvcResult.getResponse().getContentAsString(Charset.defaultCharset()), ResultHolder.class);
Assertions.assertTrue(resultHolder.getCode() == ApiResultCode.RESOURCE_POOL_EXECUTE_ERROR.getCode() || Assertions.assertTrue(resultHolder.getCode() == ApiResultCode.RESOURCE_POOL_EXECUTE_ERROR.getCode() ||

View File

@ -9,7 +9,7 @@
type="line" type="line"
@change="loadScenarioList(false)" @change="loadScenarioList(false)"
/> />
<span style="margin-left: 8px; color: #323233; font-family: 'PingFang SC'">{{ <span style="margin-left: 8px; font-family: 'PingFang SC'; color: #323233">{{
t('apiScenario.table.showChildrenModuleScenario') t('apiScenario.table.showChildrenModuleScenario')
}}</span> }}</span>
</span> </span>
@ -425,14 +425,14 @@
}, },
{ {
title: 'apiScenario.table.columns.createUser', title: 'apiScenario.table.columns.createUser',
dataIndex: 'createUser', dataIndex: 'createUserName',
titleSlotName: 'createUser', titleSlotName: 'createUser',
width: 109, width: 109,
showDrag: true, showDrag: true,
}, },
{ {
title: 'apiScenario.table.columns.updateUser', title: 'apiScenario.table.columns.updateUser',
dataIndex: 'updateUser', dataIndex: 'updateUserName',
titleSlotName: 'updateUser', titleSlotName: 'updateUser',
width: 109, width: 109,
showDrag: true, showDrag: true,

View File

@ -9,7 +9,7 @@
type="line" type="line"
@change="loadScenarioList(false)" @change="loadScenarioList(false)"
/> />
<span style="margin-left: 8px; color: #323233; font-family: 'PingFang SC'">{{ <span style="margin-left: 8px; font-family: 'PingFang SC'; color: #323233">{{
t('apiScenario.table.showChildrenModuleScenario') t('apiScenario.table.showChildrenModuleScenario')
}}</span> }}</span>
</span> </span>
@ -265,14 +265,14 @@
}, },
{ {
title: 'apiScenario.table.columns.createUser', title: 'apiScenario.table.columns.createUser',
dataIndex: 'createUser', dataIndex: 'createUserName',
titleSlotName: 'createUser', titleSlotName: 'createUser',
width: 109, width: 109,
showDrag: true, showDrag: true,
}, },
{ {
title: 'apiScenario.table.columns.updateUser', title: 'apiScenario.table.columns.updateUser',
dataIndex: 'updateUser', dataIndex: 'updateUserName',
titleSlotName: 'updateUser', titleSlotName: 'updateUser',
width: 109, width: 109,
showDrag: true, showDrag: true,

View File

@ -82,7 +82,7 @@
placeholder: 'project.environmental.host.ipPlaceholder', placeholder: 'project.environmental.host.ipPlaceholder',
rules: [ rules: [
{ required: true, message: t('project.environmental.host.ipIsRequire') }, { required: true, message: t('project.environmental.host.ipIsRequire') },
{ notRepeat: true, message: 'project.environmental.host.ipNotRepeat' }, { notRepeat: true, message: t('project.environmental.host.ipNotRepeat') },
], ],
}, },
{ {