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