Merge "Add framework for presentations"
[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 instance_1="opnfv-instance1"
70 instance_2="opnfv-instance2"
71 instance_3="opnfv-instance3"
72 instance_4="opnfv-instance4"
73
74
75
76 function wait_for_ip() {
77     # $1 is the instance name
78     # $2 is the first octet of the subnet ip
79     timeout=60
80     while [[ ${timeout} > 0 ]]; do
81         if [[ $(nova console-log $1|grep "No lease, failing") ]]; then
82             error "The instance $1 couldn't get an IP from the DHCP agent." | tee -a $LOG_FILE 1>&2
83             exit 1
84         elif [[ $(nova console-log $1|grep "^Lease"|grep "obtained") ]]; then
85             debug "The instance $1 got an IP successfully from the DHCP agent." | tee -a $LOG_FILE 1>&2
86             break
87         fi
88         let timeout=timeout-1
89         sleep 1
90     done
91 }
92
93
94 #################################
95 info "Testing Keystone API..." | tee -a $LOG_FILE 1>&2
96 #################################
97 openstack project create ${project_1}
98 debug "project '${project_1}' created."
99 openstack project create ${project_2}
100 debug "project '${project_2}' created."
101 openstack user create ${user_1} --project ${project_1}
102 debug "user '${user_1}' created in project ${project_1}."
103 openstack user create ${user_2} --project ${project_1}
104 debug "user '${user_2}' created in project ${project_1}."
105 openstack user create ${user_3} --project ${project_1}
106 debug "user '${user_3}' created in project ${project_1}."
107 openstack user create ${user_4} --project ${project_2}
108 debug "user '${user_4}' created in project ${project_2}."
109 openstack user create ${user_5} --project ${project_2}
110 debug "user '${user_5}' created in project ${project_2}."
111 openstack user create ${user_6} --project ${project_2}
112 debug "user '${user_6}' created in project ${project_2}."
113 info "...Keystone OK!"
114
115 #################################
116 info "Testing Glance API..."
117 #################################
118 image=/home/opnfv/functest/data/cirros-0.3.4-x86_64-disk.img
119 glance image-create --name ${image_1} --disk-format qcow2 --container-format bare < ${image}
120 debug "image '${image_1}' created."
121 glance image-create --name ${image_2} --disk-format qcow2 --container-format bare < ${image}
122 debug "image '${image_2}' created."
123 info "... Glance OK!"
124
125 #################################
126 info "Testing Cinder API..."
127 #################################
128 cinder create --display_name ${volume_1} 1
129 debug "volume '${volume_1}' created."
130 cinder create --display_name ${volume_2} 10
131 debug "volume '${volume_2}' created."
132 info "...Cinder OK!"
133
134 #################################
135 info "Testing Neutron API..."
136 #################################
137
138 network_ids=($(neutron net-list|grep -v "+"|grep -v name|awk '{print $2}'))
139 for id in ${network_ids[@]}; do
140     [[ $(neutron net-show ${id}|grep 'router:external'|grep -i "true") != "" ]] && ext_net_id=${id}
141 done
142 if [[ "${ext_net_id}" == "" ]]; then
143     error "No external network found. Exiting Health Check..."
144     exit 1
145 else
146     info "External network found. ${ext_net_id}"
147 fi
148
149 info "1. Create Networks..."
150 neutron net-create ${net_1}
151 debug "net '${net_1}' created."
152 neutron net-create ${net_2}
153 debug "net '${net_2}' created."
154 net1_id=$(neutron net-list | grep ${net_1} | awk '{print $2}')
155 net2_id=$(neutron net-list | grep ${net_2} | awk '{print $2}')
156
157 info "2. Create subnets..."
158 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
159 debug "subnet '${subnet_1}' created."
160 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
161 debug "subnet '${subnet_2}' created."
162
163 info "4. Create Routers..."
164 neutron router-create ${router_1}
165 debug "router '${router_1}' created."
166 neutron router-create ${router_2}
167 debug "router '${router_2}' created."
168
169 neutron router-gateway-set ${router_1} ${ext_net_id}
170 debug "router '${router_1}' gateway set to ${ext_net_id}."
171 neutron router-gateway-set ${router_2} ${ext_net_id}
172 debug "router '${router_2}' gateway set to ${ext_net_id}."
173
174 neutron router-interface-add ${router_1} ${subnet_1}
175 debug "router '${router_1}' interface added ${subnet_1}."
176 neutron router-interface-add ${router_2} ${subnet_2}
177 debug "router '${router_2}' interface added ${subnet_2}."
178
179 info "...Neutron OK!"
180
181 #################################
182 info "Testing Nova API..."
183 #################################
184
185 # This delay should be removed after resolving Jira case APEX-149.
186 # The purpose is to give some time to populate openflow rules
187 # by SDN controller in case of odl_l2 scenario.
188 sleep 60
189
190 nova boot --flavor m1.small --image ${image_1} --nic net-id=${net1_id} ${instance_1}
191 debug "nova instance '${instance_1}' booted on ${net_1}."
192 nova boot --flavor m1.small --image ${image_1} --nic net-id=${net1_id} ${instance_2}
193 debug "nova instance '${instance_2}' booted on ${net_1}."
194 nova boot --flavor m1.small --image ${image_2} --nic net-id=${net2_id} ${instance_3}
195 debug "nova instance '${instance_3}' booted on ${net_2}."
196 nova boot --flavor m1.small --image ${image_2} --nic net-id=${net2_id} ${instance_4}
197 debug "nova instance '${instance_4}' booted on ${net_2}."
198
199 vm1_id=$(nova list | grep ${instance_1} | awk '{print $2}')
200 vm2_id=$(nova list | grep ${instance_2} | awk '{print $2}')
201 vm3_id=$(nova list | grep ${instance_3} | awk '{print $2}')
202 vm4_id=$(nova list | grep ${instance_4} | awk '{print $2}')
203 info "...Nova OK!"
204
205 info "Checking if instances get an IP from DHCP..."
206 wait_for_ip ${instance_1} "10.6"
207 wait_for_ip ${instance_2} "10.6"
208 wait_for_ip ${instance_3} "10.7"
209 wait_for_ip ${instance_4} "10.7"
210 info "...DHCP OK!"
211
212 info "Health check passed!"
213 exit 0