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