1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.tradefed.sandbox; 17 18 import com.android.tradefed.config.Configuration; 19 import com.android.tradefed.config.IConfiguration; 20 import com.android.tradefed.invoker.TestInformation; 21 import com.android.tradefed.invoker.logger.InvocationMetricLogger; 22 import com.android.tradefed.invoker.logger.InvocationMetricLogger.InvocationMetricKey; 23 import com.android.tradefed.log.LogUtil.CLog; 24 import com.android.tradefed.result.ITestInvocationListener; 25 import com.android.tradefed.util.CommandResult; 26 import com.android.tradefed.util.CommandStatus; 27 import com.android.tradefed.util.PrettyPrintDelimiter; 28 29 /** Run the tests associated with the invocation in the sandbox. */ 30 public class SandboxInvocationRunner { 31 32 /** 33 * Do setup and run the tests. 34 * 35 * @return True if the invocation is successful. False otherwise. 36 */ prepareAndRun( TestInformation info, IConfiguration config, ITestInvocationListener listener)37 public static boolean prepareAndRun( 38 TestInformation info, IConfiguration config, ITestInvocationListener listener) 39 throws Throwable { 40 prepareSandbox(info, config, listener); 41 return runSandbox(info, config, listener); 42 } 43 teardownSandbox(IConfiguration config)44 public static void teardownSandbox(IConfiguration config) { 45 ISandbox sandbox = 46 (ISandbox) config.getConfigurationObject(Configuration.SANDBOX_TYPE_NAME); 47 if (sandbox == null) { 48 throw new RuntimeException("Couldn't find the sandbox object."); 49 } 50 sandbox.tearDown(); 51 } 52 53 /** Preparation step of the sandbox */ prepareSandbox( TestInformation info, IConfiguration config, ITestInvocationListener listener)54 public static void prepareSandbox( 55 TestInformation info, IConfiguration config, ITestInvocationListener listener) 56 throws Throwable { 57 ISandbox sandbox = 58 (ISandbox) config.getConfigurationObject(Configuration.SANDBOX_TYPE_NAME); 59 if (sandbox == null) { 60 throw new RuntimeException("Couldn't find the sandbox object."); 61 } 62 PrettyPrintDelimiter.printStageDelimiter("Starting Sandbox Environment Setup"); 63 Exception res = null; 64 try { 65 res = sandbox.prepareEnvironment(info.getContext(), config, listener); 66 } catch (RuntimeException e) { 67 sandbox.tearDown(); 68 throw e; 69 } 70 if (res != null) { 71 CLog.w("Sandbox prepareEnvironment threw an Exception."); 72 sandbox.tearDown(); 73 throw res; 74 } 75 PrettyPrintDelimiter.printStageDelimiter("Done with Sandbox Environment Setup"); 76 } 77 78 /** Execution step of the sandbox */ runSandbox( TestInformation info, IConfiguration config, ITestInvocationListener listener)79 public static boolean runSandbox( 80 TestInformation info, IConfiguration config, ITestInvocationListener listener) 81 throws Throwable { 82 ISandbox sandbox = 83 (ISandbox) config.getConfigurationObject(Configuration.SANDBOX_TYPE_NAME); 84 if (sandbox == null) { 85 throw new RuntimeException("Couldn't find the sandbox object."); 86 } 87 long start = System.currentTimeMillis(); 88 try { 89 CommandResult result = sandbox.run(info, config, listener); 90 return CommandStatus.SUCCESS.equals(result.getStatus()); 91 } finally { 92 sandbox.tearDown(); 93 // Only log if it was no already logged to keep the value closest to execution 94 if (!InvocationMetricLogger.getInvocationMetrics() 95 .containsKey(InvocationMetricKey.TEST_PAIR.toString())) { 96 InvocationMetricLogger.addInvocationPairMetrics( 97 InvocationMetricKey.TEST_PAIR, start, System.currentTimeMillis()); 98 } 99 } 100 } 101 } 102