cros_run_bvt: Tweak for labqual.

Dangerous tweak to address a gap in labqual.

BUG=b:269503867
TEST=confirmed that "./cros_run_bvt --board=atlas --labqual --debug \
127.0.0.1" generates an error (no servo type specificed),

confirmed that "./cros_run_bvt --board=atlas --debug" matches the
behavior of the previous version of the script (errors with "Must \
specify exactly one DUT",

confirmed that "./cros_run_bvt --board=atlas --labqual \
--servo_type=servo_v4_with_servo_micro --debug 127.0.0.1" echos a
dut-control command that executes successfully,

confirmed that "./cros_run_bvt --board=nami --labqual \
--servo_type=servo_v4_with_servo_micro_foo $LOCAL_DUT_IP" fails with a
servo type mismatch,

confirmed that "./cros_run_bvt --board=nami --labqual \
--servo_type=servo_v4_with_servo_micro $LOCAL_DUT_IP" executed and
passed the autotest and launched the tast tests (which looked to fail
for an unrelated reason).

Change-Id: I5f53ed3661e6d15b234aa639b1c02807ca65d8cb
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crostestutils/+/4262830
Commit-Queue: Kevin Shelton <kmshelton@chromium.org>
Reviewed-by: Matthew Blecker <matthewb@chromium.org>
Tested-by: Kevin Shelton <kmshelton@chromium.org>
Reviewed-by: Chen-Tsung Hsieh <chentsung@chromium.org>
diff --git a/cros_run_bvt b/cros_run_bvt
index 52c301e..55f5034 100755
--- a/cros_run_bvt
+++ b/cros_run_bvt
@@ -3,6 +3,7 @@
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
+# TODO(b:269503867): De-shell this utility.
 # This script requires that you run build_packages first.
 
 # Special because this can only run inside the chroot.
@@ -31,6 +32,9 @@
   "When running servo tests, specifies the host running servod"
 DEFINE_string servo_port "" \
   "When running servo tests, specifies the port for contacting servod"
+# Free-form string to check against servod's reported servo type.
+DEFINE_string servo_type "" \
+  "When running servo tests, specifies the servo type"
 
 # Parse command line; die on errors
 FLAGS_HELP="usage: $(basename "$0") [flags] <hostname-or-ipaddr>"
@@ -42,6 +46,11 @@
   die "--board required"
 fi
 
+DEBUG=
+if [ "${FLAGS_debug}" -eq "${FLAGS_TRUE}" ]; then
+  DEBUG="echo"
+fi
+
 if [ $# -ne 1 ]; then
   die "Must specify exactly one DUT"
 fi
@@ -49,6 +58,7 @@
 DUT="$1"
 OPTIONS=( --board="${FLAGS_board}" )
 TESTS=( suite:{labqual,bvt-{inline,cq,perbuild,tast-cq}} )
+GET_SERVO_TYPE_CMD=( "dut-control" "--value_only" )
 
 SUITE_CHECK=0
 if [ "${FLAGS_smoke}" -eq "${FLAGS_TRUE}" ]; then
@@ -64,6 +74,9 @@
   TESTS=( suite:labqual)
 
   # Handle Servo options for the lab qualification tests.
+  if [ -z "${FLAGS_servo_type}" ]; then
+    die "servo type must be specified for labquals to prevent false positives"
+  fi
   if [ -n "${FLAGS_servo_host}" ]; then
     SERVO_ARGS="--args=servo_host=${FLAGS_servo_host}"
     if [ -n "${FLAGS_servo_port}" ]; then
@@ -73,17 +86,32 @@
   elif [ -n "${FLAGS_servo_port}" ]; then
     OPTIONS+=( "--args=servo_port=${FLAGS_servo_port}" )
   fi
+
+  if [ -n "${FLAGS_servo_host}" ]; then
+    GET_SERVO_TYPE_CMD+=( "--host=${FLAGS_servo_host}" )
+  fi
+  if [ -n "${FLAGS_servo_port}" ]; then
+    GET_SERVO_TYPE_CMD+=( "--port=${FLAGS_servo_port}" )
+  fi
+  GET_SERVO_TYPE_CMD+=( "servo_type" )
+
+  DETECTED_SERVO_TYPE=""
+  if [ -n "${DEBUG}" ]; then
+    echo "${GET_SERVO_TYPE_CMD[@]}"
+  else
+    DETECTED_SERVO_TYPE=$("${GET_SERVO_TYPE_CMD[@]}")
+  fi
+  if [ -z "${DEBUG}" ]; then
+    if [ "${DETECTED_SERVO_TYPE}" != "${FLAGS_servo_type}" ]; then
+      die "detected servo type does not match expected servo type"
+    fi
+  fi
 fi
 
 if [ "${SUITE_CHECK}" -gt 1 ]; then
   die "can specify at most one of --smoke --commit or --labqual"
 fi
 
-DEBUG=
-if [ "${FLAGS_debug}" -eq "${FLAGS_TRUE}" ]; then
-  DEBUG="echo"
-fi
-
 ${DEBUG} test_that "${OPTIONS[@]}" "${DUT}" "${TESTS[@]}"
 AUTOTEST_STATUS=$?