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