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 [[ "${PARSER_CI_DEBUG}" == "true" ]] && {
20 PARSER_IMAGE_URL_FILE=cirros-0.3.0-x86_64-disk.img
21 PARSER_IMAGE_URL=https://launchpad.net/cirros/trunk/0.3.0/+download/${PARSER_IMAGE_URL_FILE}
22 PARSER_IMAGE_NAME=rhel-6.5-test-image
23 PARSER_IMAGE_FILE="${PARSER_IMAGE_NAME}.img"
24 PARSER_IMAGE_FORMAT=qcow2
26 PARSER_VM_FLAVOR=m1.tiny
29 PARSER_PASSWORD=parser
30 PARSER_EMAIL='shang.xiaodong@zte.com.cn'
32 PARSER_TENANT=${PARSER_PROJECT}
36 PARSER_STACK_NAME=vRNC_Stack
38 # VRNC_INPUT_TEMPLATE_FILE=../tosca2heat/tosca-parser/toscaparser/extensions/nfv/tests/data/vRNC/Definitions/vRNC.yaml
39 VRNC_INPUT_TEMPLATE_FILE=../tosca2heat/heat-translator/translator/tests/data/test_tosca_nfv_sample.yaml
41 VRNC_OUTPUT_TEMPLATE_FILE=./vRNC_Hot_Template.yaml
43 download_parser_image() {
44 [ -e "${PARSER_IMAGE_URL_FILE}" ] && {
45 echo " Image ${PARSER_IMAGE_URL_FILE} has bee cached, needn't download again."
46 cp ${PARSER_IMAGE_URL_FILE} ${PARSER_IMAGE_FILE}
51 echo " Download image ${PARSER_IMAGE_URL_FILE}..."
52 wget ${PARSER_IMAGE_URL} -o ${PARSER_IMAGE_FILE}
55 register_parser_image() {
56 openstack ${debug} image list | grep -qwo "${PARSER_IMAGE_NAME}" && {
57 echo " Image ${PARSER_IMAGE_NAME} has bee registed, needn't registe again."
62 echo " Registe image ${PARSER_IMAGE_NAME}..."
63 openstack ${debug} image create "${PARSER_IMAGE_NAME}" \
65 --disk-format ${PARSER_IMAGE_FORMAT} \
66 --container-format bare \
67 --file ${PARSER_IMAGE_FILE}
70 create_parser_user_and_project() {
72 # 1. create parser project
73 openstack ${debug} project list | grep -qwo "${PARSER_PROJECT}" && {
74 echo " Project ${PARSER_PROJECT} exist, doesn't create agian."
76 openstack ${debug} project create ${PARSER_PROJECT} \
77 --description "Project for parser test"
78 echo " Create project ${PARSER_PROJECT} successful."
81 # 2. create parser user.
82 openstack ${debug} user list | grep -qwo ${PARSER_USER} && {
83 echo " User ${PARSER_USER} exist, doesn't create again."
85 openstack ${debug} user create ${PARSER_USER} --password ${PARSER_PASSWORD} \
86 --project ${PARSER_PROJECT} --email ${PARSER_EMAIL}
87 echo " Create user ${PARSER_USER} successful."
90 # 3. grant role for parser user
91 openstack ${debug} user role list ${PARSER_USER} --project ${PARSER_PROJECT} \
92 | grep -qow ${PARSER_ROLE} && {
93 echo " User ${PARSER_USER} has role ${PARSER_ROLE} in project ${PARSER_PROJECT}, doesn't create."
95 openstack ${debug} role add ${PARSER_ROLE} --user ${PARSER_USER} \
96 --project ${PARSER_PROJECT}
97 echo " Grant user ${PARSER_USER} the role ${PARSER_ROLE} in project ${PARSER_PROJECT} successful."
102 change_env_to_parser_user_project() {
104 export OS_USERNAME=${PARSER_USER}
105 export OS_PASSWORD=${PARSER_PASSWORD}
106 export OS_PROJECT_NAME=${PARSER_PROJECT}
107 export OS_TENANT_NAME=${PARSER_TENANT}
111 translator_and_deploy_vRNC() {
114 # 1. Delete parser stack ${PARSER_STACK_NAME}, use admin user in admin project
115 openstack ${debug} stack list | grep -qow ${PARSER_STACK_NAME} && {
116 echo " Stack ${PARSER_STACK_NAME} exist, delete it first."
117 openstack stack delete --yes --wait ${PARSER_STACK_NAME}
119 # 2. Switch env to parser project temporally
120 echo " Switch openstack env to parser project"
121 change_env_to_parser_user_project
124 echo " Translator use parser:"
125 echo " 1. Input file: ${VRNC_INPUT_TEMPLATE_FILE}"
126 echo " 2. Output file: ${VRNC_OUTPUT_TEMPLATE_FILE}"
127 heat-translator --template-type tosca --template-file ${VRNC_INPUT_TEMPLATE_FILE} \
128 --output-file ${VRNC_OUTPUT_TEMPLATE_FILE}
131 echo " Deploy stack..."
132 [[ "${PARSER_CI_DEBUG}" == "true" ]] && debug="--debug" || debug=""
133 openstack ${debug} stack create --timeout 30 --wait --enable-rollback \
134 -t ${VRNC_OUTPUT_TEMPLATE_FILE} ${PARSER_STACK_NAME}
136 # 5. Validate the deploy result.
137 echo " Checking the result of deployment..."
138 openstack ${debug} stack show ${PARSER_STACK_NAME} | grep -qow "CREATE_COMPLETE" && {
139 echo " Check the result of deployment successfully."
141 echo " Check the result of deployment unsuccessfully."
147 reset_parser_test() {
151 echo " Clean-up the environment..."
152 # 1. Delete resource created by parser user
154 # 1). Switch env to parser project temporally
155 change_env_to_parser_user_project
157 # 2). Delete the stack ${PARSER_STACK_NAME}
158 openstack ${debug} stack list | grep -qow ${PARSER_STACK_NAME} && {
159 echo " Stack ${PARSER_STACK_NAME} has been created, delete it after test."
160 openstack ${debug} stack delete --yes --wait ${PARSER_STACK_NAME}
163 # 3). Delete hot tmp file ${VRNC_OUTPUT_TEMPLATE_FILE}
164 [ -e ${VRNC_OUTPUT_TEMPLATE_FILE} -a ${PARSER_CI_DEBUG} != "true" ] && {
165 echo " Delete hot temp file ${VRNC_OUTPUT_TEMPLATE_FILE} after test."
166 rm -fr ${VRNC_OUTPUT_TEMPLATE_FILE}
169 # 4). Delete tmp image ${PARSER_IMAGE_FILE}
170 [[ -e ${PARSER_IMAGE_FILE} ]] && {
171 echo " Delete local image file ${PARSER_IMAGE_FILE} after test."
172 rm -fr ${PARSER_IMAGE_FILE}
175 # 5). Delete tmp image ${PARSER_IMAGE_URL_FILE}
176 [ -e ${PARSER_IMAGE_URL_FILE} -a ${PARSER_CI_DEBUG} != "true" ] && {
177 echo " Delete local URL image file ${PARSER_IMAGE_URL_FILE} after test."
178 rm -fr ${PARSER_IMAGE_URL_FILE}
181 # 6). Delete image from openstack
182 parser_image_id=$(openstack ${debug} image list | grep -w "${PARSER_IMAGE_NAME}" | awk '{print $2}')
183 [[ -n "${parser_image_id}" ]] && openstack image delete "${parser_image_id}"
188 # 2. Delete role, user and project
189 echo " Delete user ${PARSER_USER}'s role from project ${PARSER_PROJECT}"
190 openstack ${debug} user role list "${PARSER_USER}" --project "${PARSER_PROJECT}" \
191 | grep -qow "${PARSER_ROLE}" && {
192 openstack ${debug} role remove "${PARSER_ROLE}" --user "${PARSER_USER}" \
193 --project "${PARSER_PROJECT}"
196 echo " Delete user ${PARSER_USER}"
197 openstack ${debug} user list | grep -qow "${PARSER_USER}" && {
198 openstack user delete "${PARSER_USER}"
201 echo " Delete project ${PARSER_PROJECT}"
202 openstack ${debug} project list | grep -qwo "${PARSER_PROJECT}" && {
203 openstack project delete "${PARSER_PROJECT}"
207 echo "======================= Parser functest end =========================="
215 echo "======================= Parser functest begin =========================="
218 trap reset_parser_test EXIT
220 # start syslog for loghander
221 service rsyslog restart
223 echo "|========= 1/4. Preparing VM image for parser... =========|"
224 download_parser_image
225 register_parser_image
228 echo "|========= 2/4. Creating test user for parser... =========|"
229 create_parser_user_and_project
232 echo "|========= 3/4. Parse -> translate -> deploy vRNC... =========|"
233 translator_and_deploy_vRNC
236 echo "|========= 4/4. Test ok... =========|"