如何通过 API 触发 MeterSphere 测试计划
  ytXF8ujKKYXO 2023年11月02日 48 0

1 关于 MS 接口的API调用

1、请求的 HTTP 头要设置两个值:accessKey 和签名

2、加密方式:对称加密

3、加密算法:AES,加密模式:CBC,填充方式:PKCS5Padding

4、签名中加了时间戳,也就是说生成的签名只在特定的时间范围内有效,默认 30 分钟

2 具体步骤

1、首先需要知道 MS 的 accessKey 和 secretKey来生成 signature,ak 和 sk 可以在个人信息中生成

如何通过 API 触发 MeterSphere 测试计划_MeterSphere api 测计划

2、还需要一个“浏览器”( POST 请求),去执行我们的测试计划

/**
* http post 请求
*
* @param url
* @param json
* @param config
* @return
*/
public static String post(String url, String json, HttpClientConfig config) {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(url);
if (config == null) {
config = new HttpClientConfig();
}
try {
httpPost.setConfig(config.buildRequestConfig());

Map<String, String> header = config.getHeader();
for (String key : header.keySet()) {
httpPost.addHeader(key, header.get(key));
}
httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json");
httpPost.addHeader(HTTP.CONTENT_ENCODING, config.getCharset());

EntityBuilder entityBuilder = EntityBuilder.create();
entityBuilder.setText(json);
entityBuilder.setContentType(ContentType.APPLICATION_JSON);
entityBuilder.setContentEncoding(config.getCharset());
HttpEntity requestEntity = entityBuilder.build();
httpPost.setEntity(requestEntity);

HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity, config.getCharset());
} catch (Exception e) {
// logger.error("HttpClient查询失败", e);
throw new RuntimeException("HttpClient查询失败", e);
} finally {
try {
httpClient.close();
} catch (Exception e) {
// logger.error("HttpClient关闭连接失败", e);
}
}
}

3、定义全部变量,分别写 ak,sk 和 ms 地址

   //MS accessKey,可在个人信息生成
private static String accessKey = "xxxxxx";
//MS secretKey,可在个人信息生成
private static String secretKey = "xxxxxx";
//MS服务器地址
private static String endpointUrl = "https://xxxxxxxxxx";

4、用 ak 和 sk 来生成 signatur的算法

 /**
* 签名算法
*
* @param src
* @param secretKey
* @param iv
* @return
* @throws Exception
*/
private static String aesEncrypt(String src, String secretKey, String iv) throws Exception {
byte[] raw = secretKey.getBytes(StandardCharsets.UTF_8);
SecretKeySpec secretKeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec iv1 = new IvParameterSpec(iv.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, iv1);
byte[] encrypted = cipher.doFinal(src.getBytes(StandardCharsets.UTF_8));
return Base64.encodeBase64String(encrypted);
}

5、生成签名,并设置到 header

/**
* 生成签名,并设置到header
*
* @return
*/
private static HttpClientConfig auth() {
HttpClientConfig httpClientConfig = new HttpClientConfig();
httpClientConfig.addHeader("Accept", "application/json;charset=UTF-8");
httpClientConfig.addHeader("accessKey", accessKey);
httpClientConfig.addHeader("Content-type", "application/json");
String signature;
try {
signature = aesEncrypt(accessKey + "|" + UUID.randomUUID().toString()
+ "|" + System.currentTimeMillis(), secretKey, accessKey);
} catch (Exception e) {
LogUtil.error(e.getMessage(), e);
throw new MeterSphereException("签名失败: " + e.getMessage());
}
httpClientConfig.addHeader("signature", signature);
return httpClientConfig;
}

6、设置请求测试计划接口需要的参数

/**
* 调起测试计划
*/
public static void execTestPlan() {
//项目Id,必填
String projectId = "xxxxxxxxxx";
//运行环境Id,非必填
String runEnvironmentId = "";
String testPlanId = "xxxxxxxxx";
String userId = "xxxx";
//串行还是并行,serial---串行,parallel---并行
String mode = "parallel";
//资源池Id,非必填
String resourcePoolId = "";

Map<String, String> envMap = new HashMap<>();
envMap.put(projectId, runEnvironmentId);
Map<String, Object> params = new HashMap<>();
params.put("testPlanId", testPlanId);
params.put("projectId", projectId);
params.put("triggerMode", "API");
params.put("userId", userId);
params.put("mode", mode);
params.put("resourcePoolId", resourcePoolId);
params.put("runWithinResourcePool", true);
params.put("onSampleError", false);
params.put("reportType", "iddReport");
params.put("envMap", envMap);
params.put("environmentGroupId", "");
params.put("environmentType", "JSON");

System.out.println("测试计划执行param" + JSON.toJSONString(params));
String responseJson = post(endpointUrl + "/test/plan/run/", JSON.toJSONString(params), auth());
System.out.println("执行结果:" + responseJson);
}

7、main 方法执行

 public static void main(String[] args) {
//执行测试计划
execTestPlan();
}

8、控制台执行结果

如何通过 API 触发 MeterSphere 测试计划_MeterSphere api 测计划_02

9、然后我们去 MS 服务看一下,确实已经执行成功了

如何通过 API 触发 MeterSphere 测试计划_MeterSphere api 测计划_03

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

ytXF8ujKKYXO
最新推荐 更多