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:-""}
24 echo "Prepare parser ..."
25 echo "==================="
27 ${PRASER_WORK_DIR}/parser_install.sh ${PRASER_WORK_DIR}/../..
29 echo "Prepare result: $?"
30 echo "==================="
32 if [ -e "${LOCAL_IMAGE_FILE}" ]; then
33 echo "Input local image file: ${LOCAL_IMAGE_FILE}"
34 PARSER_IMAGE_URL_FILE=${LOCAL_IMAGE_FILE}
36 echo "No local image file or the file(${LOCAL_IMAGE_FILE}) doesn't exsit!"
38 PARSER_IMAGE_URL_FILE=cirros-0.3.5-x86_64-disk.img
39 PARSER_IMAGE_VERSION=$(echo ${PARSER_IMAGE_URL_FILE} | awk -F- '{print $2}')
40 # PARSER_IMAGE_URL=https://launchpad.net/cirros/trunk/0.3.0/+download/${PARSER_IMAGE_URL_FILE}
41 PARSER_IMAGE_URL=http://download.cirros-cloud.net/${PARSER_IMAGE_VERSION}/${PARSER_IMAGE_URL_FILE}
42 echo "so will download image(${PARSER_IMAGE_URL_FILE}) from ${PARSER_IMAGE_URL}."
45 # PARSER_IMAGE_NAME=rhel-6.5-test-image
46 # fiexd image name according to the translator default vlaue of images
47 PARSER_IMAGE_NAME=cirros-0.3.2-x86_64-uec
48 PARSER_IMAGE_FILE="${PARSER_IMAGE_NAME}.img"
49 PARSER_IMAGE_FORMAT=qcow2
52 PARSER_PASSWORD=parser
53 PARSER_EMAIL='shang.xiaodong@zte.com.cn'
55 PARSER_TENANT=${PARSER_PROJECT}
59 PARSER_STACK_NAME=vRNC_Stack
61 # VRNC_INPUT_TEMPLATE_FILE=../tosca2heat/tosca-parser/toscaparser/extensions/nfv/tests/data/vRNC/Definitions/vRNC.yaml
62 # VRNC_INPUT_TEMPLATE_RAW_FILE=../tosca2heat/heat-translator/translator/tests/data/test_tosca_nfv_sample.yaml
63 VRNC_INPUT_TEMPLATE_RAW_FILE=../tosca2heat/heat-translator/translator/tests/data/vRNC/Definitions/vRNC.yaml
64 VRNC_OUTPUT_TEMPLATE_FILE=../tosca2heat/heat-translator/translator/tests/data/vRNC/vRNC_Hot_Template.yaml
66 VRNC_INPUT_TEMPLATE_FILE=${VRNC_INPUT_TEMPLATE_RAW_FILE%.*}_patch.yaml
68 download_parser_image() {
69 [ -e "${PARSER_IMAGE_URL_FILE}" ] && {
70 echo " Image ${PARSER_IMAGE_URL_FILE} has bee cached, needn't download again."
71 cp ${PARSER_IMAGE_URL_FILE} ${PARSER_IMAGE_FILE}
76 echo " Download image ${PARSER_IMAGE_URL_FILE}..."
77 wget ${PARSER_IMAGE_URL} -o ${PARSER_IMAGE_FILE}
80 register_parser_image() {
81 openstack ${debug} image list | grep -qwo "${PARSER_IMAGE_NAME}" && {
82 echo " Image ${PARSER_IMAGE_NAME} has bee registed, needn't registe again."
87 echo " Registe image ${PARSER_IMAGE_NAME}..."
88 openstack ${debug} image create "${PARSER_IMAGE_NAME}" \
90 --disk-format ${PARSER_IMAGE_FORMAT} \
91 --container-format bare \
92 --file ${PARSER_IMAGE_FILE}
95 create_parser_user_and_project() {
97 # 1. create parser project
98 openstack ${debug} project list | grep -qwo "${PARSER_PROJECT}" && {
99 echo " Project ${PARSER_PROJECT} exist, doesn't create agian."
101 openstack ${debug} project create ${PARSER_PROJECT} \
102 --description "Project for parser test"
103 echo " Create project ${PARSER_PROJECT} successful."
106 # 2. create parser user.
107 openstack ${debug} user list | grep -qwo ${PARSER_USER} && {
108 echo " User ${PARSER_USER} exist, doesn't create again."
110 openstack ${debug} user create ${PARSER_USER} --password ${PARSER_PASSWORD} \
111 --project ${PARSER_PROJECT} --email ${PARSER_EMAIL}
112 echo " Create user ${PARSER_USER} successful."
115 # 3. grant role for parser user
116 openstack ${debug} user role list ${PARSER_USER} --project ${PARSER_PROJECT} \
117 | grep -qow ${PARSER_ROLE} && {
118 echo " User ${PARSER_USER} has role ${PARSER_ROLE} in project ${PARSER_PROJECT}, doesn't create."
120 openstack ${debug} role add ${PARSER_ROLE} --user ${PARSER_USER} \
121 --project ${PARSER_PROJECT}
122 echo " Grant user ${PARSER_USER} the role ${PARSER_ROLE} in project ${PARSER_PROJECT} successful."
127 change_env_to_parser_user_project() {
129 export OS_USERNAME=${PARSER_USER}
130 export OS_PASSWORD=${PARSER_PASSWORD}
131 export OS_PROJECT_NAME=${PARSER_PROJECT}
132 export OS_TENANT_NAME=${PARSER_TENANT}
134 export OS_PROJECT_DOMAIN_NAME=${OS_PROJECT_DOMAIN_NAME:-'Default'}
135 export OS_USER_DOMAIN_NAME=${OS_USER_DOMAIN_NAME:-'Default'}
140 make_patch_for_provider_network() {
143 echo " Patch provider network for input file:"
144 echo " Raw yaml file: ${VRNC_INPUT_TEMPLATE_RAW_FILE}"
145 echo " Patched yaml file: ${VRNC_INPUT_TEMPLATE_FILE}"
146 cp ${VRNC_INPUT_TEMPLATE_RAW_FILE} ${VRNC_INPUT_TEMPLATE_FILE}
149 # Delete the provider:network_type
150 echo " Patching provider:network_type..."
151 sed -i '/network_type:/d' ${VRNC_INPUT_TEMPLATE_FILE}
153 # Delete the provider:segmentation_id
154 echo " Patching provider:segmentation_id..."
155 sed -i '/segmentation_id:/d' ${VRNC_INPUT_TEMPLATE_FILE}
157 # Delete the provider:physical_network
158 echo " Patching provider:physical_network..."
159 sed -i '/physical_network:/d' ${VRNC_INPUT_TEMPLATE_FILE}
165 make_patch_for_translated_file() {
167 # Replace the signal_transport
168 echo " Patch yaml file: ${VRNC_OUTPUT_TEMPLATE_FILE}"
170 echo " Patching compute:signal_transport"
171 sed -i '1,$s/HEAT_SIGNAL/NO_SIGNAL/g' ${VRNC_OUTPUT_TEMPLATE_FILE}
176 translator_and_deploy_vRNC() {
179 # 1. Delete parser stack ${PARSER_STACK_NAME}, use admin user in admin project
180 openstack ${debug} stack list | grep -qow ${PARSER_STACK_NAME} && {
181 echo " Stack ${PARSER_STACK_NAME} exist, delete it first."
182 openstack stack delete --yes --wait ${PARSER_STACK_NAME}
184 # 2. Switch env to parser project temporally
185 echo " Switch openstack env to parser project"
186 change_env_to_parser_user_project
189 # 3. Patch provider network
190 echo " Make patch for provider network"
191 make_patch_for_provider_network
194 echo " Translator use parser:"
195 echo " 1. Input file: ${VRNC_INPUT_TEMPLATE_FILE}"
196 echo " 2. Output file: ${VRNC_OUTPUT_TEMPLATE_FILE}"
197 heat-translator --template-type tosca --template-file ${VRNC_INPUT_TEMPLATE_FILE} \
198 --output-file ${VRNC_OUTPUT_TEMPLATE_FILE}
201 # 5. Patch translated file
202 echo " Make patch for translated file:"
203 make_patch_for_translated_file
207 echo " Deploy stack..."
208 [[ "${PARSER_CI_DEBUG}" == "true" ]] && debug="--debug" || debug=""
209 openstack ${debug} stack create --timeout 15 --wait --enable-rollback \
210 -t ${VRNC_OUTPUT_TEMPLATE_FILE} ${PARSER_STACK_NAME}
212 # 7. basic information.
213 echo " The basic information of deployment..."
214 openstack ${debug} stack show ${PARSER_STACK_NAME}
216 # 8. deployed resources.
217 echo " The resources of deployment..."
218 openstack ${debug} stack resource list ${PARSER_STACK_NAME}
220 # 9. deployed outputs.
221 echo " The outputs of deployment..."
222 openstack ${debug} stack output list ${PARSER_STACK_NAME}
224 # 10. Validate the deploy result.
225 echo " Checking the result of deployment..."
226 openstack ${debug} stack show ${PARSER_STACK_NAME} | grep -qow "CREATE_COMPLETE" && {
227 echo " Check the result of deployment successfully."
229 echo " Check the result of deployment unsuccessfully."
235 reset_parser_test() {
239 echo " Clean-up the environment..."
240 # 1. Delete resource created by parser user
242 # 1). Switch env to parser project temporally
243 change_env_to_parser_user_project
245 # 2). Delete the stack ${PARSER_STACK_NAME}
247 openstack ${debug} stack list | grep -qow ${PARSER_STACK_NAME} && {
248 echo " Stack ${PARSER_STACK_NAME} has been created, delete it after test."
250 openstack ${debug} stack delete --yes --wait ${PARSER_STACK_NAME}
254 # 3). Delete patch tmp file ${VRNC_INPUT_TEMPLATE_FILE}
255 [ -e ${VRNC_OUTPUT_TEMPLATE_FILE} -a ${PARSER_CI_DEBUG} != "true" ] && {
256 echo " Delete patch temp file ${VRNC_INPUT_TEMPLATE_FILE} after test."
257 rm -fr ${VRNC_INPUT_TEMPLATE_FILE}
260 # 4). Delete hot tmp file ${VRNC_OUTPUT_TEMPLATE_FILE}
261 [ -e ${VRNC_OUTPUT_TEMPLATE_FILE} -a ${PARSER_CI_DEBUG} != "true" ] && {
262 echo " Delete hot temp file ${VRNC_OUTPUT_TEMPLATE_FILE} after test."
263 rm -fr ${VRNC_OUTPUT_TEMPLATE_FILE}
266 # 5). Delete tmp image ${PARSER_IMAGE_FILE}
267 [[ -e ${PARSER_IMAGE_FILE} ]] && {
268 echo " Delete local image file ${PARSER_IMAGE_FILE} after test."
269 rm -fr ${PARSER_IMAGE_FILE}
272 # 6). Delete tmp image ${PARSER_IMAGE_URL_FILE}
273 [ -e ${PARSER_IMAGE_URL_FILE} -a ${PARSER_CI_DEBUG} != "true" ] && {
274 echo " Delete local URL image file ${PARSER_IMAGE_URL_FILE} after test."
275 rm -fr ${PARSER_IMAGE_URL_FILE}
278 # 7). Delete image from openstack
279 parser_image_id=$(openstack ${debug} image list | grep -w "${PARSER_IMAGE_NAME}" | awk '{print $2}')
280 [[ -n "${parser_image_id}" ]] && openstack image delete "${parser_image_id}"
285 # 2. Delete role, user and project
286 echo " Delete user ${PARSER_USER}'s role from project ${PARSER_PROJECT}"
287 openstack ${debug} user role list "${PARSER_USER}" --project "${PARSER_PROJECT}" \
288 | grep -qow "${PARSER_ROLE}" && {
289 openstack ${debug} role remove "${PARSER_ROLE}" --user "${PARSER_USER}" \
290 --project "${PARSER_PROJECT}"
293 echo " Delete user ${PARSER_USER}"
294 openstack ${debug} user list | grep -qow "${PARSER_USER}" && {
295 openstack user delete "${PARSER_USER}"
298 echo " Delete project ${PARSER_PROJECT}"
299 openstack ${debug} project list | grep -qwo "${PARSER_PROJECT}" && {
300 openstack project delete "${PARSER_PROJECT}"
304 echo "======================={ Parser functest end }=========================="
312 echo "======================={ Parser functest begin }=========================="
315 trap reset_parser_test EXIT
317 # start syslog for loghander
318 service rsyslog restart
320 echo "|========= 1/4. Preparing VM image for parser... =========|"
321 download_parser_image
322 register_parser_image
325 echo "|========= 2/4. Creating test user for parser... =========|"
326 create_parser_user_and_project
329 echo "|========= 3/4. Parse -> translate -> deploy vRNC... =========|"
330 translator_and_deploy_vRNC
333 echo "|========= 4/4. Test ok... =========|"