aboutsummaryrefslogtreecommitdiffstats
path: root/functest/opnfv_tests/vnf/ims/cloudify_ims.py
blob: e584519b7787eab88f41610f6bf466dfa3f04da0 (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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/env python

# Copyright (c) 2016 Orange and others.
#
# 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

import json
import os
import requests
import subprocess
import time

import functest.core.vnf_base as vnf_base
import functest.utils.functest_logger as ft_logger
import functest.utils.functest_utils as ft_utils
import functest.utils.openstack_utils as os_utils

from clearwater import Clearwater
from functest.utils.constants import CONST
from orchestrator_cloudify import Orchestrator


class ImsVnf(vnf_base.VnfOnBoardingBase):

    def __init__(self, project='functest', case='', repo='', cmd=''):
        super(ImsVnf, self).__init__(project, case, repo, cmd)
        self.logger = ft_logger.Logger("vIMS").getLogger()
        self.case_dir = os.path.join(CONST.functest_test, 'vnf/ims/')
        self.data_dir = CONST.dir_vIMS_data
        self.test_dir = CONST.dir_repo_vims_test

        self.orchestrator = dict(
            requirements=CONST.cloudify_requirements,
            blueprint=CONST.cloudify_blueprint,
            inputs=CONST.cloudify_inputs
        )

        self.vnf = dict(
            blueprint=CONST.clearwater_blueprint,
            deployment_name=CONST.clearwater_deployment_name,
            inputs=CONST.clearwater_inputs,
            requirements=CONST.clearwater_requirements
        )

        # vIMS Data directory creation
        if not os.path.exists(self.data_dir):
            os.makedirs(self.data_dir)

    def deploy_orchestrator(self, **kwargs):
        public_auth_url = os_utils.get_endpoint('identity')

        cfy = Orchestrator(self.data_dir, self.orchestrator.inputs)
        self.orchestrator.object = cfy

        if 'tenant_name' in self.creds.keys():
            tenant_name = self.creds['tenant_name']
        elif 'project_name' in self.creds.keys():
            tenant_name = self.creds['project_name']

        cfy.set_credentials(username=self.creds['username'],
                            password=self.creds['password'],
                            tenant_name=tenant_name,
                            auth_url=public_auth_url)

        # orchestrator VM flavor
        flavor_id = self.get_flavor("m1.large", self.orchestrator.requirements)
        if not flavor_id:
            self.logger.info("Available flavors are: ")
            self.pMsg(self.nova_client.flavor.list())
            self.step_failure("Failed to find required flavor"
                              "for this deployment")
        cfy.set_flavor_id(flavor_id)

        # orchestrator VM image
        if 'os_image' in self.orchestrator.requirements.keys():
            image_id = os_utils.get_image_id(
                self.glance_client, self.orchestrator.requirements['os_image'])
            if image_id == '':
                self.step_failure("Failed to find required OS image"
                                  " for cloudify manager")
        else:
            self.step_failure("Failed to find required OS image"
                              " for cloudify manager")

        cfy.set_image_id(image_id)

        ext_net = os_utils.get_external_net(self.neutron_client)
        if not ext_net:
            self.step_failure("Failed to get external network")

        cfy.set_external_network_name(ext_net)

        ns = ft_utils.get_resolvconf_ns()
        if ns:
            cfy.set_nameservers(ns)

        if 'compute' in self.nova_client.client.services_url:
            cfy.set_nova_url(self.nova_client.client.services_url['compute'])
        if self.neutron_client.httpclient.endpoint_url is not None:
            cfy.set_neutron_url(self.neutron_client.httpclient.endpoint_url)

        self.logger.info("Prepare virtualenv for cloudify-cli")
        cmd = "chmod +x " + self.case_dir + "create_venv.sh"
        ft_utils.execute_command(cmd)
        time.sleep(3)
        cmd = self.case_dir + "create_venv.sh " + self.data_dir
        ft_utils.execute_command(cmd)

        cfy.download_manager_blueprint(self.orchestrator.blueprint['url'],
                                       self.orchestrator.blueprint['branch'])

        cfy.deploy_manager()
        return {'status': 'PASS', 'result': ''}

    def deploy_vnf(self):
        cw = Clearwater(self.vnf.inputs, self.orchestrator.object, self.logger)
        self.vnf.object = cw

        self.logger.info("Collect flavor id for all clearwater vm")
        flavor_id = self.get_flavor("m1.small", self.vnf.requirements)
        if not flavor_id:
            self.logger.info("Available flavors are: ")
            self.pMsg(self.nova_client.flavor.list())
            self.step_failure("Failed to find required flavor"
                              " for this deployment")

        cw.set_flavor_id(flavor_id)

        # VMs image
        if 'os_image' in self.vnf.requirements.keys():
            image_id = os_utils.get_image_id(
                self.glance_client, self.vnf.requirements['os_image'])
            if image_id == '':
                self.step_failure("Failed to find required OS image"
                                  " for clearwater VMs")
        else:
            self.step_failure("Failed to find required OS image"
                              " for clearwater VMs")

        cw.set_image_id(image_id)

        ext_net = os_utils.get_external_net(self.neutron_client)
        if not ext_net:
            self.step_failure("Failed to get external network")

        cw.set_external_network_name(ext_net)

        cw.deploy_vnf()
        return {'status': 'PASS', 'result': ''}

    def test_vnf(self):
        script = "source {0}venv_cloudify/bin/activate; "
        script += "cd {0}; "
        script += "cfy status | grep -Eo \"([0-9]{{1,3}}\.){{3}}[0-9]{{1,3}}\""
        cmd = "/bin/bash -c '" + script.format(self.data_dir) + "'"

        try:
            self.logger.debug("Trying to get clearwater manager IP ... ")
            mgr_ip = os.popen(cmd).read()
            mgr_ip = mgr_ip.splitlines()[0]
        except:
            self.step_failure("Unable to retrieve the IP of the "
                              "cloudify manager server !")

        api_url = "http://" + mgr_ip + "/api/v2"
        dep_outputs = requests.get(api_url + "/deployments/" +
                                   self.vnf.deployment_name + "/outputs")
        dns_ip = dep_outputs.json()['outputs']['dns_ip']
        ellis_ip = dep_outputs.json()['outputs']['ellis_ip']

        ellis_url = "http://" + ellis_ip + "/"
        url = ellis_url + "accounts"

        params = {"password": "functest",
                  "full_name": "opnfv functest user",
                  "email": "functest@opnfv.fr",
                  "signup_code": "secret"}

        rq = requests.post(url, data=params)
        i = 20
        while rq.status_code != 201 and i > 0:
            rq = requests.post(url, data=params)
            i = i - 1
            time.sleep(10)

        if rq.status_code == 201:
            url = ellis_url + "session"
            rq = requests.post(url, data=params)
            cookies = rq.cookies

        url = ellis_url + "accounts/" + params['email'] + "/numbers"
        if cookies != "":
            rq = requests.post(url, cookies=cookies)
            i = 24
            while rq.status_code != 200 and i > 0:
                rq = requests.post(url, cookies=cookies)
                i = i - 1
                time.sleep(25)

        if rq.status_code != 200:
            self.step_failure("Unable to create a number: %s"
                              % rq.json()['reason'])

        nameservers = ft_utils.get_resolvconf_ns()
        resolvconf = ""
        for ns in nameservers:
            resolvconf += "\nnameserver " + ns

        if dns_ip != "":
            script = ('echo -e "nameserver ' + dns_ip + resolvconf +
                      '" > /etc/resolv.conf; ')
            script += 'source /etc/profile.d/rvm.sh; '
            script += 'cd {0}; '
            script += ('rake test[{1}] SIGNUP_CODE="secret"')

            cmd = ("/bin/bash -c '" +
                   script.format(self.data_dir, self.inputs["public_domain"]) +
                   "'")
            output_file = "output.txt"
            f = open(output_file, 'w+')
            subprocess.call(cmd, shell=True, stdout=f,
                            stderr=subprocess.STDOUT)
            f.close()

            f = open(output_file, 'r')
            result = f.read()
            if result != "":
                self.logger.debug(result)

            vims_test_result = ""
            tempFile = os.path.join(self.test_dir, "temp.json")
            try:
                self.logger.debug("Trying to load test results")
                with open(tempFile) as f:
                    vims_test_result = json.load(f)
                f.close()
            except:
                self.logger.error("Unable to retrieve test results")

            try:
                os.remove(tempFile)
            except:
                self.logger.error("Deleting file failed")

            if vims_test_result != '':
                return {'status': 'PASS', 'result': vims_test_result}
            else:
                return {'status': 'FAIL', 'result': ''}

    def clean(self):
        self.vnf.object.undeploy_vnf()
        self.orchestrator.object.undeploy_manager()
        super(ImsVnf, self).clean()

    def get_flavor(self, flavor_name, requirements):
        try:
            flavor_id = os_utils.get_flavor_id(self.nova_client, flavor_name)
            if 'ram_min' in requirements.keys():
                flavor_id = os_utils.get_flavor_id_by_ram_range(
                    self.nova_client, requirements['ram_min'], 7500)

            if flavor_id == '':
                self.logger.error(
                    "Failed to find %s flavor. "
                    "Try with ram range default requirement !" % flavor_name)
                flavor_id = os_utils.get_flavor_id_by_ram_range(
                                    self.nova_client,
                                    4000, 10000)
            return flavor_id
        except:
            self.logger.error("Flavor '%s' not found." % self.flavor_name)
            self.logger.info("Available flavors are: ")
            self.pMsg(self.nova_client.flavor.list())
            return None