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)/..) |
Jim Tang | 4b54e1b | 2019-10-15 17:03:28 +0800 | [diff] [blame] | 23 | |
| 24 | # The core functional tests for AIDEGen. |
albaltai | 64f3d59 | 2019-09-12 11:49:05 +0800 | [diff] [blame] | 25 | function run_functiontests() { |
Jim Tang | 4b54e1b | 2019-10-15 17:03:28 +0800 | [diff] [blame] | 26 | local target="aidegen_functional_test" |
| 27 | if ! m $target; then |
| 28 | echo -e "\n[Error] Fail to build $target.\n" |
albaltai | 64f3d59 | 2019-09-12 11:49:05 +0800 | [diff] [blame] | 29 | exit 1 |
| 30 | fi |
Jim Tang | 4b54e1b | 2019-10-15 17:03:28 +0800 | [diff] [blame] | 31 | if ! ${target}-dev -b; then |
| 32 | echo -e "\n[Error] Fail to run ${target}-dev.\n" |
albaltai | 64f3d59 | 2019-09-12 11:49:05 +0800 | [diff] [blame] | 33 | exit 1 |
| 34 | fi |
| 35 | } |
| 36 | |
Jim Tang | 4b54e1b | 2019-10-15 17:03:28 +0800 | [diff] [blame] | 37 | # Initialize $ANDROID_BUILD_TOP in container and install m(). |
albaltai | 64f3d59 | 2019-09-12 11:49:05 +0800 | [diff] [blame] | 38 | function check_env() { |
Jim Tang | 4b54e1b | 2019-10-15 17:03:28 +0800 | [diff] [blame] | 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 |
albaltai | 64f3d59 | 2019-09-12 11:49:05 +0800 | [diff] [blame] | 49 | fi |
| 50 | function m() { |
Jim Tang | 4b54e1b | 2019-10-15 17:03:28 +0800 | [diff] [blame] | 51 | echo "[Info] m $@" |
| 52 | ${ANDROID_BUILD_TOP}/build/soong/soong_ui.bash --make-mode "$@" |
albaltai | 64f3d59 | 2019-09-12 11:49:05 +0800 | [diff] [blame] | 53 | } |
Jim Tang | 4b54e1b | 2019-10-15 17:03:28 +0800 | [diff] [blame] | 54 | [[ $(uname -s) = "Darwin" ]] && export IS_CONTAINER=true |
albaltai | 64f3d59 | 2019-09-12 11:49:05 +0800 | [diff] [blame] | 55 | } |
| 56 | |
Jim Tang | 4b54e1b | 2019-10-15 17:03:28 +0800 | [diff] [blame] | 57 | # TODO: Move this method to asuite.sh. |
| 58 | function 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. |
| 72 | function run_docker_instance() { |
| 73 | echo "[Info] Start a Docker container..." |
Jim Tang | 30627f5 | 2022-01-25 10:35:49 +0800 | [diff] [blame^] | 74 | docker run --rm -v $ANDROID_BUILD_TOP:$SRCTOP $IMAGE prebuilts/asuite/aidegen/smoke_tests |
Jim Tang | 4b54e1b | 2019-10-15 17:03:28 +0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | # TODO: Move this method to asuite.sh. |
| 78 | function 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 Tang | db963c3 | 2020-08-10 23:16:22 +0800 | [diff] [blame] | 83 | elif ! docker ps -q 2>/dev/null; then |
Jim Tang | 4b54e1b | 2019-10-15 17:03:28 +0800 | [diff] [blame] | 84 | echo "[Error] $USER not in docker group." |
| 85 | exit 1 |
| 86 | fi |
| 87 | } |
| 88 | |
| 89 | # TODO: Move this method to asuite.sh. |
| 90 | function 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 Tang | db963c3 | 2020-08-10 23:16:22 +0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | function helper() { |
| 99 | cat << END |
| 100 | Usage: |
| 101 | |
| 102 | smoke_tests: run aidegen functional tests. If docker is unavailable, run |
| 103 | functional test directly; otherwise build necessary image before |
| 104 | running tests. |
| 105 | smoke_tests -m: force rebuilding a new image and running smoke testing, |
| 106 | especially after a new Dockerfile has been updated. |
| 107 | |
| 108 | Whenever needs to access the instance for investigation, please issue: |
| 109 | docker run -it -v $ANDROID_BUILD_TOP:$SRCTOP $IMAGE bash |
| 110 | |
| 111 | END |
| 112 | exit 0 |
Jim Tang | 4b54e1b | 2019-10-15 17:03:28 +0800 | [diff] [blame] | 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. |
| 117 | function main() { |
| 118 | check_env |
Jim Tang | db963c3 | 2020-08-10 23:16:22 +0800 | [diff] [blame] | 119 | while getopts ":m" opt; do |
| 120 | case "$opt" in |
| 121 | m) has_docker && build_docker_image;; |
| 122 | *) helper;; |
| 123 | esac |
| 124 | done |
Jim Tang | 4b54e1b | 2019-10-15 17:03:28 +0800 | [diff] [blame] | 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 | |
Jim Tang | db963c3 | 2020-08-10 23:16:22 +0800 | [diff] [blame] | 136 | main "$@" |