build.sh 14.1 KB
Newer Older
L
LiFeng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#!/bin/bash
#######################################################################
##- @Copyright (C) Huawei Technologies., Ltd. 2020. All rights reserved.
# - iSulad licensed under the Mulan PSL v2.
# - You can use this software according to the terms and conditions of the Mulan PSL v2.
# - You may obtain a copy of Mulan PSL v2 at:
# -     http://license.coscl.org.cn/MulanPSL2
# - THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# - IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# - PURPOSE.
# - See the Mulan PSL v2 for more details.
##- @Description: generate cetification
##- @Author: lifeng
##- @Create: 2020-03-30
#######################################################################

set +e
set -x

L
lifeng68 已提交
20
enable_gcov=OFF
L
LiFeng 已提交
21 22 23 24 25 26
ignore_ci=false
basepath=$(cd `dirname $0`; pwd)
source $basepath/helper.sh
TOPDIR=`pwd`
src_code_dir="$TOPDIR"
make_script="${TOPDIR}/CI/make-and-install.sh"
L
lifeng68 已提交
27
gcov_script="${TOPDIR}/CI/generate_gcov.sh"
L
LiFeng 已提交
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
CIDIR="$TOPDIR/CI"
testcase_script="${src_code_dir}/CI/run-testcases.sh"
testcase_test="${src_code_dir}/CI/test.sh"
testcase_data="/tmp/testcases_data"
LXC_LOCK_DIR_CONTAINER="/run/lxc/lock/mount_lock"
LXC_LOCK_DIR_HOST="/tmp/lxc_mount_dir"
KEEP_CONTAINERS_ALIVE_DIR="/tmp/containerslock"
TESTCASE_ASSIGN="${CIDIR}/testcase_assign"
BASE_IMAGE=""

rm -rf ${TESTCASE_ASSIGN}_*

# #Run this file will generate default BASE_IMAGE and auto run isulad unit tests
# #You should cd the root path of isulad, and run:
# ./CI/build.sh

declare -a modules
container_nums=0

function usage() {
    echo "Usage: $0 [options]"
    echo "Continuous integration (CI) script for isulad/lcr project"
    echo "Options:"
    echo "    -m, --module        Execute scripts related to the specified module"
    echo "    -n, --container-num Multiple containers execute scripts in parallel"
    echo "    -g, --enable-gcov   Enable gcov for code coverage analysis"
    echo "    -i, --ignore-ci     Not running testcase"
    echo "        --rm            Auto remove containers after testcase run success"
    echo "    -h, --help          Script help information"
}

function err() {
    echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $@" >&2
}

L
lifeng68 已提交
63
args=`getopt -o m:n:g:i:h --long module:,container-num:,enable-gcov:,ignore-ci:,help -- "$@"`
L
LiFeng 已提交
64 65 66 67 68 69 70
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
eval set -- "$args"

while true; do
    case "$1" in
        -m|--module)        modules=${2} ; modules=(${modules// / }) ; shift 2 ;;
        -n|--container-num) container_nums=${2} ; shift 2 ;;
L
lifeng68 已提交
71
        -g|--enable-gcov)   enable_gcov=${2} ; shift 2 ;;
L
LiFeng 已提交
72 73 74 75 76 77 78
        -i|--ignore-ci)     ignore_ci=${2} ; shift 2 ;;
        -h|--help)          usage ; exit 0 ;;
        --)                 shift ; break ;;
        *)                  err "invalid parameter" ; exit -1 ;;
    esac
done

L
lifeng68 已提交
79 80 81 82
if [[ "x${enable_gcov}" == "xON" ]]; then
  container_nums=1
fi

L
LiFeng 已提交
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
declare -A scripts
pwd
TESTCASE_PATH="./CI/test_cases"

