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