lib.sh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #!/bin/bash -eu
  2. set -o pipefail
  3. HARDWIRED_MOCK_TOP=
  4. # Uncomment this to be able to view the source tree after a test is run
  5. # HARDWIRED_MOCK_TOP=/tmp/td
  6. REAL_TOP="$(readlink -f "$(dirname "$0")"/../../..)"
  7. if [[ -n "$HARDWIRED_MOCK_TOP" ]]; then
  8. MOCK_TOP="$HARDWIRED_MOCK_TOP"
  9. else
  10. MOCK_TOP=$(mktemp -t -d st.XXXXX)
  11. trap cleanup_mock_top EXIT
  12. fi
  13. WARMED_UP_MOCK_TOP=$(mktemp -t soong_integration_tests_warmup.XXXXXX.tar.gz)
  14. trap 'rm -f "$WARMED_UP_MOCK_TOP"' EXIT
  15. function warmup_mock_top {
  16. info "Warming up mock top ..."
  17. info "Mock top warmup archive: $WARMED_UP_MOCK_TOP"
  18. cleanup_mock_top
  19. mkdir -p "$MOCK_TOP"
  20. cd "$MOCK_TOP"
  21. create_mock_soong
  22. run_soong
  23. tar czf "$WARMED_UP_MOCK_TOP" *
  24. }
  25. function cleanup_mock_top {
  26. cd /
  27. rm -fr "$MOCK_TOP"
  28. }
  29. function info {
  30. echo -e "\e[92;1m[TEST HARNESS INFO]\e[0m" "$*"
  31. }
  32. function fail {
  33. echo -e "\e[91;1mFAILED:\e[0m" "$*"
  34. exit 1
  35. }
  36. function copy_directory {
  37. local dir="$1"
  38. local -r parent="$(dirname "$dir")"
  39. mkdir -p "$MOCK_TOP/$parent"
  40. cp -R "$REAL_TOP/$dir" "$MOCK_TOP/$parent"
  41. }
  42. function symlink_file {
  43. local file="$1"
  44. mkdir -p "$MOCK_TOP/$(dirname "$file")"
  45. ln -s "$REAL_TOP/$file" "$MOCK_TOP/$file"
  46. }
  47. function symlink_directory {
  48. local dir="$1"
  49. mkdir -p "$MOCK_TOP/$dir"
  50. # We need to symlink the contents of the directory individually instead of
  51. # using one symlink for the whole directory because finder.go doesn't follow
  52. # symlinks when looking for Android.bp files
  53. for i in "$REAL_TOP/$dir"/*; do
  54. i=$(basename "$i")
  55. local target="$MOCK_TOP/$dir/$i"
  56. local source="$REAL_TOP/$dir/$i"
  57. if [[ -e "$target" ]]; then
  58. if [[ ! -d "$source" || ! -d "$target" ]]; then
  59. fail "Trying to symlink $dir twice"
  60. fi
  61. else
  62. ln -s "$REAL_TOP/$dir/$i" "$MOCK_TOP/$dir/$i";
  63. fi
  64. done
  65. }
  66. function create_mock_soong {
  67. create_mock_bazel
  68. copy_directory build/blueprint
  69. copy_directory build/soong
  70. copy_directory build/make
  71. symlink_directory prebuilts/sdk
  72. symlink_directory prebuilts/go
  73. symlink_directory prebuilts/build-tools
  74. symlink_directory prebuilts/clang/host
  75. symlink_directory external/compiler-rt
  76. symlink_directory external/go-cmp
  77. symlink_directory external/golang-protobuf
  78. symlink_directory external/licenseclassifier
  79. symlink_directory external/starlark-go
  80. symlink_directory external/python
  81. symlink_directory external/sqlite
  82. symlink_directory external/spdx-tools
  83. symlink_directory libcore
  84. # TODO: b/286872909 - Remove these when the blocking bug is completed
  85. symlink_directory external/libavc
  86. symlink_directory external/libaom
  87. symlink_directory external/libvpx
  88. symlink_directory frameworks/base/libs/androidfw
  89. symlink_directory external/libhevc
  90. symlink_directory external/libexif
  91. symlink_directory external/libopus
  92. symlink_directory external/libmpeg2
  93. symlink_directory external/expat
  94. symlink_directory external/flac
  95. symlink_directory system/extras/toolchain-extras
  96. touch "$MOCK_TOP/Android.bp"
  97. }
  98. function setup {
  99. cleanup_mock_top
  100. mkdir -p "$MOCK_TOP"
  101. echo
  102. echo ----------------------------------------------------------------------------
  103. info "Running test case \e[96;1m${FUNCNAME[1]}\e[0m"
  104. cd "$MOCK_TOP"
  105. tar xzf "$WARMED_UP_MOCK_TOP" --warning=no-timestamp
  106. }
  107. # shellcheck disable=SC2120
  108. function run_soong {
  109. USE_RBE=false build/soong/soong_ui.bash --make-mode --skip-ninja --skip-config --soong-only --skip-soong-tests "$@"
  110. }
  111. function create_mock_bazel {
  112. copy_directory build/bazel
  113. copy_directory build/bazel_common_rules
  114. symlink_directory packages/modules/common/build
  115. symlink_directory prebuilts/bazel
  116. symlink_directory prebuilts/clang
  117. symlink_directory prebuilts/jdk
  118. symlink_directory external/bazel-skylib
  119. symlink_directory external/bazelbuild-rules_android
  120. symlink_directory external/bazelbuild-rules_go
  121. symlink_directory external/bazelbuild-rules_license
  122. symlink_directory external/bazelbuild-kotlin-rules
  123. symlink_file WORKSPACE
  124. symlink_file BUILD
  125. }
  126. function run_bazel {
  127. # Remove the ninja_build output marker file to communicate to buildbot that this is not a regular Ninja build, and its
  128. # output should not be parsed as such.
  129. rm -rf out/ninja_build
  130. build/bazel/bin/bazel "$@"
  131. }
  132. function run_ninja {
  133. build/soong/soong_ui.bash --make-mode --skip-config --soong-only --skip-soong-tests "$@"
  134. }
  135. info "Starting Soong integration test suite $(basename "$0")"
  136. info "Mock top: $MOCK_TOP"
  137. export ALLOW_MISSING_DEPENDENCIES=true
  138. export ALLOW_BP_UNDER_SYMLINKS=true
  139. warmup_mock_top
  140. function scan_and_run_tests {
  141. # find all test_ functions
  142. # NB "declare -F" output is sorted, hence test order is deterministic
  143. readarray -t test_fns < <(declare -F | sed -n -e 's/^declare -f \(test_.*\)$/\1/p')
  144. info "Found ${#test_fns[*]} tests"
  145. if [[ ${#test_fns[*]} -eq 0 ]]; then
  146. fail "No tests found"
  147. fi
  148. for f in ${test_fns[*]}; do
  149. $f
  150. info "Completed test case \e[96;1m$f\e[0m"
  151. done
  152. }