blob: f4e394917ecae67ef064c4bb2ae129981ad15a1b [file] [log] [blame]
Julien Desprez093c3f62018-05-21 16:23:37 -07001/*
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 */
16package com.android.tradefed.result.proto;
17
18import static org.junit.Assert.assertEquals;
19import static org.junit.Assert.assertNotNull;
20
Julien Desprez50706022018-07-18 15:41:44 -070021import com.android.tradefed.config.ConfigurationDescriptor;
Julien Desprez093c3f62018-05-21 16:23:37 -070022import com.android.tradefed.invoker.IInvocationContext;
23import com.android.tradefed.invoker.InvocationContext;
24import com.android.tradefed.metrics.proto.MetricMeasurement.Metric;
Julien Desprez7fc54252020-04-30 15:08:45 -070025import com.android.tradefed.result.FailureDescription;
Julien Desprez093c3f62018-05-21 16:23:37 -070026import com.android.tradefed.result.LogDataType;
27import com.android.tradefed.result.LogFile;
28import com.android.tradefed.result.TestDescription;
Julien Desprezed8bd9b2018-07-13 08:50:20 -070029import com.android.tradefed.result.proto.TestRecordProto.ChildReference;
Julien Desprez7fc54252020-04-30 15:08:45 -070030import com.android.tradefed.result.proto.TestRecordProto.DebugInfo;
31import com.android.tradefed.result.proto.TestRecordProto.DebugInfoContext;
32import com.android.tradefed.result.proto.TestRecordProto.FailureStatus;
Julien Desprezed8bd9b2018-07-13 08:50:20 -070033import com.android.tradefed.result.proto.TestRecordProto.TestRecord;
34import com.android.tradefed.result.proto.TestRecordProto.TestStatus;
Julien Desprez093c3f62018-05-21 16:23:37 -070035import com.android.tradefed.testtype.suite.ModuleDefinition;
Julien Desprez7fc54252020-04-30 15:08:45 -070036import com.android.tradefed.util.SerializationUtil;
Julien Desprez093c3f62018-05-21 16:23:37 -070037import com.android.tradefed.util.proto.TfMetricProtoUtil;
38
Julien Desprez093c3f62018-05-21 16:23:37 -070039import org.junit.Before;
40import org.junit.Test;
41import org.junit.runner.RunWith;
42import org.junit.runners.JUnit4;
43
44import java.util.HashMap;
45
46/** Unit tests for {@link ProtoResultReporter}. */
47@RunWith(JUnit4.class)
48public class ProtoResultReporterTest {
49
50 private ProtoResultReporter mReporter;
51 private TestRecord mFinalRecord;
52 private IInvocationContext mInvocationContext;
53
54 public class TestableProtoResultReporter extends ProtoResultReporter {
55 @Override
56 public void processFinalProto(TestRecord finalRecord) {
57 mFinalRecord = finalRecord;
58 }
59 }
60
61 @Before
62 public void setUp() {
63 mReporter = new TestableProtoResultReporter();
64 mInvocationContext = new InvocationContext();
Julien Desprez50706022018-07-18 15:41:44 -070065 mInvocationContext.setConfigurationDescriptor(new ConfigurationDescriptor());
Julien Desprez093c3f62018-05-21 16:23:37 -070066 }
67
68 /** Test an invocation with the proto being populated. */
69 @Test
70 public void testFinalizeProto() {
71 // Invocation start
72 mReporter.invocationStarted(mInvocationContext);
73 // Run modules
74 mReporter.testModuleStarted(createModuleContext("arm64 module1"));
75 mReporter.testRunStarted("run1", 2);
76
77 TestDescription test1 = new TestDescription("class1", "test1");
78 mReporter.testStarted(test1, 5L);
79 mReporter.testEnded(test1, 10L, new HashMap<String, Metric>());
80
81 TestDescription test2 = new TestDescription("class1", "test2");
82 mReporter.testStarted(test2, 11L);
83 mReporter.testFailed(test2, "I failed");
84 HashMap<String, Metric> metrics = new HashMap<String, Metric>();
85 metrics.put("metric1", TfMetricProtoUtil.stringToMetric("value1"));
86 // test log
87 mReporter.logAssociation("log1", new LogFile("path", "url", false, LogDataType.TEXT, 5));
88
89 mReporter.testEnded(test2, 60L, metrics);
90 // run log
91 mReporter.logAssociation(
92 "run_log1", new LogFile("path", "url", false, LogDataType.LOGCAT, 5));
93 mReporter.testRunEnded(50L, new HashMap<String, Metric>());
94
95 mReporter.testModuleEnded();
96 mReporter.testModuleStarted(createModuleContext("arm32 module1"));
97 mReporter.testModuleEnded();
98 // Invocation ends
Julien Desprez14a2ceb2019-07-15 12:03:17 -070099 mReporter.invocationFailed(new NullPointerException());
Julien Desprez093c3f62018-05-21 16:23:37 -0700100 mReporter.invocationEnded(500L);
101
102 // ------ Verify that everything was populated ------
103 assertNotNull(mFinalRecord.getTestRecordId());
104 assertNotNull(mFinalRecord.getStartTime().getSeconds());
105 assertNotNull(mFinalRecord.getEndTime().getSeconds());
Julien Desprez14a2ceb2019-07-15 12:03:17 -0700106 assertNotNull(mFinalRecord.getDebugInfo());
Julien Desprez093c3f62018-05-21 16:23:37 -0700107
108 // The invocation has 2 modules
109 assertEquals(2, mFinalRecord.getChildrenCount());
110 ChildReference child1 = mFinalRecord.getChildrenList().get(0);
111
112 TestRecord module1 = child1.getInlineTestRecord();
113 // module1 has one test run
114 assertEquals(1, module1.getChildrenCount());
115 TestRecord run1 = module1.getChildrenList().get(0).getInlineTestRecord();
116 // run1 has 2 test cases
117 assertEquals(2, run1.getChildrenCount());
118 TestRecord testRecord1 = run1.getChildrenList().get(0).getInlineTestRecord();
119 assertEquals(TestStatus.PASS, testRecord1.getStatus());
120 TestRecord testRecord2 = run1.getChildrenList().get(1).getInlineTestRecord();
121 assertEquals(TestStatus.FAIL, testRecord2.getStatus());
122 assertEquals("I failed", testRecord2.getDebugInfo().getTrace());
123 // test 2 has 1 metric and 1 log artifact
Joseph Murphye87b17f2019-12-06 17:53:33 -0800124 assertEquals(1, testRecord2.getMetricsMap().size());
125 assertEquals(1, testRecord2.getArtifactsMap().size());
Julien Desprez093c3f62018-05-21 16:23:37 -0700126 // run 1 has one log file
Joseph Murphye87b17f2019-12-06 17:53:33 -0800127 assertEquals(1, run1.getArtifactsMap().size());
Julien Desprez093c3f62018-05-21 16:23:37 -0700128 }
129
130 /** Test when testRunFailed in called from within a test context (testStarted/TestEnded). */
131 @Test
132 public void testRunFail_interleavedWithTest() {
133 mReporter.invocationStarted(mInvocationContext);
134 // Run modules
135 mReporter.testRunStarted("run1", 2);
136
137 TestDescription test1 = new TestDescription("class1", "test1");
138 mReporter.testStarted(test1, 5L);
139 // test run failed inside a test
140 mReporter.testRunFailed("run failure");
141
142 mReporter.testEnded(test1, 10L, new HashMap<String, Metric>());
143
144 mReporter.testRunEnded(50L, new HashMap<String, Metric>());
145 // Invocation ends
146 mReporter.invocationEnded(500L);
147
148 assertEquals(1, mFinalRecord.getChildrenCount());
149 TestRecord run1 = mFinalRecord.getChildrenList().get(0).getInlineTestRecord();
150 assertEquals("run failure", run1.getDebugInfo().getErrorMessage());
151 }
152
Julien Desprez7fc54252020-04-30 15:08:45 -0700153 /** Test an invocation with the proto invocation failure being populated. */
154 @Test
155 public void testInvocationFailure() throws Exception {
156 // Invocation start
157 mReporter.invocationStarted(mInvocationContext);
158 // Invocation ends
159 FailureDescription invocationFailure = FailureDescription.create("error");
160 invocationFailure.setFailureStatus(FailureStatus.INFRA_FAILURE);
161 invocationFailure.setCause(new NullPointerException("error"));
162 mReporter.invocationFailed(invocationFailure);
163 mReporter.invocationEnded(500L);
164
165 // ------ Verify that everything was populated ------
166 assertNotNull(mFinalRecord.getTestRecordId());
167 assertNotNull(mFinalRecord.getStartTime().getSeconds());
168 assertNotNull(mFinalRecord.getEndTime().getSeconds());
169 assertNotNull(mFinalRecord.getDebugInfo());
170
171 DebugInfo invocFailure = mFinalRecord.getDebugInfo();
172 assertEquals("error", invocFailure.getErrorMessage());
173 assertEquals(FailureStatus.INFRA_FAILURE, invocFailure.getFailureStatus());
174 assertNotNull(invocFailure.getDebugInfoContext());
175 DebugInfoContext debugContext = invocFailure.getDebugInfoContext();
176 String serializedException = debugContext.getErrorType();
177 NullPointerException npe =
178 (NullPointerException) SerializationUtil.deserialize(serializedException);
179 assertEquals("error", npe.getMessage());
180 }
181
Julien Desprez093c3f62018-05-21 16:23:37 -0700182 /** Helper to create a module context. */
183 private IInvocationContext createModuleContext(String moduleId) {
184 IInvocationContext context = new InvocationContext();
185 context.addInvocationAttribute(ModuleDefinition.MODULE_ID, moduleId);
Julien Desprez50706022018-07-18 15:41:44 -0700186 context.setConfigurationDescriptor(new ConfigurationDescriptor());
Julien Desprez093c3f62018-05-21 16:23:37 -0700187 return context;
188 }
189}