Add new --servo option to run_remote_tests.sh

When the new option is used, the script will start servod
automatically at the start of testing, and stop it when the
tests are complete.

CQ-DEPEND=I93372498dc494309a134ad2eddf14f542b95e4a6

BUG=chromium-os:30961
TEST=platform_ServoPyAuto

Change-Id: I0e6d64e004ea603fa71e6aa6dd93c12aa8b222a1
Reviewed-on: https://gerrit.chromium.org/gerrit/23566
Tested-by: Richard Barnette <jrbarnette@chromium.org>
Reviewed-by: Todd Broch <tbroch@chromium.org>
Reviewed-by: Chris Sosa <sosa@chromium.org>
Commit-Ready: Richard Barnette <jrbarnette@chromium.org>
diff --git a/run_remote_tests.sh b/run_remote_tests.sh
index 2758576..59e941d 100755
--- a/run_remote_tests.sh
+++ b/run_remote_tests.sh
@@ -9,12 +9,16 @@
 . "${CROSUTILS}/common.sh" || exit 1
 . "${CROSUTILS}/remote_access.sh" || die "Unable to load remote_access.sh"
 
+get_default_board
+
 DEFINE_string args "" \
     "Command line arguments for test. Quoted and space separated if multiple." a
 DEFINE_string autotest_dir "" \
     "Skip autodetection of autotest and use the specified location (must be in \
 chroot)."
-DEFINE_string board "" \
+DEFINE_boolean servo ${FLAGS_FALSE} \
+    "Run servod for a locally attached servo board while testing."
+DEFINE_string board "${DEFAULT_BOARD}" \
     "The board for which you are building autotest"
 DEFINE_boolean build ${FLAGS_FALSE} "Build tests while running" b
 DEFINE_boolean cleanup ${FLAGS_FALSE} "Clean up temp directory"
@@ -82,6 +86,7 @@
   fi
   stop_ssh_agent
   cleanup_remote_access
+  stop_servod
 }
 
 # Determine if a control is for a client or server test.  Echos
@@ -280,6 +285,44 @@
   fi
 }
 
+# If the user requested it, start a 'servod' process in the
+# background in order to serve Servo-based tests.  Wait until
+# the process is up and serving, or die trying.
+start_servod() {
+  if [ ${FLAGS_servo} -eq ${FLAGS_FALSE} ]; then
+    return
+  fi
+
+  sudo servod --board=${FLAGS_board} >${TMP}/servod.log 2>&1 &
+  SERVOD=$!
+  echo
+  info "Started servod; pid = ${SERVOD}."
+  info "For the log, see ${TMP}/servod.log"
+  local timeout=10
+  while [ ${timeout} -gt 0 ]; do
+    if dut-control >/dev/null 2>&1; then
+      return
+    fi
+    timeout=$(( timeout - 1 ))
+    sleep 1
+  done
+  error "'servod' not working after 10 seconds of trying.  Log file:"
+  cat ${TMP}/servod.log
+  die_notrace "Giving up."
+}
+
+# If there's a servod running in the background from `start_servod`,
+# terminate it.
+stop_servod() {
+  if [ -n "$SERVOD" ]; then
+    # The 'if kill -0 ...' allows us to ignore an already dead
+    # process, but still report other errors on stderr.
+    if kill -0 $SERVOD 2>/dev/null; then
+      kill $SERVOD || true
+    fi
+    SERVOD=
+  fi
+}
 
 main() {
   cd "${SCRIPTS_DIR}"
@@ -291,6 +334,12 @@
     exit 1
   fi
 
+  if [ ${FLAGS_servo} -eq ${FLAGS_TRUE} ]; then
+    if [ -z "${FLAGS_board}" ]; then
+      die "Must specify board when using --servo"
+    fi
+  fi
+
   # Check the validity of the user-specified result directory
   # It must be within the /tmp directory
   if [[ -n "${FLAGS_results_dir_root}" ]]; then
@@ -493,6 +542,8 @@
 
     sudo chmod a+w ./server/{tests,site_tests}
 
+    start_servod
+
     # --args must be specified as a separate parameter outside of the local
     # autoserv_args variable, otherwise ${FLAGS_args} values with embedded
     # spaces won't pass correctly to autoserv.