Merge "NSB test: remove vld_id it is not needed"
[yardstick.git] / tests / unit / network_services / vnf_generic / vnf / test_cgnapt_vnf.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2016-2017 Intel Corporation
4 #
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
8 #
9 #      http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16 #
17
18 from __future__ import absolute_import
19
20 import os
21 import unittest
22 import mock
23
24 from copy import deepcopy
25
26 from tests.unit import STL_MOCKS
27 from tests.unit.network_services.vnf_generic.vnf.test_base import mock_ssh
28
29
30 STLClient = mock.MagicMock()
31 stl_patch = mock.patch.dict("sys.modules", STL_MOCKS)
32 stl_patch.start()
33
34 if stl_patch:
35     from yardstick.network_services.vnf_generic.vnf.cgnapt_vnf import CgnaptApproxVnf, \
36         CgnaptApproxSetupEnvHelper
37     from yardstick.network_services.vnf_generic.vnf import cgnapt_vnf
38     from yardstick.network_services.nfvi.resource import ResourceProfile
39
40 TEST_FILE_YAML = 'nsb_test_case.yaml'
41 SSH_HELPER = 'yardstick.network_services.vnf_generic.vnf.sample_vnf.VnfSshHelper'
42
43
44 name = 'vnf__1'
45
46
47 class TestCgnaptApproxSetupEnvHelper(unittest.TestCase):
48
49     def test__generate_ip_from_pool(self):
50
51         ip = CgnaptApproxSetupEnvHelper._generate_ip_from_pool("1.2.3.4")
52         self.assertEqual(next(ip), '1.2.3.4')
53         self.assertEqual(next(ip), '1.2.4.4')
54         self.assertEqual(next(ip), '1.2.5.4')
55
56     def test__update_cgnat_script_file(self):
57
58         sample = """\
59 # See the License for the specific language governing permissions and
60 # limitations under the License.
61
62 link 0 down
63 link 0 config {port0_local_ip} {port0_prefixlen}
64 link 0 up
65 link 1 down
66 link 1 config {port1_local_ip} {port1_prefixlen}
67 link 1 up
68 """
69         header = "This is a header"
70
71         out = CgnaptApproxSetupEnvHelper._update_cgnat_script_file(header, sample.splitlines())
72         self.assertNotIn("This is a header", out)
73
74     def test__get_cgnapt_config(self):
75         vnfd_helper = mock.Mock()
76         vnfd_helper.port_pairs.priv_ports = [{"name": 'a'}, {"name": "b"}, {"name": "c"}]
77
78         helper = CgnaptApproxSetupEnvHelper(vnfd_helper, mock.Mock(), mock.Mock())
79         helper._get_ports_gateway = mock.Mock(side_effect=[3, 5, 2])
80         result = helper._get_cgnapt_config([{"name": 'a'}, {}, {"name": "b"}, {}, {"name": "c"}])
81         self.assertEqual(result, [3, 5, 2])
82
83     def test_scale(self):
84         helper = CgnaptApproxSetupEnvHelper(mock.Mock(), mock.Mock(), mock.Mock())
85         with self.assertRaises(NotImplementedError):
86             helper.scale()
87
88
89 @mock.patch("yardstick.network_services.vnf_generic.vnf.sample_vnf.Process")
90 class TestCgnaptApproxVnf(unittest.TestCase):
91     VNFD = {'vnfd:vnfd-catalog':
92             {'vnfd':
93              [{'short-name': 'VpeVnf',
94                'vdu':
95                [{'routing_table':
96                  [{'network': '152.16.100.20',
97                    'netmask': '255.255.255.0',
98                    'gateway': '152.16.100.20',
99                    'if': 'xe0'},
100                   {'network': '152.16.40.20',
101                    'netmask': '255.255.255.0',
102                    'gateway': '152.16.40.20',
103                    'if': 'xe1'}],
104                  'description': 'VPE approximation using DPDK',
105                  'name': 'vpevnf-baremetal',
106                  'nd_route_tbl':
107                  [{'network': '0064:ff9b:0:0:0:0:9810:6414',
108                    'netmask': '112',
109                    'gateway': '0064:ff9b:0:0:0:0:9810:6414',
110                    'if': 'xe0'},
111                   {'network': '0064:ff9b:0:0:0:0:9810:2814',
112                    'netmask': '112',
113                    'gateway': '0064:ff9b:0:0:0:0:9810:2814',
114                    'if': 'xe1'}],
115                  'id': 'vpevnf-baremetal',
116                  'external-interface':
117                  [{'virtual-interface':
118                    {'dst_mac': '00:00:00:00:00:04',
119                     'vpci': '0000:05:00.0',
120                     'local_ip': '152.16.100.19',
121                     'type': 'PCI-PASSTHROUGH',
122                     'netmask': '255.255.255.0',
123                     'dpdk_port_num': 0,
124                     'bandwidth': '10 Gbps',
125                     'driver': "i40e",
126                     'dst_ip': '152.16.100.20',
127                     'local_iface_name': 'xe0',
128                     'local_mac': '00:00:00:00:00:02'},
129                    'vnfd-connection-point-ref': 'xe0',
130                    'name': 'xe0'},
131                   {'virtual-interface':
132                    {'dst_mac': '00:00:00:00:00:03',
133                     'vpci': '0000:05:00.1',
134                     'local_ip': '152.16.40.19',
135                     'type': 'PCI-PASSTHROUGH',
136                     'driver': "i40e",
137                     'netmask': '255.255.255.0',
138                     'dpdk_port_num': 1,
139                     'bandwidth': '10 Gbps',
140                     'dst_ip': '152.16.40.20',
141                     'local_iface_name': 'xe1',
142                     'local_mac': '00:00:00:00:00:01'},
143                    'vnfd-connection-point-ref': 'xe1',
144                    'name': 'xe1'}]}],
145                'description': 'Vpe approximation using DPDK',
146                'mgmt-interface':
147                    {'vdu-id': 'vpevnf-baremetal',
148                     'host': '1.2.1.1',
149                     'password': 'r00t',
150                     'user': 'root',
151                     'ip': '1.2.1.1'},
152                'benchmark':
153                    {'kpi': ['packets_in', 'packets_fwd', 'packets_dropped']},
154                'connection-point': [{'type': 'VPORT', 'name': 'xe0'},
155                                     {'type': 'VPORT', 'name': 'xe1'}],
156                'id': 'CgnaptApproxVnf', 'name': 'VPEVnfSsh'}]}}
157
158     SCENARIO_CFG = {
159         'options': {
160             'packetsize': 64,
161             'traffic_type': 4,
162             'rfc2544': {
163                 'allowed_drop_rate': '0.8 - 1',
164             },
165             'vnf__1': {
166                 'napt': 'dynamic',
167                 'vnf_config': {
168                     'lb_config': 'SW',
169                     'lb_count': 1,
170                     'worker_config':
171                     '1C/1T',
172                     'worker_threads': 1,
173                 },
174             },
175         },
176         'task_id': 'a70bdf4a-8e67-47a3-9dc1-273c14506eb7',
177         'task_path': '/tmp',
178         'tc': 'tc_ipv4_1Mflow_64B_packetsize',
179         'runner': {
180             'object': 'NetworkServiceTestCase',
181             'interval': 35,
182             'output_filename': '/tmp/yardstick.out',
183             'runner_id': 74476,
184             'duration': 400,
185             'type': 'Duration',
186         },
187         'traffic_profile': 'ipv4_throughput_acl.yaml',
188         'traffic_options': {
189             'flow': 'ipv4_Packets_acl.yaml',
190             'imix': 'imix_voice.yaml',
191         },
192         'type': 'ISB',
193         'nodes': {
194             'tg__2': 'trafficgen_2.yardstick',
195             'tg__1': 'trafficgen_1.yardstick',
196             'vnf__1': 'vnf.yardstick',
197         },
198         'topology': 'vpe-tg-topology-baremetal.yaml',
199     }
200
201     context_cfg = {'nodes': {'tg__2':
202                              {'member-vnf-index': '3',
203                               'role': 'TrafficGen',
204                               'name': 'trafficgen_2.yardstick',
205                               'vnfd-id-ref': 'tg__2',
206                               'ip': '1.2.1.1',
207                               'interfaces':
208                               {'xe0': {'local_iface_name': 'ens513f0',
209                                        'vld_id': 'public',
210                                        'netmask': '255.255.255.0',
211                                        'local_ip': '152.16.40.20',
212                                        'dst_mac': '00:00:00:00:00:01',
213                                        'local_mac': '00:00:00:00:00:03',
214                                        'dst_ip': '152.16.40.19',
215                                        'driver': 'ixgbe',
216                                        'vpci': '0000:02:00.0',
217                                        'dpdk_port_num': 0},
218                                'xe1': {'local_iface_name': 'ens513f1',
219                                        'netmask': '255.255.255.0',
220                                        'network': '202.16.100.0',
221                                        'local_ip': '202.16.100.20',
222                                        'local_mac': '00:1e:67:d0:60:5d',
223                                        'driver': 'ixgbe',
224                                        'vpci': '0000:02:00.1',
225                                        'dpdk_port_num': 1}},
226                               'password': 'r00t',
227                               'VNF model': 'l3fwd_vnf.yaml',
228                               'user': 'root'},
229                              'tg__1':
230                              {'member-vnf-index': '1',
231                               'role': 'TrafficGen',
232                               'name': 'trafficgen_1.yardstick',
233                               'vnfd-id-ref': 'tg__1',
234                               'ip': '1.2.1.1',
235                               'interfaces':
236                               {'xe0': {'local_iface_name': 'ens785f0',
237                                        'vld_id': 'private',
238                                        'netmask': '255.255.255.0',
239                                        'local_ip': '152.16.100.20',
240                                        'dst_mac': '00:00:00:00:00:02',
241                                        'local_mac': '00:00:00:00:00:04',
242                                        'dst_ip': '152.16.100.19',
243                                        'driver': 'i40e',
244                                        'vpci': '0000:05:00.0',
245                                        'dpdk_port_num': 0},
246                                'xe1': {'local_iface_name': 'ens785f1',
247                                        'netmask': '255.255.255.0',
248                                        'local_ip': '152.16.100.21',
249                                        'local_mac': '00:00:00:00:00:01',
250                                        'driver': 'i40e',
251                                        'vpci': '0000:05:00.1',
252                                        'dpdk_port_num': 1}},
253                               'password': 'r00t',
254                               'VNF model': 'tg_rfc2544_tpl.yaml',
255                               'user': 'root'},
256                              'vnf__1':
257                              {'name': 'vnf.yardstick',
258                               'vnfd-id-ref': 'vnf__1',
259                               'ip': '1.2.1.1',
260                               'interfaces':
261                               {'xe0': {'local_iface_name': 'ens786f0',
262                                        'vld_id': 'private',
263                                        'netmask': '255.255.255.0',
264                                        'local_ip': '152.16.100.19',
265                                        'dst_mac': '00:00:00:00:00:04',
266                                        'local_mac': '00:00:00:00:00:02',
267                                        'dst_ip': '152.16.100.20',
268                                        'driver': 'i40e',
269                                        'vpci': '0000:05:00.0',
270                                        'dpdk_port_num': 0},
271                                'xe1': {'local_iface_name': 'ens786f1',
272                                        'vld_id': 'public',
273                                        'netmask': '255.255.255.0',
274                                        'local_ip': '152.16.40.19',
275                                        'dst_mac': '00:00:00:00:00:03',
276                                        'local_mac': '00:00:00:00:00:01',
277                                        'dst_ip': '152.16.40.20',
278                                        'driver': 'i40e',
279                                        'vpci': '0000:05:00.1',
280                                        'dpdk_port_num': 1}},
281                               'routing_table':
282                               [{'netmask': '255.255.255.0',
283                                 'gateway': '152.16.100.20',
284                                 'network': '152.16.100.20',
285                                 'if': 'xe0'},
286                                {'netmask': '255.255.255.0',
287                                 'gateway': '152.16.40.20',
288                                 'network': '152.16.40.20',
289                                 'if': 'xe1'}],
290                               'member-vnf-index': '2',
291                               'host': '1.2.1.1',
292                               'role': 'vnf',
293                               'user': 'root',
294                               'nd_route_tbl':
295                               [{'netmask': '112',
296                                 'gateway': '0064:ff9b:0:0:0:0:9810:6414',
297                                 'network': '0064:ff9b:0:0:0:0:9810:6414',
298                                 'if': 'xe0'},
299                                {'netmask': '112',
300                                 'gateway': '0064:ff9b:0:0:0:0:9810:2814',
301                                 'network': '0064:ff9b:0:0:0:0:9810:2814',
302                                 'if': 'xe1'}],
303                               'password': 'r00t',
304                               'VNF model': 'cgnapt_vnf.yaml'}}}
305
306     def setUp(self):
307         self.scenario_cfg = deepcopy(self.SCENARIO_CFG)
308
309     def test___init__(self, mock_process):
310         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
311         cgnapt_approx_vnf = CgnaptApproxVnf(name, vnfd)
312         self.assertIsNone(cgnapt_approx_vnf._vnf_process)
313
314     @mock.patch(SSH_HELPER)
315     def test_collect_kpi(self, ssh, mock_process):
316         mock_ssh(ssh)
317
318         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
319         cgnapt_approx_vnf = CgnaptApproxVnf(name, vnfd)
320         cgnapt_approx_vnf.q_in = mock.MagicMock()
321         cgnapt_approx_vnf.q_out = mock.MagicMock()
322         cgnapt_approx_vnf.q_out.qsize = mock.Mock(return_value=0)
323         cgnapt_approx_vnf.resource = mock.Mock(autospec=ResourceProfile)
324         result = {'packets_dropped': 0, 'packets_fwd': 0, 'packets_in': 0}
325         self.assertEqual(result, cgnapt_approx_vnf.collect_kpi())
326
327     @mock.patch(SSH_HELPER)
328     def test_vnf_execute_command(self, ssh, mock_process):
329         mock_ssh(ssh)
330
331         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
332         cgnapt_approx_vnf = CgnaptApproxVnf(name, vnfd)
333         cgnapt_approx_vnf.q_in = mock.MagicMock()
334         cgnapt_approx_vnf.q_out = mock.MagicMock()
335         cgnapt_approx_vnf.q_out.qsize = mock.Mock(return_value=0)
336         cmd = "quit"
337         self.assertEqual("", cgnapt_approx_vnf.vnf_execute(cmd))
338
339     @mock.patch(SSH_HELPER)
340     def test_get_stats(self, ssh, mock_process):
341         mock_ssh(ssh)
342
343         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
344         cgnapt_approx_vnf = CgnaptApproxVnf(name, vnfd)
345         cgnapt_approx_vnf.q_in = mock.MagicMock()
346         cgnapt_approx_vnf.q_out = mock.MagicMock()
347         cgnapt_approx_vnf.q_out.qsize = mock.Mock(return_value=0)
348         result = \
349             "CG-NAPT(.*\n)*Received 100, Missed 0, Dropped 0,Translated 100,ingress"
350         cgnapt_approx_vnf.vnf_execute = mock.Mock(return_value=result)
351         self.assertListEqual(list(result), list(cgnapt_approx_vnf.get_stats()))
352
353     def _get_file_abspath(self, filename):
354         curr_path = os.path.dirname(os.path.abspath(__file__))
355         file_path = os.path.join(curr_path, filename)
356         return file_path
357
358     @mock.patch("yardstick.network_services.vnf_generic.vnf.cgnapt_vnf.hex")
359     @mock.patch("yardstick.network_services.vnf_generic.vnf.cgnapt_vnf.eval")
360     @mock.patch('yardstick.network_services.vnf_generic.vnf.cgnapt_vnf.open')
361     @mock.patch(SSH_HELPER)
362     def test_run_vcgnapt(self, ssh, mock_hex, mock_eval, mock_open, mock_process):
363         mock_ssh(ssh)
364
365         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
366         cgnapt_approx_vnf = CgnaptApproxVnf(name, vnfd)
367         cgnapt_approx_vnf._build_config = mock.MagicMock()
368         cgnapt_approx_vnf.queue_wrapper = mock.MagicMock()
369         cgnapt_approx_vnf.ssh_helper = mock.MagicMock()
370         cgnapt_approx_vnf.ssh_helper.run = mock.MagicMock()
371         cgnapt_approx_vnf.scenario_helper.scenario_cfg = self.scenario_cfg
372         cgnapt_approx_vnf._run()
373         cgnapt_approx_vnf.ssh_helper.run.assert_called_once()
374
375     @mock.patch("yardstick.network_services.vnf_generic.vnf.sample_vnf.Context")
376     @mock.patch(SSH_HELPER)
377     def test_instantiate(self, ssh, mock_context, mock_process):
378         mock_ssh(ssh)
379
380         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
381         cgnapt_approx_vnf = CgnaptApproxVnf(name, vnfd)
382         cgnapt_approx_vnf.deploy_helper = mock.MagicMock()
383         cgnapt_approx_vnf.resource_helper = mock.MagicMock()
384         cgnapt_approx_vnf._build_config = mock.MagicMock()
385         self.scenario_cfg['vnf_options'] = {'acl': {'cfg': "",
386                                                     'rules': ""}}
387         cgnapt_approx_vnf.q_out.put("pipeline>")
388         cgnapt_vnf.WAIT_TIME = 3
389         self.scenario_cfg.update({"nodes": {"vnf__1": ""}})
390         self.assertIsNone(cgnapt_approx_vnf.instantiate(self.scenario_cfg,
391                                                         self.context_cfg))
392
393     def test_scale(self, mock_process):
394         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
395         cgnapt_approx_vnf = CgnaptApproxVnf(name, vnfd)
396         flavor = ""
397         self.assertRaises(NotImplementedError, cgnapt_approx_vnf.scale, flavor)
398
399     @mock.patch("yardstick.network_services.vnf_generic.vnf.sample_vnf.time")
400     @mock.patch(SSH_HELPER)
401     def test_terminate(self, ssh, mock_time, mock_process):
402         mock_ssh(ssh)
403
404         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
405         cgnapt_approx_vnf = CgnaptApproxVnf(name, vnfd)
406         cgnapt_approx_vnf._vnf_process = mock.MagicMock()
407         cgnapt_approx_vnf._vnf_process.terminate = mock.Mock()
408         cgnapt_approx_vnf.used_drivers = {"01:01.0": "i40e",
409                                           "01:01.1": "i40e"}
410         cgnapt_approx_vnf.vnf_execute = mock.MagicMock()
411         cgnapt_approx_vnf.dpdk_nic_bind = "dpdk_nic_bind.py"
412         cgnapt_approx_vnf._resource_collect_stop = mock.Mock()
413         self.assertEqual(None, cgnapt_approx_vnf.terminate())
414
415     @mock.patch("yardstick.network_services.vnf_generic.vnf.sample_vnf.time")
416     @mock.patch(SSH_HELPER)
417     def test__vnf_up_post(self, ssh, mock_time, mock_process):
418         mock_ssh(ssh)
419
420         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
421         self.scenario_cfg['options'][name]['napt'] = 'static'
422
423         cgnapt_approx_vnf = CgnaptApproxVnf(name, vnfd)
424         cgnapt_approx_vnf._vnf_process = mock.MagicMock()
425         cgnapt_approx_vnf._vnf_process.terminate = mock.Mock()
426         cgnapt_approx_vnf.vnf_execute = mock.MagicMock()
427         cgnapt_approx_vnf.scenario_helper.scenario_cfg = self.scenario_cfg
428         cgnapt_approx_vnf._resource_collect_stop = mock.Mock()
429         cgnapt_approx_vnf._vnf_up_post()
430
431     @mock.patch("yardstick.network_services.vnf_generic.vnf.sample_vnf.time")
432     @mock.patch(SSH_HELPER)
433     def test__vnf_up_post_short(self, ssh, mock_time, mock_process):
434         mock_ssh(ssh)
435
436         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
437         cgnapt_approx_vnf = CgnaptApproxVnf(name, vnfd)
438         cgnapt_approx_vnf._vnf_process = mock.MagicMock()
439         cgnapt_approx_vnf._vnf_process.terminate = mock.Mock()
440         cgnapt_approx_vnf.vnf_execute = mock.MagicMock()
441         cgnapt_approx_vnf.scenario_helper.scenario_cfg = self.scenario_cfg
442         cgnapt_approx_vnf._resource_collect_stop = mock.Mock()
443         cgnapt_approx_vnf._vnf_up_post()
444
445
446 if __name__ == '__main__':
447     unittest.main()