Merge "Yardstick TC082: move sample test case perf.yaml"
[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 tests.unit import STL_MOCKS
25 from tests.unit.network_services.vnf_generic.vnf.test_base import mock_ssh
26
27
28 STLClient = mock.MagicMock()
29 stl_patch = mock.patch.dict("sys.modules", STL_MOCKS)
30 stl_patch.start()
31
32 if stl_patch:
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
37
38 TEST_FILE_YAML = 'nsb_test_case.yaml'
39 SSH_HELPER = 'yardstick.network_services.vnf_generic.vnf.sample_vnf.VnfSshHelper'
40
41
42 name = 'vnf__1'
43
44
45 class TestCgnaptApproxSetupEnvHelper(unittest.TestCase):
46
47     def test__generate_ip_from_pool(self):
48
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')
53
54     def test__update_cgnat_script_file(self):
55
56         sample = """\
57 # See the License for the specific language governing permissions and
58 # limitations under the License.
59
60 link 0 down
61 link 0 config {port0_local_ip} {port0_prefixlen}
62 link 0 up
63 link 1 down
64 link 1 config {port1_local_ip} {port1_prefixlen}
65 link 1 up
66 """
67         header = "This is a header"
68
69         out = CgnaptApproxSetupEnvHelper._update_cgnat_script_file(header, sample.splitlines(), "")
70         self.assertNotIn("This is a header", out)
71
72     def test__get_cgnapt_confgi(self):
73
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])
78
79
80 @mock.patch("yardstick.network_services.vnf_generic.vnf.sample_vnf.Process")
81 class TestCgnaptApproxVnf(unittest.TestCase):
82     VNFD = {'vnfd:vnfd-catalog':
83             {'vnfd':
84              [{'short-name': 'VpeVnf',
85                'vdu':
86                [{'routing_table':
87                  [{'network': '152.16.100.20',
88                    'netmask': '255.255.255.0',
89                    'gateway': '152.16.100.20',
90                    'if': 'xe0'},
91                   {'network': '152.16.40.20',
92                    'netmask': '255.255.255.0',
93                    'gateway': '152.16.40.20',
94                    'if': 'xe1'}],
95                  'description': 'VPE approximation using DPDK',
96                  'name': 'vpevnf-baremetal',
97                  'nd_route_tbl':
98                  [{'network': '0064:ff9b:0:0:0:0:9810:6414',
99                    'netmask': '112',
100                    'gateway': '0064:ff9b:0:0:0:0:9810:6414',
101                    'if': 'xe0'},
102                   {'network': '0064:ff9b:0:0:0:0:9810:2814',
103                    'netmask': '112',
104                    'gateway': '0064:ff9b:0:0:0:0:9810:2814',
105                    'if': 'xe1'}],
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',
116                     'driver': "i40e",
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',
121                    'name': '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',
127                     'driver': "i40e",
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',
135                    'name': 'xe1'}]}],
136                'description': 'Vpe approximation using DPDK',
137                'mgmt-interface':
138                    {'vdu-id': 'vpevnf-baremetal',
139                     'host': '1.2.1.1',
140                     'password': 'r00t',
141                     'user': 'root',
142                     'ip': '1.2.1.1'},
143                'benchmark':
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'}]}}
148
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',
153                                                           'lb_count': 1,
154                                                           'worker_config':
155                                                           '1C/1T',
156                                                           'worker_threads': 1}}
157                                 },
158                     'task_id': 'a70bdf4a-8e67-47a3-9dc1-273c14506eb7',
159                     'task_path': '/tmp',
160                     'tc': 'tc_ipv4_1Mflow_64B_packetsize',
161                     'runner': {'object': 'NetworkServiceTestCase',
162                                'interval': 35,
163                                'output_filename': '/tmp/yardstick.out',
164                                'runner_id': 74476, 'duration': 400,
165                                'type': 'Duration'},
166                     'traffic_profile': 'ipv4_throughput_acl.yaml',
167                     'traffic_options': {'flow': 'ipv4_Packets_acl.yaml',
168                                         'imix': 'imix_voice.yaml'},
169                     'type': 'ISB',
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'}
174
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',
180                               'ip': '1.2.1.1',
181                               'interfaces':
182                               {'xe0': {'local_iface_name': 'ens513f0',
183                                        'vld_id': 'public',
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',
189                                        'driver': 'ixgbe',
190                                        'vpci': '0000:02:00.0',
191                                        'dpdk_port_num': 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',
197                                        'driver': 'ixgbe',
198                                        'vpci': '0000:02:00.1',
199                                        'dpdk_port_num': 1}},
200                               'password': 'r00t',
201                               'VNF model': 'l3fwd_vnf.yaml',
202                               'user': 'root'},
203                              'tg__1':
204                              {'member-vnf-index': '1',
205                               'role': 'TrafficGen',
206                               'name': 'trafficgen_1.yardstick',
207                               'vnfd-id-ref': 'tg__1',
208                               'ip': '1.2.1.1',
209                               'interfaces':
210                               {'xe0': {'local_iface_name': 'ens785f0',
211                                        'vld_id': 'private',
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',
217                                        'driver': 'i40e',
218                                        'vpci': '0000:05:00.0',
219                                        'dpdk_port_num': 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',
224                                        'driver': 'i40e',
225                                        'vpci': '0000:05:00.1',
226                                        'dpdk_port_num': 1}},
227                               'password': 'r00t',
228                               'VNF model': 'tg_rfc2544_tpl.yaml',
229                               'user': 'root'},
230                              'vnf__1':
231                              {'name': 'vnf.yardstick',
232                               'vnfd-id-ref': 'vnf__1',
233                               'ip': '1.2.1.1',
234                               'interfaces':
235                               {'xe0': {'local_iface_name': 'ens786f0',
236                                        'vld_id': 'private',
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',
242                                        'driver': 'i40e',
243                                        'vpci': '0000:05:00.0',
244                                        'dpdk_port_num': 0},
245                                'xe1': {'local_iface_name': 'ens786f1',
246                                        'vld_id': 'public',
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',
252                                        'driver': 'i40e',
253                                        'vpci': '0000:05:00.1',
254                                        'dpdk_port_num': 1}},
255                               'routing_table':
256                               [{'netmask': '255.255.255.0',
257                                 'gateway': '152.16.100.20',
258                                 'network': '152.16.100.20',
259                                 'if': 'xe0'},
260                                {'netmask': '255.255.255.0',
261                                 'gateway': '152.16.40.20',
262                                 'network': '152.16.40.20',
263                                 'if': 'xe1'}],
264                               'member-vnf-index': '2',
265                               'host': '1.2.1.1',
266                               'role': 'vnf',
267                               'user': 'root',
268                               'nd_route_tbl':
269                               [{'netmask': '112',
270                                 'gateway': '0064:ff9b:0:0:0:0:9810:6414',
271                                 'network': '0064:ff9b:0:0:0:0:9810:6414',
272                                 'if': 'xe0'},
273                                {'netmask': '112',
274                                 'gateway': '0064:ff9b:0:0:0:0:9810:2814',
275                                 'network': '0064:ff9b:0:0:0:0:9810:2814',
276                                 'if': 'xe1'}],
277                               'password': 'r00t',
278                               'VNF model': 'cgnapt_vnf.yaml'}}}
279
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)
284
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):
288         mock_ssh(ssh)
289
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())
298
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):
302         mock_ssh(ssh)
303
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)
309         cmd = "quit"
310         self.assertEqual("", cgnapt_approx_vnf.vnf_execute(cmd))
311
312     @mock.patch(SSH_HELPER)
313     def test_get_stats(self, ssh, mock_process):
314         mock_ssh(ssh)
315
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)
321         result = \
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()))
325
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)
329         return file_path
330
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):
336         mock_ssh(ssh)
337
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()
347
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):
351         mock_ssh(ssh)
352
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': "",
359                                                     'rules': ""}}
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,
364                                                         self.context_cfg))
365
366     def test_scale(self, mock_process):
367         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
368         cgnapt_approx_vnf = CgnaptApproxVnf(name, vnfd)
369         flavor = ""
370         self.assertRaises(NotImplementedError, cgnapt_approx_vnf.scale, flavor)
371
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):
375         mock_ssh(ssh)
376
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",
382                                           "01:01.1": "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())
387
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):
392         mock_ssh(ssh)
393
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()
403
404
405 if __name__ == '__main__':
406     unittest.main()