blob: 590c52d529791ef622d98bb3b915196882848fad [file] [log] [blame]
albaltai64f3d592019-09-12 11:49:05 +08001#!/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
Jim Tang4b54e1b2019-10-15 17:03:28 +080017# 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)/..)
Jim Tang4b54e1b2019-10-15 17:03:28 +080023
24# The core functional tests for AIDEGen.
albaltai64f3d592019-09-12 11:49:05 +080025function run_functiontests() {
Jim Tang4b54e1b2019-10-15 17:03:28 +080026 local target="aidegen_functional_test"
27 if ! m $target; then
28 echo -e "\n[Error] Fail to build $target.\n"
albaltai64f3d592019-09-12 11:49:05 +080029 exit 1
30 fi
Jim Tang4b54e1b2019-10-15 17:03:28 +080031 if ! ${target}-dev -b; then
32 echo -e "\n[Error] Fail to run ${target}-dev.\n"
albaltai64f3d592019-09-12 11:49:05 +080033 exit 1
34 fi
35}
36
Jim Tang4b54e1b2019-10-15 17:03:28 +080037# Initialize $ANDROID_BUILD_TOP in container and install m().
albaltai64f3d592019-09-12 11:49:05 +080038function check_env() {
Jim Tang4b54e1b2019-10-15 17:03:28 +080039 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
albaltai64f3d592019-09-12 11:49:05 +080049 fi
50 function m() {
Jim Tang4b54e1b2019-10-15 17:03:28 +080051 echo "[Info] m $@"
52 ${ANDROID_BUILD_TOP}/build/soong/soong_ui.bash --make-mode "$@"
albaltai64f3d592019-09-12 11:49:05 +080053 }
Jim Tang4b54e1b2019-10-15 17:03:28 +080054 [[ $(uname -s) = "Darwin" ]] && export IS_CONTAINER=true
albaltai64f3d592019-09-12 11:49:05 +080055}
56
Jim Tang4b54e1b2019-10-15 17:03:28 +080057# 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..."
Jim Tang30627f52022-01-25 10:35:49 +080074 docker run --rm -v $ANDROID_BUILD_TOP:$SRCTOP $IMAGE prebuilts/asuite/aidegen/smoke_tests
Jim Tang4b54e1b2019-10-15 17:03:28 +080075}
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
Jim Tangdb963c32020-08-10 23:16:22 +080083 elif ! docker ps -q 2>/dev/null; then
Jim Tang4b54e1b2019-10-15 17:03:28 +080084 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
Jim Tangdb963c32020-08-10 23:16:22 +080096}
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
Jim Tang4b54e1b2019-10-15 17:03:28 +0800113}
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
Jim Tangdb963c32020-08-10 23:16:22 +0800119 while getopts ":m" opt; do
120 case "$opt" in
121 m) has_docker && build_docker_image;;
122 *) helper;;
123 esac
124 done
Jim Tang4b54e1b2019-10-15 17:03:28 +0800125 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
Jim Tangdb963c32020-08-10 23:16:22 +0800136main "$@"