9616fb310d5e3b778fe3525bd9ab3d7d620e5f76
[releng.git] / jjb / dovetail / dovetail-run.sh
1 #!/bin/bash
2 ##############################################################################
3 # Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 #the noun INSTALLER is used in community, here is just the example to run.
11 #multi-platforms are supported.
12
13 set -e
14 [[ $CI_DEBUG == true ]] && redirect="/dev/stdout" || redirect="/dev/null"
15
16 DEPLOY_TYPE=baremetal
17 [[ $BUILD_TAG =~ "virtual" ]] && DEPLOY_TYPE=virt
18
19 DOVETAIL_HOME=${WORKSPACE}/cvp
20 [ -d ${DOVETAIL_HOME} ] && sudo rm -rf ${DOVETAIL_HOME}
21
22 mkdir -p ${DOVETAIL_HOME}
23
24 DOVETAIL_CONFIG=${DOVETAIL_HOME}/pre_config
25 mkdir -p ${DOVETAIL_CONFIG}
26
27 DOVETAIL_IMAGES=${DOVETAIL_HOME}/images
28 mkdir -p ${DOVETAIL_IMAGES}
29
30 ssh_options="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
31
32 sshkey=""
33 # The path of openrc.sh is defined in fetch_os_creds.sh
34 OPENRC=${DOVETAIL_CONFIG}/env_config.sh
35 CACERT=${DOVETAIL_CONFIG}/os_cacert
36 if [[ ${INSTALLER_TYPE} == 'apex' ]]; then
37     instack_mac=$(sudo virsh domiflist undercloud | grep default | \
38                   grep -Eo "[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+")
39     INSTALLER_IP=$(/usr/sbin/arp -e | grep ${instack_mac} | awk {'print $1'})
40     sshkey="-v /root/.ssh/id_rsa:/root/.ssh/id_rsa"
41     if [[ -n $(sudo iptables -L FORWARD |grep "REJECT"|grep "reject-with icmp-port-unreachable") ]]; then
42         #note: this happens only in opnfv-lf-pod1
43         sudo iptables -D FORWARD -o virbr0 -j REJECT --reject-with icmp-port-unreachable
44         sudo iptables -D FORWARD -i virbr0 -j REJECT --reject-with icmp-port-unreachable
45     fi
46 elif [[ ${INSTALLER_TYPE} == 'joid' ]]; then
47     # If production lab then creds may be retrieved dynamically
48     # creds are on the jumphost, always in the same folder
49     sudo cp $LAB_CONFIG/admin-openrc $OPENRC
50     # If dev lab, credentials may not be the default ones, just provide a path to put them into docker
51     # replace the default one by the customized one provided by jenkins config
52 fi
53
54 # Set iptables rule to allow forwarding return traffic for container
55 if ! sudo iptables -C FORWARD -j RETURN 2> ${redirect} || ! sudo iptables -L FORWARD | awk 'NR==3' | grep RETURN 2> ${redirect}; then
56     sudo iptables -I FORWARD -j RETURN
57 fi
58
59 releng_repo=${WORKSPACE}/releng
60 [ -d ${releng_repo} ] && sudo rm -rf ${releng_repo}
61 git clone https://gerrit.opnfv.org/gerrit/releng ${releng_repo} >/dev/null
62
63 if [[ ${INSTALLER_TYPE} != 'joid' ]]; then
64     echo "SUT branch is $SUT_BRANCH"
65     echo "dovetail branch is $BRANCH"
66     BRANCH_BACKUP=$BRANCH
67     export BRANCH=$SUT_BRANCH
68     ${releng_repo}/utils/fetch_os_creds.sh -d ${OPENRC} -i ${INSTALLER_TYPE} -a ${INSTALLER_IP} -o ${CACERT} >${redirect}
69     export BRANCH=$BRANCH_BACKUP
70 fi
71
72 if [[ -f $OPENRC ]]; then
73     echo "INFO: openstack credentials path is $OPENRC"
74     if [[ ! "${SUT_BRANCH}" =~ "danube" && ${INSTALLER_TYPE} == "compass" ]]; then
75         if [[ -f ${CACERT} ]]; then
76             echo "INFO: ${INSTALLER_TYPE} openstack cacert file is ${CACERT}"
77             echo "export OS_CACERT=${CACERT}" >> ${OPENRC}
78         else
79             echo "ERROR: Can't find ${INSTALLER_TYPE} openstack cacert file. Please check if it is existing."
80             sudo ls -al ${DOVETAIL_CONFIG}
81             exit 1
82         fi
83     fi
84     echo "export EXTERNAL_NETWORK=${EXTERNAL_NETWORK}" >> ${OPENRC}
85 else
86     echo "ERROR: cannot find file $OPENRC. Please check if it is existing."
87     sudo ls -al ${DOVETAIL_CONFIG}
88     exit 1
89 fi
90
91 if [[ ! "${SUT_BRANCH}" =~ "danube" && ${INSTALLER_TYPE} == "fuel" ]]; then
92     sed -i "s#/etc/ssl/certs/mcp_os_cacert#${CACERT}#g" ${OPENRC}
93 fi
94 cat $OPENRC
95
96 if [[ ! "${SUT_BRANCH}" =~ "danube" && ${INSTALLER_TYPE} == "compass" ]]; then
97     compass_repo=${WORKSPACE}/compass4nfv/
98     git clone https://github.com/opnfv/compass4nfv.git ${compass_repo} >/dev/null
99     sudo pip install shyaml
100     scenario_file=${compass_repo}/deploy/conf/hardware_environment/$NODE_NAME/os-nosdn-nofeature-ha.yml
101     ipmiIp=$(cat ${scenario_file} | shyaml get-value hosts.0.ipmiIp)
102     ipmiPass=$(cat ${scenario_file} | shyaml get-value hosts.0.ipmiPass)
103     ipmiUser=root
104     jumpserver_ip=$(ifconfig | grep -A 5 docker0 | grep "inet addr" | cut -d ':' -f 2 | cut -d ' ' -f 1)
105
106     cat << EOF >${DOVETAIL_CONFIG}/pod.yaml
107 nodes:
108 - {ip: ${jumpserver_ip}, name: node0, password: root, role: Jumpserver, user: root}
109 - {ip: 10.1.0.50, name: node1, password: root, role: controller, user: root,
110    ipmi_ip: ${ipmiIp}, ipmi_user: ${ipmiUser}, ipmi_password: ${ipmiPass}}
111 - {ip: 10.1.0.51, name: node2, password: root, role: controller, user: root}
112 - {ip: 10.1.0.52, name: node3, password: root, role: controller, user: root}
113 - {ip: 10.1.0.53, name: node4, password: root, role: compute, user: root}
114 - {ip: 10.1.0.54, name: node5, password: root, role: compute, user: root}
115
116 EOF
117 fi
118
119 if [[ ! "${SUT_BRANCH}" =~ "danube" && ${INSTALLER_TYPE} == 'fuel' && ${DEPLOY_TYPE} == 'baremetal' ]]; then
120     fuel_ctl_ssh_options="${ssh_options} -i ${SSH_KEY}"
121     ssh_user="ubuntu"
122     fuel_ctl_ip=$(ssh 2>/dev/null ${fuel_ctl_ssh_options} "${ssh_user}@${INSTALLER_IP}" \
123             "sudo salt --out yaml 'ctl*' pillar.get _param:openstack_control_address | \
124                 awk '{print \$2; exit}'") &> /dev/null
125     cat << EOF >${DOVETAIL_CONFIG}/pod.yaml
126 nodes:
127 - {ip: ${fuel_ctl_ip}, name: node1, key_filename: /root/.ssh/id_rsa, role: controller, user: ${ssh_user}}
128
129 EOF
130 fi
131
132 if [[ ! -f ${DOVETAIL_CONFIG}/pod.yaml ]]; then
133     set +e
134
135     sudo pip install virtualenv
136
137     cd ${releng_repo}/modules
138     sudo virtualenv venv
139     source venv/bin/activate
140     sudo pip install -e ./ >/dev/null
141     sudo pip install netaddr
142
143     if [[ ${INSTALLER_TYPE} == compass ]]; then
144         options="-u root -p root"
145     elif [[ ${INSTALLER_TYPE} == fuel ]]; then
146         options="-u root -p r00tme"
147     elif [[ ${INSTALLER_TYPE} == apex ]]; then
148         options="-u stack -k /root/.ssh/id_rsa"
149     elif [[ ${INSTALLER_TYPE} == daisy ]]; then
150         options="-u root -p r00tme"
151     else
152         echo "Don't support to generate pod.yaml on ${INSTALLER_TYPE} currently."
153         echo "HA test cases may not run properly."
154     fi
155
156     cmd="sudo python ${releng_repo}/utils/create_pod_file.py -t ${INSTALLER_TYPE} \
157          -i ${INSTALLER_IP} ${options} -f ${DOVETAIL_CONFIG}/pod.yaml"
158     echo ${cmd}
159     ${cmd}
160
161     deactivate
162
163     set -e
164
165     cd ${WORKSPACE}
166 fi
167
168 if [ -f ${DOVETAIL_CONFIG}/pod.yaml ]; then
169     sudo chmod 666 ${DOVETAIL_CONFIG}/pod.yaml
170     echo "Adapt process info for $INSTALLER_TYPE ..."
171     attack_process='rabbitmq'
172     cat << EOF >> ${DOVETAIL_CONFIG}/pod.yaml
173 process_info:
174 - {testcase_name: dovetail.ha.tc010, attack_process: ${attack_process}}
175
176 EOF
177     echo "file ${DOVETAIL_CONFIG}/pod.yaml:"
178     cat ${DOVETAIL_CONFIG}/pod.yaml
179 else
180     echo "Error: cannot find file ${DOVETAIL_CONFIG}/pod.yaml. Please check if it is existing."
181     sudo ls -al ${DOVETAIL_CONFIG}
182     echo "HA test cases may not run properly."
183 fi
184
185 if [ "$INSTALLER_TYPE" == "fuel" ]; then
186     if [[ "${SUT_BRANCH}" =~ "danube" ]]; then
187         echo "Fetching id_rsa file from jump_server $INSTALLER_IP..."
188         sshpass -p r00tme sudo scp $ssh_options root@${INSTALLER_IP}:~/.ssh/id_rsa ${DOVETAIL_CONFIG}/id_rsa
189     else
190         cp ${SSH_KEY} ${DOVETAIL_CONFIG}/id_rsa
191     fi
192 fi
193
194 if [ "$INSTALLER_TYPE" == "apex" ]; then
195     echo "Fetching id_rsa file from jump_server $INSTALLER_IP..."
196     sudo scp $ssh_options stack@${INSTALLER_IP}:~/.ssh/id_rsa ${DOVETAIL_CONFIG}/id_rsa
197 fi
198
199 if [ "$INSTALLER_TYPE" == "daisy" ]; then
200     echo "Fetching id_dsa file from jump_server $INSTALLER_IP..."
201     sshpass -p r00tme sudo scp $ssh_options root@${INSTALLER_IP}:~/.ssh/id_dsa ${DOVETAIL_CONFIG}/id_rsa
202 fi
203
204
205 image_path=${HOME}/opnfv/dovetail/images
206 if [[ ! -d ${image_path} ]]; then
207     mkdir -p ${image_path}
208 fi
209 # sdnvpn test case needs to download this image first before running
210 ubuntu_image=${image_path}/ubuntu-16.04-server-cloudimg-amd64-disk1.img
211 if [[ ! -f ${ubuntu_image} ]]; then
212     echo "Download image ubuntu-16.04-server-cloudimg-amd64-disk1.img ..."
213     wget -q -nc http://artifacts.opnfv.org/sdnvpn/ubuntu-16.04-server-cloudimg-amd64-disk1.img -P ${image_path}
214 fi
215 sudo cp ${ubuntu_image} ${DOVETAIL_IMAGES}
216
217 # yardstick and bottlenecks need to download this image first before running
218 cirros_image=${image_path}/cirros-0.3.5-x86_64-disk.img
219 if [[ ! -f ${cirros_image} ]]; then
220     echo "Download image cirros-0.3.5-x86_64-disk.img ..."
221     wget -q -nc http://download.cirros-cloud.net/0.3.5/cirros-0.3.5-x86_64-disk.img -P ${image_path}
222 fi
223 sudo cp ${cirros_image} ${DOVETAIL_IMAGES}
224
225 # functest needs to download this image first before running
226 cirros_image=${image_path}/cirros-0.4.0-x86_64-disk.img
227 if [[ ! -f ${cirros_image} ]]; then
228     echo "Download image cirros-0.4.0-x86_64-disk.img ..."
229     wget -q -nc http://download.cirros-cloud.net/0.4.0/cirros-0.4.0-x86_64-disk.img -P ${image_path}
230 fi
231 sudo cp ${cirros_image} ${DOVETAIL_IMAGES}
232
233 # snaps_smoke test case needs to download this image first before running
234 ubuntu14_image=${image_path}/ubuntu-14.04-server-cloudimg-amd64-disk1.img
235 if [[ ! -f ${ubuntu14_image} ]]; then
236     echo "Download image ubuntu-14.04-server-cloudimg-amd64-disk1.img ..."
237     wget -q -nc https://cloud-images.ubuntu.com/releases/14.04/release/ubuntu-14.04-server-cloudimg-amd64-disk1.img -P ${image_path}
238 fi
239 sudo cp ${ubuntu14_image} ${DOVETAIL_IMAGES}
240
241 # cloudify_ims test case needs to download these 2 images first before running
242 cloudify_image=${image_path}/cloudify-manager-premium-4.0.1.qcow2
243 if [[ ! -f ${cloudify_image} ]]; then
244     echo "Download image cloudify-manager-premium-4.0.1.qcow2 ..."
245     wget -q -nc http://repository.cloudifysource.org/cloudify/4.0.1/sp-release/cloudify-manager-premium-4.0.1.qcow2 -P ${image_path}
246 fi
247 sudo cp ${cloudify_image} ${DOVETAIL_IMAGES}
248 trusty_image=${image_path}/trusty-server-cloudimg-amd64-disk1.img
249 if [[ ! -f ${trusty_image} ]]; then
250     echo "Download image trusty-server-cloudimg-amd64-disk1.img ..."
251     wget -q -nc http://cloud-images.ubuntu.com/trusty/current/trusty-server-cloudimg-amd64-disk1.img -P ${image_path}
252 fi
253 sudo cp ${trusty_image} ${DOVETAIL_IMAGES}
254
255 opts="--privileged=true -id"
256
257 docker_volume="-v /var/run/docker.sock:/var/run/docker.sock"
258 dovetail_home_volume="-v ${DOVETAIL_HOME}:${DOVETAIL_HOME}"
259
260 # Pull the image with correct tag
261 DOCKER_REPO='opnfv/dovetail'
262 if [ "$(uname -m)" = 'aarch64' ]; then
263     DOCKER_REPO="${DOCKER_REPO}_$(uname -m)"
264     DOCKER_TAG="latest"
265 fi
266
267 echo "Dovetail: Pulling image ${DOCKER_REPO}:${DOCKER_TAG}"
268 docker pull ${DOCKER_REPO}:$DOCKER_TAG >$redirect
269
270 cmd="docker run ${opts} -e DOVETAIL_HOME=${DOVETAIL_HOME} ${docker_volume} ${dovetail_home_volume} \
271      ${sshkey} ${DOCKER_REPO}:${DOCKER_TAG} /bin/bash"
272 echo "Dovetail: running docker run command: ${cmd}"
273 ${cmd} >${redirect}
274 sleep 5
275 container_id=$(docker ps | grep "${DOCKER_REPO}:${DOCKER_TAG}" | awk '{print $1}' | head -1)
276 echo "Container ID=${container_id}"
277 if [ -z ${container_id} ]; then
278     echo "Cannot find ${DOCKER_REPO} container ID ${container_id}. Please check if it is existing."
279     docker ps -a
280     exit 1
281 fi
282 echo "Container Start: docker start ${container_id}"
283 docker start ${container_id}
284 sleep 5
285 docker ps >${redirect}
286 if [ $(docker ps | grep "${DOCKER_REPO}:${DOCKER_TAG}" | wc -l) == 0 ]; then
287     echo "The container ${DOCKER_REPO} with ID=${container_id} has not been properly started. Exiting..."
288     exit 1
289 fi
290
291 # Modify tempest_conf.yaml file
292 tempest_conf_file=${DOVETAIL_CONFIG}/tempest_conf.yaml
293 if [[ ${INSTALLER_TYPE} == 'compass' || ${INSTALLER_TYPE} == 'apex' ]]; then
294     volume_device='vdb'
295 else
296     volume_device='vdc'
297 fi
298
299 cat << EOF >$tempest_conf_file
300
301 compute:
302     min_compute_nodes: 2
303     volume_device_name: ${volume_device}
304
305 EOF
306
307 echo "${tempest_conf_file}..."
308 cat ${tempest_conf_file}
309
310 cp_tempest_cmd="docker cp ${DOVETAIL_CONFIG}/tempest_conf.yaml $container_id:/home/opnfv/dovetail/dovetail/userconfig"
311 echo "exec command: ${cp_tempest_cmd}"
312 $cp_tempest_cmd
313
314 if [[ ${TESTSUITE} == 'default' ]]; then
315     testsuite=''
316 else
317     testsuite="--testsuite ${TESTSUITE}"
318 fi
319
320 run_cmd="dovetail run ${testsuite} -d"
321 echo "Container exec command: ${run_cmd}"
322 docker exec $container_id ${run_cmd}
323
324 sudo cp -r ${DOVETAIL_HOME}/results ./
325 # To make sure the file owner is the current user, for the copied results files in the above line
326 echo "Change owner of result files ..."
327 CURRENT_USER=${SUDO_USER:-$USER}
328 PRIMARY_GROUP=$(id -gn $CURRENT_USER)
329 echo "Current user is ${CURRENT_USER}, group is ${PRIMARY_GROUP}"
330 sudo chown -R ${CURRENT_USER}:${PRIMARY_GROUP} ./results
331
332 #remove useless files to save disk space
333 sudo rm -rf ./results/workspace
334 sudo rm -f ./results/yardstick.img
335 sudo rm -f ./results/tmp*
336
337 echo "Dovetail: done!"
338