summaryrefslogtreecommitdiffstats
path: root/tests/functest_run.sh
blob: 615cad5d5dec324c7c8c1e1f2b80b4addf395ab5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/bin/bash -e
##############################################################################
# Copyright (c) 2016 ZTE Corporation.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################

PARSER_CI_DEBUG=${CI_DEBUG:-false}
[[ "${PARSER_CI_DEBUG}" == "true" ]] && {
    set -x
    debug="--debug"
} || {
    set +x
    debug=""
}

PARSER_IMAGE_URL_FILE=cirros-0.3.0-x86_64-disk.img
PARSER_IMAGE_URL=https://launchpad.net/cirros/trunk/0.3.0/+download/${PARSER_IMAGE_URL_FILE}
PARSER_IMAGE_NAME=rhel-6.5-test-image
PARSER_IMAGE_FILE="${PARSER_IMAGE_NAME}.img"
PARSER_IMAGE_FORMAT=qcow2

PARSER_VM_FLAVOR=m1.tiny

PARSER_USER=parser
PARSER_PASSWORD=parser
PARSER_EMAIL='shang.xiaodong@zte.com.cn'
PARSER_PROJECT=parser
PARSER_TENANT=${PARSER_PROJECT}

PARSER_ROLE=admin

PARSER_STACK_NAME=vRNC_Stack

# VRNC_INPUT_TEMPLATE_FILE=../tosca2heat/tosca-parser/toscaparser/extensions/nfv/tests/data/vRNC/Definitions/vRNC.yaml
VRNC_INPUT_TEMPLATE_FILE=../tosca2heat/heat-translator/translator/tests/data/test_tosca_nfv_sample.yaml

VRNC_OUTPUT_TEMPLATE_FILE=./vRNC_Hot_Template.yaml

download_parser_image() {
    [ -e "${PARSER_IMAGE_URL_FILE}" ] && {
        echo "  Image ${PARSER_IMAGE_URL_FILE} has bee cached, needn't download again."
        cp ${PARSER_IMAGE_URL_FILE} ${PARSER_IMAGE_FILE}
        return 0
    }

    echo ""
    echo "  Download image ${PARSER_IMAGE_URL_FILE}..."
    wget ${PARSER_IMAGE_URL} -o ${PARSER_IMAGE_FILE}
}

register_parser_image() {
    openstack ${debug} image list | grep -qwo "${PARSER_IMAGE_NAME}" && {
        echo "  Image ${PARSER_IMAGE_NAME} has bee registed, needn't registe again."
        return 0
    }

    echo ""
    echo "  Registe image ${PARSER_IMAGE_NAME}..."
    openstack ${debug} image create "${PARSER_IMAGE_NAME}" \
                           --public \
                           --disk-format ${PARSER_IMAGE_FORMAT} \
                           --container-format bare \
                           --file ${PARSER_IMAGE_FILE}
}

create_parser_user_and_project() {

    # 1. create parser project
    openstack ${debug} project list | grep -qwo "${PARSER_PROJECT}" && {
        echo "  Project ${PARSER_PROJECT} exist, doesn't create agian."
    } || {
        openstack  ${debug} project create ${PARSER_PROJECT} \
            --description "Project for parser test"
        echo "  Create project ${PARSER_PROJECT} successful."
    }

    # 2. create parser user.
    openstack ${debug} user list | grep -qwo ${PARSER_USER} && {
        echo "  User ${PARSER_USER} exist, doesn't create again."
    } || {
        openstack ${debug} user create ${PARSER_USER} --password ${PARSER_PASSWORD} \
            --project ${PARSER_PROJECT} --email ${PARSER_EMAIL}
        echo "  Create user ${PARSER_USER} successful."
    }

    # 3. grant role for parser user
    openstack ${debug} user role list ${PARSER_USER} --project ${PARSER_PROJECT} \
    | grep -qow ${PARSER_ROLE} && {
        echo "  User ${PARSER_USER} has role ${PARSER_ROLE} in project ${PARSER_PROJECT}, doesn't create."
    } || {
        openstack ${debug} role add ${PARSER_ROLE} --user ${PARSER_USER} \
                           --project ${PARSER_PROJECT}
        echo "  Grant user ${PARSER_USER} the role ${PARSER_ROLE} in project ${PARSER_PROJECT} successful."
    }

}

change_env_to_parser_user_project() {

    export OS_USERNAME=${PARSER_USER}
    export OS_PASSWORD=${PARSER_PASSWORD}
    export OS_PROJECT_NAME=${PARSER_PROJECT}
    export OS_TENANT_NAME=${PARSER_TENANT}

}

