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 [[ "${CI_DEBUG:-true}" == "true" ]] && set -x
13 PARSER_IMAGE_URL_FILE=cirros-0.3.0-x86_64-disk.img
14 PARSER_IMAGE_URL=https://launchpad.net/cirros/trunk/0.3.0/+download/${PARSER_IMAGE_URL_FILE}
15 PARSER_IMAGE_NAME=parser_image
16 PARSER_IMAGE_FILE="${PARSER_IMAGE_NAME}.img"
17 PARSER_IMAGE_FORMAT=qcow2
19 PARSER_VM_FLAVOR=m1.tiny
22 PARSER_PASSWORD=parser
24 PARSER_TENANT=${PARSER_PROJECT}
28 PARSER_STACK_NAME=vRNC_Stack
30 # VRNC_INPUT_TEMPLATE_FILE=../tosca2heat/tosca-parser/toscaparser/extensions/nfv/tests/data/vRNC/Definitions/vRNC.yaml
31 VRNC_INPUT_TEMPLATE_FILE=../tosca2heat/heat-translator/translator/tests/data/test_tosca_nfv_sample.yaml
33 VRNC_OUTPUT_TEMPLATE_FILE=./vRNC_Hot_Template.yaml
35 download_parser_image() {
36 [ -e "${PARSER_IMAGE_URL_FILE}" ] && {
37 echo "Image ${PARSER_IMAGE_URL_FILE} has bee cached, needn't download again."
38 cp ${PARSER_IMAGE_URL_FILE} ${PARSER_IMAGE_FILE}
42 echo "Download image ${PARSER_IMAGE_URL_FILE}..."
43 wget "${PARSER_IMAGE_URL}" -o "${PARSER_IMAGE_FILE}"
46 register_parser_image() {
47 openstack image list | grep -qwo "${PARSER_IMAGE_NAME}" && {
48 echo "Image ${PARSER_IMAGE_NAME} has bee registed, needn't registe again."
52 echo "Registe image ${PARSER_IMAGE_NAME}..."
53 openstack image create "${PARSER_IMAGE_NAME}" \
55 --disk-format "${PARSER_IMAGE_FORMAT}" \
56 --container-format bare \
57 --file "${PARSER_IMAGE_FILE}"
60 create_parser_user_and_project() {
62 # 1. create parser user.
63 openstack user list | grep -qwo "${PARSER_USER}" && {
64 echo "User ${PARSER_USER} exist, doesn't create again."
66 openstack user create "${PARSER_USER}" --password "${PARSER_PASSWORD}"
67 echo "Create user ${PARSER_USER} successful."
70 # 2. create parser project
71 openstack project list | grep -qwo "${PARSER_PROJECT}" && {
72 echo "Project ${PARSER_PROJECT} exist, doesn't create agian."
74 openstack project create "${PARSER_PROJECT}"
75 echo "Create project ${PARSER_PROJECT} successful."
78 # 3. grant role for parser user
79 openstack user role list "${PARSER_USER}" --project "${PARSER_PROJECT}" \
80 | grep -qow " ${PARSER_ROLE}" && {
81 echo "User ${PARSER_USER} has role ${PARSER_ROLE} in project ${PARSER_PROJECT}, doesn't create."
83 openstack role add "${PARSER_ROLE}" --user "${PARSER_USER}" \
84 --project "${PARSER_PROJECT}"
85 echo "Grant user ${PARSER_USER} the role ${PARSER_ROLE} in project ${PARSER_PROJECT} successful."
90 change_env_to_parser_user_project() {
92 export OS_USERNAME="$PARSER_USER"
93 export OS_PASSWORD="$PARSER_PASSWORD"
94 export OS_PROJECT_NAME="$PARSER_PROJECT"
95 export OS_TENANT_NAME="$PARSER_TENANT"
99 translator_and_deploy_vRNC() {
101 # 1. Delete parser stack ${PARSER_STACK_NAME}, use admin user in admin project
102 openstack stack list | grep -qow ${PARSER_STACK_NAME} && {
103 echo "stack ${PARSER_STACK_NAME} exist, delete it first."
104 openstack stack delete ${PARSER_STACK_NAME}
106 # 2. Switch env to parser project temporally
107 change_env_to_parser_user_project
109 # 3. Translator and deploy vRNC
110 heat-translator --template-type tosca --template-file ${VRNC_INPUT_TEMPLATE_FILE} -o ${VRNC_OUTPUT_TEMPLATE_FILE} --deploy True
112 # 4. Wait for create vRNC
115 # 5. Validate the deploy result.
121 reset_parser_test() {
128 # 1. Switch env to parser project temporally
129 change_env_to_parser_user_project
131 # 2. Delete the stack ${PARSER_STACK_NAME}
132 openstack stack list | grep -qow ${PARSER_STACK_NAME} && {
133 echo "stack ${PARSER_STACK_NAME} has been created, delete it after test."
134 openstack stack delete ${PARSER_STACK_NAME}
137 # 3. Delete hot tmp file ${VRNC_OUTPUT_TEMPLATE_FILE}
138 [[ -e ${VRNC_OUTPUT_TEMPLATE_FILE} ]] && {
139 echo "delete hot temp file ${VRNC_OUTPUT_TEMPLATE_FILE} after test."
140 rm -fr ${VRNC_OUTPUT_TEMPLATE_FILE}
143 # 4. Delete tmp image ${PARSER_IMAGE_FILE}
144 [[ -e ${PARSER_IMAGE_FILE} ]] && {
145 echo "delete local image file ${PARSER_IMAGE_FILE} after test."
146 rm -fr ${PARSER_IMAGE_FILE}
149 # 5. Delete tmp image ${PARSER_IMAGE_URL_FILE}
150 [[ -e ${PARSER_IMAGE_URL_FILE} ]] && {
151 echo "delete local image file ${PARSER_IMAGE_URL_FILE} after test."
152 rm -fr ${PARSER_IMAGE_URL_FILE}
158 # 3. Delete parser user and project
159 parser_image_id=$(openstack image list | grep -ow "${PARSER_IMAGE_NAME}" | awk '{print $2}')
161 [ -n "${parser_image_id}" ] && openstack image delete "${parser_image_id}"
162 openstack role remove "${PARSER_ROLE}" --user "${PARSER_USER}" \
163 --project "${PARSER_PROJECT}"
164 openstack project delete "${PARSER_PROJECT}"
165 openstack user delete "${PARSER_USER}"
167 if [[ ret != "test_ok" ]]; then
168 echo " ========= 4/4. test error, check your env or code. ========= "
169 echo "======================= Parser functest end =========================="
175 echo "======================= Parser functest begin =========================="
177 trap reset_parser_test EXIT
179 echo " ========= 1/4. Preparing VM image for parser... ========= "
180 download_parser_image
181 register_parser_image
183 echo " ========= 2/4. Creating test user for parser... ========= "
184 create_parser_user_and_project
186 echo " ========= 3/4. Parse -> translate -> deploy vRNC... ========= "
187 translator_and_deploy_vRNC
189 echo " ========= 4/4. Test ok, clear the test evn... ========= "
190 reset_parser_test "test_ok"
192 echo "======================= Parser functest end =========================="