Support opera test in functest releng
[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
52 # Set iptables rule to allow forwarding return traffic for container
53 if ! sudo iptables -C FORWARD -j RETURN 2> ${redirect} || ! sudo iptables -L FORWARD | awk 'NR==3' | grep RETURN 2> ${redirect}; then
54     sudo iptables -I FORWARD -j RETURN
55 fi
56
57 DEPLOY_TYPE=baremetal
58 [[ $BUILD_TAG =~ "virtual" ]] && DEPLOY_TYPE=virt
59
60 echo "Functest: Start Docker and prepare environment"
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 volumes="${results_vol} ${sshkey_vol} ${stackrc_vol} ${rc_file_vol}"
84
85 HOST_ARCH=$(uname -m)
86 FUNCTEST_IMAGE="opnfv/functest"
87 if [ "$HOST_ARCH" = "aarch64" ]; then
88     FUNCTEST_IMAGE="${FUNCTEST_IMAGE}_${HOST_ARCH}"
89 fi
90
91 echo "Functest: Pulling image ${FUNCTEST_IMAGE}:${DOCKER_TAG}"
92 docker pull ${FUNCTEST_IMAGE}:$DOCKER_TAG >/dev/null
93
94 cmd="sudo docker run --privileged=true -id ${envs} ${volumes} \
95      ${custom_params} ${TESTCASE_OPTIONS} \
96      ${FUNCTEST_IMAGE}:${DOCKER_TAG} /bin/bash"
97 echo "Functest: Running docker run command: ${cmd}"
98 ${cmd} >${redirect}
99 sleep 5
100 container_id=$(docker ps | grep "${FUNCTEST_IMAGE}:${DOCKER_TAG}" | awk '{print $1}' | head -1)
101 echo "Container ID=${container_id}"
102 if [ -z ${container_id} ]; then
103     echo "Cannot find opnfv/functest container ID ${container_id}. Please check if it is existing."
104     docker ps -a
105     exit 1
106 fi
107 echo "Starting the container: docker start ${container_id}"
108 docker start ${container_id}
109 sleep 5
110 docker ps >${redirect}
111 if [ $(docker ps | grep "${FUNCTEST_IMAGE}:${DOCKER_TAG}" | wc -l) == 0 ]; then
112     echo "The container ${FUNCTEST_IMAGE} with ID=${container_id} has not been properly started. Exiting..."
113     exit 1
114 fi
115 if [[ "$BRANCH" =~ 'brahmaputra' ]]; then
116     cmd="${FUNCTEST_REPO_DIR}/docker/prepare_env.sh"
117 elif [[ "$BRANCH" =~ 'colorado' ]]; then
118     cmd="python ${FUNCTEST_REPO_DIR}/ci/prepare_env.py start"
119 else
120     cmd="python ${FUNCTEST_REPO_DIR}/functest/ci/prepare_env.py start"
121 fi
122 echo "Executing command inside the docker: ${cmd}"
123 docker exec ${container_id} ${cmd}