Rewrite run-libcore-tests.sh in python

Bug: 142039427
Test: run-libcore-tests.sh --mode=host
Test: run-libcore-tests.sh --mode=device
Change-Id: I9d90fa97a0b1540b80d358ce7482012202a62687
diff --git a/tools/run-libcore-tests.py b/tools/run-libcore-tests.py
new file mode 100755
index 0000000..ebcf7d3
--- /dev/null
+++ b/tools/run-libcore-tests.py
@@ -0,0 +1,208 @@
+#!/bin/python3
+#
+# Copyright (C) 2021 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.
+
+import sys, os, argparse, subprocess, shlex
+
+def parse_args():
+  parser = argparse.ArgumentParser(description="Run libcore tests using the vogar testing tool.")
+  parser.add_argument('--mode', choices=['device', 'host', 'jvm'], required=True,
+                      help='Specify where tests should be run.')
+  parser.add_argument('--variant', choices=['X32', 'X64'],
+                      help='Which dalvikvm variant to execute with.')
+  parser.add_argument('--timeout', type=int,
+                      help='How long to run the test before aborting (seconds).')
+  parser.add_argument('--debug', action='store_true',
+                      help='Use debug version of ART (device|host only).')
+  parser.add_argument('--dry-run', action='store_true',
+                      help='Print vogar command-line, but do not run.')
+  parser.add_argument('--no-getrandom', action='store_false', dest='getrandom',
+                      help='Ignore failures from getrandom() (for kernel < 3.17).')
+  parser.add_argument('--no-jit', action='store_false', dest='jit',
+                      help='Disable JIT (device|host only).')
+  parser.add_argument('--gcstress', action='store_true',
+                      help='Enable GC stress configuration (device|host only).')
+  parser.add_argument('tests', nargs="*",
+                      help='Name(s) of the test(s) to run')
+  return parser.parse_args()
+
+ART_TEST_ANDROID_ROOT = os.environ.get("ART_TEST_ANDROID_ROOT", "/system")
+ART_TEST_CHROOT = os.environ.get("ART_TEST_CHROOT")
+ANDROID_PRODUCT_OUT = os.environ.get("ANDROID_PRODUCT_OUT")
+
+LIBCORE_TEST_NAMES = [
+  "libcore.android.system",
+  "libcore.build",
+  "libcore.dalvik.system",
+  "libcore.java.awt",
+  "libcore.java.lang",
+  "libcore.java.math",
+  "libcore.java.text",
+  "libcore.java.util",
+  "libcore.javax.crypto",
+  "libcore.javax.net",
+  "libcore.javax.security",
+  "libcore.javax.sql",
+  "libcore.javax.xml",
+  "libcore.libcore.icu",
+  "libcore.libcore.internal",
+  "libcore.libcore.io",
+  "libcore.libcore.net",
+  "libcore.libcore.reflect",
+  "libcore.libcore.util",
+  "libcore.sun.invoke",
+  "libcore.sun.net",
+  "libcore.sun.misc",
+  "libcore.sun.security",
+  "libcore.sun.util",
+  "libcore.xml",
+  "org.apache.harmony.annotation",
+  "org.apache.harmony.crypto",
+  "org.apache.harmony.luni",
+  "org.apache.harmony.nio",
+  "org.apache.harmony.regex",
+  "org.apache.harmony.testframework",
+  "org.apache.harmony.tests.java.io",
+  "org.apache.harmony.tests.java.lang",
+  "org.apache.harmony.tests.java.math",
+  "org.apache.harmony.tests.java.util",
+  "org.apache.harmony.tests.java.text",
+  "org.apache.harmony.tests.javax.security",
+  "tests.java.lang.String",
+  "jsr166",
+  "libcore.highmemorytest",
+]
+# "org.apache.harmony.security",  # We don't have rights to revert changes in case of failures.
+
+# Note: This must start with the CORE_IMG_JARS in Android.common_path.mk
+# because that's what we use for compiling the boot.art image.
+# It may contain additional modules from TEST_CORE_JARS.
+BOOT_CLASSPATH = [
+  "/apex/com.android.art/javalib/core-oj.jar",
+  "/apex/com.android.art/javalib/core-libart.jar",
+  "/apex/com.android.art/javalib/okhttp.jar",
+  "/apex/com.android.art/javalib/bouncycastle.jar",
+  "/apex/com.android.art/javalib/apache-xml.jar",
+  "/apex/com.android.i18n/javalib/core-icu4j.jar",
+  "/apex/com.android.conscrypt/javalib/conscrypt.jar",
+]
+
+CLASSPATH = ["core-tests", "jsr166-tests", "mockito-target"]
+
+def get_jar_filename(classpath):
+  base_path = (ANDROID_PRODUCT_OUT + "/../..") if ANDROID_PRODUCT_OUT else "out/target"
+  base_path = os.path.normpath(base_path)  # Normalize ".." components for readability.
+  return f"{base_path}/common/obj/JAVA_LIBRARIES/{classpath}_intermediates/classes.jar"
+
+def get_timeout_secs():
+  default_timeout_secs = 600
+  if args.mode == "device" and args.gcstress:
+    default_timeout_secs = 1200
+    if args.debug:
+      default_timeout_secs = 1800
+  return args.timeout or default_timeout_secs
+
+def get_expected_failures():
+  failures = ["art/tools/libcore_failures.txt"]
+  if args.mode != "jvm":
+    if args.gcstress:
+      failures.append("art/tools/libcore_gcstress_failures.txt")
+    if args.gcstress and args.debug:
+      failures.append("art/tools/libcore_gcstress_debug_failures.txt")
+    if args.debug and not args.gcstress:
+      failures.append("art/tools/libcore_debug_failures.txt")
+    if not args.getrandom:
+      failures.append("art/tools/libcore_fugu_failures.txt")
+  return failures
+
+def get_test_names():
+  if args.tests:
+    return args.tests
+  test_names = list(LIBCORE_TEST_NAMES)
+  # See b/78228743 and b/178351808.
+  if args.gcstress or args.debug or args.mode == "jvm":
+    test_names.remove("libcore.highmemorytest")
+  return test_names
+
+def get_vogar_command(test_names):
+  cmd = ["vogar"]
+  if args.mode == "device":
+    cmd.append("--mode=device --vm-arg -Ximage:/apex/com.android.art/javalib/boot.art")
+    cmd.append("--vm-arg -Xbootclasspath:" + ":".join(BOOT_CLASSPATH))
+  if args.mode == "host":
+    # We explicitly give a wrong path for the image, to ensure vogar
+    # will create a boot image with the default compiler. Note that
+    # giving an existing image on host does not work because of
+    # classpath/resources differences when compiling the boot image.
+    cmd.append("--mode=host --vm-arg -Ximage:/non/existent/vogar.art")
+  if args.mode == "jvm":
+    cmd.append("--mode=jvm")
+  if args.variant:
+    cmd.append("--variant=" + args.variant)
+  if args.gcstress:
+    cmd.append("--vm-arg -Xgc:gcstress")
+  if args.debug:
+    cmd.append("--vm-arg -XXlib:libartd.so --vm-arg -XX:SlowDebug=true")
+
+  if args.mode == "device":
+    if ART_TEST_CHROOT:
+      cmd.append(f"--chroot {ART_TEST_CHROOT} --device-dir=/tmp")
+    else:
+      cmd.append("--device-dir=/data/local/tmp")
+    cmd.append(f"--vm-command={ART_TEST_ANDROID_ROOT}/bin/art")
+
+  if args.mode != "jvm":
+    cmd.append("--timeout {}".format(get_timeout_secs()))
+
+    # Suppress explicit gc logs that are triggered an absurd number of times by these tests.
+    cmd.append("--vm-arg -XX:AlwaysLogExplicitGcs:false")
+    cmd.append("--toolchain d8 --language CUR")
+    if args.jit:
+      cmd.append("--vm-arg -Xcompiler-option --vm-arg --compiler-filter=quicken")
+    cmd.append("--vm-arg -Xusejit:{}".format(str(args.jit).lower()))
+
+    if args.gcstress:
+      # Bump pause threshold as long pauses cause explicit gc logging to occur irrespective
+      # of -XX:AlwayLogExplicitGcs:false.
+      cmd.append("--vm-arg -XX:LongPauseLogThreshold=15") # 15 ms (default: 5ms))
+
+  # Suppress color codes if not attached to a terminal
+  if not sys.stdout.isatty():
+    cmd.append("--no-color")
+
+  cmd.extend("--expectations " + f for f in get_expected_failures())
+  cmd.extend("--classpath " + get_jar_filename(cp) for cp in CLASSPATH)
+  cmd.extend(test_names)
+  return cmd
+
+def main():
+  global args
+  args = parse_args()
+
+  if not os.path.exists('build/envsetup.sh'):
+    raise AssertionError("Script needs to be run at the root of the android tree")
+  for jar in map(get_jar_filename, CLASSPATH):
+    if not os.path.exists(jar):
+      raise AssertionError(f"Missing {jar}. Run buildbot-build.sh first.")
+
+  cmd = " ".join(get_vogar_command(get_test_names()))
+  print(cmd)
+  if not args.dry_run:
+    with subprocess.Popen(shlex.split(cmd)) as proc:
+      exit_code = proc.wait()
+      sys.exit(exit_code)
+
+if __name__ == '__main__':
+  main()
diff --git a/tools/run-libcore-tests.sh b/tools/run-libcore-tests.sh
index 0aedd66..7cbf5ea 100755
--- a/tools/run-libcore-tests.sh
+++ b/tools/run-libcore-tests.sh
@@ -14,341 +14,4 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-# Exit on errors.
-set -e
-
-if [ ! -d libcore ]; then
-  echo "Script needs to be run at the root of the android tree"
-  exit 1
-fi
-
-# "Root" (actually "system") directory on device (in the case of
-# target testing).
-android_root=${ART_TEST_ANDROID_ROOT:-/system}
-
-function classes_jar_path {
-  local var="$1"
-  local suffix="jar"
-  if [ -z "$ANDROID_PRODUCT_OUT" ] ; then
-    local java_libraries=out/target/common/obj/JAVA_LIBRARIES
-  else
-    local java_libraries=${ANDROID_PRODUCT_OUT}/../../common/obj/JAVA_LIBRARIES
-  fi
-  echo "${java_libraries}/${var}_intermediates/classes.${suffix}"
-}
-
-function cparg {
-  for var
-  do
-    printf -- "--classpath $(classes_jar_path "$var") ";
-  done
-}
-
-function boot_classpath_arg {
-  local dir="$1"
-  shift 1
-  printf -- "--vm-arg -Xbootclasspath"
-  for var
-  do
-    printf -- ":${dir}/${var}.jar";
-  done
-  printf -- ":/apex/com.android.i18n/javalib/core-icu4j.jar:/apex/com.android.conscrypt/javalib/conscrypt.jar";
-}
-
-function usage {
-  local me=$(basename "${BASH_SOURCE[0]}")
-  (
-    cat << EOF
-  Usage: ${me} --mode=<mode> [options] [-- <package_to_test> ...]
-
-  Run libcore tests using the vogar testing tool.
-
-  Required parameters:
-    --mode=device|host|jvm Specify where tests should be run.
-
-  Optional parameters:
-    --debug                Use debug version of ART (device|host only).
-    --dry-run              Print vogar command-line, but do not run.
-    --no-getrandom         Ignore failures from getrandom() (for kernel < 3.17).
-    --no-jit               Disable JIT (device|host only).
-    --gcstress             Enable GC stress configuration (device|host only).
-
-  The script passes unrecognized options to the command-line created for vogar.
-
-  The script runs a hardcoded list of libcore test packages by default. The user
-  may run a subset of packages by appending '--' followed by a list of package
-  names.
-
-  Examples:
-
-    1. Run full test suite on host:
-      ${me} --mode=host
-
-    2. Run full test suite on device:
-      ${me} --mode=device
-
-    3. Run tests only from the libcore.java.lang package on device:
-      ${me} --mode=device -- libcore.java.lang
-EOF
-  ) | sed -e 's/^  //' >&2 # Strip leading whitespace from heredoc.
-}
-
-# Packages that currently work correctly with the expectation files.
-working_packages=("libcore.android.system"
-                  "libcore.build"
-                  "libcore.dalvik.system"
-                  "libcore.java.awt"
-                  "libcore.java.lang"
-                  "libcore.java.math"
-                  "libcore.java.text"
-                  "libcore.java.util"
-                  "libcore.javax.crypto"
-                  "libcore.javax.net"
-                  "libcore.javax.security"
-                  "libcore.javax.sql"
-                  "libcore.javax.xml"
-                  "libcore.libcore.icu"
-                  "libcore.libcore.internal"
-                  "libcore.libcore.io"
-                  "libcore.libcore.net"
-                  "libcore.libcore.reflect"
-                  "libcore.libcore.util"
-                  "libcore.sun.invoke"
-                  "libcore.sun.net"
-                  "libcore.sun.misc"
-                  "libcore.sun.security"
-                  "libcore.sun.util"
-                  "libcore.xml"
-                  "org.apache.harmony.annotation"
-                  "org.apache.harmony.crypto"
-                  "org.apache.harmony.luni"
-                  "org.apache.harmony.nio"
-                  "org.apache.harmony.regex"
-                  "org.apache.harmony.testframework"
-                  "org.apache.harmony.tests.java.io"
-                  "org.apache.harmony.tests.java.lang"
-                  "org.apache.harmony.tests.java.math"
-                  "org.apache.harmony.tests.java.util"
-                  "org.apache.harmony.tests.java.text"
-                  "org.apache.harmony.tests.javax.security"
-                  "tests.java.lang.String"
-                  "jsr166")
-
-# List of packages we could run, but don't have rights to revert
-# changes in case of failures.
-# "org.apache.harmony.security"
-
-#
-# Setup environment for running tests.
-#
-source build/envsetup.sh >&/dev/null # for get_build_var, setpaths
-setpaths # include platform prebuilt java, javac, etc in $PATH.
-
-# Note: This must start with the CORE_IMG_JARS in Android.common_path.mk
-# because that's what we use for compiling the boot.art image.
-# It may contain additional modules from TEST_CORE_JARS.
-BOOT_CLASSPATH_JARS="core-oj core-libart okhttp bouncycastle apache-xml"
-
-DEPS="core-tests jsr166-tests mockito-target"
-
-for lib in $DEPS
-do
-  if [[ ! -f "$(classes_jar_path "$lib")" ]]; then
-    echo "${lib} is missing. Before running, you must run art/tools/buildbot-build.sh"
-    exit 1
-  fi
-done
-
-#
-# Defaults affected by command-line parsing
-#
-
-# Use JIT compiling by default.
-use_jit=true
-
-debug=false
-dry_run=false
-gcstress=false
-heap_poisoning=${ART_HEAP_POISONING:-false}
-
-# Run tests that use the getrandom() syscall? (Requires Linux 3.17+).
-getrandom=true
-
-# Execution mode specifies where to run tests (device|host|jvm).
-execution_mode=""
-
-# Default expectations file.
-expectations="--expectations art/tools/libcore_failures.txt"
-
-vogar_args=""
-while [ -n "$1" ]; do
-  case "$1" in
-    --mode=device)
-      vogar_args="$vogar_args --mode=device"
-      vogar_args="$vogar_args --vm-arg -Ximage:/apex/com.android.art/javalib/boot.art"
-      vogar_args="$vogar_args $(boot_classpath_arg /apex/com.android.art/javalib $BOOT_CLASSPATH_JARS)"
-      execution_mode="device"
-      ;;
-    --mode=host)
-      # We explicitly give a wrong path for the image, to ensure vogar
-      # will create a boot image with the default compiler. Note that
-      # giving an existing image on host does not work because of
-      # classpath/resources differences when compiling the boot image.
-      vogar_args="$vogar_args $1 --vm-arg -Ximage:/non/existent/vogar.art"
-      execution_mode="host"
-      ;;
-    --mode=jvm)
-      vogar_args="$vogar_args $1"
-      execution_mode="jvm"
-      ;;
-    --no-getrandom)
-      getrandom=false
-      ;;
-    --no-jit)
-      use_jit=false
-      ;;
-    --debug)
-      vogar_args="$vogar_args --vm-arg -XXlib:libartd.so --vm-arg -XX:SlowDebug=true"
-      debug=true
-      ;;
-    --gcstress)
-      vogar_args="$vogar_args --vm-arg -Xgc:gcstress"
-      gcstress=true
-      ;;
-    -Xgc:gcstress)
-      # Deprecated option for selecting gcstress (b/172923084).
-      echo "Warning: -Xgc:gcstress is deprecated, use --gcstress instead." 1>&2
-      vogar_args="$vogar_args $1" # note: requires --vm-arg before -Xgc:gcstress
-      gcstress=true
-      ;;
-    --dry-run)
-      dry_run=true
-      ;;
-    --)
-      shift
-      # Assume remaining elements are packages to test.
-      user_packages=("$@")
-      break
-      ;;
-    --help)
-      usage
-      exit 1
-      ;;
-    *)
-      vogar_args="$vogar_args $1"
-      ;;
-  esac
-  shift
-done
-
-if [ -z "$execution_mode" ]; then
-  usage
-  exit 1
-fi
-
-# Default timeout, gets overridden on device under gcstress.
-default_timeout_secs=480
-
-if [ $execution_mode = "device" ]; then
-  # Honor environment variable ART_TEST_CHROOT.
-  if [[ -n "$ART_TEST_CHROOT" ]]; then
-    # Set Vogar's `--chroot` option.
-    vogar_args="$vogar_args --chroot $ART_TEST_CHROOT"
-    vogar_args="$vogar_args --device-dir=/tmp"
-  else
-    # When not using a chroot on device, set Vogar's work directory to
-    # /data/local/tmp.
-    vogar_args="$vogar_args --device-dir=/data/local/tmp"
-  fi
-  vogar_args="$vogar_args --vm-command=$android_root/bin/art"
-
-  # Increase the timeout, as vogar cannot set individual test
-  # timeout when being asked to run packages, and some tests go above
-  # the default timeout.
-  if $gcstress; then
-    if $debug; then
-      # Increasing for unwinding changes (b/185305054).
-      default_timeout_secs=1800
-    else
-      default_timeout_secs=1200
-    fi
-  elif $heap_poisoning && $debug; then
-    # Increase the timeout for heap poisoning and debug combo
-    # following ICU rewrites (b/161420453).
-    default_timeout_secs=600
-  fi
-elif [ $execution_mode = "host" ]; then
-  # Increase timeout for gcstress and debug combo following ICU
-  # rewrites (b/161420453).
-  if $gcstress && $debug; then
-    default_timeout_secs=600
-  fi
-fi
-
-if [ $execution_mode = "device" -o $execution_mode = "host" ]; then
-  # Add timeout to vogar command-line (if not explicitly present in the command-line arguments) .
-  if [[ "$vogar_args" != *" --timeout "* ]]; then
-    vogar_args="$vogar_args --timeout $default_timeout_secs"
-  fi
-
-  # Suppress explicit gc logs that are triggered an absurd number of times by these tests.
-  vogar_args="$vogar_args --vm-arg -XX:AlwaysLogExplicitGcs:false"
-
-  # set the toolchain to use.
-  vogar_args="$vogar_args --toolchain d8 --language CUR"
-
-  # JIT settings.
-  if $use_jit; then
-    vogar_args="$vogar_args --vm-arg -Xcompiler-option --vm-arg --compiler-filter=quicken"
-  fi
-  vogar_args="$vogar_args --vm-arg -Xusejit:$use_jit"
-
-  # gcstress may lead to timeouts, so we need dedicated expectations files for it.
-  if $gcstress; then
-    expectations="$expectations --expectations art/tools/libcore_gcstress_failures.txt"
-    if $debug; then
-      expectations="$expectations --expectations art/tools/libcore_gcstress_debug_failures.txt"
-    fi
-
-    # Bump pause threshold as long pauses cause explicit gc logging to occur irrespective
-    # of -XX:AlwayLogExplicitGcs:false.
-    vogar_args="$vogar_args --vm-arg -XX:LongPauseLogThreshold=15" # 15 ms (default: 5ms)
-  else
-    # Include debug expectations if not on fugu.
-    if $debug && $getrandom; then
-      expectations="$expectations --expectations art/tools/libcore_debug_failures.txt"
-    fi
-
-    # We only run this package when user has not specified packages
-    # to run and not under gcstress / debug as it can cause timeouts. See
-    # b/78228743 and b/178351808.
-    if ! $debug ; then
-      working_packages+=("libcore.highmemorytest")
-    fi
-  fi
-
-  if $getrandom; then :; else
-    # Ignore failures in tests that use the system calls not supported
-    # on fugu (Nexus Player, kernel version Linux 3.10).
-    expectations="$expectations --expectations art/tools/libcore_fugu_failures.txt"
-  fi
-fi
-
-if [ ! -t 1 ] ; then
-  # Suppress color codes if not attached to a terminal
-  vogar_args="$vogar_args --no-color"
-fi
-
-# Override working_packages if user provided specific packages to
-# test.
-if [[ ${#user_packages[@]} != 0 ]] ; then
-  working_packages=("${user_packages[@]}")
-fi
-
-# Run the tests using vogar.
-echo "Running tests for the following test packages:"
-echo ${working_packages[@]} | tr " " "\n"
-
-cmd="vogar $vogar_args $expectations $(cparg $DEPS) ${working_packages[@]}"
-echo "Running $cmd"
-$dry_run || eval $cmd
+art/tools/run-libcore-tests.py "$@"