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;
17 
18 import com.android.ddmlib.testrunner.TestIdentifier;
19 import com.android.tradefed.build.IBuildInfo;
20 import com.android.tradefed.invoker.IInvocationContext;
21 import com.android.tradefed.log.LogUtil.CLog;
22 import com.android.tradefed.testtype.IAbi;
23 import com.android.tradefed.util.SubprocessEventHelper.BaseTestEventInfo;
24 import com.android.tradefed.util.SubprocessEventHelper.FailedTestEventInfo;
25 import com.android.tradefed.util.SubprocessEventHelper.InvocationFailedEventInfo;
26 import com.android.tradefed.util.SubprocessEventHelper.InvocationStartedEventInfo;
27 import com.android.tradefed.util.SubprocessEventHelper.TestEndedEventInfo;
28 import com.android.tradefed.util.SubprocessEventHelper.TestRunEndedEventInfo;
29 import com.android.tradefed.util.SubprocessEventHelper.TestRunFailedEventInfo;
30 import com.android.tradefed.util.SubprocessEventHelper.TestRunStartedEventInfo;
31 import com.android.tradefed.util.SubprocessEventHelper.TestStartedEventInfo;
32 import com.android.tradefed.util.SubprocessEventHelper.TestModuleStartedEventInfo;
33 import com.android.tradefed.util.SubprocessEventHelper.LogAssociationEventInfo;
34 import com.android.tradefed.util.SubprocessTestResultsParser;
35 
36 import org.json.JSONObject;
37 
38 import java.io.Serializable;
39 import java.util.Map;
40 
41 /**
42  * A frozen implementation of the subprocess results reporter which should remain compatible with
43  * earlier versions of TF/CTS (e.g. 8+), despite changes in its superclass.
44  */
45 public final class LegacySubprocessResultsReporter extends SubprocessResultsReporter {
46 
47     /* Legacy method compatible with TF/CTS 8+. */
testAssumptionFailure(TestIdentifier testId, String trace)48     public void testAssumptionFailure(TestIdentifier testId, String trace) {
49         FailedTestEventInfo info =
50                 new FailedTestEventInfo(testId.getClassName(), testId.getTestName(), trace);
51         printEvent(SubprocessTestResultsParser.StatusKeys.TEST_ASSUMPTION_FAILURE, info);
52     }
53 
54     /* Legacy method compatible with TF/CTS 8+. */
testEnded(TestIdentifier testId, Map<String, String> metrics)55     public void testEnded(TestIdentifier testId, Map<String, String> metrics) {
56         testEnded(testId, System.currentTimeMillis(), metrics);
57     }
58 
59     /* Legacy method compatible with TF/CTS 8+. */
testEnded(TestIdentifier testId, long endTime, Map<String, String> metrics)60     public void testEnded(TestIdentifier testId, long endTime, Map<String, String> metrics) {
61         TestEndedEventInfo info =
62                 new TestEndedEventInfo(
63                         testId.getClassName(), testId.getTestName(), endTime, metrics);
64         printEvent(SubprocessTestResultsParser.StatusKeys.TEST_ENDED, info);
65     }
66 
67     /* Legacy method compatible with TF/CTS 8+. */
testFailed(TestIdentifier testId, String reason)68     public void testFailed(TestIdentifier testId, String reason) {
69         FailedTestEventInfo info =
70                 new FailedTestEventInfo(testId.getClassName(), testId.getTestName(), reason);
71         printEvent(SubprocessTestResultsParser.StatusKeys.TEST_FAILED, info);
72     }
73 
74     /* Legacy method compatible with TF/CTS 8+. */
testIgnored(TestIdentifier testId)75     public void testIgnored(TestIdentifier testId) {
76         BaseTestEventInfo info = new BaseTestEventInfo(testId.getClassName(), testId.getTestName());
77         printEvent(SubprocessTestResultsParser.StatusKeys.TEST_IGNORED, info);
78     }
79 
80     /* Legacy method compatible with TF/CTS 8+. */
testStarted(TestIdentifier testId)81     public void testStarted(TestIdentifier testId) {
82         testStarted(testId, System.currentTimeMillis());
83     }
84 
85     /* Legacy method compatible with TF/CTS 8+. */
testStarted(TestIdentifier testId, long startTime)86     public void testStarted(TestIdentifier testId, long startTime) {
87         TestStartedEventInfo info =
88                 new TestStartedEventInfo(testId.getClassName(), testId.getTestName(), startTime);
89         printEvent(SubprocessTestResultsParser.StatusKeys.TEST_STARTED, info);
90     }
91 
92     /* Legacy method compatible with TF/CTS 8+. */
invocationStarted(IBuildInfo buildInfo)93     public void invocationStarted(IBuildInfo buildInfo) {
94         InvocationStartedEventInfo info =
95                 new InvocationStartedEventInfo(buildInfo.getTestTag(), System.currentTimeMillis());
96         printEvent(SubprocessTestResultsParser.StatusKeys.INVOCATION_STARTED, info);
97     }
98 
99     /* Legacy method compatible with TF/CTS 8+. */
100     @Override
invocationFailed(Throwable cause)101     public void invocationFailed(Throwable cause) {
102         InvocationFailedEventInfo info = new InvocationFailedEventInfo(cause);
103         printEvent(SubprocessTestResultsParser.StatusKeys.INVOCATION_FAILED, info);
104     }
105 
106     /* Legacy method compatible with TF/CTS 8. */
107     @Override
invocationEnded(long elapsedTime)108     public void invocationEnded(long elapsedTime) {
109         // ignore
110     }
111 
112     /* Legacy method compatible with TF/CTS 8+. */
113     @Override
testRunFailed(String reason)114     public void testRunFailed(String reason) {
115         TestRunFailedEventInfo info = new TestRunFailedEventInfo(reason);
116         printEvent(SubprocessTestResultsParser.StatusKeys.TEST_RUN_FAILED, info);
117     }
118 
119     /* Legacy method compatible with TF/CTS 8+. */
120     @Override
testRunStarted(String runName, int testCount)121     public void testRunStarted(String runName, int testCount) {
122         TestRunStartedEventInfo info = new TestRunStartedEventInfo(runName, testCount);
123         printEvent(SubprocessTestResultsParser.StatusKeys.TEST_RUN_STARTED, info);
124     }
125 
126     /* Legacy method compatible with TF/CTS 8+. */
127     @Override
testRunEnded(long time, Map<String, String> runMetrics)128     public void testRunEnded(long time, Map<String, String> runMetrics) {
129         TestRunEndedEventInfo info = new TestRunEndedEventInfo(time, runMetrics);
130         printEvent(SubprocessTestResultsParser.StatusKeys.TEST_RUN_ENDED, info);
131     }
132 
133     /* Legacy method compatible with TF/CTS 9+ (skipped in 8.1 and not called in 8). */
134     @Override
testModuleStarted(IInvocationContext moduleContext)135     public void testModuleStarted(IInvocationContext moduleContext) {
136         if (!Serializable.class.isAssignableFrom(IAbi.class)) {
137             // TODO(b/154349022): remove after releasing serialization fix
138             // Test packages prior to 1d6869f will fail with a not serializable exception.
139             // see https://cs.android.com/android/_/android/platform/tools/tradefederation/+/1d6869f
140             CLog.d("testModuleStarted is called but ignored intentionally");
141             return;
142         }
143         TestModuleStartedEventInfo info = new TestModuleStartedEventInfo(moduleContext);
144         printEvent(SubprocessTestResultsParser.StatusKeys.TEST_MODULE_STARTED, info);
145     }
146 
147     /* Legacy method compatible with TF/CTS 9+ (skipped in 8.1 and not called in 8). */
148     @Override
testModuleEnded()149     public void testModuleEnded() {
150         if (!Serializable.class.isAssignableFrom(IAbi.class)) {
151             // TODO(b/154349022): remove after releasing serialization fix
152             // Test packages prior to 1d6869f will fail with a not serializable exception.
153             // see https://cs.android.com/android/_/android/platform/tools/tradefederation/+/1d6869f
154             CLog.d("testModuleEnded is called but ignored intentionally");
155             return;
156         }
157         printEvent(SubprocessTestResultsParser.StatusKeys.TEST_MODULE_ENDED, new JSONObject());
158     }
159 
160     /* Legacy method compatible with TF/CTS 8.1+ (not called in 8). */
161     @Override
testLogSaved( String dataName, LogDataType dataType, InputStreamSource dataStream, LogFile logFile)162     public void testLogSaved(
163             String dataName, LogDataType dataType, InputStreamSource dataStream, LogFile logFile) {
164         // ignore
165     }
166 
167     /* Legacy method compatible with TF/CTS 9+ (skipped in 8.1 and not called in 8). */
168     @Override
logAssociation(String dataName, LogFile logFile)169     public void logAssociation(String dataName, LogFile logFile) {
170         if (!Serializable.class.isAssignableFrom(LogFile.class)) {
171             // TODO(b/154349022): remove after releasing serialization fix
172             // Test packages prior to 52fb8df will fail with a not serializable exception.
173             // see https://cs.android.com/android/_/android/platform/tools/tradefederation/+/52fb8df
174             CLog.d("logAssociation is called but ignored intentionally");
175             return;
176         }
177         LogAssociationEventInfo info = new LogAssociationEventInfo(dataName, logFile);
178         printEvent(SubprocessTestResultsParser.StatusKeys.LOG_ASSOCIATION, info);
179     }
180 
181     /* Legacy method compatible with TF/CTS 8.1+ (not called in 8). */
182     @Override
setLogSaver(ILogSaver logSaver)183     public void setLogSaver(ILogSaver logSaver) {
184         // ignore
185     }
186 }
187