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 | |
| 18 | import static org.junit.Assert.assertEquals; |
| 19 | import static org.junit.Assert.assertNotNull; |
| 20 | |
Julien Desprez | 5070602 | 2018-07-18 15:41:44 -0700 | [diff] [blame] | 21 | import com.android.tradefed.config.ConfigurationDescriptor; |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 22 | import com.android.tradefed.invoker.IInvocationContext; |
| 23 | import com.android.tradefed.invoker.InvocationContext; |
| 24 | import com.android.tradefed.metrics.proto.MetricMeasurement.Metric; |
Julien Desprez | 7fc5425 | 2020-04-30 15:08:45 -0700 | [diff] [blame^] | 25 | import com.android.tradefed.result.FailureDescription; |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 26 | import com.android.tradefed.result.LogDataType; |
| 27 | import com.android.tradefed.result.LogFile; |
| 28 | import com.android.tradefed.result.TestDescription; |
Julien Desprez | ed8bd9b | 2018-07-13 08:50:20 -0700 | [diff] [blame] | 29 | import com.android.tradefed.result.proto.TestRecordProto.ChildReference; |
Julien Desprez | 7fc5425 | 2020-04-30 15:08:45 -0700 | [diff] [blame^] | 30 | import com.android.tradefed.result.proto.TestRecordProto.DebugInfo; |
| 31 | import com.android.tradefed.result.proto.TestRecordProto.DebugInfoContext; |
| 32 | import com.android.tradefed.result.proto.TestRecordProto.FailureStatus; |
Julien Desprez | ed8bd9b | 2018-07-13 08:50:20 -0700 | [diff] [blame] | 33 | import com.android.tradefed.result.proto.TestRecordProto.TestRecord; |
| 34 | import com.android.tradefed.result.proto.TestRecordProto.TestStatus; |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 35 | import com.android.tradefed.testtype.suite.ModuleDefinition; |
Julien Desprez | 7fc5425 | 2020-04-30 15:08:45 -0700 | [diff] [blame^] | 36 | import com.android.tradefed.util.SerializationUtil; |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 37 | import com.android.tradefed.util.proto.TfMetricProtoUtil; |
| 38 | |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 39 | import org.junit.Before; |
| 40 | import org.junit.Test; |
| 41 | import org.junit.runner.RunWith; |
| 42 | import org.junit.runners.JUnit4; |
| 43 | |
| 44 | import java.util.HashMap; |
| 45 | |
| 46 | /** Unit tests for {@link ProtoResultReporter}. */ |
| 47 | @RunWith(JUnit4.class) |
| 48 | public 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 Desprez | 5070602 | 2018-07-18 15:41:44 -0700 | [diff] [blame] | 65 | mInvocationContext.setConfigurationDescriptor(new ConfigurationDescriptor()); |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 66 | } |
| 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 Desprez | 14a2ceb | 2019-07-15 12:03:17 -0700 | [diff] [blame] | 99 | mReporter.invocationFailed(new NullPointerException()); |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 100 | 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 Desprez | 14a2ceb | 2019-07-15 12:03:17 -0700 | [diff] [blame] | 106 | assertNotNull(mFinalRecord.getDebugInfo()); |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 107 | |
| 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 Murphy | e87b17f | 2019-12-06 17:53:33 -0800 | [diff] [blame] | 124 | assertEquals(1, testRecord2.getMetricsMap().size()); |
| 125 | assertEquals(1, testRecord2.getArtifactsMap().size()); |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 126 | // run 1 has one log file |
Joseph Murphy | e87b17f | 2019-12-06 17:53:33 -0800 | [diff] [blame] | 127 | assertEquals(1, run1.getArtifactsMap().size()); |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 128 | } |
| 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 Desprez | 7fc5425 | 2020-04-30 15:08:45 -0700 | [diff] [blame^] | 153 | /** 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 Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 182 | /** 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 Desprez | 5070602 | 2018-07-18 15:41:44 -0700 | [diff] [blame] | 186 | context.setConfigurationDescriptor(new ConfigurationDescriptor()); |
Julien Desprez | 093c3f6 | 2018-05-21 16:23:37 -0700 | [diff] [blame] | 187 | return context; |
| 188 | } |
| 189 | } |