add project when create user
[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_options 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_options 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=$(ip route get $COMPUTE_IP | awk '/ src /{print $NF}')
118     echo "CONSUMER_IP=$CONSUMER_IP"
119
120     if [[ -z "$CONSUMER_IP" ]]; then
121         echo "ERROR: Could not get CONSUMER_IP."
122         exit 1
123     fi
124 }
125
126 download_image() {
127     [ -e "$IMAGE_FILE" ] && return 0
128     wget "$IMAGE_URL" -o "$IMAGE_FILE"
129 }
130
131 register_image() {
132     openstack image list | grep -q " $IMAGE_NAME " && return 0
133     openstack image create "$IMAGE_NAME" \
134                            --public \
135                            --disk-format "$IMAGE_FORMAT" \
136                            --container-format bare \
137                            --file "$IMAGE_FILE"
138 }
139
140 create_test_user() {
141     openstack project list | grep -q " $DOCTOR_PROJECT " || {
142         openstack project create "$DOCTOR_PROJECT"
143     }
144     openstack user list | grep -q " $DOCTOR_USER " || {
145         openstack user create "$DOCTOR_USER" --password "$DOCTOR_PW" \
146                               --project "$DOCTOR_PROJECT"
147     }
148     openstack user role list "$DOCTOR_USER" --project "$DOCTOR_PROJECT" \
149     | grep -q " $DOCTOR_ROLE " || {
150         openstack role add "$DOCTOR_ROLE" --user "$DOCTOR_USER" \
151                            --project "$DOCTOR_PROJECT"
152     }
153 }
154
155 boot_vm() {
156     # test VM done with test user, so can test non-admin
157     openstack $as_doctor_user server list | grep -q " $VM_NAME " && return 0
158     openstack $as_doctor_user server create --flavor "$VM_FLAVOR" \
159                             --image "$IMAGE_NAME" \
160                             "$VM_NAME"
161     sleep 1
162 }
163
164 create_alarm() {
165     # get vm_id as test user
166     ceilometer $as_doctor_user alarm-list | grep -q " $ALARM_NAME " && return 0
167     vm_id=$(openstack $as_doctor_user server list | grep " $VM_NAME " | awk '{print $2}')
168     ceilometer $as_doctor_user alarm-event-create --name "$ALARM_NAME" \
169         --alarm-action "http://$CONSUMER_IP:$CONSUMER_PORT/failure" \
170         --description "VM failure" \
171         --enabled True \
172         --repeat-actions False \
173         --severity "moderate" \
174         --event-type compute.instance.update \
175         -q "traits.state=string::error; traits.instance_id=string::$vm_id"
176 }
177
178 print_log() {
179     log_file=$1
180     echo "$log_file:"
181     sed -e 's/^/    /' "$log_file"
182 }
183
184 start_monitor() {
185     pgrep -f "python monitor.py" && return 0
186     sudo python monitor.py "$COMPUTE_HOST" "$COMPUTE_IP" \
187         "http://127.0.0.1:$INSPECTOR_PORT/events" > monitor.log 2>&1 &
188 }
189
190 stop_monitor() {
191     pgrep -f "python monitor.py" || return 0
192     sudo kill $(pgrep -f "python monitor.py")
193     print_log monitor.log
194 }
195
196 start_inspector() {
197     pgrep -f "python inspector.py" && return 0
198     python inspector.py "$INSPECTOR_PORT" > inspector.log 2>&1 &
199 }
200
201 stop_inspector() {
202     pgrep -f "python inspector.py" || return 0
203     kill $(pgrep -f "python inspector.py")
204     print_log inspector.log
205 }
206
207 start_consumer() {
208     pgrep -f "python consumer.py" && return 0
209     python consumer.py "$CONSUMER_PORT" > consumer.log 2>&1 &
210 }
211
212 stop_consumer() {
213     pgrep -f "python consumer.py" || return 0
214     kill $(pgrep -f "python consumer.py")
215     print_log consumer.log
216 }
217
218 wait_for_vm_launch() {
219     echo "waiting for vm launch..."
220
221     count=0
222     while [[ ${count} -lt 60 ]]
223     do
224         state=$(openstack $as_doctor_user server list | grep " $VM_NAME " | awk '{print $6}')
225         [[ "$state" == "ACTIVE" ]] && return 0
226         [[ "$state" == "ERROR" ]] && echo "vm state is ERROR" && exit 1
227         count=$(($count+1))
228         sleep 1
229     done
230     echo "ERROR: time out while waiting for vm launch"
231     exit 1
232 }
233
234 inject_failure() {
235     echo "disabling network of compute host [$COMPUTE_HOST] for 3 mins..."
236     cat > disable_network.sh << 'END_TXT'
237 #!/bin/bash -x
238 dev=$(sudo ip a | awk '/ @COMPUTE_IP@\//{print $7}')
239 sleep 1
240 sudo ip link set $dev down
241 sleep 180
242 sudo ip link set $dev up
243 sleep 1
244 END_TXT
245     sed -i -e "s/@COMPUTE_IP@/$COMPUTE_IP/" disable_network.sh
246     chmod +x disable_network.sh
247     scp $ssh_opts_cpu disable_network.sh "$COMPUTE_USER@$COMPUTE_IP:"
248     ssh $ssh_opts_cpu "$COMPUTE_USER@$COMPUTE_IP" 'nohup ./disable_network.sh > disable_network.log 2>&1 &'
249 }
250
251 calculate_notification_time() {
252     detected=$(grep "doctor monitor detected at" monitor.log | awk '{print $5}')
253     notified=$(grep "doctor consumer notified at" consumer.log | awk '{print $5}')
254     if ! grep -q "doctor consumer notified at" consumer.log ; then
255         echo "ERROR: consumer hasn't received fault notification."
256         exit 1
257     fi
258     echo "$notified $detected" | \
259         awk '{d = $1 - $2; if (d < 1 && d > 0) print d " OK"; else print d " NG"}'
260 }
261
262 check_host_status() {
263     expected_state=$1
264
265     host_status_line=$(openstack $as_doctor_user --os-compute-api-version 2.16 \
266                        server show $VM_NAME | grep "host_status")
267     host_status=$(echo $host_status_line | awk '{print $4}')
268     if [ -z "$host_status" ] ; then
269         echo "ERROR: host_status not reported by: nova show $VM_NAME"
270         exit 1
271     elif [[ "$expected_state" =~ "$host_status" ]] ; then
272         echo "$VM_NAME showing host_status: $host_status"
273     else
274         echo "ERROR: host_status:$host_status not equal to expected_state: $expected_state"
275         exit 1
276     fi
277 }
278
279 cleanup() {
280     set +e
281     echo "cleanup..."
282     stop_monitor
283     stop_inspector
284     stop_consumer
285
286     echo "waiting disabled compute host back to be enabled..."
287     python ./nova_force_down.py "$COMPUTE_HOST" --unset
288     sleep 240
289     check_host_status "UP"
290     scp $ssh_opts_cpu "$COMPUTE_USER@$COMPUTE_IP:disable_network.log" .
291     print_log disable_network.log
292
293     openstack $as_doctor_user server list | grep -q " $VM_NAME " && openstack $as_doctor_user server delete "$VM_NAME"
294     sleep 1
295     alarm_id=$(ceilometer $as_doctor_user alarm-list | grep " $ALARM_NAME " | awk '{print $2}')
296     sleep 1
297     [ -n "$alarm_id" ] && ceilometer $as_doctor_user alarm-delete "$alarm_id"
298     sleep 1
299
300     image_id=$(openstack image list | grep " $IMAGE_NAME " | awk '{print $2}')
301     sleep 1
302     [ -n "$image_id" ] && openstack image delete "$image_id"
303     openstack role remove "$DOCTOR_ROLE" --user "$DOCTOR_USER" \
304                               --project "$DOCTOR_PROJECT"
305     openstack project delete "$DOCTOR_PROJECT"
306     openstack user delete "$DOCTOR_USER"
307 }
308
309
310 echo "Note: doctor/tests/run.sh has been executed."
311
312 trap cleanup EXIT
313
314 echo "preparing VM image..."
315 download_image
316 register_image
317
318 echo "creating test user..."
319 create_test_user
320
321 echo "creating VM..."
322 boot_vm
323 wait_for_vm_launch
324 openstack $as_doctor_user server show $VM_NAME
325
326 echo "get computer host info and prepare to ssh..."
327 get_compute_host_info
328 prepare_compute_ssh
329
330 echo "creating alarm..."
331 get_consumer_ip
332 create_alarm
333
334 echo "starting doctor sample components..."
335 start_monitor
336 start_inspector
337 start_consumer
338
339 sleep 60
340 echo "injecting host failure..."
341 inject_failure
342 sleep 60
343
344 check_host_status "(DOWN|UNKNOWN)"
345 calculate_notification_time
346
347 echo "done"