persistent_bazel_test.sh 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/bin/bash -eu
  2. # Copyright (C) 2023 The Android Open Source Project
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. set -o pipefail
  16. source "$(dirname "$0")/lib.sh"
  17. # This test verifies that adding USE_PERSISTENT_BAZEL creates a Bazel process
  18. # that outlasts the build process.
  19. # This test should only be run in sandboxed environments (because this test
  20. # verifies a Bazel process using global process list, and may spawn lingering
  21. # Bazel processes).
  22. function test_persistent_bazel {
  23. setup
  24. # Ensure no existing Bazel process.
  25. if [[ -e out/bazel/output/server/server.pid.txt ]]; then
  26. kill $(cat out/bazel/output/server/server.pid.txt) 2>/dev/null || true
  27. if kill -0 $(cat out/bazel/output/server/server.pid.txt) 2>/dev/null ; then
  28. fail "Error killing pre-setup bazel"
  29. fi
  30. fi
  31. USE_PERSISTENT_BAZEL=1 run_soong nothing
  32. if ! kill -0 $(cat out/bazel/output/server/server.pid.txt) 2>/dev/null ; then
  33. fail "Persistent bazel process expected, but not found after first build"
  34. fi
  35. BAZEL_PID=$(cat out/bazel/output/server/server.pid.txt)
  36. USE_PERSISTENT_BAZEL=1 run_soong nothing
  37. if ! kill -0 $BAZEL_PID 2>/dev/null ; then
  38. fail "Bazel pid $BAZEL_PID was killed after second build"
  39. fi
  40. kill $BAZEL_PID 2>/dev/null
  41. if ! kill -0 $BAZEL_PID 2>/dev/null ; then
  42. fail "Error killing bazel on shutdown"
  43. fi
  44. }
  45. # Verifies that USE_PERSISTENT_BAZEL mode operates as expected in the event
  46. # that there are Bazel failures.
  47. function test_bazel_failure {
  48. setup
  49. # Ensure no existing Bazel process.
  50. if [[ -e out/bazel/output/server/server.pid.txt ]]; then
  51. kill $(cat out/bazel/output/server/server.pid.txt) 2>/dev/null || true
  52. if kill -0 $(cat out/bazel/output/server/server.pid.txt) 2>/dev/null ; then
  53. fail "Error killing pre-setup bazel"
  54. fi
  55. fi
  56. # Introduce a syntax error in a BUILD file which is used in every build
  57. # (Note this is a BUILD file which is copied as part of test setup, so this
  58. # has no effect on sources outside of this test.
  59. rm -rf build/bazel/rules
  60. USE_PERSISTENT_BAZEL=1 run_soong nothing 1>out/failurelog.txt 2>&1 && fail "Expected build failure" || true
  61. if ! grep -sq "cannot load //build/bazel/rules/common/api_constants.bzl" out/failurelog.txt ; then
  62. fail "Expected error to contain 'cannot load //build/bazel/rules/common/api_constants.bzl', instead got:\n$(cat out/failurelog.txt)"
  63. fi
  64. kill $(cat out/bazel/output/server/server.pid.txt) 2>/dev/null || true
  65. }
  66. scan_and_run_tests