Update cirros image to 0.3.5
[parser.git] / tests / functest_run.sh
1 #!/bin/bash -e
2 ##############################################################################
3 # Copyright (c) 2016 ZTE Corporation.
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 PARSER_CI_DEBUG=${CI_DEBUG:-false}
12 [[ "${PARSER_CI_DEBUG}" == "true" ]] && {
13     set -x
14     debug="--debug"
15 } || {
16     set +x
17     debug=""
18 }
19
20 if [ -n $1 ]; then
21     PARSER_IMAGE_URL_FILE=$1
22 else
23     PARSER_IMAGE_URL_FILE=cirros-0.3.5-x86_64-disk.img
24 fi
25 # PARSER_IMAGE_URL=https://launchpad.net/cirros/trunk/0.3.0/+download/${PARSER_IMAGE_URL_FILE}
26 PARSER_IMAGE_URL=http://download.cirros-cloud.net/0.3.2/${PARSER_IMAGE_URL_FILE}
27 # PARSER_IMAGE_NAME=rhel-6.5-test-image
28 PARSER_IMAGE_NAME=cirros-0.3.2-x86_64-uec
29 PARSER_IMAGE_FILE="${PARSER_IMAGE_NAME}.img"
30 PARSER_IMAGE_FORMAT=qcow2
31
32 PARSER_USER=parser
33 PARSER_PASSWORD=parser
34 PARSER_EMAIL='shang.xiaodong@zte.com.cn'
35 PARSER_PROJECT=parser
36 PARSER_TENANT=${PARSER_PROJECT}
37
38 PARSER_ROLE=admin
39
40 PARSER_STACK_NAME=vRNC_Stack
41
42 # VRNC_INPUT_TEMPLATE_FILE=../tosca2heat/tosca-parser/toscaparser/extensions/nfv/tests/data/vRNC/Definitions/vRNC.yaml
43 # VRNC_INPUT_TEMPLATE_RAW_FILE=../tosca2heat/heat-translator/translator/tests/data/test_tosca_nfv_sample.yaml
44 VRNC_INPUT_TEMPLATE_RAW_FILE=../tosca2heat/heat-translator/translator/tests/data/vRNC/Definitions/vRNC.yaml
45 VRNC_OUTPUT_TEMPLATE_FILE=../tosca2heat/heat-translator/translator/tests/data/vRNC/vRNC_Hot_Template.yaml
46
47 VRNC_INPUT_TEMPLATE_FILE=${VRNC_INPUT_TEMPLATE_RAW_FILE%.*}_patch.yaml
48
49 download_parser_image() {
50     [ -e "${PARSER_IMAGE_URL_FILE}" ] && {
51         echo "  Image ${PARSER_IMAGE_URL_FILE} has bee cached, needn't download again."
52         cp ${PARSER_IMAGE_URL_FILE} ${PARSER_IMAGE_FILE}
53         return 0
54     }
55
56     echo ""
57     echo "  Download image ${PARSER_IMAGE_URL_FILE}..."
58     wget ${PARSER_IMAGE_URL} -o ${PARSER_IMAGE_FILE}
59 }
60
61 register_parser_image() {
62     openstack ${debug} image list | grep -qwo "${PARSER_IMAGE_NAME}" && {
63         echo "  Image ${PARSER_IMAGE_NAME} has bee registed, needn't registe again."
64         return 0
65     }
66
67     echo ""
68     echo "  Registe image ${PARSER_IMAGE_NAME}..."
69     openstack ${debug} image create "${PARSER_IMAGE_NAME}" \
70                            --public \
71                            --disk-format ${PARSER_IMAGE_FORMAT} \
72                            --container-format bare \
73                            --file ${PARSER_IMAGE_FILE}
74 }
75
76 create_parser_user_and_project() {
77
78     # 1. create parser project
79     openstack ${debug} project list | grep -qwo "${PARSER_PROJECT}" && {
80         echo "  Project ${PARSER_PROJECT} exist, doesn't create agian."
81     } || {
82         openstack  ${debug} project create ${PARSER_PROJECT} \
83             --description "Project for parser test"
84         echo "  Create project ${PARSER_PROJECT} successful."
85     }
86
87     # 2. create parser user.
88     openstack ${debug} user list | grep -qwo ${PARSER_USER} && {
89         echo "  User ${PARSER_USER} exist, doesn't create again."
90     } || {
91         openstack ${debug} user create ${PARSER_USER} --password ${PARSER_PASSWORD} \
92             --project ${PARSER_PROJECT} --email ${PARSER_EMAIL}
93         echo "  Create user ${PARSER_USER} successful."
94     }
95
96     # 3. grant role for parser user
97     openstack ${debug} user role list ${PARSER_USER} --project ${PARSER_PROJECT} \
98     | grep -qow ${PARSER_ROLE} && {
99         echo "  User ${PARSER_USER} has role ${PARSER_ROLE} in project ${PARSER_PROJECT}, doesn't create."
100     } || {
101         openstack ${debug} role add ${PARSER_ROLE} --user ${PARSER_USER} \
102                            --project ${PARSER_PROJECT}
103         echo "  Grant user ${PARSER_USER} the role ${PARSER_ROLE} in project ${PARSER_PROJECT} successful."
104     }
105
106 }
107
108 change_env_to_parser_user_project() {
109
110     export OS_USERNAME=${PARSER_USER}
111     export OS_PASSWORD=${PARSER_PASSWORD}
112     export OS_PROJECT_NAME=${PARSER_PROJECT}
113     export OS_TENANT_NAME=${PARSER_TENANT}
114
115     export OS_PROJECT_DOMAIN_NAME=${OS_PROJECT_DOMAIN_NAME:-'Default'}
116     export OS_USER_DOMAIN_NAME=${OS_USER_DOMAIN_NAME:-'Default'}
117
118 }
119
120
121 make_patch_for_provider_network() {
122
123     # copy temp file
124     echo "    Patch provider network for input file:"
125     echo "      Raw yaml file: ${VRNC_INPUT_TEMPLATE_RAW_FILE}"
126     echo "      Patched yaml file: ${VRNC_INPUT_TEMPLATE_FILE}"
127     cp ${VRNC_INPUT_TEMPLATE_RAW_FILE} ${VRNC_INPUT_TEMPLATE_FILE}
128     echo ""
129
130     # Delete the provider:network_type
131     echo "      Patching provider:network_type..."
132     sed -i '/network_type:/d' ${VRNC_INPUT_TEMPLATE_FILE}
133
134     # Delete the provider:segmentation_id
135     echo "      Patching provider:segmentation_id..."
136     sed -i '/segmentation_id:/d' ${VRNC_INPUT_TEMPLATE_FILE}
137
138     # Delete the provider:physical_network
139     echo "      Patching provider:physical_network..."
140     sed -i '/physical_network:/d' ${VRNC_INPUT_TEMPLATE_FILE}
141
142     echo ""
143
144 }
145
146 make_patch_for_translated_file() {
147
148     # Replace the signal_transport
149     echo "    Patch yaml file: ${VRNC_OUTPUT_TEMPLATE_FILE}"
150     echo ""
151     echo "    Patching compute:signal_transport"
152     sed -i '1,$s/HEAT_SIGNAL/NO_SIGNAL/g' ${VRNC_OUTPUT_TEMPLATE_FILE}
153
154 }
155
156
157 translator_and_deploy_vRNC() {
158
159     (
160         # 1. Delete parser stack ${PARSER_STACK_NAME}, use admin user in admin project
161         openstack ${debug} stack list | grep -qow ${PARSER_STACK_NAME} && {
162             echo "  Stack ${PARSER_STACK_NAME} exist, delete it first."
163             openstack stack delete --yes --wait ${PARSER_STACK_NAME}
164         }
165         # 2. Switch env to parser project temporally
166         echo "  Switch openstack env to parser project"
167         change_env_to_parser_user_project
168         echo ""
169
170         # 3. Patch provider network
171         echo "  Make patch for provider network"
172         make_patch_for_provider_network
173
174         # 4. Translator yaml
175         echo "  Translator use parser:"
176         echo "    1. Input  file: ${VRNC_INPUT_TEMPLATE_FILE}"
177         echo "    2. Output file: ${VRNC_OUTPUT_TEMPLATE_FILE}"
178         heat-translator --template-type tosca --template-file ${VRNC_INPUT_TEMPLATE_FILE} \
179             --output-file ${VRNC_OUTPUT_TEMPLATE_FILE}
180         echo ""
181
182         # 5. Patch translated file
183         echo "  Make patch for translated file:"
184         make_patch_for_translated_file
185         echo ""
186
187         # 6. deploy vRNC
188         echo "  Deploy stack..."
189         [[ "${PARSER_CI_DEBUG}" == "true" ]] && debug="--debug" || debug=""
190         openstack ${debug} stack create --timeout 15 --wait --enable-rollback \
191                                         -t ${VRNC_OUTPUT_TEMPLATE_FILE} ${PARSER_STACK_NAME}
192
193         # 7. basic information.
194         echo "  The basic information of deployment..."
195         openstack ${debug} stack show ${PARSER_STACK_NAME}
196
197         # 8. deployed resources.
198         echo "  The resources of deployment..."
199         openstack ${debug} stack resource list ${PARSER_STACK_NAME}
200
201         # 9. deployed outputs.
202         echo "  The outputs of deployment..."
203         openstack ${debug} stack output list ${PARSER_STACK_NAME}
204
205         # 10. Validate the deploy result.
206         echo "  Checking the result of deployment..."
207         openstack ${debug} stack show ${PARSER_STACK_NAME} | grep -qow "CREATE_COMPLETE" && {
208             echo "    Check the result of deployment successfully."
209         } || {
210             echo "    Check the result of deployment unsuccessfully."
211         }
212     )
213
214 }
215
216 reset_parser_test() {
217
218     set +e
219
220     echo "  Clean-up the environment..."
221     # 1. Delete resource created by parser user
222     (
223         # 1). Switch env to parser project temporally
224         change_env_to_parser_user_project
225
226         # 2). Delete the stack ${PARSER_STACK_NAME}
227         echo ""
228         openstack ${debug} stack list | grep -qow ${PARSER_STACK_NAME} && {
229             echo "    Stack ${PARSER_STACK_NAME} has been created, delete it after test."
230             echo ""
231             openstack ${debug} stack delete --yes --wait ${PARSER_STACK_NAME}
232             echo ""
233         }
234
235         # 3). Delete patch tmp file ${VRNC_INPUT_TEMPLATE_FILE}
236         [ -e ${VRNC_OUTPUT_TEMPLATE_FILE} -a ${PARSER_CI_DEBUG} != "true" ] && {
237             echo "    Delete patch temp file ${VRNC_INPUT_TEMPLATE_FILE} after test."
238             rm -fr ${VRNC_INPUT_TEMPLATE_FILE}
239         }
240
241         # 4). Delete hot tmp file ${VRNC_OUTPUT_TEMPLATE_FILE}
242         [ -e ${VRNC_OUTPUT_TEMPLATE_FILE} -a ${PARSER_CI_DEBUG} != "true" ] && {
243             echo "    Delete hot temp file ${VRNC_OUTPUT_TEMPLATE_FILE} after test."
244             rm -fr ${VRNC_OUTPUT_TEMPLATE_FILE}
245         }
246
247         # 5). Delete tmp image ${PARSER_IMAGE_FILE}
248         [[ -e ${PARSER_IMAGE_FILE} ]] && {
249             echo "    Delete local image file ${PARSER_IMAGE_FILE} after test."
250             rm -fr ${PARSER_IMAGE_FILE}
251         }
252
253         # 6). Delete tmp image ${PARSER_IMAGE_URL_FILE}
254         [ -e ${PARSER_IMAGE_URL_FILE} -a ${PARSER_CI_DEBUG} != "true" ] && {
255             echo "    Delete local URL image file ${PARSER_IMAGE_URL_FILE} after test."
256             rm -fr ${PARSER_IMAGE_URL_FILE}
257         }
258
259         # 7). Delete image from openstack
260         parser_image_id=$(openstack ${debug} image list | grep -w "${PARSER_IMAGE_NAME}" | awk '{print $2}')
261         [[ -n "${parser_image_id}" ]] && openstack image delete "${parser_image_id}"
262
263         sleep 3
264     )
265
266     # 2. Delete role, user and project
267     echo "    Delete user ${PARSER_USER}'s role from project ${PARSER_PROJECT}"
268     openstack ${debug} user role list "${PARSER_USER}" --project "${PARSER_PROJECT}" \
269     | grep -qow "${PARSER_ROLE}" && {
270         openstack ${debug} role remove "${PARSER_ROLE}" --user "${PARSER_USER}" \
271                               --project "${PARSER_PROJECT}"
272     }
273
274     echo "    Delete user ${PARSER_USER}"
275     openstack ${debug} user list | grep -qow "${PARSER_USER}" && {
276         openstack user delete "${PARSER_USER}"
277     }
278
279     echo "    Delete project ${PARSER_PROJECT}"
280     openstack ${debug} project list | grep -qwo "${PARSER_PROJECT}" && {
281         openstack project delete "${PARSER_PROJECT}"
282     }
283
284     echo ""
285     echo "======================={ Parser functest end }=========================="
286     echo ""
287     echo ""
288
289 }
290
291 echo ""
292 echo ""
293 echo "======================={ Parser functest begin }=========================="
294 echo ""
295
296 trap reset_parser_test EXIT
297
298 # start syslog for loghander
299 service rsyslog restart
300
301 echo "|========= 1/4. Preparing VM image for parser...     =========|"
302 download_parser_image
303 register_parser_image
304
305 echo ""
306 echo "|========= 2/4. Creating test user for parser...     =========|"
307 create_parser_user_and_project
308
309 echo ""
310 echo "|========= 3/4. Parse -> translate -> deploy vRNC... =========|"
311 translator_and_deploy_vRNC
312
313 echo ""
314 echo "|========= 4/4. Test ok...                           =========|"
315 echo ""