Merge "update verigraph"
[parser.git] / tests / functest_run.sh
1 #!/bin/bash -e
2 ##############################################################################
3 # Copyright (c) 2016 ZTE Corporation.
4 #
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 ##############################################################################
10
11 PARSER_CI_DEBUG=${CI_DEBUG:-false}
12 PRASER_WORK_DIR=$(cd $(dirname $0) && pwd)
13
14 [[ "${PARSER_CI_DEBUG}" == "true" ]] && {
15     set -x
16     debug="--debug"
17 } || {
18     set +x
19     debug=""
20 }
21 LOCAL_IMAGE_FILE=${1:-""}
22
23 if [ -e "${LOCAL_IMAGE_FILE}" ]; then
24     echo "Input local image file: ${LOCAL_IMAGE_FILE}"
25     PARSER_IMAGE_URL_FILE=${LOCAL_IMAGE_FILE}
26 else
27     echo "No local image file or the file(${LOCAL_IMAGE_FILE}) doesn't exsit!"
28
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}."
34 fi
35
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
41
42 PARSER_USER=parser
43 PARSER_PASSWORD=parser
44 PARSER_EMAIL='shang.xiaodong@zte.com.cn'
45 PARSER_PROJECT=parser
46 PARSER_TENANT=${PARSER_PROJECT}
47
48 PARSER_ROLE=admin
49
50 PARSER_STACK_NAME=vRNC_Stack
51
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
56
57 VRNC_INPUT_TEMPLATE_FILE=${VRNC_INPUT_TEMPLATE_RAW_FILE%.*}_patch.yaml
58
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}
63         return 0
64     }
65
66     echo ""
67     echo "  Download image ${PARSER_IMAGE_URL_FILE}..."
68     wget ${PARSER_IMAGE_URL} -o ${PARSER_IMAGE_FILE}
69 }
70
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."
74         return 0
75     }
76
77     echo ""
78     echo "  Registe image ${PARSER_IMAGE_NAME}..."
79     openstack ${debug} image create "${PARSER_IMAGE_NAME}" \
80                            --public \
81                            --disk-format ${PARSER_IMAGE_FORMAT} \
82                            --container-format bare \
83                            --file ${PARSER_IMAGE_FILE}
84 }
85
86 create_parser_user_and_project() {
87
88     # 1. create parser project
89     openstack ${debug} project list | grep -qwo "${PARSER_PROJECT}" && {
90         echo "  Project ${PARSER_PROJECT} exist, doesn't create agian."
91     } || {
92         openstack  ${debug} project create ${PARSER_PROJECT} \
93             --description "Project for parser test"
94         echo "  Create project ${PARSER_PROJECT} successful."
95     }
96
97     # 2. create parser user.
98     openstack ${debug} user list | grep -qwo ${PARSER_USER} && {
99         echo "  User ${PARSER_USER} exist, doesn't create again."
100     } || {
101         openstack ${debug} user create ${PARSER_USER} --password ${PARSER_PASSWORD} \
102             --project ${PARSER_PROJECT} --email ${PARSER_EMAIL}
103         echo "  Create user ${PARSER_USER} successful."
104     }
105
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."
110     } || {
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."
114     }
115
116 }
117
118 change_env_to_parser_user_project() {
119
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}
124
125     export OS_PROJECT_DOMAIN_NAME=${OS_PROJECT_DOMAIN_NAME:-'Default'}
126     export OS_USER_DOMAIN_NAME=${OS_USER_DOMAIN_NAME:-'Default'}
127
128 }
129
130
131 make_patch_for_provider_network() {
132
133     # copy temp file
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}
138     echo ""
139
140     # Delete the provider:network_type
141     echo "      Patching provider:network_type..."
142     sed -i '/network_type:/d' ${VRNC_INPUT_TEMPLATE_FILE}
143
144     # Delete the provider:segmentation_id
145     echo "      Patching provider:segmentation_id..."
146     sed -i '/segmentation_id:/d' ${VRNC_INPUT_TEMPLATE_FILE}
147
148     # Delete the provider:physical_network
149     echo "      Patching provider:physical_network..."
150     sed -i '/physical_network:/d' ${VRNC_INPUT_TEMPLATE_FILE}
151
152     echo ""
153
154 }
155
156 make_patch_for_translated_file() {
157
158     # Replace the signal_transport
159     echo "    Patch yaml file: ${VRNC_OUTPUT_TEMPLATE_FILE}"
160     echo ""
161     echo "    Patching compute:signal_transport"
162     sed -i '1,$s/HEAT_SIGNAL/NO_SIGNAL/g' ${VRNC_OUTPUT_TEMPLATE_FILE}
163
164 }
165
166
167 translator_and_deploy_vRNC() {
168
169     (
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}
174         }
175         # 2. Switch env to parser project temporally
176         echo "  Switch openstack env to parser project"
177         change_env_to_parser_user_project
178         echo ""
179
180         # 3. Patch provider network
181         echo "  Make patch for provider network"
182         make_patch_for_provider_network
183
184         # 4. Translator yaml
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}
190         echo ""
191
192         # 5. Patch translated file
193         echo "  Make patch for translated file:"
194         make_patch_for_translated_file
195         echo ""
196
197         # 6. deploy vRNC
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}
202
203         # 7. basic information.
204         echo "  The basic information of deployment..."
205         openstack ${debug} stack show ${PARSER_STACK_NAME}
206
207         # 8. deployed resources.
208         echo "  The resources of deployment..."
209         openstack ${debug} stack resource list ${PARSER_STACK_NAME}
210
211         # 9. deployed outputs.
212         echo "  The outputs of deployment..."
213         openstack ${debug} stack output list ${PARSER_STACK_NAME}
214
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."
219         } || {
220             echo "    Check the result of deployment unsuccessfully."
221         }
222     )
223
224 }
225
226 reset_parser_test() {
227
228     set +e
229
230     echo "  Clean-up the environment..."
231     # 1. Delete resource created by parser user
232     (
233         # 1). Switch env to parser project temporally
234         change_env_to_parser_user_project
235
236         # 2). Delete the stack ${PARSER_STACK_NAME}
237         echo ""
238         openstack ${debug} stack list | grep -qow ${PARSER_STACK_NAME} && {
239             echo "    Stack ${PARSER_STACK_NAME} has been created, delete it after test."
240             echo ""
241             openstack ${debug} stack delete --yes --wait ${PARSER_STACK_NAME}
242             echo ""
243         }
244
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}
249         }
250
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}
255         }
256
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}
261         }
262
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}
267         }
268
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}"
272
273         sleep 3
274     )
275
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}"
282     }
283
284     echo "    Delete user ${PARSER_USER}"
285     openstack ${debug} user list | grep -qow "${PARSER_USER}" && {
286         openstack user delete "${PARSER_USER}"
287     }
288
289     echo "    Delete project ${PARSER_PROJECT}"
290     openstack ${debug} project list | grep -qwo "${PARSER_PROJECT}" && {
291         openstack project delete "${PARSER_PROJECT}"
292     }
293
294     echo ""
295     echo "======================={ Parser functest end }=========================="
296     echo ""
297     echo ""
298
299 }
300
301 echo ""
302 echo ""
303 echo "======================={ Parser functest begin }=========================="
304 echo ""
305
306 trap reset_parser_test EXIT
307
308 # start syslog for loghander
309 which service > /dev/null  && service rsyslog restart || true
310
311 echo "|========= 1/4. Preparing VM image for parser...     =========|"
312 download_parser_image
313 register_parser_image
314
315 echo ""
316 echo "|========= 2/4. Creating test user for parser...     =========|"
317 create_parser_user_and_project
318
319 echo ""
320 echo "|========= 3/4. Parse -> translate -> deploy vRNC... =========|"
321 translator_and_deploy_vRNC
322
323 echo ""
324 echo "|========= 4/4. Test ok...                           =========|"
325 echo ""