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