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_INSPECTOR_TYPES="sample congress"
30 INSPECTOR_TYPE=${INSPECTOR_TYPE:-sample}
31 TOP_DIR=$(cd $(dirname "$0") && pwd)
33 ssh_opts="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
34 as_doctor_user="--os-username $DOCTOR_USER --os-password $DOCTOR_PW
35 --os-tenant-name $DOCTOR_PROJECT"
37 if [[ ! "$SUPPORTED_INSPECTOR_TYPES" =~ "$INSPECTOR_TYPE" ]] ; then
38 echo "ERROR: INSPECTOR_TYPE=$INSPECTOR_TYPE is not supported."
42 get_compute_host_info() {
43 # get computer host info which VM boot in
44 COMPUTE_HOST=$(openstack $as_doctor_user server show $VM_NAME |
45 grep "OS-EXT-SRV-ATTR:host" | awk '{ print $4 }')
46 compute_host_in_undercloud=${COMPUTE_HOST%%.*}
47 die_if_not_set $LINENO COMPUTE_HOST "Failed to get compute hostname"
49 if is_installer apex; then
50 COMPUTE_USER=${COMPUTE_USER:-heat-admin}
51 COMPUTE_IP=$(sudo ssh $ssh_opts $INSTALLER_IP \
53 nova show $compute_host_in_undercloud \
54 | awk '/ ctlplane network /{print \$5}'")
55 elif is_installer fuel; then
56 COMPUTE_USER=${COMPUTE_USER:-root}
57 node_id=$(echo $compute_host_in_undercloud | cut -d "-" -f 2)
58 COMPUTE_IP=$(sshpass -p r00tme ssh 2>/dev/null $ssh_opts root@${INSTALLER_IP} \
59 "fuel node|awk -F '|' -v id=$node_id '{if (\$1 == id) print \$5}' |xargs")
60 elif is_installer local; then
61 COMPUTE_USER=${COMPUTE_USER:-$(whoami)}
62 COMPUTE_IP=$(getent hosts "$COMPUTE_HOST" | awk '{ print $1 }')
65 die_if_not_set $LINENO COMPUTE_IP "Could not resolve $COMPUTE_HOST. Either manually set COMPUTE_IP or enable DNS resolution."
66 echo "COMPUTE_HOST=$COMPUTE_HOST"
67 echo "COMPUTE_IP=$COMPUTE_IP"
69 # verify connectivity to target compute host
70 ping -c 1 "$COMPUTE_IP"
71 if [[ $? -ne 0 ]] ; then
72 die $LINENO "Can not ping to computer host"
75 # verify ssh to target compute host
76 ssh $ssh_opts_cpu "$COMPUTE_USER@$COMPUTE_IP" 'exit'
77 if [[ $? -ne 0 ]] ; then
78 die $LINENO "Can not ssh to computer host"
83 local get_consumer_command="ip route get $COMPUTE_IP | awk '/ src /{print \$NF}'"
84 if is_installer apex; then
85 CONSUMER_IP=$(sudo ssh $ssh_opts root@$INSTALLER_IP \
86 "$get_consumer_command")
87 elif is_installer fuel; then
88 CONSUMER_IP=$(sudo sshpass -p r00tme ssh $ssh_opts root@${INSTALLER_IP} \
89 "$get_consumer_command")
90 elif is_installer local; then
91 CONSUMER_IP=`$get_consumer_command`
93 echo "CONSUMER_IP=$CONSUMER_IP"
95 die_if_not_set $LINENO CONSUMER_IP "Could not get CONSUMER_IP."
99 #if a different name was provided for the image in the enviroment there's no need to download the image
100 use_existing_image=false
101 openstack image list | grep -q " $IMAGE_NAME " && use_existing_image=true
103 if [[ "$use_existing_image" == false ]] ; then
104 [ -e "$IMAGE_FILE" ] && return 0
105 wget "$IMAGE_URL" -o "$IMAGE_FILE"
110 openstack image list | grep -q " $IMAGE_NAME " && return 0
111 openstack image create "$IMAGE_NAME" \
113 --disk-format "$IMAGE_FORMAT" \
114 --container-format bare \
119 openstack project list | grep -q " $DOCTOR_PROJECT " || {
120 openstack project create "$DOCTOR_PROJECT"
122 openstack user list | grep -q " $DOCTOR_USER " || {
123 openstack user create "$DOCTOR_USER" --password "$DOCTOR_PW" \
124 --project "$DOCTOR_PROJECT"
126 openstack user role list "$DOCTOR_USER" --project "$DOCTOR_PROJECT" \
127 | grep -q " $DOCTOR_ROLE " || {
128 openstack role add "$DOCTOR_ROLE" --user "$DOCTOR_USER" \
129 --project "$DOCTOR_PROJECT"
134 # test VM done with test user, so can test non-admin
135 openstack $as_doctor_user server list | grep -q " $VM_NAME " && return 0
136 openstack $as_doctor_user server create --flavor "$VM_FLAVOR" \
137 --image "$IMAGE_NAME" \
143 # get vm_id as test user
144 ceilometer $as_doctor_user alarm-list | grep -q " $ALARM_NAME " && return 0
145 vm_id=$(openstack $as_doctor_user server list | grep " $VM_NAME " | awk '{print $2}')
146 # TODO(r-mibu): change notification endpoint from localhost to the consumer
147 # IP address (functest container).
148 ceilometer $as_doctor_user alarm-event-create --name "$ALARM_NAME" \
149 --alarm-action "http://localhost:$CONSUMER_PORT/failure" \
150 --description "VM failure" \
152 --repeat-actions False \
153 --severity "moderate" \
154 --event-type compute.instance.update \
155 -q "traits.state=string::error; traits.instance_id=string::$vm_id"
159 pgrep -f "python monitor.py" && return 0
160 sudo -E python monitor.py "$COMPUTE_HOST" "$COMPUTE_IP" "$INSPECTOR_TYPE" \
161 "http://127.0.0.1:$INSPECTOR_PORT/events" > monitor.log 2>&1 &
165 pgrep -f "python monitor.py" || return 0
166 sudo kill $(pgrep -f "python monitor.py")
169 congress_add_rule() {
174 if ! openstack congress policy rule list $policy | grep -q -e "// Name: $name$" ; then
175 openstack congress policy rule create --name $name $policy "$rule"
179 congress_del_rule() {
183 if openstack congress policy rule list $policy | grep -q -e "^// Name: $name$" ; then
184 openstack congress policy rule delete $policy $name
188 congress_setup_rules() {
189 congress_add_rule host_down classification \
191 doctor:events(hostname=host, type="compute.host.down", status="down")'
193 congress_add_rule active_instance_in_host classification \
194 'active_instance_in_host(vmid, host) :-
195 nova:servers(id=vmid, host_name=host, status="ACTIVE")'
197 congress_add_rule host_force_down classification \
198 'execute[nova:services.force_down(host, "nova-compute", "True")] :-
201 congress_add_rule error_vm_states classification \
202 'execute[nova:servers.reset_state(vmid, "error")] :-
204 active_instance_in_host(vmid, host)'
208 if [[ "$INSPECTOR_TYPE" == "sample" ]] ; then
209 pgrep -f "python inspector.py" && return 0
210 python inspector.py "$INSPECTOR_PORT" > inspector.log 2>&1 &
211 elif [[ "$INSPECTOR_TYPE" == "congress" ]] ; then
212 nova_api_min_version="2.11"
213 nova_api_version=$(openstack congress datasource list | \
214 grep nova | grep -Po "(?<='api_version': ')[^']*")
215 if ! is_set nova_api_version; then
216 nova_api_version="2.0"
218 if [[ "$nova_api_version" < "$nova_api_min_version" ]]; then
219 die $LINENO "Congress Nova datasource API version < $nova_api_min_version ($nova_api_version)"
221 openstack congress driver list | grep -q " doctor "
222 openstack congress datasource list | grep -q " doctor " || {
223 openstack congress datasource create doctor doctor
230 if [[ "$INSPECTOR_TYPE" == "sample" ]] ; then
231 pgrep -f "python inspector.py" || return 0
232 kill $(pgrep -f "python inspector.py")
233 elif [[ "$INSPECTOR_TYPE" == "congress" ]] ; then
234 congress_del_rule host_force_down classification
235 congress_del_rule error_vm_states classification
236 congress_del_rule active_instance_in_host classification
237 congress_del_rule host_down classification
242 pgrep -f "python consumer.py" && return 0
243 python consumer.py "$CONSUMER_PORT" > consumer.log 2>&1 &
245 # NOTE(r-mibu): create tunnel to the controller nodes, so that we can
246 # avoid some network problems dpends on infra and installers.
247 # This tunnel will be terminated by stop_consumer() or after 10 mins passed.
248 if ! is_installer local; then
249 if is_installer apex; then
250 CONTROLLER_IPS=$(sudo ssh $ssh_opts $INSTALLER_IP \
252 nova list | grep ' overcloud-controller-[0-9] ' \
253 | sed -e 's/^.*ctlplane=//' -e 's/ *|\$//'")
254 elif is_installer fuel; then
255 CONTROLLER_IPS=$(sshpass -p r00tme ssh 2>/dev/null $ssh_opts root@${INSTALLER_IP} \
256 "fuel node | grep controller | cut -d '|' -f 5|xargs")
259 die_if_not_set $LINENO CONTROLLER_IPS "Could not get CONTROLLER_IPS."
260 for ip in $CONTROLLER_IPS
262 forward_rule="-R $CONSUMER_PORT:localhost:$CONSUMER_PORT"
263 tunnel_command="sudo ssh $ssh_opts_cpu $COMPUTE_USER@$ip $forward_rule sleep 600"
264 $tunnel_command > "ssh_tunnel.${ip}.log" 2>&1 < /dev/null &
270 pgrep -f "python consumer.py" || return 0
271 kill $(pgrep -f "python consumer.py")
273 # NOTE(r-mibu): terminate tunnels to the controller nodes
274 if ! is_installer local; then
275 for ip in $CONTROLLER_IPS
277 forward_rule="-R $CONSUMER_PORT:localhost:$CONSUMER_PORT"
278 tunnel_command="sudo ssh $ssh_opts_cpu $COMPUTE_USER@$ip $forward_rule sleep 600"
279 kill $(pgrep -f "$tunnel_command")
284 wait_for_vm_launch() {
285 echo "waiting for vm launch..."
288 while [[ ${count} -lt 60 ]]
290 state=$(openstack $as_doctor_user server list | grep " $VM_NAME " | awk '{print $6}')
291 if [[ "$state" == "ACTIVE" ]]; then
292 # NOTE(cgoncalves): sleeping for a bit to stabilize
293 # See python-openstackclient/functional/tests/compute/v2/test_server.py:wait_for_status
297 if [[ "$state" == "ERROR" ]]; then
298 die $LINENO "vm state is ERROR"
303 die $LINENO "Time out while waiting for VM launch"
307 echo "disabling network of compute host [$COMPUTE_HOST] for 3 mins..."
308 cat > disable_network.sh << 'END_TXT'
310 dev=$(sudo ip a | awk '/ @COMPUTE_IP@\//{print $7}')
312 sudo ip link set $dev down
314 sudo ip link set $dev up
317 sed -i -e "s/@COMPUTE_IP@/$COMPUTE_IP/" disable_network.sh
318 chmod +x disable_network.sh
319 scp $ssh_opts_cpu disable_network.sh "$COMPUTE_USER@$COMPUTE_IP:"
320 ssh $ssh_opts_cpu "$COMPUTE_USER@$COMPUTE_IP" 'nohup ./disable_network.sh > disable_network.log 2>&1 &'
323 calculate_notification_time() {
324 detected=$(grep "doctor monitor detected at" monitor.log | awk '{print $10}')
325 notified=$(grep "doctor consumer notified at" consumer.log | awk '{print $10}')
326 if ! grep -q "doctor consumer notified at" consumer.log ; then
327 die $LINENO "Consumer hasn't received fault notification."
329 echo "$notified $detected" | \
332 if (d < 1 && d > 0) { print d " OK"; exit 0 }
333 else { print d " NG"; exit 1 }
337 check_host_status() {
340 host_status_line=$(openstack $as_doctor_user --os-compute-api-version 2.16 \
341 server show $VM_NAME | grep "host_status")
342 host_status=$(echo $host_status_line | awk '{print $4}')
343 die_if_not_set $LINENO host_status "host_status not reported by: nova show $VM_NAME"
344 if [[ "$expected_state" =~ "$host_status" ]] ; then
345 echo "$VM_NAME showing host_status: $host_status"
347 die $LINENO "host_status:$host_status not equal to expected_state: $expected_state"
358 echo "waiting disabled compute host back to be enabled..."
359 python ./nova_force_down.py "$COMPUTE_HOST" --unset
361 check_host_status "UP"
362 scp $ssh_opts_cpu "$COMPUTE_USER@$COMPUTE_IP:disable_network.log" .
364 openstack $as_doctor_user server list | grep -q " $VM_NAME " && openstack $as_doctor_user server delete "$VM_NAME"
366 alarm_id=$(ceilometer $as_doctor_user alarm-list | grep " $ALARM_NAME " | awk '{print $2}')
368 [ -n "$alarm_id" ] && ceilometer $as_doctor_user alarm-delete "$alarm_id"
371 image_id=$(openstack image list | grep " $IMAGE_NAME " | awk '{print $2}')
373 #if an existing image was used, there's no need to remove it here
374 if [[ "$use_existing_image" == false ]] ; then
375 [ -n "$image_id" ] && openstack image delete "$image_id"
377 openstack role remove "$DOCTOR_ROLE" --user "$DOCTOR_USER" \
378 --project "$DOCTOR_PROJECT"
379 openstack project delete "$DOCTOR_PROJECT"
380 openstack user delete "$DOCTOR_USER"
386 echo "Note: doctor/tests/run.sh has been executed."
390 source $TOP_DIR/functions-common
391 source $TOP_DIR/lib/installer
395 echo "preparing VM image..."
399 echo "creating test user..."
402 echo "creating VM..."
406 echo "get computer host info..."
407 get_compute_host_info
409 echo "creating alarm..."
410 #TODO: change back to use, network problems depends on infra and installers
414 echo "starting doctor sample components..."
420 echo "injecting host failure..."
424 check_host_status "(DOWN|UNKNOWN)"
425 calculate_notification_time