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 echo " Grant user ${PARSER_USER} the role ${PARSER_ROLE} in project ${PARSER_PROJECT} successful."
118 change_env_to_parser_user_project() {
120 export OS_USERNAME=${PARSER_USER}
121 export OS_PASSWORD=${PARSER_PASSWORD}
122 export OS_PROJECT_NAME=${PARSER_PROJECT}
123 export OS_TENANT_NAME=${PARSER_TENANT}
125 export OS_PROJECT_DOMAIN_NAME=${OS_PROJECT_DOMAIN_NAME:-'Default'}
126 export OS_USER_DOMAIN_NAME=${OS_USER_DOMAIN_NAME:-'Default'}
131 make_patch_for_provider_network() {
134 echo " Patch provider network for input file:"
135 echo " Raw yaml file: ${VRNC_INPUT_TEMPLATE_RAW_FILE}"
136 echo " Patched yaml file: ${VRNC_INPUT_TEMPLATE_FILE}"
137 cp ${VRNC_INPUT_TEMPLATE_RAW_FILE} ${VRNC_INPUT_TEMPLATE_FILE}
140 # Delete the provider:network_type
141 echo " Patching provider:network_type..."
142 sed -i '/network_type:/d' ${VRNC_INPUT_TEMPLATE_FILE}
144 # Delete the provider:segmentation_id
145 echo " Patching provider:segmentation_id..."
146 sed -i '/segmentation_id:/d' ${VRNC_INPUT_TEMPLATE_FILE}
148 # Delete the provider:physical_network
149 echo " Patching provider:physical_network..."
150 sed -i '/physical_network:/d' ${VRNC_INPUT_TEMPLATE_FILE}
156 make_patch_for_translated_file() {
158 # Replace the signal_transport
159 echo " Patch yaml file: ${VRNC_OUTPUT_TEMPLATE_FILE}"
161 echo " Patching compute:signal_transport"
162 sed -i '1,$s/HEAT_SIGNAL/NO_SIGNAL/g' ${VRNC_OUTPUT_TEMPLATE_FILE}
167 translator_and_deploy_vRNC() {
170 # 1. Delete parser stack ${PARSER_STACK_NAME}, use admin user in admin project
171 openstack ${debug} stack list | grep -qow ${PARSER_STACK_NAME} && {
172 echo " Stack ${PARSER_STACK_NAME} exist, delete it first."
173 openstack stack delete --yes --wait ${PARSER_STACK_NAME}
175 # 2. Switch env to parser project temporally
176 echo " Switch openstack env to parser project"
177 change_env_to_parser_user_project
180 # 3. Patch provider network
181 echo " Make patch for provider network"
182 make_patch_for_provider_network
185 echo " Translator use parser:"
186 echo " 1. Input file: ${VRNC_INPUT_TEMPLATE_FILE}"
187 echo " 2. Output file: ${VRNC_OUTPUT_TEMPLATE_FILE}"
188 heat-translator --template-type tosca --template-file ${VRNC_INPUT_TEMPLATE_FILE} \
189 --output-file ${VRNC_OUTPUT_TEMPLATE_FILE}
192 # 5. Patch translated file
193 echo " Make patch for translated file:"
194 make_patch_for_translated_file
198 echo " Deploy stack..."
199 [[ "${PARSER_CI_DEBUG}" == "true" ]] && debug="--debug" || debug=""
200 openstack ${debug} stack create --timeout 15 --wait --enable-rollback \
201 -t ${VRNC_OUTPUT_TEMPLATE_FILE} ${PARSER_STACK_NAME}
203 # 7. basic information.
204 echo " The basic information of deployment..."
205 openstack ${debug} stack show ${PARSER_STACK_NAME}
207 # 8. deployed resources.
208 echo " The resources of deployment..."
209 openstack ${debug} stack resource list ${PARSER_STACK_NAME}
211 # 9. deployed outputs.
212 echo " The outputs of deployment..."
213 openstack ${debug} stack output list ${PARSER_STACK_NAME}
215 # 10. Validate the deploy result.
216 echo " Checking the result of deployment..."
217 openstack ${debug} stack show ${PARSER_STACK_NAME} | grep -qow "CREATE_COMPLETE" && {
218 echo " Check the result of deployment successfully."
220 echo " Check the result of deployment unsuccessfully."
226 reset_parser_test() {
230 echo " Clean-up the environment..."
231 # 1. Delete resource created by parser user
233 # 1). Switch env to parser project temporally
234 change_env_to_parser_user_project
236 # 2). Delete the stack ${PARSER_STACK_NAME}
238 openstack ${debug} stack list | grep -qow ${PARSER_STACK_NAME} && {
239 echo " Stack ${PARSER_STACK_NAME} has been created, delete it after test."
241 openstack ${debug} stack delete --yes --wait ${PARSER_STACK_NAME}
245 # 3). Delete patch tmp file ${VRNC_INPUT_TEMPLATE_FILE}
246 [ -e ${VRNC_OUTPUT_TEMPLATE_FILE} -a ${PARSER_CI_DEBUG} != "true" ] && {
247 echo " Delete patch temp file ${VRNC_INPUT_TEMPLATE_FILE} after test."
248 rm -fr ${VRNC_INPUT_TEMPLATE_FILE}
251 # 4). Delete hot tmp file ${VRNC_OUTPUT_TEMPLATE_FILE}
252 [ -e ${VRNC_OUTPUT_TEMPLATE_FILE} -a ${PARSER_CI_DEBUG} != "true" ] && {
253 echo " Delete hot temp file ${VRNC_OUTPUT_TEMPLATE_FILE} after test."
254 rm -fr ${VRNC_OUTPUT_TEMPLATE_FILE}
257 # 5). Delete tmp image ${PARSER_IMAGE_FILE}
258 [[ -e ${PARSER_IMAGE_FILE} ]] && {
259 echo " Delete local image file ${PARSER_IMAGE_FILE} after test."
260 rm -fr ${PARSER_IMAGE_FILE}
263 # 6). Delete tmp image ${PARSER_IMAGE_URL_FILE}
264 [ -e ${PARSER_IMAGE_URL_FILE} -a ${PARSER_CI_DEBUG} != "true" ] && {
265 echo " Delete local URL image file ${PARSER_IMAGE_URL_FILE} after test."
266 rm -fr ${PARSER_IMAGE_URL_FILE}
269 # 7). Delete image from openstack
270 parser_image_id=$(openstack ${debug} image list | grep -w "${PARSER_IMAGE_NAME}" | awk '{print $2}')
271 [[ -n "${parser_image_id}" ]] && openstack image delete "${parser_image_id}"
276 # 2. Delete role, user and project
277 echo " Delete user ${PARSER_USER}'s role from project ${PARSER_PROJECT}"
278 openstack ${debug} user role list "${PARSER_USER}" --project "${PARSER_PROJECT}" \
279 | grep -qow "${PARSER_ROLE}" && {
280 openstack ${debug} role remove "${PARSER_ROLE}" --user "${PARSER_USER}" \
281 --project "${PARSER_PROJECT}"
284 echo " Delete user ${PARSER_USER}"
285 openstack ${debug} user list | grep -qow "${PARSER_USER}" && {
286 openstack user delete "${PARSER_USER}"
289 echo " Delete project ${PARSER_PROJECT}"
290 openstack ${debug} project list | grep -qwo "${PARSER_PROJECT}" && {
291 openstack project delete "${PARSER_PROJECT}"
295 echo "======================={ Parser functest end }=========================="
303 echo "======================={ Parser functest begin }=========================="
306 trap reset_parser_test EXIT
308 # start syslog for loghander
309 which service > /dev/null && service rsyslog restart || true
311 echo "|========= 1/4. Preparing VM image for parser... =========|"
312 download_parser_image
313 register_parser_image
316 echo "|========= 2/4. Creating test user for parser... =========|"
317 create_parser_user_and_project
320 echo "|========= 3/4. Parse -> translate -> deploy vRNC... =========|"
321 translator_and_deploy_vRNC
324 echo "|========= 4/4. Test ok... =========|"