Test Congress Doctor driver support
[doctor.git] / tests / run.sh
1 #!/bin/bash -e
2 ##############################################################################
3 # Copyright (c) 2016 NEC Corporation and others.
4 #
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 ##############################################################################
10
11 [[ "${CI_DEBUG:-true}" == [Tt]rue ]] && set -x
12
13 IMAGE_URL=https://launchpad.net/cirros/trunk/0.3.0/+download/cirros-0.3.0-x86_64-disk.img
14 IMAGE_NAME=cirros
15 IMAGE_FILE="${IMAGE_NAME}.img"
16 IMAGE_FORMAT=qcow2
17 VM_NAME=doctor_vm1
18 VM_FLAVOR=m1.tiny
19 ALARM_NAME=doctor_alarm1
20 INSPECTOR_PORT=12345
21 CONSUMER_PORT=12346
22 DOCTOR_USER=doctor
23 DOCTOR_PW=doctor
24 DOCTOR_PROJECT=doctor
25 #TODO: change back to `_member_` when JIRA DOCTOR-55 is done
26 DOCTOR_ROLE=admin
27
28 SUPPORTED_INSTALLER_TYPES="apex fuel local"
29 INSTALLER_TYPE=${INSTALLER_TYPE:-local}
30 INSTALLER_IP=${INSTALLER_IP:-none}
31
32 SUPPORTED_INSPECTOR_TYPES="sample congress"
33 INSPECTOR_TYPE=${INSPECTOR_TYPE:-sample}
34
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"
38
39 if [[ ! "$SUPPORTED_INSTALLER_TYPES" =~ "$INSTALLER_TYPE" ]] ; then
40     echo "ERROR: INSTALLER_TYPE=$INSTALLER_TYPE is not supported."
41     exit 1
42 fi
43
44 if [[ ! "$SUPPORTED_INSPECTOR_TYPES" =~ "$INSPECTOR_TYPE" ]] ; then
45     echo "ERROR: INSPECTOR_TYPE=$INSPECTOR_TYPE is not supported."
46     exit 1
47 fi
48
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"
56         exit 1
57     fi
58
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}')
64         fi
65         COMPUTE_IP=$(sudo ssh $ssh_opts $INSTALLER_IP \
66              "source stackrc; \
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}')
74         fi
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 }')
81     fi
82
83     if [[ -z "$COMPUTE_IP" ]]; then
84         echo "ERROR: Could not resolve $COMPUTE_HOST. Either manually set COMPUTE_IP or enable DNS resolution."
85         exit 1
86     fi
87     echo "COMPUTE_HOST=$COMPUTE_HOST"
88     echo "COMPUTE_IP=$COMPUTE_IP"
89
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"
94         exit 1
95     fi
96 }
97
98 prepare_compute_ssh() {
99     ssh_opts_cpu="$ssh_opts"
100
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"
114     fi
115
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"
120         exit 1
121     fi
122 }
123
124 get_consumer_ip() {
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"
128
129     if [[ -z "$CONSUMER_IP" ]]; then
130         echo "ERROR: Could not get CONSUMER_IP."
131         exit 1
132     fi
133 }
134
135 download_image() {
136     [ -e "$IMAGE_FILE" ] && return 0
137     wget "$IMAGE_URL" -o "$IMAGE_FILE"
138 }
139
140 register_image() {
141     openstack image list | grep -q " $IMAGE_NAME " && return 0
142     openstack image create "$IMAGE_NAME" \
143                            --public \
144                            --disk-format "$IMAGE_FORMAT" \
145                            --container-format bare \
146                            --file "$IMAGE_FILE"
147 }
148
149 create_test_user() {
150     openstack project list | grep -q " $DOCTOR_PROJECT " || {
151         openstack project create "$DOCTOR_PROJECT"
152     }
153     openstack user list | grep -q " $DOCTOR_USER " || {
154         openstack user create "$DOCTOR_USER" --password "$DOCTOR_PW" \
155                               --project "$DOCTOR_PROJECT"
156     }
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"
161     }
162 }
163
164 boot_vm() {
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" \
169                             "$VM_NAME"
170     sleep 1
171 }
172
173 create_alarm() {
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" \
182         --enabled True \
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"
187 }
188
189 print_log() {
190     log_file=$1
191     echo "$log_file:"
192     sed -e 's/^/    /' "$log_file"
193 }
194
195 start_monitor() {
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 &
199 }
200
201 stop_monitor() {
202     pgrep -f "python monitor.py" || return 0
203     sudo kill $(pgrep -f "python monitor.py")
204     print_log monitor.log
205 }
206
207 congress_add_rule() {
208     name=$1
209     policy=$2
210     rule=$3
211
212     if ! openstack congress policy rule list $policy | grep -q -e "// Name: $name$" ; then
213         openstack congress policy rule create --name $name $policy "$rule"
214     fi
215 }
216
217 congress_del_rule() {
218     name=$1
219     policy=$2
220
221     if openstack congress policy rule list $policy | grep -q -e "^// Name: $name$" ; then
222         openstack congress policy rule delete $policy $name
223     fi
224 }
225
226 congress_setup_rules() {
227     congress_add_rule host_down classification \
228         'host_down(host) :-
229             doctor:events(hostname=host, type="compute.host.down", status="down")'
230
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")'
234
235     congress_add_rule host_force_down classification \
236         'execute[nova:services.force_down(host, "nova-compute", "True")] :-
237             host_down(host)'
238
239     congress_add_rule error_vm_states classification \
240         'execute[nova:servers.reset_state(vmid, "error")] :-
241             host_down(host),
242             active_instance_in_host(vmid, host)'
243 }
244
245 start_inspector() {
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)"
256             exit 1
257         fi
258         openstack congress driver list | grep -q " doctor "
259         openstack congress datasource list | grep -q " doctor " || {
260             openstack congress datasource create doctor doctor
261         }
262         congress_setup_rules
263     fi
264 }
265
266 stop_inspector() {
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
276     fi
277 }
278
279 start_consumer() {
280     pgrep -f "python consumer.py" && return 0
281     python consumer.py "$CONSUMER_PORT" > consumer.log 2>&1 &
282
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 \
288                          "source stackrc; \
289                          nova list | grep ' overcloud-controller-[0-9] ' \
290                          | sed -e 's/^.*ctlplane=//' -e 's/ *|\$//'")
291     fi
292     if [[ -z "$CONTROLLER_IPS" ]]; then
293         echo "ERROR: Could not get CONTROLLER_IPS."
294         exit 1
295     fi
296     for ip in $CONTROLLER_IPS
297     do
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 &
301     done
302 }
303
304 stop_consumer() {
305     pgrep -f "python consumer.py" || return 0
306     kill $(pgrep -f "python consumer.py")
307     print_log consumer.log
308
309     # NOTE(r-mibu): terminate tunnels to the controller nodes
310     for ip in $CONTROLLER_IPS
311     do
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"
316     done
317 }
318
319 wait_for_vm_launch() {
320     echo "waiting for vm launch..."
321
322     count=0
323     while [[ ${count} -lt 60 ]]
324     do
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
328         count=$(($count+1))
329         sleep 1
330     done
331     echo "ERROR: time out while waiting for vm launch"
332     exit 1
333 }
334
335 inject_failure() {
336     echo "disabling network of compute host [$COMPUTE_HOST] for 3 mins..."
337     cat > disable_network.sh << 'END_TXT'
338 #!/bin/bash -x
339 dev=$(sudo ip a | awk '/ @COMPUTE_IP@\//{print $7}')
340 sleep 1
341 sudo ip link set $dev down
342 sleep 180
343 sudo ip link set $dev up
344 sleep 1
345 END_TXT
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 &'
350 }
351
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."
357         exit 1
358     fi
359     echo "$notified $detected" | \
360         awk '{d = $1 - $2; if (d < 1 && d > 0) print d " OK"; else print d " NG"}'
361 }
362
363 check_host_status() {
364     expected_state=$1
365
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"
371         exit 1
372     elif [[ "$expected_state" =~ "$host_status" ]] ; then
373         echo "$VM_NAME showing host_status: $host_status"
374     else
375         echo "ERROR: host_status:$host_status not equal to expected_state: $expected_state"
376         exit 1
377     fi
378 }
379
380 cleanup() {
381     set +e
382     echo "cleanup..."
383     stop_monitor
384     stop_inspector
385     stop_consumer
386
387     echo "waiting disabled compute host back to be enabled..."
388     python ./nova_force_down.py "$COMPUTE_HOST" --unset
389     sleep 240
390     check_host_status "UP"
391     scp $ssh_opts_cpu "$COMPUTE_USER@$COMPUTE_IP:disable_network.log" .
392     print_log disable_network.log
393
394     openstack $as_doctor_user server list | grep -q " $VM_NAME " && openstack $as_doctor_user server delete "$VM_NAME"
395     sleep 1
396     alarm_id=$(ceilometer $as_doctor_user alarm-list | grep " $ALARM_NAME " | awk '{print $2}')
397     sleep 1
398     [ -n "$alarm_id" ] && ceilometer $as_doctor_user alarm-delete "$alarm_id"
399     sleep 1
400
401     image_id=$(openstack image list | grep " $IMAGE_NAME " | awk '{print $2}')
402     sleep 1
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"
408 }
409
410
411 echo "Note: doctor/tests/run.sh has been executed."
412
413 trap cleanup EXIT
414
415 echo "preparing VM image..."
416 download_image
417 register_image
418
419 echo "creating test user..."
420 create_test_user
421
422 echo "creating VM..."
423 boot_vm
424 wait_for_vm_launch
425 openstack $as_doctor_user server show $VM_NAME
426
427 echo "get computer host info and prepare to ssh..."
428 get_compute_host_info
429 prepare_compute_ssh
430
431 echo "creating alarm..."
432 get_consumer_ip
433 create_alarm
434
435 echo "starting doctor sample components..."
436 start_inspector
437 start_monitor
438 start_consumer
439
440 sleep 60
441 echo "injecting host failure..."
442 inject_failure
443 sleep 60
444
445 check_host_status "(DOWN|UNKNOWN)"
446 calculate_notification_time
447
448 echo "done"