xref: /aosp_15_r20/prebuilts/asuite/aidegen/smoke_tests (revision b3adc34a6be591bf5fd4943dfdce52f3982696f0)
1#!/usr/bin/env bash
2#
3# Copyright 2019, 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# Variables that will be used inside of containers.
18SRCTOP="/home/$USER/aosp"
19TARGET="aosp_cf_x86_phone-userdebug"
20# Variables that will be used when creating docker image.
21IMAGE="aosp/asuite"
22DOCKERFILE_DIR=$(realpath $(dirname $0)/..)
23
24# The core functional tests for AIDEGen.
25function run_functiontests() {
26    local target="aidegen_functional_test"
27    if ! m $target; then
28        echo -e "\n[Error] Fail to build $target.\n"
29        exit 1
30    fi
31    if ! ${target}-dev -b; then
32        echo -e "\n[Error] Fail to run ${target}-dev.\n"
33        exit 1
34    fi
35}
36
37# Initialize $ANDROID_BUILD_TOP in container and install m().
38function check_env() {
39    if [[ -z "$ANDROID_BUILD_TOP" ]]; then
40        if [[ "$IS_CONTAINER" = "true" ]]; then
41            pushd "$SRCTOP"
42            source build/envsetup.sh && lunch $TARGET
43            popd
44        else
45            echo -ne "\n[Error] Missing \$ANDROID_BUILD_TOP variable. "
46            echo -e "Please run 'lunch' first.\n"
47            exit 1
48        fi
49    fi
50    function m() {
51        echo "[Info] m $@"
52        ${ANDROID_BUILD_TOP}/build/soong/soong_ui.bash --make-mode "$@"
53    }
54    [[ $(uname -s) = "Darwin" ]] && export IS_CONTAINER=true
55}
56
57# TODO: Move this method to asuite.sh.
58function build_docker_image() {
59    echo "[Info] Start building Docker image $IMAGE..."
60    build_cmd="docker build --rm --force-rm --no-cache \
61        --build-arg USER=$USER \
62        --build-arg UID=$UID \
63        --build-arg SRCTOP=$SRCTOP \
64        -t $IMAGE $DOCKERFILE_DIR"
65    if ! eval $build_cmd; then
66        echo -e "\n[Error] Failed to build docker image."
67        exit 1
68    fi
69}
70
71# TODO: Move this method to asuite.sh.
72function run_docker_instance() {
73    echo "[Info] Start a Docker container..."
74    docker run --rm -v $ANDROID_BUILD_TOP:$SRCTOP $IMAGE prebuilts/asuite/aidegen/smoke_tests
75}
76
77# TODO: Move this method to asuite.sh.
78function has_docker() {
79    [[ "$IS_CONTAINER" = "true" ]] && return 1
80    if ! systemctl is-active docker -q; then
81        echo "[Error] Docker daemon not running."
82        exit 1
83    elif ! docker ps -q 2>/dev/null; then
84        echo "[Error] $USER not in docker group."
85        exit 1
86    fi
87}
88
89# TODO: Move this method to asuite.sh.
90function has_docker_image() {
91    image_id=$(docker images --filter=reference=$IMAGE --format "{{.ID}}")
92    if [[ -z $image_id ]]; then
93        echo "[Info] Docker image $IMAGE not found."
94        return 1
95    fi
96}
97
98function helper() {
99    cat << END
100Usage:
101
102smoke_tests: run aidegen functional tests. If docker is unavailable, run
103             functional test directly; otherwise build necessary image before
104             running tests.
105smoke_tests -m: force rebuilding a new image and running smoke testing,
106                especially after a new Dockerfile has been updated.
107
108Whenever needs to access the instance for investigation, please issue:
109    docker run -it -v $ANDROID_BUILD_TOP:$SRCTOP $IMAGE bash
110
111END
112    exit 0
113}
114
115# Run functional tests directly when in MacOS or in a container.
116# Pass IS_CONTAINER=true to run functional tests when Docker is installed on Linux.
117function main() {
118    check_env
119    while getopts ":m" opt; do
120        case "$opt" in
121            m) has_docker && build_docker_image;;
122            *) helper;;
123        esac
124    done
125    if ! has_docker; then
126        m clean
127        run_functiontests
128    else
129        if ! has_docker_image; then
130            build_docker_image
131        fi
132        run_docker_instance
133    fi
134}
135
136main "$@"
137