test: fix DNS issue in test scenario
[doctor.git] / tests / run.sh
1 #!/bin/bash -ex
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 IMAGE_URL=https://launchpad.net/cirros/trunk/0.3.0/+download/cirros-0.3.0-x86_64-disk.img
12 IMAGE_NAME=cirros
13 IMAGE_FILE="${IMAGE_NAME}.img"
14 IMAGE_FORMAT=qcow2
15 VM_NAME=doctor_vm1
16 VM_FLAVOR=m1.tiny
17 ALARM_NAME=doctor_alarm1
18 INSPECTOR_PORT=12345
19 CONSUMER_PORT=12346
20
21 INSTALLER_TYPE=${INSTALLER_TYPE:-apex}
22 INSTALLER_IP=${INSTALLER_IP:-none}
23 COMPUTE_HOST=${COMPUTE_HOST:-overcloud-novacompute-0}
24 COMPUTE_IP=${COMPUTE_IP:-none}
25 ssh_opts="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
26
27 if [[ "$INSTALLER_TYPE" != "apex" ]] ; then
28     echo "ERROR: INSTALLER_TYPE=$INSTALLER_TYPE is not supported."
29     exit 1
30 fi
31
32 if [[ "$INSTALLER_IP" == "none" ]] ; then
33     instack_mac=$(sudo virsh domiflist instack | awk '/default/{print $5}')
34     INSTALLER_IP=$(/usr/sbin/arp -e | grep ${instack_mac} | awk '{print $1}')
35 fi
36
37 if [[ "$COMPUTE_IP" == "none" ]] ; then
38     COMPUTE_IP=$(sudo ssh $ssh_opts $INSTALLER_IP \
39                  "source stackrc; \
40                   nova show $COMPUTE_HOST \
41                   | awk '/ ctlplane network /{print \$5}'")
42 fi
43
44 prepare_compute_ssh() {
45     ping -c 1 "$COMPUTE_IP"
46
47     # get ssh key from installer node
48     sudo scp $ssh_opts /home/stack/.ssh/id_rsa instack_key
49     if [ ! -r instack_key ]; then
50         sudo chown $(whoami):$(whoami) instack_key
51     fi
52     chmod 400 instack_key
53     ssh_opts_cpu="$ssh_opts -i instack_key -l heat-admin"
54 }
55
56 download_image() {
57     [ -e "$IMAGE_FILE" ] && return 0
58     wget "$IMAGE_URL" -o "$IMAGE_FILE"
59 }
60
61 register_image() {
62     glance image-list | grep -q " $IMAGE_NAME " && return 0
63     glance image-create --name "$IMAGE_NAME" \
64                         --visibility public \
65                         --disk-format "$IMAGE_FORMAT" \
66                         --container-format bare \
67                         --file "$IMAGE_FILE"
68 }
69
70 boot_vm() {
71     nova list | grep -q " $VM_NAME " && return 0
72     nova boot --flavor "$VM_FLAVOR" \
73               --image "$IMAGE_NAME" \
74               "$VM_NAME"
75     sleep 1
76 }
77
78 create_alarm() {
79     ceilometer alarm-list | grep -q " $ALARM_NAME " && return 0
80     vm_id=$(nova list | grep " $VM_NAME " | awk '{print $2}')
81     ceilometer alarm-event-create --name "$ALARM_NAME" \
82         --alarm-action "http://localhost:$CONSUMER_PORT/failure" \
83         --description "VM failure" \
84         --enabled True \
85         --repeat-actions False \
86         --severity "moderate" \
87         --event-type compute.instance.update \
88         -q "traits.state=string::error; traits.instance_id=string::$vm_id"
89 }
90
91 start_monitor() {
92     pgrep -f "python monitor.py" && return 0
93     sudo python monitor.py "$COMPUTE_HOST" "$COMPUTE_IP" \
94         "http://127.0.0.1:$INSPECTOR_PORT/events" > monitor.log 2>&1 &
95 }
96
97 stop_monitor() {
98     pgrep -f "python monitor.py" || return 0
99     sudo kill $(pgrep -f "python monitor.py")
100     cat monitor.log
101 }
102
103 start_inspector() {
104     pgrep -f "python inspector.py" && return 0
105     python inspector.py "$INSPECTOR_PORT" > inspector.log 2>&1 &
106 }
107
108 stop_inspector() {
109     pgrep -f "python inspector.py" || return 0
110     kill $(pgrep -f "python inspector.py")
111     cat inspector.log
112 }
113
114 start_consumer() {
115     pgrep -f "python consumer.py" && return 0
116     python consumer.py "$CONSUMER_PORT" > consumer.log 2>&1 &
117 }
118
119 stop_consumer() {
120     pgrep -f "python consumer.py" || return 0
121     kill $(pgrep -f "python consumer.py")
122     cat consumer.log
123 }
124
125 wait_for_vm_launch() {
126     echo "waiting for vm launch..."
127     while true
128     do
129         state=$(nova list | grep " $VM_NAME " | awk '{print $6}')
130         [[ "$state" == "ACTIVE" ]] && return 0
131         sleep 1
132     done
133 }
134
135 inject_failure() {
136     echo "disabling network of comupte host [$COMPUTE_HOST] for 3 mins..."
137     cat > disable_network.sh << 'END_TXT'
138 #!/bin/bash -x
139 dev=$(/usr/sbin/ip route | awk '/^default/{print $5}')
140 sleep 1
141 echo sudo ip link set $dev down
142 sleep 120
143 echo sudo ip link set $dev up
144 sleep 1
145 END_TXT
146     chmod +x disable_network.sh
147     scp $ssh_opts_cpu disable_network.sh "$COMPUTE_IP:"
148     ssh $ssh_opts_cpu "$COMPUTE_IP:" 'nohup ./disable_network.sh > disable_network.log 2>&1 &'
149 }
150
151 calculate_notification_time() {
152     detect=$(grep "doctor monitor detected at" monitor.log | awk '{print $5}')
153     notified=$(grep "doctor consumer notified at" consumer.log | awk '{print $5}')
154     duration=$(echo "$notified $detect" | awk '{print $1 - $2 }')
155     echo "$notified $detect" | \
156         awk '{d = $1 - $2; if (d < 1 ) print d " OK"; else print d " NG"}'
157 }
158
159 cleanup() {
160     set +e
161     echo "cleanup..."
162     stop_monitor
163     stop_inspector
164     stop_consumer
165     ssh $ssh_opts_cpu $COMPUTE_IP \
166         "[ -e disable_network.log ] && cat disable_network.log"
167
168     nova service-force-down --unset "$COMPUTE_HOST" nova-compute
169     sleep 1
170     nova delete "$VM_NAME"
171     sleep 1
172     alarm_id=$(ceilometer alarm-list | grep " $ALARM_NAME " | awk '{print $2}')
173     sleep 1
174     [ -n "$alarm_id" ] && ceilometer alarm-delete "$alarm_id"
175     sleep 1
176     image_id=$(glance image-list | grep " $IMAGE_NAME " | awk '{print $2}')
177     sleep 1
178     [ -n "$image_id" ] && glance image-delete "$image_id"
179     #TODO: add host status check via nova admin api
180     echo "waiting disabled compute host back to be enabled..."
181     sleep 180
182 }
183
184
185 echo "Note: doctor/tests/run.sh has been executed."
186
187 prepare_compute_ssh
188
189 trap cleanup ERR
190
191 echo "preparing VM image..."
192 download_image
193 register_image
194
195 echo "starting doctor sample components..."
196 start_monitor
197 start_inspector
198 start_consumer
199
200 echo "creating VM and alarm..."
201 boot_vm
202 create_alarm
203 wait_for_vm_launch
204
205 sleep 60
206 echo "injecting host failure..."
207 inject_failure
208 sleep 10
209
210 calculate_notification_time
211
212 cleanup
213
214 echo "done"