Remove useless vyos_vrouter output dir
[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 import pkg_resources
19 import scp
20
21 from functest.core import cloudify
22 from functest.opnfv_tests.vnf.router import vrouter_base
23 from functest.opnfv_tests.vnf.router.utilvnf import Utilvnf
24 from functest.utils import config
25 from functest.utils import functest_utils
26
27
28 __author__ = "Shuya Nakama <shuya.nakama@okinawaopenlabs.org>"
29
30
31 class CloudifyVrouter(cloudify.Cloudify):
32     # pylint: disable=too-many-instance-attributes
33     """vrouter testcase deployed with Cloudify Orchestrator."""
34
35     __logger = logging.getLogger(__name__)
36
37     filename_alt = '/home/opnfv/functest/images/vyos-1.1.8-amd64.qcow2'
38
39     flavor_alt_ram = 1024
40     flavor_alt_vcpus = 1
41     flavor_alt_disk = 3
42
43     cop_yaml = ("https://github.com/cloudify-cosmo/cloudify-openstack-plugin/"
44                 "releases/download/2.14.7/plugin.yaml")
45     cop_wgn = ("https://github.com/cloudify-cosmo/cloudify-openstack-plugin/"
46                "releases/download/2.14.7/cloudify_openstack_plugin-2.14.7-py27"
47                "-none-linux_x86_64-centos-Core.wgn")
48
49     def __init__(self, **kwargs):
50         if "case_name" not in kwargs:
51             kwargs["case_name"] = "vyos_vrouter"
52         super(CloudifyVrouter, self).__init__(**kwargs)
53
54         # Retrieve the configuration
55         try:
56             self.config = getattr(
57                 config.CONF, 'vnf_{}_config'.format(self.case_name))
58         except Exception:
59             raise Exception("VNF config file not found")
60
61         self.case_dir = pkg_resources.resource_filename(
62             'functest', 'opnfv_tests/vnf/router')
63         config_file = os.path.join(self.case_dir, self.config)
64         self.orchestrator = dict(
65             requirements=functest_utils.get_parameter_from_yaml(
66                 "orchestrator.requirements", config_file),
67         )
68         self.details['orchestrator'] = dict(
69             name=functest_utils.get_parameter_from_yaml(
70                 "orchestrator.name", config_file),
71             version=functest_utils.get_parameter_from_yaml(
72                 "orchestrator.version", config_file),
73             status='ERROR',
74             result=''
75         )
76         self.__logger.debug("Orchestrator configuration %s", self.orchestrator)
77         self.__logger.debug("name = %s", __name__)
78         self.vnf = dict(
79             descriptor=functest_utils.get_parameter_from_yaml(
80                 "vnf.descriptor", config_file),
81             inputs=functest_utils.get_parameter_from_yaml(
82                 "vnf.inputs", config_file),
83             requirements=functest_utils.get_parameter_from_yaml(
84                 "vnf.requirements", config_file)
85         )
86         self.details['vnf'] = dict(
87             descriptor_version=self.vnf['descriptor']['version'],
88             name=functest_utils.get_parameter_from_yaml(
89                 "vnf.name", config_file),
90             version=functest_utils.get_parameter_from_yaml(
91                 "vnf.version", config_file),
92         )
93         self.__logger.debug("VNF configuration: %s", self.vnf)
94
95         self.util = Utilvnf()
96         self.util.set_credentials(self.cloud)
97         credentials = {"cloud": self.cloud}
98         self.util_info = {"credentials": credentials,
99                           "vnf_data_dir": self.util.vnf_data_dir}
100
101         self.details['test_vnf'] = dict(
102             name=functest_utils.get_parameter_from_yaml(
103                 "vnf_test_suite.name", config_file),
104             version=functest_utils.get_parameter_from_yaml(
105                 "vnf_test_suite.version", config_file)
106         )
107         self.images = functest_utils.get_parameter_from_yaml(
108             "tenant_images", config_file)
109         self.__logger.info("Images needed for vrouter: %s", self.images)
110
111         self.image_alt = None
112         self.flavor_alt = None
113
114     def execute(self):
115         # pylint: disable=too-many-locals,too-many-statements
116         """
117         Deploy Cloudify Manager.
118         network, security group, fip, VM creation
119         """
120         # network creation
121         super(CloudifyVrouter, self).execute()
122         start_time = time.time()
123         self.__logger.info("Put private keypair in manager")
124         scpc = scp.SCPClient(self.ssh.get_transport())
125         scpc.put(self.key_filename, remote_path='~/cloudify_ims.pem')
126         (_, stdout, stderr) = self.ssh.exec_command(
127             "sudo docker exec cfy_manager_local "
128             "cfy plugins upload -y {} {} && "
129             "sudo docker cp ~/cloudify_ims.pem "
130             "cfy_manager_local:/etc/cloudify/ && "
131             "sudo docker exec cfy_manager_local "
132             "chmod 444 /etc/cloudify/cloudify_ims.pem && "
133             "sudo docker exec cfy_manager_local cfy status".format(
134                 self.cop_yaml, self.cop_wgn))
135         self.__logger.info("output:\n%s", stdout.read())
136         self.__logger.info("error:\n%s", stderr.read())
137
138         self.image_alt = self.publish_image_alt()
139         self.flavor_alt = self.create_flavor_alt()
140
141         duration = time.time() - start_time
142         self.details['orchestrator'].update(status='PASS', duration=duration)
143
144         self.vnf['inputs'].update(dict(
145             external_network_name=self.ext_net.name))
146         self.vnf['inputs'].update(dict(
147             target_vnf_image_id=self.image_alt.id))
148         self.vnf['inputs'].update(dict(
149             reference_vnf_image_id=self.image_alt.id))
150         self.vnf['inputs'].update(dict(
151             target_vnf_flavor_id=self.flavor_alt.id))
152         self.vnf['inputs'].update(dict(
153             reference_vnf_flavor_id=self.flavor_alt.id))
154         self.vnf['inputs'].update(dict(
155             keystone_username=self.project.user.name))
156         self.vnf['inputs'].update(dict(
157             keystone_password=self.project.password))
158         self.vnf['inputs'].update(dict(
159             keystone_tenant_name=self.project.project.name))
160         self.vnf['inputs'].update(dict(
161             keystone_user_domain_name=os.environ.get(
162                 'OS_USER_DOMAIN_NAME', 'Default')))
163         self.vnf['inputs'].update(dict(
164             keystone_project_domain_name=os.environ.get(
165                 'OS_PROJECT_DOMAIN_NAME', 'Default')))
166         self.vnf['inputs'].update(dict(
167             region=os.environ.get('OS_REGION_NAME', 'RegionOne')))
168         self.vnf['inputs'].update(dict(
169             keystone_url=self.get_public_auth_url(self.orig_cloud)))
170
171         if self.deploy_vnf() and self.test_vnf():
172             self.result = 100
173             return 0
174         self.result = 1/3 * 100
175         return 1
176
177     def deploy_vnf(self):
178         start_time = time.time()
179         self.__logger.info("Upload VNFD")
180         descriptor = self.vnf['descriptor']
181         self.util_info["cfy"] = self.cfy_client
182         self.util_info["cfy_manager_ip"] = self.fip.floating_ip_address
183         self.util_info["deployment_name"] = descriptor.get('name')
184
185         self.cfy_client.blueprints.upload(
186             descriptor.get('file_name'), descriptor.get('name'))
187
188         self.__logger.info("Create VNF Instance")
189         self.cfy_client.deployments.create(
190             descriptor.get('name'), descriptor.get('name'),
191             self.vnf.get('inputs'))
192
193         cloudify.wait_for_execution(
194             self.cfy_client, cloudify.get_execution_id(
195                 self.cfy_client, descriptor.get('name')),
196             self.__logger, timeout=7200)
197
198         self.__logger.info("Start the VNF Instance deployment")
199         execution = self.cfy_client.executions.start(
200             descriptor.get('name'), 'install')
201         # Show execution log
202         execution = cloudify.wait_for_execution(
203             self.cfy_client, execution, self.__logger)
204
205         duration = time.time() - start_time
206
207         self.__logger.info(execution)
208         if execution.status == 'terminated':
209             self.details['vnf'].update(status='PASS', duration=duration)
210             result = True
211         else:
212             self.details['vnf'].update(status='FAIL', duration=duration)
213             result = False
214         return result
215
216     def test_vnf(self):
217         start_time = time.time()
218         testing = vrouter_base.VrouterOnBoardingBase(self.util, self.util_info)
219         result, test_result_data = testing.test_vnf()
220         duration = time.time() - start_time
221         if result:
222             self.details['test_vnf'].update(
223                 status='PASS', result='OK', full_result=test_result_data,
224                 duration=duration)
225         else:
226             self.details['test_vnf'].update(
227                 status='FAIL', result='NG', full_result=test_result_data,
228                 duration=duration)
229         return True
230
231     def clean(self):
232         try:
233             dep_name = self.vnf['descriptor'].get('name')
234             # kill existing execution
235             self.__logger.info('Deleting the current deployment')
236             exec_list = self.cfy_client.executions.list()
237             for execution in exec_list:
238                 if execution['status'] == "started":
239                     try:
240                         self.cfy_client.executions.cancel(
241                             execution['id'], force=True)
242                     except Exception:  # pylint: disable=broad-except
243                         self.__logger.warn("Can't cancel the current exec")
244
245             execution = self.cfy_client.executions.start(
246                 dep_name, 'uninstall', parameters=dict(ignore_failure=True))
247
248             cloudify.wait_for_execution(
249                 self.cfy_client, execution, self.__logger)
250             self.cfy_client.deployments.delete(
251                 self.vnf['descriptor'].get('name'))
252             time.sleep(10)
253             self.cfy_client.blueprints.delete(
254                 self.vnf['descriptor'].get('name'))
255         except Exception:  # pylint: disable=broad-except
256             self.__logger.exception("Some issue during the undeployment ..")
257         if self.image_alt:
258             self.cloud.delete_image(self.image_alt)
259         if self.flavor_alt:
260             self.orig_cloud.delete_flavor(self.flavor_alt.id)
261         super(CloudifyVrouter, self).clean()