1 /* 2 * Copyright (C) 2017 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.tradefed.testtype.retry; 17 18 import static org.junit.Assert.assertEquals; 19 20 import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper; 21 import com.android.compatibility.common.tradefed.testtype.suite.CompatibilityTestSuite; 22 import com.android.compatibility.common.tradefed.util.RetryFilterHelper; 23 import com.android.tradefed.build.IBuildInfo; 24 import com.android.tradefed.build.IDeviceBuildInfo; 25 import com.android.tradefed.config.Configuration; 26 import com.android.tradefed.config.ConfigurationDef; 27 import com.android.tradefed.config.IConfiguration; 28 import com.android.tradefed.config.OptionClass; 29 import com.android.tradefed.config.OptionSetter; 30 import com.android.tradefed.device.DeviceNotAvailableException; 31 import com.android.tradefed.device.ITestDevice; 32 import com.android.tradefed.invoker.IInvocationContext; 33 import com.android.tradefed.invoker.InvocationContext; 34 import com.android.tradefed.invoker.TestInformation; 35 import com.android.tradefed.metrics.proto.MetricMeasurement.Metric; 36 import com.android.tradefed.result.ITestInvocationListener; 37 import com.android.tradefed.suite.checker.ISystemStatusChecker; 38 import com.android.tradefed.testtype.IRemoteTest; 39 import com.android.tradefed.testtype.StubTest; 40 41 import org.easymock.EasyMock; 42 import org.junit.Before; 43 import org.junit.Test; 44 import org.junit.runner.RunWith; 45 import org.junit.runners.JUnit4; 46 47 import java.util.ArrayList; 48 import java.util.Collection; 49 import java.util.HashMap; 50 import java.util.LinkedHashMap; 51 import java.util.List; 52 53 /** 54 * Unit tests for {@link RetryFactoryTest}. 55 */ 56 @RunWith(JUnit4.class) 57 public class RetryFactoryTestTest { 58 59 private RetryFactoryTest mFactory; 60 private ITestInvocationListener mMockListener; 61 private RetryFilterHelper mSpyFilter; 62 63 private List<ISystemStatusChecker> mCheckers; 64 private IBuildInfo mMockInfo; 65 private ITestDevice mMockDevice; 66 private IConfiguration mMockMainConfiguration; 67 private IInvocationContext mMockContext; 68 private TestInformation mTestInfo; 69 70 /** 71 * A {@link CompatibilityTestSuite} that does not run anything. 72 */ 73 @OptionClass(alias = "compatibility") 74 public static class VoidCompatibilityTest extends CompatibilityTestSuite { 75 @Override loadTests()76 public LinkedHashMap<String, IConfiguration> loadTests() { 77 return new LinkedHashMap<>(); 78 } 79 80 @Override split(int shardCountHint)81 public Collection<IRemoteTest> split(int shardCountHint) { 82 List<IRemoteTest> tests = new ArrayList<>(); 83 for (int i = 0; i < shardCountHint; i++) { 84 tests.add(new StubTest()); 85 } 86 return tests; 87 } 88 } 89 90 @OptionClass(alias = "compatibility") 91 public static class TestCompatibilityTestSuite extends CompatibilityTestSuite { 92 @Override loadTests()93 public LinkedHashMap<String, IConfiguration> loadTests() { 94 LinkedHashMap<String, IConfiguration> tests = new LinkedHashMap<>(); 95 IConfiguration config = new Configuration("test", "test"); 96 config.setTest(new StubTest()); 97 tests.put("module1", config); 98 return tests; 99 } 100 } 101 102 @Before setUp()103 public void setUp() { 104 mMockMainConfiguration = new Configuration("mockMain", "mockMain"); 105 mCheckers = new ArrayList<>(); 106 mMockInfo = EasyMock.createMock(IDeviceBuildInfo.class); 107 EasyMock.expect(mMockInfo.getRemoteFiles()).andStubReturn(null); 108 mMockDevice = EasyMock.createMock(ITestDevice.class); 109 mMockContext = new InvocationContext(); 110 mMockContext.addAllocatedDevice(ConfigurationDef.DEFAULT_DEVICE_NAME, mMockDevice); 111 mMockContext.addDeviceBuildInfo(ConfigurationDef.DEFAULT_DEVICE_NAME, mMockInfo); 112 mTestInfo = TestInformation.newBuilder().setInvocationContext(mMockContext).build(); 113 114 mSpyFilter = new RetryFilterHelper() { 115 @Override 116 public void validateBuildFingerprint(ITestDevice device) 117 throws DeviceNotAvailableException { 118 // do nothing 119 } 120 @Override 121 public void setCommandLineOptionsFor(Object obj) { 122 // do nothing 123 } 124 @Override 125 public void setBuildInfoRetryCommand(IBuildInfo info) { 126 // do nothing 127 } 128 @Override 129 public void populateFiltersBySubPlan() { 130 // do nothing 131 } 132 }; 133 mFactory = new RetryFactoryTest() { 134 @Override 135 protected RetryFilterHelper createFilterHelper(CompatibilityBuildHelper buildHelper) { 136 return mSpyFilter; 137 } 138 @Override 139 CompatibilityTestSuite createTest() { 140 return new VoidCompatibilityTest(); 141 } 142 }; 143 mMockListener = EasyMock.createMock(ITestInvocationListener.class); 144 } 145 146 /** 147 * Tests that the CompatibilityTest created can receive all the options without throwing. 148 */ 149 @Test testRetry_receiveOption()150 public void testRetry_receiveOption() throws Exception { 151 OptionSetter setter = new OptionSetter(mFactory); 152 setter.setOptionValue("retry", "10599"); 153 setter.setOptionValue("test-arg", "abcd"); 154 EasyMock.replay(mMockListener); 155 mFactory.run(mTestInfo, mMockListener); 156 EasyMock.verify(mMockListener); 157 } 158 159 /** 160 * Assert that the {@link RetryFactoryTest#split(int)} calls the 161 * {@link CompatibilityTestSuite#split(int)} after applying all the filters. 162 */ 163 @Test testRetry_split()164 public void testRetry_split() throws Exception { 165 EasyMock.replay(mMockListener); 166 Collection<IRemoteTest> res = mFactory.split(2); 167 assertEquals(2, res.size()); 168 EasyMock.verify(mMockListener); 169 } 170 171 /** 172 * This test is meant to validate more end-to-end that the retry can create the runner, and 173 * running it works properly for the main use case. 174 */ 175 @Test testValidation()176 public void testValidation() throws Exception { 177 mFactory = new RetryFactoryTest() { 178 @Override 179 protected RetryFilterHelper createFilterHelper(CompatibilityBuildHelper buildHelper) { 180 return mSpyFilter; 181 } 182 @Override 183 CompatibilityTestSuite createTest() { 184 return new TestCompatibilityTestSuite(); 185 } 186 }; 187 mFactory.setBuild(mMockInfo); 188 mFactory.setDevice(mMockDevice); 189 mFactory.setSystemStatusChecker(mCheckers); 190 mFactory.setConfiguration(mMockMainConfiguration); 191 mFactory.setInvocationContext(mMockContext); 192 193 mMockListener.testModuleStarted(EasyMock.anyObject()); 194 mMockListener.testRunStarted( 195 EasyMock.eq("module1"), EasyMock.eq(0), EasyMock.eq(0), EasyMock.anyLong()); 196 mMockListener.testRunEnded( 197 EasyMock.anyLong(), EasyMock.<HashMap<String, Metric>>anyObject()); 198 mMockListener.testModuleEnded(); 199 200 EasyMock.replay(mMockListener, mMockInfo, mMockDevice); 201 mFactory.run(mTestInfo, mMockListener); 202 EasyMock.verify(mMockListener, mMockInfo, mMockDevice); 203 } 204 } 205