Home | History | Annotate | Download | only in targetprep
      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 
     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.DeviceNotAvailableException;
     23 import com.android.tradefed.device.ITestDevice;
     24 import com.android.tradefed.log.LogUtil.CLog;
     25 import com.android.tradefed.util.CommandResult;
     26 import com.android.tradefed.util.IRunUtil;
     27 import com.android.tradefed.util.RunUtil;
     28 
     29 import com.google.common.annotations.VisibleForTesting;
     30 
     31 import java.util.LinkedList;
     32 import java.util.List;
     33 
     34 /** Target preparer to run arbitrary host commands before and after running the test. */
     35 @OptionClass(alias = "run-host-command")
     36 public class RunHostCommandTargetPreparer implements ITargetCleaner {
     37 
     38     @Option(
     39         name = "host-setup-command",
     40         description = "Command to be run before the test. Can be repeated."
     41     )
     42     private List<String> mSetUpCommands = new LinkedList<>();
     43 
     44     @Option(
     45         name = "host-teardown-command",
     46         description = "Command to be run after the test. Can be repeated."
     47     )
     48     private List<String> mTearDownCommands = new LinkedList<>();
     49 
     50     @Option(
     51         name = "host-cmd-timeout",
     52         isTimeVal = true,
     53         description = "Timeout for each command specified."
     54     )
     55     private long mTimeout = 60000L;
     56 
     57     /** {@inheritDoc} */
     58     @Override
     59     public void setUp(ITestDevice device, IBuildInfo buildInfo)
     60             throws TargetSetupError, BuildError, DeviceNotAvailableException {
     61         runCommandList(mSetUpCommands);
     62     }
     63 
     64     /** {@inheritDoc} */
     65     @Override
     66     public void tearDown(ITestDevice device, IBuildInfo buildInfo, Throwable e)
     67             throws DeviceNotAvailableException {
     68         runCommandList(mTearDownCommands);
     69     }
     70 
     71     /**
     72      * Sequentially runs command from specified list.
     73      *
     74      * @param commands list of commands to run.
     75      */
     76     private void runCommandList(final List<String> commands) {
     77         for (final String command : commands) {
     78             final CommandResult result = getRunUtil().runTimedCmd(mTimeout, command.split("\\s+"));
     79             switch (result.getStatus()) {
     80                 case SUCCESS:
     81                     CLog.i(
     82                             "Command %s finished successfully, stdout = [%s].",
     83                             command, result.getStdout());
     84                     break;
     85                 case FAILED:
     86                     CLog.e(
     87                             "Command %s failed, stdout = [%s], stderr = [%s].",
     88                             command, result.getStdout(), result.getStderr());
     89                     break;
     90                 case TIMED_OUT:
     91                     CLog.e("Command %s timed out.", command);
     92                     break;
     93                 case EXCEPTION:
     94                     CLog.e("Exception occurred when running command %s.", command);
     95                     break;
     96             }
     97         }
     98     }
     99 
    100     /**
    101      * Gets instance of {@link IRunUtil}.
    102      *
    103      * @return instance of {@link IRunUtil}.
    104      */
    105     @VisibleForTesting
    106     IRunUtil getRunUtil() {
    107         return RunUtil.getDefault();
    108     }
    109 }
    110