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