82f57ca7b8eac82cf5be6989b7f38a83394aaeb6
[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 env
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     check_console_loop = 12
44
45     cop_yaml = ("https://github.com/cloudify-cosmo/cloudify-openstack-plugin/"
46                 "releases/download/2.14.7/plugin.yaml")
47     cop_wgn = ("https://github.com/cloudify-cosmo/cloudify-openstack-plugin/"
48                "releases/download/2.14.7/cloudify_openstack_plugin-2.14.7-py27"
49                "-none-linux_x86_64-centos-Core.wgn")
50
51     def __init__(self, **kwargs):
52         if "case_name" not in kwargs:
53             kwargs["case_name"] = "vyos_vrouter"
54         super(CloudifyVrouter, self).__init__(**kwargs)
55
56         # Retrieve the configuration
57         try:
58             self.config = getattr(
59                 config.CONF, 'vnf_{}_config'.format(self.case_name))
60         except Exception:
61             raise Exception("VNF config file not found")
62
63         self.case_dir = pkg_resources.resource_filename(
64             'functest', 'opnfv_tests/vnf/router')
65         config_file = os.path.join(self.case_dir, self.config)
66         self.orchestrator = dict(
67             requirements=functest_utils.get_parameter_from_yaml(
68                 "orchestrator.requirements", config_file),
69         )
70         self.details['orchestrator'] = dict(
71             name=functest_utils.get_parameter_from_yaml(
72                 "orchestrator.name", config_file),
73             version=functest_utils.get_parameter_from_yaml(
74                 "orchestrator.version", config_file),
75             status='ERROR',
76             result=''
77         )
78         self.__logger.debug("Orchestrator configuration %s", self.orchestrator)
79         self.__logger.debug("name = %s", __name__)
80         self.vnf = dict(
81             descriptor=functest_utils.get_parameter_from_yaml(
82                 "vnf.descriptor", config_file),
83             inputs=functest_utils.get_parameter_from_yaml(
84                 "vnf.inputs", config_file),
85             requirements=functest_utils.get_parameter_from_yaml(
86                 "vnf.requirements", config_file)
87         )
88         self.details['vnf'] = dict(
89             descriptor_version=self.vnf['descriptor']['version'],
90             name=functest_utils.get_parameter_from_yaml(
91                 "vnf.name", config_file),
92             version=functest_utils.get_parameter_from_yaml(
93                 "vnf.version", config_file),
94         )
95         self.__logger.debug("VNF configuration: %s", self.vnf)
96
97         self.util = Utilvnf()
98         self.util.set_credentials(self.cloud)
99         credentials = {"cloud": self.cloud}
100         self.util_info = {"credentials": credentials,
101                           "vnf_data_dir": self.util.vnf_data_dir}
102
103         self.details['test_vnf'] = dict(
104             name=functest_utils.get_parameter_from_yaml(
105                 "vnf_test_suite.name", config_file),
106             version=functest_utils.get_parameter_from_yaml(
107                 "vnf_test_suite.version", config_file)
108         )
109         self.images = functest_utils.get_parameter_from_yaml(
110             "tenant_images", config_file)
111         self.__logger.info("Images needed for vrouter: %s", self.images)
112
113         self.image_alt = None
114         self.flavor_alt = None
115
116     def check_requirements(self):
117         if env.get('NEW_USER_ROLE').lower() == "admin":
118             self.__logger.warning(
119                 "Defining NEW_USER_ROLE=admin will easily break the testcase "
120                 "because Cloudify doesn't manage tenancy (e.g. subnet "
121                 "overlapping)")
122
123     def execute(self):
124         # pylint: disable=too-many-locals,too-many-statements
125         """
126         Deploy Cloudify Manager.
127         network, security group, fip, VM creation
128         """
129         # network creation
130         super(CloudifyVrouter, self).execute()
131         start_time = time.time()
132         self.put_private_key()
133         self.upload_cfy_plugins(self.cop_yaml, self.cop_wgn)
134
135         self.image_alt = self.publish_image_alt()
136         self.flavor_alt = self.create_flavor_alt()
137
138         duration = time.time() - start_time
139         self.details['orchestrator'].update(status='PASS', duration=duration)
140
141         self.vnf['inputs'].update(dict(
142             external_network_name=self.ext_net.name))
143         self.vnf['inputs'].update(dict(
144             target_vnf_image_id=self.image_alt.id))
145         self.vnf['inputs'].update(dict(
146             reference_vnf_image_id=self.image_alt.id))
147         self.vnf['inputs'].update(dict(
148             target_vnf_flavor_id=self.flavor_alt.id))
149         self.vnf['inputs'].update(dict(
150             reference_vnf_flavor_id=self.flavor_alt.id))
151         self.vnf['inputs'].update(dict(
152             keystone_username=self.project.user.name))
153         self.vnf['inputs'].update(dict(
154             keystone_password=self.project.password))
155         self.vnf['inputs'].update(dict(
156             keystone_tenant_name=self.project.project.name))
157         self.vnf['inputs'].update(dict(
158             keystone_user_domain_name=os.environ.get(
159                 'OS_USER_DOMAIN_NAME', 'Default')))
160         self.vnf['inputs'].update(dict(
161             keystone_project_domain_name=os.environ.get(
162                 'OS_PROJECT_DOMAIN_NAME', 'Default')))
163         self.vnf['inputs'].update(dict(
164             region=os.environ.get('OS_REGION_NAME', 'RegionOne')))
165         self.vnf['inputs'].update(dict(
166             keystone_url=self.get_public_auth_url(self.orig_cloud)))
167
168         if self.deploy_vnf() and self.test_vnf():
169             self.result = 100
170             return 0
171         self.result = 1/3 * 100
172         return 1
173
174     def deploy_vnf(self):
175         start_time = time.time()
176         self.__logger.info("Upload VNFD")
177         descriptor = self.vnf['descriptor']
178         self.util_info["cfy"] = self.cfy_client
179         self.util_info["cfy_manager_ip"] = self.fip.floating_ip_address
180         self.util_info["deployment_name"] = descriptor.get('name')
181
182         self.cfy_client.blueprints.upload(
183             descriptor.get('file_name'), descriptor.get('name'))
184
185         self.__logger.info("Create VNF Instance")
186         self.cfy_client.deployments.create(
187             descriptor.get('name'), descriptor.get('name'),
188             self.vnf.get('inputs'))
189
190         cloudify.wait_for_execution(
191             self.cfy_client, cloudify.get_execution_id(
192                 self.cfy_client, descriptor.get('name')),
193             self.__logger, timeout=7200)
194
195         self.__logger.info("Start the VNF Instance deployment")
196         execution = self.cfy_client.executions.start(
197             descriptor.get('name'), 'install')
198         # Show execution log
199         execution = cloudify.wait_for_execution(
200             self.cfy_client, execution, self.__logger)
201
202         duration = time.time() - start_time
203
204         self.__logger.info(execution)
205         if execution.status == 'terminated':
206             self.details['vnf'].update(status='PASS', duration=duration)
207             result = True
208         else:
209             self.details['vnf'].update(status='FAIL', duration=duration)
210             result = False
211         return result
212
213     def test_vnf(self):
214         start_time = time.time()
215         testing = vrouter_base.VrouterOnBoardingBase(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         self.kill_existing_execution(self.vnf['descriptor'].get('name'))
230         if self.image_alt:
231             self.cloud.delete_image(self.image_alt)
232         if self.flavor_alt:
233             self.orig_cloud.delete_flavor(self.flavor_alt.id)
234         super(CloudifyVrouter, self).clean()