tests: fix inject_failure() for deployment built by apex
[doctor.git] / tests / run.sh
1 #!/bin/bash -ex
2 #
3 # Copyright 2016 NEC Corporation.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License"); you may
6 # not use this file except in compliance with the License. You may obtain
7 # a copy of the License at
8 #
9 #      http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 # License for the specific language governing permissions and limitations
15 # under the License.
16
17 IMAGE_URL=https://launchpad.net/cirros/trunk/0.3.0/+download/cirros-0.3.0-x86_64-disk.img
18 IMAGE_NAME=cirros
19 IMAGE_FILE="${IMAGE_NAME}.img"
20 IMAGE_FORMAT=qcow2
21 VM_NAME=doctor_vm1
22 VM_FLAVOR=m1.tiny
23 ALARM_NAME=doctor_alarm1
24 INSPECTOR_PORT=12345
25 CONSUMER_PORT=12346
26
27 # NOTE: You have to be changed these paramas depends on your machine,
28 #       installer and configs.
29 COMPUTE_HOST='192.0.2.8'
30 SSH_TO_COMPUTE_HOST="ssh heat-admin@$COMPUTE_HOST"
31
32
33 download_image() {
34     [ -e "$IMAGE_FILE" ] && return 0
35     wget "$IMAGE_URL" -o "$IMAGE_FILE"
36 }
37
38 register_image() {
39     glance image-list | grep -q " $IMAGE_NAME " && return 0
40     glance image-create --name "$IMAGE_NAME" \
41                         --visibility public \
42                         --disk-format "$IMAGE_FORMAT" \
43                         --container-format bare \
44                         --file "$IMAGE_FILE"
45 }
46
47 boot_vm() {
48     nova list | grep -q " $VM_NAME " && return 0
49     nova boot --flavor "$VM_FLAVOR" \
50               --image "$IMAGE_NAME" \
51               "$VM_NAME"
52     sleep 1
53 }
54
55 create_alarm() {
56     ceilometer alarm-list | grep -q " $ALARM_NAME " && return 0
57     vm_id=$(nova list | grep " $VM_NAME " | awk '{print $2}')
58     ceilometer alarm-event-create --name "$ALARM_NAME" \
59         --alarm-action "http://localhost:$CONSUMER_PORT/failure" \
60         --description "VM failure" \
61         --enabled True \
62         --repeat-actions False \
63         --severity "moderate" \
64         --event-type compute.instance.update \
65         -q "traits.state=string::error; traits.instance_id=string::$vm_id"
66 }
67
68 start_monitor() {
69     pgrep -f "python monitor.py" && return 0
70     sudo python monitor.py "$COMPUTE_HOST" "http://127.0.0.1:$INSPECTOR_PORT/events" > monitor.log 2>&1 &
71     MONITOR_PID=$!
72 }
73
74 stop_monitor() {
75     pgrep -f "python monitor.py" || return 0
76     sudo kill $(pgrep -f "python monitor.py")
77     cat monitor.log
78 }
79
80 start_inspector() {
81     pgrep -f "python inspector.py" && return 0
82     python inspector.py "$INSPECTOR_PORT" > inspector.log 2>&1 &
83 }
84
85 stop_inspector() {
86     pgrep -f "python inspector.py" || return 0
87     kill $(pgrep -f "python inspector.py")
88     cat inspector.log
89 }
90
91 start_consumer() {
92     pgrep -f "python consumer.py" && return 0
93     python consumer.py "$CONSUMER_PORT" > consumer.log 2>&1 &
94 }
95
96 stop_consumer() {
97     pgrep -f "python consumer.py" || return 0
98     kill $(pgrep -f "python consumer.py")
99     cat consumer.log
100 }
101
102 wait_for_vm_launch() {
103     echo "waiting for vm launch..."
104     while true
105     do
106         state=$(nova list | grep " $VM_NAME " | awk '{print $6}')
107         [[ "$state" == "ACTIVE" ]] && return 0
108         sleep 1
109     done
110 }
111
112 inject_failure() {
113     echo "disabling network of comupte host [$COMPUTE_HOST] for 3 mins..."
114     $SSH_TO_COMPUTE_HOST "
115 cat > disable_network.sh << 'END_TXT'
116 #!/bin/bash
117 dev=\$(/usr/sbin/ip route | awk '/^default/{print \$5}')
118 sleep 1
119 echo sudo ip link set \$dev down
120 sleep 180
121 echo sudo ip link set \$dev up
122 sleep 1
123 END_TXT
124 chmod +x disable_network.sh
125 nohup ./disable_network.sh > c 2>&1 &"
126 }
127
128 calculate_notification_time() {
129     detect=$(grep "doctor monitor detected at" monitor.log | awk '{print $5}')
130     notified=$(grep "doctor consumer notified at" consumer.log | awk '{print $5}')
131     duration=$(echo "$notified $detect" | awk '{print $1 - $2 }')
132     echo "$notified $detect" | \
133         awk '{d = $1 - $2; if (d < 1 ) print d " OK"; else print d " NG"}'
134 }
135
136 # TODO(r-mibu): Make sure env params are set properly for OpenStack clients
137 # TODO(r-mibu): Make sure POD for doctor test is available in Pharos
138
139 echo "Note: doctor/tests/run.sh has been executed, "
140 echo "      but skipping this test due to lack of available test env/deployment."
141 exit 0
142
143 download_image
144 register_image
145
146 start_monitor
147 start_inspector
148 start_consumer
149
150 boot_vm
151 create_alarm
152 wait_for_vm_launch
153
154 sleep 60
155 inject_failure
156 sleep 10
157
158 calculate_notification_time
159
160 echo "done"