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 SSH_HELPER = 'yardstick.network_services.vnf_generic.vnf.sample_vnf.VnfSshHelper'
29 'stl': mock.MagicMock(),
30 'stl.trex_stl_lib': mock.MagicMock(),
31 'stl.trex_stl_lib.base64': mock.MagicMock(),
32 'stl.trex_stl_lib.binascii': mock.MagicMock(),
33 'stl.trex_stl_lib.collections': mock.MagicMock(),
34 'stl.trex_stl_lib.copy': mock.MagicMock(),
35 'stl.trex_stl_lib.datetime': mock.MagicMock(),
36 'stl.trex_stl_lib.functools': mock.MagicMock(),
37 'stl.trex_stl_lib.imp': mock.MagicMock(),
38 'stl.trex_stl_lib.inspect': mock.MagicMock(),
39 'stl.trex_stl_lib.json': mock.MagicMock(),
40 'stl.trex_stl_lib.linecache': mock.MagicMock(),
41 'stl.trex_stl_lib.math': mock.MagicMock(),
42 'stl.trex_stl_lib.os': mock.MagicMock(),
43 'stl.trex_stl_lib.platform': mock.MagicMock(),
44 'stl.trex_stl_lib.pprint': mock.MagicMock(),
45 'stl.trex_stl_lib.random': mock.MagicMock(),
46 'stl.trex_stl_lib.re': mock.MagicMock(),
47 'stl.trex_stl_lib.scapy': mock.MagicMock(),
48 'stl.trex_stl_lib.socket': mock.MagicMock(),
49 'stl.trex_stl_lib.string': mock.MagicMock(),
50 'stl.trex_stl_lib.struct': mock.MagicMock(),
51 'stl.trex_stl_lib.sys': mock.MagicMock(),
52 'stl.trex_stl_lib.threading': mock.MagicMock(),
53 'stl.trex_stl_lib.time': mock.MagicMock(),
54 'stl.trex_stl_lib.traceback': mock.MagicMock(),
55 'stl.trex_stl_lib.trex_stl_async_client': mock.MagicMock(),
56 'stl.trex_stl_lib.trex_stl_client': mock.MagicMock(),
57 'stl.trex_stl_lib.trex_stl_exceptions': mock.MagicMock(),
58 'stl.trex_stl_lib.trex_stl_ext': mock.MagicMock(),
59 'stl.trex_stl_lib.trex_stl_jsonrpc_client': mock.MagicMock(),
60 'stl.trex_stl_lib.trex_stl_packet_builder_interface': mock.MagicMock(),
61 'stl.trex_stl_lib.trex_stl_packet_builder_scapy': mock.MagicMock(),
62 'stl.trex_stl_lib.trex_stl_port': mock.MagicMock(),
63 'stl.trex_stl_lib.trex_stl_stats': mock.MagicMock(),
64 'stl.trex_stl_lib.trex_stl_streams': mock.MagicMock(),
65 'stl.trex_stl_lib.trex_stl_types': mock.MagicMock(),
66 'stl.trex_stl_lib.types': mock.MagicMock(),
67 'stl.trex_stl_lib.utils': mock.MagicMock(),
68 'stl.trex_stl_lib.utils.argparse': mock.MagicMock(),
69 'stl.trex_stl_lib.utils.collections': mock.MagicMock(),
70 'stl.trex_stl_lib.utils.common': mock.MagicMock(),
71 'stl.trex_stl_lib.utils.json': mock.MagicMock(),
72 'stl.trex_stl_lib.utils.os': mock.MagicMock(),
73 'stl.trex_stl_lib.utils.parsing_opts': mock.MagicMock(),
74 'stl.trex_stl_lib.utils.pwd': mock.MagicMock(),
75 'stl.trex_stl_lib.utils.random': mock.MagicMock(),
76 'stl.trex_stl_lib.utils.re': mock.MagicMock(),
77 'stl.trex_stl_lib.utils.string': mock.MagicMock(),
78 'stl.trex_stl_lib.utils.sys': mock.MagicMock(),
79 'stl.trex_stl_lib.utils.text_opts': mock.MagicMock(),
80 'stl.trex_stl_lib.utils.text_tables': mock.MagicMock(),
81 'stl.trex_stl_lib.utils.texttable': mock.MagicMock(),
82 'stl.trex_stl_lib.warnings': mock.MagicMock(),
83 'stl.trex_stl_lib.yaml': mock.MagicMock(),
84 'stl.trex_stl_lib.zlib': mock.MagicMock(),
85 'stl.trex_stl_lib.zmq': mock.MagicMock(),
88 STLClient = mock.MagicMock()
89 stl_patch = mock.patch.dict("sys.modules", STL_MOCKS)
93 from yardstick.network_services.vnf_generic.vnf.prox_vnf import ProxApproxVnf
94 from tests.unit.network_services.vnf_generic.vnf.test_base import mock_ssh
100 @mock.patch('yardstick.network_services.vnf_generic.vnf.prox_helpers.time')
101 class TestProxApproxVnf(unittest.TestCase):
104 'short-name': 'ProxVnf',
109 'network': '152.16.100.20',
110 'netmask': '255.255.255.0',
111 'gateway': '152.16.100.20',
115 'network': '152.16.40.20',
116 'netmask': '255.255.255.0',
117 'gateway': '152.16.40.20',
121 'description': 'PROX approximation using DPDK',
122 'name': 'proxvnf-baremetal',
125 'network': '0064:ff9b:0:0:0:0:9810:6414',
127 'gateway': '0064:ff9b:0:0:0:0:9810:6414',
131 'network': '0064:ff9b:0:0:0:0:9810:2814',
133 'gateway': '0064:ff9b:0:0:0:0:9810:2814',
137 'id': 'proxvnf-baremetal',
138 'external-interface': [
140 'virtual-interface': {
141 'dst_mac': '00:00:00:00:00:04',
142 'vpci': '0000:05:00.0',
143 'local_ip': '152.16.100.19',
144 'type': 'PCI-PASSTHROUGH',
146 'netmask': '255.255.255.0',
147 'dpdk_port_num': '0',
148 'bandwidth': '10 Gbps',
150 'dst_ip': '152.16.100.20',
151 'local_iface_name': 'xe0',
152 'local_mac': '00:00:00:00:00:02',
154 'vnfd-connection-point-ref': 'xe0',
158 'virtual-interface': {
159 'dst_mac': '00:00:00:00:00:03',
160 'vpci': '0000:05:00.1',
161 'local_ip': '152.16.40.19',
162 'type': 'PCI-PASSTHROUGH',
165 'netmask': '255.255.255.0',
166 'dpdk_port_num': '1',
167 'bandwidth': '10 Gbps',
168 'dst_ip': '152.16.40.20',
169 'local_iface_name': 'xe1',
170 'local_mac': '00:00:00:00:00:01',
172 'vnfd-connection-point-ref': 'xe1',
178 'description': 'PROX approximation using DPDK',
180 'vdu-id': 'proxvnf-baremetal',
193 'connection-point': [
203 'id': 'ProxApproxVnf',
208 'vnfd:vnfd-catalog': {
218 'tg__1': 'trafficgen_1.yardstick',
219 'vnf__1': 'vnf.yardstick'},
221 'duration': 600, 'type': 'Duration'},
222 'topology': 'prox-tg-topology-2.yaml',
223 'traffic_profile': '../../traffic_profiles/prox_binsearch.yaml',
226 'tg__1': {'prox_args': {'-e': '',
228 'prox_config': 'configs/l3-gen-2.cfg',
230 '/root/dppd-PROX-v035/build/prox'},
232 'prox_args': {'-t': ''},
233 'prox_config': 'configs/l3-swap-2.cfg',
234 'prox_path': '/root/dppd-PROX-v035/build/prox'}}}
239 'member-vnf-index': '3',
240 'role': 'TrafficGen',
241 'name': 'trafficgen_2.yardstick',
242 'vnfd-id-ref': 'tg__2',
246 'local_iface_name': 'ens513f0',
248 'netmask': '255.255.255.0',
249 'local_ip': '152.16.40.20',
250 'dst_mac': '00:00:00:00:00:01',
251 'local_mac': '00:00:00:00:00:03',
252 'dst_ip': '152.16.40.19',
254 'vpci': '0000:02:00.0',
258 'local_iface_name': 'ens513f1',
259 'netmask': '255.255.255.0',
260 'network': '202.16.100.0',
261 'local_ip': '202.16.100.20',
262 'local_mac': '00:1e:67:d0:60:5d',
264 'vpci': '0000:02:00.1',
269 'VNF model': 'l3fwd_vnf.yaml',
273 'member-vnf-index': '1',
274 'role': 'TrafficGen',
275 'name': 'trafficgen_1.yardstick',
276 'vnfd-id-ref': 'tg__1',
280 'local_iface_name': 'ens785f0',
282 'netmask': '255.255.255.0',
283 'local_ip': '152.16.100.20',
284 'dst_mac': '00:00:00:00:00:02',
285 'local_mac': '00:00:00:00:00:04',
286 'dst_ip': '152.16.100.19',
288 'vpci': '0000:05:00.0',
292 'local_iface_name': 'ens785f1',
293 'netmask': '255.255.255.0',
294 'local_ip': '152.16.100.21',
295 'local_mac': '00:00:00:00:00:01',
297 'vpci': '0000:05:00.1',
302 'VNF model': 'tg_rfc2544_tpl.yaml',
306 'name': 'vnf.yardstick',
307 'vnfd-id-ref': 'vnf__1',
311 'local_iface_name': 'ens786f0',
313 'netmask': '255.255.255.0',
314 'local_ip': '152.16.100.19',
315 'dst_mac': '00:00:00:00:00:04',
316 'local_mac': '00:00:00:00:00:02',
317 'dst_ip': '152.16.100.20',
319 'vpci': '0000:05:00.0',
323 'local_iface_name': 'ens786f1',
325 'netmask': '255.255.255.0',
326 'local_ip': '152.16.40.19',
327 'dst_mac': '00:00:00:00:00:03',
328 'local_mac': '00:00:00:00:00:01',
329 'dst_ip': '152.16.40.20',
331 'vpci': '0000:05:00.1',
337 'netmask': '255.255.255.0',
338 'gateway': '152.16.100.20',
339 'network': '152.16.100.20',
343 'netmask': '255.255.255.0',
344 'gateway': '152.16.40.20',
345 'network': '152.16.40.20',
349 'member-vnf-index': '2',
356 'gateway': '0064:ff9b:0:0:0:0:9810:6414',
357 'network': '0064:ff9b:0:0:0:0:9810:6414',
362 'gateway': '0064:ff9b:0:0:0:0:9810:2814',
363 'network': '0064:ff9b:0:0:0:0:9810:2814',
368 'VNF model': 'prox_vnf.yaml',
373 @mock.patch(SSH_HELPER)
374 def test___init__(self, ssh, mock_time):
376 prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
377 self.assertIsNone(prox_approx_vnf._vnf_process)
379 @mock.patch(SSH_HELPER)
380 def test_collect_kpi_no_client(self, ssh, mock_time):
383 prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
384 prox_approx_vnf.resource_helper = None
387 'packets_dropped': 0,
389 'collect_stats': {'core': {}},
391 result = prox_approx_vnf.collect_kpi()
392 self.assertEqual(result, expected)
394 @mock.patch(SSH_HELPER)
395 def test_collect_kpi(self, ssh, mock_time):
398 resource_helper = mock.MagicMock()
399 resource_helper.execute.return_value = list(range(12))
400 resource_helper.collect_kpi.return_value = {'core': {'result': 234}}
402 prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
403 prox_approx_vnf.resource_helper = resource_helper
407 'packets_dropped': 1,
409 'collect_stats': {'core': {'result': 234}},
411 result = prox_approx_vnf.collect_kpi()
412 self.assertEqual(result, expected)
414 @mock.patch(SSH_HELPER)
415 def test_collect_kpi_error(self, ssh, mock_time):
418 resource_helper = mock.MagicMock()
420 prox_approx_vnf = ProxApproxVnf(NAME, deepcopy(self.VNFD0))
421 prox_approx_vnf.resource_helper = resource_helper
422 prox_approx_vnf.vnfd_helper['vdu'][0]['external-interface'] = []
424 with self.assertRaises(RuntimeError):
425 prox_approx_vnf.collect_kpi()
427 def _get_file_abspath(self, filename, mock_time):
428 curr_path = os.path.dirname(os.path.abspath(__file__))
429 file_path = os.path.join(curr_path, filename)
432 @mock.patch(SSH_HELPER)
433 def test_run_prox(self, ssh, mock_time):
436 prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
438 filewrapper = mock.MagicMock()
439 config_path = self.SCENARIO_CFG['options']["vnf__1"]["prox_config"]
440 prox_path = self.SCENARIO_CFG['options']["vnf__1"]["prox_path"]
441 prox_args = self.SCENARIO_CFG['options']["vnf__1"]["prox_args"]
442 prox_approx_vnf.WAIT_TIME = 0
443 prox_approx_vnf._run_prox(filewrapper, config_path, prox_path, prox_args)
445 self.assertEqual(prox_approx_vnf.ssh_helper.run.call_args[0][0],
446 "sudo bash -c 'cd /root/dppd-PROX-v035/build; "
447 "/root/dppd-PROX-v035/build/prox -o cli -t -f configs/l3-swap-2.cfg '")
449 @mock.patch('yardstick.network_services.vnf_generic.vnf.sample_vnf.CpuSysCores')
450 @mock.patch('yardstick.network_services.vnf_generic.vnf.prox_helpers.find_relative_file')
451 @mock.patch(SSH_HELPER)
452 def test_instantiate(self, ssh, mock_find, mock_cpu_sys_cores, mock_time):
455 mock_cpu_sys_cores.get_core_socket.return_value = {'0': '01234'}
457 prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
458 prox_approx_vnf.ssh_helper = mock.MagicMock(
459 **{"execute.return_value": (0, "", ""), "bin_path": ""})
460 prox_approx_vnf.setup_helper._setup_resources = mock.MagicMock()
461 prox_approx_vnf.setup_helper._find_used_drivers = mock.MagicMock()
462 prox_approx_vnf.setup_helper.used_drivers = {}
463 prox_approx_vnf.setup_helper.bound_pci = []
464 prox_approx_vnf._run_prox = mock.MagicMock(return_value=0)
465 prox_approx_vnf.resource_helper = mock.MagicMock()
466 prox_approx_vnf.resource_helper.get_process_args.return_value = {
469 }, 'configs/l3-gen-2.cfg', '/root/dppd-PROX-v035/build/prox'
471 prox_approx_vnf.copy_to_target = mock.MagicMock()
472 prox_approx_vnf.upload_prox_config = mock.MagicMock()
473 prox_approx_vnf.generate_prox_config_file = mock.MagicMock()
474 prox_approx_vnf.q_out.put("PROX started")
475 prox_approx_vnf.WAIT_TIME = 0
477 # if process it still running exitcode will be None
479 result = prox_approx_vnf.instantiate(self.SCENARIO_CFG, self.CONTEXT_CFG)
480 self.assertIn(result, expected)
482 @mock.patch(SSH_HELPER)
483 def test_wait_for_instantiate_panic(self, ssh, mock_time):
484 mock_ssh(ssh, exec_result=(1, "", ""))
485 prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
486 prox_approx_vnf._vnf_process = mock.MagicMock(**{"is_alive.return_value": True})
487 prox_approx_vnf._run_prox = mock.Mock(return_value=0)
488 prox_approx_vnf.WAIT_TIME = 0
489 prox_approx_vnf.q_out.put("PANIC")
490 with self.assertRaises(RuntimeError):
491 prox_approx_vnf.wait_for_instantiate()
493 @mock.patch(SSH_HELPER)
494 def test_scale(self, ssh, mock_time):
496 prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
497 with self.assertRaises(NotImplementedError):
498 prox_approx_vnf.scale('')
500 @mock.patch('yardstick.network_services.vnf_generic.vnf.prox_helpers.socket')
501 @mock.patch(SSH_HELPER)
502 def test_terminate(self, ssh, mock_socket, mock_time):
504 prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
505 prox_approx_vnf._vnf_process = mock.MagicMock()
506 prox_approx_vnf._vnf_process.terminate = mock.Mock()
507 prox_approx_vnf.ssh_helper = mock.MagicMock()
508 prox_approx_vnf.setup_helper = mock.Mock()
509 prox_approx_vnf.resource_helper = mock.MagicMock()
511 self.assertIsNone(prox_approx_vnf.terminate())
513 @mock.patch(SSH_HELPER)
514 def test__vnf_up_post(self, ssh, mock_time):
516 prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
517 prox_approx_vnf.resource_helper = resource_helper = mock.Mock()
519 prox_approx_vnf._vnf_up_post()
520 self.assertEqual(resource_helper.up_post.call_count, 1)
523 if __name__ == '__main__':