Merge "improve tc055 results"
[yardstick.git] / tests / unit / network_services / vnf_generic / vnf / test_udp_replay.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 unittest
21 import mock
22 import os
23
24 from tests.unit import STL_MOCKS
25
26
27 STLClient = mock.MagicMock()
28 stl_patch = mock.patch.dict("sys.modules", STL_MOCKS)
29 stl_patch.start()
30
31 if stl_patch:
32     from yardstick.network_services.vnf_generic.vnf.udp_replay import UdpReplayApproxVnf
33     from yardstick.network_services.vnf_generic.vnf import udp_replay
34
35 TEST_FILE_YAML = 'nsb_test_case.yaml'
36
37
38 NAME = "tg__1"
39
40
41 @mock.patch("yardstick.network_services.vnf_generic.vnf.sample_vnf.Process")
42 class TestAclApproxVnf(unittest.TestCase):
43     VNFD = {'vnfd:vnfd-catalog':
44             {'vnfd':
45              [{'short-name': 'VpeVnf',
46                'vdu':
47                [{'routing_table':
48                  [{'network': '152.16.100.20',
49                    'netmask': '255.255.255.0',
50                    'gateway': '152.16.100.20',
51                    'if': 'xe0'},
52                   {'network': '152.16.40.20',
53                    'netmask': '255.255.255.0',
54                    'gateway': '152.16.40.20',
55                    'if': 'xe1'}],
56                  'description': 'VPE approximation using DPDK',
57                  'name': 'vpevnf-baremetal',
58                  'nd_route_tbl':
59                  [{'network': '0064:ff9b:0:0:0:0:9810:6414',
60                    'netmask': '112',
61                    'gateway': '0064:ff9b:0:0:0:0:9810:6414',
62                    'if': 'xe0'},
63                   {'network': '0064:ff9b:0:0:0:0:9810:2814',
64                    'netmask': '112',
65                    'gateway': '0064:ff9b:0:0:0:0:9810:2814',
66                    'if': 'xe1'}],
67                  'id': 'vpevnf-baremetal',
68                  'external-interface':
69                  [{'virtual-interface':
70                    {'dst_mac': '00:00:00:00:00:04',
71                     'vpci': '0000:05:00.0',
72                     'local_ip': '152.16.100.19',
73                     'type': 'PCI-PASSTHROUGH',
74                     'netmask': '255.255.255.0',
75                     'dpdk_port_num': '0',
76                     'bandwidth': '10 Gbps',
77                     'driver': "i40e",
78                     'dst_ip': '152.16.100.20',
79                     'local_iface_name': 'xe0',
80                     'local_mac': '00:00:00:00:00:02'},
81                    'vnfd-connection-point-ref': 'xe0',
82                    'name': 'xe0'},
83                   {'virtual-interface':
84                    {'dst_mac': '00:00:00:00:00:03',
85                     'vpci': '0000:05:00.1',
86                     'local_ip': '152.16.40.19',
87                     'type': 'PCI-PASSTHROUGH',
88                     'driver': "i40e",
89                     'netmask': '255.255.255.0',
90                     'dpdk_port_num': '1',
91                     'bandwidth': '10 Gbps',
92                     'dst_ip': '152.16.40.20',
93                     'local_iface_name': 'xe1',
94                     'local_mac': '00:00:00:00:00:01'},
95                    'vnfd-connection-point-ref': 'xe1',
96                    'name': 'xe1'}]}],
97                'description': 'Vpe approximation using DPDK',
98                'mgmt-interface':
99                    {'vdu-id': 'vpevnf-baremetal',
100                     'host': '1.2.1.1',
101                     'password': 'r00t',
102                     'user': 'root',
103                     'ip': '1.2.1.1'},
104                'benchmark':
105                    {'kpi': ['packets_in', 'packets_fwd', 'packets_dropped']},
106                'connection-point': [{'type': 'VPORT', 'name': 'xe0'},
107                                     {'type': 'VPORT', 'name': 'xe1'}],
108                'id': 'UdpReplayApproxVnf', 'name': 'VPEVnfSsh'}]}}
109
110     scenario_cfg = {'options': {'packetsize': 64, 'traffic_type': 4,
111                                 'rfc2544': {'allowed_drop_rate': '0.8 - 1'},
112                                 'vnf__1': {'rules': 'acl_1rule.yaml',
113                                            'vnf_config': {'lb_config': 'SW',
114                                                           'lb_count': 1,
115                                                           'worker_config':
116                                                           '1C/1T',
117                                                           'worker_threads': 1}}
118                                 },
119                     'task_id': 'a70bdf4a-8e67-47a3-9dc1-273c14506eb7',
120                     'tc': 'tc_ipv4_1Mflow_64B_packetsize',
121                     'runner': {'object': 'NetworkServiceTestCase',
122                                'interval': 35,
123                                'output_filename': '/tmp/yardstick.out',
124                                'runner_id': 74476, 'duration': 400,
125                                'type': 'Duration'},
126                     'traffic_profile': 'ipv4_throughput_acl.yaml',
127                     'traffic_options': {'flow': 'ipv4_Packets_acl.yaml',
128                                         'imix': 'imix_voice.yaml'},
129                     'type': 'ISB',
130                     'nodes': {'tg__2': 'trafficgen_2.yardstick',
131                               'tg__1': 'trafficgen_1.yardstick',
132                               'vnf__1': 'vnf.yardstick'},
133                     'topology': 'vpe-tg-topology-baremetal.yaml'}
134
135     context_cfg = {'nodes': {'trafficgen_2.yardstick':
136                              {'member-vnf-index': '3',
137                               'role': 'TrafficGen',
138                               'name': 'trafficgen_2.yardstick',
139                               'vnfd-id-ref': 'tg__2',
140                               'ip': '1.2.1.1',
141                               'interfaces':
142                               {'xe0': {'local_iface_name': 'ens513f0',
143                                        'vld_id': 'public',
144                                        'netmask': '255.255.255.0',
145                                        'local_ip': '152.16.40.20',
146                                        'dst_mac': '00:00:00:00:00:01',
147                                        'local_mac': '00:00:00:00:00:03',
148                                        'dst_ip': '152.16.40.19',
149                                        'driver': 'ixgbe',
150                                        'vpci': '0000:02:00.0',
151                                        'dpdk_port_num': 0},
152                                'xe1': {'local_iface_name': 'ens513f1',
153                                        'netmask': '255.255.255.0',
154                                        'network': '202.16.100.0',
155                                        'local_ip': '202.16.100.20',
156                                        'local_mac': '00:1e:67:d0:60:5d',
157                                        'driver': 'ixgbe',
158                                        'vpci': '0000:02:00.1',
159                                        'dpdk_port_num': 1}},
160                               'password': 'r00t',
161                               'VNF model': 'l3fwd_vnf.yaml',
162                               'user': 'root'},
163                              'trafficgen_1.yardstick':
164                              {'member-vnf-index': '1',
165                               'role': 'TrafficGen',
166                               'name': 'trafficgen_1.yardstick',
167                               'vnfd-id-ref': 'tg__1',
168                               'ip': '1.2.1.1',
169                               'interfaces':
170                               {'xe0': {'local_iface_name': 'ens785f0',
171                                        'vld_id': 'private',
172                                        'netmask': '255.255.255.0',
173                                        'local_ip': '152.16.100.20',
174                                        'dst_mac': '00:00:00:00:00:02',
175                                        'local_mac': '00:00:00:00:00:04',
176                                        'dst_ip': '152.16.100.19',
177                                        'driver': 'i40e',
178                                        'vpci': '0000:05:00.0',
179                                        'dpdk_port_num': 0},
180                                'xe1': {'local_iface_name': 'ens785f1',
181                                        'netmask': '255.255.255.0',
182                                        'local_ip': '152.16.100.21',
183                                        'local_mac': '00:00:00:00:00:01',
184                                        'driver': 'i40e',
185                                        'vpci': '0000:05:00.1',
186                                        'dpdk_port_num': 1}},
187                               'password': 'r00t',
188                               'VNF model': 'tg_rfc2544_tpl.yaml',
189                               'user': 'root'},
190                              'vnf__1':
191                              {'name': 'vnf.yardstick',
192                               'vnfd-id-ref': 'vnf__1',
193                               'ip': '1.2.1.1',
194                               'interfaces':
195                               {'xe0': {'local_iface_name': 'ens786f0',
196                                        'vld_id': 'private',
197                                        'netmask': '255.255.255.0',
198                                        'local_ip': '152.16.100.19',
199                                        'dst_mac': '00:00:00:00:00:04',
200                                        'local_mac': '00:00:00:00:00:02',
201                                        'dst_ip': '152.16.100.20',
202                                        'driver': 'i40e',
203                                        'vpci': '0000:05:00.0',
204                                        'dpdk_port_num': 0},
205                                'xe1': {'local_iface_name': 'ens786f1',
206                                        'vld_id': 'public',
207                                        'netmask': '255.255.255.0',
208                                        'local_ip': '152.16.40.19',
209                                        'dst_mac': '00:00:00:00:00:03',
210                                        'local_mac': '00:00:00:00:00:01',
211                                        'dst_ip': '152.16.40.20',
212                                        'driver': 'i40e',
213                                        'vpci': '0000:05:00.1',
214                                        'dpdk_port_num': 1}},
215                               'routing_table':
216                               [{'netmask': '255.255.255.0',
217                                 'gateway': '152.16.100.20',
218                                 'network': '152.16.100.20',
219                                 'if': 'xe0'},
220                                {'netmask': '255.255.255.0',
221                                 'gateway': '152.16.40.20',
222                                 'network': '152.16.40.20',
223                                 'if': 'xe1'}],
224                               'member-vnf-index': '2',
225                               'host': '1.2.1.1',
226                               'role': 'vnf',
227                               'user': 'root',
228                               'nd_route_tbl':
229                               [{'netmask': '112',
230                                 'gateway': '0064:ff9b:0:0:0:0:9810:6414',
231                                 'network': '0064:ff9b:0:0:0:0:9810:6414',
232                                 'if': 'xe0'},
233                                {'netmask': '112',
234                                 'gateway': '0064:ff9b:0:0:0:0:9810:2814',
235                                 'network': '0064:ff9b:0:0:0:0:9810:2814',
236                                 'if': 'xe1'}],
237                               'password': 'r00t',
238                               'VNF model': 'udp_replay.yaml'}}}
239
240     def test___init__(self, mock_process):
241         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
242         udp_approx_vnf = UdpReplayApproxVnf(NAME, vnfd)
243         self.assertIsNone(udp_approx_vnf._vnf_process)
244
245     @mock.patch("yardstick.network_services.vnf_generic.vnf.sample_vnf.time")
246     def test_collect_kpi(self, mock_time, mock_process):
247         with mock.patch("yardstick.ssh.SSH") as ssh:
248             ssh_mock = mock.Mock(autospec=ssh.SSH)
249             ssh_mock.execute = \
250                 mock.Mock(return_value=(0, "", ""))
251             ssh.from_node.return_value = ssh_mock
252             vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
253             result = "stats\r\r\n\r\nUDP_Replay stats:\r\n--------------\r\n" \
254                 "Port\t\tRx Packet\t\tTx Packet\t\tRx Pkt Drop\t\tTx Pkt Drop \r\n"\
255                 "0\t\t7374156\t\t7374136\t\t\t0\t\t\t0\r\n" \
256                 "1\t\t7374316\t\t7374315\t\t\t0\t\t\t0\r\n\r\nReplay>\r\r\nReplay>"
257             udp_approx_vnf = UdpReplayApproxVnf(NAME, vnfd)
258             udp_approx_vnf.q_in = mock.MagicMock()
259             udp_approx_vnf.q_out = mock.MagicMock()
260             udp_approx_vnf.q_out.qsize = mock.Mock(return_value=0)
261             udp_approx_vnf.all_ports = [0, 1]
262             udp_approx_vnf.interfaces = vnfd["vdu"][0]['external-interface']
263             udp_approx_vnf.get_stats = mock.Mock(return_value=result)
264             result = {'collect_stats': {}, 'packets_dropped': 0,
265                       'packets_fwd': 14748451, 'packets_in': 14748472}
266             self.assertEqual(result, udp_approx_vnf.collect_kpi())
267
268     @mock.patch("yardstick.network_services.vnf_generic.vnf.sample_vnf.time")
269     def test_vnf_execute_command(self, mock_time, mock_process):
270         with mock.patch("yardstick.ssh.SSH") as ssh:
271             ssh_mock = mock.Mock(autospec=ssh.SSH)
272             ssh_mock.execute = \
273                 mock.Mock(return_value=(0, "", ""))
274             ssh.from_node.return_value = ssh_mock
275             vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
276             udp_approx_vnf = UdpReplayApproxVnf(NAME, vnfd)
277             cmd = "quit"
278             self.assertEqual("", udp_approx_vnf.vnf_execute(cmd))
279
280     def test_get_stats(self, mock_process):
281         with mock.patch("yardstick.ssh.SSH") as ssh:
282             ssh_mock = mock.Mock(autospec=ssh.SSH)
283             ssh_mock.execute = \
284                 mock.Mock(return_value=(0, "", ""))
285             ssh.from_node.return_value = ssh_mock
286             vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
287             udp_approx_vnf = UdpReplayApproxVnf(NAME, vnfd)
288             udp_approx_vnf.q_in = mock.MagicMock()
289             udp_approx_vnf.q_out = mock.MagicMock()
290             udp_approx_vnf.q_out.qsize = mock.Mock(return_value=0)
291             mock_result = \
292                 "CG-NAPT(.*\n)*Received 100, Missed 0, Dropped 0,Translated 100,ingress"
293             udp_approx_vnf.vnf_execute = mock.Mock(return_value=mock_result)
294             self.assertEqual(mock_result,
295                              udp_approx_vnf.get_stats())
296
297     def _get_file_abspath(self, filename):
298         curr_path = os.path.dirname(os.path.abspath(__file__))
299         file_path = os.path.join(curr_path, filename)
300         return file_path
301
302     @mock.patch('yardstick.network_services.vnf_generic.vnf.udp_replay.open')
303     def test__build_pipeline_kwargs(self, mock_open, mock_process):
304         with mock.patch("yardstick.ssh.SSH") as ssh:
305             ssh_mock = mock.Mock(autospec=ssh.SSH)
306             ssh.from_node.return_value = ssh_mock
307             vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
308             udp_approx_vnf = UdpReplayApproxVnf(NAME, vnfd)
309             udp_approx_vnf._build_config = mock.MagicMock()
310             udp_approx_vnf.queue_wrapper = mock.MagicMock()
311             udp_approx_vnf.nfvi_type = "baremetal"
312             udp_approx_vnf.bound_pci = []
313             udp_approx_vnf.all_ports = [0, 1]
314             udp_approx_vnf.ssh_helper = mock.MagicMock(
315                 **{"provision_tool.return_value": "tool_path"})
316             udp_approx_vnf.vnf_cfg = {'lb_config': 'SW',
317                                       'lb_count': 1,
318                                       'worker_config': '1C/1T',
319                                       'worker_threads': 1}
320             udp_approx_vnf.options = {'traffic_type': '4',
321                                       'topology': 'nsb_test_case.yaml'}
322
323             udp_approx_vnf._build_pipeline_kwargs()
324             self.assertEqual(udp_approx_vnf.pipeline_kwargs, {
325                 'config': '(0, 0, 1)(1, 0, 2)',
326                 'cpu_mask_hex': '0x6',
327                 'hw_csum': '',
328                 'ports_len_hex': '0x3',
329                 'tool_path': 'tool_path',
330                 'whitelist': ''
331             })
332
333     @mock.patch("yardstick.network_services.vnf_generic.vnf.udp_replay.hex")
334     @mock.patch("yardstick.network_services.vnf_generic.vnf.udp_replay.eval")
335     @mock.patch('yardstick.network_services.vnf_generic.vnf.udp_replay.open')
336     def test_run_udp_replay(self, mock_open, eval, hex, mock_process):
337         with mock.patch("yardstick.ssh.SSH") as ssh:
338             ssh_mock = mock.Mock(autospec=ssh.SSH)
339             ssh_mock.execute = \
340                 mock.Mock(return_value=(0, "", ""))
341             ssh_mock.run = \
342                 mock.Mock(return_value=(0, "", ""))
343             ssh.from_node.return_value = ssh_mock
344             vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
345             udp_approx_vnf = UdpReplayApproxVnf(NAME, vnfd)
346             udp_approx_vnf._build_config = mock.MagicMock()
347             udp_approx_vnf.queue_wrapper = mock.MagicMock()
348             udp_approx_vnf.ssh_helper = mock.MagicMock()
349             udp_approx_vnf.ssh_helper.run = mock.MagicMock()
350             udp_approx_vnf.vnf_cfg = {'lb_config': 'SW',
351                                       'lb_count': 1,
352                                       'worker_config': '1C/1T',
353                                       'worker_threads': 1}
354             udp_approx_vnf.options = {'traffic_type': '4',
355                                       'topology': 'nsb_test_case.yaml'}
356
357             udp_approx_vnf._run()
358             udp_approx_vnf.ssh_helper.run.assert_called_once()
359
360     @mock.patch("yardstick.network_services.vnf_generic.vnf.sample_vnf.Context")
361     def test_instantiate(self, Context, mock_process):
362         with mock.patch("yardstick.ssh.SSH") as ssh:
363             ssh_mock = mock.Mock(autospec=ssh.SSH)
364             ssh_mock.execute = \
365                 mock.Mock(return_value=(0, "", ""))
366             ssh.from_node.return_value = ssh_mock
367             vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
368             udp_approx_vnf = UdpReplayApproxVnf(NAME, vnfd)
369             self.scenario_cfg['vnf_options'] = {'cgnapt': {'cfg': "",
370                                                            'rules': ""}}
371             udp_approx_vnf._run_udp_replay = mock.Mock(return_value=0)
372             udp_approx_vnf._parse_rule_file = mock.Mock(return_value={})
373             udp_approx_vnf.deploy_udp_replay_vnf = mock.Mock(return_value=1)
374             udp_approx_vnf.q_out.put("Replay>")
375             udp_approx_vnf.get_my_ports = mock.Mock(return_value=[0, 1])
376             udp_replay.WAIT_TIME = 3
377             udp_approx_vnf.get_nfvi_type = mock.Mock(return_value="baremetal")
378
379             udp_approx_vnf._vnf_process = mock.MagicMock()
380             udp_approx_vnf._vnf_process.is_alive = \
381                 mock.Mock(return_value=1)
382             self.assertIsNone(udp_approx_vnf.instantiate(self.scenario_cfg,
383                                                          self.context_cfg))
384
385     def test_scale(self, mock_process):
386         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
387         udp_approx_vnf = UdpReplayApproxVnf(NAME, vnfd)
388         flavor = ""
389         self.assertRaises(NotImplementedError, udp_approx_vnf.scale, flavor)
390
391     @mock.patch("yardstick.network_services.vnf_generic.vnf.sample_vnf.time")
392     def test_terminate(self, mock_time, mock_process):
393         with mock.patch("yardstick.ssh.SSH") as ssh:
394             ssh_mock = mock.Mock(autospec=ssh.SSH)
395             ssh_mock.execute = \
396                 mock.Mock(return_value=(0, "", ""))
397             ssh.from_node.return_value = ssh_mock
398             vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
399             udp_approx_vnf = UdpReplayApproxVnf(NAME, vnfd)
400             udp_approx_vnf._vnf_process = mock.MagicMock()
401             udp_approx_vnf._vnf_process.terminate = mock.Mock()
402             udp_approx_vnf.used_drivers = {"01:01.0": "i40e",
403                                            "01:01.1": "i40e"}
404             udp_approx_vnf.execute_command = mock.Mock()
405             udp_approx_vnf.ssh_helper = ssh_mock
406             udp_approx_vnf.dpdk_nic_bind = "dpdk_nic_bind.py"
407             self.assertEqual(None, udp_approx_vnf.terminate())
408
409 if __name__ == '__main__':
410     unittest.main()