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