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