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
23 from copy import deepcopy
25 from tests.unit import STL_MOCKS
28 SSH_HELPER = 'yardstick.network_services.vnf_generic.vnf.sample_vnf.VnfSshHelper'
30 STLClient = mock.MagicMock()
31 stl_patch = mock.patch.dict("sys.modules", STL_MOCKS)
35 from yardstick.network_services.vnf_generic.vnf.prox_vnf import ProxApproxVnf
36 from tests.unit.network_services.vnf_generic.vnf.test_base import mock_ssh
42 @mock.patch('yardstick.network_services.vnf_generic.vnf.prox_helpers.time')
43 class TestProxApproxVnf(unittest.TestCase):
46 'short-name': 'ProxVnf',
51 'network': '152.16.100.20',
52 'netmask': '255.255.255.0',
53 'gateway': '152.16.100.20',
57 'network': '152.16.40.20',
58 'netmask': '255.255.255.0',
59 'gateway': '152.16.40.20',
63 'description': 'PROX approximation using DPDK',
64 'name': 'proxvnf-baremetal',
67 'network': '0064:ff9b:0:0:0:0:9810:6414',
69 'gateway': '0064:ff9b:0:0:0:0:9810:6414',
73 'network': '0064:ff9b:0:0:0:0:9810:2814',
75 'gateway': '0064:ff9b:0:0:0:0:9810:2814',
79 'id': 'proxvnf-baremetal',
80 'external-interface': [
82 'virtual-interface': {
83 'dst_mac': '00:00:00:00:00:04',
84 'vpci': '0000:05:00.0',
85 'local_ip': '152.16.100.19',
86 'type': 'PCI-PASSTHROUGH',
88 'netmask': '255.255.255.0',
90 'bandwidth': '10 Gbps',
92 'dst_ip': '152.16.100.20',
93 'local_iface_name': 'xe0',
94 'local_mac': '00:00:00:00:00:02',
96 'vnfd-connection-point-ref': 'xe0',
100 'virtual-interface': {
101 'dst_mac': '00:00:00:00:00:03',
102 'vpci': '0000:05:00.1',
103 'local_ip': '152.16.40.19',
104 'type': 'PCI-PASSTHROUGH',
107 'netmask': '255.255.255.0',
108 'dpdk_port_num': '1',
109 'bandwidth': '10 Gbps',
110 'dst_ip': '152.16.40.20',
111 'local_iface_name': 'xe1',
112 'local_mac': '00:00:00:00:00:01',
114 'vnfd-connection-point-ref': 'xe1',
120 'description': 'PROX approximation using DPDK',
122 'vdu-id': 'proxvnf-baremetal',
135 'connection-point': [
145 'id': 'ProxApproxVnf',
150 'vnfd:vnfd-catalog': {
160 'tg__1': 'trafficgen_1.yardstick',
161 'vnf__1': 'vnf.yardstick'},
163 'duration': 600, 'type': 'Duration'},
164 'topology': 'prox-tg-topology-2.yaml',
165 'traffic_profile': '../../traffic_profiles/prox_binsearch.yaml',
168 'tg__1': {'prox_args': {'-e': '',
170 'prox_config': 'configs/l3-gen-2.cfg',
172 '/root/dppd-PROX-v035/build/prox'},
174 'prox_args': {'-t': ''},
175 'prox_config': 'configs/l3-swap-2.cfg',
176 'prox_path': '/root/dppd-PROX-v035/build/prox'}}}
181 'member-vnf-index': '3',
182 'role': 'TrafficGen',
183 'name': 'trafficgen_2.yardstick',
184 'vnfd-id-ref': 'tg__2',
188 'local_iface_name': 'ens513f0',
190 'netmask': '255.255.255.0',
191 'local_ip': '152.16.40.20',
192 'dst_mac': '00:00:00:00:00:01',
193 'local_mac': '00:00:00:00:00:03',
194 'dst_ip': '152.16.40.19',
196 'vpci': '0000:02:00.0',
200 'local_iface_name': 'ens513f1',
201 'netmask': '255.255.255.0',
202 'network': '202.16.100.0',
203 'local_ip': '202.16.100.20',
204 'local_mac': '00:1e:67:d0:60:5d',
206 'vpci': '0000:02:00.1',
211 'VNF model': 'l3fwd_vnf.yaml',
215 'member-vnf-index': '1',
216 'role': 'TrafficGen',
217 'name': 'trafficgen_1.yardstick',
218 'vnfd-id-ref': 'tg__1',
222 'local_iface_name': 'ens785f0',
224 'netmask': '255.255.255.0',
225 'local_ip': '152.16.100.20',
226 'dst_mac': '00:00:00:00:00:02',
227 'local_mac': '00:00:00:00:00:04',
228 'dst_ip': '152.16.100.19',
230 'vpci': '0000:05:00.0',
234 'local_iface_name': 'ens785f1',
235 'netmask': '255.255.255.0',
236 'local_ip': '152.16.100.21',
237 'local_mac': '00:00:00:00:00:01',
239 'vpci': '0000:05:00.1',
244 'VNF model': 'tg_rfc2544_tpl.yaml',
248 'name': 'vnf.yardstick',
249 'vnfd-id-ref': 'vnf__1',
253 'local_iface_name': 'ens786f0',
255 'netmask': '255.255.255.0',
256 'local_ip': '152.16.100.19',
257 'dst_mac': '00:00:00:00:00:04',
258 'local_mac': '00:00:00:00:00:02',
259 'dst_ip': '152.16.100.20',
261 'vpci': '0000:05:00.0',
265 'local_iface_name': 'ens786f1',
267 'netmask': '255.255.255.0',
268 'local_ip': '152.16.40.19',
269 'dst_mac': '00:00:00:00:00:03',
270 'local_mac': '00:00:00:00:00:01',
271 'dst_ip': '152.16.40.20',
273 'vpci': '0000:05:00.1',
279 'netmask': '255.255.255.0',
280 'gateway': '152.16.100.20',
281 'network': '152.16.100.20',
285 'netmask': '255.255.255.0',
286 'gateway': '152.16.40.20',
287 'network': '152.16.40.20',
291 'member-vnf-index': '2',
298 'gateway': '0064:ff9b:0:0:0:0:9810:6414',
299 'network': '0064:ff9b:0:0:0:0:9810:6414',
304 'gateway': '0064:ff9b:0:0:0:0:9810:2814',
305 'network': '0064:ff9b:0:0:0:0:9810:2814',
310 'VNF model': 'prox_vnf.yaml',
315 @mock.patch(SSH_HELPER)
316 def test___init__(self, ssh, mock_time):
318 prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
319 self.assertIsNone(prox_approx_vnf._vnf_process)
321 @mock.patch(SSH_HELPER)
322 def test_collect_kpi_no_client(self, ssh, mock_time):
325 prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
326 prox_approx_vnf.resource_helper = None
329 'packets_dropped': 0,
331 'collect_stats': {'core': {}},
333 result = prox_approx_vnf.collect_kpi()
334 self.assertEqual(result, expected)
336 @mock.patch(SSH_HELPER)
337 def test_collect_kpi(self, ssh, mock_time):
340 resource_helper = mock.MagicMock()
341 resource_helper.execute.return_value = list(range(12))
342 resource_helper.collect_kpi.return_value = {'core': {'result': 234}}
344 prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
345 prox_approx_vnf.resource_helper = resource_helper
349 'packets_dropped': 1,
351 'collect_stats': {'core': {'result': 234}},
353 result = prox_approx_vnf.collect_kpi()
354 self.assertEqual(result, expected)
356 @mock.patch(SSH_HELPER)
357 def test_collect_kpi_error(self, ssh, mock_time):
360 resource_helper = mock.MagicMock()
362 prox_approx_vnf = ProxApproxVnf(NAME, deepcopy(self.VNFD0))
363 prox_approx_vnf.resource_helper = resource_helper
364 prox_approx_vnf.vnfd_helper['vdu'][0]['external-interface'] = []
366 with self.assertRaises(RuntimeError):
367 prox_approx_vnf.collect_kpi()
369 def _get_file_abspath(self, filename, mock_time):
370 curr_path = os.path.dirname(os.path.abspath(__file__))
371 file_path = os.path.join(curr_path, filename)
374 @mock.patch(SSH_HELPER)
375 def test_run_prox(self, ssh, mock_time):
378 prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
380 filewrapper = mock.MagicMock()
381 config_path = self.SCENARIO_CFG['options']["vnf__1"]["prox_config"]
382 prox_path = self.SCENARIO_CFG['options']["vnf__1"]["prox_path"]
383 prox_args = self.SCENARIO_CFG['options']["vnf__1"]["prox_args"]
384 prox_approx_vnf.WAIT_TIME = 0
385 prox_approx_vnf._run_prox(filewrapper, config_path, prox_path, prox_args)
387 self.assertEqual(prox_approx_vnf.ssh_helper.run.call_args[0][0],
388 "sudo bash -c 'cd /root/dppd-PROX-v035/build; "
389 "/root/dppd-PROX-v035/build/prox -o cli -t -f configs/l3-swap-2.cfg '")
391 @mock.patch('yardstick.network_services.vnf_generic.vnf.sample_vnf.CpuSysCores')
392 @mock.patch('yardstick.network_services.vnf_generic.vnf.prox_helpers.find_relative_file')
393 @mock.patch(SSH_HELPER)
394 def test_instantiate(self, ssh, mock_find, mock_cpu_sys_cores, mock_time):
397 mock_cpu_sys_cores.get_core_socket.return_value = {'0': '01234'}
399 prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
400 prox_approx_vnf.ssh_helper = mock.MagicMock(
401 **{"execute.return_value": (0, "", ""), "bin_path": ""})
402 prox_approx_vnf.setup_helper._setup_resources = mock.MagicMock()
403 prox_approx_vnf.setup_helper._find_used_drivers = mock.MagicMock()
404 prox_approx_vnf.setup_helper.used_drivers = {}
405 prox_approx_vnf.setup_helper.bound_pci = []
406 prox_approx_vnf._run_prox = mock.MagicMock(return_value=0)
407 prox_approx_vnf.resource_helper = mock.MagicMock()
408 prox_approx_vnf.resource_helper.get_process_args.return_value = {
411 }, 'configs/l3-gen-2.cfg', '/root/dppd-PROX-v035/build/prox'
413 prox_approx_vnf.copy_to_target = mock.MagicMock()
414 prox_approx_vnf.upload_prox_config = mock.MagicMock()
415 prox_approx_vnf.generate_prox_config_file = mock.MagicMock()
416 prox_approx_vnf.q_out.put("PROX started")
417 prox_approx_vnf.WAIT_TIME = 0
419 # if process it still running exitcode will be None
421 result = prox_approx_vnf.instantiate(self.SCENARIO_CFG, self.CONTEXT_CFG)
422 self.assertIn(result, expected)
424 @mock.patch(SSH_HELPER)
425 def test_wait_for_instantiate_panic(self, ssh, mock_time):
426 mock_ssh(ssh, exec_result=(1, "", ""))
427 prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
428 prox_approx_vnf._vnf_process = mock.MagicMock(**{"is_alive.return_value": True})
429 prox_approx_vnf._run_prox = mock.Mock(return_value=0)
430 prox_approx_vnf.WAIT_TIME = 0
431 prox_approx_vnf.q_out.put("PANIC")
432 with self.assertRaises(RuntimeError):
433 prox_approx_vnf.wait_for_instantiate()
435 @mock.patch(SSH_HELPER)
436 def test_scale(self, ssh, mock_time):
438 prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
439 with self.assertRaises(NotImplementedError):
440 prox_approx_vnf.scale('')
442 @mock.patch('yardstick.network_services.vnf_generic.vnf.prox_helpers.socket')
443 @mock.patch(SSH_HELPER)
444 def test_terminate(self, ssh, mock_socket, mock_time):
446 prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
447 prox_approx_vnf._vnf_process = mock.MagicMock()
448 prox_approx_vnf._vnf_process.terminate = mock.Mock()
449 prox_approx_vnf.ssh_helper = mock.MagicMock()
450 prox_approx_vnf.setup_helper = mock.Mock()
451 prox_approx_vnf.resource_helper = mock.MagicMock()
453 self.assertIsNone(prox_approx_vnf.terminate())
455 @mock.patch(SSH_HELPER)
456 def test__vnf_up_post(self, ssh, mock_time):
458 prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
459 prox_approx_vnf.resource_helper = resource_helper = mock.Mock()
461 prox_approx_vnf._vnf_up_post()
462 self.assertEqual(resource_helper.up_post.call_count, 1)
465 if __name__ == '__main__':