Merge "Doesn't support nested exceptioncollector when implement the nested tosca...
[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 [[ "${CI_DEBUG:-true}" == "true" ]] && set -x
12
13 PARSER_IMAGE_URL_FILE=cirros-0.3.0-x86_64-disk.img
14 PARSER_IMAGE_URL=https://launchpad.net/cirros/trunk/0.3.0/+download/${PARSER_IMAGE_URL_FILE}
15 PARSER_IMAGE_NAME=cirros
16 PARSER_IMAGE_FILE="${PARSER_IMAGE_NAME}.img"
17 PARSER_IMAGE_FORMAT=qcow2
18
19 PARSER_VM_FLAVOR=m1.tiny
20
21 PARSER_USER=parser
22 PARSER_PASSWORD=parser
23 PARSER_PROJECT=parser
24 PARSER_TENANT=${PARSER_PROJECT}
25
26 PARSER_ROLE=admin
27
28 PARSER_STACK_NAME=vRNC_Stack
29
30 # VRNC_INPUT_TEMPLATE_FILE=../tosca2heat/tosca-parser/toscaparser/extensions/nfv/tests/data/vRNC/Definitions/vRNC.yaml
31 VRNC_INPUT_TEMPLATE_FILE=../tosca2heat/tosca-parser/toscaparser/extensions/nfv/tests/data/tosca_helloworld_nfv.yaml
32
33 VRNC_OUTPUT_TEMPLATE_FILE=./vRNC_Hot_Template.yaml
34
35 download_parser_image() {
36     [ -e "${PARSER_IMAGE_URL_FILE}" ] && cp ${PARSER_IMAGE_URL_FILE} ${PARSER_IMAGE_FILE} && return 0
37     wget "${PARSER_IMAGE_URL}" -o "${PARSER_IMAGE_FILE}"
38 }
39
40 register_parser_image() {
41     openstack image list | grep -qwo "${PARSER_IMAGE_NAME}" && return 0
42     openstack image create "${PARSER_IMAGE_NAME}" \
43                            --public \
44                            --disk-format "${PARSER_IMAGE_FORMAT}" \
45                            --container-format bare \
46                            --file "${PARSER_IMAGE_FILE}"
47 }
48
49 create_parser_user_and_project() {
50
51     # 1. create parser user.
52     openstack user list | grep -qwo "${PARSER_USER}" && {
53         echo "User ${PARSER_USER} exist, doesn't crate."
54     } || {
55         openstack user create "${PARSER_USER}" --password "${PARSER_PASSWORD}"
56         echo "Create user ${PARSER_USER} successful."
57     }
58
59     # 2. create parser project
60     openstack project list | grep -qwo "${PARSER_PROJECT}" && {
61         echo "Project ${PARSER_PROJECT} exist, doesn't crate."
62     } || {
63         openstack project create "${PARSER_PROJECT}"
64         echo "Create project ${PARSER_PROJECT} successful."
65     }
66
67     # 3. grant role for parser user
68     openstack user role list "${PARSER_USER}" --project "${PARSER_PROJECT}" \
69     | grep -qow " ${PARSER_ROLE}" && {
70         echo "User ${PARSER_USER} has role ${PARSER_ROLE} in project ${PARSER_PROJECT}, doesn't crate."
71     } || {
72         openstack role add "${PARSER_ROLE}" --user "${PARSER_USER}" \
73                            --project "${PARSER_PROJECT}"
74         echo "Grant user ${PARSER_USER} the role ${PARSER_ROLE} in project ${PARSER_PROJECT} successful."
75     }
76
77 }
78
79 change_env_to_parser_user_project() {
80
81     export OS_USERNAME="$PARSER_USER"
82     export OS_PASSWORD="$PARSER_PASSWORD"
83     export OS_PROJECT_NAME="$PARSER_PROJECT"
84     export OS_TENANT_NAME="$PARSER_TENANT"
85
86 }
87
88 translator_and_deploy_vRNC() {
89     (
90         # 1. Delete parser stack ${PARSER_STACK_NAME}, use admin user in admin project
91         openstack stack list | grep -qow ${PARSER_STACK_NAME} && {
92             echo "stack ${PARSER_STACK_NAME} exist, delete it first."
93             openstack stack delete ${PARSER_STACK_NAME}
94         }
95         # 2. Switch env to parser project temporally
96         change_env_to_parser_user_project
97
98         # 3. Translator and deploy vRNC
99         heat-translator --template-type tosca -f ${VRNC_INPUT_TEMPLATE_FILE} -o ${VRNC_OUTPUT_TEMPLATE_FILE} --deploy True
100
101         # 4. Wait for create vRNC
102         sleep 60
103
104         # 5. Validate the deploy result.
105
106     )
107
108 }
109
110 reset_parser_test() {
111     set +e
112
113     ret=$1
114
115     echo "cleanup..."
116     (
117         # 1. Switch env to parser project temporally
118         change_env_to_parser_user_project
119
120         # 2. Delete the stack ${PARSER_STACK_NAME}
121         openstack stack list | grep -qow ${PARSER_STACK_NAME} && {
122             echo "stack ${PARSER_STACK_NAME} has been created, delete it after test."
123             openstack stack delete ${PARSER_STACK_NAME}
124         }
125
126         # 3. Delete hot tmp file ${VRNC_OUTPUT_TEMPLATE_FILE}
127         [[ -e ${VRNC_OUTPUT_TEMPLATE_FILE} ]] && {
128             echo "delete hot temp file ${VRNC_OUTPUT_TEMPLATE_FILE} after test."
129             rm -fr ${VRNC_OUTPUT_TEMPLATE_FILE}
130         }
131
132         # 4. Delete tmp image ${PARSER_IMAGE_FILE}
133         [[ -e ${PARSER_IMAGE_FILE} ]] && {
134             echo "delete local image file ${PARSER_IMAGE_FILE} after tes."
135             rm -fr ${PARSER_IMAGE_FILE}
136         }
137
138         # 5. Delete tmp image ${PARSER_IMAGE_URL_FILE}
139         [[ -e ${PARSER_IMAGE_URL_FILE} ]] && {
140             echo "delete local image file ${PARSER_IMAGE_URL_FILE} after tes."
141             rm -fr ${PARSER_IMAGE_URL_FILE}
142         }
143
144         sleep 3
145     )
146
147     # 3. Delete parser user and project
148     parser_image_id=$(openstack image list | grep -ow "${PARSER_IMAGE_NAME}" | awk '{print $2}')
149     sleep 1
150     [ -n "${parser_image_id}" ] && openstack image delete "${parser_image_id}"
151     openstack role remove "${PARSER_ROLE}" --user "${PARSER_USER}" \
152                               --project "${PARSER_PROJECT}"
153     openstack project delete "${PARSER_PROJECT}"
154     openstack user delete "${PARSER_USER}"
155
156     if [[ ret != "test_ok" ]]; then
157        echo " ========= 4/4. test error, check your env or code. ========= "
158        echo "======================= Parser functest end =========================="
159        exit 1
160     fi
161 }
162
163
164 echo "======================= Parser functest begin =========================="
165
166 trap reset_parser_test EXIT
167
168 echo " ========= 1/4. Preparing VM image for parser...     ========= "
169 download_parser_image
170 register_parser_image
171
172 echo " ========= 2/4. Creating test user for parser...     ========= "
173 create_parser_user_and_project
174
175 echo " ========= 3/4. Parse -> translate -> deploy vRNC... ========= "
176 translator_and_deploy_vRNC
177
178 echo " ========= 4/4. Test ok, clear the test evn...       ========= "
179 reset_parser_test "test_ok"
180
181 echo "======================= Parser functest end =========================="
182
183 exit 0
184