Make patch for provider network when run in functest
[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 [[ "${PARSER_CI_DEBUG}" == "true" ]] && {
13     set -x
14     debug="--debug"
15 } || {
16     set +x
17     debug=""
18 }
19
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
25
26 PARSER_VM_FLAVOR=m1.tiny
27
28 PARSER_USER=parser
29 PARSER_PASSWORD=parser
30 PARSER_EMAIL='shang.xiaodong@zte.com.cn'
31 PARSER_PROJECT=parser
32 PARSER_TENANT=${PARSER_PROJECT}
33
34 PARSER_ROLE=admin
35
36 PARSER_STACK_NAME=vRNC_Stack
37
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
41
42 VRNC_INPUT_TEMPLATE_FILE=${VRNC_INPUT_TEMPLATE_RAW_FILE%.*}_patch.yaml
43
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}
48         return 0
49     }
50
51     echo ""
52     echo "  Download image ${PARSER_IMAGE_URL_FILE}..."
53     wget ${PARSER_IMAGE_URL} -o ${PARSER_IMAGE_FILE}
54 }
55
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."
59         return 0
60     }
61
62     echo ""
63     echo "  Registe image ${PARSER_IMAGE_NAME}..."
64     openstack ${debug} image create "${PARSER_IMAGE_NAME}" \
65                            --public \
66                            --disk-format ${PARSER_IMAGE_FORMAT} \
67                            --container-format bare \
68                            --file ${PARSER_IMAGE_FILE}
69 }
70
71 create_parser_user_and_project() {
72
73     # 1. create parser project
74     openstack ${debug} project list | grep -qwo "${PARSER_PROJECT}" && {
75         echo "  Project ${PARSER_PROJECT} exist, doesn't create agian."
76     } || {
77         openstack  ${debug} project create ${PARSER_PROJECT} \
78             --description "Project for parser test"
79         echo "  Create project ${PARSER_PROJECT} successful."
80     }
81
82     # 2. create parser user.
83     openstack ${debug} user list | grep -qwo ${PARSER_USER} && {
84         echo "  User ${PARSER_USER} exist, doesn't create again."
85     } || {
86         openstack ${debug} user create ${PARSER_USER} --password ${PARSER_PASSWORD} \
87             --project ${PARSER_PROJECT} --email ${PARSER_EMAIL}
88         echo "  Create user ${PARSER_USER} successful."
89     }
90
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."
95     } || {
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."
99     }
100
101 }
102
103 change_env_to_parser_user_project() {
104
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}
109
110 }
111
112
113 make_patch_for_provider_network() {
114
115     # copy temp file
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}
120     echo ""
121
122     # Delete the provider:network_type
123     echo "    Patch provider:network_type..."
124     sed -i '/network_type:/d' ${VRNC_INPUT_TEMPLATE_FILE}
125
126     # Delete the provider:segmentation_id
127     echo "    Patch provider:segmentation_id..."
128     sed -i '/segmentation_id:/d' ${VRNC_INPUT_TEMPLATE_FILE}
129
130     # Delete the provider:physical_network
131     echo "    Patch provider:physical_network..."
132     sed -i '/physical_network:/d' ${VRNC_INPUT_TEMPLATE_FILE}
133
134     echo ""
135
136 }
137
138
139 translator_and_deploy_vRNC() {
140
141     (
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}
146         }
147         # 2. Switch env to parser project temporally
148         echo "  Switch openstack env to parser project"
149         change_env_to_parser_user_project
150         echo ""
151
152         # 3. Patch provider network
153         echo "  Make patch for provider network"
154         make_patch_for_provider_network
155
156         # 4. Translator yaml
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}
162         echo ""
163
164         # 5. deploy vRNC
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}
169
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."
174         } || {
175             echo "    Check the result of deployment unsuccessfully."
176         }
177     )
178
179 }
180
181 reset_parser_test() {
182
183     set +e
184
185     echo "  Clean-up the environment..."
186     # 1. Delete resource created by parser user
187     (
188         # 1). Switch env to parser project temporally
189         change_env_to_parser_user_project
190
191         # 2). Delete the stack ${PARSER_STACK_NAME}
192         echo ""
193         openstack ${debug} stack list | grep -qow ${PARSER_STACK_NAME} && {
194             echo "    Stack ${PARSER_STACK_NAME} has been created, delete it after test."
195             echo ""
196             openstack ${debug} stack delete --yes --wait ${PARSER_STACK_NAME}
197             echo ""
198         }
199
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}
204         }
205
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}
210         }
211
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}
216         }
217
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}
222         }
223
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}"
227
228         sleep 3
229     )
230
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}"
237     }
238
239     echo "    Delete user ${PARSER_USER}"
240     openstack ${debug} user list | grep -qow "${PARSER_USER}" && {
241         openstack user delete "${PARSER_USER}"
242     }
243
244     echo "    Delete project ${PARSER_PROJECT}"
245     openstack ${debug} project list | grep -qwo "${PARSER_PROJECT}" && {
246         openstack project delete "${PARSER_PROJECT}"
247     }
248
249     echo ""
250     echo "======================= Parser functest end =========================="
251     echo ""
252     echo ""
253
254 }
255
256 echo ""
257 echo ""
258 echo "======================= Parser functest begin =========================="
259 echo ""
260
261 trap reset_parser_test EXIT
262
263 # start syslog for loghander
264 service rsyslog restart
265
266 echo "|========= 1/4. Preparing VM image for parser...     =========|"
267 download_parser_image
268 register_parser_image
269
270 echo ""
271 echo "|========= 2/4. Creating test user for parser...     =========|"
272 create_parser_user_and_project
273
274 echo ""
275 echo "|========= 3/4. Parse -> translate -> deploy vRNC... =========|"
276 translator_and_deploy_vRNC
277
278 echo ""
279 echo "|========= 4/4. Test ok...                           =========|"
280 echo ""