lib.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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/go-cmp
  76. symlink_directory external/golang-protobuf
  77. symlink_directory external/starlark-go
  78. symlink_directory external/python
  79. symlink_directory external/sqlite
  80. touch "$MOCK_TOP/Android.bp"
  81. }
  82. function setup {
  83. cleanup_mock_top
  84. mkdir -p "$MOCK_TOP"
  85. echo
  86. echo ----------------------------------------------------------------------------
  87. info "Running test case \e[96;1m${FUNCNAME[1]}\e[0m"
  88. cd "$MOCK_TOP"
  89. tar xzf "$WARMED_UP_MOCK_TOP" --warning=no-timestamp
  90. }
  91. # shellcheck disable=SC2120
  92. function run_soong {
  93. USE_RBE=false build/soong/soong_ui.bash --make-mode --skip-ninja --skip-config --soong-only --skip-soong-tests "$@"
  94. }
  95. function create_mock_bazel {
  96. copy_directory build/bazel
  97. copy_directory build/bazel_common_rules
  98. symlink_directory packages/modules/common/build
  99. symlink_directory prebuilts/bazel
  100. symlink_directory prebuilts/clang
  101. symlink_directory prebuilts/jdk
  102. symlink_directory external/bazel-skylib
  103. symlink_directory external/bazelbuild-rules_android
  104. symlink_directory external/bazelbuild-rules_license
  105. symlink_directory external/bazelbuild-kotlin-rules
  106. symlink_file WORKSPACE
  107. symlink_file BUILD
  108. }
  109. function run_bazel {
  110. # Remove the ninja_build output marker file to communicate to buildbot that this is not a regular Ninja build, and its
  111. # output should not be parsed as such.
  112. rm -rf out/ninja_build
  113. build/bazel/bin/bazel "$@"
  114. }
  115. function run_ninja {
  116. build/soong/soong_ui.bash --make-mode --skip-config --soong-only --skip-soong-tests "$@"
  117. }
  118. info "Starting Soong integration test suite $(basename "$0")"
  119. info "Mock top: $MOCK_TOP"
  120. export ALLOW_MISSING_DEPENDENCIES=true
  121. export ALLOW_BP_UNDER_SYMLINKS=true
  122. warmup_mock_top
  123. function scan_and_run_tests {
  124. # find all test_ functions
  125. # NB "declare -F" output is sorted, hence test order is deterministic
  126. readarray -t test_fns < <(declare -F | sed -n -e 's/^declare -f \(test_.*\)$/\1/p')
  127. info "Found ${#test_fns[*]} tests"
  128. if [[ ${#test_fns[*]} -eq 0 ]]; then
  129. fail "No tests found"
  130. fi
  131. for f in ${test_fns[*]}; do
  132. $f
  133. done
  134. }