blob: c094f57c5f3449b5fe99b60bb7c5747b0ba0d48a [file] [log] [blame]
jdesprez481a83f2018-03-14 16:20:39 -07001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.tradefed.invoker;
17
jdesprez7509fca2018-04-02 16:36:56 -070018import static org.mockito.Mockito.any;
19import static org.mockito.Mockito.doReturn;
jdesprez481a83f2018-03-14 16:20:39 -070020import static org.mockito.Mockito.times;
21
22import com.android.tradefed.build.IBuildProvider;
23import com.android.tradefed.command.CommandRunner.ExitCode;
24import com.android.tradefed.config.Configuration;
25import com.android.tradefed.config.ConfigurationDescriptor;
26import com.android.tradefed.config.GlobalConfiguration;
27import com.android.tradefed.config.IConfiguration;
jdesprez7509fca2018-04-02 16:36:56 -070028import com.android.tradefed.guice.InvocationScope;
jdesprez481a83f2018-03-14 16:20:39 -070029import com.android.tradefed.invoker.sandbox.SandboxedInvocationExecution;
30import com.android.tradefed.log.ILogRegistry;
31import com.android.tradefed.result.ILogSaver;
32import com.android.tradefed.result.ITestInvocationListener;
jdesprez7509fca2018-04-02 16:36:56 -070033import com.android.tradefed.result.LogDataType;
34import com.android.tradefed.result.LogFile;
jdesprez481a83f2018-03-14 16:20:39 -070035
36import org.junit.Before;
37import org.junit.Test;
38import org.junit.runner.RunWith;
39import org.junit.runners.JUnit4;
40import org.mockito.Mock;
41import org.mockito.Mockito;
42import org.mockito.MockitoAnnotations;
43
44/** Unit tests for {@link SandboxedInvocationExecution}. */
45@RunWith(JUnit4.class)
46public 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 }
jdesprez7509fca2018-04-02 16:36:56 -070079
80 @Override
81 InvocationScope getInvocationScope() {
82 // Avoid re-entry in the current TF invocation scope for unit tests.
83 return new InvocationScope();
84 }
jdesprez481a83f2018-03-14 16:20:39 -070085 };
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
jdesprez7509fca2018-04-02 16:36:56 -0700101 doReturn(new LogFile("file", "url", LogDataType.TEXT))
102 .when(mMockLogSaver)
103 .saveLogData(any(), any(), any());
104
jdesprez481a83f2018-03-14 16:20:39 -0700105 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}