3 # Copyright (c) 2016-2017 Intel Corporation
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
18 from __future__ import absolute_import
24 from copy import deepcopy
26 from tests.unit import STL_MOCKS
29 SSH_HELPER = 'yardstick.network_services.vnf_generic.vnf.sample_vnf.VnfSshHelper'
31 STLClient = mock.MagicMock()
32 stl_patch = mock.patch.dict("sys.modules", STL_MOCKS)
36 from yardstick.network_services.vnf_generic.vnf.prox_vnf import ProxApproxVnf
37 from tests.unit.network_services.vnf_generic.vnf.test_base import mock_ssh
43 @mock.patch('yardstick.network_services.vnf_generic.vnf.prox_helpers.time')
44 class TestProxApproxVnf(unittest.TestCase):
47 'short-name': 'ProxVnf',
52 'network': '152.16.100.20',
53 'netmask': '255.255.255.0',
54 'gateway': '152.16.100.20',
58 'network': '152.16.40.20',
59 'netmask': '255.255.255.0',
60 'gateway': '152.16.40.20',
64 'description': 'PROX approximation using DPDK',
65 'name': 'proxvnf-baremetal',
68 'network': '0064:ff9b:0:0:0:0:9810:6414',
70 'gateway': '0064:ff9b:0:0:0:0:9810:6414',
74 'network': '0064:ff9b:0:0:0:0:9810:2814',
76 'gateway': '0064:ff9b:0:0:0:0:9810:2814',
80 'id': 'proxvnf-baremetal',
81 'external-interface': [
83 'virtual-interface': {
84 'dst_mac': '00:00:00:00:00:04',
85 'vpci': '0000:05:00.0',
86 'local_ip': '152.16.100.19',
87 'type': 'PCI-PASSTHROUGH',
89 'netmask': '255.255.255.0',
91 'bandwidth': '10 Gbps',
93 'dst_ip': '152.16.100.20',
94 'local_iface_name': 'xe0',
95 'local_mac': '00:00:00:00:00:02',
97 'vnfd-connection-point-ref': 'xe0',
101 'virtual-interface': {
102 'dst_mac': '00:00:00:00:00:03',
103 'vpci': '0000:05:00.1',
104 'local_ip': '152.16.40.19',
105 'type': 'PCI-PASSTHROUGH',
108 'netmask': '255.255.255.0',
109 'dpdk_port_num': '1',
110 'bandwidth': '10 Gbps',
111 'dst_ip': '152.16.40.20',
112 'local_iface_name': 'xe1',
113 'local_mac': '00:00:00:00:00:01',
115 'vnfd-connection-point-ref': 'xe1',
121 'description': 'PROX approximation using DPDK',
123 'vdu-id': 'proxvnf-baremetal',
136 'connection-point': [
146 'id': 'ProxApproxVnf',
151 'vnfd:vnfd-catalog': {
161 'tg__1': 'trafficgen_1.yardstick',
162 'vnf__1': 'vnf.yardstick'},
164 'duration': 600, 'type': 'Duration'},
165 'topology': 'prox-tg-topology-2.yaml',
166 'traffic_profile': '../../traffic_profiles/prox_binsearch.yaml',
169 'tg__1': {'prox_args': {'-e': '',
171 'prox_config': 'configs/l3-gen-2.cfg',
173 '/root/dppd-PROX-v035/build/prox'},
175 'prox_args': {'-t': ''},
176 'prox_config': 'configs/l3-swap-2.cfg',
177 'prox_path': '/root/dppd-PROX-v035/build/prox'}}}
182 'member-vnf-index': '3',
183 'role': 'TrafficGen',
184 'name': 'trafficgen_2.yardstick',
185 'vnfd-id-ref': 'tg__2',
189 'local_iface_name': 'ens513f0',
191 'netmask': '255.255.255.0',
192 'local_ip': '152.16.40.20',
193 'dst_mac': '00:00:00:00:00:01',
194 'local_mac': '00:00:00:00:00:03',
195 'dst_ip': '152.16.40.19',
197 'vpci': '0000:02:00.0',
201 'local_iface_name': 'ens513f1',
202 'netmask': '255.255.255.0',
203 'network': '202.16.100.0',
204 'local_ip': '202.16.100.20',
205 'local_mac': '00:1e:67:d0:60:5d',
207 'vpci': '0000:02:00.1',
212 'VNF model': 'l3fwd_vnf.yaml',
216 'member-vnf-index': '1',
217 'role': 'TrafficGen',
218 'name': 'trafficgen_1.yardstick',
219 'vnfd-id-ref': 'tg__1',
223 'local_iface_name': 'ens785f0',
225 'netmask': '255.255.255.0',
226 'local_ip': '152.16.100.20',
227 'dst_mac': '00:00:00:00:00:02',
228 'local_mac': '00:00:00:00:00:04',
229 'dst_ip': '152.16.100.19',
231 'vpci': '0000:05:00.0',
235 'local_iface_name': 'ens785f1',
236 'netmask': '255.255.255.0',
237 'local_ip': '152.16.100.21',
238 'local_mac': '00:00:00:00:00:01',
240 'vpci': '0000:05:00.1',
245 'VNF model': 'tg_rfc2544_tpl.yaml',
249 'name': 'vnf.yardstick',
250 'vnfd-id-ref': 'vnf__1',
254 'local_iface_name': 'ens786f0',
256 'netmask': '255.255.255.0',
257 'local_ip': '152.16.100.19',
258 'dst_mac': '00:00:00:00:00:04',
259 'local_mac': '00:00:00:00:00:02',
260 'dst_ip': '152.16.100.20',
262 'vpci': '0000:05:00.0',
266 'local_iface_name': 'ens786f1',
268 'netmask': '255.255.255.0',
269 'local_ip': '152.16.40.19',
270 'dst_mac': '00:00:00:00:00:03',
271 'local_mac': '00:00:00:00:00:01',
272 'dst_ip': '152.16.40.20',
274 'vpci': '0000:05:00.1',
280 'netmask': '255.255.255.0',
281 'gateway': '152.16.100.20',
282 'network': '152.16.100.20',
286 'netmask': '255.255.255.0',
287 'gateway': '152.16.40.20',
288 'network': '152.16.40.20',
292 'member-vnf-index': '2',
299 'gateway': '0064:ff9b:0:0:0:0:9810:6414',
300 'network': '0064:ff9b:0:0:0:0:9810:6414',
305 'gateway': '0064:ff9b:0:0:0:0:9810:2814',
306 'network': '0064:ff9b:0:0:0:0:9810:2814',
311 'VNF model': 'prox_vnf.yaml',
316 @mock.patch(SSH_HELPER)
317 def test___init__(self, ssh, mock_time):
319 prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
320 self.assertIsNone(prox_approx_vnf._vnf_process)
322 @mock.patch(SSH_HELPER)
323 def test_collect_kpi_no_client(self, ssh, mock_time):
326 prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
327 prox_approx_vnf.resource_helper = None
330 'packets_dropped': 0,
332 'collect_stats': {'core': {}},
334 result = prox_approx_vnf.collect_kpi()
335 self.assertEqual(result, expected)
337 @mock.patch(SSH_HELPER)
338 def test_collect_kpi(self, ssh, mock_time):
341 resource_helper = mock.MagicMock()
342 resource_helper.execute.return_value = list(range(12))
343 resource_helper.collect_kpi.return_value = {'core': {'result': 234}}
345 prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
346 prox_approx_vnf.resource_helper = resource_helper
350 'packets_dropped': 1,
352 'collect_stats': {'core': {'result': 234}},
354 result = prox_approx_vnf.collect_kpi()
355 self.assertEqual(result, expected)
357 @mock.patch(SSH_HELPER)
358 def test_collect_kpi_error(self, ssh, mock_time):
361 resource_helper = mock.MagicMock()
363 prox_approx_vnf = ProxApproxVnf(NAME, deepcopy(self.VNFD0))
364 prox_approx_vnf.resource_helper = resource_helper
365 prox_approx_vnf.vnfd_helper['vdu'][0]['external-interface'] = []
367 with self.assertRaises(RuntimeError):
368 prox_approx_vnf.collect_kpi()
370 def _get_file_abspath(self, filename, mock_time):
371 curr_path = os.path.dirname(os.path.abspath(__file__))
372 file_path = os.path.join(curr_path, filename)
375 @mock.patch(SSH_HELPER)
376 def test_run_prox(self, ssh, mock_time):
379 prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
380 prox_approx_vnf.scenario_helper.scenario_cfg = self.SCENARIO_CFG
381 prox_approx_vnf.ssh_helper.provision_tool.return_value = '/tool_path12/tool_file34'
382 prox_approx_vnf.setup_helper.remote_path = 'configs/file56.cfg'
384 expected = "sudo bash -c 'cd /tool_path12; " \
385 "/tool_path12/tool_file34 -o cli -t -f configs/file56.cfg '"
387 prox_approx_vnf._run()
388 result = prox_approx_vnf.ssh_helper.run.call_args[0][0]
389 self.assertEqual(result, expected)
391 @mock.patch(SSH_HELPER)
392 def bad_test_instantiate(self, ssh, mock_time):
393 prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
394 prox_approx_vnf.scenario_helper = mock.MagicMock()
395 prox_approx_vnf.setup_helper = mock.MagicMock()
396 # we can't mock super
397 prox_approx_vnf.instantiate(self.SCENARIO_CFG, self.CONTEXT_CFG)
398 prox_approx_vnf.setup_helper.build_config.assert_called_once
400 @mock.patch(SSH_HELPER)
401 def test_wait_for_instantiate_panic(self, ssh, mock_time):
402 mock_ssh(ssh, exec_result=(1, "", ""))
403 prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
404 prox_approx_vnf._vnf_process = mock.MagicMock(**{"is_alive.return_value": True})
405 prox_approx_vnf._run_prox = mock.Mock(return_value=0)
406 prox_approx_vnf.WAIT_TIME = 0
407 prox_approx_vnf.q_out.put("PANIC")
408 with self.assertRaises(RuntimeError):
409 prox_approx_vnf.wait_for_instantiate()
411 @mock.patch(SSH_HELPER)
412 def test_scale(self, ssh, mock_time):
414 prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
415 with self.assertRaises(NotImplementedError):
416 prox_approx_vnf.scale()
418 @mock.patch('yardstick.network_services.vnf_generic.vnf.prox_helpers.socket')
419 @mock.patch(SSH_HELPER)
420 def test_terminate(self, ssh, mock_socket, mock_time):
422 prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
423 prox_approx_vnf._vnf_process = mock.MagicMock()
424 prox_approx_vnf._vnf_process.terminate = mock.Mock()
425 prox_approx_vnf.ssh_helper = mock.MagicMock()
426 prox_approx_vnf.setup_helper = mock.Mock()
427 prox_approx_vnf.resource_helper = mock.MagicMock()
429 self.assertIsNone(prox_approx_vnf.terminate())
431 @mock.patch(SSH_HELPER)
432 def test__vnf_up_post(self, ssh, mock_time):
434 prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
435 prox_approx_vnf.resource_helper = resource_helper = mock.Mock()
437 prox_approx_vnf._vnf_up_post()
438 self.assertEqual(resource_helper.up_post.call_count, 1)
440 @mock.patch(SSH_HELPER)
441 def test_vnf_execute_oserror(self, ssh, mock_time):
443 prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
444 prox_approx_vnf.resource_helper = resource_helper = mock.Mock()
446 resource_helper.execute.side_effect = OSError(errno.EPIPE, "")
447 prox_approx_vnf.vnf_execute("", _ignore_errors=True)
449 resource_helper.execute.side_effect = OSError(errno.ESHUTDOWN, "")
450 prox_approx_vnf.vnf_execute("", _ignore_errors=True)
452 resource_helper.execute.side_effect = OSError(errno.EADDRINUSE, "")
453 with self.assertRaises(OSError):
454 prox_approx_vnf.vnf_execute("", _ignore_errors=True)
456 if __name__ == '__main__':