Commit 92399ded authored by 马博's avatar 马博

add serviceDoctor

parent 4e2cca80
......@@ -214,6 +214,7 @@
<sendPlatform>false</sendPlatform>
<run>${run}</run>
<maxRetryCount>0</maxRetryCount>
<ServiceDoctor>false</ServiceDoctor>
</systemPropertyVariables>
</configuration>
......
......@@ -12,6 +12,7 @@ public class SFExpress {
private final static List<String> exceptionServices=new ArrayList<>();
private final static List<String> exceptionMessage=new ArrayList<>();
private final static Set<String> toBeTestedServices=new HashSet<>();
public static int skip;
public static void addToBeTestedServices(Set<String> services){
toBeTestedServices.addAll(services);
}
......
package base;
import java.util.Iterator;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestNGMethod;
import org.testng.ITestResult;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.Environment;
import org.testng.*;
import utils.ServiceDoctor;
/**
* Created by mabo on 2018/9/29
......@@ -33,7 +35,9 @@ public class TestListener implements ITestListener {
public void onTestStart(ITestResult result) {
// TODO Auto-generated method stub
logger.info("Current Thread Id: " + Thread.currentThread().getId());
logger.info("on " + Thread.currentThread().getId());
}
public void onTestSuccess(ITestResult result) {
......@@ -55,5 +59,35 @@ public class TestListener implements ITestListener {
public void onStart(ITestContext context) {
// TODO Auto-generated method stub
logger.info("onStart~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
boolean ServiceDoctor=Boolean.parseBoolean(System.getProperty("ServiceDoctor"));
if(ServiceDoctor){
try{
serviceDoctor();
}catch (SkipException e){
SFExpress.skip = context.getAllTestMethods().length;
throw e;
}
}
}
public void serviceDoctor(){
logger.info("duiba-server开始检测");
ServiceDoctor serviceDoctor=new ServiceDoctor("duiba-server.properties");
SFExpress.addToBeTestedServices(serviceDoctor.getToBeTestedServices());
//fromMap.putAll(serviceDoctor.getLogLenth());
SFExpress.addExceptionService(serviceDoctor.getExceptionService(60*2));
logger.info("duiba-server检测完毕");
logger.info("duiba-server-center开始检测");
serviceDoctor=new ServiceDoctor("duiba-server-center.properties");
SFExpress.addToBeTestedServices(serviceDoctor.getToBeTestedServices());
//fromMap.putAll(serviceDoctor.getLogLenth());
SFExpress.addExceptionService(serviceDoctor.getExceptionService(60*2));
logger.info("duiba-server-center检测完毕");
if(SFExpress.getExceptionServices().size()>0){
logger.info("服务未启动:"+SFExpress.getExceptionServices().toString());
throw new SkipException("被测服务未全部启动");
}
}
}
\ No newline at end of file
......@@ -30,7 +30,13 @@ public class TestReport implements IReporter{
total = context.getAllTestMethods().length;
passCount = context.getPassedTests().size();
failCount = context.getFailedTests().size();
skipCount = context.getSkippedTests().size();
if(SFExpress.getExceptionServices().size()>0){
skipCount = SFExpress.skip;
}else{
skipCount = context.getSkippedTests().size();
}
System.out.println("report skip :"+skipCount);
for (ITestNGMethod m:
context.getFailedTests().getAllMethods()) {
String[] classes = m.getTestClass().toString().split(" ");
......
......@@ -6,10 +6,13 @@ import base.MysqlConnPool;
import http.service.testservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.testng.Assert;
import org.testng.SkipException;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
import java.sql.SQLException;
import java.util.Map;
import java.util.Properties;
import java.util.Random;
/**
......@@ -49,7 +52,9 @@ public class http_Test extends DuibaBase{
@Test
public void test4() throws InterruptedException, SQLException {
Thread.sleep(1000);
System.out.println(4);
Properties prop = System.getProperties();
String OS = prop.getProperty("os.name");
System.out.println(OS);
Map<String,Object> map = jdbc.findSimpleResult("select * from dafuweng.user where id=2709");
}
}
package utils;
import io.restassured.response.Response;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.IOException;
import java.util.*;
import static io.restassured.RestAssured.given;
import static io.restassured.path.json.JsonPath.from;
/**
* Created by hanzhanli on 2017/12/1.
*/
public class ServiceDoctor {
private String propertiesFileName;
private Map<String, String> propertiesMap;
private String ip=null;
public ServiceDoctor(String propertiesFileName){
propertiesMap=propertiesToMap(propertiesFileName);
ip=propertiesMap.get("ip");
propertiesMap.remove("ip");
}
public Set<String> getToBeTestedServices(){
return this.propertiesMap.keySet();
}
public String getLogLenth(String service){
String lenth="0";
//http://121.40.80.93:9527/log/gettotal?service=tuia-advert-web
String url="http://"+ip+":9527/log/gettotal";
if(propertiesMap.get(service)!=null) {
Response response = given().param("service", service).get(url);
response.prettyPrint();
lenth = from(response.asString()).getString("total");
logger.info(lenth);
}
return lenth;
}
public Map<String,String> getLogLenth(){
Map<String,String> lengthMap=new HashMap<>();
for(String service:propertiesMap.keySet()){
if(!service.equals("ip")) {
String lenth = getLogLenth(service);
lengthMap.put(service, lenth);
}
}
return lengthMap;
}
public Map<String,List<String>> getDetail(Map<String,String> fromMap){
Map<String,List<String>> logMap=new HashMap<>();
for(String service:propertiesMap.keySet()){
if(!service.equals("ip")) {
String from = fromMap.get(service);
List<String> log = getDetail(service, from);
logMap.put(service, log);
}
}
return logMap;
}
public List<String> getDetail(String service,String from){
List<String> detail=null;
String url="http://"+ip+":9527/log/getdetail";
if(propertiesMap.get(service)!=null) {
Response response = given().param("service", service).param("from",from).get(url);
response.prettyPrint();
detail=from(response.asString()).getList("detail");
logger.info(detail.size());
}
return detail;
}
//monitor/check
public List<String> getNotRunning(){
List<String> deadServices=new ArrayList<>();
String ip=this.ip;
String url="http://"+ip;
String path="/monitor/check";
String port;
for(String service:propertiesMap.keySet()){
if(!service.equals("ip")){
port=propertiesMap.get(service);
try {
logger.info(url + ":" + port + path);
Response response = given().get(url + ":" + port + path);
logger.info("被测服务体检:" + service + ":" + response.asString());
//response.prettyPrint();
if (response.getStatusCode() != 200) {
logger.info("未启动服务:" + url + ":" + port + path);
deadServices.add(service);
}
}catch (Exception e){
deadServices.add(service);
}
}
}
logger.info("未启动服务:"+deadServices.toString());
return deadServices;
}
public boolean isRunning(String service){
boolean isRunning=true;
String ip=this.ip;
String url="http://"+ip;
String path="/monitor/check";
String port;
port=propertiesMap.get(service);
try {
Response response = given().get(url + ":" + port + path);
logger.info("被测服务体检:" + service + ":" + response.asString());
//response.prettyPrint();
if (response.getStatusCode() != 200) {
logger.info("未启动服务状态:"+response.getStatusCode());
logger.info("未启动服务:" + url + ":" + port + path);
isRunning=false;
}
}catch (Exception e){
logger.info("未启动服务:" + url + ":" + port + path);
isRunning=false;
}
return isRunning;
}
private static Logger logger = LogManager.getLogger(ServiceDoctor.class);
private static Map<String, String> propertiesToMap(String propertiesFileName){
Properties properties=new Properties();
try {
properties.load(ServiceDoctor.class.getClassLoader().getResourceAsStream(propertiesFileName));
} catch (IOException e) {
logger.error("属性文件路径错误:"+propertiesFileName);
e.printStackTrace();
}
Set<Object> keys=properties.keySet();
Map<String, String> map=new HashMap<String, String>();
for (Object k : keys) {
map.put((String)k, (String)properties.get(k));
}
return map;
}
public List<String> getExceptionService(int maxTime){
int time=0;
List<String> exceptionList=this.getNotRunning();
//maxTime时间后仍异常的接口,记为异常
while (exceptionList.size()>0&&time<maxTime){
for(int i=0;i<exceptionList.size();i++){
String service=exceptionList.get(i);
logger.info(time+":"+service);
if (this.isRunning(service)){
exceptionList.remove(i);
i--;
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
time++;
}
return exceptionList;
}
}
ip=10.110.10.10
intersection-service=8013
zuul-server=1027
#sso-service=8888
thirdparty-service=9766
ip=10.110.10.11
#duiba-service=6080
credits-home-web=7755
#duiba-manager-web=7091
goods-access-web=7101
activity-access-web=7555
activity-cms-web=7655
trade-access-web=7855
da-fu-weng=9002
duiba-manager=9090
seckill-web=7788
consumer-center=8002
developer-center=8003
trade-center=8011
goods-center=9768
activity-center=8779
hdtool-web=8701
hdtool-center=8501
prize-center=8601
plugin-web=8711
plugin-center=8611
sign-web=8990
sign-center=8989
activity-comm-center=8301
kvtable-service=8801
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment