Home | History | Annotate | Download | only in targetprep
      1 /*
      2  * Copyright (C) 2016 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.targetprep;
     17 
     18 import com.android.ddmlib.CollectingOutputReceiver;
     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.DeviceNotAvailableException;
     23 import com.android.tradefed.device.ITestDevice;
     24 import com.android.tradefed.log.LogUtil.CLog;
     25 
     26 import java.util.concurrent.TimeUnit;
     27 
     28 /**
     29  * A {@link ITargetPreparer} that pushes files from tests zip onto device, mark them as executable
     30  * and invokes the binary or script on device. See also {@link TestFilePushSetup}.
     31  */
     32 @OptionClass(alias = "push-file-invoker")
     33 public class PushFileInvoker extends TestFilePushSetup {
     34 
     35     @Option(name = "script-timeout",
     36             description = "Timeout for script execution, can be any valid time string",
     37             isTimeVal = true)
     38     private long mScriptTimeout = 2 * 60 * 1000;
     39 
     40     @Option(name = "execute-as-root",
     41             description = "Execute the pushed files with root identity. Note that this requires "
     42                     + "'su' on device, typically only available on debug builds.")
     43     private boolean mExecuteAsRoot = true;
     44 
     45     /**
     46      * {@inheritDoc}
     47      */
     48     @Override
     49     public void setUp(ITestDevice device, IBuildInfo buildInfo)
     50             throws TargetSetupError, BuildError, DeviceNotAvailableException {
     51         if (isDisabled()) {
     52             CLog.i("Performance setup script disabled.");
     53             return;
     54         }
     55         // call super to push files first
     56         super.setUp(device, buildInfo);
     57         for (String file : getTestFileNames()) {
     58             String devicePath = String.format("/data/%s", file);
     59             if (!device.doesFileExist(devicePath)) {
     60                 CLog.w("Ignoring non-existent path %s", devicePath);
     61                 continue;
     62             }
     63             if (device.isDirectory(devicePath)) {
     64                 CLog.w("%s is a directory, skipping", devicePath);
     65                 continue;
     66             }
     67             // first ensure that file is executable
     68             device.executeShellCommand(String.format("chmod 755 %s", devicePath));
     69             // next decide if we need `su`
     70             if (mExecuteAsRoot) {
     71                 devicePath = String.format("su root %s", devicePath);
     72             }
     73             CollectingOutputReceiver receiver = new CollectingOutputReceiver();
     74             device.executeShellCommand(devicePath, receiver,
     75                     mScriptTimeout, TimeUnit.MILLISECONDS, 0);
     76             CLog.d("Executed perf setup script\noutput:\n%s\nEND", receiver.getOutput());
     77             device.waitForDeviceAvailable();
     78         }
     79     }
     80 }
     81