Fix bug: authentication error when deploy vRNC with nfv tosca template
[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" ]] && set -x
13
14 PARSER_IMAGE_URL_FILE=cirros-0.3.0-x86_64-disk.img
15 PARSER_IMAGE_URL=https://launchpad.net/cirros/trunk/0.3.0/+download/${PARSER_IMAGE_URL_FILE}
16 PARSER_IMAGE_NAME=rhel-6.5-test-image
17 PARSER_IMAGE_FILE="${PARSER_IMAGE_NAME}.img"
18 PARSER_IMAGE_FORMAT=qcow2
19
20 PARSER_VM_FLAVOR=m1.tiny
21
22 PARSER_USER=parser
23 PARSER_PASSWORD=parser
24 PARSER_EMAIL='shang.xiaodong@zte.com.cn'
25 PARSER_PROJECT=parser
26 PARSER_TENANT=${PARSER_PROJECT}
27
28 PARSER_ROLE=admin
29
30 PARSER_STACK_NAME=vRNC_Stack
31
32 # VRNC_INPUT_TEMPLATE_FILE=../tosca2heat/tosca-parser/toscaparser/extensions/nfv/tests/data/vRNC/Definitions/vRNC.yaml
33 VRNC_INPUT_TEMPLATE_FILE=../tosca2heat/heat-translator/translator/tests/data/test_tosca_nfv_sample.yaml
34
35 VRNC_OUTPUT_TEMPLATE_FILE=./vRNC_Hot_Template.yaml
36
37 download_parser_image() {
38     [ -e "${PARSER_IMAGE_URL_FILE}" ] && {
39         echo "Image ${PARSER_IMAGE_URL_FILE} has bee cached, needn't download again."
40         cp ${PARSER_IMAGE_URL_FILE} ${PARSER_IMAGE_FILE}
41         return 0
42     }
43
44     echo "Download image ${PARSER_IMAGE_URL_FILE}..."
45     wget ${PARSER_IMAGE_URL} -o ${PARSER_IMAGE_FILE}
46 }
47
48 register_parser_image() {
49     openstack image list | grep -qwo "${PARSER_IMAGE_NAME}" && {
50         echo "Image ${PARSER_IMAGE_NAME} has bee registed, needn't registe again."
51         return 0
52     }
53
54     echo "Registe image ${PARSER_IMAGE_NAME}..."
55     openstack image create "${PARSER_IMAGE_NAME}" \
56                            --public \
57                            --disk-format ${PARSER_IMAGE_FORMAT} \
58                            --container-format bare \
59                            --file ${PARSER_IMAGE_FILE}
60 }
61
62 create_parser_user_and_project() {
63
64
65     # 1. create parser project
66     openstack project list | grep -qwo "${PARSER_PROJECT}" && {
67         echo "Project ${PARSER_PROJECT} exist, doesn't create agian."
68     } || {
69         openstack project create ${PARSER_PROJECT} \
70             --description "Project for parser test"
71         echo "Create project ${PARSER_PROJECT} successful."
72     }
73
74     # 2. create parser user.
75     openstack user list | grep -qwo ${PARSER_USER} && {
76         echo "User ${PARSER_USER} exist, doesn't create again."
77     } || {
78         openstack user create ${PARSER_USER} --password ${PARSER_PASSWORD} \
79             --project ${PARSER_PROJECT} --email ${PARSER_EMAIL}
80         echo "Create user ${PARSER_USER} successful."
81     }
82
83     # 3. grant role for parser user
84     openstack user role list ${PARSER_USER} --project ${PARSER_PROJECT} \
85     | grep -qow ${PARSER_ROLE} && {
86         echo "User ${PARSER_USER} has role ${PARSER_ROLE} in project ${PARSER_PROJECT}, doesn't create."
87     } || {
88         openstack role add ${PARSER_ROLE} --user ${PARSER_USER} \
89                            --project ${PARSER_PROJECT}
90         echo "Grant user ${PARSER_USER} the role ${PARSER_ROLE} in project ${PARSER_PROJECT} successful."
91     }
92
93 }
94
95 change_env_to_parser_user_project() {
96
97     export OS_USERNAME=${PARSER_USER}
98     export OS_PASSWORD=${PARSER_PASSWORD}
99     export OS_PROJECT_NAME=${PARSER_PROJECT}
100     export OS_TENANT_NAME=${PARSER_TENANT}
101
102 }
103
104 translator_and_deploy_vRNC() {
105     (
106         # 1. Delete parser stack ${PARSER_STACK_NAME}, use admin user in admin project
107         openstack stack list | grep -qow ${PARSER_STACK_NAME} && {
108             echo "stack ${PARSER_STACK_NAME} exist, delete it first."
109             openstack stack delete --yes --wait ${PARSER_STACK_NAME}
110         }
111         # 2. Switch env to parser project temporally
112         echo "switch openstack env to parser project"
113         change_env_to_parser_user_project
114
115         # 3. Translator yaml
116         echo "Translator input file ${VRNC_INPUT_TEMPLATE_FILE} and output is ${VRNC_OUTPUT_TEMPLATE_FILE}"
117         heat-translator --template-type tosca --template-file ${VRNC_INPUT_TEMPLATE_FILE} \
118             --output-file ${VRNC_OUTPUT_TEMPLATE_FILE}
119
120         # 4. deploy vRNC
121         echo "Deploy stack..."
122         openstack stack create -t ${VRNC_OUTPUT_TEMPLATE_FILE} ${PARSER_STACK_NAME}
123
124         # 5. Wait for create vRNC
125         echo "Waiting for deploying stack..."
126         sleep 180
127
128         # 6. Validate the deploy result.
129         echo "Checking the result of deployment..."
130         # 1). check vdu
131         openstack server list | grep -qwo "${PARSER_STACK_NAME}" && {
132             echo "  Check VDU successful."
133         } || {
134             echo "  Check VDU unsuccessful."
135             exit 1
136         }
137         # 2). check VL-network
138         openstack network list | grep -qwo "${PARSER_STACK_NAME}" && {
139             echo "  Check VL-network successful."
140         } || {
141             echo "  Check VL-network unsuccessful."
142             exit 1
143         }
144         # 3). check VL-subnet
145         openstack subnet list | grep -qwo "${PARSER_STACK_NAME}" && {
146             echo "  Check VL-subnet successful."
147         } || {
148             echo "  Check VL-subnet unsuccessful."
149             exit 1
150         }
151         # 4). check port
152         neutron port-list | grep -qwo "${PARSER_STACK_NAME}" && {
153             echo "  Check CP successful."
154         } || {
155             echo "  Check CP unsuccessful."
156             exit 1
157         }
158         echo "Checkthe result of deployment successfully."
159     )
160
161 }
162
163 reset_parser_test() {
164     set +e
165
166     echo "cleanup..."
167     # 1. Delete resource created by parser user
168     (
169         # 1). Switch env to parser project temporally
170         change_env_to_parser_user_project
171
172         # 2). Delete the stack ${PARSER_STACK_NAME}
173         openstack stack list | grep -qow ${PARSER_STACK_NAME} && {
174             echo "stack ${PARSER_STACK_NAME} has been created, delete it after test."
175             openstack stack delete --yes --wait ${PARSER_STACK_NAME}
176         }
177
178         # 3). Delete hot tmp file ${VRNC_OUTPUT_TEMPLATE_FILE}
179         [ -e ${VRNC_OUTPUT_TEMPLATE_FILE} -a ${PARSER_CI_DEBUG} != "true" ] && {
180             echo "delete hot temp file ${VRNC_OUTPUT_TEMPLATE_FILE} after test."
181             rm -fr ${VRNC_OUTPUT_TEMPLATE_FILE}
182         }
183
184         # 4). Delete tmp image ${PARSER_IMAGE_FILE}
185         [[ -e ${PARSER_IMAGE_FILE} ]] && {
186             echo "delete local image file ${PARSER_IMAGE_FILE} after test."
187             rm -fr ${PARSER_IMAGE_FILE}
188         }
189
190         # 5). Delete tmp image ${PARSER_IMAGE_URL_FILE}
191         [ -e ${PARSER_IMAGE_URL_FILE} -a ${PARSER_CI_DEBUG} != "true" ] && {
192             echo "delete local URL image file ${PARSER_IMAGE_URL_FILE} after test."
193             rm -fr ${PARSER_IMAGE_URL_FILE}
194         }
195
196         # 6). Delete image from openstack
197         parser_image_id=$(openstack image list | grep -w "${PARSER_IMAGE_NAME}" | awk '{print $2}')
198         [[ -n "${parser_image_id}" ]] && openstack image delete "${parser_image_id}"
199
200         sleep 3
201     )
202
203     # 2. Delete role, user and project
204     openstack user role list "${PARSER_USER}" --project "${PARSER_PROJECT}" \
205     | grep -qow " ${PARSER_ROLE}" && {
206         openstack role remove "${PARSER_ROLE}" --user "${PARSER_USER}" \
207                               --project "${PARSER_PROJECT}"
208     }
209
210     openstack user list | grep -qow "${PARSER_USER}" && {
211         openstack user delete "${PARSER_USER}"
212     }
213
214     openstack project list | grep -qwo "${PARSER_PROJECT}" && {
215         openstack project delete "${PARSER_PROJECT}"
216     }
217
218     echo ""
219     echo "======================= Parser functest end =========================="
220     echo ""
221     echo ""
222
223 }
224
225 echo ""
226 echo ""
227 echo "======================= Parser functest begin =========================="
228 echo ""
229
230 trap reset_parser_test EXIT
231
232 # start syslog for loghander
233 service rsyslog restart
234
235 echo "|========= 1/4. Preparing VM image for parser...     =========|"
236 download_parser_image
237 register_parser_image
238
239 echo "|========= 2/4. Creating test user for parser...     =========|"
240 create_parser_user_and_project
241
242 echo "|========= 3/4. Parse -> translate -> deploy vRNC... =========|"
243 translator_and_deploy_vRNC
244
245 echo "|========= 4/4. Test ok...                           =========|"