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 VM_FLAVOR_NAME=m1.tiny
55 PARSER_STACK_NAME=vRNC_Stack
57 # VRNC_INPUT_TEMPLATE_FILE=${PRASER_WORK_DIR}/../tosca2heat/tosca-parser/toscaparser/extensions/nfv/tests/data/vRNC/Definitions/vRNC.yaml
58 # VRNC_INPUT_TEMPLATE_RAW_FILE=${PRASER_WORK_DIR}/../tosca2heat/heat-translator/translator/tests/data/test_tosca_nfv_sample.yaml
59 VRNC_INPUT_TEMPLATE_RAW_FILE=${PRASER_WORK_DIR}/../tosca2heat/heat-translator/translator/tests/data/vRNC/Definitions/vRNC.yaml
60 VRNC_OUTPUT_TEMPLATE_FILE=${PRASER_WORK_DIR}/../tosca2heat/heat-translator/translator/tests/data/vRNC/vRNC_Hot_Template.yaml
62 VRNC_INPUT_TEMPLATE_FILE=${VRNC_INPUT_TEMPLATE_RAW_FILE%.*}_patch.yaml
64 download_parser_image() {
65 [ -e "${PARSER_IMAGE_URL_FILE}" ] && {
66 echo " Image ${PARSER_IMAGE_URL_FILE} has bee cached, needn't download again."
67 cp ${PARSER_IMAGE_URL_FILE} ${PARSER_IMAGE_FILE}
72 echo " Download image ${PARSER_IMAGE_URL_FILE}..."
73 wget ${PARSER_IMAGE_URL} -o ${PARSER_IMAGE_FILE}
76 register_parser_image_and_flavor() {
77 openstack ${debug} image list | grep -qwo "${PARSER_IMAGE_NAME}" && {
78 echo " Image ${PARSER_IMAGE_NAME} has bee registed, needn't registe again."
83 echo " Registe image ${PARSER_IMAGE_NAME}..."
84 openstack ${debug} image create "${PARSER_IMAGE_NAME}" \
86 --disk-format ${PARSER_IMAGE_FORMAT} \
87 --container-format bare \
88 --file ${PARSER_IMAGE_FILE}
90 [[ ! openstack flavor show ${VM_FLAVOR_NAME} ]] && {
91 echo " Create default flavor ${VM_FLAVOR_NAME}..."
92 openstack flavor create --ram ${VM_FLAVOR_RAM} \
93 --vcpus ${VM_FLAVOR_CPUS} --disk ${VM_FLAVOR_DISK} ${VM_FLAVOR_NAM}
97 create_parser_user_and_project() {
99 # 1. create parser project
100 openstack ${debug} project list | grep -qwo "${PARSER_PROJECT}" && {
101 echo " Project ${PARSER_PROJECT} exist, doesn't create agian."
103 openstack ${debug} project create ${PARSER_PROJECT} \
104 --description "Project for parser test"
105 echo " Create project ${PARSER_PROJECT} successful."
108 # 2. create parser user.
109 openstack ${debug} user list | grep -qwo ${PARSER_USER} && {
110 echo " User ${PARSER_USER} exist, doesn't create again."
112 openstack ${debug} user create ${PARSER_USER} --password ${PARSER_PASSWORD} \
113 --project ${PARSER_PROJECT} --email ${PARSER_EMAIL}
114 echo " Create user ${PARSER_USER} successful."
117 # 3. grant role for parser user
118 openstack ${debug} role assignment list --user ${PARSER_USER} --project ${PARSER_PROJECT} \
119 | grep -qow ${PARSER_ROLE} && {
120 echo " User ${PARSER_USER} has role ${PARSER_ROLE} in project ${PARSER_PROJECT}, doesn't create."
122 openstack ${debug} role add ${PARSER_ROLE} --user ${PARSER_USER} \
123 --project ${PARSER_PROJECT}
124 echo " Grant user ${PARSER_USER} the role of ${PARSER_ROLE} in project ${PARSER_PROJECT} successful."
125 openstack ${debug} role list | grep -qow heat_stack_owner && {
126 openstack ${debug} role add heat_stack_owner --user ${PARSER_USER} \
127 --project ${PARSER_PROJECT}
128 echo " Grant user ${PARSER_USER} the role of heat_stack_owner in project ${PARSER_PROJECT} successful."
134 change_env_to_parser_user_project() {
136 export OS_USERNAME=${PARSER_USER}
137 export OS_PASSWORD=${PARSER_PASSWORD}
138 export OS_PROJECT_NAME=${PARSER_PROJECT}
139 export OS_TENANT_NAME=${PARSER_TENANT}
141 export OS_PROJECT_DOMAIN_NAME=${OS_PROJECT_DOMAIN_NAME:-'Default'}
142 export OS_USER_DOMAIN_NAME=${OS_USER_DOMAIN_NAME:-'Default'}
147 make_patch_for_provider_network() {
150 echo " Patch provider network for input file:"
151 echo " Raw yaml file: ${VRNC_INPUT_TEMPLATE_RAW_FILE}"
152 echo " Patched yaml file: ${VRNC_INPUT_TEMPLATE_FILE}"
153 cp ${VRNC_INPUT_TEMPLATE_RAW_FILE} ${VRNC_INPUT_TEMPLATE_FILE}
156 # Delete the provider:network_type
157 echo " Patching provider:network_type..."
158 sed -i '/network_type:/d' ${VRNC_INPUT_TEMPLATE_FILE}
160 # Delete the provider:segmentation_id
161 echo " Patching provider:segmentation_id..."
162 sed -i '/segmentation_id:/d' ${VRNC_INPUT_TEMPLATE_FILE}
164 # Delete the provider:physical_network
165 echo " Patching provider:physical_network..."
166 sed -i '/physical_network:/d' ${VRNC_INPUT_TEMPLATE_FILE}
172 make_patch_for_translated_file() {
174 # Replace the signal_transport
175 echo " Patch yaml file: ${VRNC_OUTPUT_TEMPLATE_FILE}"
177 echo " Patching compute:signal_transport"
178 sed -i '1,$s/HEAT_SIGNAL/NO_SIGNAL/g' ${VRNC_OUTPUT_TEMPLATE_FILE}
183 translator_and_deploy_vRNC() {
186 # 1. Delete parser stack ${PARSER_STACK_NAME}, use admin user in admin project
187 openstack ${debug} stack list | grep -qow ${PARSER_STACK_NAME} && {
188 echo " Stack ${PARSER_STACK_NAME} exist, delete it first."
189 openstack stack delete --yes --wait ${PARSER_STACK_NAME}
191 # 2. Switch env to parser project temporally
192 echo " Switch openstack env to parser project"
193 change_env_to_parser_user_project
196 # 3. Patch provider network
197 echo " Make patch for provider network"
198 make_patch_for_provider_network
201 echo " Translator use parser:"
202 echo " 1. Input file: ${VRNC_INPUT_TEMPLATE_FILE}"
203 echo " 2. Output file: ${VRNC_OUTPUT_TEMPLATE_FILE}"
204 heat-translator --template-type tosca --template-file ${VRNC_INPUT_TEMPLATE_FILE} \
205 --output-file ${VRNC_OUTPUT_TEMPLATE_FILE}
208 # 5. Patch translated file
209 echo " Make patch for translated file:"
210 make_patch_for_translated_file
214 echo " Deploy stack..."
215 [[ "${PARSER_CI_DEBUG}" == "true" ]] && debug="--debug" || debug=""
216 openstack ${debug} stack create --timeout 15 --wait --enable-rollback \
217 -t ${VRNC_OUTPUT_TEMPLATE_FILE} ${PARSER_STACK_NAME}
219 # 7. basic information.
220 echo " The basic information of deployment..."
221 openstack ${debug} stack show ${PARSER_STACK_NAME}
223 # 8. deployed resources.
224 echo " The resources of deployment..."
225 openstack ${debug} stack resource list ${PARSER_STACK_NAME}
227 # 9. deployed outputs.
228 echo " The outputs of deployment..."
229 openstack ${debug} stack output list ${PARSER_STACK_NAME}
231 # 10. Validate the deploy result.
232 echo " Checking the result of deployment..."
233 openstack ${debug} stack show ${PARSER_STACK_NAME} | grep -qow "CREATE_COMPLETE" && {
234 echo " Check the result of deployment successfully."
236 echo " Check the result of deployment unsuccessfully."
242 reset_parser_test() {
246 echo " Clean-up the environment..."
247 # 1. Delete resource created by parser user
249 # 1). Switch env to parser project temporally
250 change_env_to_parser_user_project
252 # 2). Delete the stack ${PARSER_STACK_NAME}
254 openstack ${debug} stack list | grep -qow ${PARSER_STACK_NAME} && {
255 echo " Stack ${PARSER_STACK_NAME} has been created, delete it after test."
257 openstack ${debug} stack delete --yes --wait ${PARSER_STACK_NAME}
261 # 3). Delete patch tmp file ${VRNC_INPUT_TEMPLATE_FILE}
262 [ -e ${VRNC_OUTPUT_TEMPLATE_FILE} -a ${PARSER_CI_DEBUG} != "true" ] && {
263 echo " Delete patch temp file ${VRNC_INPUT_TEMPLATE_FILE} after test."
264 rm -fr ${VRNC_INPUT_TEMPLATE_FILE}
267 # 4). Delete hot tmp file ${VRNC_OUTPUT_TEMPLATE_FILE}
268 [ -e ${VRNC_OUTPUT_TEMPLATE_FILE} -a ${PARSER_CI_DEBUG} != "true" ] && {
269 echo " Delete hot temp file ${VRNC_OUTPUT_TEMPLATE_FILE} after test."
270 rm -fr ${VRNC_OUTPUT_TEMPLATE_FILE}
273 # 5). Delete tmp image ${PARSER_IMAGE_FILE}
274 [[ -e ${PARSER_IMAGE_FILE} ]] && {
275 echo " Delete local image file ${PARSER_IMAGE_FILE} after test."
276 rm -fr ${PARSER_IMAGE_FILE}
279 # 6). Delete tmp image ${PARSER_IMAGE_URL_FILE}
280 [ -e ${PARSER_IMAGE_URL_FILE} -a ${PARSER_CI_DEBUG} != "true" ] && {
281 echo " Delete local URL image file ${PARSER_IMAGE_URL_FILE} after test."
282 rm -fr ${PARSER_IMAGE_URL_FILE}
285 # 7). Delete image from openstack
286 parser_image_id=$(openstack ${debug} image list | grep -w "${PARSER_IMAGE_NAME}" | awk '{print $2}')
287 [[ -n "${parser_image_id}" ]] && openstack image delete "${parser_image_id}"
292 # 2. Delete role, user and project
293 echo " Delete user ${PARSER_USER}'s role from project ${PARSER_PROJECT}"
294 openstack ${debug} user role list "${PARSER_USER}" --project "${PARSER_PROJECT}" \
295 | grep -qow "${PARSER_ROLE}" && {
296 openstack ${debug} role remove "${PARSER_ROLE}" --user "${PARSER_USER}" \
297 --project "${PARSER_PROJECT}"
300 echo " Delete user ${PARSER_USER}"
301 openstack ${debug} user list | grep -qow "${PARSER_USER}" && {
302 openstack user delete "${PARSER_USER}"
305 echo " Delete project ${PARSER_PROJECT}"
306 openstack ${debug} project list | grep -qwo "${PARSER_PROJECT}" && {
307 openstack project delete "${PARSER_PROJECT}"
311 echo "======================={ Parser functest end }=========================="
319 echo "======================={ Parser functest begin }=========================="
322 trap reset_parser_test EXIT
324 # start syslog for loghander
325 which service > /dev/null && service rsyslog restart || true
327 echo "|========= 1/4. Preparing VM image and flavor for parser... =========|"
328 download_parser_image
329 register_parser_image_and_flavor
332 echo "|========= 2/4. Creating test user for parser... =========|"
333 create_parser_user_and_project
336 echo "|========= 3/4. Parse -> translate -> deploy vRNC... =========|"
337 translator_and_deploy_vRNC
340 echo "|========= 4/4. Test ok... =========|"