1 # Copyright (c) 2016-2017 Intel Corporation
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
20 from copy import deepcopy
22 from tests.unit import STL_MOCKS
25 SSH_HELPER = 'yardstick.network_services.vnf_generic.vnf.sample_vnf.VnfSshHelper'
27 STLClient = mock.MagicMock()
28 stl_patch = mock.patch.dict("sys.modules", STL_MOCKS)
32 from yardstick.network_services.vnf_generic.vnf.prox_vnf import ProxApproxVnf
33 from tests.unit.network_services.vnf_generic.vnf.test_base import mock_ssh
39 @mock.patch('yardstick.network_services.vnf_generic.vnf.prox_helpers.time')
40 class TestProxApproxVnf(unittest.TestCase):
43 'short-name': 'ProxVnf',
48 'network': '152.16.100.20',
49 'netmask': '255.255.255.0',
50 'gateway': '152.16.100.20',
54 'network': '152.16.40.20',
55 'netmask': '255.255.255.0',
56 'gateway': '152.16.40.20',
60 'description': 'PROX approximation using DPDK',
61 'name': 'proxvnf-baremetal',
64 'network': '0064:ff9b:0:0:0:0:9810:6414',
66 'gateway': '0064:ff9b:0:0:0:0:9810:6414',
70 'network': '0064:ff9b:0:0:0:0:9810:2814',
72 'gateway': '0064:ff9b:0:0:0:0:9810:2814',
76 'id': 'proxvnf-baremetal',
77 'external-interface': [
79 'virtual-interface': {
80 'dst_mac': '00:00:00:00:00:04',
81 'vpci': '0000:05:00.0',
82 'local_ip': '152.16.100.19',
83 'type': 'PCI-PASSTHROUGH',
84 'vld_id': 'downlink_0',
86 'netmask': '255.255.255.0',
88 'bandwidth': '10 Gbps',
90 'dst_ip': '152.16.100.20',
91 'local_iface_name': 'xe0',
92 'local_mac': '00:00:00:00:00:02',
94 'vnfd-connection-point-ref': 'xe0',
98 'virtual-interface': {
99 'dst_mac': '00:00:00:00:00:03',
100 'vpci': '0000:05:00.1',
101 'local_ip': '152.16.40.19',
102 'type': 'PCI-PASSTHROUGH',
103 'vld_id': 'uplink_0',
106 'netmask': '255.255.255.0',
108 'bandwidth': '10 Gbps',
109 'dst_ip': '152.16.40.20',
110 'local_iface_name': 'xe1',
111 'local_mac': '00:00:00:00:00:01',
113 'vnfd-connection-point-ref': 'xe1',
119 'description': 'PROX approximation using DPDK',
121 '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',
190 'vld_id': ProxApproxVnf.DOWNLINK,
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',
224 'vld_id': ProxApproxVnf.UPLINK,
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',
255 'vld_id': ProxApproxVnf.UPLINK,
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',
267 'vld_id': ProxApproxVnf.DOWNLINK,
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, *args):
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, *args):
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, *args):
341 resource_helper = mock.MagicMock()
342 resource_helper.execute.return_value = list(range(12))
343 resource_helper.collect_collectd_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['packets_in'], expected['packets_in'])
356 self.assertEqual(result['packets_dropped'], expected['packets_dropped'])
357 self.assertEqual(result['packets_fwd'], expected['packets_fwd'])
358 self.assertNotEqual(result['packets_fwd'], 0)
359 self.assertNotEqual(result['packets_fwd'], 0)
361 @mock.patch(SSH_HELPER)
362 def test_collect_kpi_error(self, ssh, *args):
365 resource_helper = mock.MagicMock()
367 prox_approx_vnf = ProxApproxVnf(NAME, deepcopy(self.VNFD0))
368 prox_approx_vnf.resource_helper = resource_helper
369 prox_approx_vnf.vnfd_helper['vdu'][0]['external-interface'] = []
370 prox_approx_vnf.vnfd_helper.port_pairs.interfaces = []
372 with self.assertRaises(RuntimeError):
373 prox_approx_vnf.collect_kpi()
375 def _get_file_abspath(self, filename, *args):
376 curr_path = os.path.dirname(os.path.abspath(__file__))
377 file_path = os.path.join(curr_path, filename)
380 @mock.patch('yardstick.common.utils.open', create=True)
381 @mock.patch('yardstick.benchmark.scenarios.networking.vnf_generic.open', create=True)
382 @mock.patch('yardstick.network_services.helpers.iniparser.open', create=True)
383 @mock.patch(SSH_HELPER)
384 def test_run_prox(self, ssh, *_):
387 prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
388 prox_approx_vnf.scenario_helper.scenario_cfg = self.SCENARIO_CFG
389 prox_approx_vnf.ssh_helper.join_bin_path.return_value = '/tool_path12/tool_file34'
390 prox_approx_vnf.setup_helper.remote_path = 'configs/file56.cfg'
392 expected = "sudo bash -c 'cd /tool_path12; " \
393 "/tool_path12/tool_file34 -o cli -t -f /tmp/l3-swap-2.cfg '"
395 prox_approx_vnf._run()
396 result = prox_approx_vnf.ssh_helper.run.call_args[0][0]
397 self.assertEqual(result, expected)
399 @mock.patch(SSH_HELPER)
400 def bad_test_instantiate(self, *args):
401 prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
402 prox_approx_vnf.scenario_helper = mock.MagicMock()
403 prox_approx_vnf.setup_helper = mock.MagicMock()
404 # we can't mock super
405 prox_approx_vnf.instantiate(self.SCENARIO_CFG, self.CONTEXT_CFG)
406 prox_approx_vnf.setup_helper.build_config.assert_called_once()
408 @mock.patch(SSH_HELPER)
409 def test_wait_for_instantiate_panic(self, ssh, *args):
410 mock_ssh(ssh, exec_result=(1, "", ""))
411 prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
412 prox_approx_vnf._vnf_process = mock.MagicMock(**{"is_alive.return_value": True})
413 prox_approx_vnf._run_prox = mock.Mock(return_value=0)
414 prox_approx_vnf.WAIT_TIME = 0
415 prox_approx_vnf.q_out.put("PANIC")
416 with self.assertRaises(RuntimeError):
417 prox_approx_vnf.wait_for_instantiate()
419 @mock.patch('yardstick.network_services.vnf_generic.vnf.prox_helpers.socket')
420 @mock.patch(SSH_HELPER)
421 def test_terminate(self, ssh, *args):
423 prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
424 prox_approx_vnf._vnf_process = mock.MagicMock()
425 prox_approx_vnf._vnf_process.terminate = mock.Mock()
426 prox_approx_vnf.ssh_helper = mock.MagicMock()
427 prox_approx_vnf.setup_helper = mock.Mock()
428 prox_approx_vnf.resource_helper = mock.MagicMock()
430 self.assertIsNone(prox_approx_vnf.terminate())
432 @mock.patch(SSH_HELPER)
433 def test__vnf_up_post(self, ssh, *args):
435 prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
436 prox_approx_vnf.resource_helper = resource_helper = mock.Mock()
438 prox_approx_vnf._vnf_up_post()
439 self.assertEqual(resource_helper.up_post.call_count, 1)
441 @mock.patch(SSH_HELPER)
442 def test_vnf_execute_oserror(self, ssh, *args):
444 prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
445 prox_approx_vnf.resource_helper = resource_helper = mock.Mock()
447 resource_helper.execute.side_effect = OSError(errno.EPIPE, "")
448 prox_approx_vnf.vnf_execute("", _ignore_errors=True)
450 resource_helper.execute.side_effect = OSError(errno.ESHUTDOWN, "")
451 prox_approx_vnf.vnf_execute("", _ignore_errors=True)
453 resource_helper.execute.side_effect = OSError(errno.EADDRINUSE, "")
454 with self.assertRaises(OSError):
455 prox_approx_vnf.vnf_execute("", _ignore_errors=True)