56bacca700411883a0a1af41bd50320392a996b0
[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}" == "true" ]] && 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 TEST_USER=demo
23 TEST_PW=demo
24 TEST_PROJECT=demo
25 TEST_ROLE=_member_
26
27 SUPPORTED_INSTALLER_TYPES="apex local"
28 INSTALLER_TYPE=${INSTALLER_TYPE:-apex}
29 INSTALLER_IP=${INSTALLER_IP:-none}
30 COMPUTE_HOST=${COMPUTE_HOST:-overcloud-novacompute-0}
31 COMPUTE_IP=${COMPUTE_IP:-none}
32 COMPUTE_USER=${COMPUTE_USER:-heat-admin}
33 ssh_opts="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
34
35 if [[ ! "$SUPPORTED_INSTALLER_TYPES" =~ "$INSTALLER_TYPE" ]] ; then
36     echo "ERROR: INSTALLER_TYPE=$INSTALLER_TYPE is not supported."
37     exit 1
38 fi
39
40 prepare_compute_ssh() {
41     ssh_opts_cpu="$ssh_opts"
42
43     if [[ "$INSTALLER_TYPE" == "apex" ]] ; then
44         if [[ "$INSTALLER_IP" == "none" ]] ; then
45             instack_mac=$(sudo virsh domiflist instack | awk '/default/{print $5}')
46             INSTALLER_IP=$(/usr/sbin/arp -e | grep ${instack_mac} | awk '{print $1}')
47         fi
48
49         if [[ "$COMPUTE_IP" == "none" ]] ; then
50             COMPUTE_IP=$(sudo ssh $ssh_opts $INSTALLER_IP \
51                          "source stackrc; \
52                           nova show $COMPUTE_HOST \
53                           | awk '/ ctlplane network /{print \$5}'")
54         fi
55
56         # get ssh key from installer node
57         sudo scp $ssh_opts root@"$INSTALLER_IP":/home/stack/.ssh/id_rsa instack_key
58         sudo chown $(whoami):$(whoami) instack_key
59         chmod 400 instack_key
60         ssh_opts_cpu+=" -i instack_key"
61     elif [[ "$INSTALLER_TYPE" == "local" ]] ; then
62         if [[ "$COMPUTE_IP" == "none" ]] ; then
63             COMPUTE_IP=$(getent hosts "$COMPUTE_HOST" | awk '{ print $1 }')
64             if [[ -z "$COMPUTE_IP" ]]; then
65                 echo "ERROR: Could not resolve $COMPUTE_HOST. Either manually set COMPUTE_IP or enable DNS resolution."
66                 exit 1
67             fi
68         fi
69
70         echo "INSTALLER_TYPE set to 'local'. Assuming SSH keys already exchanged with $COMPUTE_HOST"
71     fi
72
73     # verify connectivity to target compute host
74     ping -c 1 "$COMPUTE_IP"
75 }
76
77 download_image() {
78     [ -e "$IMAGE_FILE" ] && return 0
79     wget "$IMAGE_URL" -o "$IMAGE_FILE"
80 }
81
82 register_image() {
83     glance image-list | grep -q " $IMAGE_NAME " && return 0
84     glance image-create --name "$IMAGE_NAME" \
85                         --visibility public \
86                         --disk-format "$IMAGE_FORMAT" \
87                         --container-format bare \
88                         --file "$IMAGE_FILE"
89 }
90
91 create_test_user() {
92     openstack user list | grep -q "$TEST_USER" || {
93         openstack user create "$TEST_USER" --password "$TEST_PW"
94     }
95     openstack project list | grep -q "$TEST_PROJECT" || {
96         openstack project create "$TEST_PROJECT"
97     }
98     openstack user role list "$TEST_USER" --project "$TEST_PROJECT" \
99     | grep -q "$TEST_ROLE" || {
100         openstack role add "$TEST_ROLE" --user "$TEST_USER" \
101                            --project "$TEST_PROJECT"
102     }
103 }
104
105 boot_vm() {
106     nova list | grep -q " $VM_NAME " && return 0
107     (
108         # test VM done with test user, so can test non-admin
109         export OS_USERNAME="$TEST_USER"
110         export OS_PASSWORD="$TEST_PW"
111         export OS_TENANT_NAME="$TEST_PROJECT"
112         nova boot --flavor "$VM_FLAVOR" \
113                   --image "$IMAGE_NAME" \
114                   "$VM_NAME"
115         sleep 1
116     )
117
118 }
119
120 create_alarm() {
121     ceilometer alarm-list | grep -q " $ALARM_NAME " && return 0
122     vm_id=$(nova list | grep " $VM_NAME " | awk '{print $2}')
123     ceilometer alarm-event-create --name "$ALARM_NAME" \
124         --alarm-action "http://localhost:$CONSUMER_PORT/failure" \
125         --description "VM failure" \
126         --enabled True \
127         --repeat-actions False \
128         --severity "moderate" \
129         --event-type compute.instance.update \
130         -q "traits.state=string::error; traits.instance_id=string::$vm_id"
131 }
132
133 start_monitor() {
134     pgrep -f "python monitor.py" && return 0
135     sudo python monitor.py "$COMPUTE_HOST" "$COMPUTE_IP" \
136         "http://127.0.0.1:$INSPECTOR_PORT/events" > monitor.log 2>&1 &
137 }
138
139 stop_monitor() {
140     pgrep -f "python monitor.py" || return 0
141     sudo kill $(pgrep -f "python monitor.py")
142     cat monitor.log
143 }
144
145 start_inspector() {
146     pgrep -f "python inspector.py" && return 0
147     python inspector.py "$INSPECTOR_PORT" > inspector.log 2>&1 &
148 }
149
150 stop_inspector() {
151     pgrep -f "python inspector.py" || return 0
152     kill $(pgrep -f "python inspector.py")
153     cat inspector.log
154 }
155
156 start_consumer() {
157     pgrep -f "python consumer.py" && return 0
158     python consumer.py "$CONSUMER_PORT" > consumer.log 2>&1 &
159 }
160
161 stop_consumer() {
162     pgrep -f "python consumer.py" || return 0
163     kill $(pgrep -f "python consumer.py")
164     cat consumer.log
165 }
166
167 wait_for_vm_launch() {
168     echo "waiting for vm launch..."
169     while true
170     do
171         state=$(nova list | grep " $VM_NAME " | awk '{print $6}')
172         [[ "$state" == "ACTIVE" ]] && return 0
173         sleep 1
174     done
175 }
176
177 inject_failure() {
178     echo "disabling network of compute host [$COMPUTE_HOST] for 3 mins..."
179     cat > disable_network.sh << 'END_TXT'
180 #!/bin/bash -x
181 dev=$(sudo ip route | awk '/^default/{print $5}')
182 sleep 1
183 sudo ip link set $dev down
184 sleep 180
185 sudo ip link set $dev up
186 sleep 1
187 END_TXT
188     chmod +x disable_network.sh
189     scp $ssh_opts_cpu disable_network.sh "$COMPUTE_USER@$COMPUTE_IP:"
190     ssh $ssh_opts_cpu "$COMPUTE_USER@$COMPUTE_IP" 'nohup ./disable_network.sh > disable_network.log 2>&1 &'
191 }
192
193 calculate_notification_time() {
194     detected=$(grep "doctor monitor detected at" monitor.log | awk '{print $5}')
195     notified=$(grep "doctor consumer notified at" consumer.log | awk '{print $5}')
196     echo "$notified $detected" | \
197         awk '{d = $1 - $2; if (d < 1 && d > 0) print d " OK"; else print d " NG"}'
198 }
199
200 check_host_status_down() {
201     (
202         # Switching to test user
203         export OS_USERNAME="$TEST_USER"
204         export OS_PASSWORD="$TEST_PW"
205         export OS_TENANT_NAME="$TEST_PROJECT"
206
207         host_status_line=$(nova show $VM_NAME | grep "host_status")
208         [[ $? -ne 0 ]] && {
209             echo "ERROR: host_status not configured for owner in Nova policy.json"
210         }
211
212         host_status=$(echo $host_status_line | awk '{print $4}')
213         [[ "$host_status" == "DOWN" ]] && {
214             echo "$VM_NAME showing host_status: $host_status"
215         }
216         echo "ERROR: host_status not reported by: nova show $VM_NAME"
217     )
218 }
219
220 cleanup() {
221     set +e
222     echo "cleanup..."
223     stop_monitor
224     stop_inspector
225     stop_consumer
226
227     python ./nova_force_down.py "$COMPUTE_HOST" --unset
228     sleep 1
229     nova list | grep -q " $VM_NAME " && nova delete "$VM_NAME"
230     sleep 1
231     alarm_id=$(ceilometer alarm-list | grep " $ALARM_NAME " | awk '{print $2}')
232     sleep 1
233     [ -n "$alarm_id" ] && ceilometer alarm-delete "$alarm_id"
234     sleep 1
235     image_id=$(glance image-list | grep " $IMAGE_NAME " | awk '{print $2}')
236     sleep 1
237     [ -n "$image_id" ] && glance image-delete "$image_id"
238     openstack role remove "$TEST_ROLE" --user "$TEST_USER" \
239                               --project "$TEST_PROJECT"
240     openstack project delete "$TEST_PROJECT"
241     openstack user delete "$TEST_USER"
242
243     #TODO: add host status check via nova admin api
244     echo "waiting disabled compute host back to be enabled..."
245     sleep 180
246     ssh $ssh_opts_cpu "$COMPUTE_USER@$COMPUTE_IP" \
247         "[ -e disable_network.log ] && cat disable_network.log"
248 }
249
250
251 echo "Note: doctor/tests/run.sh has been executed."
252
253 prepare_compute_ssh
254
255 trap cleanup EXIT
256
257 echo "preparing VM image..."
258 download_image
259 register_image
260
261 echo "starting doctor sample components..."
262 start_monitor
263 start_inspector
264 start_consumer
265
266 echo "creating test user..."
267 create_test_user
268
269 echo "creating VM and alarm..."
270 boot_vm
271 create_alarm
272 wait_for_vm_launch
273
274 sleep 60
275 echo "injecting host failure..."
276 inject_failure
277 sleep 10
278
279 check_host_status_down
280 calculate_notification_time
281
282 echo "done"