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.sandbox; 17 18 import com.android.tradefed.build.BuildRetrievalError; 19 import com.android.tradefed.build.IBuildInfo; 20 import com.android.tradefed.build.IBuildProvider; 21 import com.android.tradefed.config.IConfiguration; 22 import com.android.tradefed.config.IDeviceConfiguration; 23 import com.android.tradefed.device.DeviceNotAvailableException; 24 import com.android.tradefed.invoker.IInvocationContext; 25 import com.android.tradefed.invoker.IRescheduler; 26 import com.android.tradefed.invoker.InvocationExecution; 27 import com.android.tradefed.log.LogUtil.CLog; 28 import com.android.tradefed.result.ITestInvocationListener; 29 import com.android.tradefed.testtype.IInvocationContextReceiver; 30 31 /** 32 * Special sandbox execution of the invocation: This is the InvocationExection for when we are 33 * inside the sandbox running the command. The build should already be available in the context. 34 */ 35 public class SandboxedInvocationExecution extends InvocationExecution { 36 37 /** {@inheritDoc} */ 38 @Override fetchBuild( IInvocationContext context, IConfiguration config, IRescheduler rescheduler, ITestInvocationListener listener)39 public boolean fetchBuild( 40 IInvocationContext context, 41 IConfiguration config, 42 IRescheduler rescheduler, 43 ITestInvocationListener listener) 44 throws DeviceNotAvailableException, BuildRetrievalError { 45 // If the invocation is currently sandboxed, builds have already been downloaded. 46 CLog.d("Skipping download in the sandbox."); 47 if (!config.getConfigurationDescription().shouldUseSandbox()) { 48 throw new RuntimeException( 49 "We should only skip download if we are a sandbox. Something went very wrong."); 50 } 51 // Even if we don't call them directly here, ensure they receive their dependencies for the 52 // buildNotTested callback. 53 for (String deviceName : context.getDeviceConfigNames()) { 54 IDeviceConfiguration deviceConfig = config.getDeviceConfigByName(deviceName); 55 IBuildProvider provider = deviceConfig.getBuildProvider(); 56 // Inject the context to the provider if it can receive it 57 if (provider instanceof IInvocationContextReceiver) { 58 ((IInvocationContextReceiver) provider).setInvocationContext(context); 59 } 60 } 61 62 // Still set the test-tag on build infos for proper reporting 63 for (IBuildInfo info : context.getBuildInfos()) { 64 setTestTag(info, config); 65 } 66 return true; 67 } 68 69 /** {@inheritDoc} */ 70 @Override resetBuildAndReschedule( Throwable exception, ITestInvocationListener listener, IConfiguration config, IInvocationContext context)71 public boolean resetBuildAndReschedule( 72 Throwable exception, 73 ITestInvocationListener listener, 74 IConfiguration config, 75 IInvocationContext context) { 76 if (!config.getConfigurationDescription().shouldUseSandbox()) { 77 throw new RuntimeException( 78 "We should only skip resetAndReschedule if we are a sandbox. " 79 + "Something went very wrong."); 80 } 81 // If we are sandboxed, build reset and reschedule should happen on the parents. 82 return false; 83 } 84 } 85