Use flavor m1.tiny in healthcheck
[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 echo "">$LOG_FILE
22 exec 1<>$LOG_FILE
23
24 info ()  {
25     echo -e "$(date '+%Y-%m-%d %H:%M:%S,%3N') - healtcheck - INFO - " "$*" | tee -a $LOG_FILE 1>&2
26 }
27
28 debug ()  {
29     if [[ "${CI_DEBUG,,}" == "true" ]]; then
30         echo -e "$(date '+%Y-%m-%d %H:%M:%S,%3N') - healtcheck - DEBUG - " "$*" | tee -a $LOG_FILE 1>&2
31     fi
32 }
33
34 error () {
35     echo -e "$(date '+%Y-%m-%d %H:%M:%S,%3N') - healtcheck - ERROR - " "$*" | tee -a $LOG_FILE 1>&2
36     exit 1
37 }
38
39 if [ -z $OS_AUTH_URL ]; then
40     echo "Source credentials first."
41     exit 1
42 fi
43
44
45 echo "Using following credentials:"
46 env | grep OS
47
48 ## Variables:
49 project_1="opnfv-tenant1"
50 project_2="opnfv-tenant2"
51 user_1="opnfv_user1"
52 user_2="opnfv_user2"
53 user_3="opnfv_user3"
54 user_4="opnfv_user4"
55 user_5="opnfv_user5"
56 user_6="opnfv_user6"
57 image_1="opnfv-image1"
58 image_2="opnfv-image2"
59 volume_1="opnfv-volume1"
60 volume_2="opnfv-volume2"
61 net_1="opnfv-network1"
62 net_2="opnfv-network2"
63 subnet_1="opnfv-subnet1"
64 subnet_2="opnfv-subnet2"
65 port_1="opnfv-port1"
66 port_2="opnfv-port2"
67 router_1="opnfv-router1"
68 router_2="opnfv-router2"
69 flavor="m1.tiny"
70 instance_1="opnfv-instance1"
71 instance_2="opnfv-instance2"
72 instance_3="opnfv-instance3"
73 instance_4="opnfv-instance4"
74
75
76
77 function wait_for_ip() {
78     # $1 is the instance name
79     # $2 is the first octet of the subnet ip
80     timeout=60
81     while [[ ${timeout} > 0 ]]; do
82         if [[ $(nova console-log $1|grep "No lease, failing") ]]; then
83             error "The instance $1 couldn't get an IP from the DHCP agent." | tee -a $LOG_FILE 1>&2
84             exit 1
85         elif [[ $(nova console-log $1|grep "^Lease"|grep "obtained") ]]; then
86             debug "The instance $1 got an IP successfully from the DHCP agent." | tee -a $LOG_FILE 1>&2
87             break
88         fi
89         let timeout=timeout-1
90         sleep 1
91     done
92 }
93
94
95 #################################
96 info "Testing Keystone API..." | tee -a $LOG_FILE 1>&2
97 #################################
98 openstack project create ${project_1}
99 debug "project '${project_1}' created."
100 openstack project create ${project_2}
101 debug "project '${project_2}' created."
102 openstack user create ${user_1} --project ${project_1}
103 debug "user '${user_1}' created in project ${project_1}."
104 openstack user create ${user_2} --project ${project_1}
105 debug "user '${user_2}' created in project ${project_1}."
106 openstack user create ${user_3} --project ${project_1}
107 debug "user '${user_3}' created in project ${project_1}."
108 openstack user create ${user_4} --project ${project_2}
109 debug "user '${user_4}' created in project ${project_2}."
110 openstack user create ${user_5} --project ${project_2}
111 debug "user '${user_5}' created in project ${project_2}."
112 openstack user create ${user_6} --project ${project_2}
113 debug "user '${user_6}' created in project ${project_2}."
114 info "...Keystone OK!"
115
116 #################################
117 info "Testing Glance API..."
118 #################################
119 image=/home/opnfv/functest/data/cirros-0.3.4-x86_64-disk.img
120 glance image-create --name ${image_1} --disk-format qcow2 --container-format bare < ${image}
121 debug "image '${image_1}' created."
122 glance image-create --name ${image_2} --disk-format qcow2 --container-format bare < ${image}
123 debug "image '${image_2}' created."
124 info "... Glance OK!"
125
126 #################################
127 info "Testing Cinder API..."
128 #################################
129 cinder create --display_name ${volume_1} 1
130 debug "volume '${volume_1}' created."
131 cinder create --display_name ${volume_2} 10
132 debug "volume '${volume_2}' created."
133 info "...Cinder OK!"
134
135 #################################
136 info "Testing Neutron API..."
137 #################################
138
139 network_ids=($(neutron net-list|grep -v "+"|grep -v name|awk '{print $2}'))
140 for id in ${network_ids[@]}; do
141     [[ $(neutron net-show ${id}|grep 'router:external'|grep -i "true") != "" ]] && ext_net_id=${id}
142 done
143 if [[ "${ext_net_id}" == "" ]]; then
144     error "No external network found. Exiting Health Check..."
145     exit 1
146 else
147     info "External network found. ${ext_net_id}"
148 fi
149
150 info "1. Create Networks..."
151 neutron net-create ${net_1}
152 debug "net '${net_1}' created."
153 neutron net-create ${net_2}
154 debug "net '${net_2}' created."
155 net1_id=$(neutron net-list | grep ${net_1} | awk '{print $2}')
156 net2_id=$(neutron net-list | grep ${net_2} | awk '{print $2}')
157
158 info "2. Create subnets..."
159 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
160 debug "subnet '${subnet_1}' created."
161 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
162 debug "subnet '${subnet_2}' created."
163
164 info "3. Create Routers..."
165 neutron router-create ${router_1}
166 debug "router '${router_1}' created."
167 neutron router-create ${router_2}
168 debug "router '${router_2}' created."
169
170 neutron router-gateway-set ${router_1} ${ext_net_id}
171 debug "router '${router_1}' gateway set to ${ext_net_id}."
172 neutron router-gateway-set ${router_2} ${ext_net_id}
173 debug "router '${router_2}' gateway set to ${ext_net_id}."
174
175 neutron router-interface-add ${router_1} ${subnet_1}
176 debug "router '${router_1}' interface added ${subnet_1}."
177 neutron router-interface-add ${router_2} ${subnet_2}
178 debug "router '${router_2}' interface added ${subnet_2}."
179
180 info "...Neutron OK!"
181
182 #################################
183 info "Testing Nova API..."
184 #################################
185
186 # This delay should be removed after resolving Jira case APEX-149.
187 # The purpose is to give some time to populate openflow rules
188 # by SDN controller in case of odl_l2 scenario.
189 sleep 60
190
191
192 # Check if flavor exists
193 if [[ -z $(nova flavor-list|grep $flavor) ]]; then
194     # if given flavor doesn't exist, we create one
195     debug "Flavor $flavor doesn't exist. Creating a new flavor."
196     nova flavor-create --is-public false ${flavor} auto 512 1 1 --is-public True
197 fi
198 debug "Using flavor $flavor to boot the instances."
199
200
201 nova boot --flavor ${flavor} --image ${image_1} --nic net-id=${net1_id} ${instance_1}
202 debug "nova instance '${instance_1}' booted on ${net_1}."
203 nova boot --flavor ${flavor} --image ${image_1} --nic net-id=${net1_id} ${instance_2}
204 debug "nova instance '${instance_2}' booted on ${net_1}."
205 nova boot --flavor ${flavor} --image ${image_2} --nic net-id=${net2_id} ${instance_3}
206 debug "nova instance '${instance_3}' booted on ${net_2}."
207 nova boot --flavor ${flavor} --image ${image_2} --nic net-id=${net2_id} ${instance_4}
208 debug "nova instance '${instance_4}' booted on ${net_2}."
209
210 vm1_id=$(nova list | grep ${instance_1} | awk '{print $2}')
211 vm2_id=$(nova list | grep ${instance_2} | awk '{print $2}')
212 vm3_id=$(nova list | grep ${instance_3} | awk '{print $2}')
213 vm4_id=$(nova list | grep ${instance_4} | awk '{print $2}')
214 info "...Nova OK!"
215
216 info "Checking if instances get an IP from DHCP..."
217 wait_for_ip ${instance_1} "10.6"
218 wait_for_ip ${instance_2} "10.6"
219 wait_for_ip ${instance_3} "10.7"
220 wait_for_ip ${instance_4} "10.7"
221 info "...DHCP OK!"
222
223 info "Health check passed!"
224 exit 0