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 local"
29 INSTALLER_TYPE=${INSTALLER_TYPE:-apex}
30 INSTALLER_IP=${INSTALLER_IP:-none}
32 ssh_opts="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
33 as_doctor_user="--os-username $DOCTOR_USER --os-password $DOCTOR_PW
34 --os-tenant-name $DOCTOR_PROJECT"
36 if [[ ! "$SUPPORTED_INSTALLER_TYPES" =~ "$INSTALLER_TYPE" ]] ; then
37 echo "ERROR: INSTALLER_TYPE=$INSTALLER_TYPE is not supported."
41 get_compute_host_info() {
42 # get computer host info which VM boot in
43 COMPUTE_HOST=$(openstack $as_doctor_user server show $VM_NAME |
44 grep "OS-EXT-SRV-ATTR:host" | awk '{ print $4 }')
45 if [[ -z "$COMPUTE_HOST" ]] ; then
46 echo "ERROR: failed to get compute hostname"
49 if [[ "$INSTALLER_TYPE" == "apex" ]] ; then
50 compute_host_in_undercloud=${COMPUTE_HOST%%.*}
51 COMPUTE_USER=${COMPUTE_USER:-heat-admin}
52 if [[ "$INSTALLER_IP" == "none" ]] ; then
53 instack_mac=$(sudo virsh domiflist instack | awk '/default/{print $5}')
54 INSTALLER_IP=$(/usr/sbin/arp -e | grep ${instack_mac} | awk '{print $1}')
56 COMPUTE_IP=$(sudo ssh $ssh_opts $INSTALLER_IP \
58 nova show $compute_host_in_undercloud \
59 | awk '/ ctlplane network /{print \$5}'")
60 elif [[ "$INSTALLER_TYPE" == "local" ]] ; then
61 COMPUTE_USER=${COMPUTE_USER:-$(whoami)}
62 COMPUTE_IP=$(getent hosts "$COMPUTE_HOST" | awk '{ print $1 }')
63 if [[ -z "$COMPUTE_IP" ]]; then
64 echo "ERROR: Could not resolve $COMPUTE_HOST. Either manually set COMPUTE_IP or enable DNS resolution."
68 echo "COMPUTE_HOST=$COMPUTE_HOST"
69 echo "COMPUTE_IP=$COMPUTE_IP"
71 # verify connectivity to target compute host
72 ping -c 1 "$COMPUTE_IP"
73 if [[ $? -ne 0 ]] ; then
74 echo "ERROR: can not ping to computer host"
79 prepare_compute_ssh() {
80 ssh_opts_cpu="$ssh_opts"
82 # get ssh key from installer node
83 if [[ "$INSTALLER_TYPE" == "apex" ]] ; then
84 sudo scp $ssh_opts root@"$INSTALLER_IP":/home/stack/.ssh/id_rsa instack_key
85 sudo chown $(whoami):$(whoami) instack_key
87 ssh_opts_cpu+=" -i instack_key"
88 elif [[ "$INSTALLER_TYPE" == "local" ]] ; then
89 echo "INSTALLER_TYPE set to 'local'. Assuming SSH keys already exchanged with $COMPUTE_HOST"
92 # verify ssh to target compute host
93 ssh $ssh_opts_cpu "$COMPUTE_USER@$COMPUTE_IP" 'exit'
94 if [[ $? -ne 0 ]] ; then
95 echo "ERROR: can not ssh to computer host"
101 CONSUMER_IP=$(ip route get $COMPUTE_IP | awk '/ src /{print $NF}')
102 echo "CONSUMER_IP=$CONSUMER_IP"
104 if [[ -z "$CONSUMER_IP" ]]; then
105 echo "ERROR: Could not get CONSUMER_IP."
111 [ -e "$IMAGE_FILE" ] && return 0
112 wget "$IMAGE_URL" -o "$IMAGE_FILE"
116 openstack image list | grep -q " $IMAGE_NAME " && return 0
117 openstack image create "$IMAGE_NAME" \
119 --disk-format "$IMAGE_FORMAT" \
120 --container-format bare \
125 openstack user list | grep -q " $DOCTOR_USER " || {
126 openstack user create "$DOCTOR_USER" --password "$DOCTOR_PW"
128 openstack project list | grep -q " $DOCTOR_PROJECT " || {
129 openstack project create "$DOCTOR_PROJECT"
131 openstack user role list "$DOCTOR_USER" --project "$DOCTOR_PROJECT" \
132 | grep -q " $DOCTOR_ROLE " || {
133 openstack role add "$DOCTOR_ROLE" --user "$DOCTOR_USER" \
134 --project "$DOCTOR_PROJECT"
139 # test VM done with test user, so can test non-admin
140 openstack $as_doctor_user server list | grep -q " $VM_NAME " && return 0
141 openstack $as_doctor_user server create --flavor "$VM_FLAVOR" \
142 --image "$IMAGE_NAME" \
148 # get vm_id as test user
149 ceilometer $as_doctor_user alarm-list | grep -q " $ALARM_NAME " && return 0
150 vm_id=$(openstack $as_doctor_user server list | grep " $VM_NAME " | awk '{print $2}')
151 ceilometer $as_doctor_user alarm-event-create --name "$ALARM_NAME" \
152 --alarm-action "http://$CONSUMER_IP:$CONSUMER_PORT/failure" \
153 --description "VM failure" \
155 --repeat-actions False \
156 --severity "moderate" \
157 --event-type compute.instance.update \
158 -q "traits.state=string::error; traits.instance_id=string::$vm_id"
163 pgrep -f "python monitor.py" && return 0
164 sudo python monitor.py "$COMPUTE_HOST" "$COMPUTE_IP" \
165 "http://127.0.0.1:$INSPECTOR_PORT/events" > monitor.log 2>&1 &
169 pgrep -f "python monitor.py" || return 0
170 sudo kill $(pgrep -f "python monitor.py")
175 pgrep -f "python inspector.py" && return 0
176 python inspector.py "$INSPECTOR_PORT" > inspector.log 2>&1 &
180 pgrep -f "python inspector.py" || return 0
181 kill $(pgrep -f "python inspector.py")
186 pgrep -f "python consumer.py" && return 0
187 python consumer.py "$CONSUMER_PORT" > consumer.log 2>&1 &
191 pgrep -f "python consumer.py" || return 0
192 kill $(pgrep -f "python consumer.py")
196 wait_for_vm_launch() {
197 echo "waiting for vm launch..."
200 while [[ ${count} -lt 60 ]]
202 state=$(openstack $as_doctor_user server list | grep " $VM_NAME " | awk '{print $6}')
203 [[ "$state" == "ACTIVE" ]] && return 0
204 [[ "$state" == "ERROR" ]] && echo "vm state is ERROR" && exit 1
208 echo "ERROR: time out while waiting for vm launch"
213 echo "disabling network of compute host [$COMPUTE_HOST] for 3 mins..."
214 cat > disable_network.sh << 'END_TXT'
216 dev=$(sudo ip a | awk '/ @COMPUTE_IP@\//{print $7}')
218 sudo ip link set $dev down
220 sudo ip link set $dev up
223 sed -i -e "s/@COMPUTE_IP@/$COMPUTE_IP/" disable_network.sh
224 chmod +x disable_network.sh
225 scp $ssh_opts_cpu disable_network.sh "$COMPUTE_USER@$COMPUTE_IP:"
226 ssh $ssh_opts_cpu "$COMPUTE_USER@$COMPUTE_IP" 'nohup ./disable_network.sh > disable_network.log 2>&1 &'
229 calculate_notification_time() {
230 detected=$(grep "doctor monitor detected at" monitor.log | awk '{print $5}')
231 notified=$(grep "doctor consumer notified at" consumer.log | awk '{print $5}')
232 if ! grep -q "doctor consumer notified at" consumer.log ; then
233 echo "ERROR: consumer hasn't received fault notification."
236 echo "$notified $detected" | \
237 awk '{d = $1 - $2; if (d < 1 && d > 0) print d " OK"; else print d " NG"}'
240 check_host_status() {
243 host_status_line=$(openstack $as_doctor_user --os-compute-api-version 2.16 \
244 server show $VM_NAME | grep "host_status")
245 host_status=$(echo $host_status_line | awk '{print $4}')
246 if [ -z "$host_status" ] ; then
247 echo "ERROR: host_status not reported by: nova show $VM_NAME"
249 elif [[ "$expected_state" =~ "$host_status" ]] ; then
250 echo "$VM_NAME showing host_status: $host_status"
252 echo "ERROR: host_status:$host_status not equal to expected_state: $expected_state"
264 echo "waiting disabled compute host back to be enabled..."
265 python ./nova_force_down.py "$COMPUTE_HOST" --unset
267 check_host_status "UP"
268 ssh $ssh_opts_cpu "$COMPUTE_USER@$COMPUTE_IP" \
269 "[ -e disable_network.log ] && cat disable_network.log"
271 openstack $as_doctor_user server list | grep -q " $VM_NAME " && openstack $as_doctor_user server delete "$VM_NAME"
273 alarm_id=$(ceilometer $as_doctor_user alarm-list | grep " $ALARM_NAME " | awk '{print $2}')
275 [ -n "$alarm_id" ] && ceilometer $as_doctor_user alarm-delete "$alarm_id"
278 image_id=$(openstack image list | grep " $IMAGE_NAME " | awk '{print $2}')
280 [ -n "$image_id" ] && openstack image delete "$image_id"
281 openstack role remove "$DOCTOR_ROLE" --user "$DOCTOR_USER" \
282 --project "$DOCTOR_PROJECT"
283 openstack project delete "$DOCTOR_PROJECT"
284 openstack user delete "$DOCTOR_USER"
288 echo "Note: doctor/tests/run.sh has been executed."
292 echo "preparing VM image..."
296 echo "creating test user..."
299 echo "creating VM..."
302 openstack $as_doctor_user server show $VM_NAME
304 echo "get computer host info and prepare to ssh..."
305 get_compute_host_info
308 echo "creating alarm..."
312 echo "starting doctor sample components..."
318 echo "injecting host failure..."
322 check_host_status "(DOWN|UNKNOWN)"
323 calculate_notification_time