5f936f5d9b77f95e71f3d839649865faed84f0e3
[releng.git] / jjb / functest / set-functest-env.sh
1 #!/bin/bash
2
3 set -e
4 set +u
5 set +o pipefail
6
7 [[ $CI_DEBUG == true ]] && redirect="/dev/stdout" || redirect="/dev/null"
8
9 # Prepare OpenStack credentials volume
10 if [[ ${INSTALLER_TYPE} == 'joid' ]]; then
11     rc_file_vol="-v $LAB_CONFIG/admin-openrc:/home/opnfv/functest/conf/openstack.creds"
12 else
13     rc_file_vol="-v ${HOME}/opnfv-openrc.sh:/home/opnfv/functest/conf/openstack.creds"
14 fi
15
16
17 # Set iptables rule to allow forwarding return traffic for container
18 if ! sudo iptables -C FORWARD -j RETURN 2> ${redirect} || ! sudo iptables -L FORWARD | awk 'NR==3' | grep RETURN 2> ${redirect}; then
19     sudo iptables -I FORWARD -j RETURN
20 fi
21
22 DEPLOY_TYPE=baremetal
23 [[ $BUILD_TAG =~ "virtual" ]] && DEPLOY_TYPE=virt
24
25 echo "Functest: Start Docker and prepare environment"
26
27 if [ "$BRANCH" != 'stable/danube' ]; then
28   echo "Functest: Download images that will be used by test cases"
29   images_dir="${HOME}/opnfv/functest/images"
30   chmod +x ${WORKSPACE}/functest/ci/download_images.sh
31   ${WORKSPACE}/functest/ci/download_images.sh ${images_dir} 2> ${redirect}
32   images_vol="-v ${images_dir}:/home/opnfv/functest/images"
33 fi
34
35 dir_result="${HOME}/opnfv/functest/results/${BRANCH##*/}"
36 mkdir -p ${dir_result}
37 sudo rm -rf ${dir_result}/*
38 results_vol="-v ${dir_result}:/home/opnfv/functest/results"
39 custom_params=
40 test -f ${HOME}/opnfv/functest/custom/params_${DOCKER_TAG} && custom_params=$(cat ${HOME}/opnfv/functest/custom/params_${DOCKER_TAG})
41
42 envs="-e INSTALLER_TYPE=${INSTALLER_TYPE} -e INSTALLER_IP=${INSTALLER_IP} \
43     -e NODE_NAME=${NODE_NAME} -e DEPLOY_SCENARIO=${DEPLOY_SCENARIO} \
44     -e BUILD_TAG=${BUILD_TAG} -e CI_DEBUG=${CI_DEBUG} -e DEPLOY_TYPE=${DEPLOY_TYPE}"
45
46 if [[ ${INSTALLER_TYPE} == 'compass' && ${DEPLOY_SCENARIO} == *'os-nosdn-openo-ha'* ]]; then
47     ssh_options="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
48     openo_msb_port=${openo_msb_port:-80}
49     openo_msb_endpoint="$(sshpass -p'root' ssh 2>/dev/null $ssh_options root@${installer_ip} \
50     'mysql -ucompass -pcompass -Dcompass -e "select package_config from cluster;" \
51     | sed s/,/\\n/g | grep openo_ip | cut -d \" -f 4'):$openo_msb_port"
52
53     envs=${env}" -e OPENO_MSB_ENDPOINT=${openo_msb_endpoint}"
54 fi
55
56 if [ "$BRANCH" != 'stable/danube' ]; then
57   volumes="${images_vol} ${results_vol} ${sshkey_vol} ${stackrc_vol} ${rc_file_vol}"
58 else
59   volumes="${results_vol} ${sshkey_vol} ${stackrc_vol} ${rc_file_vol}"
60 fi
61
62 HOST_ARCH=$(uname -m)
63 FUNCTEST_IMAGE="opnfv/functest"
64 if [ "$HOST_ARCH" = "aarch64" ]; then
65     FUNCTEST_IMAGE="${FUNCTEST_IMAGE}_${HOST_ARCH}"
66 fi
67
68 echo "Functest: Pulling Functest Docker image ${FUNCTEST_IMAGE}:${DOCKER_TAG}"
69 docker pull ${FUNCTEST_IMAGE}:$DOCKER_TAG >/dev/null
70
71 cmd="sudo docker run --privileged=true -id ${envs} ${volumes} \
72      ${custom_params} ${TESTCASE_OPTIONS} \
73      ${FUNCTEST_IMAGE}:${DOCKER_TAG} /bin/bash"
74 echo "Functest: Running docker run command: ${cmd}"
75 ${cmd} >${redirect}
76 sleep 5
77 container_id=$(docker ps | grep "${FUNCTEST_IMAGE}:${DOCKER_TAG}" | awk '{print $1}' | head -1)
78 echo "Container ID=${container_id}"
79 if [ -z ${container_id} ]; then
80     echo "Cannot find opnfv/functest container ID ${container_id}. Please check if it is existing."
81     docker ps -a
82     exit 1
83 fi
84 echo "Starting the container: docker start ${container_id}"
85 docker start ${container_id}
86 sleep 5
87 docker ps >${redirect}
88 if [ $(docker ps | grep "${FUNCTEST_IMAGE}:${DOCKER_TAG}" | wc -l) == 0 ]; then
89     echo "The container ${FUNCTEST_IMAGE} with ID=${container_id} has not been properly started. Exiting..."
90     exit 1
91 fi
92
93 if [ "$BRANCH" == 'master' ]; then
94     cmd="prepare_env start"
95 else
96     cmd="python ${FUNCTEST_REPO_DIR}/functest/ci/prepare_env.py start"
97 fi
98
99
100 echo "Executing command inside the docker: ${cmd}"
101 docker exec ${container_id} ${cmd}