xref: /aosp_15_r20/frameworks/native/libs/binder/tests/binder_sdk/binder_sdk_docker_test.sh (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1#!/bin/bash
2
3#
4# Copyright (C) 2024 The Android Open Source Project
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#      http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18
19set -ex
20
21TEST_NAME="$(basename "$0")"
22DOCKER_TAG="${TEST_NAME}-${RANDOM}${RANDOM}"
23DOCKER_FILE=*.Dockerfile
24DOCKER_RUN_FLAGS=
25
26# Guess if we're running as an Android test or directly
27if [ "$(ls -1 ${DOCKER_FILE} | wc -l)" == "1" ]; then
28    # likely running as `atest binder_sdk_docker_test_XYZ`
29    DOCKER_PATH="$(dirname $(readlink --canonicalize --no-newline binder_sdk.zip))"
30else
31    # likely running directly as `./binder_sdk_docker_test.sh` - provide mode for easy testing
32    RED='\033[1;31m'
33    NO_COLOR='\033[0m'
34
35    if ! modinfo vsock_loopback &>/dev/null ; then
36        echo -e "${RED}Module vsock_loopback is not installed.${NO_COLOR}"
37        exit 1
38    fi
39    if modprobe --dry-run --first-time vsock_loopback &>/dev/null ; then
40        echo "Module vsock_loopback is not loaded. Attempting to load..."
41        if ! sudo modprobe vsock_loopback ; then
42            echo -e "${RED}Module vsock_loopback is not loaded and attempt to load failed.${NO_COLOR}"
43            exit 1
44        fi
45    fi
46
47    DOCKER_RUN_FLAGS="--interactive --tty"
48
49    DOCKER_FILE="$1"
50    if [ ! -f "${DOCKER_FILE}" ]; then
51        echo -e "${RED}Docker file '${DOCKER_FILE}' doesn't exist. Please provide one as an argument.${NO_COLOR}"
52        exit 1
53    fi
54
55    if [ ! -d "${ANDROID_BUILD_TOP}" ]; then
56        echo -e "${RED}ANDROID_BUILD_TOP doesn't exist. Please lunch some target.${NO_COLOR}"
57        exit 1
58    fi
59    ${ANDROID_BUILD_TOP}/build/soong/soong_ui.bash --make-mode binder_sdk
60    BINDER_SDK_ZIP="${ANDROID_BUILD_TOP}/out/soong/.intermediates/frameworks/native/libs/binder/binder_sdk/linux_glibc_x86_64/binder_sdk.zip"
61    DOCKER_PATH="$(dirname $(ls -1 ${BINDER_SDK_ZIP} | head --lines=1))"
62fi
63
64function cleanup {
65    docker rmi --force "${DOCKER_TAG}" 2>/dev/null || true
66}
67trap cleanup EXIT
68
69docker build --force-rm --tag "${DOCKER_TAG}" --file ${DOCKER_FILE} ${DOCKER_PATH}
70docker run ${DOCKER_RUN_FLAGS} --rm "${DOCKER_TAG}"
71