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; package io.metersphere.api.dto.definition.request.sampler;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.annotation.JSONField; import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.annotation.JSONType; import com.alibaba.fastjson.annotation.JSONType;
import com.fasterxml.jackson.core.type.TypeReference; 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.ParameterConfig;
import io.metersphere.api.dto.definition.request.auth.MsAuthManager; 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.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.Body;
import io.metersphere.api.dto.scenario.HttpConfig; import io.metersphere.api.dto.scenario.HttpConfig;
import io.metersphere.api.dto.scenario.HttpConfigCondition; import io.metersphere.api.dto.scenario.HttpConfigCondition;
@ -120,6 +123,9 @@ public class MsHTTPSamplerProxy extends MsTestElement {
@JSONField(ordinal = 38) @JSONField(ordinal = 38)
private String alias; private String alias;
private MsJSR223PreProcessor preProcessor;
private MsJSR223PostProcessor postProcessor;
private void setRefElement() { private void setRefElement() {
try { try {
ApiDefinitionService apiDefinitionService = CommonBeanFactory.getBean(ApiDefinitionService.class); ApiDefinitionService apiDefinitionService = CommonBeanFactory.getBean(ApiDefinitionService.class);
@ -260,6 +266,27 @@ public class MsHTTPSamplerProxy extends MsTestElement {
addCertificate(config, httpSamplerTree); 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)) { if (CollectionUtils.isNotEmpty(hashTree)) {
for (MsTestElement el : hashTree) { for (MsTestElement el : hashTree) {
if (el.getEnvironmentId() == null) { if (el.getEnvironmentId() == null) {
@ -303,11 +330,24 @@ public class MsHTTPSamplerProxy extends MsTestElement {
private HttpConfig getHttpConfig(ParameterConfig config) { private HttpConfig getHttpConfig(ParameterConfig config) {
if (config.isEffective(this.getProjectId())) { if (config.isEffective(this.getProjectId())) {
String useEvnId = config.getConfig().get(this.getProjectId()).getApiEnvironmentid(); EnvironmentConfig environmentConfig = config.getConfig().get(this.getProjectId());
if (StringUtils.isNotEmpty(useEvnId) && !StringUtils.equals(useEvnId, this.getEnvironmentId())) { if (environmentConfig != null){
this.setEnvironmentId(useEvnId); 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 getHttpConfig(config.getConfig().get(this.getProjectId()).getHttpConfig());
} }
return null; return null;
} }

View File

@ -9,6 +9,8 @@ import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import io.metersphere.api.dto.definition.request.MsTestElement; import io.metersphere.api.dto.definition.request.MsTestElement;
import io.metersphere.api.dto.definition.request.ParameterConfig; 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.DatabaseConfig;
import io.metersphere.api.dto.scenario.KeyValue; import io.metersphere.api.dto.scenario.KeyValue;
import io.metersphere.api.dto.scenario.environment.EnvironmentConfig; import io.metersphere.api.dto.scenario.environment.EnvironmentConfig;
@ -68,6 +70,9 @@ public class MsJDBCSampler extends MsTestElement {
@JSONField(ordinal = 30) @JSONField(ordinal = 30)
private String useEnvironment; private String useEnvironment;
private MsJSR223PreProcessor preProcessor;
private MsJSR223PostProcessor postProcessor;
@Override @Override
public void toHashTree(HashTree tree, List<MsTestElement> hashTree, ParameterConfig config) { public void toHashTree(HashTree tree, List<MsTestElement> hashTree, ParameterConfig config) {
// 非导出操作且不是启用状态则跳过执行 // 非导出操作且不是启用状态则跳过执行
@ -111,8 +116,15 @@ public class MsJDBCSampler extends MsTestElement {
} else { } else {
this.dataSource = null; this.dataSource = null;
// 取当前环境下默认的一个数据源 // 取当前环境下默认的一个数据源
if (config.isEffective(this.getProjectId()) && CollectionUtils.isNotEmpty(config.getConfig().get(this.getProjectId()).getDatabaseConfigs())) { if (config.isEffective(this.getProjectId())) {
this.dataSource = config.getConfig().get(this.getProjectId()).getDatabaseConfigs().get(0); 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) { if (arguments != null) {
tree.add(arguments); 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)) { if (CollectionUtils.isNotEmpty(hashTree)) {
hashTree.forEach(el -> { hashTree.forEach(el -> {
el.toHashTree(samplerHashTree, el.getHashTree(), config); 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.automation.TcpTreeTableDataStruct;
import io.metersphere.api.dto.definition.request.MsTestElement; import io.metersphere.api.dto.definition.request.MsTestElement;
import io.metersphere.api.dto.definition.request.ParameterConfig; 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.definition.request.processors.pre.MsJSR223PreProcessor;
import io.metersphere.api.dto.scenario.KeyValue; import io.metersphere.api.dto.scenario.KeyValue;
import io.metersphere.api.dto.scenario.environment.EnvironmentConfig; import io.metersphere.api.dto.scenario.environment.EnvironmentConfig;
@ -98,6 +99,8 @@ public class MsTCPSampler extends MsTestElement {
@JSONField(ordinal = 44) @JSONField(ordinal = 44)
private String rawDataStruct; private String rawDataStruct;
private MsJSR223PreProcessor preProcessor;
private MsJSR223PostProcessor postProcessor;
/** /**
* 新加两个参数场景保存/修改时需要的参数不会传递JMeter只是用于最后的保留 * 新加两个参数场景保存/修改时需要的参数不会传递JMeter只是用于最后的保留
@ -136,6 +139,28 @@ public class MsTCPSampler extends MsTestElement {
if (tcpPreProcessor != null && StringUtils.isNotBlank(tcpPreProcessor.getScript())) { if (tcpPreProcessor != null && StringUtils.isNotBlank(tcpPreProcessor.getScript())) {
samplerHashTree.add(tcpPreProcessor.getJSR223PreProcessor()); 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)) { if (CollectionUtils.isNotEmpty(hashTree)) {
hashTree.forEach(el -> { hashTree.forEach(el -> {
el.toHashTree(samplerHashTree, el.getHashTree(), config); el.toHashTree(samplerHashTree, el.getHashTree(), config);
@ -181,17 +206,22 @@ public class MsTCPSampler extends MsTestElement {
} }
private void parseEnvironment(EnvironmentConfig config) { private void parseEnvironment(EnvironmentConfig config) {
if (!isCustomizeReq() && config != null && config.getTcpConfig() != null) { if(config != null){
this.server = config.getTcpConfig().getServer(); this.preProcessor = config.getPreProcessor();
this.port = config.getTcpConfig().getPort(); this.postProcessor = config.getPostProcessor();
if (StringUtils.equals(this.eolByte, " ")) { }
this.eolByte = ""; if (!isCustomizeReq() && config != null) {
} else { if (!isCustomizeReq() && config != null) {
if (StringUtils.isEmpty(this.eolByte)) { this.server = config.getTcpConfig().getServer();
this.eolByte = config.getTcpConfig().getEolByte(); this.port = config.getTcpConfig().getPort();
if (StringUtils.equals(this.eolByte, " ")) {
this.eolByte = "";
} else {
if (StringUtils.isEmpty(this.eolByte)) {
this.eolByte = config.getTcpConfig().getEolByte();
}
} }
} }
} }
} }

View File

@ -1,5 +1,8 @@
package io.metersphere.api.dto.scenario.environment; 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.DatabaseConfig;
import io.metersphere.api.dto.scenario.HttpConfig; import io.metersphere.api.dto.scenario.HttpConfig;
import io.metersphere.api.dto.scenario.TCPConfig; import io.metersphere.api.dto.scenario.TCPConfig;
@ -17,6 +20,9 @@ public class EnvironmentConfig {
private List<DatabaseConfig> databaseConfigs; private List<DatabaseConfig> databaseConfigs;
private TCPConfig tcpConfig; private TCPConfig tcpConfig;
private KeyStoreConfig sslConfig; private KeyStoreConfig sslConfig;
private MsJSR223PostProcessor postProcessor;
private MsJSR223PreProcessor preProcessor;
private JSONObject authManager;
public EnvironmentConfig() { public EnvironmentConfig() {
this.commonConfig = new CommonConfig(); this.commonConfig = new CommonConfig();

View File

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

View File

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

View File

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

View File

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

View File

@ -10,7 +10,14 @@
<el-tabs v-model="activeName"> <el-tabs v-model="activeName">
<el-tab-pane :label="$t('api_test.environment.common_config')" name="common"> <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"/> <ms-environment-common-config :common-config="environment.config.commonConfig" ref="commonConfig" :is-read-only="isReadOnly"/>
</el-tab-pane> </el-tab-pane>
@ -26,8 +33,20 @@
<el-tab-pane :label="$t('commons.ssl.config')" name="ssl"> <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"/> <ms-environment-s-s-l-config :project-id="projectId" :ssl-config="environment.config.sslConfig" :is-read-only="isReadOnly"/>
</el-tab-pane> </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> </el-tabs>
<div class="environment-footer"> <div class="environment-footer">
<ms-dialog-footer <ms-dialog-footer
@cancel="cancel" @cancel="cancel"
@ -48,14 +67,19 @@
import MsEnvironmentHttpConfig from "./EnvironmentHttpConfig"; import MsEnvironmentHttpConfig from "./EnvironmentHttpConfig";
import MsEnvironmentCommonConfig from "./EnvironmentCommonConfig"; import MsEnvironmentCommonConfig from "./EnvironmentCommonConfig";
import MsEnvironmentSSLConfig from "./EnvironmentSSLConfig"; 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 MsTcpConfig from "@/business/components/api/test/components/request/tcp/TcpConfig";
import {getUUID} from "@/common/js/utils"; 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 { export default {
name: "EnvironmentEdit", name: "EnvironmentEdit",
components: { components: {
MsTcpConfig, MsTcpConfig,
MsApiAuthConfig,
Jsr233ProcessorContent,
MsEnvironmentCommonConfig, MsEnvironmentCommonConfig,
MsEnvironmentHttpConfig, MsEnvironmentHttpConfig,
MsEnvironmentSSLConfig, MsEnvironmentSSLConfig,
@ -70,10 +94,10 @@
}, },
}, },
data() { data() {
return { return {
result: {}, result: {},
envEnable: false, envEnable: false,
isRefresh: true,
rules: { rules: {
name: [ name: [
{required: true, message: this.$t('commons.input_name'), trigger: 'blur'}, {required: true, message: this.$t('commons.input_name'), trigger: 'blur'},
@ -84,8 +108,47 @@
activeName: 'common' 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: { watch: {
environment: function (o) { 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; this.envEnable = o.enable;
} }
}, },

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