Modify TestCase constructor attributes
[functest.git] / functest / opnfv_tests / vnf / ims / cloudify_ims.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2016 Orange and others.
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 import os
11 import sys
12 import time
13
14 import requests
15 import yaml
16
17 from functest.opnfv_tests.vnf.ims.clearwater import Clearwater
18 import functest.opnfv_tests.vnf.ims.clearwater_ims_base as clearwater_ims_base
19 from functest.opnfv_tests.vnf.ims.orchestrator_cloudify import Orchestrator
20 from functest.utils.constants import CONST
21 import functest.utils.functest_logger as ft_logger
22 import functest.utils.functest_utils as ft_utils
23 import functest.utils.openstack_utils as os_utils
24
25
26 class CloudifyIms(clearwater_ims_base.ClearwaterOnBoardingBase):
27
28     def __init__(self, **kwargs):
29         if "case_name" not in kwargs:
30             kwargs["case_name"] = "cloudify_ims"
31         super(CloudifyIms, self).__init__(**kwargs)
32         self.logger = ft_logger.Logger(__name__).getLogger()
33
34         # Retrieve the configuration
35         try:
36             self.config = CONST.__getattribute__(
37                 'vnf_{}_config'.format(self.case_name))
38         except Exception:
39             raise Exception("VNF config file not found")
40
41         config_file = os.path.join(self.case_dir, self.config)
42         self.orchestrator = dict(
43             requirements=get_config("cloudify.requirements", config_file),
44             blueprint=get_config("cloudify.blueprint", config_file),
45             inputs=get_config("cloudify.inputs", config_file)
46         )
47         self.logger.debug("Orchestrator configuration: %s" % self.orchestrator)
48         self.vnf = dict(
49             blueprint=get_config("clearwater.blueprint", config_file),
50             deployment_name=get_config("clearwater.deployment_name",
51                                        config_file),
52             inputs=get_config("clearwater.inputs", config_file),
53             requirements=get_config("clearwater.requirements", config_file)
54         )
55         self.logger.debug("VNF configuration: %s" % self.vnf)
56
57         self.images = get_config("tenant_images", config_file)
58         self.logger.info("Images needed for vIMS: %s" % self.images)
59
60     def deploy_orchestrator(self, **kwargs):
61
62         self.logger.info("Additional pre-configuration steps")
63         self.neutron_client = os_utils.get_neutron_client(self.admin_creds)
64         self.glance_client = os_utils.get_glance_client(self.admin_creds)
65         self.keystone_client = os_utils.get_keystone_client(self.admin_creds)
66         self.nova_client = os_utils.get_nova_client(self.admin_creds)
67
68         # needs some images
69         self.logger.info("Upload some OS images if it doesn't exist")
70         temp_dir = os.path.join(self.data_dir, "tmp/")
71         for image_name, image_url in self.images.iteritems():
72             self.logger.info("image: %s, url: %s" % (image_name, image_url))
73             try:
74                 image_id = os_utils.get_image_id(self.glance_client,
75                                                  image_name)
76                 self.logger.debug("image_id: %s" % image_id)
77             except Exception:
78                 self.logger.error("Unexpected error: %s" % sys.exc_info()[0])
79
80             if image_id == '':
81                 self.logger.info("""%s image doesn't exist on glance repository. Try
82                 downloading this image and upload on glance !""" % image_name)
83                 image_id = download_and_add_image_on_glance(self.glance_client,
84                                                             image_name,
85                                                             image_url,
86                                                             temp_dir)
87             if image_id == '':
88                 self.step_failure(
89                     "Failed to find or upload required OS "
90                     "image for this deployment")
91         # Need to extend quota
92         self.logger.info("Update security group quota for this tenant")
93         tenant_id = os_utils.get_tenant_id(self.keystone_client,
94                                            self.tenant_name)
95         self.logger.debug("Tenant id found %s" % tenant_id)
96         if not os_utils.update_sg_quota(self.neutron_client,
97                                         tenant_id, 50, 100):
98             self.step_failure("Failed to update security group quota" +
99                               " for tenant " + self.tenant_name)
100         self.logger.debug("group quota extended")
101
102         # start the deployment of cloudify
103         public_auth_url = os_utils.get_endpoint('identity')
104
105         self.logger.debug("CFY inputs: %s" % self.orchestrator['inputs'])
106         cfy = Orchestrator(self.data_dir, self.orchestrator['inputs'])
107         self.orchestrator['object'] = cfy
108         self.logger.debug("Orchestrator object created")
109
110         self.logger.debug("Tenant name: %s" % self.tenant_name)
111
112         cfy.set_credentials(username=self.tenant_name,
113                             password=self.tenant_name,
114                             tenant_name=self.tenant_name,
115                             auth_url=public_auth_url)
116         self.logger.info("Credentials set in CFY")
117
118         # orchestrator VM flavor
119         self.logger.info("Check Flavor is available, if not create one")
120         self.logger.debug("Flavor details %s " %
121                           self.orchestrator['requirements']['ram_min'])
122         flavor_exist, flavor_id = os_utils.get_or_create_flavor(
123             "m1.large",
124             self.orchestrator['requirements']['ram_min'],
125             '50',
126             '2',
127             public=True)
128         self.logger.debug("Flavor id: %s" % flavor_id)
129
130         if not flavor_id:
131             self.logger.info("Available flavors are: ")
132             self.logger.info(self.nova_client.flavor.list())
133             self.step_failure("Failed to find required flavor"
134                               "for this deployment")
135         cfy.set_flavor_id(flavor_id)
136         self.logger.debug("Flavor OK")
137
138         # orchestrator VM image
139         self.logger.debug("Orchestrator image")
140         if 'os_image' in self.orchestrator['requirements'].keys():
141             image_id = os_utils.get_image_id(
142                 self.glance_client,
143                 self.orchestrator['requirements']['os_image'])
144             self.logger.debug("Orchestrator image id: %s" % image_id)
145             if image_id == '':
146                 self.logger.error("CFY image not found")
147                 self.step_failure("Failed to find required OS image"
148                                   " for cloudify manager")
149         else:
150             self.step_failure("Failed to find required OS image"
151                               " for cloudify manager")
152
153         cfy.set_image_id(image_id)
154         self.logger.debug("Orchestrator image set")
155
156         self.logger.debug("Get External network")
157         ext_net = os_utils.get_external_net(self.neutron_client)
158         self.logger.debug("External network: %s" % ext_net)
159         if not ext_net:
160             self.step_failure("Failed to get external network")
161
162         cfy.set_external_network_name(ext_net)
163         self.logger.debug("CFY External network set")
164
165         self.logger.debug("get resolvconf")
166         ns = ft_utils.get_resolvconf_ns()
167         if ns:
168             cfy.set_nameservers(ns)
169             self.logger.debug("Resolvconf set")
170
171         self.logger.info("Prepare virtualenv for cloudify-cli")
172         venv_scrit_dir = os.path.join(self.case_dir, "create_venv.sh")
173         cmd = "chmod +x " + venv_scrit_dir
174         ft_utils.execute_command(cmd)
175         time.sleep(3)
176         cmd = venv_scrit_dir + " " + self.data_dir
177         ft_utils.execute_command(cmd)
178
179         cfy.download_manager_blueprint(
180             self.orchestrator['blueprint']['url'],
181             self.orchestrator['blueprint']['branch'])
182
183         error = cfy.deploy_manager()
184         if error:
185             self.logger.error(error)
186             return {'status': 'FAIL', 'result': error}
187         else:
188             return {'status': 'PASS', 'result': ''}
189
190     def deploy_vnf(self):
191         cw = Clearwater(self.vnf['inputs'], self.orchestrator['object'],
192                         self.logger)
193         self.vnf['object'] = cw
194
195         self.logger.info("Collect flavor id for all clearwater vm")
196         flavor_exist, flavor_id = os_utils.get_or_create_flavor(
197             "m1.small",
198             self.vnf['requirements']['ram_min'],
199             '30',
200             '1',
201             public=True)
202         self.logger.debug("Flavor id: %s" % flavor_id)
203         if not flavor_id:
204             self.logger.info("Available flavors are: ")
205             self.logger.info(self.nova_client.flavor.list())
206             self.step_failure("Failed to find required flavor"
207                               " for this deployment")
208
209         cw.set_flavor_id(flavor_id)
210
211         # VMs image
212         if 'os_image' in self.vnf['requirements'].keys():
213             image_id = os_utils.get_image_id(
214                 self.glance_client, self.vnf['requirements']['os_image'])
215             if image_id == '':
216                 self.step_failure("Failed to find required OS image"
217                                   " for clearwater VMs")
218         else:
219             self.step_failure("Failed to find required OS image"
220                               " for clearwater VMs")
221
222         cw.set_image_id(image_id)
223
224         ext_net = os_utils.get_external_net(self.neutron_client)
225         if not ext_net:
226             self.step_failure("Failed to get external network")
227
228         cw.set_external_network_name(ext_net)
229
230         error = cw.deploy_vnf(self.vnf['blueprint'])
231         if error:
232             self.logger.error(error)
233             return {'status': 'FAIL', 'result': error}
234         else:
235             return {'status': 'PASS', 'result': ''}
236
237     def test_vnf(self):
238         script = "source {0}venv_cloudify/bin/activate; "
239         script += "cd {0}; "
240         script += "cfy status | grep -Eo \"([0-9]{{1,3}}\.){{3}}[0-9]{{1,3}}\""
241         cmd = "/bin/bash -c '" + script.format(self.data_dir) + "'"
242
243         try:
244             self.logger.debug("Trying to get clearwater manager IP ... ")
245             mgr_ip = os.popen(cmd).read()
246             mgr_ip = mgr_ip.splitlines()[0]
247         except Exception:
248             self.step_failure("Unable to retrieve the IP of the "
249                               "cloudify manager server !")
250
251         self.logger.info('Cloudify Manager: %s', mgr_ip)
252         api_url = 'http://{0}/api/v2/deployments/{1}/outputs'.format(
253                   mgr_ip, self.vnf['deployment_name'])
254         dep_outputs = requests.get(api_url)
255         self.logger.info(api_url)
256         outputs = dep_outputs.json()['outputs']
257         self.logger.info("Deployment outputs: %s", outputs)
258         dns_ip = outputs['dns_ip']
259         ellis_ip = outputs['ellis_ip']
260         self.config_ellis(ellis_ip)
261
262         if dns_ip != "":
263             vims_test_result = self.run_clearwater_live_test(
264                 dns_ip=dns_ip,
265                 public_domain=self.inputs["public_domain"])
266             if vims_test_result != '':
267                 return {'status': 'PASS', 'result': vims_test_result}
268             else:
269                 return {'status': 'FAIL', 'result': ''}
270
271     def clean(self):
272         self.vnf['object'].undeploy_vnf()
273         self.orchestrator['object'].undeploy_manager()
274         super(CloudifyIms, self).clean()
275
276     def main(self, **kwargs):
277         self.logger.info("Cloudify IMS VNF onboarding test starting")
278         self.execute()
279         self.logger.info("Cloudify IMS VNF onboarding test executed")
280         if self.criteria is "PASS":
281             return self.EX_OK
282         else:
283             return self.EX_RUN_ERROR
284
285     def run(self):
286         kwargs = {}
287         return self.main(**kwargs)
288
289
290 # ----------------------------------------------------------
291 #
292 #               YAML UTILS
293 #
294 # -----------------------------------------------------------
295 def get_config(parameter, file):
296     """
297     Returns the value of a given parameter in file.yaml
298     parameter must be given in string format with dots
299     Example: general.openstack.image_name
300     """
301     with open(file) as f:
302         file_yaml = yaml.safe_load(f)
303     f.close()
304     value = file_yaml
305     for element in parameter.split("."):
306         value = value.get(element)
307         if value is None:
308             raise ValueError("The parameter %s is not defined in"
309                              " reporting.yaml" % parameter)
310     return value
311
312
313 def download_and_add_image_on_glance(glance, image_name, image_url, data_dir):
314     dest_path = data_dir
315     if not os.path.exists(dest_path):
316         os.makedirs(dest_path)
317     file_name = image_url.rsplit('/')[-1]
318     if not ft_utils.download_url(image_url, dest_path):
319         return False
320
321     image = os_utils.create_glance_image(
322         glance, image_name, dest_path + file_name)
323     if not image:
324         return False
325
326     return image