Refactor inspectors support code
[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 #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"
17 IMAGE_FORMAT=qcow2
18 VM_NAME=doctor_vm1
19 VM_FLAVOR=m1.tiny
20 ALARM_NAME=doctor_alarm1
21 INSPECTOR_PORT=12345
22 CONSUMER_PORT=12346
23 DOCTOR_USER=doctor
24 DOCTOR_PW=doctor
25 DOCTOR_PROJECT=doctor
26 #TODO: change back to `_member_` when JIRA DOCTOR-55 is done
27 DOCTOR_ROLE=admin
28
29 TOP_DIR=$(cd $(dirname "$0") && pwd)
30
31 ssh_opts="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
32 as_doctor_user="--os-username $DOCTOR_USER --os-password $DOCTOR_PW
33                 --os-tenant-name $DOCTOR_PROJECT"
34
35
36 get_compute_host_info() {
37     # get computer host info which VM boot in
38     COMPUTE_HOST=$(openstack $as_doctor_user server show $VM_NAME |
39                    grep "OS-EXT-SRV-ATTR:host" | awk '{ print $4 }')
40     compute_host_in_undercloud=${COMPUTE_HOST%%.*}
41     die_if_not_set $LINENO COMPUTE_HOST "Failed to get compute hostname"
42
43     if is_installer apex; then
44         COMPUTE_USER=${COMPUTE_USER:-heat-admin}
45         COMPUTE_IP=$(sudo ssh $ssh_opts $INSTALLER_IP \
46              "source stackrc; \
47              nova show $compute_host_in_undercloud \
48              | awk '/ ctlplane network /{print \$5}'")
49     elif is_installer fuel; then
50         COMPUTE_USER=${COMPUTE_USER:-root}
51         node_id=$(echo $compute_host_in_undercloud | cut -d "-" -f 2)
52         COMPUTE_IP=$(sshpass -p r00tme ssh 2>/dev/null $ssh_opts root@${INSTALLER_IP} \
53              "fuel node|awk -F '|' -v id=$node_id '{if (\$1 == id) print \$5}' |xargs")
54     elif is_installer local; then
55         COMPUTE_USER=${COMPUTE_USER:-$(whoami)}
56         COMPUTE_IP=$(getent hosts "$COMPUTE_HOST" | awk '{ print $1 }')
57     fi
58
59     die_if_not_set $LINENO COMPUTE_IP "Could not resolve $COMPUTE_HOST. Either manually set COMPUTE_IP or enable DNS resolution."
60     echo "COMPUTE_HOST=$COMPUTE_HOST"
61     echo "COMPUTE_IP=$COMPUTE_IP"
62
63     # verify connectivity to target compute host
64     ping -c 1 "$COMPUTE_IP"
65     if [[ $? -ne 0 ]] ; then
66         die $LINENO "Can not ping to computer host"
67     fi
68
69     # verify ssh to target compute host
70     ssh $ssh_opts_cpu "$COMPUTE_USER@$COMPUTE_IP" 'exit'
71     if [[ $? -ne 0 ]] ; then
72         die $LINENO "Can not ssh to computer host"
73     fi
74 }
75
76 get_consumer_ip() {
77     local get_consumer_command="ip route get $COMPUTE_IP | awk '/ src /{print \$NF}'"
78     if is_installer apex; then
79         CONSUMER_IP=$(sudo ssh $ssh_opts root@$INSTALLER_IP \
80                       "$get_consumer_command")
81     elif is_installer fuel; then
82         CONSUMER_IP=$(sudo sshpass -p r00tme ssh $ssh_opts root@${INSTALLER_IP} \
83                       "$get_consumer_command")
84     elif is_installer local; then
85         CONSUMER_IP=`$get_consumer_command`
86     fi
87     echo "CONSUMER_IP=$CONSUMER_IP"
88
89     die_if_not_set $LINENO CONSUMER_IP "Could not get CONSUMER_IP."
90 }
91
92 download_image() {
93     #if a different name was provided for the image in the enviroment there's no need to download the image
94     use_existing_image=false
95     openstack image list | grep -q " $IMAGE_NAME " && use_existing_image=true
96
97     if [[ "$use_existing_image" == false ]] ; then
98         [ -e "$IMAGE_FILE" ] && return 0
99         wget "$IMAGE_URL" -o "$IMAGE_FILE"
100     fi
101 }
102
103 register_image() {
104     openstack image list | grep -q " $IMAGE_NAME " && return 0
105     openstack image create "$IMAGE_NAME" \
106                            --public \
107                            --disk-format "$IMAGE_FORMAT" \
108                            --container-format bare \
109                            --file "$IMAGE_FILE"
110 }
111
112 create_test_user() {
113     openstack project list | grep -q " $DOCTOR_PROJECT " || {
114         openstack project create "$DOCTOR_PROJECT"
115     }
116     openstack user list | grep -q " $DOCTOR_USER " || {
117         openstack user create "$DOCTOR_USER" --password "$DOCTOR_PW" \
118                               --project "$DOCTOR_PROJECT"
119     }
120     openstack user role list "$DOCTOR_USER" --project "$DOCTOR_PROJECT" \
121     | grep -q " $DOCTOR_ROLE " || {
122         openstack role add "$DOCTOR_ROLE" --user "$DOCTOR_USER" \
123                            --project "$DOCTOR_PROJECT"
124     }
125 }
126
127 boot_vm() {
128     # test VM done with test user, so can test non-admin
129     openstack $as_doctor_user server list | grep -q " $VM_NAME " && return 0
130     openstack $as_doctor_user server create --flavor "$VM_FLAVOR" \
131                             --image "$IMAGE_NAME" \
132                             "$VM_NAME"
133     sleep 1
134 }
135
136 create_alarm() {
137     # get vm_id as test user
138     ceilometer $as_doctor_user alarm-list | grep -q " $ALARM_NAME " && return 0
139     vm_id=$(openstack $as_doctor_user server list | grep " $VM_NAME " | awk '{print $2}')
140     # TODO(r-mibu): change notification endpoint from localhost to the consumer
141     # IP address (functest container).
142     ceilometer $as_doctor_user alarm-event-create --name "$ALARM_NAME" \
143         --alarm-action "http://localhost:$CONSUMER_PORT/failure" \
144         --description "VM failure" \
145         --enabled True \
146         --repeat-actions False \
147         --severity "moderate" \
148         --event-type compute.instance.update \
149         -q "traits.state=string::error; traits.instance_id=string::$vm_id"
150 }
151
152 start_monitor() {
153     pgrep -f "python monitor.py" && return 0
154     sudo -E python monitor.py "$COMPUTE_HOST" "$COMPUTE_IP" "$INSPECTOR_TYPE" \
155         "http://127.0.0.1:$INSPECTOR_PORT/events" > monitor.log 2>&1 &
156 }
157
158 stop_monitor() {
159     pgrep -f "python monitor.py" || return 0
160     sudo kill $(pgrep -f "python monitor.py")
161 }
162
163 start_consumer() {
164     pgrep -f "python consumer.py" && return 0
165     python consumer.py "$CONSUMER_PORT" > consumer.log 2>&1 &
166
167     # NOTE(r-mibu): create tunnel to the controller nodes, so that we can
168     # avoid some network problems dpends on infra and installers.
169     # This tunnel will be terminated by stop_consumer() or after 10 mins passed.
170     if ! is_installer local; then
171         if is_installer apex; then
172             CONTROLLER_IPS=$(sudo ssh $ssh_opts $INSTALLER_IP \
173                              "source stackrc; \
174                              nova list | grep ' overcloud-controller-[0-9] ' \
175                              | sed -e 's/^.*ctlplane=//' -e 's/ *|\$//'")
176         elif is_installer fuel; then
177             CONTROLLER_IPS=$(sshpass -p r00tme ssh 2>/dev/null $ssh_opts root@${INSTALLER_IP} \
178                             "fuel node | grep controller | cut -d '|' -f 5|xargs")
179         fi
180
181         die_if_not_set $LINENO CONTROLLER_IPS "Could not get CONTROLLER_IPS."
182         for ip in $CONTROLLER_IPS
183         do
184             forward_rule="-R $CONSUMER_PORT:localhost:$CONSUMER_PORT"
185             tunnel_command="sudo ssh $ssh_opts_cpu $COMPUTE_USER@$ip $forward_rule sleep 600"
186             $tunnel_command > "ssh_tunnel.${ip}.log" 2>&1 < /dev/null &
187         done
188     fi
189 }
190
191 stop_consumer() {
192     pgrep -f "python consumer.py" || return 0
193     kill $(pgrep -f "python consumer.py")
194
195     # NOTE(r-mibu): terminate tunnels to the controller nodes
196     if ! is_installer local; then
197         for ip in $CONTROLLER_IPS
198         do
199             forward_rule="-R $CONSUMER_PORT:localhost:$CONSUMER_PORT"
200             tunnel_command="sudo ssh $ssh_opts_cpu $COMPUTE_USER@$ip $forward_rule sleep 600"
201             kill $(pgrep -f "$tunnel_command")
202         done
203     fi
204 }
205
206 wait_for_vm_launch() {
207     echo "waiting for vm launch..."
208
209     count=0
210     while [[ ${count} -lt 60 ]]
211     do
212         state=$(openstack $as_doctor_user server list | grep " $VM_NAME " | awk '{print $6}')
213         if [[ "$state" == "ACTIVE" ]]; then
214             # NOTE(cgoncalves): sleeping for a bit to stabilize
215             # See python-openstackclient/functional/tests/compute/v2/test_server.py:wait_for_status
216             sleep 5
217             return 0
218         fi
219         if [[ "$state" == "ERROR" ]]; then
220             die $LINENO "vm state is ERROR"
221         fi
222         count=$(($count+1))
223         sleep 1
224     done
225     die $LINENO "Time out while waiting for VM launch"
226 }
227
228 inject_failure() {
229     echo "disabling network of compute host [$COMPUTE_HOST] for 3 mins..."
230     cat > disable_network.sh << 'END_TXT'
231 #!/bin/bash -x
232 dev=$(sudo ip a | awk '/ @COMPUTE_IP@\//{print $7}')
233 sleep 1
234 sudo ip link set $dev down
235 sleep 180
236 sudo ip link set $dev up
237 sleep 1
238 END_TXT
239     sed -i -e "s/@COMPUTE_IP@/$COMPUTE_IP/" disable_network.sh
240     chmod +x disable_network.sh
241     scp $ssh_opts_cpu disable_network.sh "$COMPUTE_USER@$COMPUTE_IP:"
242     ssh $ssh_opts_cpu "$COMPUTE_USER@$COMPUTE_IP" 'nohup ./disable_network.sh > disable_network.log 2>&1 &'
243 }
244
245 calculate_notification_time() {
246     detected=$(grep "doctor monitor detected at" monitor.log | awk '{print $10}')
247     notified=$(grep "doctor consumer notified at" consumer.log | awk '{print $10}')
248     if ! grep -q "doctor consumer notified at" consumer.log ; then
249         die $LINENO "Consumer hasn't received fault notification."
250     fi
251     echo "$notified $detected" | \
252         awk '{
253             d = $1 - $2;
254             if (d < 1 && d > 0) { print d " OK"; exit 0 }
255             else { print d " NG"; exit 1 }
256         }'
257 }
258
259 check_host_status() {
260     expected_state=$1
261
262     host_status_line=$(openstack $as_doctor_user --os-compute-api-version 2.16 \
263                        server show $VM_NAME | grep "host_status")
264     host_status=$(echo $host_status_line | awk '{print $4}')
265     die_if_not_set $LINENO host_status "host_status not reported by: nova show $VM_NAME"
266     if [[ "$expected_state" =~ "$host_status" ]] ; then
267         echo "$VM_NAME showing host_status: $host_status"
268     else
269         die $LINENO "host_status:$host_status not equal to expected_state: $expected_state"
270     fi
271 }
272
273 cleanup() {
274     set +e
275     echo "cleanup..."
276     stop_monitor
277     stop_inspector
278     stop_consumer
279
280     echo "waiting disabled compute host back to be enabled..."
281     python ./nova_force_down.py "$COMPUTE_HOST" --unset
282     sleep 240
283     check_host_status "UP"
284     scp $ssh_opts_cpu "$COMPUTE_USER@$COMPUTE_IP:disable_network.log" .
285
286     openstack $as_doctor_user server list | grep -q " $VM_NAME " && openstack $as_doctor_user server delete "$VM_NAME"
287     sleep 1
288     alarm_id=$(ceilometer $as_doctor_user alarm-list | grep " $ALARM_NAME " | awk '{print $2}')
289     sleep 1
290     [ -n "$alarm_id" ] && ceilometer $as_doctor_user alarm-delete "$alarm_id"
291     sleep 1
292
293     image_id=$(openstack image list | grep " $IMAGE_NAME " | awk '{print $2}')
294     sleep 1
295     #if an existing image was used, there's no need to remove it here
296     if [[ "$use_existing_image" == false ]] ; then
297         [ -n "$image_id" ] && openstack image delete "$image_id"
298     fi
299     openstack role remove "$DOCTOR_ROLE" --user "$DOCTOR_USER" \
300                               --project "$DOCTOR_PROJECT"
301     openstack project delete "$DOCTOR_PROJECT"
302     openstack user delete "$DOCTOR_USER"
303
304     cleanup_installer
305     cleanup_inspector
306 }
307
308
309 echo "Note: doctor/tests/run.sh has been executed."
310
311 trap cleanup EXIT
312
313 source $TOP_DIR/functions-common
314 source $TOP_DIR/lib/installer
315 source $TOP_DIR/lib/inspector
316
317 setup_installer
318
319 echo "preparing VM image..."
320 download_image
321 register_image
322
323 echo "creating test user..."
324 create_test_user
325
326 echo "creating VM..."
327 boot_vm
328 wait_for_vm_launch
329
330 echo "get computer host info..."
331 get_compute_host_info
332
333 echo "creating alarm..."
334 #TODO: change back to use, network problems depends on infra and installers
335 #get_consumer_ip
336 create_alarm
337
338 echo "starting doctor sample components..."
339 start_inspector
340 start_monitor
341 start_consumer
342
343 sleep 60
344 echo "injecting host failure..."
345 inject_failure
346 sleep 60
347
348 check_host_status "(DOWN|UNKNOWN)"
349 calculate_notification_time
350
351 echo "done"