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_RAW_FILE=../tosca2heat/heat-translator/translator/tests/data/test_tosca_nfv_sample.yaml
40 VRNC_OUTPUT_TEMPLATE_FILE=./vRNC_Hot_Template.yaml
42 VRNC_INPUT_TEMPLATE_FILE=${VRNC_INPUT_TEMPLATE_RAW_FILE%.*}_patch.yaml
44 download_parser_image() {
45 [ -e "${PARSER_IMAGE_URL_FILE}" ] && {
46 echo " Image ${PARSER_IMAGE_URL_FILE} has bee cached, needn't download again."
47 cp ${PARSER_IMAGE_URL_FILE} ${PARSER_IMAGE_FILE}
52 echo " Download image ${PARSER_IMAGE_URL_FILE}..."
53 wget ${PARSER_IMAGE_URL} -o ${PARSER_IMAGE_FILE}
56 register_parser_image() {
57 openstack ${debug} image list | grep -qwo "${PARSER_IMAGE_NAME}" && {
58 echo " Image ${PARSER_IMAGE_NAME} has bee registed, needn't registe again."
63 echo " Registe image ${PARSER_IMAGE_NAME}..."
64 openstack ${debug} image create "${PARSER_IMAGE_NAME}" \
66 --disk-format ${PARSER_IMAGE_FORMAT} \
67 --container-format bare \
68 --file ${PARSER_IMAGE_FILE}
71 create_parser_user_and_project() {
73 # 1. create parser project
74 openstack ${debug} project list | grep -qwo "${PARSER_PROJECT}" && {
75 echo " Project ${PARSER_PROJECT} exist, doesn't create agian."
77 openstack ${debug} project create ${PARSER_PROJECT} \
78 --description "Project for parser test"
79 echo " Create project ${PARSER_PROJECT} successful."
82 # 2. create parser user.
83 openstack ${debug} user list | grep -qwo ${PARSER_USER} && {
84 echo " User ${PARSER_USER} exist, doesn't create again."
86 openstack ${debug} user create ${PARSER_USER} --password ${PARSER_PASSWORD} \
87 --project ${PARSER_PROJECT} --email ${PARSER_EMAIL}
88 echo " Create user ${PARSER_USER} successful."
91 # 3. grant role for parser user
92 openstack ${debug} user role list ${PARSER_USER} --project ${PARSER_PROJECT} \
93 | grep -qow ${PARSER_ROLE} && {
94 echo " User ${PARSER_USER} has role ${PARSER_ROLE} in project ${PARSER_PROJECT}, doesn't create."
96 openstack ${debug} role add ${PARSER_ROLE} --user ${PARSER_USER} \
97 --project ${PARSER_PROJECT}
98 echo " Grant user ${PARSER_USER} the role ${PARSER_ROLE} in project ${PARSER_PROJECT} successful."
103 change_env_to_parser_user_project() {
105 export OS_USERNAME=${PARSER_USER}
106 export OS_PASSWORD=${PARSER_PASSWORD}
107 export OS_PROJECT_NAME=${PARSER_PROJECT}
108 export OS_TENANT_NAME=${PARSER_TENANT}
113 make_patch_for_provider_network() {
116 echo " Patch provider network for input file:"
117 echo " Raw yaml file: ${VRNC_INPUT_TEMPLATE_RAW_FILE}"
118 echo " Patched yaml file: ${VRNC_INPUT_TEMPLATE_FILE}"
119 cp ${VRNC_INPUT_TEMPLATE_RAW_FILE} ${VRNC_INPUT_TEMPLATE_FILE}
122 # Delete the provider:network_type
123 echo " Patch provider:network_type..."
124 sed -i '/network_type:/d' ${VRNC_INPUT_TEMPLATE_FILE}
126 # Delete the provider:segmentation_id
127 echo " Patch provider:segmentation_id..."
128 sed -i '/segmentation_id:/d' ${VRNC_INPUT_TEMPLATE_FILE}
130 # Delete the provider:physical_network
131 echo " Patch provider:physical_network..."
132 sed -i '/physical_network:/d' ${VRNC_INPUT_TEMPLATE_FILE}
139 translator_and_deploy_vRNC() {
142 # 1. Delete parser stack ${PARSER_STACK_NAME}, use admin user in admin project
143 openstack ${debug} stack list | grep -qow ${PARSER_STACK_NAME} && {
144 echo " Stack ${PARSER_STACK_NAME} exist, delete it first."
145 openstack stack delete --yes --wait ${PARSER_STACK_NAME}
147 # 2. Switch env to parser project temporally
148 echo " Switch openstack env to parser project"
149 change_env_to_parser_user_project
152 # 3. Patch provider network
153 echo " Make patch for provider network"
154 make_patch_for_provider_network
157 echo " Translator use parser:"
158 echo " 1. Input file: ${VRNC_INPUT_TEMPLATE_FILE}"
159 echo " 2. Output file: ${VRNC_OUTPUT_TEMPLATE_FILE}"
160 heat-translator --template-type tosca --template-file ${VRNC_INPUT_TEMPLATE_FILE} \
161 --output-file ${VRNC_OUTPUT_TEMPLATE_FILE}
165 echo " Deploy stack..."
166 [[ "${PARSER_CI_DEBUG}" == "true" ]] && debug="--debug" || debug=""
167 openstack ${debug} stack create --timeout 30 --wait --enable-rollback \
168 -t ${VRNC_OUTPUT_TEMPLATE_FILE} ${PARSER_STACK_NAME}
170 # 6. Validate the deploy result.
171 echo " Checking the result of deployment..."
172 openstack ${debug} stack show ${PARSER_STACK_NAME} | grep -qow "CREATE_COMPLETE" && {
173 echo " Check the result of deployment successfully."
175 echo " Check the result of deployment unsuccessfully."
181 reset_parser_test() {
185 echo " Clean-up the environment..."
186 # 1. Delete resource created by parser user
188 # 1). Switch env to parser project temporally
189 change_env_to_parser_user_project
191 # 2). Delete the stack ${PARSER_STACK_NAME}
193 openstack ${debug} stack list | grep -qow ${PARSER_STACK_NAME} && {
194 echo " Stack ${PARSER_STACK_NAME} has been created, delete it after test."
196 openstack ${debug} stack delete --yes --wait ${PARSER_STACK_NAME}
200 # 3). Delete patch tmp file ${VRNC_INPUT_TEMPLATE_FILE}
201 [ -e ${VRNC_OUTPUT_TEMPLATE_FILE} -a ${PARSER_CI_DEBUG} != "true" ] && {
202 echo " Delete patch temp file ${VRNC_INPUT_TEMPLATE_FILE} after test."
203 rm -fr ${VRNC_INPUT_TEMPLATE_FILE}
206 # 4). Delete hot tmp file ${VRNC_OUTPUT_TEMPLATE_FILE}
207 [ -e ${VRNC_OUTPUT_TEMPLATE_FILE} -a ${PARSER_CI_DEBUG} != "true" ] && {
208 echo " Delete hot temp file ${VRNC_OUTPUT_TEMPLATE_FILE} after test."
209 rm -fr ${VRNC_OUTPUT_TEMPLATE_FILE}
212 # 5). Delete tmp image ${PARSER_IMAGE_FILE}
213 [[ -e ${PARSER_IMAGE_FILE} ]] && {
214 echo " Delete local image file ${PARSER_IMAGE_FILE} after test."
215 rm -fr ${PARSER_IMAGE_FILE}
218 # 6). Delete tmp image ${PARSER_IMAGE_URL_FILE}
219 [ -e ${PARSER_IMAGE_URL_FILE} -a ${PARSER_CI_DEBUG} != "true" ] && {
220 echo " Delete local URL image file ${PARSER_IMAGE_URL_FILE} after test."
221 rm -fr ${PARSER_IMAGE_URL_FILE}
224 # 7). Delete image from openstack
225 parser_image_id=$(openstack ${debug} image list | grep -w "${PARSER_IMAGE_NAME}" | awk '{print $2}')
226 [[ -n "${parser_image_id}" ]] && openstack image delete "${parser_image_id}"
231 # 2. Delete role, user and project
232 echo " Delete user ${PARSER_USER}'s role from project ${PARSER_PROJECT}"
233 openstack ${debug} user role list "${PARSER_USER}" --project "${PARSER_PROJECT}" \
234 | grep -qow "${PARSER_ROLE}" && {
235 openstack ${debug} role remove "${PARSER_ROLE}" --user "${PARSER_USER}" \
236 --project "${PARSER_PROJECT}"
239 echo " Delete user ${PARSER_USER}"
240 openstack ${debug} user list | grep -qow "${PARSER_USER}" && {
241 openstack user delete "${PARSER_USER}"
244 echo " Delete project ${PARSER_PROJECT}"
245 openstack ${debug} project list | grep -qwo "${PARSER_PROJECT}" && {
246 openstack project delete "${PARSER_PROJECT}"
250 echo "======================= Parser functest end =========================="
258 echo "======================= Parser functest begin =========================="
261 trap reset_parser_test EXIT
263 # start syslog for loghander
264 service rsyslog restart
266 echo "|========= 1/4. Preparing VM image for parser... =========|"
267 download_parser_image
268 register_parser_image
271 echo "|========= 2/4. Creating test user for parser... =========|"
272 create_parser_user_and_project
275 echo "|========= 3/4. Parse -> translate -> deploy vRNC... =========|"
276 translator_and_deploy_vRNC
279 echo "|========= 4/4. Test ok... =========|"