tests: collect infra IPs
[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:-none}
24 ssh_opts="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
25
26 if [[ "$INSTALLER_TYPE" != "apex" ]] ; then
27     echo "ERROR: INSTALLER_TYPE=$INSTALLER_TYPE is not supported."
28     exit 1
29 fi
30
31 if [[ "$INSTALLER_IP" == "none" ]] ; then
32     instack_mac=$(sudo virsh domiflist instack | awk '/default/{print $5}')
33     INSTALLER_IP=$(/usr/sbin/arp -e | grep ${instack_mac} | awk '{print $1}')
34 fi
35
36 if [[ "$COMPUTE_HOST" == "none" ]] ; then
37     COMPUTE_HOST=$(sudo ssh $ssh_opts $INSTALLER_IP \
38                    "source stackrc; \
39                     nova show overcloud-novacompute-0 \
40                     | awk '/ ctlplane network /{print \$5}'")
41 fi
42
43 download_image() {
44     [ -e "$IMAGE_FILE" ] && return 0
45     wget "$IMAGE_URL" -o "$IMAGE_FILE"
46 }
47
48 register_image() {
49     glance image-list | grep -q " $IMAGE_NAME " && return 0
50     glance image-create --name "$IMAGE_NAME" \
51                         --visibility public \
52                         --disk-format "$IMAGE_FORMAT" \
53                         --container-format bare \
54                         --file "$IMAGE_FILE"
55 }
56
57 boot_vm() {
58     nova list | grep -q " $VM_NAME " && return 0
59     nova boot --flavor "$VM_FLAVOR" \
60               --image "$IMAGE_NAME" \
61               "$VM_NAME"
62     sleep 1
63 }
64
65 create_alarm() {
66     ceilometer alarm-list | grep -q " $ALARM_NAME " && return 0
67     vm_id=$(nova list | grep " $VM_NAME " | awk '{print $2}')
68     ceilometer alarm-event-create --name "$ALARM_NAME" \
69         --alarm-action "http://localhost:$CONSUMER_PORT/failure" \
70         --description "VM failure" \
71         --enabled True \
72         --repeat-actions False \
73         --severity "moderate" \
74         --event-type compute.instance.update \
75         -q "traits.state=string::error; traits.instance_id=string::$vm_id"
76 }
77
78 start_monitor() {
79     pgrep -f "python monitor.py" && return 0
80     sudo python monitor.py "$COMPUTE_HOST" "http://127.0.0.1:$INSPECTOR_PORT/events" > monitor.log 2>&1 &
81     MONITOR_PID=$!
82 }
83
84 stop_monitor() {
85     pgrep -f "python monitor.py" || return 0
86     sudo kill $(pgrep -f "python monitor.py")
87     cat monitor.log
88 }
89
90 start_inspector() {
91     pgrep -f "python inspector.py" && return 0
92     python inspector.py "$INSPECTOR_PORT" > inspector.log 2>&1 &
93 }
94
95 stop_inspector() {
96     pgrep -f "python inspector.py" || return 0
97     kill $(pgrep -f "python inspector.py")
98     cat inspector.log
99 }
100
101 start_consumer() {
102     pgrep -f "python consumer.py" && return 0
103     python consumer.py "$CONSUMER_PORT" > consumer.log 2>&1 &
104 }
105
106 stop_consumer() {
107     pgrep -f "python consumer.py" || return 0
108     kill $(pgrep -f "python consumer.py")
109     cat consumer.log
110 }
111
112 wait_for_vm_launch() {
113     echo "waiting for vm launch..."
114     while true
115     do
116         state=$(nova list | grep " $VM_NAME " | awk '{print $6}')
117         [[ "$state" == "ACTIVE" ]] && return 0
118         sleep 1
119     done
120 }
121
122 inject_failure() {
123     echo "disabling network of comupte host [$COMPUTE_HOST] for 3 mins..."
124     cat > disable_network.sh << 'END_TXT'
125 #!/bin/bash -x
126 dev=$(/usr/sbin/ip route | awk '/^default/{print $5}')
127 sleep 1
128 echo sudo ip link set $dev down
129 sleep 180
130 echo sudo ip link set $dev up
131 sleep 1
132 END_TXT
133     chmod +x disable_network.sh
134     sudo scp $ssh_opts disable_network.sh $INSTALLER_IP:
135     ssh_opts_cpu="$ssh_opts -i /home/stack/.ssh/id_rsa"
136     sudo ssh $ssh_opts $INSTALLER_IP \
137         "scp $ssh_opts_cpu disable_network.sh heat-admin@$COMPUTE_HOST: && \
138          ssh $ssh_opts_cpu 'nohup ./disable_network.sh > c 2>&1 &'"
139 }
140
141 calculate_notification_time() {
142     detect=$(grep "doctor monitor detected at" monitor.log | awk '{print $5}')
143     notified=$(grep "doctor consumer notified at" consumer.log | awk '{print $5}')
144     duration=$(echo "$notified $detect" | awk '{print $1 - $2 }')
145     echo "$notified $detect" | \
146         awk '{d = $1 - $2; if (d < 1 ) print d " OK"; else print d " NG"}'
147 }
148
149 echo "Note: doctor/tests/run.sh has been executed."
150 exit 0
151
152 download_image
153 register_image
154
155 start_monitor
156 start_inspector
157 start_consumer
158
159 boot_vm
160 create_alarm
161 wait_for_vm_launch
162
163 sleep 60
164 inject_failure
165 sleep 10
166
167 calculate_notification_time
168
169 echo "done"