e54c3bf1334044b5714952f83a3a8e2bebef7ada
[releng.git] / jjb / functest / set-functest-env.sh
1 #!/bin/bash
2
3 set -e
4 set +u
5 set +o pipefail
6
7 [[ $CI_DEBUG == true ]] && redirect="/dev/stdout" || redirect="/dev/null"
8
9 # Prepare OpenStack credentials volume
10 if [[ ${INSTALLER_TYPE} == 'joid' ]]; then
11     rc_file_vol="-v $LAB_CONFIG/admin-openrc:/home/opnfv/functest/conf/openstack.creds"
12 elif [[ ${INSTALLER_TYPE} == 'compass' && ${BRANCH} == 'master' ]]; then
13     cacert_file_vol="-v ${HOME}/os_cacert:/home/opnfv/functest/conf/os_cacert"
14     echo "export OS_CACERT=/home/opnfv/functest/conf/os_cacert" >> ${HOME}/opnfv-openrc.sh
15     rc_file_vol="-v ${HOME}/opnfv-openrc.sh:/home/opnfv/functest/conf/openstack.creds"
16 else
17     rc_file_vol="-v ${HOME}/opnfv-openrc.sh:/home/opnfv/functest/conf/openstack.creds"
18 fi
19
20
21 # Set iptables rule to allow forwarding return traffic for container
22 if ! sudo iptables -C FORWARD -j RETURN 2> ${redirect} || ! sudo iptables -L FORWARD | awk 'NR==3' | grep RETURN 2> ${redirect}; then
23     sudo iptables -I FORWARD -j RETURN
24 fi
25
26 DEPLOY_TYPE=baremetal
27 [[ $BUILD_TAG =~ "virtual" ]] && DEPLOY_TYPE=virt
28 HOST_ARCH=$(uname -m)
29
30 echo "Functest: Start Docker and prepare environment"
31
32 if [ "$BRANCH" != 'stable/danube' ]; then
33   echo "Functest: Download images that will be used by test cases"
34   images_dir="${HOME}/opnfv/functest/images"
35   chmod +x ${WORKSPACE}/functest/ci/download_images.sh
36   ${WORKSPACE}/functest/ci/download_images.sh ${images_dir} > ${redirect} 2>&1
37   images_vol="-v ${images_dir}:/home/opnfv/functest/images"
38   echo "Functest: Images successfully downloaded"
39 fi
40
41 dir_result="${HOME}/opnfv/functest/results/${BRANCH##*/}"
42 mkdir -p ${dir_result}
43 sudo rm -rf ${dir_result}/*
44 results_vol="-v ${dir_result}:/home/opnfv/functest/results"
45 custom_params=
46 test -f ${HOME}/opnfv/functest/custom/params_${DOCKER_TAG} && custom_params=$(cat ${HOME}/opnfv/functest/custom/params_${DOCKER_TAG})
47 echo "Functest: custom parameters successfully retrieved: ${custom_params}"
48
49 envs="-e INSTALLER_TYPE=${INSTALLER_TYPE} -e INSTALLER_IP=${INSTALLER_IP} \
50     -e NODE_NAME=${NODE_NAME} -e DEPLOY_SCENARIO=${DEPLOY_SCENARIO} \
51     -e BUILD_TAG=${BUILD_TAG} -e CI_DEBUG=${CI_DEBUG} -e DEPLOY_TYPE=${DEPLOY_TYPE}"
52
53 if [[ ${INSTALLER_TYPE} == 'fuel' && ! -z ${SALT_MASTER_IP} ]]; then
54   HOST_ARCH=$(ssh -l ubuntu ${SALT_MASTER_IP} -i ${SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no \
55   "sudo salt 'cmp*' grains.get cpuarch --out yaml |awk '{print \$2; exit}'")
56   envs="${envs} -e POD_ARCH=${HOST_ARCH}"
57 fi
58
59 if [[ ${INSTALLER_TYPE} == 'compass' && ${DEPLOY_SCENARIO} == *'os-nosdn-openo-ha'* ]]; then
60     ssh_options="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
61     openo_msb_port=${openo_msb_port:-80}
62     openo_msb_endpoint="$(sshpass -p'root' ssh 2>/dev/null $ssh_options root@${installer_ip} \
63     'mysql -ucompass -pcompass -Dcompass -e "select package_config from cluster;" \
64     | sed s/,/\\n/g | grep openo_ip | cut -d \" -f 4'):$openo_msb_port"
65
66     envs=${env}" -e OPENO_MSB_ENDPOINT=${openo_msb_endpoint}"
67 fi
68
69 if [ "$BRANCH" != 'stable/danube' ]; then
70   volumes="${images_vol} ${results_vol} ${sshkey_vol} ${stackrc_vol} ${rc_file_vol} ${cacert_file_vol}"
71 else
72   volumes="${results_vol} ${sshkey_vol} ${stackrc_vol} ${rc_file_vol}"
73 fi
74
75 echo "Functest: volumes defined"
76
77 FUNCTEST_IMAGE="opnfv/functest"
78 if [ "$HOST_ARCH" = "aarch64" ]; then
79     FUNCTEST_IMAGE="${FUNCTEST_IMAGE}_${HOST_ARCH}"
80 fi
81
82 echo "Functest: Pulling Functest Docker image ${FUNCTEST_IMAGE}:${DOCKER_TAG}"
83 docker pull ${FUNCTEST_IMAGE}:$DOCKER_TAG >/dev/null
84
85 cmd="sudo docker run --privileged=true -id ${envs} ${volumes} \
86      ${custom_params} ${TESTCASE_OPTIONS} \
87      ${FUNCTEST_IMAGE}:${DOCKER_TAG} /bin/bash"
88 echo "Functest: Running docker run command: ${cmd}"
89 ${cmd} >${redirect}
90 sleep 5
91 container_id=$(docker ps | grep "${FUNCTEST_IMAGE}:${DOCKER_TAG}" | awk '{print $1}' | head -1)
92 echo "Container ID=${container_id}"
93 if [ -z ${container_id} ]; then
94     echo "Cannot find opnfv/functest container ID ${container_id}. Please check if it is existing."
95     docker ps -a
96     exit 1
97 fi
98 echo "Starting the container: docker start ${container_id}"
99 docker start ${container_id}
100 sleep 5
101 docker ps >${redirect}
102 if [ $(docker ps | grep "${FUNCTEST_IMAGE}:${DOCKER_TAG}" | wc -l) == 0 ]; then
103     echo "The container ${FUNCTEST_IMAGE} with ID=${container_id} has not been properly started. Exiting..."
104     exit 1
105 fi
106
107 if [ "$BRANCH" == 'master' ]; then
108     cmd="prepare_env start"
109 else
110     cmd="python ${FUNCTEST_REPO_DIR}/functest/ci/prepare_env.py start"
111 fi
112
113
114 echo "Executing command inside the docker: ${cmd}"
115 docker exec ${container_id} ${cmd}