albaltai | 64f3d59 | 2019-09-12 11:49:05 +0800 | [diff] [blame] | 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 | |
Jim Tang | 4b54e1b | 2019-10-15 17:03:28 +0800 | [diff] [blame^] | 17 | # Variables that will be used inside of containers. |
| 18 | SRCTOP="/home/$USER/aosp" |
| 19 | TARGET="aosp_cf_x86_phone-userdebug" |
| 20 | # Variables that will be used when creating docker image. |
| 21 | IMAGE="aosp/asuite" |
| 22 | DOCKERFILE_DIR=$(realpath $(dirname $0)/..) |
| 23 | # Whenever amending Dockerfile, update the md5 checksum accordingly. |
| 24 | DOCKERFILE_MD5SUM="40ffbf07477406a7cd6c709cb7ee6c3a" |
| 25 | SLIENCE_FLAG='>/dev/null 2>&1' |
| 26 | |
| 27 | # The core functional tests for AIDEGen. |
albaltai | 64f3d59 | 2019-09-12 11:49:05 +0800 | [diff] [blame] | 28 | function run_functiontests() { |
Jim Tang | 4b54e1b | 2019-10-15 17:03:28 +0800 | [diff] [blame^] | 29 | local target="aidegen_functional_test" |
| 30 | if ! m $target; then |
| 31 | echo -e "\n[Error] Fail to build $target.\n" |
albaltai | 64f3d59 | 2019-09-12 11:49:05 +0800 | [diff] [blame] | 32 | exit 1 |
| 33 | fi |
Jim Tang | 4b54e1b | 2019-10-15 17:03:28 +0800 | [diff] [blame^] | 34 | if ! ${target}-dev -b; then |
| 35 | echo -e "\n[Error] Fail to run ${target}-dev.\n" |
albaltai | 64f3d59 | 2019-09-12 11:49:05 +0800 | [diff] [blame] | 36 | exit 1 |
| 37 | fi |
| 38 | } |
| 39 | |
Jim Tang | 4b54e1b | 2019-10-15 17:03:28 +0800 | [diff] [blame^] | 40 | # Initialize $ANDROID_BUILD_TOP in container and install m(). |
albaltai | 64f3d59 | 2019-09-12 11:49:05 +0800 | [diff] [blame] | 41 | function check_env() { |
Jim Tang | 4b54e1b | 2019-10-15 17:03:28 +0800 | [diff] [blame^] | 42 | 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 |
albaltai | 64f3d59 | 2019-09-12 11:49:05 +0800 | [diff] [blame] | 52 | fi |
| 53 | function m() { |
Jim Tang | 4b54e1b | 2019-10-15 17:03:28 +0800 | [diff] [blame^] | 54 | echo "[Info] m $@" |
| 55 | ${ANDROID_BUILD_TOP}/build/soong/soong_ui.bash --make-mode "$@" |
albaltai | 64f3d59 | 2019-09-12 11:49:05 +0800 | [diff] [blame] | 56 | } |
Jim Tang | 4b54e1b | 2019-10-15 17:03:28 +0800 | [diff] [blame^] | 57 | [[ $(uname -s) = "Darwin" ]] && export IS_CONTAINER=true |
albaltai | 64f3d59 | 2019-09-12 11:49:05 +0800 | [diff] [blame] | 58 | } |
| 59 | |
Jim Tang | 4b54e1b | 2019-10-15 17:03:28 +0800 | [diff] [blame^] | 60 | # TODO: Move this method to asuite.sh. |
| 61 | function 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. |
| 75 | function 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. |
| 81 | function 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. |
| 93 | function 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. |
| 108 | function 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 | |
| 121 | main |