2 ##############################################################################
3 # Copyright (c) 2016 NEC Corporation and others.
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
11 [[ "${CI_DEBUG:-true}" == [Tt]rue ]] && set -x
13 IMAGE_URL=https://launchpad.net/cirros/trunk/0.3.0/+download/cirros-0.3.0-x86_64-disk.img
14 #if an existing image name is provided in the enviroment, use that one
15 IMAGE_NAME=${IMAGE_NAME:-cirros}
16 IMAGE_FILE="${IMAGE_NAME}.img"
20 ALARM_NAME=doctor_alarm1
26 #TODO: change back to `_member_` when JIRA DOCTOR-55 is done
29 SUPPORTED_INSTALLER_TYPES="apex fuel local"
30 INSTALLER_TYPE=${INSTALLER_TYPE:-local}
31 INSTALLER_IP=${INSTALLER_IP:-none}
33 SUPPORTED_INSPECTOR_TYPES="sample congress"
34 INSPECTOR_TYPE=${INSPECTOR_TYPE:-sample}
35 TOP_DIR=$(cd $(dirname "$0") && pwd)
37 ssh_opts="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
38 as_doctor_user="--os-username $DOCTOR_USER --os-password $DOCTOR_PW
39 --os-tenant-name $DOCTOR_PROJECT"
41 if [[ ! "$SUPPORTED_INSTALLER_TYPES" =~ "$INSTALLER_TYPE" ]] ; then
42 echo "ERROR: INSTALLER_TYPE=$INSTALLER_TYPE is not supported."
46 if [[ ! "$SUPPORTED_INSPECTOR_TYPES" =~ "$INSPECTOR_TYPE" ]] ; then
47 echo "ERROR: INSPECTOR_TYPE=$INSPECTOR_TYPE is not supported."
52 if [[ "$INSTALLER_TYPE" == "apex" ]] ; then
53 if [[ "$INSTALLER_IP" == "none" ]] ; then
54 instack_mac=$(sudo virsh domiflist instack | awk '/default/{print $5}')
55 INSTALLER_IP=$(/usr/sbin/arp -e | grep ${instack_mac} | awk '{print $1}')
57 elif [[ "$INSTALLER_TYPE" == "fuel" ]] ; then
58 if [[ "$INSTALLER_IP" == "none" ]] ; then
59 instack_mac=$(sudo virsh domiflist fuel-opnfv | awk '/pxebr/{print $5}')
60 INSTALLER_IP=$(/usr/sbin/arp -e | grep ${instack_mac} | awk '{print $1}')
64 if [[ "$INSTALLER_TYPE" != "local" ]] ; then
65 die_if_not_set $LINENO INSTALLER_IP "No installer IP"
69 prepare_ssh_to_cloud() {
70 ssh_opts_cpu="$ssh_opts"
72 # get ssh key from installer node
73 if [[ "$INSTALLER_TYPE" == "apex" ]] ; then
74 sudo scp $ssh_opts root@"$INSTALLER_IP":/home/stack/.ssh/id_rsa instack_key
75 sudo chown $(whoami):$(whoami) instack_key
77 ssh_opts_cpu+=" -i instack_key"
78 elif [[ "$INSTALLER_TYPE" == "fuel" ]] ; then
79 sshpass -p r00tme scp $ssh_opts root@${INSTALLER_IP}:.ssh/id_rsa instack_key
80 sudo chown $(whoami):$(whoami) instack_key
82 ssh_opts_cpu+=" -i instack_key"
83 elif [[ "$INSTALLER_TYPE" == "local" ]] ; then
84 echo "INSTALLER_TYPE set to 'local'. Assuming SSH keys already exchanged with $COMPUTE_HOST"
89 #TODO delete it when fuel support the configuration
90 if [[ "$INSTALLER_TYPE" == "fuel" ]] ; then
91 echo "modify the configuration..."
92 cat > set_conf.sh << 'END_TXT'
94 if [ -e /etc/ceilometer/event_pipeline.yaml ]; then
95 if ! grep -q '^ *- notifier://?topic=alarm.all$' /etc/ceilometer/event_pipeline.yaml; then
96 sed -i 's|- notifier://|- notifier://?topic=alarm.all|' /etc/ceilometer/event_pipeline.yaml
97 echo "modify the ceilometer config"
98 service ceilometer-agent-notification restart
101 echo "ceilometer event_pipeline.yaml file does not exist"
104 if [ -e /etc/nova/nova.conf ]; then
105 if ! grep -q '^notification_driver=messaging$' /etc/nova/nova.conf; then
106 sed -i -r 's/notification_driver=/notification_driver=messaging/g' /etc/nova/nova.conf
107 echo "modify nova config"
108 service nova-api restart
111 echo "nova.conf file does not exist"
117 CONTROLLER_IP=$(sshpass -p r00tme ssh 2>/dev/null $ssh_opts root@${INSTALLER_IP} \
118 "fuel node | grep controller | cut -d '|' -f 5|xargs")
119 for node in $CONTROLLER_IP;do
120 scp $ssh_opts_cpu set_conf.sh "root@$node:"
121 ssh $ssh_opts_cpu "root@$node" './set_conf.sh > set_conf.log 2>&1 &'
123 scp $ssh_opts_cpu "root@$node:set_conf.log" set_conf_$node.log
126 if grep -q "modify the ceilometer config" set_conf_*.log ; then
127 NEED_TO_RESTORE_CEILOMETER=true
129 if grep -q "modify nova config" set_conf_*.log ; then
130 NEED_TO_RESTORE_NOVA=true
133 echo "waiting service restart..."
139 #TODO delete it when fuel support the configuration
140 if [[ "$INSTALLER_TYPE" == "fuel" ]] ; then
141 if ! ($NEED_TO_RESTORE_CEILOMETER || $NEED_TO_RESTORE_NOVA) ; then
142 echo "Don't need to restore config"
146 echo "restore the configuration..."
147 cat > restore_conf.sh << 'END_TXT'
149 if @NEED_TO_RESTORE_CEILOMETER@ ; then
150 if [ -e /etc/ceilometer/event_pipeline.yaml ]; then
151 if grep -q '^ *- notifier://?topic=alarm.all$' /etc/ceilometer/event_pipeline.yaml; then
152 sed -i 's|- notifier://?topic=alarm.all|- notifier://|' /etc/ceilometer/event_pipeline.yaml
153 service ceilometer-agent-notification restart
156 echo "ceilometer event_pipeline.yaml file does not exist"
160 if @NEED_TO_RESTORE_NOVA@ ; then
161 if [ -e /etc/nova/nova.conf ]; then
162 if grep -q '^notification_driver=messaging$' /etc/nova/nova.conf; then
163 sed -i -r 's/notification_driver=messaging/notification_driver=/g' /etc/nova/nova.conf
164 service nova-api restart
167 echo "nova.conf file does not exist"
173 sed -i -e "s/@NEED_TO_RESTORE_CEILOMETER@/$NEED_TO_RESTORE_CEILOMETER/" restore_conf.sh
174 sed -i -e "s/@NEED_TO_RESTORE_NOVA@/$NEED_TO_RESTORE_NOVA/" restore_conf.sh
175 chmod +x restore_conf.sh
176 for node in $CONTROLLER_IP;do
177 scp $ssh_opts_cpu restore_conf.sh "root@$node:"
178 ssh $ssh_opts_cpu "root@$node" './restore_conf.sh > restore_conf.log 2>&1 &'
181 echo "waiting service restart..."
186 get_compute_host_info() {
187 # get computer host info which VM boot in
188 COMPUTE_HOST=$(openstack $as_doctor_user server show $VM_NAME |
189 grep "OS-EXT-SRV-ATTR:host" | awk '{ print $4 }')
190 compute_host_in_undercloud=${COMPUTE_HOST%%.*}
191 die_if_not_set $LINENO COMPUTE_HOST "Failed to get compute hostname"
193 if [[ "$INSTALLER_TYPE" == "apex" ]] ; then
194 COMPUTE_USER=${COMPUTE_USER:-heat-admin}
195 COMPUTE_IP=$(sudo ssh $ssh_opts $INSTALLER_IP \
197 nova show $compute_host_in_undercloud \
198 | awk '/ ctlplane network /{print \$5}'")
199 elif [[ "$INSTALLER_TYPE" == "fuel" ]] ; then
200 COMPUTE_USER=${COMPUTE_USER:-root}
201 node_id=$(echo $compute_host_in_undercloud | cut -d "-" -f 2)
202 COMPUTE_IP=$(sshpass -p r00tme ssh 2>/dev/null $ssh_opts root@${INSTALLER_IP} \
203 "fuel node|awk -F '|' -v id=$node_id '{if (\$1 == id) print \$5}' |xargs")
204 elif [[ "$INSTALLER_TYPE" == "local" ]] ; then
205 COMPUTE_USER=${COMPUTE_USER:-$(whoami)}
206 COMPUTE_IP=$(getent hosts "$COMPUTE_HOST" | awk '{ print $1 }')
209 die_if_not_set $LINENO COMPUTE_IP "Could not resolve $COMPUTE_HOST. Either manually set COMPUTE_IP or enable DNS resolution."
210 echo "COMPUTE_HOST=$COMPUTE_HOST"
211 echo "COMPUTE_IP=$COMPUTE_IP"
213 # verify connectivity to target compute host
214 ping -c 1 "$COMPUTE_IP"
215 if [[ $? -ne 0 ]] ; then
216 die $LINENO "Can not ping to computer host"
219 # verify ssh to target compute host
220 ssh $ssh_opts_cpu "$COMPUTE_USER@$COMPUTE_IP" 'exit'
221 if [[ $? -ne 0 ]] ; then
222 die $LINENO "Can not ssh to computer host"
227 local get_consumer_command="ip route get $COMPUTE_IP | awk '/ src /{print \$NF}'"
228 if [[ "$INSTALLER_TYPE" == "apex" ]] ; then
229 CONSUMER_IP=$(sudo ssh $ssh_opts root@$INSTALLER_IP \
230 "$get_consumer_command")
231 elif [[ "$INSTALLER_TYPE" == "fuel" ]] ; then
232 CONSUMER_IP=$(sudo sshpass -p r00tme ssh $ssh_opts root@${INSTALLER_IP} \
233 "$get_consumer_command")
234 elif [[ "$INSTALLER_TYPE" == "local" ]] ; then
235 CONSUMER_IP=`$get_consumer_command`
237 echo "CONSUMER_IP=$CONSUMER_IP"
239 die_if_not_set $LINENO CONSUMER_IP "Could not get CONSUMER_IP."
243 #if a different name was provided for the image in the enviroment there's no need to download the image
244 use_existing_image=false
245 openstack image list | grep -q " $IMAGE_NAME " && use_existing_image=true
247 if [[ "$use_existing_image" == false ]] ; then
248 [ -e "$IMAGE_FILE" ] && return 0
249 wget "$IMAGE_URL" -o "$IMAGE_FILE"
254 openstack image list | grep -q " $IMAGE_NAME " && return 0
255 openstack image create "$IMAGE_NAME" \
257 --disk-format "$IMAGE_FORMAT" \
258 --container-format bare \
263 openstack project list | grep -q " $DOCTOR_PROJECT " || {
264 openstack project create "$DOCTOR_PROJECT"
266 openstack user list | grep -q " $DOCTOR_USER " || {
267 openstack user create "$DOCTOR_USER" --password "$DOCTOR_PW" \
268 --project "$DOCTOR_PROJECT"
270 openstack user role list "$DOCTOR_USER" --project "$DOCTOR_PROJECT" \
271 | grep -q " $DOCTOR_ROLE " || {
272 openstack role add "$DOCTOR_ROLE" --user "$DOCTOR_USER" \
273 --project "$DOCTOR_PROJECT"
278 # test VM done with test user, so can test non-admin
279 openstack $as_doctor_user server list | grep -q " $VM_NAME " && return 0
280 openstack $as_doctor_user server create --flavor "$VM_FLAVOR" \
281 --image "$IMAGE_NAME" \
287 # get vm_id as test user
288 ceilometer $as_doctor_user alarm-list | grep -q " $ALARM_NAME " && return 0
289 vm_id=$(openstack $as_doctor_user server list | grep " $VM_NAME " | awk '{print $2}')
290 # TODO(r-mibu): change notification endpoint from localhost to the consumer
291 # IP address (functest container).
292 ceilometer $as_doctor_user alarm-event-create --name "$ALARM_NAME" \
293 --alarm-action "http://localhost:$CONSUMER_PORT/failure" \
294 --description "VM failure" \
296 --repeat-actions False \
297 --severity "moderate" \
298 --event-type compute.instance.update \
299 -q "traits.state=string::error; traits.instance_id=string::$vm_id"
303 pgrep -f "python monitor.py" && return 0
304 sudo -E python monitor.py "$COMPUTE_HOST" "$COMPUTE_IP" "$INSPECTOR_TYPE" \
305 "http://127.0.0.1:$INSPECTOR_PORT/events" > monitor.log 2>&1 &
309 pgrep -f "python monitor.py" || return 0
310 sudo kill $(pgrep -f "python monitor.py")
313 congress_add_rule() {
318 if ! openstack congress policy rule list $policy | grep -q -e "// Name: $name$" ; then
319 openstack congress policy rule create --name $name $policy "$rule"
323 congress_del_rule() {
327 if openstack congress policy rule list $policy | grep -q -e "^// Name: $name$" ; then
328 openstack congress policy rule delete $policy $name
332 congress_setup_rules() {
333 congress_add_rule host_down classification \
335 doctor:events(hostname=host, type="compute.host.down", status="down")'
337 congress_add_rule active_instance_in_host classification \
338 'active_instance_in_host(vmid, host) :-
339 nova:servers(id=vmid, host_name=host, status="ACTIVE")'
341 congress_add_rule host_force_down classification \
342 'execute[nova:services.force_down(host, "nova-compute", "True")] :-
345 congress_add_rule error_vm_states classification \
346 'execute[nova:servers.reset_state(vmid, "error")] :-
348 active_instance_in_host(vmid, host)'
352 if [[ "$INSPECTOR_TYPE" == "sample" ]] ; then
353 pgrep -f "python inspector.py" && return 0
354 python inspector.py "$INSPECTOR_PORT" > inspector.log 2>&1 &
355 elif [[ "$INSPECTOR_TYPE" == "congress" ]] ; then
356 nova_api_min_version="2.11"
357 nova_api_version=$(openstack congress datasource list | \
358 grep nova | grep -Po "(?<='api_version': ')[^']*")
359 if ! is_set nova_api_version; then
360 nova_api_version="2.0"
362 if [[ "$nova_api_version" < "$nova_api_min_version" ]]; then
363 die $LINENO "Congress Nova datasource API version < $nova_api_min_version ($nova_api_version)"
365 openstack congress driver list | grep -q " doctor "
366 openstack congress datasource list | grep -q " doctor " || {
367 openstack congress datasource create doctor doctor
374 if [[ "$INSPECTOR_TYPE" == "sample" ]] ; then
375 pgrep -f "python inspector.py" || return 0
376 kill $(pgrep -f "python inspector.py")
377 elif [[ "$INSPECTOR_TYPE" == "congress" ]] ; then
378 congress_del_rule host_force_down classification
379 congress_del_rule error_vm_states classification
380 congress_del_rule active_instance_in_host classification
381 congress_del_rule host_down classification
386 pgrep -f "python consumer.py" && return 0
387 python consumer.py "$CONSUMER_PORT" > consumer.log 2>&1 &
389 # NOTE(r-mibu): create tunnel to the controller nodes, so that we can
390 # avoid some network problems dpends on infra and installers.
391 # This tunnel will be terminated by stop_consumer() or after 10 mins passed.
392 if [[ "$INSTALLER_TYPE" != "local" ]] ; then
393 if [[ "$INSTALLER_TYPE" == "apex" ]] ; then
394 CONTROLLER_IPS=$(sudo ssh $ssh_opts $INSTALLER_IP \
396 nova list | grep ' overcloud-controller-[0-9] ' \
397 | sed -e 's/^.*ctlplane=//' -e 's/ *|\$//'")
398 elif [[ "$INSTALLER_TYPE" == "fuel" ]] ; then
399 CONTROLLER_IPS=$(sshpass -p r00tme ssh 2>/dev/null $ssh_opts root@${INSTALLER_IP} \
400 "fuel node | grep controller | cut -d '|' -f 5|xargs")
403 die_if_not_set $LINENO CONTROLLER_IPS "Could not get CONTROLLER_IPS."
404 for ip in $CONTROLLER_IPS
406 forward_rule="-R $CONSUMER_PORT:localhost:$CONSUMER_PORT"
407 tunnel_command="sudo ssh $ssh_opts_cpu $COMPUTE_USER@$ip $forward_rule sleep 600"
408 $tunnel_command > "ssh_tunnel.${ip}.log" 2>&1 < /dev/null &
414 pgrep -f "python consumer.py" || return 0
415 kill $(pgrep -f "python consumer.py")
417 # NOTE(r-mibu): terminate tunnels to the controller nodes
418 if [[ "$INSTALLER_TYPE" != "local" ]] ; then
419 for ip in $CONTROLLER_IPS
421 forward_rule="-R $CONSUMER_PORT:localhost:$CONSUMER_PORT"
422 tunnel_command="sudo ssh $ssh_opts_cpu $COMPUTE_USER@$ip $forward_rule sleep 600"
423 kill $(pgrep -f "$tunnel_command")
428 wait_for_vm_launch() {
429 echo "waiting for vm launch..."
432 while [[ ${count} -lt 60 ]]
434 state=$(openstack $as_doctor_user server list | grep " $VM_NAME " | awk '{print $6}')
435 if [[ "$state" == "ACTIVE" ]]; then
436 # NOTE(cgoncalves): sleeping for a bit to stabilize
437 # See python-openstackclient/functional/tests/compute/v2/test_server.py:wait_for_status
441 if [[ "$state" == "ERROR" ]]; then
442 die $LINENO "vm state is ERROR"
447 die $LINENO "Time out while waiting for VM launch"
451 echo "disabling network of compute host [$COMPUTE_HOST] for 3 mins..."
452 cat > disable_network.sh << 'END_TXT'
454 dev=$(sudo ip a | awk '/ @COMPUTE_IP@\//{print $7}')
456 sudo ip link set $dev down
458 sudo ip link set $dev up
461 sed -i -e "s/@COMPUTE_IP@/$COMPUTE_IP/" disable_network.sh
462 chmod +x disable_network.sh
463 scp $ssh_opts_cpu disable_network.sh "$COMPUTE_USER@$COMPUTE_IP:"
464 ssh $ssh_opts_cpu "$COMPUTE_USER@$COMPUTE_IP" 'nohup ./disable_network.sh > disable_network.log 2>&1 &'
467 calculate_notification_time() {
468 detected=$(grep "doctor monitor detected at" monitor.log | awk '{print $10}')
469 notified=$(grep "doctor consumer notified at" consumer.log | awk '{print $10}')
470 if ! grep -q "doctor consumer notified at" consumer.log ; then
471 die $LINENO "Consumer hasn't received fault notification."
473 echo "$notified $detected" | \
476 if (d < 1 && d > 0) { print d " OK"; exit 0 }
477 else { print d " NG"; exit 1 }
481 check_host_status() {
484 host_status_line=$(openstack $as_doctor_user --os-compute-api-version 2.16 \
485 server show $VM_NAME | grep "host_status")
486 host_status=$(echo $host_status_line | awk '{print $4}')
487 die_if_not_set $LINENO host_status "host_status not reported by: nova show $VM_NAME"
488 if [[ "$expected_state" =~ "$host_status" ]] ; then
489 echo "$VM_NAME showing host_status: $host_status"
491 die $LINENO "host_status:$host_status not equal to expected_state: $expected_state"
502 echo "waiting disabled compute host back to be enabled..."
503 python ./nova_force_down.py "$COMPUTE_HOST" --unset
505 check_host_status "UP"
506 scp $ssh_opts_cpu "$COMPUTE_USER@$COMPUTE_IP:disable_network.log" .
508 openstack $as_doctor_user server list | grep -q " $VM_NAME " && openstack $as_doctor_user server delete "$VM_NAME"
510 alarm_id=$(ceilometer $as_doctor_user alarm-list | grep " $ALARM_NAME " | awk '{print $2}')
512 [ -n "$alarm_id" ] && ceilometer $as_doctor_user alarm-delete "$alarm_id"
515 image_id=$(openstack image list | grep " $IMAGE_NAME " | awk '{print $2}')
517 #if an existing image was used, there's no need to remove it here
518 if [[ "$use_existing_image" == false ]] ; then
519 [ -n "$image_id" ] && openstack image delete "$image_id"
521 openstack role remove "$DOCTOR_ROLE" --user "$DOCTOR_USER" \
522 --project "$DOCTOR_PROJECT"
523 openstack project delete "$DOCTOR_PROJECT"
524 openstack user delete "$DOCTOR_USER"
530 echo "Note: doctor/tests/run.sh has been executed."
534 source $TOP_DIR/functions-common
536 echo "preparing test env..."
541 echo "preparing VM image..."
545 echo "creating test user..."
548 echo "creating VM..."
552 echo "get computer host info..."
553 get_compute_host_info
555 echo "creating alarm..."
556 #TODO: change back to use, network problems depends on infra and installers
560 echo "starting doctor sample components..."
566 echo "injecting host failure..."
570 check_host_status "(DOWN|UNKNOWN)"
571 calculate_notification_time