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