Add default flavor
[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 VM_FLAVOR_NAME=m1.tiny
51 VM_FLAVOR_CPUS=1
52 VM_FLAVOR_RAM=512
53 VM_FLAVOR_DISK=1
54
55 PARSER_STACK_NAME=vRNC_Stack
56
57 # VRNC_INPUT_TEMPLATE_FILE=${PRASER_WORK_DIR}/../tosca2heat/tosca-parser/toscaparser/extensions/nfv/tests/data/vRNC/Definitions/vRNC.yaml
58 # VRNC_INPUT_TEMPLATE_RAW_FILE=${PRASER_WORK_DIR}/../tosca2heat/heat-translator/translator/tests/data/test_tosca_nfv_sample.yaml
59 VRNC_INPUT_TEMPLATE_RAW_FILE=${PRASER_WORK_DIR}/../tosca2heat/heat-translator/translator/tests/data/vRNC/Definitions/vRNC.yaml
60 VRNC_OUTPUT_TEMPLATE_FILE=${PRASER_WORK_DIR}/../tosca2heat/heat-translator/translator/tests/data/vRNC/vRNC_Hot_Template.yaml
61
62 VRNC_INPUT_TEMPLATE_FILE=${VRNC_INPUT_TEMPLATE_RAW_FILE%.*}_patch.yaml
63
64 download_parser_image() {
65     [ -e "${PARSER_IMAGE_URL_FILE}" ] && {
66         echo "  Image ${PARSER_IMAGE_URL_FILE} has bee cached, needn't download again."
67         cp ${PARSER_IMAGE_URL_FILE} ${PARSER_IMAGE_FILE}
68         return 0
69     }
70
71     echo ""
72     echo "  Download image ${PARSER_IMAGE_URL_FILE}..."
73     wget ${PARSER_IMAGE_URL} -o ${PARSER_IMAGE_FILE}
74 }
75
76 register_parser_image_and_flavor() {
77     openstack ${debug} image list | grep -qwo "${PARSER_IMAGE_NAME}" && {
78         echo "  Image ${PARSER_IMAGE_NAME} has bee registed, needn't registe again."
79         return 0
80     }
81
82     echo ""
83     echo "  Registe image ${PARSER_IMAGE_NAME}..."
84     openstack ${debug} image create "${PARSER_IMAGE_NAME}" \
85                            --public \
86                            --disk-format ${PARSER_IMAGE_FORMAT} \
87                            --container-format bare \
88                            --file ${PARSER_IMAGE_FILE}
89
90     [[ ! openstack flavor show ${VM_FLAVOR_NAME} ]] && {
91         echo "  Create default flavor ${VM_FLAVOR_NAME}..."
92         openstack flavor create --ram ${VM_FLAVOR_RAM} \
93             --vcpus ${VM_FLAVOR_CPUS} --disk ${VM_FLAVOR_DISK} ${VM_FLAVOR_NAM}
94     }
95 }
96
97 create_parser_user_and_project() {
98
99     # 1. create parser project
100     openstack ${debug} project list | grep -qwo "${PARSER_PROJECT}" && {
101         echo "  Project ${PARSER_PROJECT} exist, doesn't create agian."
102     } || {
103         openstack  ${debug} project create ${PARSER_PROJECT} \
104             --description "Project for parser test"
105         echo "  Create project ${PARSER_PROJECT} successful."
106     }
107
108     # 2. create parser user.
109     openstack ${debug} user list | grep -qwo ${PARSER_USER} && {
110         echo "  User ${PARSER_USER} exist, doesn't create again."
111     } || {
112         openstack ${debug} user create ${PARSER_USER} --password ${PARSER_PASSWORD} \
113             --project ${PARSER_PROJECT} --email ${PARSER_EMAIL}
114         echo "  Create user ${PARSER_USER} successful."
115     }
116
117     # 3. grant role for parser user
118     openstack ${debug} role assignment list --user ${PARSER_USER} --project ${PARSER_PROJECT} \
119     | grep -qow ${PARSER_ROLE} && {
120         echo "  User ${PARSER_USER} has role ${PARSER_ROLE} in project ${PARSER_PROJECT}, doesn't create."
121     } || {
122         openstack ${debug} role add ${PARSER_ROLE} --user ${PARSER_USER} \
123                            --project ${PARSER_PROJECT}
124         echo "  Grant user ${PARSER_USER} the role of ${PARSER_ROLE} in project ${PARSER_PROJECT} successful."
125         openstack ${debug} role list | grep -qow heat_stack_owner && {
126             openstack ${debug} role add heat_stack_owner --user ${PARSER_USER} \
127                                --project ${PARSER_PROJECT}
128             echo "  Grant user ${PARSER_USER} the role of heat_stack_owner in project ${PARSER_PROJECT} successful."
129         } || true
130     }
131
132 }
133
134 change_env_to_parser_user_project() {
135
136     export OS_USERNAME=${PARSER_USER}
137     export OS_PASSWORD=${PARSER_PASSWORD}
138     export OS_PROJECT_NAME=${PARSER_PROJECT}
139     export OS_TENANT_NAME=${PARSER_TENANT}
140
141     export OS_PROJECT_DOMAIN_NAME=${OS_PROJECT_DOMAIN_NAME:-'Default'}
142     export OS_USER_DOMAIN_NAME=${OS_USER_DOMAIN_NAME:-'Default'}
143
144 }
145
146
147 make_patch_for_provider_network() {
148
149     # copy temp file
150     echo "    Patch provider network for input file:"
151     echo "      Raw yaml file: ${VRNC_INPUT_TEMPLATE_RAW_FILE}"
152     echo "      Patched yaml file: ${VRNC_INPUT_TEMPLATE_FILE}"
153     cp ${VRNC_INPUT_TEMPLATE_RAW_FILE} ${VRNC_INPUT_TEMPLATE_FILE}
154     echo ""
155
156     # Delete the provider:network_type
157     echo "      Patching provider:network_type..."
158     sed -i '/network_type:/d' ${VRNC_INPUT_TEMPLATE_FILE}
159
160     # Delete the provider:segmentation_id
161     echo "      Patching provider:segmentation_id..."
162     sed -i '/segmentation_id:/d' ${VRNC_INPUT_TEMPLATE_FILE}
163
164     # Delete the provider:physical_network
165     echo "      Patching provider:physical_network..."
166     sed -i '/physical_network:/d' ${VRNC_INPUT_TEMPLATE_FILE}
167
168     echo ""
169
170 }
171
172 make_patch_for_translated_file() {
173
174     # Replace the signal_transport
175     echo "    Patch yaml file: ${VRNC_OUTPUT_TEMPLATE_FILE}"
176     echo ""
177     echo "    Patching compute:signal_transport"
178     sed -i '1,$s/HEAT_SIGNAL/NO_SIGNAL/g' ${VRNC_OUTPUT_TEMPLATE_FILE}
179
180 }
181
182
183 translator_and_deploy_vRNC() {
184
185     (
186         # 1. Delete parser stack ${PARSER_STACK_NAME}, use admin user in admin project
187         openstack ${debug} stack list | grep -qow ${PARSER_STACK_NAME} && {
188             echo "  Stack ${PARSER_STACK_NAME} exist, delete it first."
189             openstack stack delete --yes --wait ${PARSER_STACK_NAME}
190         }
191         # 2. Switch env to parser project temporally
192         echo "  Switch openstack env to parser project"
193         change_env_to_parser_user_project
194         echo ""
195
196         # 3. Patch provider network
197         echo "  Make patch for provider network"
198         make_patch_for_provider_network
199
200         # 4. Translator yaml
201         echo "  Translator use parser:"
202         echo "    1. Input  file: ${VRNC_INPUT_TEMPLATE_FILE}"
203         echo "    2. Output file: ${VRNC_OUTPUT_TEMPLATE_FILE}"
204         heat-translator --template-type tosca --template-file ${VRNC_INPUT_TEMPLATE_FILE} \
205             --output-file ${VRNC_OUTPUT_TEMPLATE_FILE}
206         echo ""
207
208         # 5. Patch translated file
209         echo "  Make patch for translated file:"
210         make_patch_for_translated_file
211         echo ""
212
213         # 6. deploy vRNC
214         echo "  Deploy stack..."
215         [[ "${PARSER_CI_DEBUG}" == "true" ]] && debug="--debug" || debug=""
216         openstack ${debug} stack create --timeout 15 --wait --enable-rollback \
217                                         -t ${VRNC_OUTPUT_TEMPLATE_FILE} ${PARSER_STACK_NAME}
218
219         # 7. basic information.
220         echo "  The basic information of deployment..."
221         openstack ${debug} stack show ${PARSER_STACK_NAME}
222
223         # 8. deployed resources.
224         echo "  The resources of deployment..."
225         openstack ${debug} stack resource list ${PARSER_STACK_NAME}
226
227         # 9. deployed outputs.
228         echo "  The outputs of deployment..."
229         openstack ${debug} stack output list ${PARSER_STACK_NAME}
230
231         # 10. Validate the deploy result.
232         echo "  Checking the result of deployment..."
233         openstack ${debug} stack show ${PARSER_STACK_NAME} | grep -qow "CREATE_COMPLETE" && {
234             echo "    Check the result of deployment successfully."
235         } || {
236             echo "    Check the result of deployment unsuccessfully."
237         }
238     )
239
240 }
241
242 reset_parser_test() {
243
244     set +e
245
246     echo "  Clean-up the environment..."
247     # 1. Delete resource created by parser user
248     (
249         # 1). Switch env to parser project temporally
250         change_env_to_parser_user_project
251
252         # 2). Delete the stack ${PARSER_STACK_NAME}
253         echo ""
254         openstack ${debug} stack list | grep -qow ${PARSER_STACK_NAME} && {
255             echo "    Stack ${PARSER_STACK_NAME} has been created, delete it after test."
256             echo ""
257             openstack ${debug} stack delete --yes --wait ${PARSER_STACK_NAME}
258             echo ""
259         }
260
261         # 3). Delete patch tmp file ${VRNC_INPUT_TEMPLATE_FILE}
262         [ -e ${VRNC_OUTPUT_TEMPLATE_FILE} -a ${PARSER_CI_DEBUG} != "true" ] && {
263             echo "    Delete patch temp file ${VRNC_INPUT_TEMPLATE_FILE} after test."
264             rm -fr ${VRNC_INPUT_TEMPLATE_FILE}
265         }
266
267         # 4). Delete hot tmp file ${VRNC_OUTPUT_TEMPLATE_FILE}
268         [ -e ${VRNC_OUTPUT_TEMPLATE_FILE} -a ${PARSER_CI_DEBUG} != "true" ] && {
269             echo "    Delete hot temp file ${VRNC_OUTPUT_TEMPLATE_FILE} after test."
270             rm -fr ${VRNC_OUTPUT_TEMPLATE_FILE}
271         }
272
273         # 5). Delete tmp image ${PARSER_IMAGE_FILE}
274         [[ -e ${PARSER_IMAGE_FILE} ]] && {
275             echo "    Delete local image file ${PARSER_IMAGE_FILE} after test."
276             rm -fr ${PARSER_IMAGE_FILE}
277         }
278
279         # 6). Delete tmp image ${PARSER_IMAGE_URL_FILE}
280         [ -e ${PARSER_IMAGE_URL_FILE} -a ${PARSER_CI_DEBUG} != "true" ] && {
281             echo "    Delete local URL image file ${PARSER_IMAGE_URL_FILE} after test."
282             rm -fr ${PARSER_IMAGE_URL_FILE}
283         }
284
285         # 7). Delete image from openstack
286         parser_image_id=$(openstack ${debug} image list | grep -w "${PARSER_IMAGE_NAME}" | awk '{print $2}')
287         [[ -n "${parser_image_id}" ]] && openstack image delete "${parser_image_id}"
288
289         sleep 3
290     )
291
292     # 2. Delete role, user and project
293     echo "    Delete user ${PARSER_USER}'s role from project ${PARSER_PROJECT}"
294     openstack ${debug} user role list "${PARSER_USER}" --project "${PARSER_PROJECT}" \
295     | grep -qow "${PARSER_ROLE}" && {
296         openstack ${debug} role remove "${PARSER_ROLE}" --user "${PARSER_USER}" \
297                               --project "${PARSER_PROJECT}"
298     }
299
300     echo "    Delete user ${PARSER_USER}"
301     openstack ${debug} user list | grep -qow "${PARSER_USER}" && {
302         openstack user delete "${PARSER_USER}"
303     }
304
305     echo "    Delete project ${PARSER_PROJECT}"
306     openstack ${debug} project list | grep -qwo "${PARSER_PROJECT}" && {
307         openstack project delete "${PARSER_PROJECT}"
308     }
309
310     echo ""
311     echo "======================={ Parser functest end }=========================="
312     echo ""
313     echo ""
314
315 }
316
317 echo ""
318 echo ""
319 echo "======================={ Parser functest begin }=========================="
320 echo ""
321
322 trap reset_parser_test EXIT
323
324 # start syslog for loghander
325 which service > /dev/null  && service rsyslog restart || true
326
327 echo "|========= 1/4. Preparing VM image and flavor for parser...     =========|"
328 download_parser_image
329 register_parser_image_and_flavor
330
331 echo ""
332 echo "|========= 2/4. Creating test user for parser...     =========|"
333 create_parser_user_and_project
334
335 echo ""
336 echo "|========= 3/4. Parse -> translate -> deploy vRNC... =========|"
337 translator_and_deploy_vRNC
338
339 echo ""
340 echo "|========= 4/4. Test ok...                           =========|"
341 echo ""