runtest.sh 5.9 KB
Newer Older
M
mamingshuai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
#!/bin/bash

# Copyright (C) 2021 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -e

usage()
{
    echo
    echo "USAGE"
    echo "       ./runtest.sh device=IP:PORT serial=[SERIAL_NUMBER] [module=MODULE_NAME] [test=TEST] [runonly=RUN_ONLY]"
    echo "                  First, you must be execute stf/start.bat in the windows which connected a device."
    echo "                  target_platform : TARGET_PLATFORM the target platform, such as phone or ivi; Default to phone"
    echo "                  device    : IP:PORT  the host ip:port of the connected device."
    echo "                  module    : MODULE_NAME the module name to run. Run all modules by default."
    echo "                  test      : TEST_NAME the test name to run. Run all tests by default. This should be a FULL-QUALIFIED className or methodName."
    echo "                  runonly   : RUN_ONLY TRUE=not build, only runtest; FALSE=build and runtest. FALSE by default."
    echo
    exit 1
}

parse_cmdline()
{
    echo parse_cmdline:$@
    SCRIPT_DIR=$(cd $(dirname $0); pwd)
    BUILD_SCRIPT=${SCRIPT_DIR}/build.sh
    BASE_HOME=${SCRIPT_DIR}/../../..
    OUT_DIR=${SCRIPT_DIR}/../../../out
    TARGET_ARCH=arm64
    TARGET_PLATFORM=all
    BUILD_VARIANT=release
    MODULE_NAME=""
    DEVICE_SN=""
    TEST_NAME=""
    RUN_ONLY=FALSE
    BUILD_PARAM=""
    ACTS_HOME=$SCRIPT_DIR/../../../out/$BUILD_VARIANT/suites/acts
    MODULE_INFO_FILE=$SCRIPT_DIR/../../../out/$BUILD_VARIANT/suites/acts/testcases/module_info.list
    export PATH=${BASE_HOME}/prebuilts/python/linux-x86/3.8.5/bin:$PATH
    BUILD_PARAM="$BUILD_PARAM target_arch=$TARGET_ARCH variant=$BUILD_VARIANT"
    
    while [ -n "$1" ]
    do
        var="$1"
        OPTIONS=$(echo ${var%%=*})
        PARAM=$(echo ${var#*=})
        case "$OPTIONS" in
        target_platform) TARGET_PLATFORM="$PARAM"
                         BUILD_PARAM="$BUILD_PARAM target_platform=$TARGET_PLATFORM"
                         ;;
        module)          MODULE_NAME="$PARAM"
                         BUILD_PARAM="$BUILD_PARAM suite=$MODULE_NAME"
                         ;;
        test)            TEST_NAME="$PARAM"
                         ;;
        device)          DEVICE_SN="$DEVICE_SN;$PARAM"
                         ;;
        runonly)         RUN_ONLY=$(echo $PARAM |tr [a-z] [A-Z])
                         ;;
        *)   usage
             break;;
        esac
        shift
    done
    if [ "$DEVICE_SN" == "" ];then
        usage
    fi
}

do_make()
{
    cd $BASE_HOME
    echo "================================"
    echo "start to build"
    echo "$BUILD_SCRIPT $BUILD_PARAM"
    echo "================================"
    
    echo BUILD_PARAM= $BUILD_PARAM
    $BUILD_SCRIPT $BUILD_PARAM
    if [ "$?" != 0 ]; then
        echo -e "\033[31mbuild error!\033[0m"
        exit 1
    fi
}

get_testmodule_name()
{
    BUILD_TARGET_NAME=$1
    # map from build_target name to actual testmodule name
    test_module_name=""
    OLD_IFS=$IFS
    IFS=$'\n'
    for line in $(cat $MODULE_INFO_FILE); do
        VAR1=$(echo $line |awk -F ' ' '{print $1}')
        VAR2=$(echo $line |awk -F ' ' '{print $2}')
        if [ "$BUILD_TARGET_NAME" == "$VAR1" ] ; then
           test_module_name=$VAR2
           break
        fi
    done
    IFS=$OLD_IFS
    echo $test_module_name
}

run_test_with_xdevice()
{
    TC_PATH=$ACTS_HOME/testcases
    RES_PATH=$ACTS_HOME/resource/
    REPORT_PATH=$ACTS_HOME/xdevice_reports/$(date "+%Y.%m.%d-%H.%M.%S")
    python -m easy_install --quiet $ACTS_HOME/tools/xdevice-0.0.0.tar.gz
    python -m easy_install --quiet $ACTS_HOME/tools/xdevice-extension-0.0.0.tar.gz

    run_arguments="run acts -tcpath $TC_PATH -respath $RES_PATH -rp $REPORT_PATH"
    if [ ! -z "$MODULE_NAME" ]; then
        run_arguments="$run_arguments -l $1"
    fi
    if [ ! -z "$TEST_NAME" ]; then
        TEST_LIST_FILE=$ACTS_HOME/include_tests
        echo "$TEST_NAME" > $TEST_LIST_FILE
        run_arguments="$run_arguments -ta test-file-include-filter:$TEST_LIST_FILE"
    fi
    if [ ! -z "$DEVICE_SN" ]; then
        run_arguments="$run_arguments $(echo $DEVICE_SN | sed 's/;/-sn /')"
    fi

    echo $run_arguments
    cd $ACTS_HOME
    python -m xdevice "$run_arguments"

    return $?
}

run_test()
{
    cd $BASE_HOME

    MAPPED_MODULE_NAME=$MODULE_NAME
    # map from build_target name to actual testmodule name
    if [ ! -z "$MODULE_NAME" ]; then
        MAPPED_MODULE_NAME=$(get_testmodule_name $MODULE_NAME $ACTS_HOME)
        if [ ! -z "$MAPPED_MODULE_NAME" ]; then
          echo -e "\033[32mTest module name of build_target "$MODULE_NAME is" "$MAPPED_MODULE_NAME"\033[0m"
        else
          echo -e "\033[31mTest module "$MODULE_NAME" dose not exist!\033[0m"
          exit 1
        fi
    fi

    JSON_CONFIG_FILE=$ACTS_HOME/testcases/$MAPPED_MODULE_NAME.json
    ret_code=0
    if [ -f "$JSON_CONFIG_FILE" ]; then 
        echo -e "\033[32mRun $MAPPED_MODULE_NAME with xdevice ...\033[0m"
        run_test_with_xdevice $MAPPED_MODULE_NAME
        ret_code=$?
    elif [ -z "$MODULE_NAME" ]; then
        echo -e "\033[32mRun all modules with xdevice ...\033[0m"
        run_test_with_xdevice
        ret_code=$?
    else
        echo -e "\033[31mNon-xdevice module in acts!\033[0m"
        exit 1
    fi
 
    if [ "$ret_code" != "0" ]; then
        echo -e "\033[31mFailed to run test, ret=$ret_code\033[0m"
        exit 1
    fi
}

parse_cmdline $@
if [ "$RUN_ONLY" == FALSE ];then
    do_make
fi
mkdir -p $OUT_DIR/$BUILD_VARIANT/suites/acts/resource/tools
run_test
exit 0