test: fix bug: missing installer_ip in scp args
[doctor.git] / tests / run.sh
1 #!/bin/bash -ex
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 IMAGE_URL=https://launchpad.net/cirros/trunk/0.3.0/+download/cirros-0.3.0-x86_64-disk.img
12 IMAGE_NAME=cirros
13 IMAGE_FILE="${IMAGE_NAME}.img"
14 IMAGE_FORMAT=qcow2
15 VM_NAME=doctor_vm1
16 VM_FLAVOR=m1.tiny
17 ALARM_NAME=doctor_alarm1
18 INSPECTOR_PORT=12345
19 CONSUMER_PORT=12346
20
21 INSTALLER_TYPE=${INSTALLER_TYPE:-apex}
22 INSTALLER_IP=${INSTALLER_IP:-none}
23 COMPUTE_HOST=${COMPUTE_HOST:-overcloud-novacompute-0}
24 COMPUTE_IP=${COMPUTE_IP:-none}
25 ssh_opts="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
26
27 if [[ "$INSTALLER_TYPE" != "apex" ]] ; then
28     echo "ERROR: INSTALLER_TYPE=$INSTALLER_TYPE is not supported."
29     exit 1
30 fi
31
32 if [[ "$INSTALLER_IP" == "none" ]] ; then
33     instack_mac=$(sudo virsh domiflist instack | awk '/default/{print $5}')
34     INSTALLER_IP=$(/usr/sbin/arp -e | grep ${instack_mac} | awk '{print $1}')
35 fi
36
37 if [[ "$COMPUTE_IP" == "none" ]] ; then
38     COMPUTE_IP=$(sudo ssh $ssh_opts $INSTALLER_IP \
39                  "source stackrc; \
40                   nova show $COMPUTE_HOST \
41                   | awk '/ ctlplane network /{print \$5}'")
42 fi
43
44 prepare_compute_ssh() {
45     # verify connectivity to target compute host
46     ping -c 1 "$COMPUTE_IP"
47
48     # get ssh key from installer node
49     sudo scp $ssh_opts root@"$INSTALLER_IP":/home/stack/.ssh/id_rsa instack_key
50     if [ ! -r instack_key ]; then
51         sudo chown $(whoami):$(whoami) instack_key
52     fi
53     chmod 400 instack_key
54     ssh_opts_cpu="$ssh_opts -i instack_key -l heat-admin"
55 }
56
57 download_image() {
58     [ -e "$IMAGE_FILE" ] && return 0
59     wget "$IMAGE_URL" -o "$IMAGE_FILE"
60 }
61
62 register_image() {
63     glance image-list | grep -q " $IMAGE_NAME " && return 0
64     glance image-create --name "$IMAGE_NAME" \
65                         --visibility public \
66                         --disk-format "$IMAGE_FORMAT" \
67                         --container-format bare \
68                         --file "$IMAGE_FILE"
69 }
70
71 boot_vm() {
72     nova list | grep -q " $VM_NAME " && return 0
73     nova boot --flavor "$VM_FLAVOR" \
74               --image "$IMAGE_NAME" \
75               "$VM_NAME"
76     sleep 1
77 }
78
79 create_alarm() {
80     ceilometer alarm-list | grep -q " $ALARM_NAME " && return 0
81     vm_id=$(nova list | grep " $VM_NAME " | awk '{print $2}')
82     ceilometer alarm-event-create --name "$ALARM_NAME" \
83         --alarm-action "http://localhost:$CONSUMER_PORT/failure" \
84         --description "VM failure" \
85         --enabled True \
86         --repeat-actions False \
87         --severity "moderate" \
88         --event-type compute.instance.update \
89         -q "traits.state=string::error; traits.instance_id=string::$vm_id"
90 }
91
92 start_monitor() {
93     pgrep -f "python monitor.py" && return 0
94     sudo python monitor.py "$COMPUTE_HOST" "$COMPUTE_IP" \
95         "http://127.0.0.1:$INSPECTOR_PORT/events" > monitor.log 2>&1 &
96 }
97
98 stop_monitor() {
99     pgrep -f "python monitor.py" || return 0
100     sudo kill $(pgrep -f "python monitor.py")
101     cat monitor.log
102 }
103
104 start_inspector() {
105     pgrep -f "python inspector.py" && return 0
106     python inspector.py "$INSPECTOR_PORT" > inspector.log 2>&1 &
107 }
108
109 stop_inspector() {
110     pgrep -f "python inspector.py" || return 0
111     kill $(pgrep -f "python inspector.py")
112     cat inspector.log
113 }
114
115 start_consumer() {
116     pgrep -f "python consumer.py" && return 0
117     python consumer.py "$CONSUMER_PORT" > consumer.log 2>&1 &
118 }
119
120 stop_consumer() {
121     pgrep -f "python consumer.py" || return 0
122     kill $(pgrep -f "python consumer.py")
123     cat consumer.log
124 }
125
126 wait_for_vm_launch() {
127     echo "waiting for vm launch..."
128     while true
129     do
130         state=$(nova list | grep " $VM_NAME " | awk '{print $6}')
131         [[ "$state" == "ACTIVE" ]] && return 0
132         sleep 1
133     done
134 }
135
136 inject_failure() {
137     echo "disabling network of comupte host [$COMPUTE_HOST] for 3 mins..."
138     cat > disable_network.sh << 'END_TXT'
139 #!/bin/bash -x
140 dev=$(/usr/sbin/ip route | awk '/^default/{print $5}')
141 sleep 1
142 echo sudo ip link set $dev down
143 sleep 120
144 echo sudo ip link set $dev up
145 sleep 1
146 END_TXT
147     chmod +x disable_network.sh
148     scp $ssh_opts_cpu disable_network.sh "$COMPUTE_IP:"
149     ssh $ssh_opts_cpu "$COMPUTE_IP:" 'nohup ./disable_network.sh > disable_network.log 2>&1 &'
150 }
151
152 calculate_notification_time() {
153     detect=$(grep "doctor monitor detected at" monitor.log | awk '{print $5}')
154     notified=$(grep "doctor consumer notified at" consumer.log | awk '{print $5}')
155     duration=$(echo "$notified $detect" | awk '{print $1 - $2 }')
156     echo "$notified $detect" | \
157         awk '{d = $1 - $2; if (d < 1 ) print d " OK"; else print d " NG"}'
158 }
159
160 cleanup() {
161     set +e
162     echo "cleanup..."
163     stop_monitor
164     stop_inspector
165     stop_consumer
166     ssh $ssh_opts_cpu $COMPUTE_IP \
167         "[ -e disable_network.log ] && cat disable_network.log"
168
169     nova service-force-down --unset "$COMPUTE_HOST" nova-compute
170     sleep 1
171     nova delete "$VM_NAME"
172     sleep 1
173     alarm_id=$(ceilometer alarm-list | grep " $ALARM_NAME " | awk '{print $2}')
174     sleep 1
175     [ -n "$alarm_id" ] && ceilometer alarm-delete "$alarm_id"
176     sleep 1
177     image_id=$(glance image-list | grep " $IMAGE_NAME " | awk '{print $2}')
178     sleep 1
179     [ -n "$image_id" ] && glance image-delete "$image_id"
180     #TODO: add host status check via nova admin api
181     echo "waiting disabled compute host back to be enabled..."
182     sleep 180
183 }
184
185
186 echo "Note: doctor/tests/run.sh has been executed."
187
188 prepare_compute_ssh
189
190 trap cleanup ERR
191
192 echo "preparing VM image..."
193 download_image
194 register_image
195
196 echo "starting doctor sample components..."
197 start_monitor
198 start_inspector
199 start_consumer
200
201 echo "creating VM and alarm..."
202 boot_vm
203 create_alarm
204 wait_for_vm_launch
205
206 sleep 60
207 echo "injecting host failure..."
208 inject_failure
209 sleep 10
210
211 calculate_notification_time
212
213 cleanup
214
215 echo "done"