jdesprez | 481a83f | 2018-03-14 16:20:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package com.android.tradefed.invoker; |
| 17 | |
jdesprez | 7509fca | 2018-04-02 16:36:56 -0700 | [diff] [blame^] | 18 | import static org.mockito.Mockito.any; |
| 19 | import static org.mockito.Mockito.doReturn; |
jdesprez | 481a83f | 2018-03-14 16:20:39 -0700 | [diff] [blame] | 20 | import static org.mockito.Mockito.times; |
| 21 | |
| 22 | import com.android.tradefed.build.IBuildProvider; |
| 23 | import com.android.tradefed.command.CommandRunner.ExitCode; |
| 24 | import com.android.tradefed.config.Configuration; |
| 25 | import com.android.tradefed.config.ConfigurationDescriptor; |
| 26 | import com.android.tradefed.config.GlobalConfiguration; |
| 27 | import com.android.tradefed.config.IConfiguration; |
jdesprez | 7509fca | 2018-04-02 16:36:56 -0700 | [diff] [blame^] | 28 | import com.android.tradefed.guice.InvocationScope; |
jdesprez | 481a83f | 2018-03-14 16:20:39 -0700 | [diff] [blame] | 29 | import com.android.tradefed.invoker.sandbox.SandboxedInvocationExecution; |
| 30 | import com.android.tradefed.log.ILogRegistry; |
| 31 | import com.android.tradefed.result.ILogSaver; |
| 32 | import com.android.tradefed.result.ITestInvocationListener; |
jdesprez | 7509fca | 2018-04-02 16:36:56 -0700 | [diff] [blame^] | 33 | import com.android.tradefed.result.LogDataType; |
| 34 | import com.android.tradefed.result.LogFile; |
jdesprez | 481a83f | 2018-03-14 16:20:39 -0700 | [diff] [blame] | 35 | |
| 36 | import org.junit.Before; |
| 37 | import org.junit.Test; |
| 38 | import org.junit.runner.RunWith; |
| 39 | import org.junit.runners.JUnit4; |
| 40 | import org.mockito.Mock; |
| 41 | import org.mockito.Mockito; |
| 42 | import org.mockito.MockitoAnnotations; |
| 43 | |
| 44 | /** Unit tests for {@link SandboxedInvocationExecution}. */ |
| 45 | @RunWith(JUnit4.class) |
| 46 | public class SandboxedInvocationExecutionTest { |
| 47 | |
| 48 | private TestInvocation mInvocation; |
| 49 | @Mock ILogRegistry mMockLogRegistry; |
| 50 | @Mock IRescheduler mMockRescheduler; |
| 51 | @Mock ITestInvocationListener mMockListener; |
| 52 | @Mock ILogSaver mMockLogSaver; |
| 53 | @Mock IBuildProvider mMockProvider; |
| 54 | |
| 55 | private IConfiguration mConfig; |
| 56 | private IInvocationContext mContext; |
| 57 | |
| 58 | @Before |
| 59 | public void setUp() throws Exception { |
| 60 | MockitoAnnotations.initMocks(this); |
| 61 | |
| 62 | try { |
| 63 | GlobalConfiguration.createGlobalConfiguration(new String[] {"empty"}); |
| 64 | } catch (IllegalStateException e) { |
| 65 | // Avoid double init issues. |
| 66 | } |
| 67 | |
| 68 | mInvocation = |
| 69 | new TestInvocation() { |
| 70 | @Override |
| 71 | ILogRegistry getLogRegistry() { |
| 72 | return mMockLogRegistry; |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | protected void setExitCode(ExitCode code, Throwable stack) { |
| 77 | // empty on purpose |
| 78 | } |
jdesprez | 7509fca | 2018-04-02 16:36:56 -0700 | [diff] [blame^] | 79 | |
| 80 | @Override |
| 81 | InvocationScope getInvocationScope() { |
| 82 | // Avoid re-entry in the current TF invocation scope for unit tests. |
| 83 | return new InvocationScope(); |
| 84 | } |
jdesprez | 481a83f | 2018-03-14 16:20:39 -0700 | [diff] [blame] | 85 | }; |
| 86 | mConfig = new Configuration("test", "test"); |
| 87 | mContext = new InvocationContext(); |
| 88 | } |
| 89 | |
| 90 | /** Basic test to go through the flow of a sandbox invocation. */ |
| 91 | @Test |
| 92 | public void testSandboxInvocation() throws Throwable { |
| 93 | // Setup as a sandbox invocation |
| 94 | ConfigurationDescriptor descriptor = new ConfigurationDescriptor(); |
| 95 | descriptor.setSandboxed(true); |
| 96 | mConfig.setConfigurationObject( |
| 97 | Configuration.CONFIGURATION_DESCRIPTION_TYPE_NAME, descriptor); |
| 98 | mConfig.setLogSaver(mMockLogSaver); |
| 99 | mConfig.setBuildProvider(mMockProvider); |
| 100 | |
jdesprez | 7509fca | 2018-04-02 16:36:56 -0700 | [diff] [blame^] | 101 | doReturn(new LogFile("file", "url", LogDataType.TEXT)) |
| 102 | .when(mMockLogSaver) |
| 103 | .saveLogData(any(), any(), any()); |
| 104 | |
jdesprez | 481a83f | 2018-03-14 16:20:39 -0700 | [diff] [blame] | 105 | mInvocation.invoke(mContext, mConfig, mMockRescheduler, mMockListener); |
| 106 | |
| 107 | // Ensure that in sandbox we don't download again. |
| 108 | Mockito.verify(mMockProvider, times(0)).getBuild(); |
| 109 | } |
| 110 | } |