Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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.tradefed.result.proto; |
| 17 | |
Julien Desprez | 5035aa6 | 2019-07-27 17:11:57 -0700 | [diff] [blame] | 18 | import com.android.tradefed.config.Option; |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 19 | import com.android.tradefed.config.OptionClass; |
| 20 | import com.android.tradefed.invoker.IInvocationContext; |
| 21 | import com.android.tradefed.log.LogUtil.CLog; |
| 22 | import com.android.tradefed.metrics.proto.MetricMeasurement.Metric; |
Julien Desprez | 8ee0923 | 2020-02-06 10:30:11 -0800 | [diff] [blame] | 23 | import com.android.tradefed.result.FailureDescription; |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 24 | import com.android.tradefed.result.ILogSaverListener; |
| 25 | import com.android.tradefed.result.ITestInvocationListener; |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 26 | import com.android.tradefed.result.LogFile; |
| 27 | import com.android.tradefed.result.TestDescription; |
Julien Desprez | ed8bd9b | 2018-07-13 08:50:20 -0700 | [diff] [blame] | 28 | import com.android.tradefed.result.proto.LogFileProto.LogFileInfo; |
| 29 | import com.android.tradefed.result.proto.TestRecordProto.ChildReference; |
| 30 | import com.android.tradefed.result.proto.TestRecordProto.DebugInfo; |
Julien Desprez | 221aaf1 | 2020-02-27 14:17:03 -0800 | [diff] [blame^] | 31 | import com.android.tradefed.result.proto.TestRecordProto.DebugInfoContext; |
Julien Desprez | ed8bd9b | 2018-07-13 08:50:20 -0700 | [diff] [blame] | 32 | import com.android.tradefed.result.proto.TestRecordProto.TestRecord; |
| 33 | import com.android.tradefed.result.proto.TestRecordProto.TestStatus; |
Julien Desprez | 5035aa6 | 2019-07-27 17:11:57 -0700 | [diff] [blame] | 34 | import com.android.tradefed.result.retry.ISupportGranularResults; |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 35 | import com.android.tradefed.testtype.suite.ModuleDefinition; |
Julien Desprez | dbc1f17 | 2018-12-21 12:14:33 -0800 | [diff] [blame] | 36 | import com.android.tradefed.util.StreamUtil; |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 37 | |
Julien Desprez | 221aaf1 | 2020-02-27 14:17:03 -0800 | [diff] [blame^] | 38 | import com.google.common.base.Strings; |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 39 | import com.google.protobuf.Any; |
| 40 | import com.google.protobuf.Timestamp; |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 41 | |
| 42 | import java.util.HashMap; |
| 43 | import java.util.Map; |
| 44 | import java.util.Stack; |
| 45 | import java.util.UUID; |
| 46 | |
| 47 | /** |
| 48 | * Result reporter build a {@link TestRecord} protobuf with all the results inside. Should be |
| 49 | * extended to handle what to do with the final proto in {@link #processFinalProto(TestRecord)}. |
| 50 | */ |
| 51 | @OptionClass(alias = "proto-reporter") |
Julien Desprez | 5035aa6 | 2019-07-27 17:11:57 -0700 | [diff] [blame] | 52 | public abstract class ProtoResultReporter |
| 53 | implements ITestInvocationListener, ILogSaverListener, ISupportGranularResults { |
| 54 | |
| 55 | @Option( |
| 56 | name = "enable-granular-attempts", |
| 57 | description = |
| 58 | "Whether or not to allow this reporter receiving granular attempts. Feature flag." |
| 59 | ) |
| 60 | private boolean mReportGranularResults = true; |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 61 | |
| 62 | private Stack<TestRecord.Builder> mLatestChild; |
| 63 | private TestRecord.Builder mInvocationRecordBuilder; |
| 64 | private long mInvocationStartTime; |
Julien Desprez | f066b70 | 2018-11-07 11:10:08 -0800 | [diff] [blame] | 65 | private IInvocationContext mContext; |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 66 | |
Julien Desprez | dbc1f17 | 2018-12-21 12:14:33 -0800 | [diff] [blame] | 67 | private Throwable mInvocationFailure = null; |
Julien Desprez | 079ab2f | 2019-03-12 10:03:45 -0700 | [diff] [blame] | 68 | /** Whether or not a testModuleStart had currently been called. */ |
| 69 | private boolean mModuleInProgress = false; |
Julien Desprez | dbc1f17 | 2018-12-21 12:14:33 -0800 | [diff] [blame] | 70 | |
Julien Desprez | 5035aa6 | 2019-07-27 17:11:57 -0700 | [diff] [blame] | 71 | @Override |
| 72 | public boolean supportGranularResults() { |
| 73 | return mReportGranularResults; |
| 74 | } |
| 75 | |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 76 | /** |
| 77 | * Handling of the partial invocation test record proto after {@link |
| 78 | * #invocationStarted(IInvocationContext)} occurred. |
| 79 | * |
| 80 | * @param invocationStartRecord The partial proto populated after the invocationStart. |
Julien Desprez | 329294b | 2018-07-25 10:16:59 -0700 | [diff] [blame] | 81 | * @param invocationContext The invocation {@link IInvocationContext}. |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 82 | */ |
Julien Desprez | 329294b | 2018-07-25 10:16:59 -0700 | [diff] [blame] | 83 | public void processStartInvocation( |
| 84 | TestRecord invocationStartRecord, IInvocationContext invocationContext) {} |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 85 | |
| 86 | /** |
| 87 | * Handling of the final proto with all results. |
| 88 | * |
| 89 | * @param finalRecord The finalized proto with all the invocation results. |
| 90 | */ |
| 91 | public void processFinalProto(TestRecord finalRecord) {} |
| 92 | |
| 93 | /** |
| 94 | * Handling of the partial module record proto after {@link |
| 95 | * #testModuleStarted(IInvocationContext)} occurred. |
| 96 | * |
| 97 | * @param moduleStartRecord The partial proto representing the module. |
| 98 | */ |
| 99 | public void processTestModuleStarted(TestRecord moduleStartRecord) {} |
| 100 | |
| 101 | /** |
| 102 | * Handling of the finalized module record proto after {@link #testModuleEnded()} occurred. |
| 103 | * |
| 104 | * @param moduleRecord The finalized proto representing the module. |
| 105 | */ |
| 106 | public void processTestModuleEnd(TestRecord moduleRecord) {} |
| 107 | |
| 108 | /** |
| 109 | * Handling of the partial test run record proto after {@link #testRunStarted(String, int)} |
| 110 | * occurred. |
| 111 | * |
| 112 | * @param runStartedRecord The partial proto representing the run. |
| 113 | */ |
| 114 | public void processTestRunStarted(TestRecord runStartedRecord) {} |
| 115 | |
| 116 | /** |
| 117 | * Handling of the finalized run record proto after {@link #testRunEnded(long, HashMap)} |
| 118 | * occurred. |
| 119 | * |
| 120 | * @param runRecord The finalized proto representing the run. |
Julien Desprez | 1bece77 | 2019-05-20 12:15:11 -0700 | [diff] [blame] | 121 | * @param moduleInProgress whether or not a module is in progress. |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 122 | */ |
Julien Desprez | 1bece77 | 2019-05-20 12:15:11 -0700 | [diff] [blame] | 123 | public void processTestRunEnded(TestRecord runRecord, boolean moduleInProgress) {} |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 124 | |
| 125 | /** |
| 126 | * Handling of the partial test case record proto after {@link #testStarted(TestDescription, |
| 127 | * long)} occurred. |
| 128 | * |
| 129 | * @param testCaseStartedRecord The partial proto representing the test case. |
| 130 | */ |
| 131 | public void processTestCaseStarted(TestRecord testCaseStartedRecord) {} |
| 132 | |
| 133 | /** |
| 134 | * Handling of the finalized test case record proto after {@link #testEnded(TestDescription, |
| 135 | * long, HashMap)} occurred. |
| 136 | * |
| 137 | * @param testCaseRecord The finalized proto representing a test case. |
| 138 | */ |
| 139 | public void processTestCaseEnded(TestRecord testCaseRecord) {} |
| 140 | |
| 141 | // Invocation events |
| 142 | |
| 143 | @Override |
| 144 | public final void invocationStarted(IInvocationContext context) { |
| 145 | mLatestChild = new Stack<>(); |
| 146 | mInvocationRecordBuilder = TestRecord.newBuilder(); |
| 147 | // Set invocation unique id |
| 148 | mInvocationRecordBuilder.setTestRecordId(UUID.randomUUID().toString()); |
| 149 | |
| 150 | // Populate start time of invocation |
| 151 | mInvocationStartTime = System.currentTimeMillis(); |
| 152 | Timestamp startTime = createTimeStamp(mInvocationStartTime); |
| 153 | mInvocationRecordBuilder.setStartTime(startTime); |
Julien Desprez | 5070602 | 2018-07-18 15:41:44 -0700 | [diff] [blame] | 154 | mInvocationRecordBuilder.setDescription(Any.pack(context.toProto())); |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 155 | |
Julien Desprez | f066b70 | 2018-11-07 11:10:08 -0800 | [diff] [blame] | 156 | mContext = context; |
| 157 | |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 158 | // Put the invocation record at the bottom of the stack |
| 159 | mLatestChild.add(mInvocationRecordBuilder); |
| 160 | |
| 161 | // Send the invocation proto with the currently set information to indicate the beginning |
| 162 | // of the invocation. |
| 163 | TestRecord startInvocationProto = mInvocationRecordBuilder.build(); |
| 164 | try { |
Julien Desprez | 329294b | 2018-07-25 10:16:59 -0700 | [diff] [blame] | 165 | processStartInvocation(startInvocationProto, context); |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 166 | } catch (RuntimeException e) { |
| 167 | CLog.e("Failed to process invocation started:"); |
| 168 | CLog.e(e); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | @Override |
Julien Desprez | dbc1f17 | 2018-12-21 12:14:33 -0800 | [diff] [blame] | 173 | public void invocationFailed(Throwable cause) { |
| 174 | mInvocationFailure = cause; |
| 175 | } |
| 176 | |
| 177 | @Override |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 178 | public final void invocationEnded(long elapsedTime) { |
Julien Desprez | 079ab2f | 2019-03-12 10:03:45 -0700 | [diff] [blame] | 179 | if (mModuleInProgress) { |
| 180 | // If we had a module in progress, and a new module start occurs, complete the call |
| 181 | testModuleEnded(); |
| 182 | } |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 183 | // Populate end time of invocation |
| 184 | Timestamp endTime = createTimeStamp(mInvocationStartTime + elapsedTime); |
| 185 | mInvocationRecordBuilder.setEndTime(endTime); |
Julien Desprez | f066b70 | 2018-11-07 11:10:08 -0800 | [diff] [blame] | 186 | // Update the context in case it changed |
| 187 | mInvocationRecordBuilder.setDescription(Any.pack(mContext.toProto())); |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 188 | |
Julien Desprez | dbc1f17 | 2018-12-21 12:14:33 -0800 | [diff] [blame] | 189 | if (mInvocationFailure != null) { |
| 190 | DebugInfo.Builder debugBuilder = DebugInfo.newBuilder(); |
Julien Desprez | 14a2ceb | 2019-07-15 12:03:17 -0700 | [diff] [blame] | 191 | if (mInvocationFailure.getMessage() != null) { |
| 192 | debugBuilder.setErrorMessage(mInvocationFailure.getMessage()); |
| 193 | } |
Julien Desprez | dbc1f17 | 2018-12-21 12:14:33 -0800 | [diff] [blame] | 194 | debugBuilder.setTrace(StreamUtil.getStackTrace(mInvocationFailure)); |
| 195 | mInvocationRecordBuilder.setDebugInfo(debugBuilder); |
| 196 | } |
| 197 | |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 198 | // Finalize the protobuf handling: where to put the results. |
| 199 | TestRecord record = mInvocationRecordBuilder.build(); |
| 200 | try { |
| 201 | processFinalProto(record); |
| 202 | } catch (RuntimeException e) { |
| 203 | CLog.e("Failed to process invocation ended:"); |
| 204 | CLog.e(e); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | // Module events (optional when there is no suite) |
| 209 | |
| 210 | @Override |
| 211 | public final void testModuleStarted(IInvocationContext moduleContext) { |
Julien Desprez | 079ab2f | 2019-03-12 10:03:45 -0700 | [diff] [blame] | 212 | if (mModuleInProgress) { |
| 213 | // If we had a module in progress, and a new module start occurs, complete the call |
| 214 | testModuleEnded(); |
| 215 | } |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 216 | TestRecord.Builder moduleBuilder = TestRecord.newBuilder(); |
| 217 | moduleBuilder.setParentTestRecordId(mInvocationRecordBuilder.getTestRecordId()); |
| 218 | moduleBuilder.setTestRecordId( |
| 219 | moduleContext.getAttributes().get(ModuleDefinition.MODULE_ID).get(0)); |
| 220 | moduleBuilder.setStartTime(createTimeStamp(System.currentTimeMillis())); |
Julien Desprez | 5070602 | 2018-07-18 15:41:44 -0700 | [diff] [blame] | 221 | moduleBuilder.setDescription(Any.pack(moduleContext.toProto())); |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 222 | mLatestChild.add(moduleBuilder); |
Julien Desprez | 079ab2f | 2019-03-12 10:03:45 -0700 | [diff] [blame] | 223 | mModuleInProgress = true; |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 224 | try { |
| 225 | processTestModuleStarted(moduleBuilder.build()); |
| 226 | } catch (RuntimeException e) { |
| 227 | CLog.e("Failed to process invocation ended:"); |
| 228 | CLog.e(e); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | @Override |
| 233 | public final void testModuleEnded() { |
| 234 | TestRecord.Builder moduleBuilder = mLatestChild.pop(); |
Julien Desprez | 079ab2f | 2019-03-12 10:03:45 -0700 | [diff] [blame] | 235 | mModuleInProgress = false; |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 236 | moduleBuilder.setEndTime(createTimeStamp(System.currentTimeMillis())); |
| 237 | TestRecord.Builder parentBuilder = mLatestChild.peek(); |
| 238 | |
| 239 | // Finalize the module and track it in the child |
| 240 | TestRecord moduleRecord = moduleBuilder.build(); |
| 241 | parentBuilder.addChildren(createChildReference(moduleRecord)); |
| 242 | try { |
| 243 | processTestModuleEnd(moduleRecord); |
| 244 | } catch (RuntimeException e) { |
| 245 | CLog.e("Failed to process test module end:"); |
| 246 | CLog.e(e); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | // Run events |
| 251 | |
| 252 | @Override |
| 253 | public final void testRunStarted(String runName, int testCount) { |
Julien Desprez | b18ff87 | 2018-08-16 10:34:28 -0700 | [diff] [blame] | 254 | testRunStarted(runName, testCount, 0); |
| 255 | } |
| 256 | |
| 257 | @Override |
| 258 | public void testRunStarted(String runName, int testCount, int attemptNumber) { |
Julien Desprez | bd1f376 | 2019-04-19 14:57:00 -0700 | [diff] [blame] | 259 | testRunStarted(runName, testCount, attemptNumber, System.currentTimeMillis()); |
| 260 | } |
| 261 | |
| 262 | @Override |
| 263 | public void testRunStarted(String runName, int testCount, int attemptNumber, long startTime) { |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 264 | TestRecord.Builder runBuilder = TestRecord.newBuilder(); |
| 265 | TestRecord.Builder parent = mLatestChild.peek(); |
| 266 | runBuilder.setParentTestRecordId(parent.getTestRecordId()); |
| 267 | runBuilder.setTestRecordId(runName); |
| 268 | runBuilder.setNumExpectedChildren(testCount); |
Julien Desprez | bd1f376 | 2019-04-19 14:57:00 -0700 | [diff] [blame] | 269 | runBuilder.setStartTime(createTimeStamp(startTime)); |
Julien Desprez | b18ff87 | 2018-08-16 10:34:28 -0700 | [diff] [blame] | 270 | runBuilder.setAttemptId(attemptNumber); |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 271 | |
| 272 | mLatestChild.add(runBuilder); |
| 273 | try { |
| 274 | processTestRunStarted(runBuilder.build()); |
| 275 | } catch (RuntimeException e) { |
| 276 | CLog.e("Failed to process invocation ended:"); |
| 277 | CLog.e(e); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | @Override |
| 282 | public final void testRunFailed(String errorMessage) { |
| 283 | TestRecord.Builder current = mLatestChild.peek(); |
| 284 | DebugInfo.Builder debugBuilder = DebugInfo.newBuilder(); |
| 285 | debugBuilder.setErrorMessage(errorMessage); |
| 286 | if (TestStatus.UNKNOWN.equals(current.getStatus())) { |
| 287 | current.setDebugInfo(debugBuilder.build()); |
| 288 | } else { |
| 289 | // We are in a test case and we need the run parent. |
| 290 | TestRecord.Builder test = mLatestChild.pop(); |
| 291 | TestRecord.Builder run = mLatestChild.peek(); |
| 292 | run.setDebugInfo(debugBuilder.build()); |
| 293 | // Re-add the test |
| 294 | mLatestChild.add(test); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | @Override |
Julien Desprez | 8ee0923 | 2020-02-06 10:30:11 -0800 | [diff] [blame] | 299 | public final void testRunFailed(FailureDescription failure) { |
| 300 | TestRecord.Builder current = mLatestChild.peek(); |
| 301 | DebugInfo.Builder debugBuilder = DebugInfo.newBuilder(); |
| 302 | debugBuilder.setErrorMessage(failure.toString()); |
| 303 | if (failure.getFailureStatus() != null) { |
| 304 | debugBuilder.setFailureStatus(failure.getFailureStatus()); |
| 305 | } |
Julien Desprez | 221aaf1 | 2020-02-27 14:17:03 -0800 | [diff] [blame^] | 306 | DebugInfoContext.Builder debugContext = DebugInfoContext.newBuilder(); |
| 307 | if (failure.getActionInProgress() != null) { |
| 308 | debugContext.setActionInProgress(failure.getActionInProgress().toString()); |
| 309 | } |
| 310 | if (!Strings.isNullOrEmpty(failure.getDebugHelpMessage())) { |
| 311 | debugContext.setDebugHelpMessage(failure.getDebugHelpMessage()); |
| 312 | } |
| 313 | debugBuilder.setDebugInfoContext(debugContext.build()); |
| 314 | |
Julien Desprez | 8ee0923 | 2020-02-06 10:30:11 -0800 | [diff] [blame] | 315 | if (TestStatus.UNKNOWN.equals(current.getStatus())) { |
| 316 | current.setDebugInfo(debugBuilder.build()); |
| 317 | } else { |
| 318 | // We are in a test case and we need the run parent. |
| 319 | TestRecord.Builder test = mLatestChild.pop(); |
| 320 | TestRecord.Builder run = mLatestChild.peek(); |
| 321 | run.setDebugInfo(debugBuilder.build()); |
| 322 | // Re-add the test |
| 323 | mLatestChild.add(test); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | @Override |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 328 | public final void testRunEnded(long elapsedTimeMillis, HashMap<String, Metric> runMetrics) { |
| 329 | TestRecord.Builder runBuilder = mLatestChild.pop(); |
Julien Desprez | bd1f376 | 2019-04-19 14:57:00 -0700 | [diff] [blame] | 330 | long startTime = timeStampToMillis(runBuilder.getStartTime()); |
| 331 | runBuilder.setEndTime(createTimeStamp(startTime + elapsedTimeMillis)); |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 332 | runBuilder.putAllMetrics(runMetrics); |
| 333 | TestRecord.Builder parentBuilder = mLatestChild.peek(); |
| 334 | |
| 335 | // Finalize the run and track it in the child |
| 336 | TestRecord runRecord = runBuilder.build(); |
| 337 | parentBuilder.addChildren(createChildReference(runRecord)); |
| 338 | try { |
Julien Desprez | 1bece77 | 2019-05-20 12:15:11 -0700 | [diff] [blame] | 339 | processTestRunEnded(runRecord, mModuleInProgress); |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 340 | } catch (RuntimeException e) { |
| 341 | CLog.e("Failed to process test run end:"); |
| 342 | CLog.e(e); |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | // test case events |
| 347 | |
| 348 | @Override |
Julien Desprez | 329294b | 2018-07-25 10:16:59 -0700 | [diff] [blame] | 349 | public final void testStarted(TestDescription test) { |
| 350 | testStarted(test, System.currentTimeMillis()); |
| 351 | } |
| 352 | |
| 353 | @Override |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 354 | public final void testStarted(TestDescription test, long startTime) { |
| 355 | TestRecord.Builder testBuilder = TestRecord.newBuilder(); |
| 356 | TestRecord.Builder parent = mLatestChild.peek(); |
| 357 | testBuilder.setParentTestRecordId(parent.getTestRecordId()); |
| 358 | testBuilder.setTestRecordId(test.toString()); |
| 359 | testBuilder.setStartTime(createTimeStamp(startTime)); |
| 360 | testBuilder.setStatus(TestStatus.PASS); |
| 361 | |
| 362 | mLatestChild.add(testBuilder); |
| 363 | try { |
| 364 | processTestCaseStarted(testBuilder.build()); |
| 365 | } catch (RuntimeException e) { |
| 366 | CLog.e("Failed to process invocation ended:"); |
| 367 | CLog.e(e); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | @Override |
Julien Desprez | 329294b | 2018-07-25 10:16:59 -0700 | [diff] [blame] | 372 | public void testEnded(TestDescription test, HashMap<String, Metric> testMetrics) { |
| 373 | testEnded(test, System.currentTimeMillis(), testMetrics); |
| 374 | } |
| 375 | |
| 376 | @Override |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 377 | public final void testEnded( |
| 378 | TestDescription test, long endTime, HashMap<String, Metric> testMetrics) { |
| 379 | TestRecord.Builder testBuilder = mLatestChild.pop(); |
| 380 | testBuilder.setEndTime(createTimeStamp(endTime)); |
| 381 | testBuilder.putAllMetrics(testMetrics); |
| 382 | TestRecord.Builder parentBuilder = mLatestChild.peek(); |
| 383 | |
| 384 | // Finalize the run and track it in the child |
| 385 | TestRecord testCaseRecord = testBuilder.build(); |
| 386 | parentBuilder.addChildren(createChildReference(testCaseRecord)); |
| 387 | try { |
| 388 | processTestCaseEnded(testCaseRecord); |
| 389 | } catch (RuntimeException e) { |
| 390 | CLog.e("Failed to process test case end:"); |
| 391 | CLog.e(e); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | @Override |
| 396 | public final void testFailed(TestDescription test, String trace) { |
| 397 | TestRecord.Builder testBuilder = mLatestChild.peek(); |
| 398 | |
| 399 | testBuilder.setStatus(TestStatus.FAIL); |
| 400 | DebugInfo.Builder debugBuilder = DebugInfo.newBuilder(); |
| 401 | // FIXME: extract the error message from the trace |
| 402 | debugBuilder.setErrorMessage(trace); |
| 403 | debugBuilder.setTrace(trace); |
| 404 | testBuilder.setDebugInfo(debugBuilder.build()); |
| 405 | } |
| 406 | |
| 407 | @Override |
Julien Desprez | 8ee0923 | 2020-02-06 10:30:11 -0800 | [diff] [blame] | 408 | public final void testFailed(TestDescription test, FailureDescription failure) { |
| 409 | TestRecord.Builder testBuilder = mLatestChild.peek(); |
| 410 | |
| 411 | testBuilder.setStatus(TestStatus.FAIL); |
| 412 | DebugInfo.Builder debugBuilder = DebugInfo.newBuilder(); |
| 413 | // FIXME: extract the error message from the trace |
| 414 | debugBuilder.setErrorMessage(failure.toString()); |
| 415 | debugBuilder.setTrace(failure.toString()); |
| 416 | if (failure.getFailureStatus() != null) { |
| 417 | debugBuilder.setFailureStatus(failure.getFailureStatus()); |
| 418 | } |
Julien Desprez | 221aaf1 | 2020-02-27 14:17:03 -0800 | [diff] [blame^] | 419 | DebugInfoContext.Builder debugContext = DebugInfoContext.newBuilder(); |
| 420 | if (failure.getActionInProgress() != null) { |
| 421 | debugContext.setActionInProgress(failure.getActionInProgress().toString()); |
| 422 | } |
| 423 | if (!Strings.isNullOrEmpty(failure.getDebugHelpMessage())) { |
| 424 | debugContext.setDebugHelpMessage(failure.getDebugHelpMessage()); |
| 425 | } |
| 426 | debugBuilder.setDebugInfoContext(debugContext.build()); |
| 427 | |
Julien Desprez | 8ee0923 | 2020-02-06 10:30:11 -0800 | [diff] [blame] | 428 | testBuilder.setDebugInfo(debugBuilder.build()); |
| 429 | } |
| 430 | |
| 431 | @Override |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 432 | public final void testIgnored(TestDescription test) { |
| 433 | TestRecord.Builder testBuilder = mLatestChild.peek(); |
| 434 | testBuilder.setStatus(TestStatus.IGNORED); |
| 435 | } |
| 436 | |
| 437 | @Override |
| 438 | public final void testAssumptionFailure(TestDescription test, String trace) { |
| 439 | TestRecord.Builder testBuilder = mLatestChild.peek(); |
| 440 | |
| 441 | testBuilder.setStatus(TestStatus.ASSUMPTION_FAILURE); |
| 442 | DebugInfo.Builder debugBuilder = DebugInfo.newBuilder(); |
| 443 | // FIXME: extract the error message from the trace |
| 444 | debugBuilder.setErrorMessage(trace); |
| 445 | debugBuilder.setTrace(trace); |
| 446 | testBuilder.setDebugInfo(debugBuilder.build()); |
| 447 | } |
| 448 | |
Julien Desprez | 53af5fe | 2020-02-19 10:31:38 -0800 | [diff] [blame] | 449 | @Override |
| 450 | public final void testAssumptionFailure(TestDescription test, FailureDescription failure) { |
| 451 | TestRecord.Builder testBuilder = mLatestChild.peek(); |
| 452 | |
| 453 | testBuilder.setStatus(TestStatus.ASSUMPTION_FAILURE); |
| 454 | DebugInfo.Builder debugBuilder = DebugInfo.newBuilder(); |
| 455 | // FIXME: extract the error message from the trace |
| 456 | debugBuilder.setErrorMessage(failure.toString()); |
| 457 | debugBuilder.setTrace(failure.toString()); |
| 458 | if (failure.getFailureStatus() != null) { |
| 459 | debugBuilder.setFailureStatus(failure.getFailureStatus()); |
| 460 | } |
| 461 | testBuilder.setDebugInfo(debugBuilder.build()); |
| 462 | } |
| 463 | |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 464 | // log events |
| 465 | |
| 466 | @Override |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 467 | public final void logAssociation(String dataName, LogFile logFile) { |
| 468 | TestRecord.Builder current = mLatestChild.peek(); |
| 469 | Map<String, Any> fullmap = new HashMap<>(); |
Joseph Murphy | e87b17f | 2019-12-06 17:53:33 -0800 | [diff] [blame] | 470 | fullmap.putAll(current.getArtifactsMap()); |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 471 | Any any = Any.pack(createFileProto(logFile)); |
| 472 | fullmap.put(dataName, any); |
| 473 | current.putAllArtifacts(fullmap); |
| 474 | } |
| 475 | |
| 476 | private ChildReference createChildReference(TestRecord record) { |
Julien Desprez | a49034d | 2018-07-24 10:07:10 -0700 | [diff] [blame] | 477 | ChildReference.Builder child = ChildReference.newBuilder(); |
| 478 | child.setTestRecordId(record.getTestRecordId()); |
| 479 | child.setInlineTestRecord(record); |
| 480 | return child.build(); |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | /** Create and populate Timestamp as recommended in the javadoc of the Timestamp proto. */ |
| 484 | private Timestamp createTimeStamp(long currentTimeMs) { |
| 485 | return Timestamp.newBuilder() |
| 486 | .setSeconds(currentTimeMs / 1000) |
| 487 | .setNanos((int) ((currentTimeMs % 1000) * 1000000)) |
| 488 | .build(); |
| 489 | } |
| 490 | |
Julien Desprez | bd1f376 | 2019-04-19 14:57:00 -0700 | [diff] [blame] | 491 | private long timeStampToMillis(Timestamp stamp) { |
| 492 | return stamp.getSeconds() * 1000L + (stamp.getNanos() / 1000000L); |
| 493 | } |
| 494 | |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 495 | private LogFileInfo createFileProto(LogFile logFile) { |
| 496 | LogFileInfo.Builder logFileBuilder = LogFileInfo.newBuilder(); |
| 497 | logFileBuilder |
| 498 | .setPath(logFile.getPath()) |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 499 | .setIsText(logFile.isText()) |
Julien Desprez | a49034d | 2018-07-24 10:07:10 -0700 | [diff] [blame] | 500 | .setLogType(logFile.getType().toString()) |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 501 | .setIsCompressed(logFile.isCompressed()) |
| 502 | .setSize(logFile.getSize()); |
Julien Desprez | 329294b | 2018-07-25 10:16:59 -0700 | [diff] [blame] | 503 | // Url can be null so avoid NPE by checking it before setting the proto |
| 504 | if (logFile.getUrl() != null) { |
| 505 | logFileBuilder.setUrl(logFile.getUrl()); |
| 506 | } |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 507 | return logFileBuilder.build(); |
| 508 | } |
| 509 | } |