Merge "Download images and map the folder to container"
[releng.git] / jjb / functest / set-functest-env.sh
1 #!/bin/bash
2
3 set -e
4 [[ $CI_DEBUG == true ]] && redirect="/dev/stdout" || redirect="/dev/null"
5 # LAB_CONFIG is used only for joid
6
7
8 if [[ ${INSTALLER_TYPE} == 'joid' ]]; then
9     # If production lab then creds may be retrieved dynamically
10     # creds are on the jumphost, always in the same folder
11     rc_file_vol="-v $LAB_CONFIG/admin-openrc:/home/opnfv/functest/conf/openstack.creds"
12     # If dev lab, credentials may not be the default ones, just provide a path to put them into docker
13     # replace the default one by the customized one provided by jenkins config
14 fi
15
16 if [[ ${RC_FILE_PATH} != '' ]] && [[ -f ${RC_FILE_PATH} ]] ; then
17     echo "Credentials file detected: ${RC_FILE_PATH}"
18     # volume if credentials file path is given to Functest
19     rc_file_vol="-v ${RC_FILE_PATH}:/home/opnfv/functest/conf/openstack.creds"
20     RC_FLAG=1
21 fi
22
23
24 if [[ ${INSTALLER_TYPE} == 'apex' ]]; then
25     ssh_options="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
26     if sudo virsh list | grep undercloud; then
27         echo "Installer VM detected"
28         undercloud_mac=$(sudo virsh domiflist undercloud | grep default | \
29                       grep -Eo "[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+")
30         INSTALLER_IP=$(/usr/sbin/arp -e | grep ${undercloud_mac} | awk {'print $1'})
31         sshkey_vol="-v /root/.ssh/id_rsa:/root/.ssh/id_rsa"
32         sudo scp $ssh_options root@${INSTALLER_IP}:/home/stack/stackrc ${HOME}/stackrc
33         stackrc_vol="-v ${HOME}/stackrc:/home/opnfv/functest/conf/stackrc"
34
35         if sudo iptables -C FORWARD -o virbr0 -j REJECT --reject-with icmp-port-unreachable 2> ${redirect}; then
36             sudo iptables -D FORWARD -o virbr0 -j REJECT --reject-with icmp-port-unreachable
37         fi
38         if sudo iptables -C FORWARD -i virbr0 -j REJECT --reject-with icmp-port-unreachable 2> ${redirect}; then
39           sudo iptables -D FORWARD -i virbr0 -j REJECT --reject-with icmp-port-unreachable
40         fi
41     elif [[ "$RC_FLAG" == 1 ]]; then
42         echo "No available installer VM, but credentials provided...continuing"
43     else
44         echo "No available installer VM exists and no credentials provided...exiting"
45         exit 1
46     fi
47
48 fi
49
50
51 # Set iptables rule to allow forwarding return traffic for container
52 if ! sudo iptables -C FORWARD -j RETURN 2> ${redirect} || ! sudo iptables -L FORWARD | awk 'NR==3' | grep RETURN 2> ${redirect}; then
53     sudo iptables -I FORWARD -j RETURN
54 fi
55
56 DEPLOY_TYPE=baremetal
57 [[ $BUILD_TAG =~ "virtual" ]] && DEPLOY_TYPE=virt
58
59 echo "Functest: Start Docker and prepare environment"
60
61 echo "Functest: Download images that will be used by test cases"
62 images_dir="${HOME}/opnfv/functest/images"
63 chmod +x ${WORKSPACE}/functest/ci/download_images.sh
64 ${WORKSPACE}/functest/ci/download_images.sh ${images_dir}
65 images_vol="-v ${images_dir}:/home/opnfv/functest/images"
66
67 dir_result="${HOME}/opnfv/functest/results/${BRANCH##*/}"
68 mkdir -p ${dir_result}
69 sudo rm -rf ${dir_result}/*
70 results_vol="-v ${dir_result}:/home/opnfv/functest/results"
71 custom_params=
72 test -f ${HOME}/opnfv/functest/custom/params_${DOCKER_TAG} && custom_params=$(cat ${HOME}/opnfv/functest/custom/params_${DOCKER_TAG})
73
74 envs="-e INSTALLER_TYPE=${INSTALLER_TYPE} -e INSTALLER_IP=${INSTALLER_IP} \
75     -e NODE_NAME=${NODE_NAME} -e DEPLOY_SCENARIO=${DEPLOY_SCENARIO} \
76     -e BUILD_TAG=${BUILD_TAG} -e CI_DEBUG=${CI_DEBUG} -e DEPLOY_TYPE=${DEPLOY_TYPE}"
77
78 if [[ ${INSTALLER_TYPE} == 'compass' && ${DEPLOY_SCENARIO} == *'os-nosdn-openo-ha'* ]]; then
79     ssh_options="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
80     openo_msb_port=${openo_msb_port:-80}
81     openo_msb_endpoint="$(sshpass -p'root' ssh 2>/dev/null $ssh_options root@${installer_ip} \
82     'mysql -ucompass -pcompass -Dcompass -e "select package_config from cluster;" \
83     | sed s/,/\\n/g | grep openo_ip | cut -d \" -f 4'):$openo_msb_port"
84
85     envs=${env}" -e OPENO_MSB_ENDPOINT=${openo_msb_endpoint}"
86 fi
87
88 volumes="${images_vol} ${results_vol} ${sshkey_vol} ${stackrc_vol} ${rc_file_vol}"
89
90 HOST_ARCH=$(uname -m)
91 FUNCTEST_IMAGE="opnfv/functest"
92 if [ "$HOST_ARCH" = "aarch64" ]; then
93     FUNCTEST_IMAGE="${FUNCTEST_IMAGE}_${HOST_ARCH}"
94 fi
95
96 echo "Functest: Pulling Functest Docker image ${FUNCTEST_IMAGE}:${DOCKER_TAG}"
97 docker pull ${FUNCTEST_IMAGE}:$DOCKER_TAG >/dev/null
98
99 cmd="sudo docker run --privileged=true -id ${envs} ${volumes} \
100      ${custom_params} ${TESTCASE_OPTIONS} \
101      ${FUNCTEST_IMAGE}:${DOCKER_TAG} /bin/bash"
102 echo "Functest: Running docker run command: ${cmd}"
103 ${cmd} >${redirect}
104 sleep 5
105 container_id=$(docker ps | grep "${FUNCTEST_IMAGE}:${DOCKER_TAG}" | awk '{print $1}' | head -1)
106 echo "Container ID=${container_id}"
107 if [ -z ${container_id} ]; then
108     echo "Cannot find opnfv/functest container ID ${container_id}. Please check if it is existing."
109     docker ps -a
110     exit 1
111 fi
112 echo "Starting the container: docker start ${container_id}"
113 docker start ${container_id}
114 sleep 5
115 docker ps >${redirect}
116 if [ $(docker ps | grep "${FUNCTEST_IMAGE}:${DOCKER_TAG}" | wc -l) == 0 ]; then
117     echo "The container ${FUNCTEST_IMAGE} with ID=${container_id} has not been properly started. Exiting..."
118     exit 1
119 fi
120
121 cmd="python ${FUNCTEST_REPO_DIR}/functest/ci/prepare_env.py start"
122
123 echo "Executing command inside the docker: ${cmd}"
124 docker exec ${container_id} ${cmd}