1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License 15 */ 16 package com.android.compatibility.common.util; 17 18 import com.android.tradefed.util.FileUtil; 19 import com.android.tradefed.util.AbiUtils; 20 21 import junit.framework.TestCase; 22 23 import java.io.File; 24 import java.io.FileWriter; 25 import java.io.IOException; 26 import java.net.InetAddress; 27 import java.net.UnknownHostException; 28 import java.util.Arrays; 29 import java.util.List; 30 import java.util.Map; 31 import java.util.Set; 32 33 /** 34 * Unit tests for {@link ResultHandler} 35 */ 36 public class ResultHandlerTest extends TestCase { 37 38 private static final String SUITE_NAME = "CTS"; 39 private static final String SUITE_VERSION = "5.0"; 40 private static final String SUITE_PLAN = "cts"; 41 private static final String SUITE_BUILD = "12345"; 42 private static final String REPORT_VERSION = "5.0"; 43 private static final String OS_NAME = System.getProperty("os.name"); 44 private static final String OS_VERSION = System.getProperty("os.version"); 45 private static final String OS_ARCH = System.getProperty("os.arch"); 46 private static final String JAVA_VENDOR = System.getProperty("java.vendor"); 47 private static final String JAVA_VERSION = System.getProperty("java.version"); 48 private static final String NAME_A = "ModuleA"; 49 private static final String NAME_B = "ModuleB"; 50 private static final String DONE_A = "false"; 51 private static final String DONE_B = "true"; 52 private static final String RUNTIME_A = "100"; 53 private static final String RUNTIME_B = "200"; 54 private static final String ABI = "mips64"; 55 private static final String ID_A = AbiUtils.createId(ABI, NAME_A); 56 private static final String ID_B = AbiUtils.createId(ABI, NAME_B); 57 58 private static final String BUILD_ID = "build_id"; 59 private static final String BUILD_PRODUCT = "build_product"; 60 private static final String EXAMPLE_BUILD_ID = "XYZ"; 61 private static final String EXAMPLE_BUILD_PRODUCT = "wolverine"; 62 63 private static final String DEVICE_A = "device123"; 64 private static final String DEVICE_B = "device456"; 65 private static final String DEVICES = "device456,device123"; 66 private static final String CLASS_A = "android.test.Foor"; 67 private static final String CLASS_B = "android.test.Bar"; 68 private static final String METHOD_1 = "testBlah1"; 69 private static final String METHOD_2 = "testBlah2"; 70 private static final String METHOD_3 = "testBlah3"; 71 private static final String METHOD_4 = "testBlah4"; 72 private static final String METHOD_5 = "testBlah5"; 73 private static final String SUMMARY_SOURCE = String.format("%s#%s:20", CLASS_B, METHOD_4); 74 private static final String SUMMARY_MESSAGE = "Headline"; 75 private static final double SUMMARY_VALUE = 9001; 76 private static final String MESSAGE = "Something small is not alright"; 77 private static final String STACK_TRACE = "Something small is not alright\n " + 78 "at four.big.insects.Marley.sing(Marley.java:10)"; 79 private static final String BUG_REPORT = "https://cnsviewer.corp.google.com/cns/bugreport.txt"; 80 private static final String LOGCAT = "https://cnsviewer.corp.google.com/cns/logcat.gz"; 81 private static final String SCREENSHOT = "https://cnsviewer.corp.google.com/screenshot.png"; 82 private static final long START_MS = 1431586801000L; 83 private static final long END_MS = 1431673199000L; 84 private static final String START_DISPLAY = "Fri Aug 20 15:13:03 PDT 2010"; 85 private static final String END_DISPLAY = "Fri Aug 20 15:13:04 PDT 2010"; 86 87 private static final String REFERENCE_URL="http://android.com"; 88 private static final String LOG_URL ="file:///path/to/logs"; 89 private static final String COMMAND_LINE_ARGS = "cts -m CtsMyModuleTestCases"; 90 private static final String XML_BASE = 91 "<?xml version='1.0' encoding='UTF-8' standalone='no' ?>" + 92 "<?xml-stylesheet type=\"text/xsl\" href=\"compatibility_result.xsl\"?>\n" + 93 "<Result start=\"%d\" end=\"%d\" start_display=\"%s\"" + 94 "end_display=\"%s\" suite_name=\"%s\" suite_version=\"%s\" " + 95 "suite_plan=\"%s\" suite_build_number=\"%s\" report_version=\"%s\" " + 96 "devices=\"%s\" host_name=\"%s\"" + 97 "os_name=\"%s\" os_version=\"%s\" os_arch=\"%s\" java_vendor=\"%s\"" + 98 "java_version=\"%s\" reference_url=\"%s\" log_url=\"%s\"" + 99 "command_line_args=\"%s\">\n" + 100 "%s%s%s" + 101 "</Result>"; 102 private static final String XML_BUILD_INFO = 103 " <Build build_fingerprint=\"%s\" " + BUILD_ID + "=\"%s\" " + 104 BUILD_PRODUCT + "=\"%s\" />\n"; 105 private static final String XML_SUMMARY = 106 " <Summary pass=\"%d\" failed=\"%d\" " + 107 "modules_done=\"1\" modules_total=\"1\" />\n"; 108 private static final String XML_MODULE = 109 " <Module name=\"%s\" abi=\"%s\" device=\"%s\" runtime=\"%s\" done=\"%s\">\n" + 110 "%s" + 111 " </Module>\n"; 112 private static final String XML_CASE = 113 " <TestCase name=\"%s\">\n" + 114 "%s" + 115 " </TestCase>\n"; 116 private static final String XML_TEST_PASS = 117 " <Test result=\"pass\" name=\"%s\"/>\n"; 118 private static final String XML_TEST_SKIP = 119 " <Test result=\"pass\" name=\"%s\" skipped=\"true\"/>\n"; 120 private static final String XML_TEST_FAIL = 121 " <Test result=\"fail\" name=\"%s\">\n" + 122 " <Failure message=\"%s\">\n" + 123 " <StackTrace>%s</StackTrace>\n" + 124 " </Failure>\n" + 125 " <BugReport>%s</BugReport>\n" + 126 " <Logcat>%s</Logcat>\n" + 127 " <Screenshot>%s</Screenshot>\n" + 128 " </Test>\n"; 129 private static final String XML_TEST_RESULT = 130 " <Test result=\"pass\" name=\"%s\">\n" + 131 " <Summary>\n" + 132 " <Metric source=\"%s\" message=\"%s\" score_type=\"%s\" score_unit=\"%s\">\n" + 133 " <Value>%s</Value>\n" + 134 " </Metric>\n" + 135 " </Summary>\n" + 136 " </Test>\n"; 137 private File resultsDir = null; 138 private File resultDir = null; 139 140 @Override setUp()141 public void setUp() throws Exception { 142 resultsDir = FileUtil.createTempDir("results"); 143 resultDir = FileUtil.createTempDir("12345", resultsDir); 144 } 145 146 @Override tearDown()147 public void tearDown() throws Exception { 148 FileUtil.recursiveDelete(resultsDir); 149 } 150 testSerialization()151 public void testSerialization() throws Exception { 152 IInvocationResult result = new InvocationResult(); 153 result.setStartTime(START_MS); 154 result.setTestPlan(SUITE_PLAN); 155 result.addDeviceSerial(DEVICE_A); 156 result.addDeviceSerial(DEVICE_B); 157 result.addInvocationInfo(BUILD_ID, EXAMPLE_BUILD_ID); 158 result.addInvocationInfo(BUILD_PRODUCT, EXAMPLE_BUILD_PRODUCT); 159 // Module A: test1 passes, test2 not executed 160 IModuleResult moduleA = result.getOrCreateModule(ID_A); 161 moduleA.setDone(false); 162 moduleA.addRuntime(Integer.parseInt(RUNTIME_A)); 163 ICaseResult moduleACase = moduleA.getOrCreateResult(CLASS_A); 164 ITestResult moduleATest1 = moduleACase.getOrCreateResult(METHOD_1); 165 moduleATest1.setResultStatus(TestStatus.PASS); 166 ITestResult moduleATest2 = moduleACase.getOrCreateResult(METHOD_2); 167 moduleATest2.setResultStatus(null); // not executed test 168 // Module B: test3 fails, test4 passes with report log, test5 passes with skip 169 IModuleResult moduleB = result.getOrCreateModule(ID_B); 170 moduleB.setDone(true); 171 moduleB.addRuntime(Integer.parseInt(RUNTIME_B)); 172 ICaseResult moduleBCase = moduleB.getOrCreateResult(CLASS_B); 173 ITestResult moduleBTest3 = moduleBCase.getOrCreateResult(METHOD_3); 174 moduleBTest3.setResultStatus(TestStatus.FAIL); 175 moduleBTest3.setMessage(MESSAGE); 176 moduleBTest3.setStackTrace(STACK_TRACE); 177 moduleBTest3.setBugReport(BUG_REPORT); 178 moduleBTest3.setLog(LOGCAT); 179 moduleBTest3.setScreenshot(SCREENSHOT); 180 ITestResult moduleBTest4 = moduleBCase.getOrCreateResult(METHOD_4); 181 moduleBTest4.setResultStatus(TestStatus.PASS); 182 ReportLog report = new ReportLog(); 183 ReportLog.Metric summary = new ReportLog.Metric(SUMMARY_SOURCE, SUMMARY_MESSAGE, 184 SUMMARY_VALUE, ResultType.HIGHER_BETTER, ResultUnit.SCORE); 185 report.setSummary(summary); 186 moduleBTest4.setReportLog(report); 187 ITestResult moduleBTest5 = moduleBCase.getOrCreateResult(METHOD_5); 188 moduleBTest5.skipped(); 189 190 // Serialize to file 191 ResultHandler.writeResults(SUITE_NAME, SUITE_VERSION, SUITE_PLAN, SUITE_BUILD, 192 result, resultDir, START_MS, END_MS, REFERENCE_URL, LOG_URL, 193 COMMAND_LINE_ARGS); 194 195 // Parse the results and assert correctness 196 checkResult(ResultHandler.getResultFromDir(resultDir)); 197 } 198 testParsing()199 public void testParsing() throws Exception { 200 File resultDir = writeResultDir(resultsDir); 201 // Parse the results and assert correctness 202 checkResult(ResultHandler.getResultFromDir(resultDir)); 203 } 204 testGetLightResults()205 public void testGetLightResults() throws Exception { 206 File resultDir = writeResultDir(resultsDir); 207 List<IInvocationResult> lightResults = ResultHandler.getLightResults(resultsDir); 208 assertEquals("Expected one result", 1, lightResults.size()); 209 IInvocationResult lightResult = lightResults.get(0); 210 checkLightResult(lightResult); 211 } 212 213 /* 214 * Helper to write a result to the results dir, for testing. 215 * @return the written resultDir 216 */ writeResultDir(File resultsDir)217 static File writeResultDir(File resultsDir) throws IOException { 218 File resultDir = null; 219 FileWriter writer = null; 220 try { 221 resultDir = FileUtil.createTempDir("12345", resultsDir); 222 // Create the result file 223 File resultFile = new File(resultDir, ResultHandler.TEST_RESULT_FILE_NAME); 224 writer = new FileWriter(resultFile); 225 String buildInfo = String.format(XML_BUILD_INFO, DEVICE_A, 226 EXAMPLE_BUILD_ID, EXAMPLE_BUILD_PRODUCT); 227 String summary = String.format(XML_SUMMARY, 2, 1); 228 String moduleATest = String.format(XML_TEST_PASS, METHOD_1); 229 String moduleACases = String.format(XML_CASE, CLASS_A, moduleATest); 230 String moduleA = String.format(XML_MODULE, NAME_A, ABI, DEVICE_A, RUNTIME_A, DONE_A, 231 moduleACases); 232 String moduleBTest3 = String.format(XML_TEST_FAIL, METHOD_3, MESSAGE, STACK_TRACE, 233 BUG_REPORT, LOGCAT, SCREENSHOT); 234 String moduleBTest4 = String.format(XML_TEST_RESULT, METHOD_4, SUMMARY_SOURCE, 235 SUMMARY_MESSAGE, ResultType.HIGHER_BETTER.toReportString(), 236 ResultUnit.SCORE.toReportString(), Double.toString(SUMMARY_VALUE)); 237 String moduleBTest5 = String.format(XML_TEST_SKIP, METHOD_5); 238 String moduleBTests = String.join("", moduleBTest3, moduleBTest4, moduleBTest5); 239 String moduleBCases = String.format(XML_CASE, CLASS_B, moduleBTests); 240 String moduleB = String.format(XML_MODULE, NAME_B, ABI, DEVICE_B, RUNTIME_B, DONE_B, 241 moduleBCases); 242 String modules = String.join("", moduleA, moduleB); 243 String hostName = ""; 244 try { 245 hostName = InetAddress.getLocalHost().getHostName(); 246 } catch (UnknownHostException ignored) {} 247 String output = String.format(XML_BASE, START_MS, END_MS, START_DISPLAY, END_DISPLAY, 248 SUITE_NAME, SUITE_VERSION, SUITE_PLAN, SUITE_BUILD, REPORT_VERSION, DEVICES, 249 hostName, OS_NAME, OS_VERSION, OS_ARCH, JAVA_VENDOR, 250 JAVA_VERSION, REFERENCE_URL, LOG_URL, COMMAND_LINE_ARGS, 251 buildInfo, summary, modules); 252 writer.write(output); 253 writer.flush(); 254 } finally { 255 if (writer != null) { 256 writer.close(); 257 } 258 } 259 return resultDir; 260 } 261 checkLightResult(IInvocationResult lightResult)262 static void checkLightResult(IInvocationResult lightResult) throws Exception { 263 assertEquals("Expected 3 passes", 3, lightResult.countResults(TestStatus.PASS)); 264 assertEquals("Expected 1 failure", 1, lightResult.countResults(TestStatus.FAIL)); 265 266 Map<String, String> buildInfo = lightResult.getInvocationInfo(); 267 assertEquals("Incorrect Build ID", EXAMPLE_BUILD_ID, buildInfo.get(BUILD_ID)); 268 assertEquals("Incorrect Build Product", 269 EXAMPLE_BUILD_PRODUCT, buildInfo.get(BUILD_PRODUCT)); 270 271 Set<String> serials = lightResult.getDeviceSerials(); 272 assertTrue("Missing device", serials.contains(DEVICE_A)); 273 assertTrue("Missing device", serials.contains(DEVICE_B)); 274 assertEquals("Expected 2 devices", 2, serials.size()); 275 assertTrue("Incorrect devices", serials.contains(DEVICE_A) && serials.contains(DEVICE_B)); 276 assertEquals("Incorrect start time", START_MS, lightResult.getStartTime()); 277 assertEquals("Incorrect test plan", SUITE_PLAN, lightResult.getTestPlan()); 278 List<IModuleResult> modules = lightResult.getModules(); 279 assertEquals("Expected 1 completed module", 1, lightResult.getModuleCompleteCount()); 280 assertEquals("Expected 2 total modules", 2, modules.size()); 281 } 282 checkResult(IInvocationResult result)283 static void checkResult(IInvocationResult result) throws Exception { 284 assertEquals("Expected 3 passes", 3, result.countResults(TestStatus.PASS)); 285 assertEquals("Expected 1 failure", 1, result.countResults(TestStatus.FAIL)); 286 287 Map<String, String> buildInfo = result.getInvocationInfo(); 288 assertEquals("Incorrect Build ID", EXAMPLE_BUILD_ID, buildInfo.get(BUILD_ID)); 289 assertEquals("Incorrect Build Product", 290 EXAMPLE_BUILD_PRODUCT, buildInfo.get(BUILD_PRODUCT)); 291 292 Set<String> serials = result.getDeviceSerials(); 293 assertTrue("Missing device", serials.contains(DEVICE_A)); 294 assertTrue("Missing device", serials.contains(DEVICE_B)); 295 assertEquals("Expected 2 devices", 2, serials.size()); 296 assertTrue("Incorrect devices", serials.contains(DEVICE_A) && serials.contains(DEVICE_B)); 297 assertEquals("Incorrect start time", START_MS, result.getStartTime()); 298 assertEquals("Incorrect test plan", SUITE_PLAN, result.getTestPlan()); 299 300 List<IModuleResult> modules = result.getModules(); 301 assertEquals("Expected 2 modules", 2, modules.size()); 302 303 IModuleResult moduleA = modules.get(0); 304 assertEquals("Expected 1 pass", 1, moduleA.countResults(TestStatus.PASS)); 305 assertEquals("Expected 0 failures", 0, moduleA.countResults(TestStatus.FAIL)); 306 assertEquals("Incorrect ABI", ABI, moduleA.getAbi()); 307 assertEquals("Incorrect name", NAME_A, moduleA.getName()); 308 assertEquals("Incorrect ID", ID_A, moduleA.getId()); 309 assertEquals("Incorrect runtime", Integer.parseInt(RUNTIME_A), moduleA.getRuntime()); 310 List<ICaseResult> moduleACases = moduleA.getResults(); 311 assertEquals("Expected 1 test case", 1, moduleACases.size()); 312 ICaseResult moduleACase = moduleACases.get(0); 313 assertEquals("Incorrect name", CLASS_A, moduleACase.getName()); 314 List<ITestResult> moduleAResults = moduleACase.getResults(); 315 assertEquals("Expected 1 result", 1, moduleAResults.size()); 316 ITestResult moduleATest1 = moduleAResults.get(0); 317 assertEquals("Incorrect name", METHOD_1, moduleATest1.getName()); 318 assertEquals("Incorrect result", TestStatus.PASS, moduleATest1.getResultStatus()); 319 assertNull("Unexpected bugreport", moduleATest1.getBugReport()); 320 assertNull("Unexpected log", moduleATest1.getLog()); 321 assertNull("Unexpected screenshot", moduleATest1.getScreenshot()); 322 assertNull("Unexpected message", moduleATest1.getMessage()); 323 assertNull("Unexpected stack trace", moduleATest1.getStackTrace()); 324 assertNull("Unexpected report", moduleATest1.getReportLog()); 325 326 IModuleResult moduleB = modules.get(1); 327 assertEquals("Expected 2 passes", 2, moduleB.countResults(TestStatus.PASS)); 328 assertEquals("Expected 1 failure", 1, moduleB.countResults(TestStatus.FAIL)); 329 assertEquals("Incorrect ABI", ABI, moduleB.getAbi()); 330 assertEquals("Incorrect name", NAME_B, moduleB.getName()); 331 assertEquals("Incorrect ID", ID_B, moduleB.getId()); 332 assertEquals("Incorrect runtime", Integer.parseInt(RUNTIME_B), moduleB.getRuntime()); 333 List<ICaseResult> moduleBCases = moduleB.getResults(); 334 assertEquals("Expected 1 test case", 1, moduleBCases.size()); 335 ICaseResult moduleBCase = moduleBCases.get(0); 336 assertEquals("Incorrect name", CLASS_B, moduleBCase.getName()); 337 List<ITestResult> moduleBResults = moduleBCase.getResults(); 338 assertEquals("Expected 3 results", 3, moduleBResults.size()); 339 ITestResult moduleBTest3 = moduleBResults.get(0); 340 assertEquals("Incorrect name", METHOD_3, moduleBTest3.getName()); 341 assertEquals("Incorrect result", TestStatus.FAIL, moduleBTest3.getResultStatus()); 342 assertEquals("Incorrect bugreport", BUG_REPORT, moduleBTest3.getBugReport()); 343 assertEquals("Incorrect log", LOGCAT, moduleBTest3.getLog()); 344 assertEquals("Incorrect screenshot", SCREENSHOT, moduleBTest3.getScreenshot()); 345 assertEquals("Incorrect message", MESSAGE, moduleBTest3.getMessage()); 346 assertEquals("Incorrect stack trace", STACK_TRACE, moduleBTest3.getStackTrace()); 347 assertNull("Unexpected report", moduleBTest3.getReportLog()); 348 ITestResult moduleBTest4 = moduleBResults.get(1); 349 assertEquals("Incorrect name", METHOD_4, moduleBTest4.getName()); 350 assertEquals("Incorrect result", TestStatus.PASS, moduleBTest4.getResultStatus()); 351 assertNull("Unexpected bugreport", moduleBTest4.getBugReport()); 352 assertNull("Unexpected log", moduleBTest4.getLog()); 353 assertNull("Unexpected screenshot", moduleBTest4.getScreenshot()); 354 assertNull("Unexpected message", moduleBTest4.getMessage()); 355 assertNull("Unexpected stack trace", moduleBTest4.getStackTrace()); 356 ReportLog report = moduleBTest4.getReportLog(); 357 assertNotNull("Expected report", report); 358 ReportLog.Metric summary = report.getSummary(); 359 assertNotNull("Expected report summary", summary); 360 assertEquals("Incorrect source", SUMMARY_SOURCE, summary.getSource()); 361 assertEquals("Incorrect message", SUMMARY_MESSAGE, summary.getMessage()); 362 assertEquals("Incorrect type", ResultType.HIGHER_BETTER, summary.getType()); 363 assertEquals("Incorrect unit", ResultUnit.SCORE, summary.getUnit()); 364 assertTrue("Incorrect values", Arrays.equals(new double[] { SUMMARY_VALUE }, 365 summary.getValues())); 366 ITestResult moduleBTest5 = moduleBResults.get(2); 367 assertEquals("Incorrect name", METHOD_5, moduleBTest5.getName()); 368 assertEquals("Incorrect result", TestStatus.PASS, moduleBTest5.getResultStatus()); 369 assertTrue("Expected skipped", moduleBTest5.isSkipped()); 370 assertNull("Unexpected bugreport", moduleBTest5.getBugReport()); 371 assertNull("Unexpected log", moduleBTest5.getLog()); 372 assertNull("Unexpected screenshot", moduleBTest5.getScreenshot()); 373 assertNull("Unexpected message", moduleBTest5.getMessage()); 374 assertNull("Unexpected stack trace", moduleBTest5.getStackTrace()); 375 assertNull("Unexpected report", moduleBTest5.getReportLog()); 376 } 377 } 378