runtests.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #!/usr/bin/env bash
  2. set -e
  3. set -u
  4. readonly DEFAULT_TESTS="
  5. netdutils_test
  6. netd_unit_test
  7. netd_integration_test
  8. resolv_integration_test
  9. resolv_unit_test
  10. "
  11. readonly EXTENDED_TESTS="
  12. netd_benchmark
  13. "
  14. readonly TEST_DEVICE_PATH="/data/local/tmp"
  15. REPO_TOP=""
  16. SOONG_BIN=""
  17. DEBUG=""
  18. function logToStdErr() {
  19. echo "$1" >&2
  20. }
  21. function testAndSetRepoTop() {
  22. if [[ -n "$1" && -d "$1/.repo" ]]; then
  23. REPO_TOP="$1"
  24. SOONG_BIN="$REPO_TOP/build/soong/soong_ui.bash"
  25. return 0
  26. fi
  27. return 1
  28. }
  29. function gotoRepoTop() {
  30. if testAndSetRepoTop "$ANDROID_BUILD_TOP"; then
  31. return
  32. fi
  33. while ! testAndSetRepoTop "$PWD"; do
  34. if [[ "$PWD" == "/" ]]; then
  35. break
  36. fi
  37. cd ..
  38. done
  39. }
  40. function runCmd() {
  41. local cmd="$@"
  42. echo "#"
  43. echo "# $cmd"
  44. $DEBUG $cmd
  45. return $?
  46. }
  47. function runOneTest() {
  48. local test="$1"
  49. echo
  50. echo "###"
  51. echo "# $test"
  52. echo "###"
  53. local prefix="$ANDROID_TARGET_OUT_TESTCASES/$test/$TARGET_ARCH/$test"
  54. for bits in '' 32 64; do
  55. local testbin="${prefix}${bits}"
  56. if [ -f "$testbin" ]; then
  57. runCmd adb push "$testbin" "$TEST_DEVICE_PATH/$test" &&
  58. runCmd adb shell "$TEST_DEVICE_PATH/$test" &&
  59. runCmd adb shell "rm $TEST_DEVICE_PATH/$test"
  60. return $?
  61. fi
  62. done
  63. logToStdErr "Couldn't find test binary '$prefix'"
  64. return 1
  65. }
  66. function main() {
  67. if [ ! -v ANDROID_BUILD_TOP ]; then
  68. logToStdErr "You need to source and lunch before you can use this script"
  69. return 1
  70. fi
  71. gotoRepoTop
  72. if ! testAndSetRepoTop "$REPO_TOP"; then
  73. logToStdErr "Could not find useful top of repo directory"
  74. return 1
  75. fi
  76. logToStdErr "Using REPO_TOP=$REPO_TOP"
  77. TARGET_ARCH=$("$SOONG_BIN" --dumpvar-mode TARGET_ARCH)
  78. if [ -z "$TARGET_ARCH" ]; then
  79. logToStdErr "Could not determine TARGET_ARCH"
  80. return 1
  81. fi
  82. local test_regex=""
  83. while [[ $# -gt 0 ]]; do
  84. case "$1" in
  85. "-n")
  86. DEBUG=echo
  87. shift
  88. ;;
  89. *)
  90. # Allow us to do things like "runtests.sh integration", etc.
  91. test_regex="$1"
  92. shift
  93. ;;
  94. esac
  95. done
  96. # Try becoming root, in case the tests will be run on a device
  97. # with a userdebug build. Failure to become root is not fatal
  98. # for all (in fact, not even most) tests so don't exit on error.
  99. runCmd adb root || logToStdErr "WARNING: unable to 'adb root'"
  100. local failures=0
  101. local skipped=0
  102. # Find out which tests to run.
  103. local tests=""
  104. for testName in $DEFAULT_TESTS; do
  105. if [[ -z "$test_regex" || "$testName" =~ "$test_regex" ]]; then
  106. tests="$tests $testName"
  107. else
  108. logToStdErr "Skipping $testName"
  109. skipped=$((skipped + 1))
  110. fi
  111. done
  112. # If something has been specified, also check the extended tests for
  113. # a possible match (i.e. in order to run benchmarks, etc).
  114. if [[ -n "$test_regex" ]]; then
  115. for testName in $EXTENDED_TESTS; do
  116. if [[ "$testName" =~ "$test_regex" ]]; then
  117. tests="$tests $testName"
  118. fi
  119. done
  120. fi
  121. if [[ -z "$tests" ]]; then
  122. logToStdErr "No tests matched"
  123. return 1
  124. fi
  125. # Build all the specified tests.
  126. runCmd "$SOONG_BIN" --make-mode "$tests" || return $?
  127. # Run all the specified tests.
  128. for testName in $tests; do
  129. runOneTest "$testName" || failures=$((failures + 1))
  130. done
  131. echo "Number of tests failing: $failures"
  132. [[ $skipped -gt 0 ]] && echo "Number of tests skipped: $skipped"
  133. return $failures
  134. }
  135. main "$@"
  136. exit $?