platform_build_soong/tests/persistent_bazel_test.sh
Cole Faust c9508aac4c Load starlark files from soong
There are a number of instances where we are exporting information
from soong to bazel via soong_injection. This could be more bazel-centric
if the information was instead held in bzl files, and both bazel and
soong read it from there.

Add a starlark package that will run
//build/bazel/constants_exported_to_soong.bzl at initialization time,
and then results can be retreived with GetStarlarkValue.

Since changes to the starlark files mean that soong has to rerun,
add them as ninja deps.

Unfortunately, the starlark code has to be run at runtime rather than
pregenerating their results, because tests run from intellij wouldn't
go through any pregeneration steps. This means that starlark is run
multiple times during the build, once per test package and once per
primary builder invocation. (currently 3, could be reduced to 2 if we
made the symlink forest generation into its own standalone tool) The
starlark code we have so far in this cl is very fast, roughly half a
millisecond, so it's not a big deal for now, but something to keep an
eye on as we add more starlark constants.

Bug: 279095899
Test: go test
Change-Id: I1e7ca1df1d8d67333cbfc46e8396e229820e4476
2023-04-26 17:18:19 -07:00

83 lines
2.9 KiB
Bash
Executable file

#!/bin/bash -eu
# Copyright (C) 2023 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -o pipefail
source "$(dirname "$0")/lib.sh"
# This test verifies that adding USE_PERSISTENT_BAZEL creates a Bazel process
# that outlasts the build process.
# This test should only be run in sandboxed environments (because this test
# verifies a Bazel process using global process list, and may spawn lingering
# Bazel processes).
function test_persistent_bazel {
setup
# Ensure no existing Bazel process.
if [[ -e out/bazel/output/server/server.pid.txt ]]; then
kill $(cat out/bazel/output/server/server.pid.txt) 2>/dev/null || true
if kill -0 $(cat out/bazel/output/server/server.pid.txt) 2>/dev/null ; then
fail "Error killing pre-setup bazel"
fi
fi
USE_PERSISTENT_BAZEL=1 run_soong nothing
if ! kill -0 $(cat out/bazel/output/server/server.pid.txt) 2>/dev/null ; then
fail "Persistent bazel process expected, but not found after first build"
fi
BAZEL_PID=$(cat out/bazel/output/server/server.pid.txt)
USE_PERSISTENT_BAZEL=1 run_soong nothing
if ! kill -0 $BAZEL_PID 2>/dev/null ; then
fail "Bazel pid $BAZEL_PID was killed after second build"
fi
kill $BAZEL_PID 2>/dev/null
if ! kill -0 $BAZEL_PID 2>/dev/null ; then
fail "Error killing bazel on shutdown"
fi
}
# Verifies that USE_PERSISTENT_BAZEL mode operates as expected in the event
# that there are Bazel failures.
function test_bazel_failure {
setup
# Ensure no existing Bazel process.
if [[ -e out/bazel/output/server/server.pid.txt ]]; then
kill $(cat out/bazel/output/server/server.pid.txt) 2>/dev/null || true
if kill -0 $(cat out/bazel/output/server/server.pid.txt) 2>/dev/null ; then
fail "Error killing pre-setup bazel"
fi
fi
# Introduce a syntax error in a BUILD file which is used in every build
# (Note this is a BUILD file which is copied as part of test setup, so this
# has no effect on sources outside of this test.
rm -rf build/bazel/rules
USE_PERSISTENT_BAZEL=1 run_soong nothing 1>out/failurelog.txt 2>&1 && fail "Expected build failure" || true
if ! grep -sq "cannot load //build/bazel/rules/common/api_constants.bzl" out/failurelog.txt ; then
fail "Expected error to contain 'cannot load //build/bazel/rules/common/api_constants.bzl', instead got:\n$(cat out/failurelog.txt)"
fi
kill $(cat out/bazel/output/server/server.pid.txt) 2>/dev/null || true
}
scan_and_run_tests