以上脚本实现了读取error值的功能,但是在jenkins上即使执行过程中产生错误,只要构建过程中每个程序的退出状态是正常的,仍然会显示构建成功,为此需要编写以下脚本,使脚本执行失败时保证该构建过程同时失败:
#!/bin/bash if grep "All interfaces have been successfully executed" result.txt then echo "result is right" exit 0 else echo "result is wrong" exit 1 fi该脚本在有脚本执行失败的情况下会强制退出状态为1,从而使得构建失败。
2.4 自动化脚本执行以及结果收集
脚本执行需要借助ptp平台的插件,具体如图所示:
执行完成后,需要获取PTP平台的执行结果,判断执行过程中是否有错误产生,具体脚本如下所示:
import os flagSucess=True path = os.getcwd() path_pertest=path path+=\'/projects\' path_curr=path f=open("/home/qatest/monitorTools/conf/topnFilesRes.txt") file = open(\'result.txt\', \'w\') info=[] for line in f: tmp=line.strip() path+="http://www.likecs.com/"+tmp info.append(path) path=path_curr for i in info: i+="/logs" os.chdir(i) fileSize = os.path.getsize("error_grinder.log") if fileSize!=0: flagSucess=False os.chdir(path_pertest) i += " make an error" file.write(i) if flagSucess: file.write("All rounds have been successfully executed")完成该部分后需要将测试结果持久化到数据库,这部分的思路是调用平台的/api/v1.0/round/${roundId}/summary接口,解析json数据,然后插入到数据库,具体代码如下。
首先需要利用httpclient获取该接口的结果然后进行解析:
public class GetRoundsAndJasonParse { @SuppressWarnings("finally") public String getJasonRes(String roundID) throws HttpException { String res=null; String prefix="http://perf.hz.netease.com/api/v1.0/round/"; prefix+=roundID; prefix+="/summary"; HttpClient client = new HttpClient(); GetMethod getMethod = new GetMethod(prefix); try { client.executeMethod(getMethod); //res = new String(getMethod.getResponseBodyAsString()); BufferedReader reader = new BufferedReader(new InputStreamReader(getMethod.getResponseBodyAsStream())); StringBuffer stringBuffer = new StringBuffer(); String str = ""; while((str = reader.readLine())!=null) { stringBuffer.append(str); } res = stringBuffer.toString(); } catch (HttpException e) { e.printStackTrace(); } finally { getMethod.releaseConnection(); return res; } } public ArrayList<Perf> getValue(JsonObject json,String[] key) { FormattingPerf fp = new FormattingPerf(); ArrayList<Perf> res=new ArrayList<Perf>(); ArrayList<String> values=new ArrayList<String>(); String machine_name=null; String test_id=null; String tmp=null; try { //if(json.containsKey(key)) String resStr = json.get("success").getAsString(); if(resStr.equals("false")) System.out.println("Check your roundID"); else { JsonArray array=json.get("data").getAsJsonArray(); for(int i=0;i<array.size();i++) { JsonObject subObject=array.get(i).getAsJsonObject(); machine_name=subObject.get("machine_name").getAsString(); test_id=subObject.get("test_id").getAsString(); if(machine_name.equals("all")&&!test_id.equals("0")) { for(int j=0;j<key.length;j++) { tmp=subObject.get(key[j]).getAsString(); values.add(tmp); } Perf perf=new Perf(values); fp.formatPerf(perf); res.add(perf); values.clear(); } } } } catch (Exception e) { e.printStackTrace(); } return res; } @SuppressWarnings("finally") public ArrayList<Perf> parseJason(String jasonbody) throws JsonIOException, JsonSyntaxException { //ArrayList<String> res=new ArrayList<String>(); ArrayList<Perf> res=new ArrayList<Perf>(); JsonParser parse =new JsonParser(); try { JsonObject json=(JsonObject) parse.parse(jasonbody); String[] key={"test_id","perf_round_id","tps","response_ave","response90","err_rate","mean_response_length"}; res=getValue(json,key); } catch (JsonIOException e) { e.printStackTrace(); } catch (JsonSyntaxException e) { e.printStackTrace(); } finally { return res; } }