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