Add send socket commands function
[yardstick.git] / yardstick / tests / unit / network_services / vnf_generic / vnf / test_acl_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 import unittest
17 import mock
18 import os
19
20 from yardstick.tests import STL_MOCKS
21 from yardstick.tests.unit.network_services.vnf_generic.vnf.test_base import mock_ssh
22 from yardstick.common import utils
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.acl_vnf import AclApproxVnf
31     from yardstick.network_services.nfvi.resource import ResourceProfile
32     from yardstick.network_services.vnf_generic.vnf.acl_vnf import AclApproxSetupEnvSetupEnvHelper
33
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__1'
40
41
42 @mock.patch("yardstick.network_services.vnf_generic.vnf.sample_vnf.Process")
43 class TestAclApproxVnf(unittest.TestCase):
44     VNFD = {'vnfd:vnfd-catalog':
45             {'vnfd':
46              [{'short-name': 'VpeVnf',
47                'vdu':
48                [{'routing_table':
49                  [{'network': '152.16.100.20',
50                    'netmask': '255.255.255.0',
51                    'gateway': '152.16.100.20',
52                    'if': 'xe0'},
53                   {'network': '152.16.40.20',
54                    'netmask': '255.255.255.0',
55                    'gateway': '152.16.40.20',
56                    'if': 'xe1'}],
57                  'description': 'VPE approximation using DPDK',
58                  'name': 'vpevnf-baremetal',
59                  'nd_route_tbl':
60                  [{'network': '0064:ff9b:0:0:0:0:9810:6414',
61                    'netmask': '112',
62                    'gateway': '0064:ff9b:0:0:0:0:9810:6414',
63                    'if': 'xe0'},
64                   {'network': '0064:ff9b:0:0:0:0:9810:2814',
65                    'netmask': '112',
66                    'gateway': '0064:ff9b:0:0:0:0:9810:2814',
67                    'if': 'xe1'}],
68                  'id': 'vpevnf-baremetal',
69                  'external-interface':
70                  [{'virtual-interface':
71                    {'dst_mac': '00:00:00:00:00:04',
72                     'vpci': '0000:05:00.0',
73                     'local_ip': '152.16.100.19',
74                     'type': 'PCI-PASSTHROUGH',
75                     'netmask': '255.255.255.0',
76                     'dpdk_port_num': 0,
77                     'bandwidth': '10 Gbps',
78                     'driver': "i40e",
79                     'dst_ip': '152.16.100.20',
80                     'local_iface_name': 'xe0',
81                     'local_mac': '00:00:00:00:00:02'},
82                    'vnfd-connection-point-ref': 'xe0',
83                    'name': 'xe0'},
84                   {'virtual-interface':
85                    {'dst_mac': '00:00:00:00:00:03',
86                     'vpci': '0000:05:00.1',
87                     'local_ip': '152.16.40.19',
88                     'type': 'PCI-PASSTHROUGH',
89                     'driver': "i40e",
90                     'netmask': '255.255.255.0',
91                     'dpdk_port_num': 1,
92                     'bandwidth': '10 Gbps',
93                     'dst_ip': '152.16.40.20',
94                     'local_iface_name': 'xe1',
95                     'local_mac': '00:00:00:00:00:01'},
96                    'vnfd-connection-point-ref': 'xe1',
97                    'name': 'xe1'}]}],
98                'description': 'Vpe approximation using DPDK',
99                'mgmt-interface':
100                    {'vdu-id': 'vpevnf-baremetal',
101                     'host': '1.2.1.1',
102                     'password': 'r00t',
103                     'user': 'root',
104                     'ip': '1.2.1.1'},
105                'benchmark':
106                    {'kpi': ['packets_in', 'packets_fwd', 'packets_dropped']},
107                'connection-point': [{'type': 'VPORT', 'name': 'xe0'},
108                                     {'type': 'VPORT', 'name': 'xe1'}],
109                'id': 'AclApproxVnf', 'name': 'VPEVnfSsh'}]}}
110
111     scenario_cfg = {'options': {'packetsize': 64, 'traffic_type': 4,
112                                 'rfc2544': {'allowed_drop_rate': '0.8 - 1'},
113                                 'vnf__1': {'rules': 'acl_1rule.yaml',
114                                            'vnf_config': {'lb_config': 'SW',
115                                                           'lb_count': 1,
116                                                           'worker_config':
117                                                           '1C/1T',
118                                                           'worker_threads': 1}}
119                                 },
120                     'task_id': 'a70bdf4a-8e67-47a3-9dc1-273c14506eb7',
121                     'task_path': '/tmp',
122                     'tc': 'tc_ipv4_1Mflow_64B_packetsize',
123                     'runner': {'object': 'NetworkServiceTestCase',
124                                'interval': 35,
125                                'output_filename': '/tmp/yardstick.out',
126                                'runner_id': 74476, 'duration': 400,
127                                'type': 'Duration'},
128                     'traffic_profile': 'ipv4_throughput_acl.yaml',
129                     'traffic_options': {'flow': 'ipv4_Packets_acl.yaml',
130                                         'imix': 'imix_voice.yaml'},
131                     'type': 'ISB',
132                     'nodes': {'tg__2': 'trafficgen_2.yardstick',
133                               'tg__1': 'trafficgen_1.yardstick',
134                               'vnf__1': 'vnf.yardstick'},
135                     'topology': 'vpe-tg-topology-baremetal.yaml'}
136
137     context_cfg = {'nodes': {'tg__2':
138                              {'member-vnf-index': '3',
139                               'role': 'TrafficGen',
140                               'name': 'trafficgen_2.yardstick',
141                               'vnfd-id-ref': 'tg__2',
142                               'ip': '1.2.1.1',
143                               'interfaces':
144                               {'xe0': {'local_iface_name': 'ens513f0',
145                                        'vld_id': AclApproxVnf.DOWNLINK,
146                                        'netmask': '255.255.255.0',
147                                        'local_ip': '152.16.40.20',
148                                        'dst_mac': '00:00:00:00:00:01',
149                                        'local_mac': '00:00:00:00:00:03',
150                                        'dst_ip': '152.16.40.19',
151                                        'driver': 'ixgbe',
152                                        'vpci': '0000:02:00.0',
153                                        'dpdk_port_num': 0},
154                                'xe1': {'local_iface_name': 'ens513f1',
155                                        'netmask': '255.255.255.0',
156                                        'network': '202.16.100.0',
157                                        'local_ip': '202.16.100.20',
158                                        'local_mac': '00:1e:67:d0:60:5d',
159                                        'driver': 'ixgbe',
160                                        'vpci': '0000:02:00.1',
161                                        'dpdk_port_num': 1}},
162                               'password': 'r00t',
163                               'VNF model': 'l3fwd_vnf.yaml',
164                               'user': 'root'},
165                              'tg__1':
166                              {'member-vnf-index': '1',
167                               'role': 'TrafficGen',
168                               'name': 'trafficgen_1.yardstick',
169                               'vnfd-id-ref': 'tg__1',
170                               'ip': '1.2.1.1',
171                               'interfaces':
172                               {'xe0': {'local_iface_name': 'ens785f0',
173                                        'vld_id': AclApproxVnf.UPLINK,
174                                        'netmask': '255.255.255.0',
175                                        'local_ip': '152.16.100.20',
176                                        'dst_mac': '00:00:00:00:00:02',
177                                        'local_mac': '00:00:00:00:00:04',
178                                        'dst_ip': '152.16.100.19',
179                                        'driver': 'i40e',
180                                        'vpci': '0000:05:00.0',
181                                        'dpdk_port_num': 0},
182                                'xe1': {'local_iface_name': 'ens785f1',
183                                        'netmask': '255.255.255.0',
184                                        'local_ip': '152.16.100.21',
185                                        'local_mac': '00:00:00:00:00:01',
186                                        'driver': 'i40e',
187                                        'vpci': '0000:05:00.1',
188                                        'dpdk_port_num': 1}},
189                               'password': 'r00t',
190                               'VNF model': 'tg_rfc2544_tpl.yaml',
191                               'user': 'root'},
192                              'vnf__1':
193                              {'name': 'vnf.yardstick',
194                               'vnfd-id-ref': 'vnf__1',
195                               'ip': '1.2.1.1',
196                               'interfaces':
197                               {'xe0': {'local_iface_name': 'ens786f0',
198                                        'vld_id': AclApproxVnf.UPLINK,
199                                        'netmask': '255.255.255.0',
200                                        'local_ip': '152.16.100.19',
201                                        'dst_mac': '00:00:00:00:00:04',
202                                        'local_mac': '00:00:00:00:00:02',
203                                        'dst_ip': '152.16.100.20',
204                                        'driver': 'i40e',
205                                        'vpci': '0000:05:00.0',
206                                        'dpdk_port_num': 0},
207                                'xe1': {'local_iface_name': 'ens786f1',
208                                        'vld_id': AclApproxVnf.DOWNLINK,
209                                        'netmask': '255.255.255.0',
210                                        'local_ip': '152.16.40.19',
211                                        'dst_mac': '00:00:00:00:00:03',
212                                        'local_mac': '00:00:00:00:00:01',
213                                        'dst_ip': '152.16.40.20',
214                                        'driver': 'i40e',
215                                        'vpci': '0000:05:00.1',
216                                        'dpdk_port_num': 1}},
217                               'routing_table':
218                               [{'netmask': '255.255.255.0',
219                                 'gateway': '152.16.100.20',
220                                 'network': '152.16.100.20',
221                                 'if': 'xe0'},
222                                {'netmask': '255.255.255.0',
223                                 'gateway': '152.16.40.20',
224                                 'network': '152.16.40.20',
225                                 'if': 'xe1'}],
226                               'member-vnf-index': '2',
227                               'host': '1.2.1.1',
228                               'role': 'vnf',
229                               'user': 'root',
230                               'nd_route_tbl':
231                               [{'netmask': '112',
232                                 'gateway': '0064:ff9b:0:0:0:0:9810:6414',
233                                 'network': '0064:ff9b:0:0:0:0:9810:6414',
234                                 'if': 'xe0'},
235                                {'netmask': '112',
236                                 'gateway': '0064:ff9b:0:0:0:0:9810:2814',
237                                 'network': '0064:ff9b:0:0:0:0:9810:2814',
238                                 'if': 'xe1'}],
239                               'password': 'r00t',
240                               'VNF model': 'acl_vnf.yaml'}}}
241
242     def test___init__(self, *args):
243         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
244         acl_approx_vnf = AclApproxVnf(name, vnfd)
245         self.assertIsNone(acl_approx_vnf._vnf_process)
246
247     @mock.patch("yardstick.network_services.vnf_generic.vnf.sample_vnf.time")
248     @mock.patch(SSH_HELPER)
249     def test_collect_kpi(self, ssh, *args):
250         mock_ssh(ssh)
251
252         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
253         acl_approx_vnf = AclApproxVnf(name, vnfd)
254         acl_approx_vnf.q_in = mock.MagicMock()
255         acl_approx_vnf.q_out = mock.MagicMock()
256         acl_approx_vnf.q_out.qsize = mock.Mock(return_value=0)
257         acl_approx_vnf.resource = mock.Mock(autospec=ResourceProfile)
258         acl_approx_vnf.vnf_execute = mock.Mock(return_value="")
259         result = {'packets_dropped': 0, 'packets_fwd': 0, 'packets_in': 0}
260         self.assertEqual(result, acl_approx_vnf.collect_kpi())
261
262     @mock.patch("yardstick.network_services.vnf_generic.vnf.sample_vnf.time")
263     @mock.patch(SSH_HELPER)
264     def test_vnf_execute_command(self, ssh, *args):
265         mock_ssh(ssh)
266
267         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
268         acl_approx_vnf = AclApproxVnf(name, vnfd)
269         acl_approx_vnf.q_in = mock.MagicMock()
270         acl_approx_vnf.q_out = mock.MagicMock()
271         acl_approx_vnf.q_out.qsize = mock.Mock(return_value=0)
272         cmd = "quit"
273         self.assertEqual("", acl_approx_vnf.vnf_execute(cmd))
274
275     @mock.patch(SSH_HELPER)
276     def test_get_stats(self, ssh, *args):
277         mock_ssh(ssh)
278
279         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
280         acl_approx_vnf = AclApproxVnf(name, vnfd)
281         acl_approx_vnf.q_in = mock.MagicMock()
282         acl_approx_vnf.q_out = mock.MagicMock()
283         acl_approx_vnf.q_out.qsize = mock.Mock(return_value=0)
284         result = "ACL TOTAL: pkts_processed: 100, pkts_drop: 0, spkts_received: 100"
285         acl_approx_vnf.vnf_execute = mock.Mock(return_value=result)
286         self.assertEqual(result, acl_approx_vnf.get_stats())
287
288     def _get_file_abspath(self, filename):
289         curr_path = os.path.dirname(os.path.abspath(__file__))
290         file_path = os.path.join(curr_path, filename)
291         return file_path
292
293     @mock.patch("yardstick.network_services.vnf_generic.vnf.acl_vnf.hex")
294     @mock.patch("yardstick.network_services.vnf_generic.vnf.acl_vnf.eval")
295     @mock.patch('yardstick.network_services.vnf_generic.vnf.acl_vnf.open')
296     @mock.patch(SSH_HELPER)
297     def test_run_acl(self, ssh, *args):
298         mock_ssh(ssh)
299
300         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
301         acl_approx_vnf = AclApproxVnf(name, vnfd)
302         acl_approx_vnf._build_config = mock.MagicMock()
303         acl_approx_vnf.queue_wrapper = mock.MagicMock()
304         acl_approx_vnf.scenario_helper.scenario_cfg = self.scenario_cfg
305         acl_approx_vnf.vnf_cfg = {'lb_config': 'SW',
306                                   'lb_count': 1,
307                                   'worker_config': '1C/1T',
308                                   'worker_threads': 1}
309         acl_approx_vnf.all_options = {'traffic_type': '4',
310                                       'topology': 'nsb_test_case.yaml'}
311         acl_approx_vnf._run()
312         acl_approx_vnf.ssh_helper.run.assert_called_once()
313
314     @mock.patch("yardstick.network_services.vnf_generic.vnf.acl_vnf.YangModel")
315     @mock.patch.object(utils, 'find_relative_file')
316     @mock.patch("yardstick.network_services.vnf_generic.vnf.sample_vnf.Context")
317     @mock.patch(SSH_HELPER)
318     def test_instantiate(self, ssh, *args):
319         mock_ssh(ssh)
320
321         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
322         acl_approx_vnf = AclApproxVnf(name, vnfd)
323         acl_approx_vnf.deploy_helper = mock.MagicMock()
324         acl_approx_vnf.resource_helper = mock.MagicMock()
325         acl_approx_vnf._build_config = mock.MagicMock()
326         self.scenario_cfg['vnf_options'] = {'acl': {'cfg': "",
327                                                     'rules': ""}}
328         acl_approx_vnf.q_out.put("pipeline>")
329         acl_approx_vnf.WAIT_TIME = 0
330         self.scenario_cfg.update({"nodes": {"vnf__1": ""}})
331         self.assertIsNone(acl_approx_vnf.instantiate(self.scenario_cfg,
332                                                      self.context_cfg))
333
334     @mock.patch("yardstick.network_services.vnf_generic.vnf.sample_vnf.time")
335     @mock.patch(SSH_HELPER)
336     def test_terminate(self, ssh, *args):
337         mock_ssh(ssh)
338
339         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
340         acl_approx_vnf = AclApproxVnf(name, vnfd)
341         acl_approx_vnf._vnf_process = mock.MagicMock()
342         acl_approx_vnf._vnf_process.terminate = mock.Mock()
343         acl_approx_vnf.used_drivers = {"01:01.0": "i40e",
344                                        "01:01.1": "i40e"}
345         acl_approx_vnf.vnf_execute = mock.MagicMock()
346         acl_approx_vnf.dpdk_devbind = "dpdk-devbind.py"
347         acl_approx_vnf._resource_collect_stop = mock.Mock()
348         self.assertIsNone(acl_approx_vnf.terminate())
349
350
351 class TestAclApproxSetupEnvSetupEnvHelper(unittest.TestCase):
352
353     @mock.patch('yardstick.network_services.vnf_generic.vnf.sample_vnf.open')
354     @mock.patch.object(utils, 'find_relative_file')
355     @mock.patch('yardstick.network_services.vnf_generic.vnf.sample_vnf.MultiPortConfig')
356     @mock.patch.object(utils, 'open_relative_file')
357     def test_build_config(self, *args):
358         vnfd_helper = mock.Mock()
359         ssh_helper = mock.Mock()
360         scenario_helper = mock.Mock()
361         scenario_helper.vnf_cfg = {'lb_config': 'HW'}
362         scenario_helper.all_options = {}
363
364         acl_approx_setup_helper = AclApproxSetupEnvSetupEnvHelper(vnfd_helper,
365                                                                   ssh_helper,
366                                                                   scenario_helper)
367
368         acl_approx_setup_helper.ssh_helper.provision_tool = mock.Mock(return_value='tool_path')
369         acl_approx_setup_helper.ssh_helper.all_ports = mock.Mock()
370         acl_approx_setup_helper.vnfd_helper.port_nums = mock.Mock(return_value=[0, 1])
371         expected = 'sudo tool_path -p 0x3 -f /tmp/acl_config -s /tmp/acl_script  --hwlb 3'
372         self.assertEqual(acl_approx_setup_helper.build_config(), expected)