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