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
15 IMAGE_FILE="${IMAGE_NAME}.img"
19 ALARM_NAME=doctor_alarm1
25 #TODO: change back to `_member_` when JIRA DOCTOR-55 is done
28 SUPPORTED_INSTALLER_TYPES="apex fuel local"
29 INSTALLER_TYPE=${INSTALLER_TYPE:-local}
30 INSTALLER_IP=${INSTALLER_IP:-none}
32 SUPPORTED_INSPECTOR_TYPES="sample congress"
33 INSPECTOR_TYPE=${INSPECTOR_TYPE:-sample}
35 ssh_opts="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
36 as_doctor_user="--os-username $DOCTOR_USER --os-password $DOCTOR_PW
37 --os-tenant-name $DOCTOR_PROJECT"
39 if [[ ! "$SUPPORTED_INSTALLER_TYPES" =~ "$INSTALLER_TYPE" ]] ; then
40 echo "ERROR: INSTALLER_TYPE=$INSTALLER_TYPE is not supported."
44 if [[ ! "$SUPPORTED_INSPECTOR_TYPES" =~ "$INSPECTOR_TYPE" ]] ; then
45 echo "ERROR: INSPECTOR_TYPE=$INSPECTOR_TYPE is not supported."
49 get_compute_host_info() {
50 # get computer host info which VM boot in
51 COMPUTE_HOST=$(openstack $as_doctor_user server show $VM_NAME |
52 grep "OS-EXT-SRV-ATTR:host" | awk '{ print $4 }')
53 compute_host_in_undercloud=${COMPUTE_HOST%%.*}
54 if [[ -z "$COMPUTE_HOST" ]] ; then
55 echo "ERROR: failed to get compute hostname"
59 if [[ "$INSTALLER_TYPE" == "apex" ]] ; then
60 COMPUTE_USER=${COMPUTE_USER:-heat-admin}
61 if [[ "$INSTALLER_IP" == "none" ]] ; then
62 instack_mac=$(sudo virsh domiflist instack | awk '/default/{print $5}')
63 INSTALLER_IP=$(/usr/sbin/arp -e | grep ${instack_mac} | awk '{print $1}')
65 COMPUTE_IP=$(sudo ssh $ssh_opts $INSTALLER_IP \
67 nova show $compute_host_in_undercloud \
68 | awk '/ ctlplane network /{print \$5}'")
69 elif [[ "$INSTALLER_TYPE" == "fuel" ]] ; then
70 COMPUTE_USER=${COMPUTE_USER:-root}
71 if [[ "$INSTALLER_IP" == "none" ]] ; then
72 instack_mac=$(sudo virsh domiflist fuel-opnfv | awk '/pxebr/{print $5}')
73 INSTALLER_IP=$(/usr/sbin/arp -e | grep ${instack_mac} | awk '{print $1}')
75 node_id=$(echo $compute_host_in_undercloud | cut -d "-" -f 2)
76 COMPUTE_IP=$(sshpass -p r00tme ssh 2>/dev/null $ssh_opts root@${INSTALLER_IP} \
77 "fuel node|awk -F '|' -v id=$node_id '{if (\$1 == id) print \$5}' |xargs")
78 elif [[ "$INSTALLER_TYPE" == "local" ]] ; then
79 COMPUTE_USER=${COMPUTE_USER:-$(whoami)}
80 COMPUTE_IP=$(getent hosts "$COMPUTE_HOST" | awk '{ print $1 }')
83 if [[ -z "$COMPUTE_IP" ]]; then
84 echo "ERROR: Could not resolve $COMPUTE_HOST. Either manually set COMPUTE_IP or enable DNS resolution."
87 echo "COMPUTE_HOST=$COMPUTE_HOST"
88 echo "COMPUTE_IP=$COMPUTE_IP"
90 # verify connectivity to target compute host
91 ping -c 1 "$COMPUTE_IP"
92 if [[ $? -ne 0 ]] ; then
93 echo "ERROR: can not ping to computer host"
98 prepare_compute_ssh() {
99 ssh_opts_cpu="$ssh_opts"
101 # get ssh key from installer node
102 if [[ "$INSTALLER_TYPE" == "apex" ]] ; then
103 sudo scp $ssh_opts root@"$INSTALLER_IP":/home/stack/.ssh/id_rsa instack_key
104 sudo chown $(whoami):$(whoami) instack_key
105 chmod 400 instack_key
106 ssh_opts_cpu+=" -i instack_key"
107 elif [[ "$INSTALLER_TYPE" == "fuel" ]] ; then
108 sshpass -p r00tme scp $ssh_opts root@${INSTALLER_IP}:.ssh/id_rsa instack_key
109 sudo chown $(whoami):$(whoami) instack_key
110 chmod 400 instack_key
111 ssh_opts_cpu+=" -i instack_key"
112 elif [[ "$INSTALLER_TYPE" == "local" ]] ; then
113 echo "INSTALLER_TYPE set to 'local'. Assuming SSH keys already exchanged with $COMPUTE_HOST"
116 # verify ssh to target compute host
117 ssh $ssh_opts_cpu "$COMPUTE_USER@$COMPUTE_IP" 'exit'
118 if [[ $? -ne 0 ]] ; then
119 echo "ERROR: can not ssh to computer host"
125 CONSUMER_IP=$(sudo ssh $ssh_opts root@$INSTALLER_IP \
126 "ip route get $COMPUTE_IP | awk '/ src /{print \$NF}'")
127 echo "CONSUMER_IP=$CONSUMER_IP"
129 if [[ -z "$CONSUMER_IP" ]]; then
130 echo "ERROR: Could not get CONSUMER_IP."
136 [ -e "$IMAGE_FILE" ] && return 0
137 wget "$IMAGE_URL" -o "$IMAGE_FILE"
141 openstack image list | grep -q " $IMAGE_NAME " && return 0
142 openstack image create "$IMAGE_NAME" \
144 --disk-format "$IMAGE_FORMAT" \
145 --container-format bare \
150 openstack project list | grep -q " $DOCTOR_PROJECT " || {
151 openstack project create "$DOCTOR_PROJECT"
153 openstack user list | grep -q " $DOCTOR_USER " || {
154 openstack user create "$DOCTOR_USER" --password "$DOCTOR_PW" \
155 --project "$DOCTOR_PROJECT"
157 openstack user role list "$DOCTOR_USER" --project "$DOCTOR_PROJECT" \
158 | grep -q " $DOCTOR_ROLE " || {
159 openstack role add "$DOCTOR_ROLE" --user "$DOCTOR_USER" \
160 --project "$DOCTOR_PROJECT"
165 # test VM done with test user, so can test non-admin
166 openstack $as_doctor_user server list | grep -q " $VM_NAME " && return 0
167 openstack $as_doctor_user server create --flavor "$VM_FLAVOR" \
168 --image "$IMAGE_NAME" \
174 # get vm_id as test user
175 ceilometer $as_doctor_user alarm-list | grep -q " $ALARM_NAME " && return 0
176 vm_id=$(openstack $as_doctor_user server list | grep " $VM_NAME " | awk '{print $2}')
177 # TODO(r-mibu): change notification endpoint from localhost to the consumer
178 # IP address (functest container).
179 ceilometer $as_doctor_user alarm-event-create --name "$ALARM_NAME" \
180 --alarm-action "http://localhost:$CONSUMER_PORT/failure" \
181 --description "VM failure" \
183 --repeat-actions False \
184 --severity "moderate" \
185 --event-type compute.instance.update \
186 -q "traits.state=string::error; traits.instance_id=string::$vm_id"
192 sed -e 's/^/ /' "$log_file"
196 pgrep -f "python monitor.py" && return 0
197 sudo -E python monitor.py "$COMPUTE_HOST" "$COMPUTE_IP" "$INSPECTOR_TYPE" \
198 "http://127.0.0.1:$INSPECTOR_PORT/events" > monitor.log 2>&1 &
202 pgrep -f "python monitor.py" || return 0
203 sudo kill $(pgrep -f "python monitor.py")
204 print_log monitor.log
207 congress_add_rule() {
212 if ! openstack congress policy rule list $policy | grep -q -e "// Name: $name$" ; then
213 openstack congress policy rule create --name $name $policy "$rule"
217 congress_del_rule() {
221 if openstack congress policy rule list $policy | grep -q -e "^// Name: $name$" ; then
222 openstack congress policy rule delete $policy $name
226 congress_setup_rules() {
227 congress_add_rule host_down classification \
229 doctor:events(hostname=host, type="compute.host.down", status="down")'
231 congress_add_rule active_instance_in_host classification \
232 'active_instance_in_host(vmid, host) :-
233 nova:servers(id=vmid, host_name=host, status="ACTIVE")'
235 congress_add_rule host_force_down classification \
236 'execute[nova:services.force_down(host, "nova-compute", "True")] :-
239 congress_add_rule error_vm_states classification \
240 'execute[nova:servers.reset_state(vmid, "error")] :-
242 active_instance_in_host(vmid, host)'
246 if [[ "$INSPECTOR_TYPE" == "sample" ]] ; then
247 pgrep -f "python inspector.py" && return 0
248 python inspector.py "$INSPECTOR_PORT" > inspector.log 2>&1 &
249 elif [[ "$INSPECTOR_TYPE" == "congress" ]] ; then
250 nova_api_min_version="2.11"
251 nova_api_version=$(openstack congress datasource list | \
252 grep nova | grep -Po "(?<='api_version': ')[^']*")
253 [[ -z $nova_api_version ]] && nova_api_version="2.0"
254 if [[ "$nova_api_version" < "$nova_api_min_version" ]]; then
255 echo "ERROR: Congress Nova datasource API version < $nova_api_min_version ($nova_api_version)"
258 openstack congress driver list | grep -q " doctor "
259 openstack congress datasource list | grep -q " doctor " || {
260 openstack congress datasource create doctor doctor
267 if [[ "$INSPECTOR_TYPE" == "sample" ]] ; then
268 pgrep -f "python inspector.py" || return 0
269 kill $(pgrep -f "python inspector.py")
270 print_log inspector.log
271 elif [[ "$INSPECTOR_TYPE" == "congress" ]] ; then
272 congress_del_rule host_force_down classification
273 congress_del_rule error_vm_states classification
274 congress_del_rule active_instance_in_host classification
275 congress_del_rule host_down classification
280 pgrep -f "python consumer.py" && return 0
281 python consumer.py "$CONSUMER_PORT" > consumer.log 2>&1 &
283 # NOTE(r-mibu): create tunnel to the controller nodes, so that we can
284 # avoid some network problems dpends on infra and installers.
285 # This tunnel will be terminated by stop_consumer() or after 10 mins passed.
286 if [[ "$INSTALLER_TYPE" == "apex" ]] ; then
287 CONTROLLER_IPS=$(sudo ssh $ssh_opts $INSTALLER_IP \
289 nova list | grep ' overcloud-controller-[0-9] ' \
290 | sed -e 's/^.*ctlplane=//' -e 's/ *|\$//'")
292 if [[ -z "$CONTROLLER_IPS" ]]; then
293 echo "ERROR: Could not get CONTROLLER_IPS."
296 for ip in $CONTROLLER_IPS
298 forward_rule="-R $CONSUMER_PORT:localhost:$CONSUMER_PORT"
299 tunnel_command="sudo ssh $ssh_opts_cpu $COMPUTE_USER@$ip $forward_rule sleep 600"
300 $tunnel_command > "ssh_tunnel.${ip}.log" 2>&1 < /dev/null &
305 pgrep -f "python consumer.py" || return 0
306 kill $(pgrep -f "python consumer.py")
307 print_log consumer.log
309 # NOTE(r-mibu): terminate tunnels to the controller nodes
310 for ip in $CONTROLLER_IPS
312 forward_rule="-R $CONSUMER_PORT:localhost:$CONSUMER_PORT"
313 tunnel_command="sudo ssh $ssh_opts_cpu $COMPUTE_USER@$ip $forward_rule sleep 600"
314 kill $(pgrep -f "$tunnel_command")
315 print_log "ssh_tunnel.${ip}.log"
319 wait_for_vm_launch() {
320 echo "waiting for vm launch..."
323 while [[ ${count} -lt 60 ]]
325 state=$(openstack $as_doctor_user server list | grep " $VM_NAME " | awk '{print $6}')
326 [[ "$state" == "ACTIVE" ]] && return 0
327 [[ "$state" == "ERROR" ]] && echo "vm state is ERROR" && exit 1
331 echo "ERROR: time out while waiting for vm launch"
336 echo "disabling network of compute host [$COMPUTE_HOST] for 3 mins..."
337 cat > disable_network.sh << 'END_TXT'
339 dev=$(sudo ip a | awk '/ @COMPUTE_IP@\//{print $7}')
341 sudo ip link set $dev down
343 sudo ip link set $dev up
346 sed -i -e "s/@COMPUTE_IP@/$COMPUTE_IP/" disable_network.sh
347 chmod +x disable_network.sh
348 scp $ssh_opts_cpu disable_network.sh "$COMPUTE_USER@$COMPUTE_IP:"
349 ssh $ssh_opts_cpu "$COMPUTE_USER@$COMPUTE_IP" 'nohup ./disable_network.sh > disable_network.log 2>&1 &'
352 calculate_notification_time() {
353 detected=$(grep "doctor monitor detected at" monitor.log | awk '{print $5}')
354 notified=$(grep "doctor consumer notified at" consumer.log | awk '{print $5}')
355 if ! grep -q "doctor consumer notified at" consumer.log ; then
356 echo "ERROR: consumer hasn't received fault notification."
359 echo "$notified $detected" | \
360 awk '{d = $1 - $2; if (d < 1 && d > 0) print d " OK"; else print d " NG"}'
363 check_host_status() {
366 host_status_line=$(openstack $as_doctor_user --os-compute-api-version 2.16 \
367 server show $VM_NAME | grep "host_status")
368 host_status=$(echo $host_status_line | awk '{print $4}')
369 if [ -z "$host_status" ] ; then
370 echo "ERROR: host_status not reported by: nova show $VM_NAME"
372 elif [[ "$expected_state" =~ "$host_status" ]] ; then
373 echo "$VM_NAME showing host_status: $host_status"
375 echo "ERROR: host_status:$host_status not equal to expected_state: $expected_state"
387 echo "waiting disabled compute host back to be enabled..."
388 python ./nova_force_down.py "$COMPUTE_HOST" --unset
390 check_host_status "UP"
391 scp $ssh_opts_cpu "$COMPUTE_USER@$COMPUTE_IP:disable_network.log" .
392 print_log disable_network.log
394 openstack $as_doctor_user server list | grep -q " $VM_NAME " && openstack $as_doctor_user server delete "$VM_NAME"
396 alarm_id=$(ceilometer $as_doctor_user alarm-list | grep " $ALARM_NAME " | awk '{print $2}')
398 [ -n "$alarm_id" ] && ceilometer $as_doctor_user alarm-delete "$alarm_id"
401 image_id=$(openstack image list | grep " $IMAGE_NAME " | awk '{print $2}')
403 [ -n "$image_id" ] && openstack image delete "$image_id"
404 openstack role remove "$DOCTOR_ROLE" --user "$DOCTOR_USER" \
405 --project "$DOCTOR_PROJECT"
406 openstack project delete "$DOCTOR_PROJECT"
407 openstack user delete "$DOCTOR_USER"
411 echo "Note: doctor/tests/run.sh has been executed."
415 echo "preparing VM image..."
419 echo "creating test user..."
422 echo "creating VM..."
425 openstack $as_doctor_user server show $VM_NAME
427 echo "get computer host info and prepare to ssh..."
428 get_compute_host_info
431 echo "creating alarm..."
435 echo "starting doctor sample components..."
441 echo "injecting host failure..."
445 check_host_status "(DOWN|UNKNOWN)"
446 calculate_notification_time