Merge "Update rally version"
[functest.git] / testcases / OpenStack / healthcheck / healthcheck.sh
1 #
2 # OpenStack Health Check
3 # This script is meant for really basic API operations on OpenStack
4 # Services tested: Keystone, Glance, Cinder, Neutron, Nova
5 #
6 #
7 # Author:
8 #    jose.lausuch@ericsson.com
9 #
10 #
11 # All rights reserved. This program and the accompanying materials
12 # are made available under the terms of the Apache License, Version 2.0
13 # which accompanies this distribution, and is available at
14 # http://www.apache.org/licenses/LICENSE-2.0
15 #
16
17 set -e
18
19 #Redirect all the output (stdout) to a log file and show only possible errors.
20 LOG_FILE=/home/opnfv/functest/results/healthcheck.log
21 YAML_FILE=${CONFIG_FUNCTEST_YAML}
22 echo "">$LOG_FILE
23 exec 1<>$LOG_FILE
24
25 info ()  {
26     echo -e "$(date '+%Y-%m-%d %H:%M:%S,%3N') - healtcheck - INFO - " "$*" | tee -a $LOG_FILE 1>&2
27 }
28
29 debug ()  {
30     if [[ "${CI_DEBUG,,}" == "true" ]]; then
31         echo -e "$(date '+%Y-%m-%d %H:%M:%S,%3N') - healtcheck - DEBUG - " "$*" | tee -a $LOG_FILE 1>&2
32     fi
33 }
34
35 error () {
36     echo -e "$(date '+%Y-%m-%d %H:%M:%S,%3N') - healtcheck - ERROR - " "$*" | tee -a $LOG_FILE 1>&2
37     exit 1
38 }
39
40 if [ -z $OS_AUTH_URL ]; then
41     echo "Source credentials first."
42     exit 1
43 fi
44
45
46 echo "Using following credentials:"
47 env | grep OS
48
49 ## Variables:
50 project_1="opnfv-tenant1"
51 project_2="opnfv-tenant2"
52 user_1="opnfv_user1"
53 user_2="opnfv_user2"
54 user_3="opnfv_user3"
55 user_4="opnfv_user4"
56 user_5="opnfv_user5"
57 user_6="opnfv_user6"
58 kernel_image="opnfv-kernel-img"
59 ramdisk_image="opnfv-ramdisk-img"
60 image_1="opnfv-image1"
61 image_2="opnfv-image2"
62 volume_1="opnfv-volume1"
63 volume_2="opnfv-volume2"
64 net_1="opnfv-network1"
65 net_2="opnfv-network2"
66 subnet_1="opnfv-subnet1"
67 subnet_2="opnfv-subnet2"
68 port_1="opnfv-port1"
69 port_2="opnfv-port2"
70 router_1="opnfv-router1"
71 router_2="opnfv-router2"
72 flavor="m1.tiny"
73 instance_1="opnfv-instance1"
74 instance_2="opnfv-instance2"
75 instance_3="opnfv-instance3"
76 instance_4="opnfv-instance4"
77
78
79
80 function wait_for_ip() {
81     # $1 is the instance name
82     # $2 is the first octet of the subnet ip
83     timeout=60
84     while [[ ${timeout} > 0 ]]; do
85         if [[ $(nova console-log $1|grep "No lease, failing") ]]; then
86             error "The instance $1 couldn't get an IP from the DHCP agent." | tee -a $LOG_FILE 1>&2
87             exit 1
88         elif [[ $(nova console-log $1|grep "^Lease"|grep "obtained") ]]; then
89             debug "The instance $1 got an IP successfully from the DHCP agent." | tee -a $LOG_FILE 1>&2
90             break
91         fi
92         let timeout=timeout-1
93         sleep 1
94     done
95 }
96
97
98 #################################
99 info "Testing Keystone API..." | tee -a $LOG_FILE 1>&2
100 #################################
101 openstack project create ${project_1}
102 debug "project '${project_1}' created."
103 openstack project create ${project_2}
104 debug "project '${project_2}' created."
105 openstack user create ${user_1} --project ${project_1}
106 debug "user '${user_1}' created in project ${project_1}."
107 openstack user create ${user_2} --project ${project_1}
108 debug "user '${user_2}' created in project ${project_1}."
109 openstack user create ${user_3} --project ${project_1}
110 debug "user '${user_3}' created in project ${project_1}."
111 openstack user create ${user_4} --project ${project_2}
112 debug "user '${user_4}' created in project ${project_2}."
113 openstack user create ${user_5} --project ${project_2}
114 debug "user '${user_5}' created in project ${project_2}."
115 openstack user create ${user_6} --project ${project_2}
116 debug "user '${user_6}' created in project ${project_2}."
117 info "...Keystone OK!"
118
119 #################################
120 info "Testing Glance API..."
121 #################################
122 disk_img=$(cat ${YAML_FILE} | shyaml get-value healthcheck.disk_image 2> /dev/null || true)
123 disk_format=$(cat ${YAML_FILE} | shyaml get-value healthcheck.disk_format 2> /dev/null || true)
124 kernel_img=$(cat ${YAML_FILE} | shyaml get-value healthcheck.kernel_image 2> /dev/null || true)
125 ramdisk_img=$(cat ${YAML_FILE} | shyaml get-value healthcheck.ramdisk_image 2> /dev/null || true)
126 extra_properties=$(cat ${YAML_FILE} | shyaml get-value healthcheck.extra_properties 2> /dev/null || true)
127
128 # Test if we need to create a 3part image
129 if [ "X$kernel_img" != "X" ]
130 then
131     img_id=$(glance image-create --name ${kernel_image} --disk-format aki \
132              --container-format bare < ${kernel_img} | awk '$2 == "id" { print $4 }')
133     extra_opts="--property kernel_id=${img_id}"
134
135     if [ "X$ramdisk_img" != "X" ]
136     then
137         img_id=$(glance image-create --name ${ramdisk_image} --disk-format ari \
138                  --container-format bare < ${ramdisk_img} | awk '$2 == "id" { print $4 }')
139         extra_opts="$extra_opts --property ramdisk_id=${img_id}"
140     fi
141 fi
142
143 if [ "X$extra_properties" != "X" ]
144 then
145     keys=$(cat ${YAML_FILE} | shyaml keys healthcheck.extra_properties)
146     for key in ${keys}
147     do
148         value=$(cat ${YAML_FILE} | shyaml get-value healthcheck.extra_properties.${key})
149         extra_opts="$extra_opts --property ${key}=\"${value}\""
150     done
151 fi
152
153 debug "image extra_properties=${extra_properties}"
154
155 eval glance image-create --name ${image_1} --disk-format ${disk_format} --container-format bare \
156              ${extra_opts} < ${disk_img}
157 debug "image '${image_1}' created."
158 eval glance image-create --name ${image_2} --disk-format ${disk_format} --container-format bare \
159              ${extra_opts} < ${disk_img}
160 debug "image '${image_2}' created."
161 info "... Glance OK!"
162
163 #################################
164 info "Testing Cinder API..."
165 #################################
166 cinder create --display_name ${volume_1} 1
167 debug "volume '${volume_1}' created."
168 cinder create --display_name ${volume_2} 10
169 debug "volume '${volume_2}' created."
170 info "...Cinder OK!"
171
172 #################################
173 info "Testing Neutron API..."
174 #################################
175
176 network_ids=($(neutron net-list|grep -v "+"|grep -v name|awk '{print $2}'))
177 for id in ${network_ids[@]}; do
178     [[ $(neutron net-show ${id}|grep 'router:external'|grep -i "true") != "" ]] && ext_net_id=${id}
179 done
180 if [[ "${ext_net_id}" == "" ]]; then
181     error "No external network found. Exiting Health Check..."
182     exit 1
183 else
184     info "External network found. ${ext_net_id}"
185 fi
186
187 info "1. Create Networks..."
188 neutron net-create ${net_1}
189 debug "net '${net_1}' created."
190 neutron net-create ${net_2}
191 debug "net '${net_2}' created."
192 net1_id=$(neutron net-list | grep ${net_1} | awk '{print $2}')
193 net2_id=$(neutron net-list | grep ${net_2} | awk '{print $2}')
194
195 info "2. Create subnets..."
196 neutron subnet-create --name ${subnet_1} --allocation-pool start=10.6.0.2,end=10.6.0.253 --gateway 10.6.0.254 ${net_1} 10.6.0.0/24
197 debug "subnet '${subnet_1}' created."
198 neutron subnet-create --name ${subnet_2} --allocation-pool start=10.7.0.2,end=10.7.0.253 --gateway 10.7.0.254 ${net_2} 10.7.0.0/24
199 debug "subnet '${subnet_2}' created."
200
201 info "3. Create Routers..."
202 neutron router-create ${router_1}
203 debug "router '${router_1}' created."
204 neutron router-create ${router_2}
205 debug "router '${router_2}' created."
206
207 neutron router-gateway-set ${router_1} ${ext_net_id}
208 debug "router '${router_1}' gateway set to ${ext_net_id}."
209 neutron router-gateway-set ${router_2} ${ext_net_id}
210 debug "router '${router_2}' gateway set to ${ext_net_id}."
211
212 neutron router-interface-add ${router_1} ${subnet_1}
213 debug "router '${router_1}' interface added ${subnet_1}."
214 neutron router-interface-add ${router_2} ${subnet_2}
215 debug "router '${router_2}' interface added ${subnet_2}."
216
217 info "...Neutron OK!"
218
219 #################################
220 info "Testing Nova API..."
221 #################################
222
223 # This delay should be removed after resolving Jira case APEX-149.
224 # The purpose is to give some time to populate openflow rules
225 # by SDN controller in case of odl_l2 scenario.
226 sleep 60
227
228
229 # Check if flavor exists
230 if [[ -z $(nova flavor-list|grep $flavor) ]]; then
231     # if given flavor doesn't exist, we create one
232     debug "Flavor $flavor doesn't exist. Creating a new flavor."
233     nova flavor-create --is-public false ${flavor} auto 512 1 1 --is-public True
234 fi
235 debug "Using flavor $flavor to boot the instances."
236
237
238 nova boot --flavor ${flavor} --image ${image_1} --nic net-id=${net1_id} ${instance_1}
239 debug "nova instance '${instance_1}' booted on ${net_1}."
240 nova boot --flavor ${flavor} --image ${image_1} --nic net-id=${net1_id} ${instance_2}
241 debug "nova instance '${instance_2}' booted on ${net_1}."
242 nova boot --flavor ${flavor} --image ${image_2} --nic net-id=${net2_id} ${instance_3}
243 debug "nova instance '${instance_3}' booted on ${net_2}."
244 nova boot --flavor ${flavor} --image ${image_2} --nic net-id=${net2_id} ${instance_4}
245 debug "nova instance '${instance_4}' booted on ${net_2}."
246
247 vm1_id=$(nova list | grep ${instance_1} | awk '{print $2}')
248 vm2_id=$(nova list | grep ${instance_2} | awk '{print $2}')
249 vm3_id=$(nova list | grep ${instance_3} | awk '{print $2}')
250 vm4_id=$(nova list | grep ${instance_4} | awk '{print $2}')
251 info "...Nova OK!"
252
253 info "Checking if instances get an IP from DHCP..."
254 wait_for_ip ${instance_1} "10.6"
255 wait_for_ip ${instance_2} "10.6"
256 wait_for_ip ${instance_3} "10.7"
257 wait_for_ip ${instance_4} "10.7"
258 info "...DHCP OK!"
259
260 info "Health check passed!"
261 exit 0