Merge "tests: fix inject_failure() for deployment built by apex" into stable/brahmaputra
[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 # NOTE: You have to be changed these paramas depends on your machine,
22 #       installer and configs.
23 COMPUTE_HOST='192.0.2.8'
24 SSH_TO_COMPUTE_HOST="ssh heat-admin@$COMPUTE_HOST"
25
26
27 download_image() {
28     [ -e "$IMAGE_FILE" ] && return 0
29     wget "$IMAGE_URL" -o "$IMAGE_FILE"
30 }
31
32 register_image() {
33     glance image-list | grep -q " $IMAGE_NAME " && return 0
34     glance image-create --name "$IMAGE_NAME" \
35                         --visibility public \
36                         --disk-format "$IMAGE_FORMAT" \
37                         --container-format bare \
38                         --file "$IMAGE_FILE"
39 }
40
41 boot_vm() {
42     nova list | grep -q " $VM_NAME " && return 0
43     nova boot --flavor "$VM_FLAVOR" \
44               --image "$IMAGE_NAME" \
45               "$VM_NAME"
46     sleep 1
47 }
48
49 create_alarm() {
50     ceilometer alarm-list | grep -q " $ALARM_NAME " && return 0
51     vm_id=$(nova list | grep " $VM_NAME " | awk '{print $2}')
52     ceilometer alarm-event-create --name "$ALARM_NAME" \
53         --alarm-action "http://localhost:$CONSUMER_PORT/failure" \
54         --description "VM failure" \
55         --enabled True \
56         --repeat-actions False \
57         --severity "moderate" \
58         --event-type compute.instance.update \
59         -q "traits.state=string::error; traits.instance_id=string::$vm_id"
60 }
61
62 start_monitor() {
63     pgrep -f "python monitor.py" && return 0
64     sudo python monitor.py "$COMPUTE_HOST" "http://127.0.0.1:$INSPECTOR_PORT/events" > monitor.log 2>&1 &
65     MONITOR_PID=$!
66 }
67
68 stop_monitor() {
69     pgrep -f "python monitor.py" || return 0
70     sudo kill $(pgrep -f "python monitor.py")
71     cat monitor.log
72 }
73
74 start_inspector() {
75     pgrep -f "python inspector.py" && return 0
76     python inspector.py "$INSPECTOR_PORT" > inspector.log 2>&1 &
77 }
78
79 stop_inspector() {
80     pgrep -f "python inspector.py" || return 0
81     kill $(pgrep -f "python inspector.py")
82     cat inspector.log
83 }
84
85 start_consumer() {
86     pgrep -f "python consumer.py" && return 0
87     python consumer.py "$CONSUMER_PORT" > consumer.log 2>&1 &
88 }
89
90 stop_consumer() {
91     pgrep -f "python consumer.py" || return 0
92     kill $(pgrep -f "python consumer.py")
93     cat consumer.log
94 }
95
96 wait_for_vm_launch() {
97     echo "waiting for vm launch..."
98     while true
99     do
100         state=$(nova list | grep " $VM_NAME " | awk '{print $6}')
101         [[ "$state" == "ACTIVE" ]] && return 0
102         sleep 1
103     done
104 }
105
106 inject_failure() {
107     echo "disabling network of comupte host [$COMPUTE_HOST] for 3 mins..."
108     $SSH_TO_COMPUTE_HOST "
109 cat > disable_network.sh << 'END_TXT'
110 #!/bin/bash
111 dev=\$(/usr/sbin/ip route | awk '/^default/{print \$5}')
112 sleep 1
113 echo sudo ip link set \$dev down
114 sleep 180
115 echo sudo ip link set \$dev up
116 sleep 1
117 END_TXT
118 chmod +x disable_network.sh
119 nohup ./disable_network.sh > c 2>&1 &"
120 }
121
122 calculate_notification_time() {
123     detect=$(grep "doctor monitor detected at" monitor.log | awk '{print $5}')
124     notified=$(grep "doctor consumer notified at" consumer.log | awk '{print $5}')
125     duration=$(echo "$notified $detect" | awk '{print $1 - $2 }')
126     echo "$notified $detect" | \
127         awk '{d = $1 - $2; if (d < 1 ) print d " OK"; else print d " NG"}'
128 }
129
130 # TODO(r-mibu): Make sure env params are set properly for OpenStack clients
131 # TODO(r-mibu): Make sure POD for doctor test is available in Pharos
132
133 echo "Note: doctor/tests/run.sh has been executed, "
134 echo "      but skipping this test due to lack of available test env/deployment."
135 exit 0
136
137 download_image
138 register_image
139
140 start_monitor
141 start_inspector
142 start_consumer
143
144 boot_vm
145 create_alarm
146 wait_for_vm_launch
147
148 sleep 60
149 inject_failure
150 sleep 10
151
152 calculate_notification_time
153
154 echo "done"