for file in $(find ${TESTCASE_PATH} -not \( -path '.*/data' -prune \) -regextype posix-extended -regex ".*\.(bash)" | grep -v "helpers.bash" | sort)
do
    attributes=$(sed -n '3p' $file)
    if [[ "x$attributes" == "x" ]] || [[ ! "${attributes}" =~ "attributes:" ]];then
        attributes=$(cat $file | grep "# attributes:")
        if [[ "x$attributes" == "x" ]] || [[ ! "${attributes}" =~ "attributes:" ]];then
            continue
        fi
    fi
    attributes=${attributes#*: }
    attributes=(${attributes// / })
    if [[ ${#modules[@]} -ne 0 ]]; then
        intersection=($(comm -12 <(echo ${modules[*]}| tr " " "\n"| sort) <(echo ${attributes[*]} | tr " " "\n"| sort)| sort -g))
        if [[ ${#intersection[@]} -eq 0 ]]; then
            continue
        fi
    fi

    concurrent=$(sed -n '4p' $file)
    concurrent=${concurrent#*: }

    spend_time=$(sed -n '5p' $file)
    spend_time=${spend_time#*: }

    info=(${spend_time} ${concurrent} ${attributes[@]})
    scripts+=([${file}]=${info[@]})
done

function check_concurrent() {
    attr=${scripts[${1}]}
    attributes=(${attr// / })
    #disable concurrent run, testcase may fail while using sleep in testcase due to concurrent
    return 1
    if [[ "x${attributes[1]}" == "xYES" ]];then
        return 0
    fi
    return 1
}

declare -A concurrent_scripts
declare -A non_concurrent_scripts

for script in "${!scripts[@]}"
do
    check_concurrent ${script}
    if [ $? -eq 0 ];then
        concurrent_scripts+=([${script}]=${scripts[${script}]})
    else
        non_concurrent_scripts+=([${script}]=${scripts[${script}]})
    fi
done

CONTAINER_INDEX=1

function calculate_non_concurrent_script_tatol_time() {
    local result=0
    for script in ${!non_concurrent_scripts[@]}
    do
        attr=${non_concurrent_scripts[${script}]}
        attributes=(${attr// / })
        spend_time=${attributes[0]}
        if [[ ${spend_time} == "-" ]]; then
            spend_time=3
        fi
        result=$((result + spend_time))
    done
    echo ${result}
}

function do_testcase_auto_assignment() {
    local index=1
    for script in ${!concurrent_scripts[@]}
    do
        script_realpath=$(realpath ${script})
        echo ${script_realpath} >> ${TESTCASE_ASSIGN}_P${CONTAINER_INDEX}
        index=$((index + 1))
        if [[ ${index} -eq 50 ]]; then
            index=1
            CONTAINER_INDEX=$((CONTAINER_INDEX + 1))
        fi
    done

    if [[ ${#concurrent_scripts[@]} -ne 0 ]]; then
        CONTAINER_INDEX=$((CONTAINER_INDEX + 1))
    fi
    local acc_time=0
    for script in ${!non_concurrent_scripts[@]}
    do
        attr=${non_concurrent_scripts[${script}]}
        attributes=(${attr// / })
        spend_time=${attributes[0]}
        if [[ ${spend_time} == "-" ]]; then
            spend_time=3
        fi

        acc_time=$((acc_time + spend_time))
L
lifeng68 已提交
183
        if [[ ${acc_time} -ge 200  ]]; then
L
LiFeng 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
            acc_time=0
            CONTAINER_INDEX=$((CONTAINER_INDEX + 1))
        fi
        script_realpath=$(realpath ${script})
        echo ${script_realpath} >> ${TESTCASE_ASSIGN}_S${CONTAINER_INDEX}
    done
    if [[ ${#non_concurrent_scripts[@]} -eq 0 ]]; then
        CONTAINER_INDEX=$((CONTAINER_INDEX - 1))
    fi
}

function do_testcase_manual_assignment() {
    local index=1
    rm -rf ${TESTCASE_ASSIGN}_*
    for script in ${!concurrent_scripts[@]}
    do
        script_realpath=$(realpath ${script})
        echo ${script_realpath} >> ${TESTCASE_ASSIGN}_P${CONTAINER_INDEX}
        index=$((index + 1))
        if [[ ${index} -eq 50 ]]; then
            index=1
            CONTAINER_INDEX=$((CONTAINER_INDEX + 1))
        fi
    done

    non_concurrent_tatol_time=$(calculate_non_concurrent_script_tatol_time)

    avg_time_per_container=$((non_concurrent_tatol_time / container_nums))

    if [[ ${#concurrent_scripts[@]} -ne 0 ]]; then
        CONTAINER_INDEX=$((CONTAINER_INDEX + 1))
    fi
    local acc_time=0
    for script in ${!non_concurrent_scripts[@]}
    do
        attr=${non_concurrent_scripts[${script}]}
        attributes=(${attr// / })
        spend_time=${attributes[0]}
        if [[ ${spend_time} == "-" ]]; then
            spend_time=3
        fi
        acc_time=$((acc_time + spend_time))

        if [[ ${acc_time} -gt ${avg_time_per_container} ]]; then
            acc_time=0
            CONTAINER_INDEX=$((CONTAINER_INDEX + 1))
        fi
        script_realpath=$(realpath ${script})
        echo ${script_realpath} >> ${TESTCASE_ASSIGN}_S${CONTAINER_INDEX}
    done
    if [[ ${#non_concurrent_scripts[@]} -eq 0 ]]; then
        CONTAINER_INDEX=$((CONTAINER_INDEX - 1))
    fi
}

if [[ ${container_nums} -le 0 ]]; then
    do_testcase_auto_assignment
else
    do_testcase_manual_assignment ${container_nums}
fi

function echo_success()
{
    echo -e "\033[1;32m"$@"\033[0m"
}

function echo_error()
{
    echo -e "\033[1;31m"$@"\033[0m"
}

DockerFile=./CI/Dockerfile
ProcsFile=/sys/fs/cgroup/cpuset/docker/cgroup.clone_children
function make_sure_cgroup()
{
    image=`cat $DockerFile | grep FROM | awk '{print $2}'`
    if [ ! -e $ProcsFile ];then
        cid=`docker run -d $image`
        if [ $? -ne 0 ];then
            echo "Can not run docker container"
            return 1
        fi
        docker rm -f $cid
    fi
    procsval=`cat $ProcsFile`
    if [ $procsval -ne 1 ];then
        echo "warning: set $ProcsFile to 1"
        echo 1 > $ProcsFile
    fi
}

function make_base_image()
{
    BASE_IMAGE=`docker build -q -f ${DockerFile} .`
}

make_sure_cgroup

make_base_image
if [ $? -ne 0 ];then
    exit 0
fi

#if you want to debug and disable cleanup all resources, create directory by 'mkdir -p $KEEP_CONTAINERS_ALIVE_DIR'
#remember to remove $KEEP_CONTAINERS_ALIVE_DIR after finished your debug.
if [ ! -d $KEEP_CONTAINERS_ALIVE_DIR ];then
    delete_old_resources $time_id "$containers_clear_time" "listcontainers" "remove_container"
    delete_old_resources $time_id "$containers_clear_time" "listtmpdirs" "remove_tmpdir"
fi

RES_CODE=0
mkdir -p $LXC_LOCK_DIR_HOST
L
lifeng68 已提交
296 297 298 299 300
env_gcov=""
if [[ "x${enable_gcov}" == "xON" ]]; then
    env_gcov="--env GCOV=ON"
fi

L
LiFeng 已提交
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
env_ignore_ci=""
if [ "x$ignore_ci" == "xON" ];then
    env_ignore_ci="--env IGNORE_CI=ON"
fi

function exec_script() {
    set +e
    local log_path="/tmp/${1}.log"
    contname="${1}"
    # keep -i so testcases which read stdin can success
    docker exec -itd -e TOPDIR=$src_code_dir -e TESTCASE_FLOCK=/tmp/runflag/${CONTAINER_NAME}.flock -e TESTCASE_SCRIPTS_LOG=/tmp/runflag/${CONTAINER_NAME}.scripts.log  -e TESTCASE_RUNFLAG=/tmp/runflag/${CONTAINER_NAME}.runflag -e TESTCASE_CONTNAME=${contname} ${contname} ${testcase_test} run ${2} ${log_path}
    docker exec ${1} $testcase_test get
    if [[ $? -ne 0 ]]; then
        rm -rf ${CIDIR}/${CONTAINER_NAME}.runflag
        docker exec ${contname} cat ${log_path}
        echo_error "testcase execute failed in container ${contname}, log: ${log_path}"
        return 1
    fi
    echo_success "Container: ${contname} success"
    return 0
}

cptemp=${tmpdir_prefix}/${CONTAINER_NAME}_cptemp
# container for testing restful and building
copycontainer=${CONTAINER_NAME}_R1
tmpdir="${tmpdir_prefix}/${copycontainer}"
containers+=(${copycontainer})
mkdir -p ${tmpdir}
touch $CIDIR/${CONTAINER_NAME}.runflag

docker run -tid -v /sys/fs/cgroup:/sys/fs/cgroup --tmpfs /tmp:exec,mode=777 --tmpfs /run:exec,mode=777 --name ${copycontainer} -v ${cptemp}:${cptemp} $env_gcov $env_ignore_ci -v ${CIDIR}:/tmp/runflag -v /lib/modules:/lib/modules -v $testcases_data_dir:$testcase_data -v $LXC_LOCK_DIR_HOST:$LXC_LOCK_DIR_CONTAINER -v $TOPDIR:$src_code_dir -v ${tmpdir}:/var/lib/isulad  --privileged -e login_username=$login_username -e login_passwd=$login_passwd $BASE_IMAGE
docker cp ${CIDIR}/testcase_assign_R1 ${copycontainer}:/root
echo_success "Run container ${copycontainer} success"

# make and install in rest container
docker exec -e TOPDIR=${src_code_dir} -e BUILDDIR=${cptemp} ${copycontainer} ${make_script}
if [ $? -ne 0 ];then
    echo_error "Make and install failed in container ${copycontainer}"
    rm -rf ${cptemp}
    exit 1
fi
echo_success "Finished build in container ${copycontainer}"

for index in $(seq 1 ${CONTAINER_INDEX})
do
    suffix=$(ls ${CIDIR} | grep testcase_assign_ | grep -E "*[S|P]${index}$" | awk -F '_' '{print $NF}')
    tmpdir="${tmpdir_prefix}/${CONTAINER_NAME}_${suffix}"
    mkdir -p ${tmpdir}
    containers+=(${CONTAINER_NAME}_${suffix})
    docker run -tid -v /sys/fs/cgroup:/sys/fs/cgroup --tmpfs /tmp:exec,mode=777 --tmpfs /run:exec,mode=777 --name ${CONTAINER_NAME}_${suffix} -v ${cptemp}:${cptemp} $env_gcov $env_ignore_ci -v ${CIDIR}:/tmp/runflag -v /lib/modules:/lib/modules -v $testcases_data_dir:$testcase_data -v $LXC_LOCK_DIR_HOST:$LXC_LOCK_DIR_CONTAINER -v $TOPDIR:$src_code_dir -v ${tmpdir}:/var/lib/isulad  --privileged -e login_username=$login_username -e login_passwd=$login_passwd $BASE_IMAGE
    docker cp ${CIDIR}/testcase_assign_${suffix} ${CONTAINER_NAME}_${suffix}:/root
    echo_success "Run container ${CONTAINER_NAME}_${suffix} success"
done

for container in ${containers[@]}
do
    {
L
LiuHao 已提交
358
        docker cp ${cptemp}/cni ${container}:/opt
L
LiFeng 已提交
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
        docker cp ${cptemp}/bin ${container}:/usr
        docker cp ${cptemp}/etc ${container}:/
        docker cp ${cptemp}/usr/bin ${container}:/usr
        docker cp ${cptemp}/include ${container}:/usr
        docker cp ${cptemp}/lib ${container}:/usr
        docker cp ${cptemp}/systemd ${container}:/lib
        # Docker cannot cp file to tmpfs /tmp in container
        docker exec ${container} sh -c "umask 0022 && cp -r ${testcase_data}/ci_testcase_data/embedded /tmp"
        docker cp ${cptemp}/rpm ${container}:/
        docker exec ${container} /bin/sh -c "rpm --force --nodeps -ivh /rpm/*"
    }&
done
wait
docker cp ${cptemp}/rest/bin ${copycontainer}:/usr
docker cp ${cptemp}/rest/etc ${copycontainer}:/
docker cp ${cptemp}/rest/include ${copycontainer}:/usr
docker cp ${cptemp}/rest/lib ${copycontainer}:/usr
rm -rf ${cptemp}
# wait for copy files become effective
sleep 3

for container in ${containers[@]}
do
    {
        exec_script ${container} ${testcase_script}
    }&
    pids="$! $pids"
done
docker exec ${copycontainer} tail -f --retry /tmp/runflag/${CONTAINER_NAME}.scripts.log 2>/dev/null &
tailpid=$!
trap "kill -9 $tailpid; exit 0" 15 2
wait $pids
kill -9 $tailpid

L
lifeng68 已提交
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
if [[ "x${enable_gcov}" == "xON" ]]; then
  rm -rf ${tmpdir}/build
  docker cp ${containers[1]}:/root/iSulad/build ${tmpdir}
  docker cp ${tmpdir}/build ${containers[0]}:/root
  docker exec -e TOPDIR=${src_code_dir} ${containers[0]} ${gcov_script}
  echo "iSulad GCOV html generated"
  tar xf ./isulad-gcov.tar.gz
  rm -rf /var/www/html/isulad-gcov
  rm -rf /var/www/html/isulad-gcov.tar.gz
  mv ./tmp/isulad-gcov /var/www/html
  cp isulad-gcov.tar.gz /var/www/html

  tar xf ./isulad-llt-gcov.tar.gz
  rm -rf /var/www/html/isulad-llt-gcov
  rm -rf /var/www/html/isulad-llt-gcov.tar.gz
  mv ./coverage /var/www/html
  cp isulad-llt-gcov.tar.gz /var/www/html
fi

L
LiFeng 已提交
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
if [[ -e $CIDIR/${CONTAINER_NAME}.runflag ]]; then
    echo_success "All \"${#scripts[@]}\" testcases passed!"
    rm -rf $CIDIR/${CONTAINER_NAME}.runflag
    for container in ${containers[@]}
    do
        docker rm -f $container
    done
    exit 0;
else
    for container in ${containers[@]}
    do
        docker rm -f $container
    done
    echo_error "Test failed!"
    exit -1;
fi