79e8d801ab6ea2b0079866056c347b5f85b481ed
[functest.git] / functest / opnfv_tests / vnf / router / cloudify_vrouter.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2017 Okinawa Open Laboratory 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 # pylint: disable=missing-docstring
11
12 """vrouter testcase implementation."""
13
14 import logging
15 import os
16 import time
17
18 from cloudify_rest_client.executions import Execution
19 import pkg_resources
20 import scp
21
22 from functest.core import cloudify
23 from functest.opnfv_tests.vnf.router import vrouter_base
24 from functest.opnfv_tests.vnf.router.utilvnf import Utilvnf
25 from functest.utils import config
26 from functest.utils import functest_utils
27
28
29 __author__ = "Shuya Nakama <shuya.nakama@okinawaopenlabs.org>"
30
31
32 class CloudifyVrouter(cloudify.Cloudify):
33     # pylint: disable=too-many-instance-attributes
34     """vrouter testcase deployed with Cloudify Orchestrator."""
35
36     __logger = logging.getLogger(__name__)
37
38     filename_alt = '/home/opnfv/functest/images/vyos-1.1.7.img'
39
40     flavor_alt_ram = 2048
41     flavor_alt_vcpus = 1
42     flavor_alt_disk = 50
43
44     def __init__(self, **kwargs):
45         if "case_name" not in kwargs:
46             kwargs["case_name"] = "vyos_vrouter"
47         super(CloudifyVrouter, self).__init__(**kwargs)
48
49         # Retrieve the configuration
50         try:
51             self.config = getattr(
52                 config.CONF, 'vnf_{}_config'.format(self.case_name))
53         except Exception:
54             raise Exception("VNF config file not found")
55
56         self.case_dir = pkg_resources.resource_filename(
57             'functest', 'opnfv_tests/vnf/router')
58         config_file = os.path.join(self.case_dir, self.config)
59         self.orchestrator = dict(
60             requirements=functest_utils.get_parameter_from_yaml(
61                 "orchestrator.requirements", config_file),
62         )
63         self.details['orchestrator'] = dict(
64             name=functest_utils.get_parameter_from_yaml(
65                 "orchestrator.name", config_file),
66             version=functest_utils.get_parameter_from_yaml(
67                 "orchestrator.version", config_file),
68             status='ERROR',
69             result=''
70         )
71         self.__logger.debug("Orchestrator configuration %s", self.orchestrator)
72         self.__logger.debug("name = %s", __name__)
73         self.vnf = dict(
74             descriptor=functest_utils.get_parameter_from_yaml(
75                 "vnf.descriptor", config_file),
76             inputs=functest_utils.get_parameter_from_yaml(
77                 "vnf.inputs", config_file),
78             requirements=functest_utils.get_parameter_from_yaml(
79                 "vnf.requirements", config_file)
80         )
81         self.details['vnf'] = dict(
82             descriptor_version=self.vnf['descriptor']['version'],
83             name=functest_utils.get_parameter_from_yaml(
84                 "vnf.name", config_file),
85             version=functest_utils.get_parameter_from_yaml(
86                 "vnf.version", config_file),
87         )
88         self.__logger.debug("VNF configuration: %s", self.vnf)
89
90         self.util = Utilvnf()
91         self.util.set_credentials(self.cloud)
92         credentials = {"cloud": self.cloud}
93         self.util_info = {"credentials": credentials,
94                           "vnf_data_dir": self.util.vnf_data_dir}
95
96         self.details['test_vnf'] = dict(
97             name=functest_utils.get_parameter_from_yaml(
98                 "vnf_test_suite.name", config_file),
99             version=functest_utils.get_parameter_from_yaml(
100                 "vnf_test_suite.version", config_file)
101         )
102         self.images = functest_utils.get_parameter_from_yaml(
103             "tenant_images", config_file)
104         self.__logger.info("Images needed for vrouter: %s", self.images)
105
106         self.image_alt = None
107         self.flavor_alt = None
108
109     def execute(self):
110         # pylint: disable=too-many-locals,too-many-statements
111         """
112         Deploy Cloudify Manager.
113         network, security group, fip, VM creation
114         """
115         # network creation
116         super(CloudifyVrouter, self).execute()
117         start_time = time.time()
118         self.__logger.info("Put private keypair in manager")
119         scpc = scp.SCPClient(self.ssh.get_transport())
120         scpc.put(self.key_filename, remote_path='~/cloudify_ims.pem')
121         (_, stdout, stderr) = self.ssh.exec_command(
122             "sudo cp ~/cloudify_ims.pem /etc/cloudify/ && "
123             "sudo chmod 444 /etc/cloudify/cloudify_ims.pem && "
124             "sudo yum install -y gcc python-devel python-cmd2 && "
125             "cfy status")
126         self.__logger.info("output:\n%s", stdout.read())
127         self.__logger.info("error:\n%s", stderr.read())
128
129         self.image_alt = self.publish_image_alt()
130         self.flavor_alt = self.create_flavor_alt()
131
132         duration = time.time() - start_time
133         self.details['orchestrator'].update(status='PASS', duration=duration)
134
135         self.vnf['inputs'].update(dict(
136             external_network_name=self.ext_net.name))
137         self.vnf['inputs'].update(dict(
138             target_vnf_image_id=self.image_alt.id))
139         self.vnf['inputs'].update(dict(
140             reference_vnf_image_id=self.image_alt.id))
141         self.vnf['inputs'].update(dict(
142             target_vnf_flavor_id=self.flavor_alt.id))
143         self.vnf['inputs'].update(dict(
144             reference_vnf_flavor_id=self.flavor_alt.id))
145         self.vnf['inputs'].update(dict(
146             keystone_username=self.project.user.name))
147         self.vnf['inputs'].update(dict(
148             keystone_password=self.project.password))
149         self.vnf['inputs'].update(dict(
150             keystone_tenant_name=self.project.project.name))
151         self.vnf['inputs'].update(dict(
152             keystone_user_domain_name=os.environ.get(
153                 'OS_USER_DOMAIN_NAME', 'Default')))
154         self.vnf['inputs'].update(dict(
155             keystone_project_domain_name=os.environ.get(
156                 'OS_PROJECT_DOMAIN_NAME', 'Default')))
157         self.vnf['inputs'].update(dict(
158             region=os.environ.get('OS_REGION_NAME', 'RegionOne')))
159         self.vnf['inputs'].update(dict(
160             keystone_url=self.get_public_auth_url(self.orig_cloud)))
161
162         if (self.deploy_vnf() and self.test_vnf()):
163             self.result = 100
164             return 0
165         self.result = 1/3 * 100
166         return 1
167
168     def deploy_vnf(self):
169         start_time = time.time()
170
171         self.cloud.create_security_group_rule(
172             'default', port_range_min=22, port_range_max=22,
173             protocol='tcp', direction='ingress')
174
175         self.__logger.info("Upload VNFD")
176         descriptor = self.vnf['descriptor']
177         self.util_info["cfy"] = self.cfy_client
178         self.util_info["cfy_manager_ip"] = self.fip.floating_ip_address
179         self.util_info["deployment_name"] = descriptor.get('name')
180
181         self.cfy_client.blueprints.upload(
182             descriptor.get('file_name'), descriptor.get('name'))
183
184         self.__logger.info("Create VNF Instance")
185         self.cfy_client.deployments.create(
186             descriptor.get('name'), descriptor.get('name'),
187             self.vnf.get('inputs'))
188
189         wait_for_execution(
190             self.cfy_client, get_execution_id(
191                 self.cfy_client, descriptor.get('name')),
192             self.__logger, timeout=7200)
193
194         self.__logger.info("Start the VNF Instance deployment")
195         execution = self.cfy_client.executions.start(
196             descriptor.get('name'), 'install')
197         # Show execution log
198         execution = wait_for_execution(
199             self.cfy_client, execution, self.__logger)
200
201         duration = time.time() - start_time
202
203         self.__logger.info(execution)
204         if execution.status == 'terminated':
205             self.details['vnf'].update(status='PASS', duration=duration)
206             result = True
207         else:
208             self.details['vnf'].update(status='FAIL', duration=duration)
209             result = False
210         return result
211
212     def test_vnf(self):
213         start_time = time.time()
214         testing = vrouter_base.VrouterOnBoardingBase(
215             self.case_name, self.util, self.util_info)
216         result, test_result_data = testing.test_vnf()
217         duration = time.time() - start_time
218         if result:
219             self.details['test_vnf'].update(
220                 status='PASS', result='OK', full_result=test_result_data,
221                 duration=duration)
222         else:
223             self.details['test_vnf'].update(
224                 status='FAIL', result='NG', full_result=test_result_data,
225                 duration=duration)
226         return True
227
228     def clean(self):
229         try:
230             dep_name = self.vnf['descriptor'].get('name')
231             # kill existing execution
232             self.__logger.info('Deleting the current deployment')
233             exec_list = self.cfy_client.executions.list()
234             for execution in exec_list:
235                 if execution['status'] == "started":
236                     try:
237                         self.cfy_client.executions.cancel(
238                             execution['id'], force=True)
239                     except Exception:  # pylint: disable=broad-except
240                         self.__logger.warn("Can't cancel the current exec")
241
242             execution = self.cfy_client.executions.start(
243                 dep_name, 'uninstall', parameters=dict(ignore_failure=True))
244
245             wait_for_execution(self.cfy_client, execution, self.__logger)
246             self.cfy_client.deployments.delete(
247                 self.vnf['descriptor'].get('name'))
248             self.cfy_client.blueprints.delete(
249                 self.vnf['descriptor'].get('name'))
250         except Exception:  # pylint: disable=broad-except
251             self.__logger.exception("Some issue during the undeployment ..")
252
253         super(CloudifyVrouter, self).clean()
254         if self.image_alt:
255             self.cloud.delete_image(self.image_alt)
256         if self.flavor_alt:
257             self.orig_cloud.delete_flavor(self.flavor_alt.id)
258
259
260 def wait_for_execution(client, execution, logger, timeout=7200, ):
261     """Wait for a workflow execution on Cloudify Manager."""
262     # if execution already ended - return without waiting
263     if execution.status in Execution.END_STATES:
264         return execution
265
266     if timeout is not None:
267         deadline = time.time() + timeout
268
269     # Poll for execution status and execution logs, until execution ends
270     # and we receive an event of type in WORKFLOW_END_TYPES
271     offset = 0
272     batch_size = 50
273     event_list = []
274     execution_ended = False
275     while True:
276         event_list = client.events.list(
277             execution_id=execution.id, _offset=offset, _size=batch_size,
278             include_logs=True, sort='@timestamp').items
279
280         offset = offset + len(event_list)
281         for event in event_list:
282             logger.debug(event.get('message'))
283
284         if timeout is not None:
285             if time.time() > deadline:
286                 raise RuntimeError(
287                     'execution of operation {0} for deployment {1} '
288                     'timed out'.format(execution.workflow_id,
289                                        execution.deployment_id))
290             else:
291                 # update the remaining timeout
292                 timeout = deadline - time.time()
293
294         if not execution_ended:
295             execution = client.executions.get(execution.id)
296             execution_ended = execution.status in Execution.END_STATES
297
298         if execution_ended:
299             break
300
301         time.sleep(5)
302
303     return execution
304
305
306 def get_execution_id(client, deployment_id):
307     """
308     Get the execution id of a env preparation.
309     network, security group, fip, VM creation
310     """
311     executions = client.executions.list(deployment_id=deployment_id)
312     for execution in executions:
313         if execution.workflow_id == 'create_deployment_environment':
314             return execution
315     raise RuntimeError('Failed to get create_deployment_environment '
316                        'workflow execution.'
317                        'Available executions: {0}'.format(executions))