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