translator_and_deploy_vRNC() {

    (
        # 1. Delete parser stack ${PARSER_STACK_NAME}, use admin user in admin project
        openstack ${debug} stack list | grep -qow ${PARSER_STACK_NAME} && {
            echo "  Stack ${PARSER_STACK_NAME} exist, delete it first."
            openstack stack delete --yes --wait ${PARSER_STACK_NAME}
        }
        # 2. Switch env to parser project temporally
        echo "  Switch openstack env to parser project"
        change_env_to_parser_user_project

        # 3. Translator yaml
        echo "  Translator use parser:"
        echo "    1. Input  file: ${VRNC_INPUT_TEMPLATE_FILE}"
        echo "    2. Output file: ${VRNC_OUTPUT_TEMPLATE_FILE}"
        heat-translator --template-type tosca --template-file ${VRNC_INPUT_TEMPLATE_FILE} \
            --output-file ${VRNC_OUTPUT_TEMPLATE_FILE}

        # 4. deploy vRNC
        echo "  Deploy stack..."
        [[ "${PARSER_CI_DEBUG}" == "true" ]] && debug="--debug" || debug=""
        openstack ${debug} stack create --timeout 30 --wait --enable-rollback \
                                        -t ${VRNC_OUTPUT_TEMPLATE_FILE} ${PARSER_STACK_NAME}

        # 5. Validate the deploy result.
        echo "  Checking the result of deployment..."
        openstack ${debug} stack show ${PARSER_STACK_NAME} | grep -qow "CREATE_COMPLETE" && {
            echo "    Check the result of deployment successfully."
        } || {
            echo "    Check the result of deployment unsuccessfully."
        }
    )

}

reset_parser_test() {

    set +e

    echo "  Clean-up the environment..."
    # 1. Delete resource created by parser user
    (
        # 1). Switch env to parser project temporally
        change_env_to_parser_user_project

        # 2). Delete the stack ${PARSER_STACK_NAME}
        openstack ${debug} stack list | grep -qow ${PARSER_STACK_NAME} && {
            echo "    Stack ${PARSER_STACK_NAME} has been created, delete it after test."
            openstack ${debug} stack delete --yes --wait ${PARSER_STACK_NAME}
        }

        # 3). Delete hot tmp file ${VRNC_OUTPUT_TEMPLATE_FILE}
        [ -e ${VRNC_OUTPUT_TEMPLATE_FILE} -a ${PARSER_CI_DEBUG} != "true" ] && {
            echo "    Delete hot temp file ${VRNC_OUTPUT_TEMPLATE_FILE} after test."
            rm -fr ${VRNC_OUTPUT_TEMPLATE_FILE}
        }

        # 4). Delete tmp image ${PARSER_IMAGE_FILE}
        [[ -e ${PARSER_IMAGE_FILE} ]] && {
            echo "    Delete local image file ${PARSER_IMAGE_FILE} after test."
            rm -fr ${PARSER_IMAGE_FILE}
        }

        # 5). Delete tmp image ${PARSER_IMAGE_URL_FILE}
        [ -e ${PARSER_IMAGE_URL_FILE} -a ${PARSER_CI_DEBUG} != "true" ] && {
            echo "    Delete local URL image file ${PARSER_IMAGE_URL_FILE} after test."
            rm -fr ${PARSER_IMAGE_URL_FILE}
        }

        # 6). Delete image from openstack
        parser_image_id=$(openstack ${debug} image list | grep -w "${PARSER_IMAGE_NAME}" | awk '{print $2}')
        [[ -n "${parser_image_id}" ]] && openstack image delete "${parser_image_id}"

        sleep 3
    )

    # 2. Delete role, user and project
    echo "    Delete user ${PARSER_USER}'s role from project ${PARSER_PROJECT}"
    openstack ${debug} user role list "${PARSER_USER}" --project "${PARSER_PROJECT}" \
    | grep -qow "${PARSER_ROLE}" && {
        openstack ${debug} role remove "${PARSER_ROLE}" --user "${PARSER_USER}" \
                              --project "${PARSER_PROJECT}"
    }

    echo "    Delete user ${PARSER_USER}"
    openstack ${debug} user list | grep -qow "${PARSER_USER}" && {
        openstack user delete "${PARSER_USER}"
    }

    echo "    Delete project ${PARSER_PROJECT}"
    openstack ${debug} project list | grep -qwo "${PARSER_PROJECT}" && {
        openstack project delete "${PARSER_PROJECT}"
    }

    echo ""
    echo "======================= Parser functest end =========================="
    echo ""
    echo ""

}

echo ""
echo ""
echo "======================= Parser functest begin =========================="
echo ""

trap reset_parser_test EXIT

# start syslog for loghander
service rsyslog restart

echo "|========= 1/4. Preparing VM image for parser...     =========|"
download_parser_image
register_parser_image

echo ""
echo "|========= 2/4. Creating test user for parser...     =========|"
create_parser_user_and_project

echo ""
echo "|========= 3/4. Parse -> translate -> deploy vRNC... =========|"
translator_and_deploy_vRNC

echo ""
echo "|========= 4/4. Test ok...                           =========|"
echo ""