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.
16 from copy import deepcopy
23 from yardstick.common import utils
25 from yardstick.tests.unit.network_services.vnf_generic.vnf.test_base import mock_ssh
29 from yardstick.network_services.vnf_generic.vnf.cgnapt_vnf import CgnaptApproxVnf, \
30 CgnaptApproxSetupEnvHelper
31 from yardstick.network_services.vnf_generic.vnf import cgnapt_vnf
32 from yardstick.network_services.nfvi.resource import ResourceProfile
34 TEST_FILE_YAML = 'nsb_test_case.yaml'
35 SSH_HELPER = 'yardstick.network_services.vnf_generic.vnf.sample_vnf.VnfSshHelper'
41 class TestCgnaptApproxSetupEnvHelper(unittest.TestCase):
43 def test__generate_ip_from_pool(self):
45 ip = CgnaptApproxSetupEnvHelper._generate_ip_from_pool("1.2.3.4")
46 self.assertEqual(next(ip), '1.2.3.4')
47 self.assertEqual(next(ip), '1.2.4.4')
48 self.assertEqual(next(ip), '1.2.5.4')
50 def test__update_cgnat_script_file(self):
53 # See the License for the specific language governing permissions and
54 # limitations under the License.
57 link 0 config {port0_local_ip} {port0_prefixlen}
60 link 1 config {port1_local_ip} {port1_prefixlen}
63 header = "This is a header"
65 out = CgnaptApproxSetupEnvHelper._update_cgnat_script_file(header, sample.splitlines())
66 self.assertNotIn("This is a header", out)
68 def test__get_cgnapt_config(self):
69 vnfd_helper = mock.MagicMock()
70 vnfd_helper.port_pairs.uplink_ports = [{"name": 'a'}, {"name": "b"}, {"name": "c"}]
72 helper = CgnaptApproxSetupEnvHelper(vnfd_helper, mock.Mock(), mock.Mock())
73 result = helper._get_cgnapt_config()
74 self.assertIsNotNone(result)
77 helper = CgnaptApproxSetupEnvHelper(mock.Mock(), mock.Mock(), mock.Mock())
78 with self.assertRaises(NotImplementedError):
81 @mock.patch('yardstick.network_services.vnf_generic.vnf.sample_vnf.open')
82 @mock.patch.object(utils, 'find_relative_file')
83 @mock.patch('yardstick.network_services.vnf_generic.vnf.sample_vnf.MultiPortConfig')
84 @mock.patch.object(utils, 'open_relative_file')
85 def test_build_config(self, *args):
86 vnfd_helper = mock.Mock()
87 ssh_helper = mock.Mock()
88 scenario_helper = mock.Mock()
89 scenario_helper.vnf_cfg = {'lb_config': 'HW'}
90 scenario_helper.all_options = {}
92 cgnat_approx_setup_helper = CgnaptApproxSetupEnvHelper(vnfd_helper,
96 cgnat_approx_setup_helper.ssh_helper.provision_tool = mock.Mock(return_value='tool_path')
97 cgnat_approx_setup_helper.ssh_helper.all_ports = mock.Mock()
98 cgnat_approx_setup_helper.vnfd_helper.port_nums = mock.Mock(return_value=[0, 1])
99 expected = 'sudo tool_path -p 0x3 -f /tmp/cgnapt_config -s /tmp/cgnapt_script --hwlb 3'
100 self.assertEqual(cgnat_approx_setup_helper.build_config(), expected)
103 @mock.patch("yardstick.network_services.vnf_generic.vnf.sample_vnf.Process")
104 class TestCgnaptApproxVnf(unittest.TestCase):
105 VNFD = {'vnfd:vnfd-catalog':
107 [{'short-name': 'VpeVnf',
110 [{'network': '152.16.100.20',
111 'netmask': '255.255.255.0',
112 'gateway': '152.16.100.20',
114 {'network': '152.16.40.20',
115 'netmask': '255.255.255.0',
116 'gateway': '152.16.40.20',
118 'description': 'VPE approximation using DPDK',
119 'name': 'vpevnf-baremetal',
121 [{'network': '0064:ff9b:0:0:0:0:9810:6414',
123 'gateway': '0064:ff9b:0:0:0:0:9810:6414',
125 {'network': '0064:ff9b:0:0:0:0:9810:2814',
127 'gateway': '0064:ff9b:0:0:0:0:9810:2814',
129 'id': 'vpevnf-baremetal',
130 'external-interface':
131 [{'virtual-interface':
132 {'dst_mac': '00:00:00:00:00:04',
133 'vpci': '0000:05:00.0',
134 'local_ip': '152.16.100.19',
135 'type': 'PCI-PASSTHROUGH',
136 'netmask': '255.255.255.0',
138 'bandwidth': '10 Gbps',
140 'dst_ip': '152.16.100.20',
141 'local_iface_name': 'xe0',
142 'local_mac': '00:00:00:00:00:02'},
143 'vnfd-connection-point-ref': 'xe0',
145 {'virtual-interface':
146 {'dst_mac': '00:00:00:00:00:03',
147 'vpci': '0000:05:00.1',
148 'local_ip': '152.16.40.19',
149 'type': 'PCI-PASSTHROUGH',
151 'netmask': '255.255.255.0',
153 'bandwidth': '10 Gbps',
154 'dst_ip': '152.16.40.20',
155 'local_iface_name': 'xe1',
156 'local_mac': '00:00:00:00:00:01'},
157 'vnfd-connection-point-ref': 'xe1',
159 'description': 'Vpe approximation using DPDK',
161 {'vdu-id': 'vpevnf-baremetal',
167 {'kpi': ['packets_in', 'packets_fwd', 'packets_dropped']},
168 'connection-point': [{'type': 'VPORT', 'name': 'xe0'},
169 {'type': 'VPORT', 'name': 'xe1'}],
170 'id': 'CgnaptApproxVnf', 'name': 'VPEVnfSsh'}]}}
177 'allowed_drop_rate': '0.8 - 1',
190 'dst_ip': [{'tg__1': 'xe0'}],
192 'src_ip': [{'tg__0': 'xe0'}]},
194 'task_id': 'a70bdf4a-8e67-47a3-9dc1-273c14506eb7',
196 'tc': 'tc_ipv4_1Mflow_64B_packetsize',
198 'object': 'NetworkServiceTestCase',
200 'output_filename': '/tmp/yardstick.out',
205 'traffic_profile': 'ipv4_throughput_acl.yaml',
208 'tg__1': 'trafficgen_1.yardstick',
209 'tg__0': 'trafficgen_0.yardstick',
210 'vnf__0': 'vnf.yardstick',
212 'topology': 'vpe-tg-topology-baremetal.yaml',
215 context_cfg = {'nodes': {'tg__2':
216 {'member-vnf-index': '3',
217 'role': 'TrafficGen',
218 'name': 'trafficgen_2.yardstick',
219 'vnfd-id-ref': 'tg__2',
222 {'xe0': {'local_iface_name': 'ens513f0',
223 'vld_id': CgnaptApproxVnf.DOWNLINK,
224 'netmask': '255.255.255.0',
225 'local_ip': '152.16.40.20',
226 'dst_mac': '00:00:00:00:00:01',
227 'local_mac': '00:00:00:00:00:03',
228 'dst_ip': '152.16.40.19',
230 'vpci': '0000:02:00.0',
232 'xe1': {'local_iface_name': 'ens513f1',
233 'netmask': '255.255.255.0',
234 'network': '202.16.100.0',
235 'local_ip': '202.16.100.20',
236 'local_mac': '00:1e:67:d0:60:5d',
238 'vpci': '0000:02:00.1',
239 'dpdk_port_num': 1}},
241 'VNF model': 'l3fwd_vnf.yaml',
244 {'member-vnf-index': '1',
245 'role': 'TrafficGen',
246 'name': 'trafficgen_1.yardstick',
247 'vnfd-id-ref': 'tg__1',
250 {'xe0': {'local_iface_name': 'ens785f0',
251 'vld_id': CgnaptApproxVnf.UPLINK,
252 'netmask': '255.255.255.0',
253 'local_ip': '152.16.100.20',
254 'dst_mac': '00:00:00:00:00:02',
255 'local_mac': '00:00:00:00:00:04',
256 'dst_ip': '152.16.100.19',
258 'vpci': '0000:05:00.0',
260 'xe1': {'local_iface_name': 'ens785f1',
261 'netmask': '255.255.255.0',
262 'local_ip': '152.16.100.21',
263 'local_mac': '00:00:00:00:00:01',
265 'vpci': '0000:05:00.1',
266 'dpdk_port_num': 1}},
268 'VNF model': 'tg_rfc2544_tpl.yaml',
271 {'name': 'vnf.yardstick',
272 'vnfd-id-ref': 'vnf__0',
275 {'xe0': {'local_iface_name': 'ens786f0',
276 'vld_id': CgnaptApproxVnf.UPLINK,
277 'netmask': '255.255.255.0',
278 'local_ip': '152.16.100.19',
279 'dst_mac': '00:00:00:00:00:04',
280 'local_mac': '00:00:00:00:00:02',
281 'dst_ip': '152.16.100.20',
283 'vpci': '0000:05:00.0',
285 'xe1': {'local_iface_name': 'ens786f1',
286 'vld_id': CgnaptApproxVnf.DOWNLINK,
287 'netmask': '255.255.255.0',
288 'local_ip': '152.16.40.19',
289 'dst_mac': '00:00:00:00:00:03',
290 'local_mac': '00:00:00:00:00:01',
291 'dst_ip': '152.16.40.20',
293 'vpci': '0000:05:00.1',
294 'dpdk_port_num': 1}},
296 [{'netmask': '255.255.255.0',
297 'gateway': '152.16.100.20',
298 'network': '152.16.100.20',
300 {'netmask': '255.255.255.0',
301 'gateway': '152.16.40.20',
302 'network': '152.16.40.20',
304 'member-vnf-index': '2',
310 'gateway': '0064:ff9b:0:0:0:0:9810:6414',
311 'network': '0064:ff9b:0:0:0:0:9810:6414',
314 'gateway': '0064:ff9b:0:0:0:0:9810:2814',
315 'network': '0064:ff9b:0:0:0:0:9810:2814',
318 'VNF model': 'cgnapt_vnf.yaml'}}}
321 self.scenario_cfg = deepcopy(self.SCENARIO_CFG)
323 def test___init__(self, *args):
324 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
325 cgnapt_approx_vnf = CgnaptApproxVnf(name, vnfd)
326 self.assertIsNone(cgnapt_approx_vnf._vnf_process)
328 @mock.patch('yardstick.network_services.vnf_generic.vnf.sample_vnf.time')
329 @mock.patch(SSH_HELPER)
330 def test_collect_kpi(self, ssh, *args):
333 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
334 cgnapt_approx_vnf = CgnaptApproxVnf(name, vnfd)
335 cgnapt_approx_vnf._vnf_process = mock.MagicMock(
336 **{"is_alive.return_value": True, "exitcode": None})
337 cgnapt_approx_vnf.q_in = mock.MagicMock()
338 cgnapt_approx_vnf.q_out = mock.MagicMock()
339 cgnapt_approx_vnf.q_out.qsize = mock.Mock(return_value=0)
340 cgnapt_approx_vnf.resource = mock.Mock(autospec=ResourceProfile)
341 result = {'packets_dropped': 0, 'packets_fwd': 0, 'packets_in': 0}
342 self.assertEqual(result, cgnapt_approx_vnf.collect_kpi())
344 @mock.patch('yardstick.network_services.vnf_generic.vnf.sample_vnf.time')
345 @mock.patch(SSH_HELPER)
346 def test_vnf_execute_command(self, ssh, *args):
349 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
350 cgnapt_approx_vnf = CgnaptApproxVnf(name, vnfd)
351 cgnapt_approx_vnf.q_in = mock.MagicMock()
352 cgnapt_approx_vnf.q_out = mock.MagicMock()
353 cgnapt_approx_vnf.q_out.qsize = mock.Mock(return_value=0)
355 self.assertEqual("", cgnapt_approx_vnf.vnf_execute(cmd))
357 @mock.patch(SSH_HELPER)
358 def test_get_stats(self, ssh, *args):
361 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
362 cgnapt_approx_vnf = CgnaptApproxVnf(name, vnfd)
363 cgnapt_approx_vnf.q_in = mock.MagicMock()
364 cgnapt_approx_vnf.q_out = mock.MagicMock()
365 cgnapt_approx_vnf.q_out.qsize = mock.Mock(return_value=0)
367 "CG-NAPT(.*\n)*Received 100, Missed 0, Dropped 0,Translated 100,ingress"
368 cgnapt_approx_vnf.vnf_execute = mock.Mock(return_value=result)
369 self.assertListEqual(list(result), list(cgnapt_approx_vnf.get_stats()))
371 def _get_file_abspath(self, filename):
372 curr_path = os.path.dirname(os.path.abspath(__file__))
373 file_path = os.path.join(curr_path, filename)
376 @mock.patch("yardstick.network_services.vnf_generic.vnf.cgnapt_vnf.hex")
377 @mock.patch("yardstick.network_services.vnf_generic.vnf.cgnapt_vnf.eval")
378 @mock.patch('yardstick.network_services.vnf_generic.vnf.cgnapt_vnf.open')
379 @mock.patch(SSH_HELPER)
380 def test_run_vcgnapt(self, ssh, *args):
383 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
384 cgnapt_approx_vnf = CgnaptApproxVnf(name, vnfd)
385 cgnapt_approx_vnf._build_config = mock.MagicMock()
386 cgnapt_approx_vnf.queue_wrapper = mock.MagicMock()
387 cgnapt_approx_vnf.ssh_helper = mock.MagicMock()
388 cgnapt_approx_vnf.ssh_helper.run = mock.MagicMock()
389 cgnapt_approx_vnf.scenario_helper.scenario_cfg = self.scenario_cfg
390 cgnapt_approx_vnf._run()
391 cgnapt_approx_vnf.ssh_helper.run.assert_called_once()
393 @mock.patch("yardstick.network_services.vnf_generic.vnf.sample_vnf.Context")
394 @mock.patch(SSH_HELPER)
395 def test_instantiate(self, ssh, *args):
398 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
399 cgnapt_approx_vnf = CgnaptApproxVnf(name, vnfd)
400 cgnapt_approx_vnf.deploy_helper = mock.MagicMock()
401 cgnapt_approx_vnf.resource_helper = mock.MagicMock()
402 cgnapt_approx_vnf._build_config = mock.MagicMock()
403 self.scenario_cfg['vnf_options'] = {'acl': {'cfg': "",
405 cgnapt_approx_vnf.q_out.put("pipeline>")
406 cgnapt_vnf.WAIT_TIME = 3
407 self.scenario_cfg.update({"nodes": {"vnf__0": ""}})
408 self.assertIsNone(cgnapt_approx_vnf.instantiate(self.scenario_cfg,
411 @mock.patch("yardstick.network_services.vnf_generic.vnf.sample_vnf.time")
412 @mock.patch(SSH_HELPER)
413 def test_terminate(self, ssh, *args):
416 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
417 cgnapt_approx_vnf = CgnaptApproxVnf(name, vnfd)
418 cgnapt_approx_vnf._vnf_process = mock.MagicMock()
419 cgnapt_approx_vnf._vnf_process.terminate = mock.Mock()
420 cgnapt_approx_vnf.used_drivers = {"01:01.0": "i40e",
422 cgnapt_approx_vnf.vnf_execute = mock.MagicMock()
423 cgnapt_approx_vnf.dpdk_nic_bind = "dpdk_nic_bind.py"
424 cgnapt_approx_vnf._resource_collect_stop = mock.Mock()
425 self.assertIsNone(cgnapt_approx_vnf.terminate())
427 @mock.patch("yardstick.network_services.vnf_generic.vnf.sample_vnf.time")
428 @mock.patch(SSH_HELPER)
429 def test__vnf_up_post(self, ssh, *args):
432 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
433 self.scenario_cfg['options'][name]['napt'] = 'static'
435 cgnapt_approx_vnf = CgnaptApproxVnf(name, vnfd)
436 cgnapt_approx_vnf._vnf_process = mock.MagicMock()
437 cgnapt_approx_vnf._vnf_process.terminate = mock.Mock()
438 cgnapt_approx_vnf.vnf_execute = mock.MagicMock()
439 cgnapt_approx_vnf.scenario_helper.scenario_cfg = self.scenario_cfg
440 cgnapt_approx_vnf._resource_collect_stop = mock.Mock()
441 cgnapt_approx_vnf._vnf_up_post()
443 @mock.patch("yardstick.network_services.vnf_generic.vnf.sample_vnf.time")
444 @mock.patch(SSH_HELPER)
445 def test__vnf_up_post_short(self, ssh, *args):
448 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
449 cgnapt_approx_vnf = CgnaptApproxVnf(name, vnfd)
450 cgnapt_approx_vnf._vnf_process = mock.MagicMock()
451 cgnapt_approx_vnf._vnf_process.terminate = mock.Mock()
452 cgnapt_approx_vnf.vnf_execute = mock.MagicMock()
453 cgnapt_approx_vnf.scenario_helper.scenario_cfg = self.scenario_cfg
454 cgnapt_approx_vnf._resource_collect_stop = mock.Mock()
455 cgnapt_approx_vnf._vnf_up_post()