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