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