19762338dSopenharmony_ci#!/bin/bash
29762338dSopenharmony_ci
39762338dSopenharmony_ci# Copyright (C) 2021 Huawei Device Co., Ltd.
49762338dSopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
59762338dSopenharmony_ci# you may not use this file except in compliance with the License.
69762338dSopenharmony_ci# You may obtain a copy of the License at
79762338dSopenharmony_ci#
89762338dSopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
99762338dSopenharmony_ci#
109762338dSopenharmony_ci# Unless required by applicable law or agreed to in writing, software
119762338dSopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
129762338dSopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139762338dSopenharmony_ci# See the License for the specific language governing permissions and
149762338dSopenharmony_ci# limitations under the License.
159762338dSopenharmony_ci
169762338dSopenharmony_ciset -e
179762338dSopenharmony_ci
189762338dSopenharmony_ciusage()
199762338dSopenharmony_ci{
209762338dSopenharmony_ci    echo
219762338dSopenharmony_ci    echo "USAGE"
229762338dSopenharmony_ci    echo "       ./runtest.sh device=IP:PORT serial=[SERIAL_NUMBER] [module=MODULE_NAME] [test=TEST] [runonly=RUN_ONLY]"
239762338dSopenharmony_ci    echo "                  First, you must be execute stf/start.bat in the windows which connected a device."
249762338dSopenharmony_ci    echo "                  target_platform : TARGET_PLATFORM the target platform, such as phone or ivi; Default to phone"
259762338dSopenharmony_ci    echo "                  device    : IP:PORT  the host ip:port of the connected device."
269762338dSopenharmony_ci    echo "                  module    : MODULE_NAME the module name to run. Run all modules by default."
279762338dSopenharmony_ci    echo "                  test      : TEST_NAME the test name to run. Run all tests by default. This should be a FULL-QUALIFIED className or methodName."
289762338dSopenharmony_ci    echo "                  runonly   : RUN_ONLY TRUE=not build, only runtest; FALSE=build and runtest. FALSE by default."
299762338dSopenharmony_ci    echo
309762338dSopenharmony_ci    exit 1
319762338dSopenharmony_ci}
329762338dSopenharmony_ci
339762338dSopenharmony_ciparse_cmdline()
349762338dSopenharmony_ci{
359762338dSopenharmony_ci    echo parse_cmdline:$@
369762338dSopenharmony_ci    SCRIPT_DIR=$(cd $(dirname $0); pwd)
379762338dSopenharmony_ci    BUILD_SCRIPT=${SCRIPT_DIR}/build.sh
389762338dSopenharmony_ci    BASE_HOME=${SCRIPT_DIR}/../../..
399762338dSopenharmony_ci    OUT_DIR=${SCRIPT_DIR}/../../../out
409762338dSopenharmony_ci    TARGET_ARCH=arm64
419762338dSopenharmony_ci    TARGET_PLATFORM=all
429762338dSopenharmony_ci    BUILD_VARIANT=release
439762338dSopenharmony_ci    MODULE_NAME=""
449762338dSopenharmony_ci    DEVICE_SN=""
459762338dSopenharmony_ci    TEST_NAME=""
469762338dSopenharmony_ci    RUN_ONLY=FALSE
479762338dSopenharmony_ci    BUILD_PARAM=""
489762338dSopenharmony_ci    HATS_HOME=$SCRIPT_DIR/../../../out/$BUILD_VARIANT/suites/hats
499762338dSopenharmony_ci    MODULE_INFO_FILE=$SCRIPT_DIR/../../../out/$BUILD_VARIANT/suites/hats/testcases/module_info.list
509762338dSopenharmony_ci    export PATH=${BASE_HOME}/prebuilts/python/linux-x86/3.8.5/bin:$PATH
519762338dSopenharmony_ci    BUILD_PARAM="$BUILD_PARAM target_arch=$TARGET_ARCH variant=$BUILD_VARIANT"
529762338dSopenharmony_ci    
539762338dSopenharmony_ci    while [ -n "$1" ]
549762338dSopenharmony_ci    do
559762338dSopenharmony_ci        var="$1"
569762338dSopenharmony_ci        OPTIONS=$(echo ${var%%=*})
579762338dSopenharmony_ci        PARAM=$(echo ${var#*=})
589762338dSopenharmony_ci        case "$OPTIONS" in
599762338dSopenharmony_ci        target_platform) TARGET_PLATFORM="$PARAM"
609762338dSopenharmony_ci                         BUILD_PARAM="$BUILD_PARAM target_platform=$TARGET_PLATFORM"
619762338dSopenharmony_ci                         ;;
629762338dSopenharmony_ci        module)          MODULE_NAME="$PARAM"
639762338dSopenharmony_ci                         BUILD_PARAM="$BUILD_PARAM suite=$MODULE_NAME"
649762338dSopenharmony_ci                         ;;
659762338dSopenharmony_ci        test)            TEST_NAME="$PARAM"
669762338dSopenharmony_ci                         ;;
679762338dSopenharmony_ci        device)          DEVICE_SN="$DEVICE_SN;$PARAM"
689762338dSopenharmony_ci                         ;;
699762338dSopenharmony_ci        runonly)         RUN_ONLY=$(echo $PARAM |tr [a-z] [A-Z])
709762338dSopenharmony_ci                         ;;
719762338dSopenharmony_ci        *)   usage
729762338dSopenharmony_ci             break;;
739762338dSopenharmony_ci        esac
749762338dSopenharmony_ci        shift
759762338dSopenharmony_ci    done
769762338dSopenharmony_ci    if [ "$DEVICE_SN" == "" ];then
779762338dSopenharmony_ci        usage
789762338dSopenharmony_ci    fi
799762338dSopenharmony_ci}
809762338dSopenharmony_ci
819762338dSopenharmony_cido_make()
829762338dSopenharmony_ci{
839762338dSopenharmony_ci    cd $BASE_HOME
849762338dSopenharmony_ci    echo "================================"
859762338dSopenharmony_ci    echo "start to build"
869762338dSopenharmony_ci    echo "$BUILD_SCRIPT $BUILD_PARAM"
879762338dSopenharmony_ci    echo "================================"
889762338dSopenharmony_ci    
899762338dSopenharmony_ci    echo BUILD_PARAM= $BUILD_PARAM
909762338dSopenharmony_ci    $BUILD_SCRIPT $BUILD_PARAM
919762338dSopenharmony_ci    if [ "$?" != 0 ]; then
929762338dSopenharmony_ci        echo -e "\033[31mbuild error!\033[0m"
939762338dSopenharmony_ci        exit 1
949762338dSopenharmony_ci    fi
959762338dSopenharmony_ci}
969762338dSopenharmony_ci
979762338dSopenharmony_ciget_testmodule_name()
989762338dSopenharmony_ci{
999762338dSopenharmony_ci    BUILD_TARGET_NAME=$1
1009762338dSopenharmony_ci    # map from build_target name to actual testmodule name
1019762338dSopenharmony_ci    test_module_name=""
1029762338dSopenharmony_ci    OLD_IFS=$IFS
1039762338dSopenharmony_ci    IFS=$'\n'
1049762338dSopenharmony_ci    for line in $(cat $MODULE_INFO_FILE); do
1059762338dSopenharmony_ci        VAR1=$(echo $line |awk -F ' ' '{print $1}')
1069762338dSopenharmony_ci        VAR2=$(echo $line |awk -F ' ' '{print $2}')
1079762338dSopenharmony_ci        if [ "$BUILD_TARGET_NAME" == "$VAR1" ] ; then
1089762338dSopenharmony_ci           test_module_name=$VAR2
1099762338dSopenharmony_ci           break
1109762338dSopenharmony_ci        fi
1119762338dSopenharmony_ci    done
1129762338dSopenharmony_ci    IFS=$OLD_IFS
1139762338dSopenharmony_ci    echo $test_module_name
1149762338dSopenharmony_ci}
1159762338dSopenharmony_ci
1169762338dSopenharmony_cirun_test_with_xdevice()
1179762338dSopenharmony_ci{
1189762338dSopenharmony_ci    TC_PATH=$HATS_HOME/testcases
1199762338dSopenharmony_ci    RES_PATH=$HATS_HOME/resource/
1209762338dSopenharmony_ci    REPORT_PATH=$HATS_HOME/xdevice_reports/$(date "+%Y.%m.%d-%H.%M.%S")
1219762338dSopenharmony_ci    python -m easy_install --quiet $HATS_HOME/tools/xdevice-0.0.0.tar.gz
1229762338dSopenharmony_ci    python -m easy_install --quiet $HATS_HOME/tools/xdevice-extension-0.0.0.tar.gz
1239762338dSopenharmony_ci
1249762338dSopenharmony_ci    run_arguments="run hats -tcpath $TC_PATH -respath $RES_PATH -rp $REPORT_PATH"
1259762338dSopenharmony_ci    if [ ! -z "$MODULE_NAME" ]; then
1269762338dSopenharmony_ci        run_arguments="$run_arguments -l $1"
1279762338dSopenharmony_ci    fi
1289762338dSopenharmony_ci    if [ ! -z "$TEST_NAME" ]; then
1299762338dSopenharmony_ci        TEST_LIST_FILE=$HATS_HOME/include_tests
1309762338dSopenharmony_ci        echo "$TEST_NAME" > $TEST_LIST_FILE
1319762338dSopenharmony_ci        run_arguments="$run_arguments -ta test-file-include-filter:$TEST_LIST_FILE"
1329762338dSopenharmony_ci    fi
1339762338dSopenharmony_ci    if [ ! -z "$DEVICE_SN" ]; then
1349762338dSopenharmony_ci        run_arguments="$run_arguments $(echo $DEVICE_SN | sed 's/;/-sn /')"
1359762338dSopenharmony_ci    fi
1369762338dSopenharmony_ci
1379762338dSopenharmony_ci    echo $run_arguments
1389762338dSopenharmony_ci    cd $HATS_HOME
1399762338dSopenharmony_ci    python -m xdevice "$run_arguments"
1409762338dSopenharmony_ci
1419762338dSopenharmony_ci    return $?
1429762338dSopenharmony_ci}
1439762338dSopenharmony_ci
1449762338dSopenharmony_cirun_test()
1459762338dSopenharmony_ci{
1469762338dSopenharmony_ci    cd $BASE_HOME
1479762338dSopenharmony_ci
1489762338dSopenharmony_ci    MAPPED_MODULE_NAME=$MODULE_NAME
1499762338dSopenharmony_ci    # map from build_target name to actual testmodule name
1509762338dSopenharmony_ci    if [ ! -z "$MODULE_NAME" ]; then
1519762338dSopenharmony_ci        MAPPED_MODULE_NAME=$(get_testmodule_name $MODULE_NAME $HATS_HOME)
1529762338dSopenharmony_ci        if [ ! -z "$MAPPED_MODULE_NAME" ]; then
1539762338dSopenharmony_ci          echo -e "\033[32mTest module name of build_target "$MODULE_NAME is" "$MAPPED_MODULE_NAME"\033[0m"
1549762338dSopenharmony_ci        else
1559762338dSopenharmony_ci          echo -e "\033[31mTest module "$MODULE_NAME" dose not exist!\033[0m"
1569762338dSopenharmony_ci          exit 1
1579762338dSopenharmony_ci        fi
1589762338dSopenharmony_ci    fi
1599762338dSopenharmony_ci
1609762338dSopenharmony_ci    JSON_CONFIG_FILE=$HATS_HOME/testcases/$MAPPED_MODULE_NAME.json
1619762338dSopenharmony_ci    ret_code=0
1629762338dSopenharmony_ci    if [ -f "$JSON_CONFIG_FILE" ]; then 
1639762338dSopenharmony_ci        echo -e "\033[32mRun $MAPPED_MODULE_NAME with xdevice ...\033[0m"
1649762338dSopenharmony_ci        run_test_with_xdevice $MAPPED_MODULE_NAME
1659762338dSopenharmony_ci        ret_code=$?
1669762338dSopenharmony_ci    elif [ -z "$MODULE_NAME" ]; then
1679762338dSopenharmony_ci        echo -e "\033[32mRun all modules with xdevice ...\033[0m"
1689762338dSopenharmony_ci        run_test_with_xdevice
1699762338dSopenharmony_ci        ret_code=$?
1709762338dSopenharmony_ci    else
1719762338dSopenharmony_ci        echo -e "\033[31mNon-xdevice module in hats!\033[0m"
1729762338dSopenharmony_ci        exit 1
1739762338dSopenharmony_ci    fi
1749762338dSopenharmony_ci 
1759762338dSopenharmony_ci    if [ "$ret_code" != "0" ]; then
1769762338dSopenharmony_ci        echo -e "\033[31mFailed to run test, ret=$ret_code\033[0m"
1779762338dSopenharmony_ci        exit 1
1789762338dSopenharmony_ci    fi
1799762338dSopenharmony_ci}
1809762338dSopenharmony_ci
1819762338dSopenharmony_ciparse_cmdline $@
1829762338dSopenharmony_ciif [ "$RUN_ONLY" == FALSE ];then
1839762338dSopenharmony_ci    do_make
1849762338dSopenharmony_cifi
1859762338dSopenharmony_cimkdir -p $OUT_DIR/$BUILD_VARIANT/suites/hats/resource/tools
1869762338dSopenharmony_cirun_test
1879762338dSopenharmony_ciexit 0
188