Merge "Apex: remove downloading ISO for deploy"
[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 if [ "$BRANCH" != 'stable/danube' ]; then
55   echo "Functest: Download images that will be used by test cases"
56   images_dir="${HOME}/opnfv/functest/images"
57   chmod +x ${WORKSPACE}/functest/ci/download_images.sh
58   ${WORKSPACE}/functest/ci/download_images.sh ${images_dir} 2> ${redirect}
59   images_vol="-v ${images_dir}:/home/opnfv/functest/images"
60 fi
61
62 dir_result="${HOME}/opnfv/functest/results/${BRANCH##*/}"
63 mkdir -p ${dir_result}
64 sudo rm -rf ${dir_result}/*
65 results_vol="-v ${dir_result}:/home/opnfv/functest/results"
66 custom_params=
67 test -f ${HOME}/opnfv/functest/custom/params_${DOCKER_TAG} && custom_params=$(cat ${HOME}/opnfv/functest/custom/params_${DOCKER_TAG})
68
69 envs="-e INSTALLER_TYPE=${INSTALLER_TYPE} -e INSTALLER_IP=${INSTALLER_IP} \
70     -e NODE_NAME=${NODE_NAME} -e DEPLOY_SCENARIO=${DEPLOY_SCENARIO} \
71     -e BUILD_TAG=${BUILD_TAG} -e CI_DEBUG=${CI_DEBUG} -e DEPLOY_TYPE=${DEPLOY_TYPE}"
72
73 if [[ ${INSTALLER_TYPE} == 'compass' && ${DEPLOY_SCENARIO} == *'os-nosdn-openo-ha'* ]]; then
74     ssh_options="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
75     openo_msb_port=${openo_msb_port:-80}
76     openo_msb_endpoint="$(sshpass -p'root' ssh 2>/dev/null $ssh_options root@${installer_ip} \
77     'mysql -ucompass -pcompass -Dcompass -e "select package_config from cluster;" \
78     | sed s/,/\\n/g | grep openo_ip | cut -d \" -f 4'):$openo_msb_port"
79
80     envs=${env}" -e OPENO_MSB_ENDPOINT=${openo_msb_endpoint}"
81 fi
82
83 if [ "$BRANCH" != 'stable/danube' ]; then
84   volumes="${images_vol} ${results_vol} ${sshkey_vol} ${stackrc_vol} ${rc_file_vol}"
85 else
86   volumes="${results_vol} ${sshkey_vol} ${stackrc_vol} ${rc_file_vol}"
87 fi
88
89 HOST_ARCH=$(uname -m)
90 FUNCTEST_IMAGE="opnfv/functest"
91 if [ "$HOST_ARCH" = "aarch64" ]; then
92     FUNCTEST_IMAGE="${FUNCTEST_IMAGE}_${HOST_ARCH}"
93 fi
94
95 echo "Functest: Pulling Functest Docker image ${FUNCTEST_IMAGE}:${DOCKER_TAG}"
96 docker pull ${FUNCTEST_IMAGE}:$DOCKER_TAG >/dev/null
97
98 cmd="sudo docker run --privileged=true -id ${envs} ${volumes} \
99      ${custom_params} ${TESTCASE_OPTIONS} \
100      ${FUNCTEST_IMAGE}:${DOCKER_TAG} /bin/bash"
101 echo "Functest: Running docker run command: ${cmd}"
102 ${cmd} >${redirect}
103 sleep 5
104 container_id=$(docker ps | grep "${FUNCTEST_IMAGE}:${DOCKER_TAG}" | awk '{print $1}' | head -1)
105 echo "Container ID=${container_id}"
106 if [ -z ${container_id} ]; then
107     echo "Cannot find opnfv/functest container ID ${container_id}. Please check if it is existing."
108     docker ps -a
109     exit 1
110 fi
111 echo "Starting the container: docker start ${container_id}"
112 docker start ${container_id}
113 sleep 5
114 docker ps >${redirect}
115 if [ $(docker ps | grep "${FUNCTEST_IMAGE}:${DOCKER_TAG}" | wc -l) == 0 ]; then
116     echo "The container ${FUNCTEST_IMAGE} with ID=${container_id} has not been properly started. Exiting..."
117     exit 1
118 fi
119
120 cmd="python ${FUNCTEST_REPO_DIR}/functest/ci/prepare_env.py start"
121
122 echo "Executing command inside the docker: ${cmd}"
123 docker exec ${container_id} ${cmd}