feat(环境配置): #1002361 环境配置中增加全局前/后置脚本、认证配置

【环境配置中增加全局前/后置脚本】https://www.tapd.cn/55049933/prong/stories/view/1155049933001002361
This commit is contained in:
song-tianyang 2021-09-02 14:33:36 +08:00 committed by 刘瑞斌
parent 560a2466d2
commit 2cd2cfe01b
10 changed files with 213 additions and 39 deletions

View File

@ -1,6 +1,7 @@
package io.metersphere.api.dto.definition.request.sampler;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.annotation.JSONType;
import com.fasterxml.jackson.core.type.TypeReference;
@ -10,6 +11,8 @@ import io.metersphere.api.dto.definition.request.MsTestElement;
import io.metersphere.api.dto.definition.request.ParameterConfig;
import io.metersphere.api.dto.definition.request.auth.MsAuthManager;
import io.metersphere.api.dto.definition.request.dns.MsDNSCacheManager;
import io.metersphere.api.dto.definition.request.processors.post.MsJSR223PostProcessor;
import io.metersphere.api.dto.definition.request.processors.pre.MsJSR223PreProcessor;
import io.metersphere.api.dto.scenario.Body;
import io.metersphere.api.dto.scenario.HttpConfig;
import io.metersphere.api.dto.scenario.HttpConfigCondition;
@ -120,6 +123,9 @@ public class MsHTTPSamplerProxy extends MsTestElement {
@JSONField(ordinal = 38)
private String alias;
private MsJSR223PreProcessor preProcessor;
private MsJSR223PostProcessor postProcessor;
private void setRefElement() {
try {
ApiDefinitionService apiDefinitionService = CommonBeanFactory.getBean(ApiDefinitionService.class);
@ -260,6 +266,27 @@ public class MsHTTPSamplerProxy extends MsTestElement {
addCertificate(config, httpSamplerTree);
//增加全局前后至脚本
if(this.preProcessor != null){
if (this.preProcessor.getEnvironmentId() == null) {
if (this.getEnvironmentId() == null) {
this.preProcessor.setEnvironmentId(useEnvironment);
} else {
this.preProcessor.setEnvironmentId(this.getEnvironmentId());
}
}
this.preProcessor.toHashTree(httpSamplerTree, this.preProcessor.getHashTree(), config);
}
if(this.postProcessor != null){
if (this.postProcessor.getEnvironmentId() == null) {
if (this.getEnvironmentId() == null) {
this.postProcessor.setEnvironmentId(useEnvironment);
} else {
this.postProcessor.setEnvironmentId(this.getEnvironmentId());
}
}
this.postProcessor.toHashTree(httpSamplerTree, this.postProcessor.getHashTree(), config);
}
if (CollectionUtils.isNotEmpty(hashTree)) {
for (MsTestElement el : hashTree) {
if (el.getEnvironmentId() == null) {
@ -303,12 +330,25 @@ public class MsHTTPSamplerProxy extends MsTestElement {
private HttpConfig getHttpConfig(ParameterConfig config) {
if (config.isEffective(this.getProjectId())) {
String useEvnId = config.getConfig().get(this.getProjectId()).getApiEnvironmentid();
EnvironmentConfig environmentConfig = config.getConfig().get(this.getProjectId());
if (environmentConfig != null){
String useEvnId = environmentConfig.getApiEnvironmentid();
this.preProcessor = environmentConfig.getPreProcessor();
this.postProcessor = environmentConfig.getPostProcessor();
if(this.authManager == null && environmentConfig.getAuthManager() != null && environmentConfig.getAuthManager().containsKey("hashTree") ){
try {
JSONArray jsonArray = environmentConfig.getAuthManager().getJSONArray("hashTree");
if(jsonArray.size() > 0){
this.authManager = jsonArray.getJSONObject(0).toJavaObject(MsAuthManager.class);
}
}catch (Exception e){}
}
if (StringUtils.isNotEmpty(useEvnId) && !StringUtils.equals(useEvnId, this.getEnvironmentId())) {
this.setEnvironmentId(useEvnId);
}
return getHttpConfig(config.getConfig().get(this.getProjectId()).getHttpConfig());
}
}
return null;
}

View File

@ -9,6 +9,8 @@ import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.metersphere.api.dto.definition.request.MsTestElement;
import io.metersphere.api.dto.definition.request.ParameterConfig;
import io.metersphere.api.dto.definition.request.processors.post.MsJSR223PostProcessor;
import io.metersphere.api.dto.definition.request.processors.pre.MsJSR223PreProcessor;
import io.metersphere.api.dto.scenario.DatabaseConfig;
import io.metersphere.api.dto.scenario.KeyValue;
import io.metersphere.api.dto.scenario.environment.EnvironmentConfig;
@ -68,6 +70,9 @@ public class MsJDBCSampler extends MsTestElement {
@JSONField(ordinal = 30)
private String useEnvironment;
private MsJSR223PreProcessor preProcessor;
private MsJSR223PostProcessor postProcessor;
@Override
public void toHashTree(HashTree tree, List<MsTestElement> hashTree, ParameterConfig config) {
// 非导出操作且不是启用状态则跳过执行
@ -111,8 +116,15 @@ public class MsJDBCSampler extends MsTestElement {
} else {
this.dataSource = null;
// 取当前环境下默认的一个数据源
if (config.isEffective(this.getProjectId()) && CollectionUtils.isNotEmpty(config.getConfig().get(this.getProjectId()).getDatabaseConfigs())) {
this.dataSource = config.getConfig().get(this.getProjectId()).getDatabaseConfigs().get(0);
if (config.isEffective(this.getProjectId())) {
if(config.getConfig().get(this.getProjectId()) != null){
EnvironmentConfig envConfig = config.getConfig().get(this.getProjectId());
this.preProcessor = envConfig.getPreProcessor();
this.postProcessor = envConfig.getPostProcessor();
if(CollectionUtils.isNotEmpty(envConfig.getDatabaseConfigs())){
this.dataSource = envConfig.getDatabaseConfigs().get(0);
}
}
}
}
@ -132,6 +144,29 @@ public class MsJDBCSampler extends MsTestElement {
if (arguments != null) {
tree.add(arguments);
}
//增加全局前后至脚本
if(this.preProcessor != null){
if (this.preProcessor.getEnvironmentId() == null) {
if (this.getEnvironmentId() == null) {
this.preProcessor.setEnvironmentId(useEnvironment);
} else {
this.preProcessor.setEnvironmentId(this.getEnvironmentId());
}
}
this.preProcessor.toHashTree(samplerHashTree, this.preProcessor.getHashTree(), config);
}
if(this.postProcessor != null){
if (this.postProcessor.getEnvironmentId() == null) {
if (this.getEnvironmentId() == null) {
this.postProcessor.setEnvironmentId(useEnvironment);
} else {
this.postProcessor.setEnvironmentId(this.getEnvironmentId());
}
}
this.postProcessor.toHashTree(samplerHashTree, this.postProcessor.getHashTree(), config);
}
if (CollectionUtils.isNotEmpty(hashTree)) {
hashTree.forEach(el -> {
el.toHashTree(samplerHashTree, el.getHashTree(), config);
@ -211,6 +246,10 @@ public class MsJDBCSampler extends MsTestElement {
}
});
}
if(envConfig != null){
this.preProcessor = envConfig.getPreProcessor();
this.postProcessor = envConfig.getPostProcessor();
}
}
}

View File

@ -10,6 +10,7 @@ import io.metersphere.api.dto.automation.EsbDataStruct;
import io.metersphere.api.dto.automation.TcpTreeTableDataStruct;
import io.metersphere.api.dto.definition.request.MsTestElement;
import io.metersphere.api.dto.definition.request.ParameterConfig;
import io.metersphere.api.dto.definition.request.processors.post.MsJSR223PostProcessor;
import io.metersphere.api.dto.definition.request.processors.pre.MsJSR223PreProcessor;
import io.metersphere.api.dto.scenario.KeyValue;
import io.metersphere.api.dto.scenario.environment.EnvironmentConfig;
@ -98,6 +99,8 @@ public class MsTCPSampler extends MsTestElement {
@JSONField(ordinal = 44)
private String rawDataStruct;
private MsJSR223PreProcessor preProcessor;
private MsJSR223PostProcessor postProcessor;
/**
* 新加两个参数场景保存/修改时需要的参数不会传递JMeter只是用于最后的保留
@ -136,6 +139,28 @@ public class MsTCPSampler extends MsTestElement {
if (tcpPreProcessor != null && StringUtils.isNotBlank(tcpPreProcessor.getScript())) {
samplerHashTree.add(tcpPreProcessor.getJSR223PreProcessor());
}
//增加全局前后至脚本
if(this.preProcessor != null){
if (this.preProcessor.getEnvironmentId() == null) {
if (this.getEnvironmentId() == null) {
this.preProcessor.setEnvironmentId(useEnvironment);
} else {
this.preProcessor.setEnvironmentId(this.getEnvironmentId());
}
}
this.preProcessor.toHashTree(samplerHashTree, this.preProcessor.getHashTree(), config);
}
if(this.postProcessor != null){
if (this.postProcessor.getEnvironmentId() == null) {
if (this.getEnvironmentId() == null) {
this.postProcessor.setEnvironmentId(useEnvironment);
} else {
this.postProcessor.setEnvironmentId(this.getEnvironmentId());
}
}
this.postProcessor.toHashTree(samplerHashTree, this.postProcessor.getHashTree(), config);
}
if (CollectionUtils.isNotEmpty(hashTree)) {
hashTree.forEach(el -> {
el.toHashTree(samplerHashTree, el.getHashTree(), config);
@ -181,7 +206,12 @@ public class MsTCPSampler extends MsTestElement {
}
private void parseEnvironment(EnvironmentConfig config) {
if (!isCustomizeReq() && config != null && config.getTcpConfig() != null) {
if(config != null){
this.preProcessor = config.getPreProcessor();
this.postProcessor = config.getPostProcessor();
}
if (!isCustomizeReq() && config != null) {
if (!isCustomizeReq() && config != null) {
this.server = config.getTcpConfig().getServer();
this.port = config.getTcpConfig().getPort();
if (StringUtils.equals(this.eolByte, " ")) {
@ -191,7 +221,7 @@ public class MsTCPSampler extends MsTestElement {
this.eolByte = config.getTcpConfig().getEolByte();
}
}
}
}
}

View File

@ -1,5 +1,8 @@
package io.metersphere.api.dto.scenario.environment;
import com.alibaba.fastjson.JSONObject;
import io.metersphere.api.dto.definition.request.processors.post.MsJSR223PostProcessor;
import io.metersphere.api.dto.definition.request.processors.pre.MsJSR223PreProcessor;
import io.metersphere.api.dto.scenario.DatabaseConfig;
import io.metersphere.api.dto.scenario.HttpConfig;
import io.metersphere.api.dto.scenario.TCPConfig;
@ -17,6 +20,9 @@ public class EnvironmentConfig {
private List<DatabaseConfig> databaseConfigs;
private TCPConfig tcpConfig;
private KeyStoreConfig sslConfig;
private MsJSR223PostProcessor postProcessor;
private MsJSR223PreProcessor preProcessor;
private JSONObject authManager;
public EnvironmentConfig() {
this.commonConfig = new CommonConfig();

View File

@ -34,7 +34,7 @@ public class TCPServer implements Runnable {
}
public boolean isSocketOpen(){
if (this.serverSocket != null && !this.serverSocket.isClosed()) {
if (this.serverSocket != null && !this.serverSocket.isClosed() &&this.servicer != null) {
return true;
}else {
return false;
@ -55,6 +55,7 @@ public class TCPServer implements Runnable {
try {
this.openSocket();
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@ -28,27 +28,20 @@ public class TCPServicer {
os = s.getOutputStream();
int len = is.read(b);
message = new String(b,0,len);
// } catch (Exception e) {
// e.printStackTrace();
// }
returnMsg = this.getReturnMsg(message);
// try {
os.write(returnMsg.getBytes());
} catch (Exception e) {
e.printStackTrace();
}finally {
this.close();
}
//关闭资源
// this.close();
}
private String getReturnMsg(String message) {
MockConfigService mockConfigService = CommonBeanFactory.getBean(MockConfigService.class);
MockExpectConfigWithBLOBs matchdMockExpect = mockConfigService.matchTcpMockExpect(message,this.port);
String returnMsg = "";
if(matchdMockExpect != null){
String response = matchdMockExpect.getResponse();
JSONObject responseObj = JSONObject.parseObject(response);
try {
@ -57,8 +50,8 @@ public class TCPServicer {
} catch (InterruptedException e) {
e.printStackTrace();
}
String returnMsg = responseObj.getString("body");
returnMsg = responseObj.getString("body");
}
return returnMsg;
}

View File

@ -79,6 +79,9 @@ public class ZipUtils {
BufferedReader bufferedReder = new BufferedReader(fileReader);
StringBuilder stringBuffer = new StringBuilder();
while (bufferedReder.ready()) {
if(stringBuffer.length() > 0){
stringBuffer.append("\r\n");
}
stringBuffer.append(bufferedReder.readLine());
}
// 打开的文件需关闭在unix下可以删除否则在windows下不能删除file.delete())

View File

@ -9,7 +9,6 @@
<el-tabs v-model="activeName">
<el-tab-pane :label="$t('api_test.environment.common_config')" name="common">
<ms-environment-common-config :common-config="environment.config.commonConfig" ref="commonConfig" :is-read-only="isReadOnly"/>
</el-tab-pane>

View File

@ -10,6 +10,13 @@
<el-tabs v-model="activeName">
<el-tab-pane :label="$t('api_test.definition.request.pre_script')" name="prescript">
<jsr233-processor-content v-if="isRefresh"
:jsr223-processor="environment.config.preProcessor"
:is-pre-processor="true"
:is-read-only="isReadOnly"/>
</el-tab-pane>
<el-tab-pane :label="$t('api_test.environment.common_config')" name="common">
<ms-environment-common-config :common-config="environment.config.commonConfig" ref="commonConfig" :is-read-only="isReadOnly"/>
</el-tab-pane>
@ -26,8 +33,20 @@
<el-tab-pane :label="$t('commons.ssl.config')" name="ssl">
<ms-environment-s-s-l-config :project-id="projectId" :ssl-config="environment.config.sslConfig" :is-read-only="isReadOnly"/>
</el-tab-pane>
<el-tab-pane :label="$t('api_test.definition.request.post_script')" name="postscript">
<jsr233-processor-content v-if="isRefresh"
:jsr223-processor="environment.config.postProcessor"
:is-pre-processor="false"
:is-read-only="false"/>
</el-tab-pane>
<!-- 认证配置 -->
<el-tab-pane :label="$t('api_test.definition.request.auth_config')" name="authConfig" v-if="isRefresh">
<el-tooltip class="item-tabs" effect="dark" :content="$t('api_test.definition.request.auth_config_info')" placement="top-start" slot="label">
<span>{{$t('api_test.definition.request.auth_config')}}</span>
</el-tooltip>
<ms-api-auth-config :is-read-only="isReadOnly" :request="environment.config.authManager"/>
</el-tab-pane>
</el-tabs>
<div class="environment-footer">
<ms-dialog-footer
@cancel="cancel"
@ -48,14 +67,19 @@
import MsEnvironmentHttpConfig from "./EnvironmentHttpConfig";
import MsEnvironmentCommonConfig from "./EnvironmentCommonConfig";
import MsEnvironmentSSLConfig from "./EnvironmentSSLConfig";
import MsApiAuthConfig from "@/business/components/api/definition/components/auth/ApiAuthConfig";
import MsTcpConfig from "@/business/components/api/test/components/request/tcp/TcpConfig";
import {getUUID} from "@/common/js/utils";
import Jsr233ProcessorContent from "@/business/components/api/automation/scenario/common/Jsr233ProcessorContent";
import {createComponent} from "@/business/components/api/definition/components/jmeter/components";
export default {
name: "EnvironmentEdit",
components: {
MsTcpConfig,
MsApiAuthConfig,
Jsr233ProcessorContent,
MsEnvironmentCommonConfig,
MsEnvironmentHttpConfig,
MsEnvironmentSSLConfig,
@ -70,10 +94,10 @@
},
},
data() {
return {
result: {},
envEnable: false,
isRefresh: true,
rules: {
name: [
{required: true, message: this.$t('commons.input_name'), trigger: 'blur'},
@ -84,8 +108,47 @@
activeName: 'common'
}
},
created() {
if(!this.environment.config.preProcessor){
this.environment.config.preProcessor = createComponent("JDBCPreProcessor");
}
if(!this.environment.config.postProcessor){
this.environment.config.postProcessor = createComponent("JSR223PostProcessor");
}
if(!this.environment.config.authManager){
this.environment.config.authManager = {'hashTree':[]};
}
if(!this.environment.config.authManager.hashTree){
this.environment.config.authManager.hashTree = [];
}
},
watch: {
environment: function (o) {
if(!this.environment.config.preProcessor){
this.environment.config.preProcessor = createComponent("JDBCPreProcessor");
if(!this.environment.config.preProcessor.script){
this.environment.config.preProcessor.script = "";
}
}
if(!this.environment.config.postProcessor){
this.environment.config.postProcessor = createComponent("JSR223PostProcessor");
if(!this.environment.config.postProcessor.script){
this.environment.config.postProcessor.script = "";
}
}
if(!this.environment.config.authManager){
this.environment.config.authManager = {'hashTree':[]};
}
if(!this.environment.config.authManager.hashTree){
this.environment.config.authManager.hashTree = [];
}
this.isRefresh = false;
this.$nextTick(() => {
this.isRefresh = true;
});
this.envEnable = o.enable;
}
},

@ -1 +1 @@
Subproject commit 8da3e5d33a1aff686266c86a380b626433476f1a
Subproject commit a9eedda56ce800f1d5313db0fb7ff398b91f6fc5