add test scenario and sample components
[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 #branch=$(git rev-parse --abbrev-ref HEAD)
18 BRANCH=master
19
20 IMAGE_URL=https://launchpad.net/cirros/trunk/0.3.0/+download/cirros-0.3.0-x86_64-disk.img
21 IMAGE_NAME=cirros
22 IMAGE_FILE="${IMAGE_NAME}.img"
23 IMAGE_FORMAT=qcow2
24 VM_NAME=doctor_vm1
25 VM_FLAVOR=m1.tiny
26 COMPUTE_HOST='s142'
27 ALARM_NAME=doctor_alarm1
28 INSPECTOR_PORT=12345
29 CONSUMER_PORT=12346
30
31
32 download_image() {
33     [ -e "$IMAGE_FILE" ] && return 0
34     wget "$IMAGE_URL" -o "$IMAGE_FILE"
35 }
36
37 register_image() {
38     glance image-list | grep -q " $IMAGE_NAME " && return 0
39     glance image-create --name "$IMAGE_NAME" \
40                         --visibility public \
41                         --disk-format "$IMAGE_FORMAT" \
42                         --container-format bare \
43                         --file "$IMAGE_FILE"
44 }
45
46 boot_vm() {
47     nova list | grep -q " $VM_NAME " && return 0
48     nova boot --flavor "$VM_FLAVOR" \
49               --image "$IMAGE_NAME" \
50               "$VM_NAME"
51     sleep 1
52 }
53
54 create_alarm() {
55     ceilometer alarm-list | grep -q " $ALARM_NAME " && return 0
56     vm_id=$(nova list | grep " $VM_NAME " | awk '{print $2}')
57     ceilometer alarm-event-create --name "$ALARM_NAME" \
58         --alarm-action "http://localhost:$CONSUMER_PORT/failure" \
59         --description "VM failure" \
60         --enabled True \
61         --repeat-actions False \
62         --severity "moderate" \
63         --event-type compute.instance.update \
64         -q "traits.state=string::error; traits.instance_id=string::$vm_id"
65 }
66
67 start_monitor() {
68     pgrep -f "python monitor.py" && return 0
69     sudo python monitor.py "$COMPUTE_HOST" "http://127.0.0.1:$INSPECTOR_PORT/events" > monitor.log 2>&1 &
70     MONITOR_PID=$!
71 }
72
73 stop_monitor() {
74     pgrep -f "python monitor.py" || return 0
75     sudo kill $(pgrep -f "python monitor.py")
76     cat monitor.log
77 }
78
79 start_inspector() {
80     pgrep -f "python inspector.py" && return 0
81     python inspector.py "$INSPECTOR_PORT" > inspector.log 2>&1 &
82 }
83
84 stop_inspector() {
85     pgrep -f "python inspector.py" || return 0
86     kill $(pgrep -f "python inspector.py")
87     cat inspector.log
88 }
89
90 start_consumer() {
91     pgrep -f "python consumer.py" && return 0
92     python consumer.py "$CONSUMER_PORT" > consumer.log 2>&1 &
93 }
94
95 stop_consumer() {
96     pgrep -f "python consumer.py" || return 0
97     kill $(pgrep -f "python consumer.py")
98     cat consumer.log
99 }
100
101 wait_for_vm_launch() {
102     echo "waiting for vm launch..."
103     while true
104     do
105         state=$(nova list | grep " $VM_NAME " | awk '{print $6}')
106         [[ "$state" == "ACTIVE" ]] && return 0
107         sleep 1
108     done
109 }
110
111 inject_failure() {
112     #FIXME
113     echo ssh $COMPUTE_HOST "ip link set eno1 down"
114 }
115
116 calculate_notification_time() {
117     detect=$(grep "doctor monitor detected at" monitor.log | awk '{print $5}')
118     notified=$(grep "doctor consumer notified at" consumer.log | awk '{print $5}')
119     duration=$(echo "$notified $detect" | awk '{print $1 - $2 }')
120     echo "$notified $detect" | \
121         awk '{d = $1 - $2; if (d < 1 ) print d " OK"; else print d " NG"}'
122 }
123
124 # TODO(r-mibu): Make sure env params are set properly for OpenStack clients
125 # TODO(r-mibu): Make sure POD for doctor test is available in Pharos
126
127 echo "Note: doctor/tests/run.sh has been executed, "
128 echo "      but skipping this test due to lack of available test env/deployment."
129 exit 0
130
131 download_image
132 register_image
133
134 start_monitor
135 start_inspector
136 start_consumer
137
138 boot_vm
139 create_alarm
140 wait_for_vm_launch
141
142 sleep 60
143 inject_failure
144 sleep 10
145
146 calculate_notification_time
147
148 echo "done"