blob: 0fb5682519d9d3fa81ca539240878514a24eb871 [file] [log] [blame]
Brett Chabot74121d82010-01-28 20:14:27 -08001/*
2 * Copyright (C) 2010 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.invoker;
17
jdesprezbc580f92017-06-02 11:41:40 -070018import static org.mockito.Mockito.doReturn;
19
20import com.android.ddmlib.IDevice;
jdesprezcf545552017-07-12 02:08:08 -070021import com.android.tradefed.build.BuildInfo;
Brett Chabotbeaebb02011-02-07 16:16:06 -080022import com.android.tradefed.build.BuildRetrievalError;
23import com.android.tradefed.build.IBuildInfo;
jdesprez53387682018-02-27 14:10:46 -080024import com.android.tradefed.build.IBuildInfo.BuildInfoProperties;
Brett Chabotbeaebb02011-02-07 16:16:06 -080025import com.android.tradefed.build.IBuildProvider;
jdesprezda087c02017-06-26 14:23:07 -070026import com.android.tradefed.build.IDeviceBuildInfo;
Julien Desprezaa9ea852016-11-25 10:56:35 +000027import com.android.tradefed.build.IDeviceBuildProvider;
28import com.android.tradefed.command.CommandOptions;
Julien Desprez75fbb802016-11-15 12:35:58 +000029import com.android.tradefed.command.CommandRunner.ExitCode;
Brett Chabota17af5d2010-08-23 13:51:17 -070030import com.android.tradefed.command.FatalHostError;
Julien Desprezaa9ea852016-11-25 10:56:35 +000031import com.android.tradefed.command.ICommandOptions;
Julien Desprez07fe1532016-08-12 11:48:32 +010032import com.android.tradefed.command.remote.DeviceDescriptor;
Brett Chabotb1052c22011-01-12 19:44:23 -080033import com.android.tradefed.config.Configuration;
Julien Desprez47c287f2016-07-14 12:16:57 +010034import com.android.tradefed.config.ConfigurationDef;
35import com.android.tradefed.config.DeviceConfigurationHolder;
Brett Chabot7e5f8ac2010-05-17 12:27:56 -070036import com.android.tradefed.config.IConfiguration;
Moon Kimb78076b2016-07-06 15:12:07 -070037import com.android.tradefed.config.IConfigurationFactory;
Julien Desprez47c287f2016-07-14 12:16:57 +010038import com.android.tradefed.config.IDeviceConfiguration;
jdesprez839825d2018-01-31 12:14:23 -080039import com.android.tradefed.config.Option;
jdesprezbc580f92017-06-02 11:41:40 -070040import com.android.tradefed.config.OptionSetter;
Julien Desprez07fe1532016-08-12 11:48:32 +010041import com.android.tradefed.device.DeviceAllocationState;
Brett Chabot5e4bc502010-06-10 20:10:30 -070042import com.android.tradefed.device.DeviceNotAvailableException;
Brett Chabotf7ad1562010-07-27 11:58:58 -070043import com.android.tradefed.device.IDeviceRecovery;
jdesprezbc580f92017-06-02 11:41:40 -070044import com.android.tradefed.device.INativeDevice;
Brett Chabot74121d82010-01-28 20:14:27 -080045import com.android.tradefed.device.ITestDevice;
Julien Desprez70488e82016-02-05 11:28:34 +000046import com.android.tradefed.device.ITestDevice.RecoveryMode;
jdesprezbc580f92017-06-02 11:41:40 -070047import com.android.tradefed.device.StubDevice;
Julien Desprez0750c382016-05-23 10:40:26 +010048import com.android.tradefed.device.TestDeviceOptions;
jdesprez267ab892017-09-08 15:30:06 -070049import com.android.tradefed.device.metric.BaseDeviceMetricCollector;
jdesprez267ab892017-09-08 15:30:06 -070050import com.android.tradefed.device.metric.DeviceMetricData;
jdesprez5ae744c2017-10-27 10:36:53 -070051import com.android.tradefed.device.metric.IMetricCollector;
jdesprezbc580f92017-06-02 11:41:40 -070052import com.android.tradefed.invoker.shard.IShardHelper;
53import com.android.tradefed.invoker.shard.ShardHelper;
54import com.android.tradefed.invoker.shard.StrictShardHelper;
Brett Chabot7e5f8ac2010-05-17 12:27:56 -070055import com.android.tradefed.log.ILeveledLogOutput;
Brett Chabot1d35e662011-05-25 19:48:24 -070056import com.android.tradefed.log.ILogRegistry;
Omari Stephens41978ee2011-02-23 19:03:05 -080057import com.android.tradefed.result.ByteArrayInputStreamSource;
Eric Rowe0b445112013-07-12 09:29:47 -070058import com.android.tradefed.result.ILogSaver;
59import com.android.tradefed.result.ILogSaverListener;
Brett Chabot74121d82010-01-28 20:14:27 -080060import com.android.tradefed.result.ITestInvocationListener;
Omari Stephens203296c2010-09-02 18:20:45 -070061import com.android.tradefed.result.ITestSummaryListener;
Brett Chabot1d35e662011-05-25 19:48:24 -070062import com.android.tradefed.result.InputStreamSource;
Brett Chabot2e441bd2010-09-16 18:25:11 -070063import com.android.tradefed.result.InvocationStatus;
Brett Chabot7e5f8ac2010-05-17 12:27:56 -070064import com.android.tradefed.result.LogDataType;
Eric Rowe0b445112013-07-12 09:29:47 -070065import com.android.tradefed.result.LogFile;
jdesprez821a0052018-01-16 03:37:16 -080066import com.android.tradefed.result.TestDescription;
Omari Stephensa01abfe2010-09-07 15:48:44 -070067import com.android.tradefed.result.TestSummary;
Brett Chabotbeaebb02011-02-07 16:16:06 -080068import com.android.tradefed.targetprep.BuildError;
Brett Chabot87803df2013-03-01 16:01:50 -080069import com.android.tradefed.targetprep.ITargetCleaner;
Brett Chabotbeaebb02011-02-07 16:16:06 -080070import com.android.tradefed.targetprep.ITargetPreparer;
Brett Chabot74121d82010-01-28 20:14:27 -080071import com.android.tradefed.testtype.IDeviceTest;
Julien Desprezaa9ea852016-11-25 10:56:35 +000072import com.android.tradefed.testtype.IInvocationContextReceiver;
Brett Chabot74121d82010-01-28 20:14:27 -080073import com.android.tradefed.testtype.IRemoteTest;
Brett Chabotb1052c22011-01-12 19:44:23 -080074import com.android.tradefed.testtype.IResumableTest;
Eric Rowea3b85852012-07-16 15:07:49 -070075import com.android.tradefed.testtype.IRetriableTest;
Julien Desprez2763ca02016-12-06 14:39:33 +000076import com.android.tradefed.testtype.IShardableTest;
Moon Kimb78076b2016-07-06 15:12:07 -070077import com.android.tradefed.testtype.IStrictShardableTest;
jdesprez267ab892017-09-08 15:30:06 -070078import com.android.tradefed.testtype.StubTest;
jdesprezda087c02017-06-26 14:23:07 -070079import com.android.tradefed.util.FileUtil;
Brett Chabot74121d82010-01-28 20:14:27 -080080
jdesprezbc580f92017-06-02 11:41:40 -070081import com.google.common.util.concurrent.SettableFuture;
82
Brett Chabot0eabebe2011-09-12 16:15:42 -070083import junit.framework.Test;
84import junit.framework.TestCase;
85
Jed Estepa97349e2016-11-02 20:44:09 -070086import org.easymock.Capture;
87import org.easymock.EasyMock;
jdesprezbc580f92017-06-02 11:41:40 -070088import org.mockito.Mockito;
Jed Estepa97349e2016-11-02 20:44:09 -070089
jdesprezda087c02017-06-26 14:23:07 -070090import java.io.File;
Eric Rowe0b445112013-07-12 09:29:47 -070091import java.io.IOException;
92import java.io.InputStream;
Brett Chabot664b8932010-08-06 17:17:33 -070093import java.util.ArrayList;
Brett Chabotbcb497c2010-08-07 17:07:36 -070094import java.util.Collections;
jdesprez53387682018-02-27 14:10:46 -080095import java.util.HashSet;
Brett Chabot664b8932010-08-06 17:17:33 -070096import java.util.List;
Brett Chabotbcb497c2010-08-07 17:07:36 -070097import java.util.Map;
jdesprez53387682018-02-27 14:10:46 -080098import java.util.Set;
Brett Chabot7e5f8ac2010-05-17 12:27:56 -070099
jdesprezd0e00da2017-09-07 18:54:49 -0700100/** Unit tests for {@link TestInvocation}. */
101@SuppressWarnings("MustBeClosedChecker")
Brett Chabot74121d82010-01-28 20:14:27 -0800102public class TestInvocationTest extends TestCase {
103
Brett Chaboteba1fd62011-09-13 17:17:45 -0700104 private static final String SERIAL = "serial";
Brett Chabotbcb497c2010-08-07 17:07:36 -0700105 private static final Map<String, String> EMPTY_MAP = Collections.emptyMap();
Eric Rowe0b445112013-07-12 09:29:47 -0700106 private static final String PATH = "path";
107 private static final String URL = "url";
Omari Stephensa01abfe2010-09-07 15:48:44 -0700108 private static final TestSummary mSummary = new TestSummary("http://www.url.com/report.txt");
Michael Rosenfeld55012252017-01-12 15:45:19 -0800109 private static final InputStreamSource EMPTY_STREAM_SOURCE =
110 new ByteArrayInputStreamSource(new byte[0]);
111 private static final String LOGCAT_NAME_ERROR =
Michael Rosenfeldb0f58d22017-01-26 14:38:32 -0800112 TestInvocation.getDeviceLogName(TestInvocation.Stage.ERROR);
Michael Rosenfeld55012252017-01-12 15:45:19 -0800113 private static final String LOGCAT_NAME_SETUP =
Michael Rosenfeldb0f58d22017-01-26 14:38:32 -0800114 TestInvocation.getDeviceLogName(TestInvocation.Stage.SETUP);
Michael Rosenfeld55012252017-01-12 15:45:19 -0800115 private static final String LOGCAT_NAME_TEST =
Michael Rosenfeldb0f58d22017-01-26 14:38:32 -0800116 TestInvocation.getDeviceLogName(TestInvocation.Stage.TEST);
Michael Rosenfeld55012252017-01-12 15:45:19 -0800117 private static final String LOGCAT_NAME_TEARDOWN =
Michael Rosenfeldb0f58d22017-01-26 14:38:32 -0800118 TestInvocation.getDeviceLogName(TestInvocation.Stage.TEARDOWN);
Brett Chabot74121d82010-01-28 20:14:27 -0800119 /** The {@link TestInvocation} under test, with all dependencies mocked out */
120 private TestInvocation mTestInvocation;
121
Brett Chabotb1052c22011-01-12 19:44:23 -0800122 private IConfiguration mStubConfiguration;
Julien Desprez47c287f2016-07-14 12:16:57 +0100123 private IConfiguration mStubMultiConfiguration;
124
125 private IInvocationContext mStubInvocationMetadata;
Brett Chabotb1052c22011-01-12 19:44:23 -0800126
Brett Chabot7e5f8ac2010-05-17 12:27:56 -0700127 // The mock objects.
Brett Chabot74121d82010-01-28 20:14:27 -0800128 private ITestDevice mMockDevice;
129 private ITargetPreparer mMockPreparer;
Brett Chabotfe88a152010-08-02 17:52:59 -0700130 private IBuildProvider mMockBuildProvider;
Brett Chabot74121d82010-01-28 20:14:27 -0800131 private IBuildInfo mMockBuildInfo;
132 private ITestInvocationListener mMockTestListener;
Omari Stephens203296c2010-09-02 18:20:45 -0700133 private ITestSummaryListener mMockSummaryListener;
Brett Chabot7e5f8ac2010-05-17 12:27:56 -0700134 private ILeveledLogOutput mMockLogger;
Eric Rowe0b445112013-07-12 09:29:47 -0700135 private ILogSaver mMockLogSaver;
Brett Chabotf7ad1562010-07-27 11:58:58 -0700136 private IDeviceRecovery mMockRecovery;
Omari Stephensa01abfe2010-09-07 15:48:44 -0700137 private Capture<List<TestSummary>> mUriCapture;
Brett Chabot1d35e662011-05-25 19:48:24 -0700138 private ILogRegistry mMockLogRegistry;
Moon Kimb78076b2016-07-06 15:12:07 -0700139 private IConfigurationFactory mMockConfigFactory;
Julien Desprez2040ac02016-03-21 18:05:30 +0000140 private IRescheduler mockRescheduler;
Julien Desprez07fe1532016-08-12 11:48:32 +0100141 private DeviceDescriptor mFakeDescriptor;
Brett Chabot74121d82010-01-28 20:14:27 -0800142
143 @Override
144 protected void setUp() throws Exception {
145 super.setUp();
146
Brett Chabote7e335e2011-02-16 14:51:03 -0800147 mStubConfiguration = new Configuration("foo", "bar");
Julien Desprez47c287f2016-07-14 12:16:57 +0100148 mStubMultiConfiguration = new Configuration("foo", "bar");
Brett Chabotb1052c22011-01-12 19:44:23 -0800149
Brett Chabot74121d82010-01-28 20:14:27 -0800150 mMockDevice = EasyMock.createMock(ITestDevice.class);
Brett Chabotf7ad1562010-07-27 11:58:58 -0700151 mMockRecovery = EasyMock.createMock(IDeviceRecovery.class);
Brett Chabot74121d82010-01-28 20:14:27 -0800152 mMockPreparer = EasyMock.createMock(ITargetPreparer.class);
Brett Chabotfe88a152010-08-02 17:52:59 -0700153 mMockBuildProvider = EasyMock.createMock(IBuildProvider.class);
Brett Chabotb1052c22011-01-12 19:44:23 -0800154
Omari Stephens203296c2010-09-02 18:20:45 -0700155 // Use strict mocks here since the order of Listener calls is important
156 mMockTestListener = EasyMock.createStrictMock(ITestInvocationListener.class);
157 mMockSummaryListener = EasyMock.createStrictMock(ITestSummaryListener.class);
Brett Chabot5dae8722010-09-17 20:05:51 -0700158 mMockBuildInfo = EasyMock.createMock(IBuildInfo.class);
Brett Chabot35d50c62011-03-07 10:39:23 -0800159 mMockLogger = EasyMock.createMock(ILeveledLogOutput.class);
Brett Chabot1d35e662011-05-25 19:48:24 -0700160 mMockLogRegistry = EasyMock.createMock(ILogRegistry.class);
Eric Rowe0b445112013-07-12 09:29:47 -0700161 mMockLogSaver = EasyMock.createMock(ILogSaver.class);
Moon Kimb78076b2016-07-06 15:12:07 -0700162 mMockConfigFactory = EasyMock.createMock(IConfigurationFactory.class);
Julien Desprez2040ac02016-03-21 18:05:30 +0000163 mockRescheduler = EasyMock.createMock(IRescheduler.class);
Brett Chabot74121d82010-01-28 20:14:27 -0800164
Brett Chabotb1052c22011-01-12 19:44:23 -0800165 mStubConfiguration.setDeviceRecovery(mMockRecovery);
166 mStubConfiguration.setTargetPreparer(mMockPreparer);
167 mStubConfiguration.setBuildProvider(mMockBuildProvider);
Julien Desprez47c287f2016-07-14 12:16:57 +0100168
jdesprez5ae744c2017-10-27 10:36:53 -0700169 EasyMock.expect(mMockPreparer.isDisabled()).andStubReturn(false);
170
Julien Desprez47c287f2016-07-14 12:16:57 +0100171 List<IDeviceConfiguration> deviceConfigs = new ArrayList<IDeviceConfiguration>();
Julien Desprez53353e62016-08-12 15:24:33 +0100172 IDeviceConfiguration device1 =
173 new DeviceConfigurationHolder(ConfigurationDef.DEFAULT_DEVICE_NAME);
Julien Desprez47c287f2016-07-14 12:16:57 +0100174 device1.addSpecificConfig(mMockRecovery);
175 device1.addSpecificConfig(mMockPreparer);
176 device1.addSpecificConfig(mMockBuildProvider);
177 deviceConfigs.add(device1);
178 mStubMultiConfiguration.setDeviceConfigList(deviceConfigs);
179
Eric Rowe0b445112013-07-12 09:29:47 -0700180 mStubConfiguration.setLogSaver(mMockLogSaver);
Julien Desprez47c287f2016-07-14 12:16:57 +0100181 mStubMultiConfiguration.setLogSaver(mMockLogSaver);
Omari Stephens203296c2010-09-02 18:20:45 -0700182
Brett Chabotad123582011-02-09 18:26:40 -0800183 List<ITestInvocationListener> listenerList = new ArrayList<ITestInvocationListener>(1);
184 listenerList.add(mMockTestListener);
185 listenerList.add(mMockSummaryListener);
186 mStubConfiguration.setTestInvocationListeners(listenerList);
Julien Desprez47c287f2016-07-14 12:16:57 +0100187 mStubMultiConfiguration.setTestInvocationListeners(listenerList);
Omari Stephens203296c2010-09-02 18:20:45 -0700188
Brett Chabotb1052c22011-01-12 19:44:23 -0800189 mStubConfiguration.setLogOutput(mMockLogger);
Julien Desprez47c287f2016-07-14 12:16:57 +0100190 mStubMultiConfiguration.setLogOutput(mMockLogger);
Brett Chaboteba1fd62011-09-13 17:17:45 -0700191 EasyMock.expect(mMockDevice.getSerialNumber()).andStubReturn(SERIAL);
Guang Zhu789a52b2014-01-17 14:27:01 -0800192 EasyMock.expect(mMockDevice.getIDevice()).andStubReturn(null);
Brett Chabotf7ad1562010-07-27 11:58:58 -0700193 mMockDevice.setRecovery(mMockRecovery);
Julien Desprez26bee8d2016-03-29 12:09:48 +0100194 mMockDevice.preInvocationSetup((IBuildInfo)EasyMock.anyObject());
195 EasyMock.expectLastCall().anyTimes();
196 mMockDevice.postInvocationTearDown();
197 EasyMock.expectLastCall().anyTimes();
Julien Desprez07fe1532016-08-12 11:48:32 +0100198 mFakeDescriptor = new DeviceDescriptor(SERIAL, false, DeviceAllocationState.Available,
199 "unknown", "unknown", "unknown", "unknown", "unknown");
200 EasyMock.expect(mMockDevice.getDeviceDescriptor()).andStubReturn(mFakeDescriptor);
Brett Chabot74121d82010-01-28 20:14:27 -0800201
Jorge Gonzalez3777e322011-05-11 18:04:29 -0700202 EasyMock.expect(mMockBuildInfo.getBuildId()).andStubReturn("1");
Brett Chabotbcb497c2010-08-07 17:07:36 -0700203 EasyMock.expect(mMockBuildInfo.getBuildAttributes()).andStubReturn(EMPTY_MAP);
Brett Chabot2b79e0f2013-03-18 16:50:16 -0700204 EasyMock.expect(mMockBuildInfo.getBuildBranch()).andStubReturn("branch");
205 EasyMock.expect(mMockBuildInfo.getBuildFlavor()).andStubReturn("flavor");
jdesprez53387682018-02-27 14:10:46 -0800206 EasyMock.expect(mMockBuildInfo.getProperties()).andStubReturn(new HashSet<>());
Julien Desprezf7f32c92016-07-27 10:07:50 +0100207
Brett Chabot1d35e662011-05-25 19:48:24 -0700208 // always expect logger initialization and cleanup calls
209 mMockLogRegistry.registerLogger(mMockLogger);
210 mMockLogger.init();
211 mMockLogger.closeLog();
212 mMockLogRegistry.unregisterLogger();
Omari Stephensa01abfe2010-09-07 15:48:44 -0700213 mUriCapture = new Capture<List<TestSummary>>();
Brett Chabotbcb497c2010-08-07 17:07:36 -0700214
Julien Desprez47c287f2016-07-14 12:16:57 +0100215 mStubInvocationMetadata = new InvocationContext();
216 mStubInvocationMetadata.addAllocatedDevice(ConfigurationDef.DEFAULT_DEVICE_NAME,
217 mMockDevice);
Julien Desprez53353e62016-08-12 15:24:33 +0100218 mStubInvocationMetadata.addDeviceBuildInfo(ConfigurationDef.DEFAULT_DEVICE_NAME,
219 mMockBuildInfo);
Julien Desprez47c287f2016-07-14 12:16:57 +0100220
Brett Chabot74121d82010-01-28 20:14:27 -0800221 // create the BaseTestInvocation to test
jdesprezbc580f92017-06-02 11:41:40 -0700222 mTestInvocation =
223 new TestInvocation() {
224 @Override
225 ILogRegistry getLogRegistry() {
226 return mMockLogRegistry;
227 }
Moon Kimb78076b2016-07-06 15:12:07 -0700228
jdesprezbc580f92017-06-02 11:41:40 -0700229 @Override
jdesprez85129dd2017-11-13 06:05:57 -0800230 public IInvocationExecution createInvocationExec() {
231 return new InvocationExecution() {
232 @Override
233 protected IShardHelper createShardHelper() {
234 return new ShardHelper();
235 }
236 };
jdesprezbc580f92017-06-02 11:41:40 -0700237 }
238
239 @Override
240 protected void setExitCode(ExitCode code, Throwable stack) {
241 // empty on purpose
242 }
243 };
Brett Chabot74121d82010-01-28 20:14:27 -0800244 }
245
Neal Nguyen2ceabaa2010-05-26 14:42:14 -0700246 @Override
247 protected void tearDown() throws Exception {
248 super.tearDown();
249
250 }
251
Brett Chabot74121d82010-01-28 20:14:27 -0800252 /**
253 * Test the normal case invoke scenario with a {@link IRemoteTest}.
254 * <p/>
255 * Verifies that all external interfaces get notified as expected.
256 */
Brett Chabot87803df2013-03-01 16:01:50 -0800257 public void testInvoke_RemoteTest() throws Throwable {
Brett Chabot74121d82010-01-28 20:14:27 -0800258 IRemoteTest test = EasyMock.createMock(IRemoteTest.class);
Omari Stephens203296c2010-09-02 18:20:45 -0700259 setupMockSuccessListeners();
Julien Desprez26bee8d2016-03-29 12:09:48 +0100260
Brett Chabotad123582011-02-09 18:26:40 -0800261 test.run((ITestInvocationListener)EasyMock.anyObject());
Brett Chabot7e5f8ac2010-05-17 12:27:56 -0700262 setupNormalInvoke(test);
Julien Desprez2040ac02016-03-21 18:05:30 +0000263 EasyMock.replay(mockRescheduler);
Julien Desprez47c287f2016-07-14 12:16:57 +0100264 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
265 verifyMocks(test, mockRescheduler);
266 verifySummaryListener();
267 }
268
269 /**
270 * Test the normal case for multi invoke scenario with a {@link IRemoteTest}.
271 * <p/>
272 * Verifies that all external interfaces get notified as expected.
273 */
274 public void testInvokeMulti_RemoteTest() throws Throwable {
275 IRemoteTest test = EasyMock.createMock(IRemoteTest.class);
276 setupMockSuccessListeners();
277
278 test.run((ITestInvocationListener)EasyMock.anyObject());
279 setupNormalInvoke(test);
280 EasyMock.replay(mockRescheduler);
Julien Desprez53353e62016-08-12 15:24:33 +0100281 mTestInvocation.invoke(mStubInvocationMetadata, mStubMultiConfiguration, mockRescheduler);
Julien Desprez2040ac02016-03-21 18:05:30 +0000282 verifyMocks(test, mockRescheduler);
Omari Stephens203296c2010-09-02 18:20:45 -0700283 verifySummaryListener();
284 }
285
286 /**
287 * Test the normal case invoke scenario with an {@link ITestSummaryListener} masquerading as
288 * an {@link ITestInvocationListener}.
289 * <p/>
290 * Verifies that all external interfaces get notified as expected.
Omari Stephens203296c2010-09-02 18:20:45 -0700291 */
Julien Desprez78fcda22016-06-16 17:35:11 +0100292 public void testInvoke_twoSummary() throws Throwable {
Omari Stephens203296c2010-09-02 18:20:45 -0700293
294 IRemoteTest test = EasyMock.createMock(IRemoteTest.class);
295 setupMockSuccessListeners();
296
Brett Chabotad123582011-02-09 18:26:40 -0800297 test.run((ITestInvocationListener)EasyMock.anyObject());
Omari Stephens203296c2010-09-02 18:20:45 -0700298 setupNormalInvoke(test);
Julien Desprez2040ac02016-03-21 18:05:30 +0000299 EasyMock.replay(mockRescheduler);
Julien Desprez47c287f2016-07-14 12:16:57 +0100300 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
Julien Desprez2040ac02016-03-21 18:05:30 +0000301 verifyMocks(test, mockRescheduler);
Omari Stephens203296c2010-09-02 18:20:45 -0700302 verifySummaryListener();
Brett Chabot74121d82010-01-28 20:14:27 -0800303 }
304
305 /**
306 * Test the invoke scenario where build retrieve fails.
Brett Chabot057e16e2010-06-27 21:53:48 -0700307 * <p/>
Brett Chabotc9924302011-06-30 16:48:01 -0700308 * An invocation will be started in this scenario.
Brett Chabot74121d82010-01-28 20:14:27 -0800309 */
Brett Chabot87803df2013-03-01 16:01:50 -0800310 public void testInvoke_buildFailed() throws Throwable {
Brett Chabotc9924302011-06-30 16:48:01 -0700311 BuildRetrievalError exception = new BuildRetrievalError("error", null, mMockBuildInfo);
Brett Chabotfe88a152010-08-02 17:52:59 -0700312 EasyMock.expect(mMockBuildProvider.getBuild()).andThrow(exception);
Julien Desprez949ba3a2016-11-28 15:18:32 +0000313 EasyMock.expect(mMockBuildInfo.getTestTag()).andStubReturn(null);
Brett Chabotc9924302011-06-30 16:48:01 -0700314 setupMockFailureListeners(exception);
Brett Chabot44556372014-03-19 19:07:15 -0700315 setupInvoke();
Brett Chabotccfda2e2011-02-09 14:39:59 -0800316 IRemoteTest test = EasyMock.createMock(IRemoteTest.class);
Julien Desprez949ba3a2016-11-28 15:18:32 +0000317 CommandOptions cmdOptions = new CommandOptions();
318 final String expectedTestTag = "TEST_TAG";
319 cmdOptions.setTestTag(expectedTestTag);
320 mStubConfiguration.setCommandOptions(cmdOptions);
Brett Chabotb1052c22011-01-12 19:44:23 -0800321 mStubConfiguration.setTest(test);
Michael Rosenfeld55012252017-01-12 15:45:19 -0800322 EasyMock.expect(mMockLogger.getLog()).andReturn(EMPTY_STREAM_SOURCE);
323 EasyMock.expect(mMockDevice.getLogcat()).andReturn(EMPTY_STREAM_SOURCE).times(2);
324 mMockDevice.clearLogcat();
325 EasyMock.expectLastCall().times(2);
jdesprez3dff70b2017-04-18 10:39:13 -0700326 mMockLogRegistry.unregisterLogger();
327 mMockLogRegistry.dumpToGlobalLog(mMockLogger);
328 mMockLogger.closeLog();
jdespreza90f9722017-08-11 16:48:24 -0700329 mMockBuildProvider.cleanUp(mMockBuildInfo);
Julien Desprez2040ac02016-03-21 18:05:30 +0000330 replayMocks(test, mockRescheduler);
Julien Desprez47c287f2016-07-14 12:16:57 +0100331 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
Julien Desprez2040ac02016-03-21 18:05:30 +0000332 verifyMocks(test, mockRescheduler);
Julien Desprez949ba3a2016-11-28 15:18:32 +0000333 // invocation test tag was updated.
334 assertEquals(expectedTestTag, mStubInvocationMetadata.getTestTag());
Brett Chabot74121d82010-01-28 20:14:27 -0800335 }
336
337 /**
Brett Chabotdd21ddf2010-06-07 14:16:19 -0700338 * Test the invoke scenario where there is no build to test.
339 */
Brett Chabot87803df2013-03-01 16:01:50 -0800340 public void testInvoke_noBuild() throws Throwable {
Brett Chabotfe88a152010-08-02 17:52:59 -0700341 EasyMock.expect(mMockBuildProvider.getBuild()).andReturn(null);
Brett Chabotccfda2e2011-02-09 14:39:59 -0800342 IRemoteTest test = EasyMock.createMock(IRemoteTest.class);
Brett Chabotb1052c22011-01-12 19:44:23 -0800343 mStubConfiguration.setTest(test);
jdespreza90f9722017-08-11 16:48:24 -0700344 mMockBuildProvider.cleanUp(mMockBuildInfo);
Brett Chabot1d35e662011-05-25 19:48:24 -0700345 mMockLogRegistry.dumpToGlobalLog(mMockLogger);
Brett Chabot44556372014-03-19 19:07:15 -0700346 setupInvoke();
Julien Desprez2040ac02016-03-21 18:05:30 +0000347 replayMocks(test, mockRescheduler);
Julien Desprez47c287f2016-07-14 12:16:57 +0100348 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
Brett Chabot057e16e2010-06-27 21:53:48 -0700349 verifyMocks(test);
Brett Chabot74121d82010-01-28 20:14:27 -0800350 }
351
Brett Chabot74121d82010-01-28 20:14:27 -0800352 /**
Eric Rowef85ffac2012-10-03 12:56:12 -0700353 * Test the invoke scenario where there is no build to test for a {@link IRetriableTest}.
354 */
Brett Chabot87803df2013-03-01 16:01:50 -0800355 public void testInvoke_noBuildRetry() throws Throwable {
Eric Rowef85ffac2012-10-03 12:56:12 -0700356 EasyMock.expect(mMockBuildProvider.getBuild()).andReturn(null);
357
358 IRetriableTest test = EasyMock.createMock(IRetriableTest.class);
359 EasyMock.expect(test.isRetriable()).andReturn(Boolean.TRUE);
360
Eric Rowef85ffac2012-10-03 12:56:12 -0700361 EasyMock.expect(mockRescheduler.rescheduleCommand()).andReturn(EasyMock.anyBoolean());
362
363 mStubConfiguration.setTest(test);
364 mStubConfiguration.getCommandOptions().setLoopMode(false);
365 mMockLogRegistry.dumpToGlobalLog(mMockLogger);
jdespreza90f9722017-08-11 16:48:24 -0700366 EasyMock.expectLastCall().times(1);
Brett Chabot44556372014-03-19 19:07:15 -0700367 setupInvoke();
jdespreza90f9722017-08-11 16:48:24 -0700368 mMockBuildProvider.cleanUp(mMockBuildInfo);
Eric Rowef85ffac2012-10-03 12:56:12 -0700369 replayMocks(test);
370 EasyMock.replay(mockRescheduler);
Julien Desprez47c287f2016-07-14 12:16:57 +0100371 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
Eric Rowef85ffac2012-10-03 12:56:12 -0700372 EasyMock.verify(mockRescheduler);
373 verifyMocks(test);
374 }
375
376 /**
Julien Desprez47c287f2016-07-14 12:16:57 +0100377 * Test the{@link TestInvocation#invoke(IInvocationContext, IConfiguration, IRescheduler,
Julien Desprez2040ac02016-03-21 18:05:30 +0000378 * ITestInvocationListener[])} scenario
Eric Rowe0b445112013-07-12 09:29:47 -0700379 * where the test is a {@link IDeviceTest}
Brett Chabot74121d82010-01-28 20:14:27 -0800380 */
Brett Chabot87803df2013-03-01 16:01:50 -0800381 public void testInvoke_deviceTest() throws Throwable {
Brett Chabot2e441bd2010-09-16 18:25:11 -0700382 DeviceConfigTest mockDeviceTest = EasyMock.createMock(DeviceConfigTest.class);
Brett Chabotb1052c22011-01-12 19:44:23 -0800383 mStubConfiguration.setTest(mockDeviceTest);
Brett Chabot2e441bd2010-09-16 18:25:11 -0700384 mockDeviceTest.setDevice(mMockDevice);
Brett Chabotad123582011-02-09 18:26:40 -0800385 mockDeviceTest.run((ITestInvocationListener)EasyMock.anyObject());
Omari Stephens203296c2010-09-02 18:20:45 -0700386 setupMockSuccessListeners();
Brett Chabot2e441bd2010-09-16 18:25:11 -0700387 setupNormalInvoke(mockDeviceTest);
Julien Desprez2040ac02016-03-21 18:05:30 +0000388 EasyMock.replay(mockRescheduler);
Julien Desprez47c287f2016-07-14 12:16:57 +0100389 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
Julien Desprez2040ac02016-03-21 18:05:30 +0000390 verifyMocks(mockDeviceTest, mockRescheduler);
Omari Stephens203296c2010-09-02 18:20:45 -0700391 verifySummaryListener();
Brett Chabot74121d82010-01-28 20:14:27 -0800392 }
393
394 /**
395 * Test the invoke scenario where test run throws {@link IllegalArgumentException}
396 *
397 * @throws Exception if unexpected error occurs
398 */
Brett Chabot87803df2013-03-01 16:01:50 -0800399 public void testInvoke_testFail() throws Throwable {
Brett Chabot7e5f8ac2010-05-17 12:27:56 -0700400 IllegalArgumentException exception = new IllegalArgumentException();
Brett Chabot74121d82010-01-28 20:14:27 -0800401 IRemoteTest test = EasyMock.createMock(IRemoteTest.class);
Brett Chabotad123582011-02-09 18:26:40 -0800402 test.run((ITestInvocationListener)EasyMock.anyObject());
Brett Chabot7e5f8ac2010-05-17 12:27:56 -0700403 EasyMock.expectLastCall().andThrow(exception);
Omari Stephens203296c2010-09-02 18:20:45 -0700404 setupMockFailureListeners(exception);
Brett Chabotfe88a152010-08-02 17:52:59 -0700405 mMockBuildProvider.buildNotTested(mMockBuildInfo);
Brett Chabot7e5f8ac2010-05-17 12:27:56 -0700406 setupNormalInvoke(test);
Julien Desprez2040ac02016-03-21 18:05:30 +0000407 EasyMock.replay(mockRescheduler);
Omari Stephens203296c2010-09-02 18:20:45 -0700408 try {
Julien Desprez47c287f2016-07-14 12:16:57 +0100409 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
Omari Stephens203296c2010-09-02 18:20:45 -0700410 fail("IllegalArgumentException was not rethrown");
411 } catch (IllegalArgumentException e) {
412 // expected
413 }
Julien Desprez2040ac02016-03-21 18:05:30 +0000414 verifyMocks(test, mockRescheduler);
Omari Stephens203296c2010-09-02 18:20:45 -0700415 verifySummaryListener();
Brett Chabot74121d82010-01-28 20:14:27 -0800416 }
417
418 /**
Brett Chabota17af5d2010-08-23 13:51:17 -0700419 * Test the invoke scenario where test run throws {@link FatalHostError}
420 *
421 * @throws Exception if unexpected error occurs
422 */
Brett Chabot87803df2013-03-01 16:01:50 -0800423 public void testInvoke_fatalError() throws Throwable {
Brett Chabota17af5d2010-08-23 13:51:17 -0700424 FatalHostError exception = new FatalHostError("error");
425 IRemoteTest test = EasyMock.createMock(IRemoteTest.class);
Brett Chabotad123582011-02-09 18:26:40 -0800426 test.run((ITestInvocationListener)EasyMock.anyObject());
Brett Chabota17af5d2010-08-23 13:51:17 -0700427 EasyMock.expectLastCall().andThrow(exception);
Omari Stephens203296c2010-09-02 18:20:45 -0700428 setupMockFailureListeners(exception);
Brett Chabota17af5d2010-08-23 13:51:17 -0700429 mMockBuildProvider.buildNotTested(mMockBuildInfo);
Brett Chabota17af5d2010-08-23 13:51:17 -0700430 setupNormalInvoke(test);
Julien Desprez2040ac02016-03-21 18:05:30 +0000431 EasyMock.replay(mockRescheduler);
Brett Chabota17af5d2010-08-23 13:51:17 -0700432 try {
Julien Desprez47c287f2016-07-14 12:16:57 +0100433 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
Brett Chabota17af5d2010-08-23 13:51:17 -0700434 fail("FatalHostError was not rethrown");
435 } catch (FatalHostError e) {
436 // expected
437 }
Julien Desprez2040ac02016-03-21 18:05:30 +0000438 verifyMocks(test, mockRescheduler);
Omari Stephens203296c2010-09-02 18:20:45 -0700439 verifySummaryListener();
Brett Chabota17af5d2010-08-23 13:51:17 -0700440 }
441
442 /**
Eric Rowe0b445112013-07-12 09:29:47 -0700443 * Test the invoke scenario where test run throws {@link DeviceNotAvailableException}
Brett Chabot3759c852010-07-13 17:36:31 -0700444 *
445 * @throws Exception if unexpected error occurs
446 */
Brett Chabot87803df2013-03-01 16:01:50 -0800447 public void testInvoke_deviceNotAvail() throws Throwable {
Julien Desprez47c287f2016-07-14 12:16:57 +0100448 DeviceNotAvailableException exception = new DeviceNotAvailableException("ERROR", SERIAL);
Brett Chabot3759c852010-07-13 17:36:31 -0700449 IRemoteTest test = EasyMock.createMock(IRemoteTest.class);
Brett Chabotad123582011-02-09 18:26:40 -0800450 test.run((ITestInvocationListener)EasyMock.anyObject());
Brett Chabot3759c852010-07-13 17:36:31 -0700451 EasyMock.expectLastCall().andThrow(exception);
Julien Desprez70488e82016-02-05 11:28:34 +0000452 mMockDevice.setRecoveryMode(RecoveryMode.NONE);
453 EasyMock.expectLastCall();
Omari Stephens203296c2010-09-02 18:20:45 -0700454 setupMockFailureListeners(exception);
Brett Chabotfe88a152010-08-02 17:52:59 -0700455 mMockBuildProvider.buildNotTested(mMockBuildInfo);
Brett Chabot3759c852010-07-13 17:36:31 -0700456 setupNormalInvoke(test);
Julien Desprez2040ac02016-03-21 18:05:30 +0000457 EasyMock.replay(mockRescheduler);
Brett Chabot3759c852010-07-13 17:36:31 -0700458 try {
Julien Desprez47c287f2016-07-14 12:16:57 +0100459 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
Brett Chabot3759c852010-07-13 17:36:31 -0700460 fail("DeviceNotAvailableException not thrown");
Brett Chabot3759c852010-07-13 17:36:31 -0700461 } catch (DeviceNotAvailableException e) {
462 // expected
463 }
Julien Desprez2040ac02016-03-21 18:05:30 +0000464 verifyMocks(test, mockRescheduler);
Omari Stephens203296c2010-09-02 18:20:45 -0700465 verifySummaryListener();
Brett Chabot3759c852010-07-13 17:36:31 -0700466 }
467
468 /**
Omari Stephens203296c2010-09-02 18:20:45 -0700469 * Test the invoke scenario where preparer throws {@link BuildError}
Brett Chabotfe88a152010-08-02 17:52:59 -0700470 *
471 * @throws Exception if unexpected error occurs
472 */
Brett Chabot87803df2013-03-01 16:01:50 -0800473 public void testInvoke_buildError() throws Throwable {
Julien Desprez07fe1532016-08-12 11:48:32 +0100474 BuildError exception = new BuildError("error", mFakeDescriptor);
Brett Chabotfe88a152010-08-02 17:52:59 -0700475 IRemoteTest test = EasyMock.createMock(IRemoteTest.class);
Brett Chabotb1052c22011-01-12 19:44:23 -0800476 mStubConfiguration.setTest(test);
Brett Chabotfe88a152010-08-02 17:52:59 -0700477 EasyMock.expect(mMockBuildProvider.getBuild()).andReturn(mMockBuildInfo);
Brett Chabot1d35e662011-05-25 19:48:24 -0700478
Brett Chabotfe88a152010-08-02 17:52:59 -0700479 mMockPreparer.setUp(mMockDevice, mMockBuildInfo);
480 EasyMock.expectLastCall().andThrow(exception);
Omari Stephens203296c2010-09-02 18:20:45 -0700481 setupMockFailureListeners(exception);
Michael Rosenfeld55012252017-01-12 15:45:19 -0800482 EasyMock.expect(mMockDevice.getBugreport()).andReturn(EMPTY_STREAM_SOURCE);
Brett Chabot44556372014-03-19 19:07:15 -0700483 setupInvokeWithBuild();
Brett Chabotfe88a152010-08-02 17:52:59 -0700484 replayMocks(test);
Julien Desprez2040ac02016-03-21 18:05:30 +0000485 EasyMock.replay(mockRescheduler);
Julien Desprez47c287f2016-07-14 12:16:57 +0100486 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
Julien Desprez2040ac02016-03-21 18:05:30 +0000487 verifyMocks(test, mockRescheduler);
Omari Stephens203296c2010-09-02 18:20:45 -0700488 verifySummaryListener();
Brett Chabotfe88a152010-08-02 17:52:59 -0700489 }
490
Brett Chabotfe88a152010-08-02 17:52:59 -0700491 /**
Brett Chabotb1052c22011-01-12 19:44:23 -0800492 * Test the invoke scenario for a {@link IResumableTest}.
493 *
494 * @throws Exception if unexpected error occurs
495 */
Brett Chabot87803df2013-03-01 16:01:50 -0800496 public void testInvoke_resume() throws Throwable {
Brett Chabotb1052c22011-01-12 19:44:23 -0800497 IResumableTest resumableTest = EasyMock.createMock(IResumableTest.class);
498 mStubConfiguration.setTest(resumableTest);
499 ITestInvocationListener resumeListener = EasyMock.createStrictMock(
500 ITestInvocationListener.class);
501 mStubConfiguration.setTestInvocationListener(resumeListener);
502
503 EasyMock.expect(mMockBuildProvider.getBuild()).andReturn(mMockBuildInfo);
Julien Desprez53353e62016-08-12 15:24:33 +0100504 resumeListener.invocationStarted(mStubInvocationMetadata);
Moon Kim80553c52014-09-15 13:45:40 -0700505 mMockDevice.clearLastConnectedWifiNetwork();
Brett Chabotc8b709f2011-09-02 09:04:22 -0700506 mMockDevice.setOptions((TestDeviceOptions)EasyMock.anyObject());
Brett Chaboteba1fd62011-09-13 17:17:45 -0700507 mMockBuildInfo.setDeviceSerial(SERIAL);
Julien Desprezf7f32c92016-07-27 10:07:50 +0100508 EasyMock.expect(mMockBuildInfo.getTestTag()).andStubReturn("");
Brett Chabot44556372014-03-19 19:07:15 -0700509 mMockDevice.startLogcat();
Brett Chabotb1052c22011-01-12 19:44:23 -0800510 mMockPreparer.setUp(mMockDevice, mMockBuildInfo);
511
Michael Rosenfeld55012252017-01-12 15:45:19 -0800512 resumableTest.run((ITestInvocationListener) EasyMock.anyObject());
Julien Desprez47c287f2016-07-14 12:16:57 +0100513 EasyMock.expectLastCall().andThrow(new DeviceNotAvailableException("ERROR", SERIAL));
Brett Chabotb1052c22011-01-12 19:44:23 -0800514 EasyMock.expect(resumableTest.isResumable()).andReturn(Boolean.TRUE);
Julien Desprez70488e82016-02-05 11:28:34 +0000515 mMockDevice.setRecoveryMode(RecoveryMode.NONE);
516 EasyMock.expectLastCall();
Michael Rosenfeld55012252017-01-12 15:45:19 -0800517 EasyMock.expect(mMockDevice.getLogcat()).andReturn(EMPTY_STREAM_SOURCE).times(3);
518 mMockDevice.clearLogcat();
519 EasyMock.expectLastCall().times(3);
520 EasyMock.expect(mMockLogger.getLog()).andReturn(EMPTY_STREAM_SOURCE);
521 EasyMock.expect(
522 mMockLogSaver.saveLogData(
523 EasyMock.eq(LOGCAT_NAME_SETUP),
524 EasyMock.eq(LogDataType.LOGCAT),
525 (InputStream) EasyMock.anyObject()))
526 .andReturn(new LogFile(PATH, URL, false /* compressed */, true /* text */));
527 EasyMock.expect(
528 mMockLogSaver.saveLogData(
529 EasyMock.eq(LOGCAT_NAME_TEST),
530 EasyMock.eq(LogDataType.LOGCAT),
531 (InputStream) EasyMock.anyObject()))
532 .andReturn(new LogFile(PATH, URL, false /* compressed */, true /* text */));
533 EasyMock.expect(
534 mMockLogSaver.saveLogData(
535 EasyMock.eq(LOGCAT_NAME_TEARDOWN),
536 EasyMock.eq(LogDataType.LOGCAT),
537 (InputStream) EasyMock.anyObject()))
538 .andReturn(new LogFile(PATH, URL, false /* compressed */, true /* text */));
539 EasyMock.expect(
540 mMockLogSaver.saveLogData(
541 EasyMock.eq(TestInvocation.TRADEFED_LOG_NAME),
542 EasyMock.eq(LogDataType.TEXT),
543 (InputStream) EasyMock.anyObject()))
544 .andReturn(new LogFile(PATH, URL, false /* compressed */, true /* text */));
545 resumeListener.testLog(
546 EasyMock.eq(LOGCAT_NAME_SETUP),
547 EasyMock.eq(LogDataType.LOGCAT),
548 (InputStreamSource) EasyMock.anyObject());
549 resumeListener.testLog(
550 EasyMock.eq(LOGCAT_NAME_TEST),
551 EasyMock.eq(LogDataType.LOGCAT),
552 (InputStreamSource) EasyMock.anyObject());
553 resumeListener.testLog(
554 EasyMock.eq(LOGCAT_NAME_TEARDOWN),
555 EasyMock.eq(LogDataType.LOGCAT),
556 (InputStreamSource) EasyMock.anyObject());
Brett Chabotb1052c22011-01-12 19:44:23 -0800557 resumeListener.testLog(EasyMock.eq(TestInvocation.TRADEFED_LOG_NAME),
Omari Stephens41978ee2011-02-23 19:03:05 -0800558 EasyMock.eq(LogDataType.TEXT), (InputStreamSource)EasyMock.anyObject());
Brett Chabotb1052c22011-01-12 19:44:23 -0800559
560 // just return same build and logger for simplicity
561 EasyMock.expect(mMockBuildInfo.clone()).andReturn(mMockBuildInfo);
562 EasyMock.expect(mMockLogger.clone()).andReturn(mMockLogger);
563 IRescheduler mockRescheduler = EasyMock.createMock(IRescheduler.class);
564 Capture<IConfiguration> capturedConfig = new Capture<IConfiguration>();
565 EasyMock.expect(mockRescheduler.scheduleConfig(EasyMock.capture(capturedConfig)))
566 .andReturn(Boolean.TRUE);
jdesprez58e413e2018-03-09 10:30:17 -0800567 // When resuming the original build provider is still going to handle the clean up.
Brett Chabotb1052c22011-01-12 19:44:23 -0800568 mMockBuildProvider.cleanUp(mMockBuildInfo);
jdesprez58e413e2018-03-09 10:30:17 -0800569 EasyMock.expectLastCall().times(4);
Guang Zhu0a1c63d2015-01-02 11:03:38 -0800570 mMockDevice.clearLastConnectedWifiNetwork();
Brett Chabot44556372014-03-19 19:07:15 -0700571 mMockDevice.stopLogcat();
Brett Chabotb1052c22011-01-12 19:44:23 -0800572
Brett Chabot35d50c62011-03-07 10:39:23 -0800573 mMockLogger.init();
Julien Desprez53353e62016-08-12 15:24:33 +0100574 mMockLogSaver.invocationStarted(mStubInvocationMetadata);
Brett Chabotb1052c22011-01-12 19:44:23 -0800575 // now set resumed invocation expectations
Moon Kim80553c52014-09-15 13:45:40 -0700576 mMockDevice.clearLastConnectedWifiNetwork();
Brett Chabotc8b709f2011-09-02 09:04:22 -0700577 mMockDevice.setOptions((TestDeviceOptions)EasyMock.anyObject());
Brett Chaboteba1fd62011-09-13 17:17:45 -0700578 mMockBuildInfo.setDeviceSerial(SERIAL);
Julien Desprezf7f32c92016-07-27 10:07:50 +0100579 mMockBuildInfo.setTestTag(EasyMock.eq("stub"));
580 EasyMock.expectLastCall().times(2);
Brett Chabot44556372014-03-19 19:07:15 -0700581 mMockDevice.startLogcat();
Brett Chabotb1052c22011-01-12 19:44:23 -0800582 mMockPreparer.setUp(mMockDevice, mMockBuildInfo);
Julien Desprez53353e62016-08-12 15:24:33 +0100583 mMockLogSaver.invocationStarted(mStubInvocationMetadata);
Brett Chabotb1052c22011-01-12 19:44:23 -0800584 mMockDevice.setRecovery(mMockRecovery);
Brett Chabotad123582011-02-09 18:26:40 -0800585 resumableTest.run((ITestInvocationListener)EasyMock.anyObject());
Michael Rosenfeld55012252017-01-12 15:45:19 -0800586 EasyMock.expect(mMockDevice.getLogcat()).andReturn(EMPTY_STREAM_SOURCE).times(3);
587 mMockDevice.clearLogcat();
588 EasyMock.expectLastCall().times(3);
589 EasyMock.expect(mMockLogger.getLog()).andReturn(EMPTY_STREAM_SOURCE);
590 EasyMock.expect(
591 mMockLogSaver.saveLogData(
592 EasyMock.eq(LOGCAT_NAME_SETUP),
593 EasyMock.eq(LogDataType.LOGCAT),
594 (InputStream) EasyMock.anyObject()))
595 .andReturn(new LogFile(PATH, URL, false /* compressed */, true /* text */));
596 EasyMock.expect(
597 mMockLogSaver.saveLogData(
598 EasyMock.eq(LOGCAT_NAME_TEST),
599 EasyMock.eq(LogDataType.LOGCAT),
600 (InputStream) EasyMock.anyObject()))
601 .andReturn(new LogFile(PATH, URL, false /* compressed */, true /* text */));
602 EasyMock.expect(
603 mMockLogSaver.saveLogData(
604 EasyMock.eq(LOGCAT_NAME_TEARDOWN),
605 EasyMock.eq(LogDataType.LOGCAT),
606 (InputStream) EasyMock.anyObject()))
607 .andReturn(new LogFile(PATH, URL, false /* compressed */, true /* text */));
608 EasyMock.expect(
609 mMockLogSaver.saveLogData(
610 EasyMock.eq(TestInvocation.TRADEFED_LOG_NAME),
611 EasyMock.eq(LogDataType.TEXT),
612 (InputStream) EasyMock.anyObject()))
613 .andReturn(new LogFile(PATH, URL, false /* compressed */, true /* text */));
614 resumeListener.testLog(
615 EasyMock.eq(LOGCAT_NAME_SETUP),
616 EasyMock.eq(LogDataType.LOGCAT),
617 (InputStreamSource) EasyMock.anyObject());
618 resumeListener.testLog(
619 EasyMock.eq(LOGCAT_NAME_TEST),
620 EasyMock.eq(LogDataType.LOGCAT),
621 (InputStreamSource) EasyMock.anyObject());
622 resumeListener.testLog(
623 EasyMock.eq(LOGCAT_NAME_TEARDOWN),
624 EasyMock.eq(LogDataType.LOGCAT),
625 (InputStreamSource) EasyMock.anyObject());
626 resumeListener.testLog(
627 EasyMock.eq(TestInvocation.TRADEFED_LOG_NAME),
628 EasyMock.eq(LogDataType.TEXT),
629 (InputStreamSource) EasyMock.anyObject());
Brett Chabotb1052c22011-01-12 19:44:23 -0800630 resumeListener.invocationEnded(EasyMock.anyLong());
Eric Rowe0b445112013-07-12 09:29:47 -0700631 mMockLogSaver.invocationEnded(EasyMock.anyLong());
Brett Chabotb1052c22011-01-12 19:44:23 -0800632 EasyMock.expect(resumeListener.getSummary()).andReturn(null);
Brett Chabotb1052c22011-01-12 19:44:23 -0800633 mMockLogger.closeLog();
jdesprez3dff70b2017-04-18 10:39:13 -0700634 EasyMock.expectLastCall().times(3);
Guang Zhu0a1c63d2015-01-02 11:03:38 -0800635 mMockDevice.clearLastConnectedWifiNetwork();
Brett Chabot44556372014-03-19 19:07:15 -0700636 mMockDevice.stopLogcat();
Brett Chabotb1052c22011-01-12 19:44:23 -0800637 EasyMock.replay(mockRescheduler, resumeListener, resumableTest, mMockPreparer,
Eric Rowe0b445112013-07-12 09:29:47 -0700638 mMockBuildProvider, mMockLogger, mMockLogSaver, mMockDevice, mMockBuildInfo);
Brett Chabotb1052c22011-01-12 19:44:23 -0800639
640 try {
Julien Desprez47c287f2016-07-14 12:16:57 +0100641 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
Brett Chabotb1052c22011-01-12 19:44:23 -0800642 fail("DeviceNotAvailableException not thrown");
643 } catch (DeviceNotAvailableException e) {
644 // expect
645 }
646 // now call again, and expect invocation to be resumed properly
Julien Desprez47c287f2016-07-14 12:16:57 +0100647 mTestInvocation.invoke(mStubInvocationMetadata, capturedConfig.getValue(), mockRescheduler);
Brett Chabotb1052c22011-01-12 19:44:23 -0800648
Eric Rowe0b445112013-07-12 09:29:47 -0700649 EasyMock.verify(mockRescheduler, resumeListener, resumableTest, mMockPreparer,
650 mMockBuildProvider, mMockLogger, mMockLogSaver, mMockDevice, mMockBuildInfo);
Brett Chabotb1052c22011-01-12 19:44:23 -0800651 }
652
653 /**
Eric Rowea3b85852012-07-16 15:07:49 -0700654 * Test the invoke scenario for a {@link IRetriableTest}.
655 *
656 * @throws Exception if unexpected error occurs
657 */
Brett Chabot87803df2013-03-01 16:01:50 -0800658 public void testInvoke_retry() throws Throwable {
Eric Rowea3b85852012-07-16 15:07:49 -0700659 AssertionError exception = new AssertionError();
660 IRetriableTest test = EasyMock.createMock(IRetriableTest.class);
661 test.run((ITestInvocationListener)EasyMock.anyObject());
662 EasyMock.expectLastCall().andThrow(exception);
663 EasyMock.expect(test.isRetriable()).andReturn(Boolean.TRUE);
664 mStubConfiguration.getCommandOptions().setLoopMode(false);
665 IRescheduler mockRescheduler = EasyMock.createMock(IRescheduler.class);
666 EasyMock.expect(mockRescheduler.rescheduleCommand()).andReturn(EasyMock.anyBoolean());
Eric Rowea3b85852012-07-16 15:07:49 -0700667 mMockBuildProvider.buildNotTested(mMockBuildInfo);
Eric Rowe0b445112013-07-12 09:29:47 -0700668 setupMockFailureListeners(exception);
Eric Rowea3b85852012-07-16 15:07:49 -0700669 setupNormalInvoke(test);
670 EasyMock.replay(mockRescheduler);
Julien Desprez47c287f2016-07-14 12:16:57 +0100671 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
Eric Rowe0b445112013-07-12 09:29:47 -0700672 verifyMocks(test, mockRescheduler);
Eric Rowea3b85852012-07-16 15:07:49 -0700673 verifySummaryListener();
674 }
675
676 /**
Julien Desprez47c287f2016-07-14 12:16:57 +0100677 * Test the {@link TestInvocation#invoke(IInvocationContext, IConfiguration, IRescheduler,
Julien Desprez2040ac02016-03-21 18:05:30 +0000678 * ITestInvocationListener[])} scenario
Eric Rowe0b445112013-07-12 09:29:47 -0700679 * when a {@link ITargetCleaner} is part of the config.
Brett Chabot87803df2013-03-01 16:01:50 -0800680 */
681 public void testInvoke_tearDown() throws Throwable {
682 IRemoteTest test = EasyMock.createNiceMock(IRemoteTest.class);
683 ITargetCleaner mockCleaner = EasyMock.createMock(ITargetCleaner.class);
jdesprez5ae744c2017-10-27 10:36:53 -0700684 EasyMock.expect(mockCleaner.isDisabled()).andReturn(false).times(2);
Brett Chabot87803df2013-03-01 16:01:50 -0800685 mockCleaner.setUp(mMockDevice, mMockBuildInfo);
686 mockCleaner.tearDown(mMockDevice, mMockBuildInfo, null);
687 mStubConfiguration.getTargetPreparers().add(mockCleaner);
688 setupMockSuccessListeners();
689 setupNormalInvoke(test);
Julien Desprez2040ac02016-03-21 18:05:30 +0000690 EasyMock.replay(mockCleaner, mockRescheduler);
Julien Desprez47c287f2016-07-14 12:16:57 +0100691 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
Julien Desprez2040ac02016-03-21 18:05:30 +0000692 verifyMocks(mockCleaner, mockRescheduler);
Brett Chabot87803df2013-03-01 16:01:50 -0800693 verifySummaryListener();
694 }
695
696 /**
Julien Desprez47c287f2016-07-14 12:16:57 +0100697 * Test the {@link TestInvocation#invoke(IInvocationContext, IConfiguration, IRescheduler,
Julien Desprez2040ac02016-03-21 18:05:30 +0000698 * ITestInvocationListener[])} scenario
Eric Rowe0b445112013-07-12 09:29:47 -0700699 * when a {@link ITargetCleaner} is part of the config, and the test throws a
Brett Chabot87803df2013-03-01 16:01:50 -0800700 * {@link DeviceNotAvailableException}.
701 */
702 public void testInvoke_tearDown_deviceNotAvail() throws Throwable {
Julien Desprez47c287f2016-07-14 12:16:57 +0100703 DeviceNotAvailableException exception = new DeviceNotAvailableException("ERROR", SERIAL);
Brett Chabot87803df2013-03-01 16:01:50 -0800704 IRemoteTest test = EasyMock.createMock(IRemoteTest.class);
705 test.run((ITestInvocationListener)EasyMock.anyObject());
706 EasyMock.expectLastCall().andThrow(exception);
707 ITargetCleaner mockCleaner = EasyMock.createMock(ITargetCleaner.class);
jdesprez5ae744c2017-10-27 10:36:53 -0700708 EasyMock.expect(mockCleaner.isDisabled()).andReturn(false).times(2);
Brett Chabot87803df2013-03-01 16:01:50 -0800709 mockCleaner.setUp(mMockDevice, mMockBuildInfo);
Guang Zhu36c56a22014-12-30 19:56:00 -0800710 EasyMock.expectLastCall();
711 mockCleaner.tearDown(mMockDevice, mMockBuildInfo, exception);
712 EasyMock.expectLastCall();
Julien Desprez70488e82016-02-05 11:28:34 +0000713 mMockDevice.setRecoveryMode(RecoveryMode.NONE);
714 EasyMock.expectLastCall();
Julien Desprez2040ac02016-03-21 18:05:30 +0000715 EasyMock.replay(mockCleaner, mockRescheduler);
Brett Chabot87803df2013-03-01 16:01:50 -0800716 mStubConfiguration.getTargetPreparers().add(mockCleaner);
717 setupMockFailureListeners(exception);
718 mMockBuildProvider.buildNotTested(mMockBuildInfo);
719 setupNormalInvoke(test);
Brett Chabot87803df2013-03-01 16:01:50 -0800720 try {
Julien Desprez47c287f2016-07-14 12:16:57 +0100721 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
Brett Chabot87803df2013-03-01 16:01:50 -0800722 fail("DeviceNotAvailableException not thrown");
723 } catch (DeviceNotAvailableException e) {
724 // expected
725 }
Julien Desprez2040ac02016-03-21 18:05:30 +0000726 verifyMocks(mockCleaner, mockRescheduler);
Brett Chabot87803df2013-03-01 16:01:50 -0800727 verifySummaryListener();
728 }
729
730 /**
Julien Desprez47c287f2016-07-14 12:16:57 +0100731 * Test the {@link TestInvocation#invoke(IInvocationContext, IConfiguration, IRescheduler,
Julien Desprez2040ac02016-03-21 18:05:30 +0000732 * ITestInvocationListener[])} scenario
Eric Rowe0b445112013-07-12 09:29:47 -0700733 * when a {@link ITargetCleaner} is part of the config, and the test throws a
Brett Chabot87803df2013-03-01 16:01:50 -0800734 * {@link RuntimeException}.
735 */
736 public void testInvoke_tearDown_runtime() throws Throwable {
737 RuntimeException exception = new RuntimeException();
738 IRemoteTest test = EasyMock.createMock(IRemoteTest.class);
739 test.run((ITestInvocationListener)EasyMock.anyObject());
740 EasyMock.expectLastCall().andThrow(exception);
741 ITargetCleaner mockCleaner = EasyMock.createMock(ITargetCleaner.class);
jdesprez5ae744c2017-10-27 10:36:53 -0700742 EasyMock.expect(mockCleaner.isDisabled()).andReturn(false).times(2);
Brett Chabot87803df2013-03-01 16:01:50 -0800743 mockCleaner.setUp(mMockDevice, mMockBuildInfo);
744 // tearDown should be called
745 mockCleaner.tearDown(mMockDevice, mMockBuildInfo, exception);
746 mStubConfiguration.getTargetPreparers().add(mockCleaner);
747 setupMockFailureListeners(exception);
748 mMockBuildProvider.buildNotTested(mMockBuildInfo);
749 setupNormalInvoke(test);
Julien Desprez2040ac02016-03-21 18:05:30 +0000750 EasyMock.replay(mockCleaner, mockRescheduler);
Brett Chabot87803df2013-03-01 16:01:50 -0800751 try {
Julien Desprez47c287f2016-07-14 12:16:57 +0100752 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
Brett Chabot87803df2013-03-01 16:01:50 -0800753 fail("RuntimeException not thrown");
754 } catch (RuntimeException e) {
755 // expected
756 }
Julien Desprez2040ac02016-03-21 18:05:30 +0000757 verifyMocks(mockCleaner, mockRescheduler);
Brett Chabot87803df2013-03-01 16:01:50 -0800758 verifySummaryListener();
759 }
760
761 /**
Julien Desprez47c287f2016-07-14 12:16:57 +0100762 * Test the {@link TestInvocation#invoke(IInvocationContext, IConfiguration, IRescheduler,
Julien Desprez2040ac02016-03-21 18:05:30 +0000763 * ITestInvocationListener[])} scenario
Eric Rowe0b445112013-07-12 09:29:47 -0700764 * when there is {@link ITestInvocationListener} which implements the {@link ILogSaverListener}
765 * interface.
766 */
767 public void testInvoke_logFileSaved() throws Throwable {
768 List<ITestInvocationListener> listenerList =
769 mStubConfiguration.getTestInvocationListeners();
770 ILogSaverListener logSaverListener = EasyMock.createMock(ILogSaverListener.class);
771 listenerList.add(logSaverListener);
772 mStubConfiguration.setTestInvocationListeners(listenerList);
773
774 logSaverListener.setLogSaver(mMockLogSaver);
Julien Desprez53353e62016-08-12 15:24:33 +0100775 logSaverListener.invocationStarted(mStubInvocationMetadata);
Michael Rosenfeld55012252017-01-12 15:45:19 -0800776 logSaverListener.testLog(
777 EasyMock.eq(LOGCAT_NAME_SETUP),
778 EasyMock.eq(LogDataType.LOGCAT),
779 (InputStreamSource) EasyMock.anyObject());
780 logSaverListener.testLogSaved(
781 EasyMock.eq(LOGCAT_NAME_SETUP),
782 EasyMock.eq(LogDataType.LOGCAT),
783 (InputStreamSource) EasyMock.anyObject(),
784 (LogFile) EasyMock.anyObject());
785 logSaverListener.testLog(
786 EasyMock.eq(LOGCAT_NAME_TEST),
787 EasyMock.eq(LogDataType.LOGCAT),
788 (InputStreamSource) EasyMock.anyObject());
789 logSaverListener.testLogSaved(
790 EasyMock.eq(LOGCAT_NAME_TEST),
791 EasyMock.eq(LogDataType.LOGCAT),
792 (InputStreamSource) EasyMock.anyObject(),
793 (LogFile) EasyMock.anyObject());
794 logSaverListener.testLog(
795 EasyMock.eq(LOGCAT_NAME_TEARDOWN),
796 EasyMock.eq(LogDataType.LOGCAT),
797 (InputStreamSource) EasyMock.anyObject());
798 logSaverListener.testLogSaved(
799 EasyMock.eq(LOGCAT_NAME_TEARDOWN),
800 EasyMock.eq(LogDataType.LOGCAT),
801 (InputStreamSource) EasyMock.anyObject(),
802 (LogFile) EasyMock.anyObject());
803 logSaverListener.testLog(
804 EasyMock.eq(TestInvocation.TRADEFED_LOG_NAME),
805 EasyMock.eq(LogDataType.TEXT),
806 (InputStreamSource) EasyMock.anyObject());
807 logSaverListener.testLogSaved(
808 EasyMock.eq(TestInvocation.TRADEFED_LOG_NAME),
809 EasyMock.eq(LogDataType.TEXT),
810 (InputStreamSource) EasyMock.anyObject(),
811 (LogFile) EasyMock.anyObject());
Eric Rowe0b445112013-07-12 09:29:47 -0700812 logSaverListener.invocationEnded(EasyMock.anyLong());
813 EasyMock.expect(logSaverListener.getSummary()).andReturn(mSummary);
814
815 IRemoteTest test = EasyMock.createMock(IRemoteTest.class);
816 setupMockSuccessListeners();
817 test.run((ITestInvocationListener)EasyMock.anyObject());
818 setupNormalInvoke(test);
Julien Desprez2040ac02016-03-21 18:05:30 +0000819 EasyMock.replay(logSaverListener, mockRescheduler);
Julien Desprez47c287f2016-07-14 12:16:57 +0100820 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
Julien Desprez2040ac02016-03-21 18:05:30 +0000821 verifyMocks(test, logSaverListener, mockRescheduler);
Eric Rowe0b445112013-07-12 09:29:47 -0700822 assertEquals(2, mUriCapture.getValue().size());
823 }
824
825 /**
Julien Desprez47c287f2016-07-14 12:16:57 +0100826 * Test the {@link TestInvocation#invoke(IInvocationContext, IConfiguration, IRescheduler,
827 * ITestInvocationListener[])}
Moon Kimb78076b2016-07-06 15:12:07 -0700828 * scenario with {@link IStrictShardableTest} when a shard index is given.
829 */
830 public void testInvoke_strictShardableTest_withShardIndex() throws Throwable {
jdesprezbc580f92017-06-02 11:41:40 -0700831 mTestInvocation =
832 new TestInvocation() {
833 @Override
834 ILogRegistry getLogRegistry() {
835 return mMockLogRegistry;
836 }
837
838 @Override
jdesprez85129dd2017-11-13 06:05:57 -0800839 public IInvocationExecution createInvocationExec() {
840 return new InvocationExecution() {
841 @Override
842 protected IShardHelper createShardHelper() {
843 return new StrictShardHelper();
844 }
845 };
jdesprezbc580f92017-06-02 11:41:40 -0700846 }
847
848 @Override
849 protected void setExitCode(ExitCode code, Throwable stack) {
850 // empty on purpose
851 }
852 };
Moon Kimb78076b2016-07-06 15:12:07 -0700853 String[] commandLine = {"config", "arg"};
854 int shardCount = 10;
855 int shardIndex = 5;
856 IStrictShardableTest test = EasyMock.createMock(IStrictShardableTest.class);
857 IRemoteTest testShard = EasyMock.createMock(IRemoteTest.class);
858 mStubConfiguration.setTest(test);
859 mStubConfiguration.setCommandLine(commandLine);
860 mStubConfiguration.getCommandOptions().setShardCount(shardCount);
861 mStubConfiguration.getCommandOptions().setShardIndex(shardIndex);
862
863 setupInvokeWithBuild();
864 setupMockSuccessListeners();
865 EasyMock.expect(mMockBuildProvider.getBuild()).andReturn(mMockBuildInfo);
866 mMockBuildInfo.addBuildAttribute("command_line_args", "config arg");
867 mMockBuildInfo.addBuildAttribute("shard_count", "10");
868 mMockBuildInfo.addBuildAttribute("shard_index", "5");
869 EasyMock.expect(test.getTestShard(shardCount, shardIndex)).andReturn(testShard);
Julien Desprez8427fc22016-05-31 15:56:01 +0100870 testShard.run((ITestInvocationListener)EasyMock.anyObject());
Moon Kimb78076b2016-07-06 15:12:07 -0700871 mMockPreparer.setUp(mMockDevice, mMockBuildInfo);
872 replayMocks(test, testShard);
873
Julien Desprez47c287f2016-07-14 12:16:57 +0100874 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
Moon Kimb78076b2016-07-06 15:12:07 -0700875
876 verifyMocks(test, testShard);
877 }
878
879 /**
Julien Desprez47c287f2016-07-14 12:16:57 +0100880 * Test the {@link TestInvocation#invoke(IInvocationContext, IConfiguration, IRescheduler,
881 * ITestInvocationListener[])}
Moon Kim7bc070b2016-07-14 12:29:42 -0700882 * scenario with non-{@link IStrictShardableTest} when shard index 0 is given.
883 */
884 public void testInvoke_nonStrictShardableTest_withShardIndexZero() throws Throwable {
885 String[] commandLine = {"config", "arg"};
886 int shardCount = 10;
887 int shardIndex = 0;
888 IRemoteTest test = EasyMock.createMock(IRemoteTest.class);
889 mStubConfiguration.setTest(test);
890 mStubConfiguration.setCommandLine(commandLine);
891 mStubConfiguration.getCommandOptions().setShardCount(shardCount);
892 mStubConfiguration.getCommandOptions().setShardIndex(shardIndex);
893
894 setupInvokeWithBuild();
895 setupMockSuccessListeners();
896 EasyMock.expect(mMockBuildProvider.getBuild()).andReturn(mMockBuildInfo);
897 mMockBuildInfo.addBuildAttribute("command_line_args", "config arg");
898 mMockBuildInfo.addBuildAttribute("shard_count", "10");
899 mMockBuildInfo.addBuildAttribute("shard_index", "0");
900 test.run((ITestInvocationListener)EasyMock.anyObject());
901 mMockPreparer.setUp(mMockDevice, mMockBuildInfo);
902 replayMocks(test);
903
Julien Desprez47c287f2016-07-14 12:16:57 +0100904 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
Moon Kim7bc070b2016-07-14 12:29:42 -0700905
906 verifyMocks(test);
907 }
908
909 /**
Julien Desprez47c287f2016-07-14 12:16:57 +0100910 * Test the {@link TestInvocation#invoke(IInvocationContext, IConfiguration, IRescheduler,
911 * ITestInvocationListener[])}
Moon Kim7bc070b2016-07-14 12:29:42 -0700912 * scenario with non-{@link IStrictShardableTest} when a shard index non-0 is given.
913 */
914 public void testInvoke_nonStrictShardableTest_withShardIndexNonZero() throws Throwable {
jdesprezbc580f92017-06-02 11:41:40 -0700915 mTestInvocation =
916 new TestInvocation() {
917 @Override
918 ILogRegistry getLogRegistry() {
919 return mMockLogRegistry;
920 }
921
922 @Override
jdesprez85129dd2017-11-13 06:05:57 -0800923 public IInvocationExecution createInvocationExec() {
924 return new InvocationExecution() {
925 @Override
926 protected IShardHelper createShardHelper() {
927 return new StrictShardHelper();
928 }
929 };
jdesprezbc580f92017-06-02 11:41:40 -0700930 }
931
932 @Override
933 protected void setExitCode(ExitCode code, Throwable stack) {
934 // empty on purpose
935 }
936 };
Moon Kim7bc070b2016-07-14 12:29:42 -0700937 String[] commandLine = {"config", "arg"};
938 int shardCount = 10;
939 int shardIndex = 1;
Julien Desprez47c287f2016-07-14 12:16:57 +0100940 IStrictShardableTest shardableTest = EasyMock.createMock(IStrictShardableTest.class);
Moon Kim7bc070b2016-07-14 12:29:42 -0700941 IRemoteTest test = EasyMock.createMock(IRemoteTest.class);
Julien Desprez47c287f2016-07-14 12:16:57 +0100942 EasyMock.expect(shardableTest.getTestShard(10, 1)).andReturn(test);
943 test.run((ITestInvocationListener)EasyMock.anyObject());
944 EasyMock.expectLastCall();
945 mStubConfiguration.setTest(shardableTest);
Moon Kim7bc070b2016-07-14 12:29:42 -0700946 mStubConfiguration.setCommandLine(commandLine);
947 mStubConfiguration.getCommandOptions().setShardCount(shardCount);
948 mStubConfiguration.getCommandOptions().setShardIndex(shardIndex);
949
950 setupInvokeWithBuild();
951 setupMockSuccessListeners();
952 EasyMock.expect(mMockBuildProvider.getBuild()).andReturn(mMockBuildInfo);
953 mMockBuildInfo.addBuildAttribute("command_line_args", "config arg");
954 mMockBuildInfo.addBuildAttribute("shard_count", "10");
955 mMockBuildInfo.addBuildAttribute("shard_index", "1");
956 mMockPreparer.setUp(mMockDevice, mMockBuildInfo);
Julien Desprez47c287f2016-07-14 12:16:57 +0100957 replayMocks(shardableTest, test);
Moon Kim7bc070b2016-07-14 12:29:42 -0700958
Julien Desprez47c287f2016-07-14 12:16:57 +0100959 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
Moon Kim7bc070b2016-07-14 12:29:42 -0700960
Julien Desprez47c287f2016-07-14 12:16:57 +0100961 verifyMocks(shardableTest, test);
Moon Kim7bc070b2016-07-14 12:29:42 -0700962 }
963
964 /**
Julien Desprezf7f32c92016-07-27 10:07:50 +0100965 * Test the test-tag is set when the IBuildInfo's test-tag is not.
966 */
967 public void testInvoke_testtag() throws Throwable {
968 String[] commandLine = {"run", "empty"};
969 mStubConfiguration.setCommandLine(commandLine);
970 mStubConfiguration.getCommandOptions().setTestTag("not-default");
971
972 setupInvoke();
Michael Rosenfeld55012252017-01-12 15:45:19 -0800973 EasyMock.expect(mMockDevice.getLogcat()).andReturn(EMPTY_STREAM_SOURCE).times(3);
974 mMockDevice.clearLogcat();
975 EasyMock.expectLastCall().times(3);
976 EasyMock.expect(mMockLogger.getLog()).andReturn(EMPTY_STREAM_SOURCE);
Julien Desprezf7f32c92016-07-27 10:07:50 +0100977 mMockBuildInfo.setDeviceSerial(SERIAL);
978 mMockBuildProvider.cleanUp(mMockBuildInfo);
jdespreza90f9722017-08-11 16:48:24 -0700979 EasyMock.expectLastCall().times(2);
Julien Desprezf7f32c92016-07-27 10:07:50 +0100980 setupMockSuccessListeners();
981 EasyMock.expect(mMockBuildProvider.getBuild()).andReturn(mMockBuildInfo);
982 mMockBuildInfo.addBuildAttribute("command_line_args", "run empty");
983 mMockPreparer.setUp(mMockDevice, mMockBuildInfo);
984 // Default build is "stub" so we set the test-tag
985 mMockBuildInfo.setTestTag(EasyMock.eq("not-default"));
986 EasyMock.expectLastCall();
987 EasyMock.expect(mMockBuildInfo.getTestTag()).andStubReturn("stub");
jdesprez3dff70b2017-04-18 10:39:13 -0700988 mMockLogRegistry.unregisterLogger();
989 mMockLogRegistry.dumpToGlobalLog(mMockLogger);
990 mMockLogger.closeLog();
Julien Desprezf7f32c92016-07-27 10:07:50 +0100991 replayMocks();
Julien Desprez53353e62016-08-12 15:24:33 +0100992 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
Julien Desprezf7f32c92016-07-27 10:07:50 +0100993 verifyMocks();
994 }
995
996 /**
997 * Test the test-tag of the IBuildInfo is not modified when the CommandOption default test-tag
998 * is not modified.
999 */
1000 public void testInvoke_testtag_notset() throws Throwable {
1001 String[] commandLine = {"run", "empty"};
1002 mStubConfiguration.setCommandLine(commandLine);
1003 setupInvoke();
Michael Rosenfeld55012252017-01-12 15:45:19 -08001004 EasyMock.expect(mMockDevice.getLogcat()).andReturn(EMPTY_STREAM_SOURCE).times(3);
1005 mMockDevice.clearLogcat();
1006 EasyMock.expectLastCall().times(3);
1007 EasyMock.expect(mMockLogger.getLog()).andReturn(EMPTY_STREAM_SOURCE);
Julien Desprezf7f32c92016-07-27 10:07:50 +01001008 mMockBuildInfo.setDeviceSerial(SERIAL);
1009 mMockBuildProvider.cleanUp(mMockBuildInfo);
jdespreza90f9722017-08-11 16:48:24 -07001010 EasyMock.expectLastCall().times(2);
Julien Desprezf7f32c92016-07-27 10:07:50 +01001011 setupMockSuccessListeners();
1012 EasyMock.expect(mMockBuildProvider.getBuild()).andReturn(mMockBuildInfo);
1013 mMockBuildInfo.addBuildAttribute("command_line_args", "run empty");
1014 mMockPreparer.setUp(mMockDevice, mMockBuildInfo);
1015 EasyMock.expect(mMockBuildInfo.getTestTag()).andStubReturn("buildprovidertesttag");
jdesprez3dff70b2017-04-18 10:39:13 -07001016 mMockLogRegistry.dumpToGlobalLog(mMockLogger);
1017 mMockLogRegistry.unregisterLogger();
1018 mMockLogger.closeLog();
Julien Desprezf7f32c92016-07-27 10:07:50 +01001019 replayMocks();
Julien Desprez53353e62016-08-12 15:24:33 +01001020 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
Julien Desprezf7f32c92016-07-27 10:07:50 +01001021 verifyMocks();
1022 }
1023
1024 /**
1025 * Test the test-tag of the IBuildInfo is not set and Command Option is not set either.
1026 * A default 'stub' test-tag is set to ensure reporting is done.
1027 */
1028 public void testInvoke_notesttag() throws Throwable {
1029 String[] commandLine = {"run", "empty"};
1030 mStubConfiguration.setCommandLine(commandLine);
1031 setupInvoke();
Michael Rosenfeld55012252017-01-12 15:45:19 -08001032 EasyMock.expect(mMockDevice.getLogcat()).andReturn(EMPTY_STREAM_SOURCE).times(3);
1033 mMockDevice.clearLogcat();
1034 EasyMock.expectLastCall().times(3);
1035 EasyMock.expect(mMockLogger.getLog()).andReturn(EMPTY_STREAM_SOURCE);
Julien Desprezf7f32c92016-07-27 10:07:50 +01001036 mMockBuildInfo.setDeviceSerial(SERIAL);
1037 mMockBuildProvider.cleanUp(mMockBuildInfo);
jdespreza90f9722017-08-11 16:48:24 -07001038 EasyMock.expectLastCall().times(2);
Julien Desprezf7f32c92016-07-27 10:07:50 +01001039 setupMockSuccessListeners();
1040 EasyMock.expect(mMockBuildProvider.getBuild()).andReturn(mMockBuildInfo);
1041 mMockBuildInfo.addBuildAttribute("command_line_args", "run empty");
1042 mMockPreparer.setUp(mMockDevice, mMockBuildInfo);
1043 EasyMock.expect(mMockBuildInfo.getTestTag()).andStubReturn(null);
1044 mMockBuildInfo.setTestTag(EasyMock.eq("stub"));
1045 EasyMock.expectLastCall();
jdesprez3dff70b2017-04-18 10:39:13 -07001046 mMockLogRegistry.dumpToGlobalLog(mMockLogger);
1047 mMockLogRegistry.unregisterLogger();
1048 mMockLogger.closeLog();
Julien Desprezf7f32c92016-07-27 10:07:50 +01001049 replayMocks();
Julien Desprez53353e62016-08-12 15:24:33 +01001050 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
Julien Desprezf7f32c92016-07-27 10:07:50 +01001051 verifyMocks();
1052 }
Julien Desprezaa9ea852016-11-25 10:56:35 +00001053
1054 /**
1055 * Helper tests class to expose all the interfaces needed for the tests.
1056 */
1057 private interface IFakeBuildProvider extends IDeviceBuildProvider, IInvocationContextReceiver {
1058 }
1059
1060 /**
1061 * Test the injection of test-tag from TestInvocation to the build provider via the
1062 * {@link IInvocationContextReceiver}.
1063 */
1064 public void testInvoke_buildProviderNeedTestTag() throws Throwable {
1065 final String testTag = "THISISTHETAG";
1066 String[] commandLine = {"run", "empty"};
1067 mStubConfiguration.setCommandLine(commandLine);
1068 ICommandOptions commandOption = new CommandOptions();
1069 commandOption.setTestTag(testTag);
1070 IFakeBuildProvider mockProvider = EasyMock.createMock(IFakeBuildProvider.class);
1071 mStubConfiguration.setBuildProvider(mockProvider);
1072 mStubConfiguration.setCommandOptions(commandOption);
1073 setupInvoke();
Michael Rosenfeld55012252017-01-12 15:45:19 -08001074 EasyMock.expect(mMockDevice.getLogcat()).andReturn(EMPTY_STREAM_SOURCE).times(3);
1075 mMockDevice.clearLogcat();
1076 EasyMock.expectLastCall().times(3);
1077 EasyMock.expect(mMockLogger.getLog()).andReturn(EMPTY_STREAM_SOURCE);
Julien Desprezaa9ea852016-11-25 10:56:35 +00001078 mMockBuildInfo.setDeviceSerial(SERIAL);
1079 setupMockSuccessListeners();
1080 mMockBuildInfo.addBuildAttribute("command_line_args", "run empty");
1081 mMockPreparer.setUp(mMockDevice, mMockBuildInfo);
1082 EasyMock.expect(mMockBuildInfo.getTestTag()).andStubReturn(null);
1083 // Validate proper tag is set on the build.
1084 mMockBuildInfo.setTestTag(EasyMock.eq(testTag));
1085 mockProvider.setInvocationContext((IInvocationContext)EasyMock.anyObject());
1086 EasyMock.expect(mockProvider.getBuild(mMockDevice)).andReturn(mMockBuildInfo);
1087 mockProvider.cleanUp(mMockBuildInfo);
jdespreza90f9722017-08-11 16:48:24 -07001088 EasyMock.expectLastCall().times(2);
jdesprez3dff70b2017-04-18 10:39:13 -07001089 mMockLogRegistry.dumpToGlobalLog(mMockLogger);
1090 mMockLogRegistry.unregisterLogger();
1091 mMockLogger.closeLog();
1092
Julien Desprezaa9ea852016-11-25 10:56:35 +00001093 replayMocks(mockProvider);
1094 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
1095 verifyMocks(mockProvider);
1096 }
1097
Julien Desprezf7f32c92016-07-27 10:07:50 +01001098 /**
Moon Kimb78076b2016-07-06 15:12:07 -07001099 * Set up expected conditions for normal run up to the part where tests are run.
1100 *
Brett Chabot74121d82010-01-28 20:14:27 -08001101 * @param test the {@link Test} to use.
1102 */
Brett Chabot87803df2013-03-01 16:01:50 -08001103 private void setupNormalInvoke(IRemoteTest test) throws Throwable {
Brett Chabot44556372014-03-19 19:07:15 -07001104 setupInvokeWithBuild();
Brett Chabotb1052c22011-01-12 19:44:23 -08001105 mStubConfiguration.setTest(test);
Julien Desprez47c287f2016-07-14 12:16:57 +01001106 mStubMultiConfiguration.setTest(test);
Brett Chabotfe88a152010-08-02 17:52:59 -07001107 EasyMock.expect(mMockBuildProvider.getBuild()).andReturn(mMockBuildInfo);
Brett Chabotc8b709f2011-09-02 09:04:22 -07001108
Brett Chabot74121d82010-01-28 20:14:27 -08001109 mMockPreparer.setUp(mMockDevice, mMockBuildInfo);
Brett Chabot44556372014-03-19 19:07:15 -07001110 replayMocks(test);
1111 }
1112
1113 /**
1114 * Set up expected calls that occur on every invoke, regardless of result
1115 */
1116 private void setupInvoke() {
Moon Kim80553c52014-09-15 13:45:40 -07001117 mMockDevice.clearLastConnectedWifiNetwork();
Brett Chabot44556372014-03-19 19:07:15 -07001118 mMockDevice.setOptions((TestDeviceOptions)EasyMock.anyObject());
1119 mMockDevice.startLogcat();
Guang Zhu0a1c63d2015-01-02 11:03:38 -08001120 mMockDevice.clearLastConnectedWifiNetwork();
Brett Chabot44556372014-03-19 19:07:15 -07001121 mMockDevice.stopLogcat();
1122 }
1123
1124 /**
1125 * Set up expected calls that occur on every invoke that gets a valid build
1126 */
1127 private void setupInvokeWithBuild() {
1128 setupInvoke();
Michael Rosenfeld55012252017-01-12 15:45:19 -08001129 EasyMock.expect(mMockDevice.getLogcat()).andReturn(EMPTY_STREAM_SOURCE).times(3);
1130 mMockDevice.clearLogcat();
1131 EasyMock.expectLastCall().times(3);
Omari Stephens203296c2010-09-02 18:20:45 -07001132
Michael Rosenfeld55012252017-01-12 15:45:19 -08001133 EasyMock.expect(mMockLogger.getLog()).andReturn(EMPTY_STREAM_SOURCE);
Brett Chabot44556372014-03-19 19:07:15 -07001134 mMockBuildInfo.setDeviceSerial(SERIAL);
Brett Chabot66d87572010-11-01 19:54:59 -07001135 mMockBuildProvider.cleanUp(mMockBuildInfo);
jdespreza90f9722017-08-11 16:48:24 -07001136 EasyMock.expectLastCall().anyTimes();
Julien Desprezf7f32c92016-07-27 10:07:50 +01001137 mMockBuildInfo.setTestTag(EasyMock.eq("stub"));
1138 EasyMock.expectLastCall();
1139 EasyMock.expect(mMockBuildInfo.getTestTag()).andStubReturn("");
jdesprez3dff70b2017-04-18 10:39:13 -07001140
1141 mMockLogRegistry.unregisterLogger();
1142 mMockLogRegistry.dumpToGlobalLog(mMockLogger);
1143 mMockLogger.closeLog();
Brett Chabot057e16e2010-06-27 21:53:48 -07001144 }
1145
1146 /**
Omari Stephens203296c2010-09-02 18:20:45 -07001147 * Set up expected conditions for the test InvocationListener and SummaryListener
Michael Rosenfeld55012252017-01-12 15:45:19 -08001148 *
1149 * <p>The order of calls for a single listener should be:
1150 *
Omari Stephens203296c2010-09-02 18:20:45 -07001151 * <ol>
Michael Rosenfeld55012252017-01-12 15:45:19 -08001152 * <li>invocationStarted
1153 * <li>testLog(LOGCAT_NAME_SETUP, ...) (if no build or retrieval error)
1154 * <li>invocationFailed (if run failed)
1155 * <li>testLog(LOGCAT_NAME_ERROR, ...) (if build retrieval error)
1156 * <li>testLog(LOGCAT_NAME_TEST, ...) (otherwise)
1157 * <li>testLog(build error bugreport, ...) (otherwise and if build error)
1158 * <li>testLog(LOGCAT_NAME_TEARDOWN, ...) (otherwise)
1159 * <li>testLog(TRADEFED_LOG_NAME, ...)
1160 * <li>putSummary (for an ITestSummaryListener)
1161 * <li>invocationEnded
1162 * <li>getSummary (for an ITestInvocationListener)
Omari Stephens203296c2010-09-02 18:20:45 -07001163 * </ol>
Michael Rosenfeld55012252017-01-12 15:45:19 -08001164 *
Omari Stephens203296c2010-09-02 18:20:45 -07001165 * However note that, across all listeners, any getSummary call will precede all putSummary
1166 * calls.
1167 */
Eric Rowe0b445112013-07-12 09:29:47 -07001168 private void setupMockListeners(InvocationStatus status, Throwable throwable)
1169 throws IOException {
Omari Stephens203296c2010-09-02 18:20:45 -07001170 // invocationStarted
Julien Desprez53353e62016-08-12 15:24:33 +01001171 mMockLogSaver.invocationStarted(mStubInvocationMetadata);
1172 mMockTestListener.invocationStarted(mStubInvocationMetadata);
1173 mMockSummaryListener.invocationStarted(mStubInvocationMetadata);
Omari Stephens203296c2010-09-02 18:20:45 -07001174
Michael Rosenfeldd7d700b2017-10-18 10:09:13 -07001175 if (!(throwable instanceof BuildRetrievalError)) {
Michael Rosenfeld55012252017-01-12 15:45:19 -08001176 EasyMock.expect(
1177 mMockLogSaver.saveLogData(
1178 EasyMock.eq(LOGCAT_NAME_SETUP),
1179 EasyMock.eq(LogDataType.LOGCAT),
1180 (InputStream) EasyMock.anyObject()))
1181 .andReturn(new LogFile(PATH, URL, false /* compressed */, true /* text */));
1182 mMockTestListener.testLog(
1183 EasyMock.eq(LOGCAT_NAME_SETUP),
1184 EasyMock.eq(LogDataType.LOGCAT),
1185 (InputStreamSource) EasyMock.anyObject());
1186 mMockSummaryListener.testLog(
1187 EasyMock.eq(LOGCAT_NAME_SETUP),
1188 EasyMock.eq(LogDataType.LOGCAT),
1189 (InputStreamSource) EasyMock.anyObject());
1190 }
1191
Guang Zhu36c56a22014-12-30 19:56:00 -08001192 // invocationFailed
1193 if (!status.equals(InvocationStatus.SUCCESS)) {
1194 mMockTestListener.invocationFailed(EasyMock.eq(throwable));
1195 mMockSummaryListener.invocationFailed(EasyMock.eq(throwable));
1196 }
1197
Michael Rosenfeld55012252017-01-12 15:45:19 -08001198 if (throwable instanceof BuildRetrievalError) {
1199 // Handle logcat error listeners
1200 EasyMock.expect(
1201 mMockLogSaver.saveLogData(
1202 EasyMock.eq(LOGCAT_NAME_ERROR),
1203 EasyMock.eq(LogDataType.LOGCAT),
1204 (InputStream) EasyMock.anyObject()))
Julien Desprez47c287f2016-07-14 12:16:57 +01001205 .andReturn(new LogFile(PATH, URL, false /* compressed */, true /* text */));
1206 mMockTestListener.testLog(
Michael Rosenfeld55012252017-01-12 15:45:19 -08001207 EasyMock.eq(LOGCAT_NAME_ERROR),
1208 EasyMock.eq(LogDataType.LOGCAT),
1209 (InputStreamSource) EasyMock.anyObject());
Julien Desprez47c287f2016-07-14 12:16:57 +01001210 mMockSummaryListener.testLog(
Michael Rosenfeld55012252017-01-12 15:45:19 -08001211 EasyMock.eq(LOGCAT_NAME_ERROR),
1212 EasyMock.eq(LogDataType.LOGCAT),
1213 (InputStreamSource) EasyMock.anyObject());
1214 } else {
1215 // Handle test logcat listeners
1216 EasyMock.expect(
1217 mMockLogSaver.saveLogData(
1218 EasyMock.eq(LOGCAT_NAME_TEST),
1219 EasyMock.eq(LogDataType.LOGCAT),
1220 (InputStream) EasyMock.anyObject()))
1221 .andReturn(new LogFile(PATH, URL, false /* compressed */, true /* text */));
1222 mMockTestListener.testLog(
1223 EasyMock.eq(LOGCAT_NAME_TEST),
1224 EasyMock.eq(LogDataType.LOGCAT),
1225 (InputStreamSource) EasyMock.anyObject());
1226 mMockSummaryListener.testLog(
1227 EasyMock.eq(LOGCAT_NAME_TEST),
1228 EasyMock.eq(LogDataType.LOGCAT),
1229 (InputStreamSource) EasyMock.anyObject());
1230 // Handle build error bugreport listeners
1231 if (throwable instanceof BuildError) {
1232 EasyMock.expect(
jdesprezc3537f72017-09-05 17:27:14 -07001233 mMockDevice.logBugreport(
Michael Rosenfeld55012252017-01-12 15:45:19 -08001234 EasyMock.eq(
1235 TestInvocation.BUILD_ERROR_BUGREPORT_NAME
1236 + "_"
1237 + SERIAL),
jdesprezc3537f72017-09-05 17:27:14 -07001238 EasyMock.anyObject()))
1239 .andReturn(true);
Michael Rosenfeld55012252017-01-12 15:45:19 -08001240 }
1241 // Handle teardown logcat listeners
1242 EasyMock.expect(
1243 mMockLogSaver.saveLogData(
1244 EasyMock.eq(LOGCAT_NAME_TEARDOWN),
1245 EasyMock.eq(LogDataType.LOGCAT),
1246 (InputStream) EasyMock.anyObject()))
1247 .andReturn(new LogFile(PATH, URL, false /* compressed */, true /* text */));
1248 mMockTestListener.testLog(
1249 EasyMock.eq(LOGCAT_NAME_TEARDOWN),
1250 EasyMock.eq(LogDataType.LOGCAT),
1251 (InputStreamSource) EasyMock.anyObject());
1252 mMockSummaryListener.testLog(
1253 EasyMock.eq(LOGCAT_NAME_TEARDOWN),
1254 EasyMock.eq(LogDataType.LOGCAT),
1255 (InputStreamSource) EasyMock.anyObject());
Eric Rowe4a3369d2011-11-23 12:12:16 -08001256 }
1257
Michael Rosenfeld55012252017-01-12 15:45:19 -08001258 EasyMock.expect(
1259 mMockLogSaver.saveLogData(
1260 EasyMock.eq(TestInvocation.TRADEFED_LOG_NAME),
1261 EasyMock.eq(LogDataType.TEXT),
1262 (InputStream) EasyMock.anyObject()))
1263 .andReturn(new LogFile(PATH, URL, false /* compressed */, true /* text */));
Omari Stephens203296c2010-09-02 18:20:45 -07001264 mMockTestListener.testLog(EasyMock.eq(TestInvocation.TRADEFED_LOG_NAME),
Omari Stephens41978ee2011-02-23 19:03:05 -08001265 EasyMock.eq(LogDataType.TEXT), (InputStreamSource)EasyMock.anyObject());
Omari Stephens203296c2010-09-02 18:20:45 -07001266 mMockSummaryListener.testLog(EasyMock.eq(TestInvocation.TRADEFED_LOG_NAME),
Omari Stephens41978ee2011-02-23 19:03:05 -08001267 EasyMock.eq(LogDataType.TEXT), (InputStreamSource)EasyMock.anyObject());
Omari Stephens203296c2010-09-02 18:20:45 -07001268
1269 // invocationEnded, getSummary (mMockTestListener)
1270 mMockTestListener.invocationEnded(EasyMock.anyLong());
Omari Stephensa01abfe2010-09-07 15:48:44 -07001271 EasyMock.expect(mMockTestListener.getSummary()).andReturn(mSummary);
Omari Stephens203296c2010-09-02 18:20:45 -07001272
1273 // putSummary, invocationEnded (mMockSummaryListener)
1274 mMockSummaryListener.putSummary(EasyMock.capture(mUriCapture));
1275 mMockSummaryListener.invocationEnded(EasyMock.anyLong());
Eric Rowe0b445112013-07-12 09:29:47 -07001276 mMockLogSaver.invocationEnded(EasyMock.anyLong());
Omari Stephens203296c2010-09-02 18:20:45 -07001277 }
1278
Julien Desprez2763ca02016-12-06 14:39:33 +00001279 /**
1280 * Test the {@link TestInvocation#invoke(IInvocationContext, IConfiguration, IRescheduler,
1281 * ITestInvocationListener[])} scenario with {@link IShardableTest}.
1282 */
1283 public void testInvoke_shardableTest_legacy() throws Throwable {
jdesprez8ba33462017-04-05 16:51:13 -07001284 String command = "empty --test-tag t";
1285 String[] commandLine = {"empty", "--test-tag", "t"};
Julien Desprez2763ca02016-12-06 14:39:33 +00001286 int shardCount = 2;
1287 IShardableTest test = EasyMock.createMock(IShardableTest.class);
1288 List<IRemoteTest> shards = new ArrayList<>();
1289 IRemoteTest shard1 = EasyMock.createMock(IRemoteTest.class);
1290 IRemoteTest shard2 = EasyMock.createMock(IRemoteTest.class);
1291 shards.add(shard1);
1292 shards.add(shard2);
1293 EasyMock.expect(test.split()).andReturn(shards);
1294 mStubConfiguration.setTest(test);
1295 mStubConfiguration.setCommandLine(commandLine);
jdespreza90f9722017-08-11 16:48:24 -07001296 mMockBuildProvider.cleanUp(mMockBuildInfo);
Julien Desprez2763ca02016-12-06 14:39:33 +00001297 setupInvoke();
jdesprez8ba33462017-04-05 16:51:13 -07001298 setupNShardInvocation(shardCount, command);
jdesprez3dff70b2017-04-18 10:39:13 -07001299 mMockLogRegistry.dumpToGlobalLog(mMockLogger);
Julien Desprez2763ca02016-12-06 14:39:33 +00001300 replayMocks(test, mockRescheduler, shard1, shard2);
1301 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
1302 verifyMocks(test, mockRescheduler, shard1, shard2);
1303 }
1304
jdesprezbc580f92017-06-02 11:41:40 -07001305 /**
1306 * Test that {@link TestInvocation#logDeviceBatteryLevel(IInvocationContext, String)} is not
1307 * adding battery information for placeholder device.
1308 */
1309 public void testLogDeviceBatteryLevel_placeholderDevice() {
1310 final String fakeEvent = "event";
1311 IInvocationContext context = new InvocationContext();
1312 ITestDevice device1 = EasyMock.createMock(ITestDevice.class);
1313 EasyMock.expect(device1.getIDevice()).andReturn(new StubDevice("serial1"));
1314 context.addAllocatedDevice("device1", device1);
1315 EasyMock.replay(device1);
1316 mTestInvocation.logDeviceBatteryLevel(context, fakeEvent);
1317 EasyMock.verify(device1);
1318 assertEquals(0, context.getAttributes().size());
1319 }
1320
1321 /**
1322 * Test that {@link TestInvocation#logDeviceBatteryLevel(IInvocationContext, String)} is adding
1323 * battery information for physical real device.
1324 */
1325 public void testLogDeviceBatteryLevel_physicalDevice() {
1326 final String fakeEvent = "event";
1327 IInvocationContext context = new InvocationContext();
1328 ITestDevice device1 = EasyMock.createMock(ITestDevice.class);
1329 IDevice idevice = Mockito.mock(IDevice.class);
1330 EasyMock.expect(device1.getIDevice()).andReturn(idevice);
1331 SettableFuture<Integer> future = SettableFuture.create();
1332 future.set(50);
1333 doReturn(future).when(idevice).getBattery(Mockito.anyLong(), Mockito.any());
1334 EasyMock.expect(device1.getSerialNumber()).andReturn("serial1");
1335 context.addAllocatedDevice("device1", device1);
jdesprezcf545552017-07-12 02:08:08 -07001336 context.addDeviceBuildInfo("device1", new BuildInfo());
jdesprezbc580f92017-06-02 11:41:40 -07001337 EasyMock.replay(device1);
1338 mTestInvocation.logDeviceBatteryLevel(context, fakeEvent);
1339 EasyMock.verify(device1);
jdesprezcf545552017-07-12 02:08:08 -07001340 assertEquals(1, context.getBuildInfo("device1").getBuildAttributes().size());
1341 assertEquals(
1342 "50",
1343 context.getBuildInfo("device1")
1344 .getBuildAttributes()
1345 .get("serial1-battery-" + fakeEvent));
jdesprezbc580f92017-06-02 11:41:40 -07001346 }
1347
1348 /**
1349 * Test that {@link TestInvocation#logDeviceBatteryLevel(IInvocationContext, String)} is adding
1350 * battery information for multiple physical real device.
1351 */
1352 public void testLogDeviceBatteryLevel_physicalDevice_multi() {
1353 final String fakeEvent = "event";
1354 IInvocationContext context = new InvocationContext();
1355 ITestDevice device1 = EasyMock.createMock(ITestDevice.class);
1356 IDevice idevice = Mockito.mock(IDevice.class);
1357 EasyMock.expect(device1.getSerialNumber()).andReturn("serial1");
1358 EasyMock.expect(device1.getIDevice()).andReturn(idevice);
1359 SettableFuture<Integer> future = SettableFuture.create();
1360 future.set(50);
1361 doReturn(future).when(idevice).getBattery(Mockito.anyLong(), Mockito.any());
1362 ITestDevice device2 = EasyMock.createMock(ITestDevice.class);
1363 EasyMock.expect(device2.getIDevice()).andReturn(idevice);
1364 EasyMock.expect(device2.getSerialNumber()).andReturn("serial2");
1365 context.addAllocatedDevice("device1", device1);
jdesprezcf545552017-07-12 02:08:08 -07001366 context.addDeviceBuildInfo("device1", new BuildInfo());
jdesprezbc580f92017-06-02 11:41:40 -07001367 context.addAllocatedDevice("device2", device2);
jdesprezcf545552017-07-12 02:08:08 -07001368 context.addDeviceBuildInfo("device2", new BuildInfo());
jdesprezbc580f92017-06-02 11:41:40 -07001369 EasyMock.replay(device1, device2);
1370 mTestInvocation.logDeviceBatteryLevel(context, fakeEvent);
1371 EasyMock.verify(device1, device2);
jdesprezcf545552017-07-12 02:08:08 -07001372 assertEquals(1, context.getBuildInfo("device1").getBuildAttributes().size());
1373 assertEquals(1, context.getBuildInfo("device2").getBuildAttributes().size());
1374 assertEquals(
1375 "50",
1376 context.getBuildInfo("device1")
1377 .getBuildAttributes()
1378 .get("serial1-battery-" + fakeEvent));
1379 assertEquals(
1380 "50",
1381 context.getBuildInfo("device2")
1382 .getBuildAttributes()
1383 .get("serial2-battery-" + fakeEvent));
jdesprezbc580f92017-06-02 11:41:40 -07001384 }
1385
1386 /**
1387 * Test that {@link TestInvocation#logDeviceBatteryLevel(IInvocationContext, String)} is adding
1388 * battery information for multiple physical real device, and ignore stub device if any.
1389 */
1390 public void testLogDeviceBatteryLevel_physicalDevice_stub_multi() {
1391 final String fakeEvent = "event";
1392 IInvocationContext context = new InvocationContext();
1393 ITestDevice device1 = EasyMock.createMock(ITestDevice.class);
1394 IDevice idevice = Mockito.mock(IDevice.class);
1395 EasyMock.expect(device1.getSerialNumber()).andReturn("serial1");
1396 EasyMock.expect(device1.getIDevice()).andReturn(idevice);
1397 SettableFuture<Integer> future = SettableFuture.create();
1398 future.set(50);
1399 doReturn(future).when(idevice).getBattery(Mockito.anyLong(), Mockito.any());
1400 ITestDevice device2 = EasyMock.createMock(ITestDevice.class);
1401 EasyMock.expect(device2.getIDevice()).andReturn(idevice);
1402 EasyMock.expect(device2.getSerialNumber()).andReturn("serial2");
1403 ITestDevice device3 = EasyMock.createMock(ITestDevice.class);
1404 EasyMock.expect(device1.getIDevice()).andStubReturn(new StubDevice("stub1"));
1405 ITestDevice device4 = EasyMock.createMock(ITestDevice.class);
1406 EasyMock.expect(device1.getIDevice()).andStubReturn(new StubDevice("stub2"));
1407 context.addAllocatedDevice("device1", device1);
jdesprezcf545552017-07-12 02:08:08 -07001408 context.addDeviceBuildInfo("device1", new BuildInfo());
jdesprezbc580f92017-06-02 11:41:40 -07001409 context.addAllocatedDevice("device2", device2);
jdesprezcf545552017-07-12 02:08:08 -07001410 context.addDeviceBuildInfo("device2", new BuildInfo());
jdesprezbc580f92017-06-02 11:41:40 -07001411 context.addAllocatedDevice("device3", device3);
1412 context.addAllocatedDevice("device4", device4);
1413 EasyMock.replay(device1, device2);
1414 mTestInvocation.logDeviceBatteryLevel(context, fakeEvent);
1415 EasyMock.verify(device1, device2);
jdesprezcf545552017-07-12 02:08:08 -07001416 assertEquals(1, context.getBuildInfo("device1").getBuildAttributes().size());
1417 assertEquals(1, context.getBuildInfo("device2").getBuildAttributes().size());
1418 assertEquals(
1419 "50",
1420 context.getBuildInfo("device1")
1421 .getBuildAttributes()
1422 .get("serial1-battery-" + fakeEvent));
1423 assertEquals(
1424 "50",
1425 context.getBuildInfo("device2")
1426 .getBuildAttributes()
1427 .get("serial2-battery-" + fakeEvent));
jdesprezbc580f92017-06-02 11:41:40 -07001428 }
1429
jdesprez8ba33462017-04-05 16:51:13 -07001430 /** Helper to set the expectation for N number of shards. */
1431 private void setupNShardInvocation(int numShard, String commandLine) throws Exception {
Julien Desprez2763ca02016-12-06 14:39:33 +00001432 mMockBuildInfo.setTestTag(EasyMock.eq("stub"));
1433 EasyMock.expectLastCall();
1434 EasyMock.expect(mMockBuildProvider.getBuild()).andReturn(mMockBuildInfo);
1435 EasyMock.expect(mMockBuildInfo.getTestTag()).andStubReturn("");
jdesprez8ba33462017-04-05 16:51:13 -07001436 mMockBuildInfo.addBuildAttribute("command_line_args", commandLine);
Julien Desprez2763ca02016-12-06 14:39:33 +00001437 mMockLogSaver.invocationStarted((IInvocationContext)EasyMock.anyObject());
1438 EasyMock.expectLastCall();
1439 mMockTestListener.invocationStarted((IInvocationContext)EasyMock.anyObject());
1440 EasyMock.expectLastCall();
1441 mMockSummaryListener.invocationStarted((IInvocationContext)EasyMock.anyObject());
1442 EasyMock.expectLastCall();
1443 EasyMock.expect(mMockLogger.clone()).andReturn(mMockLogger).times(numShard);
1444 EasyMock.expect(mMockBuildInfo.clone()).andReturn(mMockBuildInfo).times(numShard);
1445 EasyMock.expect(mockRescheduler.scheduleConfig(EasyMock.anyObject()))
1446 .andReturn(true).times(numShard);
1447 mMockBuildInfo.setDeviceSerial(SERIAL);
1448 EasyMock.expectLastCall();
1449 mMockBuildProvider.cleanUp(EasyMock.anyObject());
1450 EasyMock.expectLastCall();
1451 }
1452
Eric Rowe0b445112013-07-12 09:29:47 -07001453 private void setupMockSuccessListeners() throws IOException {
Omari Stephens203296c2010-09-02 18:20:45 -07001454 setupMockListeners(InvocationStatus.SUCCESS, null);
1455 }
1456
Eric Rowe0b445112013-07-12 09:29:47 -07001457 private void setupMockFailureListeners(Throwable throwable) throws IOException {
Eric Rowea3b85852012-07-16 15:07:49 -07001458 setupMockListeners(InvocationStatus.FAILED, throwable);
Omari Stephens203296c2010-09-02 18:20:45 -07001459 }
1460
1461 private void verifySummaryListener() {
1462 // Check that we captured the expected uris List
Omari Stephensa01abfe2010-09-07 15:48:44 -07001463 List<TestSummary> summaries = mUriCapture.getValue();
1464 assertEquals(1, summaries.size());
1465 assertEquals(mSummary, summaries.get(0));
Omari Stephens203296c2010-09-02 18:20:45 -07001466 }
1467
1468 /**
Brett Chabot057e16e2010-06-27 21:53:48 -07001469 * Verify all mock objects received expected calls
1470 */
Brett Chabot87803df2013-03-01 16:01:50 -08001471 private void verifyMocks(Object... mocks) {
Brett Chabot1d35e662011-05-25 19:48:24 -07001472 // note: intentionally exclude configuration from verification - don't care
Brett Chabot057e16e2010-06-27 21:53:48 -07001473 // what methods are called
Brett Chabot87803df2013-03-01 16:01:50 -08001474 EasyMock.verify(mMockTestListener, mMockSummaryListener, mMockPreparer,
Eric Rowe0b445112013-07-12 09:29:47 -07001475 mMockBuildProvider, mMockLogger, mMockBuildInfo, mMockLogRegistry,
1476 mMockLogSaver);
Brett Chabot87803df2013-03-01 16:01:50 -08001477 if (mocks.length > 0) {
1478 EasyMock.verify(mocks);
1479 }
Brett Chabot057e16e2010-06-27 21:53:48 -07001480 }
1481
1482 /**
1483 * Switch all mock objects into replay mode.
1484 */
Brett Chabot87803df2013-03-01 16:01:50 -08001485 private void replayMocks(Object... mocks) {
Eric Rowe0b445112013-07-12 09:29:47 -07001486 EasyMock.replay(mMockTestListener, mMockSummaryListener, mMockPreparer,
1487 mMockBuildProvider, mMockLogger, mMockBuildInfo, mMockLogRegistry,
Moon Kimb78076b2016-07-06 15:12:07 -07001488 mMockLogSaver, mMockDevice, mMockConfigFactory);
Brett Chabot87803df2013-03-01 16:01:50 -08001489 if (mocks.length > 0) {
1490 EasyMock.replay(mocks);
1491 }
Brett Chabot74121d82010-01-28 20:14:27 -08001492 }
1493
1494 /**
Brett Chabot74121d82010-01-28 20:14:27 -08001495 * Interface for testing device config pass through.
1496 */
Brett Chabot2e441bd2010-09-16 18:25:11 -07001497 private interface DeviceConfigTest extends IRemoteTest, IDeviceTest {
Brett Chabot74121d82010-01-28 20:14:27 -08001498
1499 }
jdesprezbc580f92017-06-02 11:41:40 -07001500
1501 /**
1502 * Test {@link INativeDevice#preInvocationSetup(IBuildInfo info)} is called when command option
1503 * skip-pre-device-setup is not set.
1504 */
1505 public void testNotSkipPreDeviceSetup() throws Throwable {
1506 IInvocationContext context = new InvocationContext();
1507 ITestDevice device1 = EasyMock.createMock(ITestDevice.class);
1508 IDevice idevice = Mockito.mock(IDevice.class);
1509 context.addAllocatedDevice("DEFAULT_DEVICE", device1);
1510 EasyMock.expect(device1.getSerialNumber()).andReturn("serial1").anyTimes();
1511 EasyMock.expect(device1.getIDevice()).andReturn(idevice).anyTimes();
1512 EasyMock.expect(device1.getLogcat()).andReturn(EMPTY_STREAM_SOURCE).times(1);
1513 device1.clearLogcat();
1514 EasyMock.expectLastCall().once();
1515 device1.preInvocationSetup((IBuildInfo) EasyMock.anyObject());
1516 EasyMock.expectLastCall().once();
1517
1518 CommandOptions commandOption = new CommandOptions();
1519 OptionSetter setter = new OptionSetter(commandOption);
1520 setter.setOptionValue("skip-pre-device-setup", "false");
1521 mStubConfiguration.setCommandOptions(commandOption);
1522
jdesprez5ae744c2017-10-27 10:36:53 -07001523 EasyMock.expect(mMockPreparer.isDisabled()).andReturn(true);
1524
jdesprezbc580f92017-06-02 11:41:40 -07001525 ITestInvocationListener listener = EasyMock.createStrictMock(ITestInvocationListener.class);
1526 listener.testLog(
1527 EasyMock.eq(LOGCAT_NAME_SETUP),
1528 EasyMock.eq(LogDataType.LOGCAT),
1529 (InputStreamSource) EasyMock.anyObject());
1530
jdesprez5ae744c2017-10-27 10:36:53 -07001531 EasyMock.replay(device1, listener, mMockPreparer);
jdesprez94cdfeb2017-09-18 10:49:24 -07001532 new InvocationExecution().doSetup(context, mStubConfiguration, listener);
jdesprez5ae744c2017-10-27 10:36:53 -07001533 EasyMock.verify(device1, listener, mMockPreparer);
jdesprez94cdfeb2017-09-18 10:49:24 -07001534
jdesprezbc580f92017-06-02 11:41:40 -07001535 }
1536
1537 /**
1538 * Test {@link INativeDevice#preInvocationSetup(IBuildInfo info)} is not called when command
1539 * option skip-pre-device-setup is set.
1540 */
1541 public void testSkipPreDeviceSetup() throws Throwable {
1542 IInvocationContext context = new InvocationContext();
1543 ITestDevice device1 = EasyMock.createMock(ITestDevice.class);
1544 IDevice idevice = Mockito.mock(IDevice.class);
1545 context.addAllocatedDevice("DEFAULT_DEVICE", device1);
1546 EasyMock.expect(device1.getSerialNumber()).andReturn("serial1").anyTimes();
1547 EasyMock.expect(device1.getIDevice()).andReturn(idevice).anyTimes();
1548 EasyMock.expect(device1.getLogcat()).andReturn(EMPTY_STREAM_SOURCE).times(1);
1549 device1.clearLogcat();
1550 EasyMock.expectLastCall().once();
1551
1552 CommandOptions commandOption = new CommandOptions();
1553 OptionSetter setter = new OptionSetter(commandOption);
1554 setter.setOptionValue("skip-pre-device-setup", "true");
1555 mStubConfiguration.setCommandOptions(commandOption);
1556
jdesprez5ae744c2017-10-27 10:36:53 -07001557 EasyMock.expect(mMockPreparer.isDisabled()).andReturn(true);
1558
jdesprezbc580f92017-06-02 11:41:40 -07001559 ITestInvocationListener listener = EasyMock.createStrictMock(ITestInvocationListener.class);
1560 listener.testLog(
1561 EasyMock.eq(LOGCAT_NAME_SETUP),
1562 EasyMock.eq(LogDataType.LOGCAT),
1563 (InputStreamSource) EasyMock.anyObject());
1564
jdesprez5ae744c2017-10-27 10:36:53 -07001565 EasyMock.replay(device1, listener, mMockPreparer);
jdesprez94cdfeb2017-09-18 10:49:24 -07001566 new InvocationExecution().doSetup(context, mStubConfiguration, listener);
jdesprez5ae744c2017-10-27 10:36:53 -07001567 EasyMock.verify(device1, listener, mMockPreparer);
jdesprezbc580f92017-06-02 11:41:40 -07001568 }
jdesprezda087c02017-06-26 14:23:07 -07001569
1570 /**
1571 * Test when a {@link IDeviceBuildInfo} is passing through we do not attempt to add any external
1572 * directories when there is none coming from environment.
1573 */
1574 public void testInvoke_deviceInfoBuild_noEnv() throws Throwable {
jdespreza04c03e2017-12-19 05:53:37 -08001575 mTestInvocation =
1576 new TestInvocation() {
1577 @Override
1578 ILogRegistry getLogRegistry() {
1579 return mMockLogRegistry;
1580 }
1581
1582 @Override
1583 public IInvocationExecution createInvocationExec() {
1584 return new InvocationExecution() {
1585 @Override
1586 protected IShardHelper createShardHelper() {
1587 return new ShardHelper();
1588 }
1589
1590 @Override
1591 List<File> getExternalTestCasesDirs() {
1592 // Return empty list to ensure we do not have any environment loaded
1593 return new ArrayList<>();
1594 }
1595 };
1596 }
1597
1598 @Override
1599 protected void setExitCode(ExitCode code, Throwable stack) {
1600 // empty on purpose
1601 }
1602 };
jdesprezda087c02017-06-26 14:23:07 -07001603 mMockBuildInfo = EasyMock.createMock(IDeviceBuildInfo.class);
jdesprez53387682018-02-27 14:10:46 -08001604 EasyMock.expect(mMockBuildInfo.getProperties()).andStubReturn(new HashSet<>());
jdesprezda087c02017-06-26 14:23:07 -07001605 IRemoteTest test = EasyMock.createNiceMock(IRemoteTest.class);
1606 ITargetCleaner mockCleaner = EasyMock.createMock(ITargetCleaner.class);
jdesprez5ae744c2017-10-27 10:36:53 -07001607 EasyMock.expect(mockCleaner.isDisabled()).andReturn(false).times(2);
jdesprezda087c02017-06-26 14:23:07 -07001608 mockCleaner.setUp(mMockDevice, mMockBuildInfo);
1609 mockCleaner.tearDown(mMockDevice, mMockBuildInfo, null);
1610 mStubConfiguration.getTargetPreparers().add(mockCleaner);
1611
1612 File tmpTestsDir = FileUtil.createTempDir("invocation-tests-dir");
1613 try {
1614 EasyMock.expect(((IDeviceBuildInfo) mMockBuildInfo).getTestsDir())
1615 .andReturn(tmpTestsDir);
1616 setupMockSuccessListeners();
1617 setupNormalInvoke(test);
1618 EasyMock.replay(mockCleaner, mockRescheduler);
1619 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
1620 verifyMocks(mockCleaner, mockRescheduler);
1621 verifySummaryListener();
1622 } finally {
1623 FileUtil.recursiveDelete(tmpTestsDir);
1624 }
1625 }
1626
1627 /**
1628 * Test when a {@link IDeviceBuildInfo} is passing through we attempt to add the external
1629 * directories to it when they are available.
1630 */
1631 public void testInvoke_deviceInfoBuild_withEnv() throws Throwable {
1632 File tmpTestsDir = FileUtil.createTempDir("invocation-tests-dir");
1633 File tmpExternalTestsDir = FileUtil.createTempDir("external-tf-dir");
1634 File tmpTestsFile = FileUtil.createTempFile("testsfile", "txt", tmpExternalTestsDir);
1635 try {
1636 mTestInvocation =
1637 new TestInvocation() {
1638 @Override
1639 ILogRegistry getLogRegistry() {
1640 return mMockLogRegistry;
1641 }
1642
1643 @Override
jdesprez85129dd2017-11-13 06:05:57 -08001644 public IInvocationExecution createInvocationExec() {
1645 return new InvocationExecution() {
1646 @Override
1647 protected IShardHelper createShardHelper() {
1648 return new ShardHelper();
1649 }
jdespreza1731b22017-12-06 06:05:52 -08001650
1651 @Override
1652 List<File> getExternalTestCasesDirs() {
1653 List<File> list = new ArrayList<>();
1654 list.add(tmpExternalTestsDir);
1655 return list;
1656 }
jdesprez85129dd2017-11-13 06:05:57 -08001657 };
jdesprezda087c02017-06-26 14:23:07 -07001658 }
1659
1660 @Override
1661 protected void setExitCode(ExitCode code, Throwable stack) {
1662 // empty on purpose
1663 }
jdesprezda087c02017-06-26 14:23:07 -07001664 };
1665 mMockBuildInfo = EasyMock.createMock(IDeviceBuildInfo.class);
1666 IRemoteTest test = EasyMock.createNiceMock(IRemoteTest.class);
1667 ITargetCleaner mockCleaner = EasyMock.createMock(ITargetCleaner.class);
jdesprez5ae744c2017-10-27 10:36:53 -07001668 EasyMock.expect(mockCleaner.isDisabled()).andReturn(false).times(2);
jdesprezda087c02017-06-26 14:23:07 -07001669 mockCleaner.setUp(mMockDevice, mMockBuildInfo);
1670 mockCleaner.tearDown(mMockDevice, mMockBuildInfo, null);
1671 mStubConfiguration.getTargetPreparers().add(mockCleaner);
1672
jdespreza04c03e2017-12-19 05:53:37 -08001673 mMockBuildInfo.setFile(
1674 EasyMock.contains(tmpExternalTestsDir.getName()),
1675 EasyMock.anyObject(),
1676 EasyMock.eq("v1"));
jdesprezda087c02017-06-26 14:23:07 -07001677 EasyMock.expect(((IDeviceBuildInfo) mMockBuildInfo).getTestsDir())
1678 .andReturn(tmpTestsDir);
jdesprez53387682018-02-27 14:10:46 -08001679 EasyMock.expect(mMockBuildInfo.getProperties()).andStubReturn(new HashSet<>());
jdesprezda087c02017-06-26 14:23:07 -07001680
1681 setupMockSuccessListeners();
1682 setupNormalInvoke(test);
1683 EasyMock.replay(mockCleaner, mockRescheduler);
1684 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
1685 verifyMocks(mockCleaner, mockRescheduler);
1686 verifySummaryListener();
1687 // Check that the external directory was copied in the testsDir.
1688 assertTrue(tmpTestsDir.listFiles().length == 1);
jdespreze5093342017-11-16 06:16:06 -08001689 // external-tf-dir - the symlink is the original file name + randomized sequence
1690 assertTrue(
1691 tmpTestsDir.listFiles()[0].getName().startsWith(tmpExternalTestsDir.getName()));
jdesprezda087c02017-06-26 14:23:07 -07001692 // testsfile.txt
1693 assertTrue(tmpTestsDir.listFiles()[0].listFiles().length == 1);
1694 assertEquals(
1695 tmpTestsFile.getName(), tmpTestsDir.listFiles()[0].listFiles()[0].getName());
1696 } finally {
1697 FileUtil.recursiveDelete(tmpTestsDir);
1698 FileUtil.recursiveDelete(tmpExternalTestsDir);
1699 }
1700 }
jdesprez267ab892017-09-08 15:30:06 -07001701
jdesprez53387682018-02-27 14:10:46 -08001702 /**
1703 * Test when a {@link IDeviceBuildInfo} is passing through we do not attempt to add the external
1704 * directories to it, since {@link BuildInfoProperties} is set to skip the linking.
1705 */
1706 public void testInvoke_deviceInfoBuild_withEnv_andSkipProperty() throws Throwable {
1707 File tmpTestsDir = FileUtil.createTempDir("invocation-tests-dir");
1708 File tmpExternalTestsDir = FileUtil.createTempDir("external-tf-dir");
1709 FileUtil.createTempFile("testsfile", "txt", tmpExternalTestsDir);
1710 try {
1711 mTestInvocation =
1712 new TestInvocation() {
1713 @Override
1714 ILogRegistry getLogRegistry() {
1715 return mMockLogRegistry;
1716 }
1717
1718 @Override
1719 public IInvocationExecution createInvocationExec() {
1720 return new InvocationExecution() {
1721 @Override
1722 protected IShardHelper createShardHelper() {
1723 return new ShardHelper();
1724 }
1725
1726 @Override
1727 List<File> getExternalTestCasesDirs() {
1728 List<File> list = new ArrayList<>();
1729 list.add(tmpExternalTestsDir);
1730 return list;
1731 }
1732 };
1733 }
1734
1735 @Override
1736 protected void setExitCode(ExitCode code, Throwable stack) {
1737 // empty on purpose
1738 }
1739 };
1740 mMockBuildInfo = EasyMock.createMock(IDeviceBuildInfo.class);
1741 IRemoteTest test = EasyMock.createNiceMock(IRemoteTest.class);
1742 ITargetCleaner mockCleaner = EasyMock.createMock(ITargetCleaner.class);
1743 EasyMock.expect(mockCleaner.isDisabled()).andReturn(false).times(2);
1744 mockCleaner.setUp(mMockDevice, mMockBuildInfo);
1745 mockCleaner.tearDown(mMockDevice, mMockBuildInfo, null);
1746 mStubConfiguration.getTargetPreparers().add(mockCleaner);
1747
1748 Set<BuildInfoProperties> prop = new HashSet<>();
1749 prop.add(BuildInfoProperties.DO_NOT_LINK_TESTS_DIR);
1750 EasyMock.expect(mMockBuildInfo.getProperties()).andStubReturn(prop);
1751
1752 setupMockSuccessListeners();
1753 setupNormalInvoke(test);
1754 EasyMock.replay(mockCleaner, mockRescheduler);
1755 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
1756 verifyMocks(mockCleaner, mockRescheduler);
1757 verifySummaryListener();
1758 // Check that the external directory was NOT copied in the testsDir.
1759 assertTrue(tmpTestsDir.listFiles().length == 0);
1760 } finally {
1761 FileUtil.recursiveDelete(tmpTestsDir);
1762 FileUtil.recursiveDelete(tmpExternalTestsDir);
1763 }
1764 }
1765
jdesprez839825d2018-01-31 12:14:23 -08001766 public static class TestableCollector extends BaseDeviceMetricCollector {
jdesprez267ab892017-09-08 15:30:06 -07001767
jdesprez839825d2018-01-31 12:14:23 -08001768 @Option(name = "name")
jdesprez267ab892017-09-08 15:30:06 -07001769 private String mName;
1770
jdesprez839825d2018-01-31 12:14:23 -08001771 public TestableCollector() {}
1772
jdesprez267ab892017-09-08 15:30:06 -07001773 public TestableCollector(String name) {
1774 mName = name;
1775 }
1776
1777 @Override
jdesprez71540902017-10-16 12:53:36 -07001778 public void onTestRunEnd(
1779 DeviceMetricData runData, final Map<String, String> currentRunMetrics) {
jdesprez267ab892017-09-08 15:30:06 -07001780 runData.addStringMetric(mName, mName);
1781 }
1782 }
1783
1784 /**
1785 * Test that when {@link IMetricCollector} are used, they wrap and call in sequence the listener
1786 * so all metrics end up on the final receiver.
1787 */
1788 public void testMetricCollectionChain() throws Exception {
1789 IConfiguration configuration = new Configuration("test", "description");
1790 StubTest test = new StubTest();
1791 OptionSetter setter = new OptionSetter(test);
1792 setter.setOptionValue("run-a-test", "true");
1793 configuration.setTest(test);
1794
1795 List<IMetricCollector> collectors = new ArrayList<>();
1796 collectors.add(new TestableCollector("collector1"));
1797 collectors.add(new TestableCollector("collector2"));
1798 collectors.add(new TestableCollector("collector3"));
1799 collectors.add(new TestableCollector("collector4"));
1800 configuration.setDeviceMetricCollectors(collectors);
1801
1802 mMockTestListener.testRunStarted("TestStub", 1);
jdesprez821a0052018-01-16 03:37:16 -08001803 TestDescription testId = new TestDescription("StubTest", "StubMethod");
jdesprez267ab892017-09-08 15:30:06 -07001804 mMockTestListener.testStarted(EasyMock.eq(testId), EasyMock.anyLong());
1805 mMockTestListener.testEnded(
1806 EasyMock.eq(testId), EasyMock.anyLong(), EasyMock.eq(Collections.emptyMap()));
1807 Capture<Map<String, String>> captured = new Capture<>();
1808 mMockTestListener.testRunEnded(EasyMock.anyLong(), EasyMock.capture(captured));
1809 EasyMock.replay(mMockTestListener);
jdesprez94cdfeb2017-09-18 10:49:24 -07001810 new InvocationExecution()
1811 .runTests(mStubInvocationMetadata, configuration, mMockTestListener);
jdesprez267ab892017-09-08 15:30:06 -07001812 EasyMock.verify(mMockTestListener);
1813 // The collectors are called in sequence
1814 List<String> listKeys = new ArrayList<>(captured.getValue().keySet());
1815 assertEquals("collector4", listKeys.get(0));
1816 assertEquals("collector3", listKeys.get(1));
1817 assertEquals("collector2", listKeys.get(2));
1818 assertEquals("collector1", listKeys.get(3));
1819 }
Brett Chabot74121d82010-01-28 20:14:27 -08001820}