Bugfix: Dovetail CI failed on fuel because of BRANCH: unbound variable
[releng.git] / jjb / dovetail / dovetail-run.sh
1 #!/bin/bash
2
3 #the noun INSTALLER is used in community, here is just the example to run.
4 #multi-platforms are supported.
5
6 set -e
7 [[ $CI_DEBUG == true ]] && redirect="/dev/stdout" || redirect="/dev/null"
8
9 DOVETAIL_HOME=${WORKSPACE}/cvp
10 if [ -d ${DOVETAIL_HOME} ]; then
11     sudo rm -rf ${DOVETAIL_HOME}/*
12 else
13     sudo mkdir -p ${DOVETAIL_HOME}
14 fi
15
16 DOVETAIL_CONFIG=${DOVETAIL_HOME}/pre_config
17 sudo mkdir -p ${DOVETAIL_CONFIG}
18
19 sshkey=""
20 # The path of openrc.sh is defined in fetch_os_creds.sh
21 OPENRC=${DOVETAIL_CONFIG}/env_config.sh
22 if [[ ${INSTALLER_TYPE} == 'apex' ]]; then
23     instack_mac=$(sudo virsh domiflist undercloud | grep default | \
24                   grep -Eo "[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+")
25     INSTALLER_IP=$(/usr/sbin/arp -e | grep ${instack_mac} | awk {'print $1'})
26     sshkey="-v /root/.ssh/id_rsa:/root/.ssh/id_rsa"
27     if [[ -n $(sudo iptables -L FORWARD |grep "REJECT"|grep "reject-with icmp-port-unreachable") ]]; then
28         #note: this happens only in opnfv-lf-pod1
29         sudo iptables -D FORWARD -o virbr0 -j REJECT --reject-with icmp-port-unreachable
30         sudo iptables -D FORWARD -i virbr0 -j REJECT --reject-with icmp-port-unreachable
31     fi
32 elif [[ ${INSTALLER_TYPE} == 'joid' ]]; then
33     # If production lab then creds may be retrieved dynamically
34     # creds are on the jumphost, always in the same folder
35     sudo cp $LAB_CONFIG/admin-openrc $OPENRC
36     # If dev lab, credentials may not be the default ones, just provide a path to put them into docker
37     # replace the default one by the customized one provided by jenkins config
38 fi
39
40 # Set iptables rule to allow forwarding return traffic for container
41 if ! sudo iptables -C FORWARD -j RETURN 2> ${redirect} || ! sudo iptables -L FORWARD | awk 'NR==3' | grep RETURN 2> ${redirect}; then
42     sudo iptables -I FORWARD -j RETURN
43 fi
44
45 releng_repo=${WORKSPACE}/releng
46 [ -d ${releng_repo} ] && sudo rm -rf ${releng_repo}
47 git clone https://gerrit.opnfv.org/gerrit/releng ${releng_repo} >/dev/null
48
49 if [[ ${INSTALLER_TYPE} != 'joid' ]]; then
50     ${releng_repo}/utils/fetch_os_creds.sh -d ${OPENRC} -i ${INSTALLER_TYPE} -a ${INSTALLER_IP} >${redirect}
51 fi
52
53 if [[ -f $OPENRC ]]; then
54     echo "INFO: openstack credentials path is $OPENRC"
55     cat $OPENRC
56 else
57     echo "ERROR: cannot find file $OPENRC. Please check if it is existing."
58     sudo ls -al ${DOVETAIL_CONFIG}
59     exit 1
60 fi
61
62 sudo pip install virtualenv
63
64 cd ${releng_repo}/modules
65 sudo virtualenv venv
66 source venv/bin/activate
67 sudo pip install -e ./ >/dev/null
68 sudo pip install netaddr
69
70 if [[ ${INSTALLER_TYPE} == compass ]]; then
71     options="-u root -p root"
72 elif [[ ${INSTALLER_TYPE} == fuel ]]; then
73     options="-u root -p r00tme"
74 else
75     echo "Don't support to generate pod.yaml on ${INSTALLER_TYPE} currently."
76     echo "HA test cases may not run properly."
77 fi
78
79 cmd="sudo python ${releng_repo}/utils/create_pod_file.py -t ${INSTALLER_TYPE} \
80      -i ${INSTALLER_IP} ${options} -f ${DOVETAIL_CONFIG}/pod.yaml"
81 echo ${cmd}
82 ${cmd}
83
84 deactivate
85
86 cd ${WORKSPACE}
87
88 if [ -f ${DOVETAIL_CONFIG}/pod.yaml ]; then
89     echo "file ${DOVETAIL_CONFIG}/pod.yaml:"
90     cat ${DOVETAIL_CONFIG}/pod.yaml
91 else
92     echo "Error: cannot find file ${DOVETAIL_CONFIG}/pod.yaml. Please check if it is existing."
93     sudo ls -al ${DOVETAIL_CONFIG}
94     echo "HA test cases may not run properly."
95 fi
96
97 ssh_options="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
98
99 if [ "$INSTALLER_TYPE" == "fuel" ]; then
100     echo "Fetching id_rsa file from jump_server $INSTALLER_IP..."
101     sshpass -p r00tme sudo scp $ssh_options root@${INSTALLER_IP}:~/.ssh/id_rsa ${DOVETAIL_CONFIG}/id_rsa
102 fi
103
104 # sdnvpn test case needs to download this image first before running
105 sudo wget -nc http://artifacts.opnfv.org/sdnvpn/ubuntu-16.04-server-cloudimg-amd64-disk1.img -P ${DOVETAIL_CONFIG}
106
107 opts="--privileged=true -id"
108
109 docker_volume="-v /var/run/docker.sock:/var/run/docker.sock"
110 dovetail_home_volume="-v ${DOVETAIL_HOME}:${DOVETAIL_HOME}"
111
112 # Pull the image with correct tag
113 echo "Dovetail: Pulling image opnfv/dovetail:${DOCKER_TAG}"
114 docker pull opnfv/dovetail:$DOCKER_TAG >$redirect
115
116 cmd="docker run ${opts} -e DOVETAIL_HOME=${DOVETAIL_HOME} ${docker_volume} ${dovetail_home_volume} \
117      ${sshkey} opnfv/dovetail:${DOCKER_TAG} /bin/bash"
118 echo "Dovetail: running docker run command: ${cmd}"
119 ${cmd} >${redirect}
120 sleep 5
121 container_id=$(docker ps | grep "opnfv/dovetail:${DOCKER_TAG}" | awk '{print $1}' | head -1)
122 echo "Container ID=${container_id}"
123 if [ -z ${container_id} ]; then
124     echo "Cannot find opnfv/dovetail container ID ${container_id}. Please check if it is existing."
125     docker ps -a
126     exit 1
127 fi
128 echo "Container Start: docker start ${container_id}"
129 docker start ${container_id}
130 sleep 5
131 docker ps >${redirect}
132 if [ $(docker ps | grep "opnfv/dovetail:${DOCKER_TAG}" | wc -l) == 0 ]; then
133     echo "The container opnfv/dovetail with ID=${container_id} has not been properly started. Exiting..."
134     exit 1
135 fi
136
137 list_cmd="dovetail list ${TESTSUITE}"
138 run_cmd="dovetail run --testsuite ${TESTSUITE} -d"
139 echo "Container exec command: ${list_cmd}"
140 docker exec $container_id ${list_cmd}
141 echo "Container exec command: ${run_cmd}"
142 docker exec $container_id ${run_cmd}
143
144 sudo cp -r ${DOVETAIL_HOME}/results ./
145 # To make sure the file owner is the current user, for the copied results files in the above line
146 # if not, there will be error when next time to wipe workspace
147 # CURRENT_USER=${SUDO_USER:-$USER}
148 # PRIMARY_GROUP=$(id -gn $CURRENT_USER)
149 # sudo chown -R ${CURRENT_USER}:${PRIMARY_GROUP} ${WORKSPACE}/results
150
151 echo "Dovetail: done!"
152