2 ##############################################################################
3 # Copyright (c) 2016 ZTE Corporation.
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 ##############################################################################
11 PARSER_CI_DEBUG=${CI_DEBUG:-false}
12 PRASER_WORK_DIR=$(cd $(dirname $0) && pwd)
14 [[ "${PARSER_CI_DEBUG}" == "true" ]] && {
21 LOCAL_IMAGE_FILE=${1:-""}
23 if [ -e "${LOCAL_IMAGE_FILE}" ]; then
24 echo "Input local image file: ${LOCAL_IMAGE_FILE}"
25 PARSER_IMAGE_URL_FILE=${LOCAL_IMAGE_FILE}
27 echo "No local image file or the file(${LOCAL_IMAGE_FILE}) doesn't exsit!"
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}."
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
43 PARSER_PASSWORD=parser
44 PARSER_EMAIL='shang.xiaodong@zte.com.cn'
46 PARSER_TENANT=${PARSER_PROJECT}
50 PARSER_STACK_NAME=vRNC_Stack
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
57 VRNC_INPUT_TEMPLATE_FILE=${VRNC_INPUT_TEMPLATE_RAW_FILE%.*}_patch.yaml
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}
67 echo " Download image ${PARSER_IMAGE_URL_FILE}..."
68 wget ${PARSER_IMAGE_URL} -o ${PARSER_IMAGE_FILE}
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."
78 echo " Registe image ${PARSER_IMAGE_NAME}..."
79 openstack ${debug} image create "${PARSER_IMAGE_NAME}" \
81 --disk-format ${PARSER_IMAGE_FORMAT} \
82 --container-format bare \
83 --file ${PARSER_IMAGE_FILE}
86 create_parser_user_and_project() {
88 # 1. create parser project
89 openstack ${debug} project list | grep -qwo "${PARSER_PROJECT}" && {
90 echo " Project ${PARSER_PROJECT} exist, doesn't create agian."
92 openstack ${debug} project create ${PARSER_PROJECT} \
93 --description "Project for parser test"
94 echo " Create project ${PARSER_PROJECT} successful."
97 # 2. create parser user.
98 openstack ${debug} user list | grep -qwo ${PARSER_USER} && {
99 echo " User ${PARSER_USER} exist, doesn't create again."
101 openstack ${debug} user create ${PARSER_USER} --password ${PARSER_PASSWORD} \
102 --project ${PARSER_PROJECT} --email ${PARSER_EMAIL}
103 echo " Create user ${PARSER_USER} successful."
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."
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."
120 change_env_to_parser_user_project() {
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}
127 export OS_PROJECT_DOMAIN_NAME=${OS_PROJECT_DOMAIN_NAME:-'Default'}
128 export OS_USER_DOMAIN_NAME=${OS_USER_DOMAIN_NAME:-'Default'}
133 make_patch_for_provider_network() {
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}
142 # Delete the provider:network_type
143 echo " Patching provider:network_type..."
144 sed -i '/network_type:/d' ${VRNC_INPUT_TEMPLATE_FILE}
146 # Delete the provider:segmentation_id
147 echo " Patching provider:segmentation_id..."
148 sed -i '/segmentation_id:/d' ${VRNC_INPUT_TEMPLATE_FILE}
150 # Delete the provider:physical_network
151 echo " Patching provider:physical_network..."
152 sed -i '/physical_network:/d' ${VRNC_INPUT_TEMPLATE_FILE}
158 make_patch_for_translated_file() {
160 # Replace the signal_transport
161 echo " Patch yaml file: ${VRNC_OUTPUT_TEMPLATE_FILE}"
163 echo " Patching compute:signal_transport"
164 sed -i '1,$s/HEAT_SIGNAL/NO_SIGNAL/g' ${VRNC_OUTPUT_TEMPLATE_FILE}
169 translator_and_deploy_vRNC() {
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}
177 # 2. Switch env to parser project temporally
178 echo " Switch openstack env to parser project"
179 change_env_to_parser_user_project
182 # 3. Patch provider network
183 echo " Make patch for provider network"
184 make_patch_for_provider_network
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}
194 # 5. Patch translated file
195 echo " Make patch for translated file:"
196 make_patch_for_translated_file
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}
205 # 7. basic information.
206 echo " The basic information of deployment..."
207 openstack ${debug} stack show ${PARSER_STACK_NAME}
209 # 8. deployed resources.
210 echo " The resources of deployment..."
211 openstack ${debug} stack resource list ${PARSER_STACK_NAME}
213 # 9. deployed outputs.
214 echo " The outputs of deployment..."
215 openstack ${debug} stack output list ${PARSER_STACK_NAME}
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."
222 echo " Check the result of deployment unsuccessfully."
228 reset_parser_test() {
232 echo " Clean-up the environment..."
233 # 1. Delete resource created by parser user
235 # 1). Switch env to parser project temporally
236 change_env_to_parser_user_project
238 # 2). Delete the stack ${PARSER_STACK_NAME}
240 openstack ${debug} stack list | grep -qow ${PARSER_STACK_NAME} && {
241 echo " Stack ${PARSER_STACK_NAME} has been created, delete it after test."
243 openstack ${debug} stack delete --yes --wait ${PARSER_STACK_NAME}
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}
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}
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}
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}
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}"
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}"
286 echo " Delete user ${PARSER_USER}"
287 openstack ${debug} user list | grep -qow "${PARSER_USER}" && {
288 openstack user delete "${PARSER_USER}"
291 echo " Delete project ${PARSER_PROJECT}"
292 openstack ${debug} project list | grep -qwo "${PARSER_PROJECT}" && {
293 openstack project delete "${PARSER_PROJECT}"
297 echo "======================={ Parser functest end }=========================="
305 echo "======================={ Parser functest begin }=========================="
308 trap reset_parser_test EXIT
310 # start syslog for loghander
311 which service > /dev/null && service rsyslog restart || true
313 echo "|========= 1/4. Preparing VM image for parser... =========|"
314 download_parser_image
315 register_parser_image
318 echo "|========= 2/4. Creating test user for parser... =========|"
319 create_parser_user_and_project
322 echo "|========= 3/4. Parse -> translate -> deploy vRNC... =========|"
323 translator_and_deploy_vRNC
326 echo "|========= 4/4. Test ok... =========|"