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