|
@@ -8,8 +8,10 @@ import com.google.gson.JsonArray;
|
|
|
import com.google.gson.JsonObject;
|
|
|
import net.mooctest.www.android_auto_test.common.BeanFactory;
|
|
|
import net.mooctest.www.android_auto_test.common.constant.Consts;
|
|
|
+import net.mooctest.www.android_auto_test.common.constant.enums.DeviceStatus;
|
|
|
import net.mooctest.www.android_auto_test.common.constant.enums.TraceStatus;
|
|
|
import net.mooctest.www.android_auto_test.services.ApkService;
|
|
|
+import net.mooctest.www.android_auto_test.services.DeviceService;
|
|
|
import net.mooctest.www.android_auto_test.services.OssService;
|
|
|
import net.mooctest.www.android_auto_test.services.TraceService;
|
|
|
import org.apache.commons.io.FileUtils;
|
|
@@ -34,11 +36,18 @@ public class TraceDaemon extends Thread{
|
|
|
private int minutes;
|
|
|
private List<CoverageTest> deviceThreads;
|
|
|
private boolean stopImmediately = false;
|
|
|
+ private boolean needGenerateReport;
|
|
|
+ private boolean needSendReport;
|
|
|
+ private String reportType;
|
|
|
|
|
|
- public TraceDaemon(String traceId, int minutes, List<CoverageTest> deviceThreads){
|
|
|
+ public TraceDaemon(String traceId, int minutes, List<CoverageTest> deviceThreads,
|
|
|
+ boolean needGenerateReport, boolean needSendReport, String reportType){
|
|
|
this.traceId = traceId;
|
|
|
this.minutes = minutes;
|
|
|
this.deviceThreads = deviceThreads;
|
|
|
+ this.needGenerateReport = needGenerateReport;
|
|
|
+ this.needSendReport = needSendReport;
|
|
|
+ this.reportType = reportType;
|
|
|
traceService = (TraceService) BeanFactory.getBean(TraceService.class);
|
|
|
ossService = (OssService) BeanFactory.getBean(OssService.class);
|
|
|
apkService = (ApkService) BeanFactory.getBean(ApkService.class);
|
|
@@ -82,62 +91,88 @@ public class TraceDaemon extends Thread{
|
|
|
} catch (InterruptedException e) {
|
|
|
e.printStackTrace();
|
|
|
} finally {
|
|
|
- traceService.updateTraceStatue(traceId, TraceStatus.GEN_REPORT);
|
|
|
- // 上传截图到OSS
|
|
|
- File traceDir = new File(AddressUtil.getTraceDir(traceId));
|
|
|
- if (traceDir.exists()){
|
|
|
- for (File f: traceDir.listFiles()){
|
|
|
- if (f.isDirectory()){
|
|
|
- String udid = f.getName();
|
|
|
- File screenDir = new File(AddressUtil.getScreenShotPath(traceId, udid));
|
|
|
- if (!screenDir.exists()){
|
|
|
- continue;
|
|
|
- }
|
|
|
- for (File screenShot: screenDir.listFiles()){
|
|
|
- System.out.println(screenShot);
|
|
|
- ossService.uploadFileToTraceDir(screenShot, traceId, screenShot.getName());
|
|
|
- }
|
|
|
+ if (needGenerateReport) {
|
|
|
+ generateReport();
|
|
|
+ }
|
|
|
+ if (needSendReport) {
|
|
|
+ //TODO 生成报告之后,将报告发给企业版主站
|
|
|
+ sendReport();
|
|
|
+ }
|
|
|
+ apkService.updateTraceEndTime(traceId);
|
|
|
+ PrintUtil.print(String.format("Trace %s is done!", traceId), TAG);
|
|
|
+ traceService.updateTraceStatue(traceId, TraceStatus.FINISH);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void generateReport(){
|
|
|
+ traceService.updateTraceStatue(traceId, TraceStatus.GEN_REPORT);
|
|
|
+ // 上传截图到OSS
|
|
|
+ File traceDir = new File(AddressUtil.getTraceDir(traceId));
|
|
|
+ if (traceDir.exists()){
|
|
|
+ for (File f: traceDir.listFiles()){
|
|
|
+ if (f.isDirectory()){
|
|
|
+ String udid = f.getName();
|
|
|
+ File screenDir = new File(AddressUtil.getScreenShotPath(traceId, udid));
|
|
|
+ if (!screenDir.exists()){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ for (File screenShot: screenDir.listFiles()){
|
|
|
+ System.out.println(screenShot);
|
|
|
+ ossService.uploadFileToTraceDir(screenShot, traceId, screenShot.getName());
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- // TODO 生成报告
|
|
|
- try {
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ if ("BugReport".equals(reportType)) {
|
|
|
PrintUtil.print("Generate bug report by AllenTian's docker", TAG);
|
|
|
String command = "java -jar tasks/AutoTestReportGenerator.jar " + traceId;
|
|
|
String result = OsUtil.runCommand(command);
|
|
|
+ // TODO 这里在田老师改了生成逻辑后,就可以删掉了
|
|
|
if (!"success\n".equals(result)){
|
|
|
PrintUtil.print(result, TAG);
|
|
|
PrintUtil.print("Generate bug report failed, use default report.", TAG);
|
|
|
FileUtils.copyFile(new File(AddressUtil.getMockDataJsonPath()), new File(AddressUtil.getDataJsonPath(traceId)));
|
|
|
}
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ // 上传data.json文件
|
|
|
+ File data = new File(AddressUtil.getDataJsonPath(traceId));
|
|
|
+ String path = ossService.uploadFileToTraceDir(data, traceId, Consts.REPORT_FILE_NAME);
|
|
|
+ int retryTimes = 0;
|
|
|
+ // 上传失败,重试5次
|
|
|
+ while (path != null && retryTimes < Consts.UPLOAD_OSS_MAX_ATTEMPTS){
|
|
|
+ try {
|
|
|
+ Thread.sleep(3000);
|
|
|
+ path = ossService.uploadFileToTraceDir(data, traceId, Consts.REPORT_FILE_NAME);
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
+ PrintUtil.print(String.format("Trace %s upload data failed %s times.", traceId, retryTimes), TAG);
|
|
|
+ } finally {
|
|
|
+ retryTimes++;
|
|
|
}
|
|
|
- // 上传data.json文件
|
|
|
- File data = new File(AddressUtil.getDataJsonPath(traceId));
|
|
|
- String path = ossService.uploadFileToTraceDir(data, traceId, Consts.REPORT_FILE_NAME);
|
|
|
- int retryTimes = 0;
|
|
|
- // 上传失败,重试5次
|
|
|
- while (path != null && retryTimes < Consts.UPLOAD_OSS_MAX_ATTEMPTS){
|
|
|
- try {
|
|
|
- Thread.sleep(3000);
|
|
|
- path = ossService.uploadFileToTraceDir(data, traceId, Consts.REPORT_FILE_NAME);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- PrintUtil.print(String.format("Trace %s upload data failed %s times.", traceId, retryTimes), TAG);
|
|
|
- } finally {
|
|
|
- retryTimes++;
|
|
|
- }
|
|
|
- }
|
|
|
- apkService.updateTraceEndTime(traceId);
|
|
|
- if (path != null){
|
|
|
- PrintUtil.print(String.format("Trace %s's data upload success.", traceId), TAG);
|
|
|
- }else {
|
|
|
- PrintUtil.print(String.format("Trace %s's data upload failed after tried 5 times.", traceId), TAG);
|
|
|
- }
|
|
|
- PrintUtil.print(String.format("Trace %s is done!", traceId), TAG);
|
|
|
- traceService.updateTraceStatue(traceId, TraceStatus.FINISH);
|
|
|
}
|
|
|
+ if (path != null){
|
|
|
+ PrintUtil.print(String.format("Trace %s's data upload success.", traceId), TAG);
|
|
|
+ }else {
|
|
|
+ PrintUtil.print(String.format("Trace %s's data upload failed after tried 5 times.", traceId), TAG);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void sendReport(){
|
|
|
+ System.out.println("Send report to Mooctest.");
|
|
|
+// RestTemplate rt = new RestTemplate();
|
|
|
+// HttpHeaders headers = new HttpHeaders();
|
|
|
+// headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
+// TraceStatusResult traceResult = new TraceStatusResult();
|
|
|
+// traceResult.setTraceId(traceId);
|
|
|
+// String downloadUrl = ossService.getDadaJsonDownloadPath(traceId);
|
|
|
+// traceResult.setDownloadUrl(downloadUrl);
|
|
|
+// HttpEntity<TraceStatusResult> httpEntity = new HttpEntity<>(traceResult, headers);
|
|
|
+// String url = "http://localhost:15926/api/v1/mock/TraceInfo";
|
|
|
+// rt.exchange(url, HttpMethod.POST, httpEntity, String.class);
|
|
|
}
|
|
|
|
|
|
/**
|