1 # Copyright (c) 2016-2019 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.
15 from copy import deepcopy
21 from yardstick.benchmark.contexts import base as ctx_base
22 from yardstick.common import utils
23 from yardstick.common import process
24 from yardstick.network_services.vnf_generic.vnf import cgnapt_vnf
25 from yardstick.network_services.vnf_generic.vnf import sample_vnf
26 from yardstick.network_services.nfvi import resource
29 TEST_FILE_YAML = 'nsb_test_case.yaml'
33 class TestCgnaptApproxSetupEnvHelper(unittest.TestCase):
35 def test__generate_ip_from_pool(self):
38 ip = cgnapt_vnf.CgnaptApproxSetupEnvHelper._generate_ip_from_pool(_ip)
39 self.assertEqual(next(ip), _ip)
40 self.assertEqual(next(ip), '1.2.4.4')
41 self.assertEqual(next(ip), '1.2.5.4')
43 def test__update_cgnat_script_file(self):
46 # See the License for the specific language governing permissions and
47 # limitations under the License.
50 link 0 config {port0_local_ip} {port0_prefixlen}
53 link 1 config {port1_local_ip} {port1_prefixlen}
56 header = "This is a header"
58 out = cgnapt_vnf.CgnaptApproxSetupEnvHelper._update_cgnat_script_file(
59 header, sample.splitlines())
60 self.assertNotIn("This is a header", out)
62 def test__get_cgnapt_config(self):
63 vnfd_helper = mock.MagicMock()
64 vnfd_helper.port_pairs.uplink_ports = [{"name": 'a'}, {"name": "b"}, {"name": "c"}]
66 helper = cgnapt_vnf.CgnaptApproxSetupEnvHelper(
67 vnfd_helper, mock.Mock(), mock.Mock())
68 result = helper._get_cgnapt_config()
69 self.assertIsNotNone(result)
72 helper = cgnapt_vnf.CgnaptApproxSetupEnvHelper(
73 mock.Mock(), mock.Mock(), mock.Mock())
74 with self.assertRaises(NotImplementedError):
77 @mock.patch('yardstick.network_services.vnf_generic.vnf.sample_vnf.open')
78 @mock.patch.object(utils, 'find_relative_file')
79 @mock.patch('yardstick.network_services.vnf_generic.vnf.sample_vnf.MultiPortConfig')
80 @mock.patch.object(utils, 'open_relative_file')
81 def test_build_config(self, *args):
82 vnfd_helper = mock.Mock()
83 ssh_helper = mock.Mock()
84 scenario_helper = mock.Mock()
85 scenario_helper.vnf_cfg = {'lb_config': 'HW'}
86 scenario_helper.options = {}
87 scenario_helper.all_options = {}
89 cgnat_approx_setup_helper = cgnapt_vnf.CgnaptApproxSetupEnvHelper(
90 vnfd_helper, ssh_helper, scenario_helper)
92 cgnat_approx_setup_helper.ssh_helper.provision_tool = mock.Mock(return_value='tool_path')
93 cgnat_approx_setup_helper.ssh_helper.all_ports = mock.Mock()
94 cgnat_approx_setup_helper.vnfd_helper.port_nums = mock.Mock(return_value=[0, 1])
95 expected = 'sudo tool_path -p 0x3 -f /tmp/cgnapt_config -s /tmp/cgnapt_script --hwlb 3'
96 self.assertEqual(cgnat_approx_setup_helper.build_config(), expected)
99 @mock.patch.object(sample_vnf, 'Process')
100 class TestCgnaptApproxVnf(unittest.TestCase):
101 VNFD = {'vnfd:vnfd-catalog':
103 [{'short-name': 'VpeVnf',
106 [{'network': '152.16.100.20',
107 'netmask': '255.255.255.0',
108 'gateway': '152.16.100.20',
110 {'network': '152.16.40.20',
111 'netmask': '255.255.255.0',
112 'gateway': '152.16.40.20',
114 'description': 'VPE approximation using DPDK',
115 'name': 'vpevnf-baremetal',
117 [{'network': '0064:ff9b:0:0:0:0:9810:6414',
119 'gateway': '0064:ff9b:0:0:0:0:9810:6414',
121 {'network': '0064:ff9b:0:0:0:0:9810:2814',
123 'gateway': '0064:ff9b:0:0:0:0:9810:2814',
125 'id': 'vpevnf-baremetal',
126 'external-interface':
127 [{'virtual-interface':
128 {'dst_mac': '00:00:00:00:00:04',
129 'vpci': '0000:05:00.0',
130 'local_ip': '152.16.100.19',
131 'type': 'PCI-PASSTHROUGH',
132 'netmask': '255.255.255.0',
134 'bandwidth': '10 Gbps',
136 'dst_ip': '152.16.100.20',
137 'local_iface_name': 'xe0',
138 'local_mac': '00:00:00:00:00:02'},
139 'vnfd-connection-point-ref': 'xe0',
141 {'virtual-interface':
142 {'dst_mac': '00:00:00:00:00:03',
143 'vpci': '0000:05:00.1',
144 'local_ip': '152.16.40.19',
145 'type': 'PCI-PASSTHROUGH',
147 'netmask': '255.255.255.0',
149 'bandwidth': '10 Gbps',
150 'dst_ip': '152.16.40.20',
151 'local_iface_name': 'xe1',
152 'local_mac': '00:00:00:00:00:01'},
153 'vnfd-connection-point-ref': 'xe1',
155 'description': 'Vpe approximation using DPDK',
157 {'vdu-id': 'vpevnf-baremetal',
163 {'kpi': ['packets_in', 'packets_fwd', 'packets_dropped']},
164 'connection-point': [{'type': 'VPORT', 'name': 'xe0'},
165 {'type': 'VPORT', 'name': 'xe1'}],
166 'id': 'CgnaptApproxVnf', 'name': 'VPEVnfSsh'}]}}
173 'allowed_drop_rate': '0.8 - 1',
186 'dst_ip': [{'tg__1': 'xe0'}],
188 'src_ip': [{'tg__0': 'xe0'}]},
190 'task_id': 'a70bdf4a-8e67-47a3-9dc1-273c14506eb7',
192 'tc': 'tc_ipv4_1Mflow_64B_packetsize',
194 'object': 'NetworkServiceTestCase',
196 'output_filename': '/tmp/yardstick.out',
201 'traffic_profile': 'ipv4_throughput_acl.yaml',
204 'tg__1': 'trafficgen_1.yardstick',
205 'tg__0': 'trafficgen_0.yardstick',
206 'vnf__0': 'vnf.yardstick',
208 'topology': 'vpe-tg-topology-baremetal.yaml',
211 context_cfg = {'nodes': {'tg__2':
212 {'member-vnf-index': '3',
213 'role': 'TrafficGen',
214 'name': 'trafficgen_2.yardstick',
215 'vnfd-id-ref': 'tg__2',
218 {'xe0': {'local_iface_name': 'ens513f0',
219 'vld_id': cgnapt_vnf.CgnaptApproxVnf.DOWNLINK,
220 'netmask': '255.255.255.0',
221 'local_ip': '152.16.40.20',
222 'dst_mac': '00:00:00:00:00:01',
223 'local_mac': '00:00:00:00:00:03',
224 'dst_ip': '152.16.40.19',
226 'vpci': '0000:02:00.0',
228 'xe1': {'local_iface_name': 'ens513f1',
229 'netmask': '255.255.255.0',
230 'network': '202.16.100.0',
231 'local_ip': '202.16.100.20',
232 'local_mac': '00:1e:67:d0:60:5d',
234 'vpci': '0000:02:00.1',
235 'dpdk_port_num': 1}},
237 'VNF model': 'l3fwd_vnf.yaml',
240 {'member-vnf-index': '1',
241 'role': 'TrafficGen',
242 'name': 'trafficgen_1.yardstick',
243 'vnfd-id-ref': 'tg__1',
246 {'xe0': {'local_iface_name': 'ens785f0',
247 'vld_id': cgnapt_vnf.CgnaptApproxVnf.UPLINK,
248 'netmask': '255.255.255.0',
249 'local_ip': '152.16.100.20',
250 'dst_mac': '00:00:00:00:00:02',
251 'local_mac': '00:00:00:00:00:04',
252 'dst_ip': '152.16.100.19',
254 'vpci': '0000:05:00.0',
256 'xe1': {'local_iface_name': 'ens785f1',
257 'netmask': '255.255.255.0',
258 'local_ip': '152.16.100.21',
259 'local_mac': '00:00:00:00:00:01',
261 'vpci': '0000:05:00.1',
262 'dpdk_port_num': 1}},
264 'VNF model': 'tg_rfc2544_tpl.yaml',
267 {'name': 'vnf.yardstick',
268 'vnfd-id-ref': 'vnf__0',
271 {'xe0': {'local_iface_name': 'ens786f0',
272 'vld_id': cgnapt_vnf.CgnaptApproxVnf.UPLINK,
273 'netmask': '255.255.255.0',
274 'local_ip': '152.16.100.19',
275 'dst_mac': '00:00:00:00:00:04',
276 'local_mac': '00:00:00:00:00:02',
277 'dst_ip': '152.16.100.20',
279 'vpci': '0000:05:00.0',
281 'xe1': {'local_iface_name': 'ens786f1',
282 'vld_id': cgnapt_vnf.CgnaptApproxVnf.DOWNLINK,
283 'netmask': '255.255.255.0',
284 'local_ip': '152.16.40.19',
285 'dst_mac': '00:00:00:00:00:03',
286 'local_mac': '00:00:00:00:00:01',
287 'dst_ip': '152.16.40.20',
289 'vpci': '0000:05:00.1',
290 'dpdk_port_num': 1}},
292 [{'netmask': '255.255.255.0',
293 'gateway': '152.16.100.20',
294 'network': '152.16.100.20',
296 {'netmask': '255.255.255.0',
297 'gateway': '152.16.40.20',
298 'network': '152.16.40.20',
300 'member-vnf-index': '2',
306 'gateway': '0064:ff9b:0:0:0:0:9810:6414',
307 'network': '0064:ff9b:0:0:0:0:9810:6414',
310 'gateway': '0064:ff9b:0:0:0:0:9810:2814',
311 'network': '0064:ff9b:0:0:0:0:9810:2814',
314 'VNF model': 'cgnapt_vnf.yaml'}}}
317 self.scenario_cfg = deepcopy(self.SCENARIO_CFG)
319 def test___init__(self, *args):
320 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
321 cgnapt_approx_vnf = cgnapt_vnf.CgnaptApproxVnf(name, vnfd)
322 self.assertIsNone(cgnapt_approx_vnf._vnf_process)
324 @mock.patch.object(process, 'check_if_process_failed')
325 @mock.patch.object(ctx_base.Context, 'get_physical_node_from_server', return_value='mock_node')
326 def test_collect_kpi(self, *args):
327 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
328 cgnapt_approx_vnf = cgnapt_vnf.CgnaptApproxVnf(name, vnfd)
329 cgnapt_approx_vnf.scenario_helper.scenario_cfg = {
330 'nodes': {cgnapt_approx_vnf.name: "mock"}
332 cgnapt_approx_vnf._vnf_process = mock.MagicMock(
333 **{"is_alive.return_value": True, "exitcode": None})
334 cgnapt_approx_vnf.q_in = mock.MagicMock()
335 cgnapt_approx_vnf.q_out = mock.MagicMock()
336 cgnapt_approx_vnf.q_out.qsize = mock.Mock(return_value=0)
337 cgnapt_approx_vnf.resource = mock.Mock(
338 autospec=resource.ResourceProfile)
340 'physical_node': 'mock_node',
341 'packets_dropped': 0,
345 with mock.patch.object(cgnapt_approx_vnf, 'get_stats',
347 self.assertEqual(result, cgnapt_approx_vnf.collect_kpi())
349 @mock.patch.object(time, 'sleep')
350 def test_vnf_execute_command(self, *args):
351 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
352 cgnapt_approx_vnf = cgnapt_vnf.CgnaptApproxVnf(name, vnfd)
353 cgnapt_approx_vnf.q_in = mock.Mock()
354 cgnapt_approx_vnf.q_out = mock.Mock()
355 cgnapt_approx_vnf.q_out.qsize = mock.Mock(return_value=0)
356 self.assertEqual("", cgnapt_approx_vnf.vnf_execute('quit'))
358 def test_get_stats(self, *args):
359 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
360 cgnapt_approx_vnf = cgnapt_vnf.CgnaptApproxVnf(name, vnfd)
361 with mock.patch.object(cgnapt_approx_vnf, 'vnf_execute') as mock_exec:
362 mock_exec.return_value = 'output'
363 self.assertEqual('output', cgnapt_approx_vnf.get_stats())
365 mock_exec.assert_called_once_with('p cgnapt stats')
367 def test_run_vcgnapt(self, *args):
368 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
369 cgnapt_approx_vnf = cgnapt_vnf.CgnaptApproxVnf(name, vnfd)
370 cgnapt_approx_vnf.ssh_helper = mock.Mock()
371 cgnapt_approx_vnf.setup_helper = mock.Mock()
372 with mock.patch.object(cgnapt_approx_vnf, '_build_config'), \
373 mock.patch.object(cgnapt_approx_vnf, '_build_run_kwargs'):
374 cgnapt_approx_vnf._run()
376 cgnapt_approx_vnf.ssh_helper.run.assert_called_once()
377 cgnapt_approx_vnf.setup_helper.kill_vnf.assert_called_once()
379 @mock.patch.object(ctx_base.Context, 'get_context_from_server')
380 def test_instantiate(self, *args):
381 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
382 cgnapt_approx_vnf = cgnapt_vnf.CgnaptApproxVnf(name, vnfd)
383 cgnapt_approx_vnf.deploy_helper = mock.MagicMock()
384 cgnapt_approx_vnf.resource_helper = mock.MagicMock()
385 cgnapt_approx_vnf._build_config = mock.MagicMock()
386 self.scenario_cfg['vnf_options'] = {'acl': {'cfg': "",
388 cgnapt_approx_vnf.q_out.put("pipeline>")
389 cgnapt_vnf.WAIT_TIME = 3
390 self.scenario_cfg.update({"nodes": {"vnf__0": ""}})
391 with mock.patch.object(cgnapt_approx_vnf, '_start_vnf'):
392 self.assertIsNone(cgnapt_approx_vnf.instantiate(
393 self.scenario_cfg, self.context_cfg))
395 @mock.patch.object(time, 'sleep')
396 def test__vnf_up_post(self, *args):
397 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
398 self.scenario_cfg['options'][name]['napt'] = 'static'
399 cgnapt_approx_vnf = cgnapt_vnf.CgnaptApproxVnf(name, vnfd)
400 cgnapt_approx_vnf.vnf_execute = mock.Mock()
401 cgnapt_approx_vnf.scenario_helper.scenario_cfg = self.scenario_cfg
402 with mock.patch.object(cgnapt_approx_vnf, 'setup_helper') as \
404 mock_setup_helper._generate_ip_from_pool.return_value = ['ip1']
405 mock_setup_helper._get_cgnapt_config.return_value = ['gw_ip1']
406 cgnapt_approx_vnf._vnf_up_post()
408 def test__vnf_up_post_short(self, *args):
409 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
410 cgnapt_approx_vnf = cgnapt_vnf.CgnaptApproxVnf(name, vnfd)
411 cgnapt_approx_vnf.scenario_helper.scenario_cfg = self.scenario_cfg
412 cgnapt_approx_vnf._vnf_up_post()