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 tests.unit import STL_MOCKS
25 from tests.unit.network_services.vnf_generic.vnf.test_base import mock_ssh
28 STLClient = mock.MagicMock()
29 stl_patch = mock.patch.dict("sys.modules", STL_MOCKS)
33 from yardstick.network_services.vnf_generic.vnf.cgnapt_vnf import CgnaptApproxVnf, \
34 CgnaptApproxSetupEnvHelper
35 from yardstick.network_services.vnf_generic.vnf import cgnapt_vnf
36 from yardstick.network_services.nfvi.resource import ResourceProfile
38 TEST_FILE_YAML = 'nsb_test_case.yaml'
39 SSH_HELPER = 'yardstick.network_services.vnf_generic.vnf.sample_vnf.VnfSshHelper'
45 class TestCgnaptApproxSetupEnvHelper(unittest.TestCase):
47 def test__generate_ip_from_pool(self):
49 ip = CgnaptApproxSetupEnvHelper._generate_ip_from_pool("1.2.3.4")
50 self.assertEqual(next(ip), '1.2.3.4')
51 self.assertEqual(next(ip), '1.2.4.4')
52 self.assertEqual(next(ip), '1.2.5.4')
54 def test__update_cgnat_script_file(self):
57 # See the License for the specific language governing permissions and
58 # limitations under the License.
61 link 0 config {port0_local_ip} {port0_prefixlen}
64 link 1 config {port1_local_ip} {port1_prefixlen}
67 header = "This is a header"
69 out = CgnaptApproxSetupEnvHelper._update_cgnat_script_file(header, sample.splitlines(), "")
70 self.assertNotIn("This is a header", out)
72 def test__get_cgnapt_confgi(self):
74 c = CgnaptApproxSetupEnvHelper(mock.MagicMock(), mock.MagicMock(), mock.MagicMock())
75 c._get_ports_gateway = mock.Mock(return_value=3)
76 ret = c._get_cgnapt_config([{"name": 'a'}, {}, {"name": "b"}, {}, {"name": "c"}])
77 self.assertEqual(ret, [3, 3, 3])
80 @mock.patch("yardstick.network_services.vnf_generic.vnf.sample_vnf.Process")
81 class TestCgnaptApproxVnf(unittest.TestCase):
82 VNFD = {'vnfd:vnfd-catalog':
84 [{'short-name': 'VpeVnf',
87 [{'network': '152.16.100.20',
88 'netmask': '255.255.255.0',
89 'gateway': '152.16.100.20',
91 {'network': '152.16.40.20',
92 'netmask': '255.255.255.0',
93 'gateway': '152.16.40.20',
95 'description': 'VPE approximation using DPDK',
96 'name': 'vpevnf-baremetal',
98 [{'network': '0064:ff9b:0:0:0:0:9810:6414',
100 'gateway': '0064:ff9b:0:0:0:0:9810:6414',
102 {'network': '0064:ff9b:0:0:0:0:9810:2814',
104 'gateway': '0064:ff9b:0:0:0:0:9810:2814',
106 'id': 'vpevnf-baremetal',
107 'external-interface':
108 [{'virtual-interface':
109 {'dst_mac': '00:00:00:00:00:04',
110 'vpci': '0000:05:00.0',
111 'local_ip': '152.16.100.19',
112 'type': 'PCI-PASSTHROUGH',
113 'netmask': '255.255.255.0',
114 'dpdk_port_num': '0',
115 'bandwidth': '10 Gbps',
117 'dst_ip': '152.16.100.20',
118 'local_iface_name': 'xe0',
119 'local_mac': '00:00:00:00:00:02'},
120 'vnfd-connection-point-ref': 'xe0',
122 {'virtual-interface':
123 {'dst_mac': '00:00:00:00:00:03',
124 'vpci': '0000:05:00.1',
125 'local_ip': '152.16.40.19',
126 'type': 'PCI-PASSTHROUGH',
128 'netmask': '255.255.255.0',
129 'dpdk_port_num': '1',
130 'bandwidth': '10 Gbps',
131 'dst_ip': '152.16.40.20',
132 'local_iface_name': 'xe1',
133 'local_mac': '00:00:00:00:00:01'},
134 'vnfd-connection-point-ref': 'xe1',
136 'description': 'Vpe approximation using DPDK',
138 {'vdu-id': 'vpevnf-baremetal',
144 {'kpi': ['packets_in', 'packets_fwd', 'packets_dropped']},
145 'connection-point': [{'type': 'VPORT', 'name': 'xe0'},
146 {'type': 'VPORT', 'name': 'xe1'}],
147 'id': 'CgnaptApproxVnf', 'name': 'VPEVnfSsh'}]}}
149 scenario_cfg = {'options': {'packetsize': 64, 'traffic_type': 4,
150 'rfc2544': {'allowed_drop_rate': '0.8 - 1'},
151 'vnf__1': {'rules': 'acl_1rule.yaml',
152 'vnf_config': {'lb_config': 'SW',
156 'worker_threads': 1}}
158 'task_id': 'a70bdf4a-8e67-47a3-9dc1-273c14506eb7',
160 'tc': 'tc_ipv4_1Mflow_64B_packetsize',
161 'runner': {'object': 'NetworkServiceTestCase',
163 'output_filename': '/tmp/yardstick.out',
164 'runner_id': 74476, 'duration': 400,
166 'traffic_profile': 'ipv4_throughput_acl.yaml',
167 'traffic_options': {'flow': 'ipv4_Packets_acl.yaml',
168 'imix': 'imix_voice.yaml'},
170 'nodes': {'tg__2': 'trafficgen_2.yardstick',
171 'tg__1': 'trafficgen_1.yardstick',
172 'vnf__1': 'vnf.yardstick'},
173 'topology': 'vpe-tg-topology-baremetal.yaml'}
175 context_cfg = {'nodes': {'tg__2':
176 {'member-vnf-index': '3',
177 'role': 'TrafficGen',
178 'name': 'trafficgen_2.yardstick',
179 'vnfd-id-ref': 'tg__2',
182 {'xe0': {'local_iface_name': 'ens513f0',
184 'netmask': '255.255.255.0',
185 'local_ip': '152.16.40.20',
186 'dst_mac': '00:00:00:00:00:01',
187 'local_mac': '00:00:00:00:00:03',
188 'dst_ip': '152.16.40.19',
190 'vpci': '0000:02:00.0',
192 'xe1': {'local_iface_name': 'ens513f1',
193 'netmask': '255.255.255.0',
194 'network': '202.16.100.0',
195 'local_ip': '202.16.100.20',
196 'local_mac': '00:1e:67:d0:60:5d',
198 'vpci': '0000:02:00.1',
199 'dpdk_port_num': 1}},
201 'VNF model': 'l3fwd_vnf.yaml',
204 {'member-vnf-index': '1',
205 'role': 'TrafficGen',
206 'name': 'trafficgen_1.yardstick',
207 'vnfd-id-ref': 'tg__1',
210 {'xe0': {'local_iface_name': 'ens785f0',
212 'netmask': '255.255.255.0',
213 'local_ip': '152.16.100.20',
214 'dst_mac': '00:00:00:00:00:02',
215 'local_mac': '00:00:00:00:00:04',
216 'dst_ip': '152.16.100.19',
218 'vpci': '0000:05:00.0',
220 'xe1': {'local_iface_name': 'ens785f1',
221 'netmask': '255.255.255.0',
222 'local_ip': '152.16.100.21',
223 'local_mac': '00:00:00:00:00:01',
225 'vpci': '0000:05:00.1',
226 'dpdk_port_num': 1}},
228 'VNF model': 'tg_rfc2544_tpl.yaml',
231 {'name': 'vnf.yardstick',
232 'vnfd-id-ref': 'vnf__1',
235 {'xe0': {'local_iface_name': 'ens786f0',
237 'netmask': '255.255.255.0',
238 'local_ip': '152.16.100.19',
239 'dst_mac': '00:00:00:00:00:04',
240 'local_mac': '00:00:00:00:00:02',
241 'dst_ip': '152.16.100.20',
243 'vpci': '0000:05:00.0',
245 'xe1': {'local_iface_name': 'ens786f1',
247 'netmask': '255.255.255.0',
248 'local_ip': '152.16.40.19',
249 'dst_mac': '00:00:00:00:00:03',
250 'local_mac': '00:00:00:00:00:01',
251 'dst_ip': '152.16.40.20',
253 'vpci': '0000:05:00.1',
254 'dpdk_port_num': 1}},
256 [{'netmask': '255.255.255.0',
257 'gateway': '152.16.100.20',
258 'network': '152.16.100.20',
260 {'netmask': '255.255.255.0',
261 'gateway': '152.16.40.20',
262 'network': '152.16.40.20',
264 'member-vnf-index': '2',
270 'gateway': '0064:ff9b:0:0:0:0:9810:6414',
271 'network': '0064:ff9b:0:0:0:0:9810:6414',
274 'gateway': '0064:ff9b:0:0:0:0:9810:2814',
275 'network': '0064:ff9b:0:0:0:0:9810:2814',
278 'VNF model': 'cgnapt_vnf.yaml'}}}
280 def test___init__(self, mock_process):
281 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
282 cgnapt_approx_vnf = CgnaptApproxVnf(name, vnfd)
283 self.assertIsNone(cgnapt_approx_vnf._vnf_process)
285 @mock.patch("yardstick.network_services.vnf_generic.vnf.sample_vnf.time")
286 @mock.patch(SSH_HELPER)
287 def test_collect_kpi(self, ssh, mock_time, mock_process):
290 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
291 cgnapt_approx_vnf = CgnaptApproxVnf(name, vnfd)
292 cgnapt_approx_vnf.q_in = mock.MagicMock()
293 cgnapt_approx_vnf.q_out = mock.MagicMock()
294 cgnapt_approx_vnf.q_out.qsize = mock.Mock(return_value=0)
295 cgnapt_approx_vnf.resource = mock.Mock(autospec=ResourceProfile)
296 result = {'packets_dropped': 0, 'packets_fwd': 0, 'packets_in': 0}
297 self.assertEqual(result, cgnapt_approx_vnf.collect_kpi())
299 @mock.patch("yardstick.network_services.vnf_generic.vnf.sample_vnf.time")
300 @mock.patch(SSH_HELPER)
301 def test_vnf_execute_command(self, ssh, mock_time, mock_process):
304 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
305 cgnapt_approx_vnf = CgnaptApproxVnf(name, vnfd)
306 cgnapt_approx_vnf.q_in = mock.MagicMock()
307 cgnapt_approx_vnf.q_out = mock.MagicMock()
308 cgnapt_approx_vnf.q_out.qsize = mock.Mock(return_value=0)
310 self.assertEqual("", cgnapt_approx_vnf.vnf_execute(cmd))
312 @mock.patch(SSH_HELPER)
313 def test_get_stats(self, ssh, mock_process):
316 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
317 cgnapt_approx_vnf = CgnaptApproxVnf(name, vnfd)
318 cgnapt_approx_vnf.q_in = mock.MagicMock()
319 cgnapt_approx_vnf.q_out = mock.MagicMock()
320 cgnapt_approx_vnf.q_out.qsize = mock.Mock(return_value=0)
322 "CG-NAPT(.*\n)*Received 100, Missed 0, Dropped 0,Translated 100,ingress"
323 cgnapt_approx_vnf.vnf_execute = mock.Mock(return_value=result)
324 self.assertListEqual(list(result), list(cgnapt_approx_vnf.get_stats()))
326 def _get_file_abspath(self, filename):
327 curr_path = os.path.dirname(os.path.abspath(__file__))
328 file_path = os.path.join(curr_path, filename)
331 @mock.patch("yardstick.network_services.vnf_generic.vnf.cgnapt_vnf.hex")
332 @mock.patch("yardstick.network_services.vnf_generic.vnf.cgnapt_vnf.eval")
333 @mock.patch('yardstick.network_services.vnf_generic.vnf.cgnapt_vnf.open')
334 @mock.patch(SSH_HELPER)
335 def test_run_vcgnapt(self, ssh, mock_hex, mock_eval, mock_open, mock_process):
338 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
339 cgnapt_approx_vnf = CgnaptApproxVnf(name, vnfd)
340 cgnapt_approx_vnf._build_config = mock.MagicMock()
341 cgnapt_approx_vnf.queue_wrapper = mock.MagicMock()
342 cgnapt_approx_vnf.ssh_helper = mock.MagicMock()
343 cgnapt_approx_vnf.ssh_helper.run = mock.MagicMock()
344 cgnapt_approx_vnf.scenario_helper.scenario_cfg = self.scenario_cfg
345 cgnapt_approx_vnf._run()
346 cgnapt_approx_vnf.ssh_helper.run.assert_called_once()
348 @mock.patch("yardstick.network_services.vnf_generic.vnf.sample_vnf.Context")
349 @mock.patch(SSH_HELPER)
350 def test_instantiate(self, ssh, mock_context, mock_process):
353 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
354 cgnapt_approx_vnf = CgnaptApproxVnf(name, vnfd)
355 cgnapt_approx_vnf.deploy_helper = mock.MagicMock()
356 cgnapt_approx_vnf.resource_helper = mock.MagicMock()
357 cgnapt_approx_vnf._build_config = mock.MagicMock()
358 self.scenario_cfg['vnf_options'] = {'acl': {'cfg': "",
360 cgnapt_approx_vnf.q_out.put("pipeline>")
361 cgnapt_vnf.WAIT_TIME = 3
362 self.scenario_cfg.update({"nodes": {"vnf__1": ""}})
363 self.assertIsNone(cgnapt_approx_vnf.instantiate(self.scenario_cfg,
366 def test_scale(self, mock_process):
367 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
368 cgnapt_approx_vnf = CgnaptApproxVnf(name, vnfd)
370 self.assertRaises(NotImplementedError, cgnapt_approx_vnf.scale, flavor)
372 @mock.patch("yardstick.network_services.vnf_generic.vnf.sample_vnf.time")
373 @mock.patch(SSH_HELPER)
374 def test_terminate(self, ssh, mock_time, mock_process):
377 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
378 cgnapt_approx_vnf = CgnaptApproxVnf(name, vnfd)
379 cgnapt_approx_vnf._vnf_process = mock.MagicMock()
380 cgnapt_approx_vnf._vnf_process.terminate = mock.Mock()
381 cgnapt_approx_vnf.used_drivers = {"01:01.0": "i40e",
383 cgnapt_approx_vnf.vnf_execute = mock.MagicMock()
384 cgnapt_approx_vnf.dpdk_nic_bind = "dpdk_nic_bind.py"
385 cgnapt_approx_vnf._resource_collect_stop = mock.Mock()
386 self.assertEqual(None, cgnapt_approx_vnf.terminate())
388 @mock.patch("yardstick.network_services.vnf_generic.vnf.sample_vnf.time")
389 @mock.patch("yardstick.network_services.vnf_generic.vnf.cgnapt_vnf.time")
390 @mock.patch(SSH_HELPER)
391 def test__vnf_up_post(self, ssh, mock_time, mock_cgnapt_time, mock_process):
394 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
395 cgnapt_approx_vnf = CgnaptApproxVnf(name, vnfd)
396 cgnapt_approx_vnf._vnf_process = mock.MagicMock()
397 cgnapt_approx_vnf._vnf_process.terminate = mock.Mock()
398 cgnapt_approx_vnf.vnf_execute = mock.MagicMock()
399 cgnapt_approx_vnf.scenario_helper.scenario_cfg = self.scenario_cfg
400 cgnapt_approx_vnf._resource_collect_stop = mock.Mock()
401 cgnapt_approx_vnf._vnf_up_post()
402 cgnapt_approx_vnf.vnf_execute.assert_called_once()
405 if __name__ == '__main__':