583ce8041ca29634773fe6580dbb1d12509e2ee7
[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     # volume if credentials file path is given to Functest
18     rc_file_vol="-v $RC_FILE_PATH:/home/opnfv/functest/conf/openstack.creds"
19 fi
20
21
22 if [[ ${INSTALLER_TYPE} == 'apex' ]]; then
23     ssh_options="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
24     if sudo virsh list | grep instack; then
25         instack_mac=$(sudo virsh domiflist instack | grep default | \
26                       grep -Eo "[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+")
27     elif sudo virsh list | grep undercloud; then
28         instack_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     else
31         echo "No available installer VM exists...exiting"
32         exit 1
33     fi
34     INSTALLER_IP=$(/usr/sbin/arp -e | grep ${instack_mac} | awk {'print $1'})
35     sshkey_vol="-v /root/.ssh/id_rsa:/root/.ssh/id_rsa"
36     sudo scp $ssh_options root@${INSTALLER_IP}:/home/stack/stackrc ${HOME}/stackrc
37     stackrc_vol="-v ${HOME}/stackrc:/home/opnfv/functest/conf/stackrc"
38
39     if sudo iptables -C FORWARD -o virbr0 -j REJECT --reject-with icmp-port-unreachable 2> ${redirect}; then
40         sudo iptables -D FORWARD -o virbr0 -j REJECT --reject-with icmp-port-unreachable
41     fi
42     if sudo iptables -C FORWARD -i virbr0 -j REJECT --reject-with icmp-port-unreachable 2> ${redirect}; then
43         sudo iptables -D FORWARD -i virbr0 -j REJECT --reject-with icmp-port-unreachable
44     fi
45 fi
46
47
48
49 # Set iptables rule to allow forwarding return traffic for container
50 if ! sudo iptables -C FORWARD -j RETURN 2> ${redirect} || ! sudo iptables -L FORWARD | awk 'NR==3' | grep RETURN 2> ${redirect}; then
51     sudo iptables -I FORWARD -j RETURN
52 fi
53
54 DEPLOY_TYPE=baremetal
55 [[ $BUILD_TAG =~ "virtual" ]] && DEPLOY_TYPE=virt
56
57 echo "Functest: Start Docker and prepare environment"
58
59 branch=${GIT_BRANCH##*/}
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
72 volumes="${results_vol} ${sshkey_vol} ${stackrc_vol} ${rc_file_vol}"
73
74
75 echo "Functest: Pulling image opnfv/functest:${DOCKER_TAG}"
76 docker pull opnfv/functest:$DOCKER_TAG >/dev/null
77
78 cmd="sudo docker run --privileged=true -id ${envs} ${volumes} \
79      ${custom_params} ${TESTCASE_OPTIONS} \
80      opnfv/functest:${DOCKER_TAG} /bin/bash"
81 echo "Functest: Running docker run command: ${cmd}"
82 ${cmd} >${redirect}
83 sleep 5
84 container_id=$(docker ps | grep "opnfv/functest:${DOCKER_TAG}" | awk '{print $1}' | head -1)
85 echo "Container ID=${container_id}"
86 if [ -z ${container_id} ]; then
87     echo "Cannot find opnfv/functest container ID ${container_id}. Please check if it is existing."
88     docker ps -a
89     exit 1
90 fi
91 echo "Starting the container: docker start ${container_id}"
92 docker start ${container_id}
93 sleep 5
94 docker ps >${redirect}
95 if [ $(docker ps | grep "opnfv/functest:${DOCKER_TAG}" | wc -l) == 0 ]; then
96     echo "The container opnfv/functest with ID=${container_id} has not been properly started. Exiting..."
97     exit 1
98 fi
99 if [[ ${branch} == *"brahmaputra"* ]]; then
100     cmd="${FUNCTEST_REPO_DIR}/docker/prepare_env.sh"
101 elif [[ ${branch} == *"colorado"* ]]; then
102     cmd="python ${FUNCTEST_REPO_DIR}/ci/prepare_env.py start"
103 else
104     cmd="python ${FUNCTEST_REPO_DIR}/functest/ci/prepare_env.py start"
105 fi
106 echo "Executing command inside the docker: ${cmd}"
107 docker exec ${container_id} ${cmd}