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