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 ##############################################################################
13 [[ "${CI_DEBUG:-true}" == [Tt]rue ]] && set -x
15 IMAGE_URL=https://launchpad.net/cirros/trunk/0.3.0/+download/cirros-0.3.0-x86_64-disk.img
16 #if an existing image name is provided in the enviroment, use that one
17 IMAGE_NAME=${IMAGE_NAME:-cirros}
18 IMAGE_FILE="${IMAGE_NAME}.img"
22 ALARM_NAME=doctor_alarm1
28 #TODO: change back to `_member_` when JIRA DOCTOR-55 is done
30 PROFILER_TYPE=${PROFILER_TYPE:-none}
32 TOP_DIR=$(cd $(dirname "$0") && pwd)
34 as_doctor_user="--os-username $DOCTOR_USER --os-password $DOCTOR_PW
35 --os-tenant-name $DOCTOR_PROJECT"
40 get_compute_host_info() {
41 # get computer host info which VM boot in
42 COMPUTE_HOST=$(openstack $as_doctor_user server show $VM_NAME |
43 grep "OS-EXT-SRV-ATTR:host" | awk '{ print $4 }')
44 compute_host_in_undercloud=${COMPUTE_HOST%%.*}
45 die_if_not_set $LINENO COMPUTE_HOST "Failed to get compute hostname"
47 get_compute_ip_from_hostname $COMPUTE_HOST
49 echo "COMPUTE_HOST=$COMPUTE_HOST"
50 echo "COMPUTE_IP=$COMPUTE_IP"
52 # verify connectivity to target compute host
53 ping -c 1 "$COMPUTE_IP"
54 if [[ $? -ne 0 ]] ; then
55 die $LINENO "Can not ping to computer host"
58 # verify ssh to target compute host
59 ssh $ssh_opts_cpu "$COMPUTE_USER@$COMPUTE_IP" 'exit'
60 if [[ $? -ne 0 ]] ; then
61 die $LINENO "Can not ssh to computer host"
65 # TODO(r-mibu): update this function to support consumer instance
66 # and migrate this function into installer lib
67 get_consumer_ip___to_be_removed() {
68 local get_consumer_command="ip route get $COMPUTE_IP | awk '/ src /{print \$NF}'"
69 if is_installer apex; then
70 CONSUMER_IP=$(sudo ssh $ssh_opts root@$INSTALLER_IP \
71 "$get_consumer_command")
72 elif is_installer fuel; then
73 CONSUMER_IP=$(sudo sshpass -p r00tme ssh $ssh_opts root@${INSTALLER_IP} \
74 "$get_consumer_command")
75 elif is_installer local; then
76 CONSUMER_IP=`$get_consumer_command`
78 echo "CONSUMER_IP=$CONSUMER_IP"
80 die_if_not_set $LINENO CONSUMER_IP "Could not get CONSUMER_IP."
84 #if a different name was provided for the image in the enviroment there's no need to download the image
85 use_existing_image=false
86 openstack image list | grep -q " $IMAGE_NAME " && use_existing_image=true
88 if [[ "$use_existing_image" == false ]] ; then
89 [ -e "$IMAGE_FILE" ] && return 0
90 wget "$IMAGE_URL" -o "$IMAGE_FILE"
95 openstack image list | grep -q " $IMAGE_NAME " && return 0
96 openstack image create "$IMAGE_NAME" \
98 --disk-format "$IMAGE_FORMAT" \
99 --container-format bare \
104 openstack project list | grep -q " $DOCTOR_PROJECT " || {
105 openstack project create "$DOCTOR_PROJECT"
107 openstack user list | grep -q " $DOCTOR_USER " || {
108 openstack user create "$DOCTOR_USER" --password "$DOCTOR_PW" \
109 --project "$DOCTOR_PROJECT"
111 openstack user role list "$DOCTOR_USER" --project "$DOCTOR_PROJECT" \
112 | grep -q " $DOCTOR_ROLE " || {
113 openstack role add "$DOCTOR_ROLE" --user "$DOCTOR_USER" \
114 --project "$DOCTOR_PROJECT"
119 # test VM done with test user, so can test non-admin
120 openstack $as_doctor_user server list | grep -q " $VM_NAME " && return 0
121 openstack $as_doctor_user server create --flavor "$VM_FLAVOR" \
122 --image "$IMAGE_NAME" \
128 # get vm_id as test user
129 ceilometer $as_doctor_user alarm-list | grep -q " $ALARM_NAME " && return 0
130 vm_id=$(openstack $as_doctor_user server list | grep " $VM_NAME " | awk '{print $2}')
131 # TODO(r-mibu): change notification endpoint from localhost to the consumer
132 # IP address (functest container).
133 ceilometer $as_doctor_user alarm-event-create --name "$ALARM_NAME" \
134 --alarm-action "http://localhost:$CONSUMER_PORT/failure" \
135 --description "VM failure" \
137 --repeat-actions False \
138 --severity "moderate" \
139 --event-type compute.instance.update \
140 -q "traits.state=string::error; traits.instance_id=string::$vm_id"
144 pgrep -f "python monitor.py" && return 0
145 sudo -E python monitor.py "$COMPUTE_HOST" "$COMPUTE_IP" "$INSPECTOR_TYPE" \
146 "http://127.0.0.1:$INSPECTOR_PORT/events" > monitor.log 2>&1 &
150 pgrep -f "python monitor.py" || return 0
151 sudo kill $(pgrep -f "python monitor.py")
155 pgrep -f "python consumer.py" && return 0
156 python consumer.py "$CONSUMER_PORT" > consumer.log 2>&1 &
158 # NOTE(r-mibu): create tunnel to the controller nodes, so that we can
159 # avoid some network problems dpends on infra and installers.
160 # This tunnel will be terminated by stop_consumer() or after 10 mins passed.
161 if ! is_installer local; then
162 for ip in $CONTROLLER_IPS
164 forward_rule="-R $CONSUMER_PORT:localhost:$CONSUMER_PORT"
165 tunnel_command="sudo ssh $ssh_opts_cpu $COMPUTE_USER@$ip $forward_rule sleep 600"
166 $tunnel_command > "ssh_tunnel.${ip}.log" 2>&1 < /dev/null &
172 pgrep -f "python consumer.py" || return 0
173 kill $(pgrep -f "python consumer.py")
175 # NOTE(r-mibu): terminate tunnels to the controller nodes
176 if ! is_installer local; then
177 for ip in $CONTROLLER_IPS
179 forward_rule="-R $CONSUMER_PORT:localhost:$CONSUMER_PORT"
180 tunnel_command="sudo ssh $ssh_opts_cpu $COMPUTE_USER@$ip $forward_rule sleep 600"
181 kill $(pgrep -f "$tunnel_command")
186 wait_for_vm_launch() {
187 echo "waiting for vm launch..."
190 while [[ ${count} -lt 60 ]]
192 state=$(openstack $as_doctor_user server list | grep " $VM_NAME " | awk '{print $6}')
193 if [[ "$state" == "ACTIVE" ]]; then
194 # NOTE(cgoncalves): sleeping for a bit to stabilize
195 # See python-openstackclient/functional/tests/compute/v2/test_server.py:wait_for_status
199 if [[ "$state" == "ERROR" ]]; then
200 openstack $as_doctor_user server show $VM_NAME
201 die $LINENO "vm state is ERROR"
206 die $LINENO "Time out while waiting for VM launch"
210 echo "disabling network of compute host [$COMPUTE_HOST] for 3 mins..."
211 cat > disable_network.sh << 'END_TXT'
213 dev=$(sudo ip a | awk '/ @COMPUTE_IP@\//{print $7}')
214 [[ -n "$dev" ]] || dev=$(sudo ip a | awk '/ @COMPUTE_IP@\//{print $5}')
216 sudo ip link set $dev down
217 echo "doctor set link down at" $(date "+%s.%N")
219 sudo ip link set $dev up
222 sed -i -e "s/@COMPUTE_IP@/$COMPUTE_IP/" disable_network.sh
223 chmod +x disable_network.sh
224 scp $ssh_opts_cpu disable_network.sh "$COMPUTE_USER@$COMPUTE_IP:"
225 ssh $ssh_opts_cpu "$COMPUTE_USER@$COMPUTE_IP" 'nohup ./disable_network.sh > disable_network.log 2>&1 &'
226 # use host time to get rid of potential time sync deviation between nodes
227 triggered=$(date "+%s.%N")
230 calculate_notification_time() {
231 if ! grep -q "doctor consumer notified at" consumer.log ; then
232 die $LINENO "Consumer hasn't received fault notification."
235 #keep 'at' as the last keyword just before the value, and
236 #use regex to get value instead of the fixed column
237 detected=$(grep "doctor monitor detected at" monitor.log |\
238 sed -e "s/^.* at //")
239 notified=$(grep "doctor consumer notified at" consumer.log |\
240 sed -e "s/^.* at //")
242 echo "$notified $detected" | \
245 if (d < 1 && d > 0) { print d " OK"; exit 0 }
246 else { print d " NG"; exit 1 }
250 check_host_status() {
253 host_status_line=$(openstack $as_doctor_user --os-compute-api-version 2.16 \
254 server show $VM_NAME | grep "host_status")
255 host_status=$(echo $host_status_line | awk '{print $4}')
256 die_if_not_set $LINENO host_status "host_status not reported by: nova show $VM_NAME"
257 if [[ "$expected_state" =~ "$host_status" ]] ; then
258 echo "$VM_NAME showing host_status: $host_status"
260 die $LINENO "host_status:$host_status not equal to expected_state: $expected_state"
264 unset_forced_down_hosts() {
265 for host in $(openstack compute service list --service nova-compute \
266 -f value -c Host -c State | sed -n -e '/down$/s/ *down$//p')
268 # TODO (r-mibu): make sample inspector use keystone v3 api
269 OS_AUTH_URL=${OS_AUTH_URL/v3/v2.0} \
270 python ./nova_force_down.py $host --unset
273 echo "waiting disabled compute host back to be enabled..."
274 wait_until 'openstack compute service list --service nova-compute
275 -f value -c State | grep -q down' 240 5
279 unset_forced_down_hosts
280 # TODO: We need to make sure the target compute host is back to IP
281 # reachable. wait_ping() will be added by tojuvone .
283 scp $ssh_opts_cpu "$COMPUTE_USER@$COMPUTE_IP:disable_network.log" .
285 # TODO(yujunz) collect other logs, e.g. nova, aodh
289 if [[ "$PROFILER_TYPE" == "poc" ]]; then
290 linkdown=$(grep "doctor set link down at " disable_network.log |\
291 sed -e "s/^.* at //")
292 vmdown=$(grep "doctor mark vm.* error at" inspector.log |tail -n 1 |\
293 sed -e "s/^.* at //")
294 hostdown=$(grep "doctor mark host.* down at" inspector.log |\
295 sed -e "s/^.* at //")
297 # TODO(yujunz) check the actual delay to verify time sync status
298 # expected ~1s delay from $trigger to $linkdown
299 relative_start=${linkdown}
300 export DOCTOR_PROFILER_T00=$(python -c \
301 "print(int(($linkdown-$relative_start)*1000))")
302 export DOCTOR_PROFILER_T01=$(python -c \
303 "print(int(($detected-$relative_start)*1000))")
304 export DOCTOR_PROFILER_T03=$(python -c \
305 "print(int(($vmdown-$relative_start)*1000))")
306 export DOCTOR_PROFILER_T04=$(python -c \
307 "print(int(($hostdown-$relative_start)*1000))")
308 export DOCTOR_PROFILER_T09=$(python -c \
309 "print(int(($notified-$relative_start)*1000))")
311 python profiler-poc.py >doctor_profiler.log 2>&1
322 unset_forced_down_hosts
323 # TODO: We need to make sure the target compute host is back to IP
324 # reachable. wait_ping() will be added by tojuvone .
326 if is_set COMPUTE_IP; then
327 scp $ssh_opts_cpu "$COMPUTE_USER@$COMPUTE_IP:disable_network.log" .
330 openstack $as_doctor_user server list | grep -q " $VM_NAME " && openstack $as_doctor_user server delete "$VM_NAME"
332 alarm_id=$(ceilometer $as_doctor_user alarm-list | grep " $ALARM_NAME " | awk '{print $2}')
334 [ -n "$alarm_id" ] && ceilometer $as_doctor_user alarm-delete "$alarm_id"
337 image_id=$(openstack image list | grep " $IMAGE_NAME " | awk '{print $2}')
339 #if an existing image was used, there's no need to remove it here
340 if [[ "$use_existing_image" == false ]] ; then
341 [ -n "$image_id" ] && openstack image delete "$image_id"
343 openstack role remove "$DOCTOR_ROLE" --user "$DOCTOR_USER" \
344 --project "$DOCTOR_PROJECT"
345 openstack project delete "$DOCTOR_PROJECT"
346 openstack user delete "$DOCTOR_USER"
354 echo "Note: doctor/tests/run.sh has been executed."
355 git log --oneline -1 || true # ignore even you don't have git installed
359 source $TOP_DIR/functions-common
360 source $TOP_DIR/lib/installer
361 source $TOP_DIR/lib/inspector
365 echo "preparing VM image..."
369 echo "creating test user..."
372 echo "creating VM..."
376 echo "get computer host info..."
377 get_compute_host_info
379 echo "creating alarm..."
380 #TODO: change back to use, network problems depends on infra and installers
384 echo "starting doctor sample components..."
390 echo "injecting host failure..."
394 check_host_status "(DOWN|UNKNOWN)"
395 calculate_notification_time