Add resources and outputs informantion in 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 [[ "${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 }
113
114
115 make_patch_for_provider_network() {
116
117     # copy temp file
118     echo "    Patch provider network for input file:"
119     echo "      Raw yaml file: ${VRNC_INPUT_TEMPLATE_RAW_FILE}"
120     echo "      Patched yaml file: ${VRNC_INPUT_TEMPLATE_FILE}"
121     cp ${VRNC_INPUT_TEMPLATE_RAW_FILE} ${VRNC_INPUT_TEMPLATE_FILE}
122     echo ""
123
124     # Delete the provider:network_type
125     echo "      Patching provider:network_type..."
126     sed -i '/network_type:/d' ${VRNC_INPUT_TEMPLATE_FILE}
127
128     # Delete the provider:segmentation_id
129     echo "      Patching provider:segmentation_id..."
130     sed -i '/segmentation_id:/d' ${VRNC_INPUT_TEMPLATE_FILE}
131
132     # Delete the provider:physical_network
133     echo "      Patching provider:physical_network..."
134     sed -i '/physical_network:/d' ${VRNC_INPUT_TEMPLATE_FILE}
135
136     echo ""
137
138 }
139
140 make_patch_for_translated_file() {
141
142     # Replace the signal_transport
143     echo "    Patch yaml file: ${VRNC_OUTPUT_TEMPLATE_FILE}"
144     echo ""
145     echo "    Patching compute:signal_transport:"
146     sed -i '1,$s/HEAT_SIGNAL/NO_SIGNAL/g' ${VRNC_OUTPUT_TEMPLATE_FILE}
147
148 }
149
150
151 translator_and_deploy_vRNC() {
152
153     (
154         # 1. Delete parser stack ${PARSER_STACK_NAME}, use admin user in admin project
155         openstack ${debug} stack list | grep -qow ${PARSER_STACK_NAME} && {
156             echo "  Stack ${PARSER_STACK_NAME} exist, delete it first."
157             openstack stack delete --yes --wait ${PARSER_STACK_NAME}
158         }
159         # 2. Switch env to parser project temporally
160         echo "  Switch openstack env to parser project"
161         change_env_to_parser_user_project
162         echo ""
163
164         # 3. Patch provider network
165         echo "  Make patch for provider network"
166         make_patch_for_provider_network
167
168         # 4. Translator yaml
169         echo "  Translator use parser:"
170         echo "    1. Input  file: ${VRNC_INPUT_TEMPLATE_FILE}"
171         echo "    2. Output file: ${VRNC_OUTPUT_TEMPLATE_FILE}"
172         heat-translator --template-type tosca --template-file ${VRNC_INPUT_TEMPLATE_FILE} \
173             --output-file ${VRNC_OUTPUT_TEMPLATE_FILE}
174         echo ""
175
176         # 5. Patch translated file
177         echo "  Make patch for translated file:"
178         make_patch_for_translated_file
179         echo ""
180
181         # 6. deploy vRNC
182         echo "  Deploy stack..."
183         [[ "${PARSER_CI_DEBUG}" == "true" ]] && debug="--debug" || debug=""
184         openstack ${debug} stack create --timeout 15 --wait --enable-rollback \
185                                         -t ${VRNC_OUTPUT_TEMPLATE_FILE} ${PARSER_STACK_NAME}
186
187         # 7. basic information.
188         echo "  The basic information of deployment..."
189         openstack ${debug} stack show ${PARSER_STACK_NAME}
190
191         # 8. deployed resources.
192         echo "  The resources of deployment..."
193         openstack ${debug} stack resource list ${PARSER_STACK_NAME}
194
195         # 9. deployed outputs.
196         echo "  The outputs of deployment..."
197         openstack ${debug} stack output list ${PARSER_STACK_NAME}
198
199         # 10. Validate the deploy result.
200         echo "  Checking the result of deployment..."
201         openstack ${debug} stack show ${PARSER_STACK_NAME} | grep -qow "CREATE_COMPLETE" && {
202             echo "    Check the result of deployment successfully."
203         } || {
204             echo "    Check the result of deployment unsuccessfully."
205         }
206     )
207
208 }
209
210 reset_parser_test() {
211
212     set +e
213
214     echo "  Clean-up the environment..."
215     # 1. Delete resource created by parser user
216     (
217         # 1). Switch env to parser project temporally
218         change_env_to_parser_user_project
219
220         # 2). Delete the stack ${PARSER_STACK_NAME}
221         echo ""
222         openstack ${debug} stack list | grep -qow ${PARSER_STACK_NAME} && {
223             echo "    Stack ${PARSER_STACK_NAME} has been created, delete it after test."
224             echo ""
225             openstack ${debug} stack delete --yes --wait ${PARSER_STACK_NAME}
226             echo ""
227         }
228
229         # 3). Delete patch tmp file ${VRNC_INPUT_TEMPLATE_FILE}
230         [ -e ${VRNC_OUTPUT_TEMPLATE_FILE} -a ${PARSER_CI_DEBUG} != "true" ] && {
231             echo "    Delete patch temp file ${VRNC_INPUT_TEMPLATE_FILE} after test."
232             rm -fr ${VRNC_INPUT_TEMPLATE_FILE}
233         }
234
235         # 4). Delete hot tmp file ${VRNC_OUTPUT_TEMPLATE_FILE}
236         [ -e ${VRNC_OUTPUT_TEMPLATE_FILE} -a ${PARSER_CI_DEBUG} != "true" ] && {
237             echo "    Delete hot temp file ${VRNC_OUTPUT_TEMPLATE_FILE} after test."
238             rm -fr ${VRNC_OUTPUT_TEMPLATE_FILE}
239         }
240
241         # 5). Delete tmp image ${PARSER_IMAGE_FILE}
242         [[ -e ${PARSER_IMAGE_FILE} ]] && {
243             echo "    Delete local image file ${PARSER_IMAGE_FILE} after test."
244             rm -fr ${PARSER_IMAGE_FILE}
245         }
246
247         # 6). Delete tmp image ${PARSER_IMAGE_URL_FILE}
248         [ -e ${PARSER_IMAGE_URL_FILE} -a ${PARSER_CI_DEBUG} != "true" ] && {
249             echo "    Delete local URL image file ${PARSER_IMAGE_URL_FILE} after test."
250             rm -fr ${PARSER_IMAGE_URL_FILE}
251         }
252
253         # 7). Delete image from openstack
254         parser_image_id=$(openstack ${debug} image list | grep -w "${PARSER_IMAGE_NAME}" | awk '{print $2}')
255         [[ -n "${parser_image_id}" ]] && openstack image delete "${parser_image_id}"
256
257         sleep 3
258     )
259
260     # 2. Delete role, user and project
261     echo "    Delete user ${PARSER_USER}'s role from project ${PARSER_PROJECT}"
262     openstack ${debug} user role list "${PARSER_USER}" --project "${PARSER_PROJECT}" \
263     | grep -qow "${PARSER_ROLE}" && {
264         openstack ${debug} role remove "${PARSER_ROLE}" --user "${PARSER_USER}" \
265                               --project "${PARSER_PROJECT}"
266     }
267
268     echo "    Delete user ${PARSER_USER}"
269     openstack ${debug} user list | grep -qow "${PARSER_USER}" && {
270         openstack user delete "${PARSER_USER}"
271     }
272
273     echo "    Delete project ${PARSER_PROJECT}"
274     openstack ${debug} project list | grep -qwo "${PARSER_PROJECT}" && {
275         openstack project delete "${PARSER_PROJECT}"
276     }
277
278     echo ""
279     echo "======================={ Parser functest end }=========================="
280     echo ""
281     echo ""
282
283 }
284
285 echo ""
286 echo ""
287 echo "======================={ Parser functest begin }=========================="
288 echo ""
289
290 trap reset_parser_test EXIT
291
292 # start syslog for loghander
293 service rsyslog restart
294
295 echo "|========= 1/4. Preparing VM image for parser...     =========|"
296 download_parser_image
297 register_parser_image
298
299 echo ""
300 echo "|========= 2/4. Creating test user for parser...     =========|"
301 create_parser_user_and_project
302
303 echo ""
304 echo "|========= 3/4. Parse -> translate -> deploy vRNC... =========|"
305 translator_and_deploy_vRNC
306
307 echo ""
308 echo "|========= 4/4. Test ok...                           =========|"
309 echo ""