1 /* 2 * Copyright (C) 2012 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 17 package com.android.tradefed.targetprep; 18 19 import com.android.tradefed.build.IBuildInfo; 20 import com.android.tradefed.config.Option; 21 import com.android.tradefed.config.OptionClass; 22 import com.android.tradefed.device.BackgroundDeviceAction; 23 import com.android.tradefed.device.CollectingOutputReceiver; 24 import com.android.tradefed.device.DeviceNotAvailableException; 25 import com.android.tradefed.device.ITestDevice; 26 import com.android.tradefed.log.LogUtil.CLog; 27 import com.android.tradefed.util.RunUtil; 28 29 import java.util.ArrayList; 30 import java.util.HashMap; 31 import java.util.List; 32 import java.util.Map; 33 import java.util.concurrent.TimeUnit; 34 35 @OptionClass(alias = "run-command") 36 public class RunCommandTargetPreparer implements ITargetCleaner { 37 38 @Option(name = "run-command", description = "adb shell command to run") 39 private List<String> mCommands = new ArrayList<String>(); 40 41 @Option(name = "run-bg-command", description = "Command to run repeatedly in the" 42 + " device background. Can be repeated to run multiple commands" 43 + " in the background.") 44 private List<String> mBgCommands = new ArrayList<String>(); 45 46 @Option(name = "hide-bg-output", description = "if true, don't log background command output") 47 private boolean mHideBgOutput = false; 48 49 @Option(name = "teardown-command", description = "adb shell command to run at teardown time") 50 private List<String> mTeardownCommands = new ArrayList<String>(); 51 52 @Option(name = "disable", description = "Disable this preparer") 53 private boolean mDisable = false; 54 55 @Option(name = "delay-after-commands", 56 description = "Time to delay after running commands, in msecs") 57 private long mDelayMsecs = 0; 58 59 @Option(name = "run-command-timeout", 60 description = "Timeout for execute shell command", 61 isTimeVal = true) 62 private long mRunCmdTimeout = 0; 63 64 private Map<BackgroundDeviceAction, CollectingOutputReceiver> mBgDeviceActionsMap = 65 new HashMap<>(); 66 67 /** 68 * {@inheritDoc} 69 */ 70 @Override 71 public void setUp(ITestDevice device, IBuildInfo buildInfo) throws TargetSetupError, 72 DeviceNotAvailableException { 73 if (mDisable) 74 return; 75 76 for (String bgCmd : mBgCommands) { 77 CollectingOutputReceiver receiver = new CollectingOutputReceiver(); 78 BackgroundDeviceAction mBgDeviceAction = 79 new BackgroundDeviceAction(bgCmd, bgCmd, device, receiver, 0); 80 mBgDeviceAction.start(); 81 mBgDeviceActionsMap.put(mBgDeviceAction, receiver); 82 } 83 84 for (String cmd : mCommands) { 85 CLog.d("About to run setup command on device %s: %s", device.getSerialNumber(), cmd); 86 if (mRunCmdTimeout > 0) { 87 CollectingOutputReceiver receiver = new CollectingOutputReceiver(); 88 device.executeShellCommand(cmd, receiver, mRunCmdTimeout, TimeUnit.MILLISECONDS, 0); 89 CLog.v("cmd: '%s', returned:\n%s", cmd, receiver.getOutput()); 90 } else { 91 String output = device.executeShellCommand(cmd); 92 CLog.v("cmd: '%s', returned:\n%s", cmd, output); 93 } 94 } 95 96 CLog.d("Sleeping %d msecs on device %s", mDelayMsecs, device.getSerialNumber()); 97 RunUtil.getDefault().sleep(mDelayMsecs); 98 } 99 100 /** 101 * {@inheritDoc} 102 */ 103 @Override 104 public void tearDown(ITestDevice device, IBuildInfo buildInfo, Throwable e) 105 throws DeviceNotAvailableException { 106 if (mDisable) 107 return; 108 109 for (Map.Entry<BackgroundDeviceAction, CollectingOutputReceiver> bgAction : 110 mBgDeviceActionsMap.entrySet()) { 111 if (!mHideBgOutput) { 112 CLog.d("Background command output : %s", bgAction.getValue().getOutput()); 113 } 114 bgAction.getKey().cancel(); 115 } 116 117 for (String cmd : mTeardownCommands) { 118 CLog.d("About to run tearDown command on device %s: %s", device.getSerialNumber(), 119 cmd); 120 String output = device.executeShellCommand(cmd); 121 CLog.v("tearDown cmd: '%s', returned:\n%s", cmd, output); 122 } 123 124 } 125 } 126 127