Check the user, role and project existence when reset test env.
[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=rhel-6.5-test-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 --yes --wait ${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} \
111             --output-file ${VRNC_OUTPUT_TEMPLATE_FILE}
112
113         # 4. deploy vRNC
114         openstack stack create -t ${VRNC_OUTPUT_TEMPLATE_FILE} ${PARSER_STACK_NAME}
115
116         # 5. Wait for create vRNC
117         sleep 60
118
119         # 6. Validate the deploy result.
120
121     )
122
123 }
124
125 reset_parser_test() {
126     set +e
127
128     echo "cleanup..."
129     # 1. Delete resource created by parser user
130     (
131         # 1). Switch env to parser project temporally
132         change_env_to_parser_user_project
133
134         # 2). Delete the stack ${PARSER_STACK_NAME}
135         openstack stack list | grep -qow ${PARSER_STACK_NAME} && {
136             echo "stack ${PARSER_STACK_NAME} has been created, delete it after test."
137             openstack stack delete --yes --wait ${PARSER_STACK_NAME}
138         }
139
140         # 3). Delete hot tmp file ${VRNC_OUTPUT_TEMPLATE_FILE}
141         [ -e ${VRNC_OUTPUT_TEMPLATE_FILE} -a ${CI_DEBUG} != "true" ] && {
142             echo "delete hot temp file ${VRNC_OUTPUT_TEMPLATE_FILE} after test."
143             rm -fr ${VRNC_OUTPUT_TEMPLATE_FILE}
144         }
145
146         # 4). Delete tmp image ${PARSER_IMAGE_FILE}
147         [[ -e ${PARSER_IMAGE_FILE} ]] && {
148             echo "delete local image file ${PARSER_IMAGE_FILE} after test."
149             rm -fr ${PARSER_IMAGE_FILE}
150         }
151
152         # 5). Delete tmp image ${PARSER_IMAGE_URL_FILE}
153         [ -e ${PARSER_IMAGE_URL_FILE} -a ${CI_DEBUG} != "true" ] && {
154             echo "delete local URL image file ${PARSER_IMAGE_URL_FILE} after test."
155             rm -fr ${PARSER_IMAGE_URL_FILE}
156         }
157
158         # 6). Delete image from openstack
159         parser_image_id=$(openstack image list | grep -w "${PARSER_IMAGE_NAME}" | awk '{print $2}')
160         [[ -n "${parser_image_id}" ]] && openstack image delete "${parser_image_id}"
161
162         sleep 3
163     )
164
165     # 2. Delete role, user and project
166     openstack user role list "${PARSER_USER}" --project "${PARSER_PROJECT}" \
167     | grep -qow " ${PARSER_ROLE}" && {
168         openstack role remove "${PARSER_ROLE}" --user "${PARSER_USER}" \
169                               --project "${PARSER_PROJECT}"
170     }
171
172     openstack project list | grep -qwo "${PARSER_PROJECT}" && {
173         openstack project delete "${PARSER_PROJECT}"
174     }
175
176     openstack user list | grep -qow "${PARSER_USER}" && {
177         openstack user delete "${PARSER_USER}"
178     }
179
180 }
181
182
183 echo "======================= Parser functest begin =========================="
184
185 trap reset_parser_test EXIT
186
187 echo " ========= 1/4. Preparing VM image for parser...     ========= "
188 download_parser_image
189 register_parser_image
190
191 echo " ========= 2/4. Creating test user for parser...     ========= "
192 create_parser_user_and_project
193
194 echo " ========= 3/4. Parse -> translate -> deploy vRNC... ========= "
195 translator_and_deploy_vRNC
196
197 echo " ========= 4/4. Test ok...                           ========= "
198
199 echo "======================= Parser functest end =========================="