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 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 compute_host_in_undercloud=${COMPUTE_HOST%%.*}
46 if [[ -z "$COMPUTE_HOST" ]] ; then
47 echo "ERROR: failed to get compute hostname"
51 if [[ "$INSTALLER_TYPE" == "apex" ]] ; then
52 COMPUTE_USER=${COMPUTE_USER:-heat-admin}
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 COMPUTE_IP=$(sudo ssh $ssh_opts $INSTALLER_IP \
59 nova show $compute_host_in_undercloud \
60 | awk '/ ctlplane network /{print \$5}'")
61 elif [[ "$INSTALLER_TYPE" == "fuel" ]] ; then
62 COMPUTE_USER=${COMPUTE_USER:-root}
63 if [[ "$INSTALLER_IP" == "none" ]] ; then
64 instack_mac=$(sudo virsh domiflist fuel-opnfv | awk '/pxebr/{print $5}')
65 INSTALLER_IP=$(/usr/sbin/arp -e | grep ${instack_mac} | awk '{print $1}')
67 node_id=$(echo $compute_host_in_undercloud | cut -d "-" -f 2)
68 COMPUTE_IP=$(sshpass -p r00tme ssh 2>/dev/null $ssh_opts root@${INSTALLER_IP} \
69 "fuel node|awk -F '|' -v id=$node_id '{if (\$1 == id) print \$5}' |xargs")
70 elif [[ "$INSTALLER_TYPE" == "local" ]] ; then
71 COMPUTE_USER=${COMPUTE_USER:-$(whoami)}
72 COMPUTE_IP=$(getent hosts "$COMPUTE_HOST" | awk '{ print $1 }')
75 if [[ -z "$COMPUTE_IP" ]]; then
76 echo "ERROR: Could not resolve $COMPUTE_HOST. Either manually set COMPUTE_IP or enable DNS resolution."
79 echo "COMPUTE_HOST=$COMPUTE_HOST"
80 echo "COMPUTE_IP=$COMPUTE_IP"
82 # verify connectivity to target compute host
83 ping -c 1 "$COMPUTE_IP"
84 if [[ $? -ne 0 ]] ; then
85 echo "ERROR: can not ping to computer host"
90 prepare_compute_ssh() {
91 ssh_opts_cpu="$ssh_opts"
93 # get ssh key from installer node
94 if [[ "$INSTALLER_TYPE" == "apex" ]] ; then
95 sudo scp $ssh_opts root@"$INSTALLER_IP":/home/stack/.ssh/id_rsa instack_key
96 sudo chown $(whoami):$(whoami) instack_key
98 ssh_opts_cpu+=" -i instack_key"
99 elif [[ "$INSTALLER_TYPE" == "fuel" ]] ; then
100 sshpass -p r00tme scp $ssh_opts root@${INSTALLER_IP}:.ssh/id_rsa instack_key
101 sudo chown $(whoami):$(whoami) instack_key
102 chmod 400 instack_key
103 ssh_opts_cpu+=" -i instack_key"
104 elif [[ "$INSTALLER_TYPE" == "local" ]] ; then
105 echo "INSTALLER_TYPE set to 'local'. Assuming SSH keys already exchanged with $COMPUTE_HOST"
108 # verify ssh to target compute host
109 ssh $ssh_opts_cpu "$COMPUTE_USER@$COMPUTE_IP" 'exit'
110 if [[ $? -ne 0 ]] ; then
111 echo "ERROR: can not ssh to computer host"
117 CONSUMER_IP=$(sudo ssh $ssh_opts root@$INSTALLER_IP \
118 "ip route get $COMPUTE_IP | awk '/ src /{print \$NF}'")
119 echo "CONSUMER_IP=$CONSUMER_IP"
121 if [[ -z "$CONSUMER_IP" ]]; then
122 echo "ERROR: Could not get CONSUMER_IP."
128 [ -e "$IMAGE_FILE" ] && return 0
129 wget "$IMAGE_URL" -o "$IMAGE_FILE"
133 openstack image list | grep -q " $IMAGE_NAME " && return 0
134 openstack image create "$IMAGE_NAME" \
136 --disk-format "$IMAGE_FORMAT" \
137 --container-format bare \
142 openstack project list | grep -q " $DOCTOR_PROJECT " || {
143 openstack project create "$DOCTOR_PROJECT"
145 openstack user list | grep -q " $DOCTOR_USER " || {
146 openstack user create "$DOCTOR_USER" --password "$DOCTOR_PW" \
147 --project "$DOCTOR_PROJECT"
149 openstack user role list "$DOCTOR_USER" --project "$DOCTOR_PROJECT" \
150 | grep -q " $DOCTOR_ROLE " || {
151 openstack role add "$DOCTOR_ROLE" --user "$DOCTOR_USER" \
152 --project "$DOCTOR_PROJECT"
157 # test VM done with test user, so can test non-admin
158 openstack $as_doctor_user server list | grep -q " $VM_NAME " && return 0
159 openstack $as_doctor_user server create --flavor "$VM_FLAVOR" \
160 --image "$IMAGE_NAME" \
166 # get vm_id as test user
167 ceilometer $as_doctor_user alarm-list | grep -q " $ALARM_NAME " && return 0
168 vm_id=$(openstack $as_doctor_user server list | grep " $VM_NAME " | awk '{print $2}')
169 ceilometer $as_doctor_user alarm-event-create --name "$ALARM_NAME" \
170 --alarm-action "http://$CONSUMER_IP:$CONSUMER_PORT/failure" \
171 --description "VM failure" \
173 --repeat-actions False \
174 --severity "moderate" \
175 --event-type compute.instance.update \
176 -q "traits.state=string::error; traits.instance_id=string::$vm_id"
182 sed -e 's/^/ /' "$log_file"
186 pgrep -f "python monitor.py" && return 0
187 sudo python monitor.py "$COMPUTE_HOST" "$COMPUTE_IP" \
188 "http://127.0.0.1:$INSPECTOR_PORT/events" > monitor.log 2>&1 &
192 pgrep -f "python monitor.py" || return 0
193 sudo kill $(pgrep -f "python monitor.py")
194 print_log monitor.log
198 pgrep -f "python inspector.py" && return 0
199 python inspector.py "$INSPECTOR_PORT" > inspector.log 2>&1 &
203 pgrep -f "python inspector.py" || return 0
204 kill $(pgrep -f "python inspector.py")
205 print_log inspector.log
209 pgrep -f "python consumer.py" && return 0
210 python consumer.py "$CONSUMER_PORT" > consumer.log 2>&1 &
211 # NOTE(r-mibu): create tunnel to the installer node, so that we can
212 # avoid some network problems dpends on infra and installers.
213 # This tunnel will be terminated by stop_consumer() or after 10 mins passed.
214 TUNNEL_COMMAND="sudo ssh $ssh_opts $INSTALLER_IP -R $CONSUMER_PORT:localhost:$CONSUMER_PORT 'sleep 600'"
215 $TUNNEL_COMMAND > ssh_tunnel.log 2>&1 < /dev/null &
219 pgrep -f "python consumer.py" || return 0
220 kill $(pgrep -f "python consumer.py")
221 print_log consumer.log
222 kill $(pgrep -f "$TUNNEL_COMMAND")
223 print_log ssh_tunnel.log
226 wait_for_vm_launch() {
227 echo "waiting for vm launch..."
230 while [[ ${count} -lt 60 ]]
232 state=$(openstack $as_doctor_user server list | grep " $VM_NAME " | awk '{print $6}')
233 [[ "$state" == "ACTIVE" ]] && return 0
234 [[ "$state" == "ERROR" ]] && echo "vm state is ERROR" && exit 1
238 echo "ERROR: time out while waiting for vm launch"
243 echo "disabling network of compute host [$COMPUTE_HOST] for 3 mins..."
244 cat > disable_network.sh << 'END_TXT'
246 dev=$(sudo ip a | awk '/ @COMPUTE_IP@\//{print $7}')
248 sudo ip link set $dev down
250 sudo ip link set $dev up
253 sed -i -e "s/@COMPUTE_IP@/$COMPUTE_IP/" disable_network.sh
254 chmod +x disable_network.sh
255 scp $ssh_opts_cpu disable_network.sh "$COMPUTE_USER@$COMPUTE_IP:"
256 ssh $ssh_opts_cpu "$COMPUTE_USER@$COMPUTE_IP" 'nohup ./disable_network.sh > disable_network.log 2>&1 &'
259 calculate_notification_time() {
260 detected=$(grep "doctor monitor detected at" monitor.log | awk '{print $5}')
261 notified=$(grep "doctor consumer notified at" consumer.log | awk '{print $5}')
262 if ! grep -q "doctor consumer notified at" consumer.log ; then
263 echo "ERROR: consumer hasn't received fault notification."
266 echo "$notified $detected" | \
267 awk '{d = $1 - $2; if (d < 1 && d > 0) print d " OK"; else print d " NG"}'
270 check_host_status() {
273 host_status_line=$(openstack $as_doctor_user --os-compute-api-version 2.16 \
274 server show $VM_NAME | grep "host_status")
275 host_status=$(echo $host_status_line | awk '{print $4}')
276 if [ -z "$host_status" ] ; then
277 echo "ERROR: host_status not reported by: nova show $VM_NAME"
279 elif [[ "$expected_state" =~ "$host_status" ]] ; then
280 echo "$VM_NAME showing host_status: $host_status"
282 echo "ERROR: host_status:$host_status not equal to expected_state: $expected_state"
294 echo "waiting disabled compute host back to be enabled..."
295 python ./nova_force_down.py "$COMPUTE_HOST" --unset
297 check_host_status "UP"
298 scp $ssh_opts_cpu "$COMPUTE_USER@$COMPUTE_IP:disable_network.log" .
299 print_log disable_network.log
301 openstack $as_doctor_user server list | grep -q " $VM_NAME " && openstack $as_doctor_user server delete "$VM_NAME"
303 alarm_id=$(ceilometer $as_doctor_user alarm-list | grep " $ALARM_NAME " | awk '{print $2}')
305 [ -n "$alarm_id" ] && ceilometer $as_doctor_user alarm-delete "$alarm_id"
308 image_id=$(openstack image list | grep " $IMAGE_NAME " | awk '{print $2}')
310 [ -n "$image_id" ] && openstack image delete "$image_id"
311 openstack role remove "$DOCTOR_ROLE" --user "$DOCTOR_USER" \
312 --project "$DOCTOR_PROJECT"
313 openstack project delete "$DOCTOR_PROJECT"
314 openstack user delete "$DOCTOR_USER"
318 echo "Note: doctor/tests/run.sh has been executed."
322 echo "preparing VM image..."
326 echo "creating test user..."
329 echo "creating VM..."
332 openstack $as_doctor_user server show $VM_NAME
334 echo "get computer host info and prepare to ssh..."
335 get_compute_host_info
338 echo "creating alarm..."
342 echo "starting doctor sample components..."
348 echo "injecting host failure..."
352 check_host_status "(DOWN|UNKNOWN)"
353 calculate_notification_time