aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--tools/tradefed-host/src/com/android/afwtest/tradefed/targetprep/AfwTestRunHostCommandTargetPreparer.java117
1 files changed, 117 insertions, 0 deletions
diff --git a/tools/tradefed-host/src/com/android/afwtest/tradefed/targetprep/AfwTestRunHostCommandTargetPreparer.java b/tools/tradefed-host/src/com/android/afwtest/tradefed/targetprep/AfwTestRunHostCommandTargetPreparer.java
new file mode 100644
index 0000000..161ec28
--- /dev/null
+++ b/tools/tradefed-host/src/com/android/afwtest/tradefed/targetprep/AfwTestRunHostCommandTargetPreparer.java
@@ -0,0 +1,117 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.afwtest.tradefed.targetprep;
+
+import com.android.tradefed.build.IBuildInfo;
+import com.android.tradefed.config.Option;
+import com.android.tradefed.config.OptionClass;
+import com.android.tradefed.device.DeviceNotAvailableException;
+import com.android.tradefed.device.ITestDevice;
+import com.android.tradefed.log.LogUtil.CLog;
+import com.android.tradefed.targetprep.BuildError;
+import com.android.tradefed.targetprep.ITargetCleaner;
+import com.android.tradefed.targetprep.TargetSetupError;
+import com.android.tradefed.util.CommandResult;
+import com.android.tradefed.util.IRunUtil;
+import com.android.tradefed.util.RunUtil;
+
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ * Target preparer to run arbitrary host commands before and after running the test.
+ */
+@OptionClass(alias = "run-host-command")
+public class AfwTestRunHostCommandTargetPreparer implements ITargetCleaner {
+
+ @Option(
+ name = "host-setup-command",
+ description = "Command to be run before the test. Can be repeated."
+ )
+ private List<String> mSetUpCommands = new LinkedList<>();
+
+ @Option(
+ name = "host-teardown-command",
+ description = "Command to be run after the test. Can be repeated."
+ )
+ private List<String> mTearDownCommands = new LinkedList<>();
+
+ @Option(
+ name = "host-cmd-device-placeholder",
+ description = "Placeholder in commands to be substituted by device serial."
+ )
+ private String mDevicePlaceholder = "%DEVICE%";
+
+ @Option(
+ name = "host-cmd-timeout",
+ isTimeVal = true,
+ description = "Timeout for each command specified."
+ )
+ private long mTimeout = 60000L;
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void setUp(ITestDevice iTestDevice, IBuildInfo iBuildInfo)
+ throws TargetSetupError, BuildError, DeviceNotAvailableException {
+ runCommandList(mSetUpCommands, iTestDevice.getSerialNumber());
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void tearDown(ITestDevice iTestDevice, IBuildInfo iBuildInfo, Throwable throwable)
+ throws DeviceNotAvailableException {
+ runCommandList(mTearDownCommands, iTestDevice.getSerialNumber());
+ }
+
+ /**
+ * Sequentially runs commands from specified list.
+ *
+ * @param commands list of commands to run.
+ * @param serial serial of the device.
+ */
+ private void runCommandList(final List<String> commands, final String serial) {
+ final IRunUtil runUtil = RunUtil.getDefault();
+ for (final String command : commands) {
+ final CommandResult result =
+ runUtil.runTimedCmd(
+ mTimeout,
+ command.replace(mDevicePlaceholder, serial).split("\\s+"));
+ switch (result.getStatus()) {
+ case SUCCESS:
+ CLog.i(
+ "Command %s finished successfully, stdout = [%s].",
+ command, result.getStdout());
+ break;
+ case FAILED:
+ CLog.e(
+ "Command %s failed, stdout = [%s], stderr = [%s].",
+ command, result.getStdout(), result.getStderr());
+ break;
+ case TIMED_OUT:
+ CLog.e("Command %s timed out.", command);
+ break;
+ case EXCEPTION:
+ CLog.e("Exception occurred when running command %s.", command);
+ break;
+ }
+ }
+ }
+}