Modify the test script for reducing the test time
[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_FILE=../tosca2heat/heat-translator/translator/tests/data/test_tosca_nfv_sample.yaml
40
41 VRNC_OUTPUT_TEMPLATE_FILE=./vRNC_Hot_Template.yaml
42
43 download_parser_image() {
44     [ -e "${PARSER_IMAGE_URL_FILE}" ] && {
45         echo "  Image ${PARSER_IMAGE_URL_FILE} has bee cached, needn't download again."
46         cp ${PARSER_IMAGE_URL_FILE} ${PARSER_IMAGE_FILE}
47         return 0
48     }
49
50     echo ""
51     echo "  Download image ${PARSER_IMAGE_URL_FILE}..."
52     wget ${PARSER_IMAGE_URL} -o ${PARSER_IMAGE_FILE}
53 }
54
55 register_parser_image() {
56     openstack ${debug} image list | grep -qwo "${PARSER_IMAGE_NAME}" && {
57         echo "  Image ${PARSER_IMAGE_NAME} has bee registed, needn't registe again."
58         return 0
59     }
60
61     echo ""
62     echo "  Registe image ${PARSER_IMAGE_NAME}..."
63     openstack ${debug} image create "${PARSER_IMAGE_NAME}" \
64                            --public \
65                            --disk-format ${PARSER_IMAGE_FORMAT} \
66                            --container-format bare \
67                            --file ${PARSER_IMAGE_FILE}
68 }
69
70 create_parser_user_and_project() {
71
72     # 1. create parser project
73     openstack ${debug} project list | grep -qwo "${PARSER_PROJECT}" && {
74         echo "  Project ${PARSER_PROJECT} exist, doesn't create agian."
75     } || {
76         openstack  ${debug} project create ${PARSER_PROJECT} \
77             --description "Project for parser test"
78         echo "  Create project ${PARSER_PROJECT} successful."
79     }
80
81     # 2. create parser user.
82     openstack ${debug} user list | grep -qwo ${PARSER_USER} && {
83         echo "  User ${PARSER_USER} exist, doesn't create again."
84     } || {
85         openstack ${debug} user create ${PARSER_USER} --password ${PARSER_PASSWORD} \
86             --project ${PARSER_PROJECT} --email ${PARSER_EMAIL}
87         echo "  Create user ${PARSER_USER} successful."
88     }
89
90     # 3. grant role for parser user
91     openstack ${debug} user role list ${PARSER_USER} --project ${PARSER_PROJECT} \
92     | grep -qow ${PARSER_ROLE} && {
93         echo "  User ${PARSER_USER} has role ${PARSER_ROLE} in project ${PARSER_PROJECT}, doesn't create."
94     } || {
95         openstack ${debug} role add ${PARSER_ROLE} --user ${PARSER_USER} \
96                            --project ${PARSER_PROJECT}
97         echo "  Grant user ${PARSER_USER} the role ${PARSER_ROLE} in project ${PARSER_PROJECT} successful."
98     }
99
100 }
101
102 change_env_to_parser_user_project() {
103
104     export OS_USERNAME=${PARSER_USER}
105     export OS_PASSWORD=${PARSER_PASSWORD}
106     export OS_PROJECT_NAME=${PARSER_PROJECT}
107     export OS_TENANT_NAME=${PARSER_TENANT}
108
109 }
110
111 translator_and_deploy_vRNC() {
112
113     (
114         # 1. Delete parser stack ${PARSER_STACK_NAME}, use admin user in admin project
115         openstack ${debug} stack list | grep -qow ${PARSER_STACK_NAME} && {
116             echo "  Stack ${PARSER_STACK_NAME} exist, delete it first."
117             openstack stack delete --yes --wait ${PARSER_STACK_NAME}
118         }
119         # 2. Switch env to parser project temporally
120         echo "  Switch openstack env to parser project"
121         change_env_to_parser_user_project
122
123         # 3. Translator yaml
124         echo "  Translator use parser:"
125         echo "    1. Input  file: ${VRNC_INPUT_TEMPLATE_FILE}"
126         echo "    2. Output file: ${VRNC_OUTPUT_TEMPLATE_FILE}"
127         heat-translator --template-type tosca --template-file ${VRNC_INPUT_TEMPLATE_FILE} \
128             --output-file ${VRNC_OUTPUT_TEMPLATE_FILE}
129
130         # 4. deploy vRNC
131         echo "  Deploy stack..."
132         [[ "${PARSER_CI_DEBUG}" == "true" ]] && debug="--debug" || debug=""
133         openstack ${debug} stack create --timeout 30 --wait --enable-rollback \
134                                         -t ${VRNC_OUTPUT_TEMPLATE_FILE} ${PARSER_STACK_NAME}
135
136         # 5. Validate the deploy result.
137         echo "  Checking the result of deployment..."
138         openstack ${debug} stack show ${PARSER_STACK_NAME} | grep -qow "CREATE_COMPLETE" && {
139             echo "    Check the result of deployment successfully."
140         } || {
141             echo "    Check the result of deployment unsuccessfully."
142         }
143     )
144
145 }
146
147 reset_parser_test() {
148
149     set +e
150
151     echo "  Clean-up the environment..."
152     # 1. Delete resource created by parser user
153     (
154         # 1). Switch env to parser project temporally
155         change_env_to_parser_user_project
156
157         # 2). Delete the stack ${PARSER_STACK_NAME}
158         openstack ${debug} stack list | grep -qow ${PARSER_STACK_NAME} && {
159             echo "    Stack ${PARSER_STACK_NAME} has been created, delete it after test."
160             openstack ${debug} stack delete --yes --wait ${PARSER_STACK_NAME}
161         }
162
163         # 3). Delete hot tmp file ${VRNC_OUTPUT_TEMPLATE_FILE}
164         [ -e ${VRNC_OUTPUT_TEMPLATE_FILE} -a ${PARSER_CI_DEBUG} != "true" ] && {
165             echo "    Delete hot temp file ${VRNC_OUTPUT_TEMPLATE_FILE} after test."
166             rm -fr ${VRNC_OUTPUT_TEMPLATE_FILE}
167         }
168
169         # 4). Delete tmp image ${PARSER_IMAGE_FILE}
170         [[ -e ${PARSER_IMAGE_FILE} ]] && {
171             echo "    Delete local image file ${PARSER_IMAGE_FILE} after test."
172             rm -fr ${PARSER_IMAGE_FILE}
173         }
174
175         # 5). Delete tmp image ${PARSER_IMAGE_URL_FILE}
176         [ -e ${PARSER_IMAGE_URL_FILE} -a ${PARSER_CI_DEBUG} != "true" ] && {
177             echo "    Delete local URL image file ${PARSER_IMAGE_URL_FILE} after test."
178             rm -fr ${PARSER_IMAGE_URL_FILE}
179         }
180
181         # 6). Delete image from openstack
182         parser_image_id=$(openstack ${debug} image list | grep -w "${PARSER_IMAGE_NAME}" | awk '{print $2}')
183         [[ -n "${parser_image_id}" ]] && openstack image delete "${parser_image_id}"
184
185         sleep 3
186     )
187
188     # 2. Delete role, user and project
189     echo "    Delete user ${PARSER_USER}'s role from project ${PARSER_PROJECT}"
190     openstack ${debug} user role list "${PARSER_USER}" --project "${PARSER_PROJECT}" \
191     | grep -qow "${PARSER_ROLE}" && {
192         openstack ${debug} role remove "${PARSER_ROLE}" --user "${PARSER_USER}" \
193                               --project "${PARSER_PROJECT}"
194     }
195
196     echo "    Delete user ${PARSER_USER}"
197     openstack ${debug} user list | grep -qow "${PARSER_USER}" && {
198         openstack user delete "${PARSER_USER}"
199     }
200
201     echo "    Delete project ${PARSER_PROJECT}"
202     openstack ${debug} project list | grep -qwo "${PARSER_PROJECT}" && {
203         openstack project delete "${PARSER_PROJECT}"
204     }
205
206     echo ""
207     echo "======================= Parser functest end =========================="
208     echo ""
209     echo ""
210
211 }
212
213 echo ""
214 echo ""
215 echo "======================= Parser functest begin =========================="
216 echo ""
217
218 trap reset_parser_test EXIT
219
220 # start syslog for loghander
221 service rsyslog restart
222
223 echo "|========= 1/4. Preparing VM image for parser...     =========|"
224 download_parser_image
225 register_parser_image
226
227 echo ""
228 echo "|========= 2/4. Creating test user for parser...     =========|"
229 create_parser_user_and_project
230
231 echo ""
232 echo "|========= 3/4. Parse -> translate -> deploy vRNC... =========|"
233 translator_and_deploy_vRNC
234
235 echo ""
236 echo "|========= 4/4. Test ok...                           =========|"
237 echo ""