blob: bffa606a713512e3f30bc7f99b35f148944f0d39 [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)/..)
23# Whenever amending Dockerfile, update the md5 checksum accordingly.
24DOCKERFILE_MD5SUM="40ffbf07477406a7cd6c709cb7ee6c3a"
25SLIENCE_FLAG='>/dev/null 2>&1'
26
27# The core functional tests for AIDEGen.
albaltai64f3d592019-09-12 11:49:05 +080028function run_functiontests() {
Jim Tang4b54e1b2019-10-15 17:03:28 +080029 local target="aidegen_functional_test"
30 if ! m $target; then
31 echo -e "\n[Error] Fail to build $target.\n"
albaltai64f3d592019-09-12 11:49:05 +080032 exit 1
33 fi
Jim Tang4b54e1b2019-10-15 17:03:28 +080034 if ! ${target}-dev -b; then
35 echo -e "\n[Error] Fail to run ${target}-dev.\n"
albaltai64f3d592019-09-12 11:49:05 +080036 exit 1
37 fi
38}
39
Jim Tang4b54e1b2019-10-15 17:03:28 +080040# Initialize $ANDROID_BUILD_TOP in container and install m().
albaltai64f3d592019-09-12 11:49:05 +080041function check_env() {
Jim Tang4b54e1b2019-10-15 17:03:28 +080042 if [[ -z "$ANDROID_BUILD_TOP" ]]; then
43 if [[ "$IS_CONTAINER" = "true" ]]; then
44 pushd "$SRCTOP"
45 source build/envsetup.sh && lunch $TARGET
46 popd
47 else
48 echo -ne "\n[Error] Missing \$ANDROID_BUILD_TOP variable. "
49 echo -e "Please run 'lunch' first.\n"
50 exit 1
51 fi
albaltai64f3d592019-09-12 11:49:05 +080052 fi
53 function m() {
Jim Tang4b54e1b2019-10-15 17:03:28 +080054 echo "[Info] m $@"
55 ${ANDROID_BUILD_TOP}/build/soong/soong_ui.bash --make-mode "$@"
albaltai64f3d592019-09-12 11:49:05 +080056 }
Jim Tang4b54e1b2019-10-15 17:03:28 +080057 [[ $(uname -s) = "Darwin" ]] && export IS_CONTAINER=true
albaltai64f3d592019-09-12 11:49:05 +080058}
59
Jim Tang4b54e1b2019-10-15 17:03:28 +080060# TODO: Move this method to asuite.sh.
61function build_docker_image() {
62 echo "[Info] Start building Docker image $IMAGE..."
63 build_cmd="docker build --rm --force-rm --no-cache \
64 --build-arg USER=$USER \
65 --build-arg UID=$UID \
66 --build-arg SRCTOP=$SRCTOP \
67 -t $IMAGE $DOCKERFILE_DIR"
68 if ! eval $build_cmd; then
69 echo -e "\n[Error] Failed to build docker image."
70 exit 1
71 fi
72}
73
74# TODO: Move this method to asuite.sh.
75function run_docker_instance() {
76 echo "[Info] Start a Docker container..."
77 docker run --rm -v $ANDROID_BUILD_TOP:$SRCTOP $IMAGE
78}
79
80# TODO: Move this method to asuite.sh.
81function has_docker() {
82 [[ "$IS_CONTAINER" = "true" ]] && return 1
83 if ! systemctl is-active docker -q; then
84 echo "[Error] Docker daemon not running."
85 exit 1
86 elif ! docker ps $SILENCE_FLAG; then
87 echo "[Error] $USER not in docker group."
88 exit 1
89 fi
90}
91
92# TODO: Move this method to asuite.sh.
93function has_docker_image() {
94 image_id=$(docker images --filter=reference=$IMAGE --format "{{.ID}}")
95 if [[ -z $image_id ]]; then
96 echo "[Info] Docker image $IMAGE not found."
97 return 1
98 fi
99 checksum=$(md5sum $DOCKERFILE_DIR/Dockerfile | awk '{print $1}')
100 if [[ $checksum != $DOCKERFILE_MD5SUM ]]; then
101 echo "[Info] Docker image $IMAGE is outdated."
102 return 1
103 fi
104}
105
106# Run functional tests directly when in MacOS or in a container.
107# Pass IS_CONTAINER=true to run functional tests when Docker is installed on Linux.
108function main() {
109 check_env
110 if ! has_docker; then
111 m clean
112 run_functiontests
113 else
114 if ! has_docker_image; then
115 build_docker_image
116 fi
117 run_docker_instance
118 fi
119}
120
121main