Merge "Disable spoof check on vfs in sriov setup"
[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 SSH_HELPER = 'yardstick.network_services.vnf_generic.vnf.sample_vnf.VnfSshHelper'
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.udp_replay import UdpReplayApproxVnf
34     from yardstick.network_services.nfvi.resource import ResourceProfile
35     from yardstick.network_services.vnf_generic.vnf.sample_vnf import ScenarioHelper
36
37 from tests.unit.network_services.vnf_generic.vnf.test_base import mock_ssh
38
39 TEST_FILE_YAML = 'nsb_test_case.yaml'
40
41
42 NAME = "vnf__1"
43
44
45 @mock.patch("yardstick.network_services.vnf_generic.vnf.sample_vnf.Process")
46 class TestUdpReplayApproxVnf(unittest.TestCase):
47
48     VNFD_0 = {
49         'short-name': 'UdpReplayVnf',
50         'vdu': [
51             {
52                 'description': 'UDPReplay approximation using DPDK',
53                 'routing_table': [
54                     {
55                         'netmask': '255.255.255.0',
56                         'if': 'xe0',
57                         'network': '152.16.100.20',
58                         'gateway': '152.16.100.20',
59                     },
60                     {
61                         'netmask': '255.255.255.0',
62                         'if': 'xe1',
63                         'network': '152.16.40.20',
64                         'gateway': '152.16.40.20',
65                     }
66                 ],
67                 'external-interface': [
68                     {
69                         'virtual-interface': {
70                             'dst_mac': '00:00:00:00:00:04',
71                             'driver': 'i40e',
72                             'local_iface_name': 'xe0',
73                             'bandwidth': '10 Gbps',
74                             'local_ip': '152.16.100.19',
75                             'local_mac': '00:00:00:00:00:02',
76                             'vpci': '0000:05:00.0',
77                             'dpdk_port_num': 0,
78                             'netmask': '255.255.255.0',
79                             'dst_ip': '152.16.100.20',
80                             'type': 'PCI-PASSTHROUGH',
81                             'vld_id': 'uplink_0',
82                             'ifname': 'xe0',
83                         },
84                         'vnfd-connection-point-ref': 'xe0',
85                         'name': 'xe0',
86                     },
87                     {
88                         'virtual-interface': {
89                             'dst_mac': '00:00:00:00:00:03',
90                             'driver': 'i40e',
91                             'local_iface_name': 'xe1',
92                             'bandwidth': '10 Gbps',
93                             'local_ip': '152.16.40.19',
94                             'local_mac': '00:00:00:00:00:01',
95                             'vpci': '0000:05:00.1',
96                             'dpdk_port_num': 1,
97                             'netmask': '255.255.255.0',
98                             'dst_ip': '152.16.40.20',
99                             'type': 'PCI-PASSTHROUGH',
100                             'vld_id': 'downlink_0',
101                             'ifname': 'xe1',
102                         },
103                         'vnfd-connection-point-ref': 'xe1',
104                         'name': 'xe1',
105                     }
106                 ],
107                 'nd_route_tbl': [
108                     {
109                         'netmask': '112',
110                         'if': 'xe0',
111                         'network': '0064:ff9b:0:0:0:0:9810:6414',
112                         'gateway': '0064:ff9b:0:0:0:0:9810:6414',
113                     },
114                     {
115                         'netmask': '112',
116                         'if': 'xe1',
117                         'network': '0064:ff9b:0:0:0:0:9810:2814',
118                         'gateway': '0064:ff9b:0:0:0:0:9810:2814',
119                     }
120                 ],
121                 'id': 'udpreplayvnf-baremetal',
122                 'name': 'udpreplayvnf-baremetal',
123             }
124         ],
125         'description': 'UDPReplay approximation using DPDK',
126         'name': 'VPEVnfSsh',
127         'mgmt-interface': {
128             'vdu-id': 'udpreplay-baremetal',
129             'host': '1.2.1.1',
130             'password': 'r00t',
131             'user': 'root',
132             'ip': '1.2.1.1',
133         },
134         'benchmark': {
135             'kpi': [
136                 'packets_in',
137                 'packets_fwd',
138                 'packets_dropped',
139             ]
140         },
141         'connection-point': [
142             {
143                 'type': 'VPORT',
144                 'name': 'xe0',
145             },
146             {
147                 'type': 'VPORT',
148                 'name': 'xe1',
149             }
150         ],
151         'id': 'UdpReplayApproxVnf',
152     }
153
154     SCENARIO_CFG = {
155         "options": {
156             "packetsize": 64,
157             "traffic_type": 4,
158             "rfc2544": {
159                 "allowed_drop_rate": "0.8 - 1",
160             },
161             "vnf__1": {
162                 "rules": "acl_1rule.yaml",
163                 "vnf_config": {
164                     "lb_config": "SW",
165                     "lb_count": 1,
166                     "worker_config": "1C/1T",
167                     "worker_threads": 1,
168                 },
169                 "hw_csum": "false",
170             }
171         },
172         "task_id": "a70bdf4a-8e67-47a3-9dc1-273c14506eb7",
173         "tc": "tc_ipv4_1Mflow_64B_packetsize",
174         "runner": {
175             "object": "NetworkServiceTestCase",
176             "interval": 35,
177             "output_filename": "/tmp/yardstick.out",
178             "runner_id": 74476, "duration": 400,
179             "type": "Duration"
180         },
181         "traffic_profile": "ipv4_throughput_acl.yaml",
182         "traffic_options": {
183             "flow": "ipv4_Packets_acl.yaml",
184             "imix": "imix_voice.yaml"
185         },
186         "type": "ISB",
187         "nodes": {
188             "tg__2": "trafficgen_2.yardstick",
189             "tg__1": "trafficgen_1.yardstick",
190             "vnf__1": "vnf.yardstick"
191         },
192         "topology": "udpreplay-tg-topology-baremetal.yaml"
193     }
194
195     CONTEXT_CFG = {
196         "nodes": {
197             "vnf__1": {
198                 "vnfd-id-ref": "vnf__1",
199                 "ip": "1.2.1.1",
200                 "interfaces": {
201                     "xe0": {
202                         "local_iface_name": "ens786f0",
203                         "vld_id": UdpReplayApproxVnf.UPLINK,
204                         "netmask": "255.255.255.0",
205                         "vpci": "0000:05:00.0",
206                         "local_ip": "152.16.100.19",
207                         "driver": "i40e",
208                         "dst_ip": "152.16.100.20",
209                         "local_mac": "00:00:00:00:00:02",
210                         "dst_mac": "00:00:00:00:00:04",
211                         "dpdk_port_num": 0
212                     },
213                     "xe1": {
214                         "local_iface_name": "ens786f1",
215                         "vld_id": UdpReplayApproxVnf.DOWNLINK,
216                         "netmask": "255.255.255.0",
217                         "vpci": "0000:05:00.1",
218                         "local_ip": "152.16.40.19",
219                         "driver": "i40e",
220                         "dst_ip": "152.16.40.20",
221                         "local_mac": "00:00:00:00:00:01",
222                         "dst_mac": "00:00:00:00:00:03",
223                         "dpdk_port_num": 1
224                     }
225                 },
226                 "host": "1.2.1.1",
227                 "user": "root",
228                 "nd_route_tbl": [
229                     {
230                         "netmask": "112",
231                         "if": "xe0",
232                         "gateway": "0064:ff9b:0:0:0:0:9810:6414",
233                         "network": "0064:ff9b:0:0:0:0:9810:6414"
234                     },
235                     {
236                         "netmask": "112",
237                         "if": "xe1",
238                         "gateway": "0064:ff9b:0:0:0:0:9810:2814",
239                         "network": "0064:ff9b:0:0:0:0:9810:2814"
240                     }
241                 ],
242                 "password": "r00t",
243                 "VNF model": "udp_replay.yaml",
244                 "name": "vnf.yardstick",
245                 "member-vnf-index": "2",
246                 "routing_table": [
247                     {
248                         "netmask": "255.255.255.0",
249                         "if": "xe0",
250                         "gateway": "152.16.100.20",
251                         "network": "152.16.100.20"
252                     },
253                     {
254                         "netmask": "255.255.255.0",
255                         "if": "xe1",
256                         "gateway": "152.16.40.20",
257                         "network": "152.16.40.20"
258                     }
259                 ],
260                 "role": "vnf"
261             },
262             "trafficgen_2.yardstick": {
263                 "member-vnf-index": "3",
264                 "role": "TrafficGen",
265                 "name": "trafficgen_2.yardstick",
266                 "vnfd-id-ref": "tg__2",
267                 "ip": "1.2.1.1",
268                 "interfaces": {
269                     "xe0": {
270                         "local_iface_name": "ens513f0",
271                         "vld_id": UdpReplayApproxVnf.DOWNLINK,
272                         "netmask": "255.255.255.0",
273                         "vpci": "0000:02:00.0",
274                         "local_ip": "152.16.40.20",
275                         "driver": "ixgbe",
276                         "dst_ip": "152.16.40.19",
277                         "local_mac": "00:00:00:00:00:03",
278                         "dst_mac": "00:00:00:00:00:01",
279                         "dpdk_port_num": 0
280                     },
281                     "xe1": {
282                         "local_iface_name": "ens513f1",
283                         "netmask": "255.255.255.0",
284                         "network": "202.16.100.0",
285                         "local_ip": "202.16.100.20",
286                         "driver": "ixgbe",
287                         "local_mac": "00:1e:67:d0:60:5d",
288                         "vpci": "0000:02:00.1",
289                         "dpdk_port_num": 1
290                     }
291                 },
292                 "password": "r00t",
293                 "VNF model": "l3fwd_vnf.yaml",
294                 "user": "root"
295             },
296             "trafficgen_1.yardstick": {
297                 "member-vnf-index": "1",
298                 "role": "TrafficGen",
299                 "name": "trafficgen_1.yardstick",
300                 "vnfd-id-ref": "tg__1",
301                 "ip": "1.2.1.1",
302                 "interfaces": {
303                     "xe0": {
304                         "local_iface_name": "ens785f0",
305                         "vld_id": UdpReplayApproxVnf.UPLINK,
306                         "netmask": "255.255.255.0",
307                         "vpci": "0000:05:00.0",
308                         "local_ip": "152.16.100.20",
309                         "driver": "i40e",
310                         "dst_ip": "152.16.100.19",
311                         "local_mac": "00:00:00:00:00:04",
312                         "dst_mac": "00:00:00:00:00:02",
313                         "dpdk_port_num": 0
314                     },
315                     "xe1": {
316                         "local_ip": "152.16.100.21",
317                         "driver": "i40e",
318                         "vpci": "0000:05:00.1",
319                         "dpdk_port_num": 1,
320                         "local_iface_name": "ens785f1",
321                         "netmask": "255.255.255.0",
322                         "local_mac": "00:00:00:00:00:01"
323                     }
324                 },
325                 "password": "r00t",
326                 "VNF model": "tg_rfc2544_tpl.yaml",
327                 "user": "root"
328             }
329         }
330     }
331
332     def test___init__(self, _):
333         udp_replay_approx_vnf = UdpReplayApproxVnf(NAME, self.VNFD_0)
334         self.assertIsNone(udp_replay_approx_vnf._vnf_process)
335
336     @mock.patch("yardstick.network_services.vnf_generic.vnf.sample_vnf.time")
337     @mock.patch(SSH_HELPER)
338     def test_collect_kpi(self, ssh, mock_time, _):
339         mock_ssh(ssh)
340
341         vnfd = self.VNFD_0
342         result = "stats\r\r\n\r\nUDP_Replay stats:\r\n--------------\r\n" \
343                  "Port\t\tRx Packet\t\tTx Packet\t\tRx Pkt Drop\t\tTx Pkt Drop \r\n"\
344                  "0\t\t7374156\t\t7374136\t\t\t0\t\t\t0\r\n" \
345                  "1\t\t7374316\t\t7374315\t\t\t0\t\t\t0\r\n\r\nReplay>\r\r\nReplay>"
346         udp_replay_approx_vnf = UdpReplayApproxVnf(NAME, vnfd)
347         udp_replay_approx_vnf.q_in = mock.MagicMock()
348         udp_replay_approx_vnf.q_out = mock.MagicMock()
349         udp_replay_approx_vnf.q_out.qsize = mock.Mock(return_value=0)
350         udp_replay_approx_vnf.all_ports = ["xe0", "xe1"]
351         udp_replay_approx_vnf.get_stats = mock.Mock(return_value=result)
352         result = {'collect_stats': {}, 'packets_dropped': 0,
353                   'packets_fwd': 14748451, 'packets_in': 14748472}
354         self.assertEqual(result, udp_replay_approx_vnf.collect_kpi())
355
356     @mock.patch(SSH_HELPER)
357     def test_get_stats(self, ssh, _):
358         mock_ssh(ssh)
359
360         udp_replay_approx_vnf = UdpReplayApproxVnf(NAME, self.VNFD_0)
361         udp_replay_approx_vnf.q_in = mock.MagicMock()
362         udp_replay_approx_vnf.q_out = mock.MagicMock()
363         udp_replay_approx_vnf.q_out.qsize = mock.Mock(return_value=0)
364         mock_result = \
365             "CG-NAPT(.*\n)*Received 100, Missed 0, Dropped 0,Translated 100,ingress"
366
367         udp_replay_approx_vnf.vnf_execute = mock.Mock(return_value=mock_result)
368
369         self.assertEqual(mock_result,
370                          udp_replay_approx_vnf.get_stats())
371
372     def _get_file_abspath(self, filename):
373         curr_path = os.path.dirname(os.path.abspath(__file__))
374         file_path = os.path.join(curr_path, filename)
375         return file_path
376
377     @mock.patch("yardstick.network_services.vnf_generic.vnf.sample_vnf.Context")
378     @mock.patch(SSH_HELPER)
379     def test__build_config(self, ssh, mock_context, *_):
380         mock_ssh(ssh)
381
382         udp_replay_approx_vnf = UdpReplayApproxVnf(NAME, self.VNFD_0)
383         udp_replay_approx_vnf.queue_wrapper = mock.MagicMock()
384         udp_replay_approx_vnf.nfvi_context = mock_context
385         udp_replay_approx_vnf.nfvi_context.attrs = {'nfvi_type': 'baremetal'}
386         udp_replay_approx_vnf.setup_helper.bound_pci = []
387         udp_replay_approx_vnf.ssh_helper.provision_tool = mock.MagicMock(return_value="tool_path")
388         udp_replay_approx_vnf.scenario_helper = ScenarioHelper(name='vnf__1')
389         udp_replay_approx_vnf.scenario_helper.scenario_cfg = self.SCENARIO_CFG
390
391         cmd_line = udp_replay_approx_vnf._build_config()
392
393         expected = \
394             "sudo tool_path --log-level=5 -c 0x7 -n 4 -w  --  -p 0x3 --config='(0,0,1),(1,0,2)'"
395         self.assertEqual(cmd_line, expected)
396
397     @mock.patch('yardstick.network_services.vnf_generic.vnf.udp_replay.open')
398     @mock.patch("yardstick.network_services.vnf_generic.vnf.sample_vnf.Context")
399     @mock.patch(SSH_HELPER)
400     def test__build_pipeline_kwargs(self, ssh, mock_context, *_):
401         mock_ssh(ssh)
402         udp_replay_approx_vnf = UdpReplayApproxVnf(NAME, self.VNFD_0)
403         udp_replay_approx_vnf.nfvi_context = mock_context
404         udp_replay_approx_vnf.nfvi_context.attrs = {'nfvi_type': 'baremetal'}
405         udp_replay_approx_vnf.setup_helper.bound_pci = ['0000:00:0.1', '0000:00:0.3']
406         udp_replay_approx_vnf.all_ports = ["xe0", "xe1"]
407         udp_replay_approx_vnf.ssh_helper.provision_tool = mock.MagicMock(return_value="tool_path")
408         udp_replay_approx_vnf.scenario_helper = ScenarioHelper(name='vnf__1')
409         udp_replay_approx_vnf.scenario_helper.scenario_cfg = self.SCENARIO_CFG
410
411         udp_replay_approx_vnf._build_pipeline_kwargs()
412
413         self.assertEqual(udp_replay_approx_vnf.pipeline_kwargs, {
414             'config': '(0,0,1),(1,0,2)',
415             'cpu_mask_hex': '0x7',
416             'hw_csum': '',
417             'port_mask_hex': '0x3',
418             'tool_path': 'tool_path',
419             'whitelist': '0000:00:0.1 -w 0000:00:0.3'
420         })
421
422     @mock.patch(SSH_HELPER)
423     def test_run_udp_replay(self, ssh, _):
424         mock_ssh(ssh)
425
426         udp_replay_approx_vnf = UdpReplayApproxVnf(NAME, self.VNFD_0)
427         udp_replay_approx_vnf._build_config = mock.MagicMock()
428         udp_replay_approx_vnf.queue_wrapper = mock.MagicMock()
429         udp_replay_approx_vnf.scenario_helper = mock.MagicMock()
430
431         udp_replay_approx_vnf._run()
432
433         udp_replay_approx_vnf.ssh_helper.run.assert_called_once()
434
435     @mock.patch("yardstick.network_services.vnf_generic.vnf.sample_vnf.Context")
436     @mock.patch(SSH_HELPER)
437     def test_instantiate(self, ssh, *_):
438         mock_ssh(ssh)
439
440         resource = mock.Mock(autospec=ResourceProfile)
441
442         udp_replay_approx_vnf = UdpReplayApproxVnf(NAME, self.VNFD_0)
443         udp_replay_approx_vnf.q_out.put("Replay>")
444         udp_replay_approx_vnf.WAIT_TIME = 0
445         udp_replay_approx_vnf.setup_helper.setup_vnf_environment = mock.Mock()
446
447         udp_replay_approx_vnf.deploy_helper = mock.MagicMock()
448         udp_replay_approx_vnf.deploy_vnfs = mock.MagicMock()
449         self.assertIsNone(udp_replay_approx_vnf.instantiate(self.SCENARIO_CFG, self.CONTEXT_CFG))
450
451         udp_replay_approx_vnf._vnf_process.is_alive = mock.Mock(return_value=1)
452         udp_replay_approx_vnf._vnf_process.exitcode = 0
453
454         self.assertEquals(udp_replay_approx_vnf.wait_for_instantiate(), 0)
455
456     @mock.patch("yardstick.network_services.vnf_generic.vnf.sample_vnf.Context")
457     @mock.patch('yardstick.ssh.SSH')
458     @mock.patch(SSH_HELPER)
459     def test_instantiate_panic(self, ssh, resource_ssh, *_):
460         udp_replay_approx_vnf = UdpReplayApproxVnf(NAME, self.VNFD_0)
461         udp_replay_approx_vnf.WAIT_TIME = 0
462         udp_replay_approx_vnf.q_out.put("some text PANIC some text")
463         udp_replay_approx_vnf.setup_helper.setup_vnf_environment = mock.Mock()
464
465         udp_replay_approx_vnf.deploy_helper = mock.MagicMock()
466         self.assertIsNone(udp_replay_approx_vnf.instantiate(self.SCENARIO_CFG, self.CONTEXT_CFG))
467         with self.assertRaises(RuntimeError):
468             udp_replay_approx_vnf.wait_for_instantiate()
469
470     def test_scale(self, _):
471         udp_replay_approx_vnf = UdpReplayApproxVnf(NAME, self.VNFD_0)
472         flavor = ""
473
474         self.assertRaises(NotImplementedError, udp_replay_approx_vnf.scale, flavor)
475
476     @mock.patch("yardstick.network_services.vnf_generic.vnf.sample_vnf.time")
477     @mock.patch(SSH_HELPER)
478     def test_terminate(self, ssh, mock_time, _):
479         mock_ssh(ssh)
480
481         udp_replay_approx_vnf = UdpReplayApproxVnf(NAME, self.VNFD_0)
482         udp_replay_approx_vnf._vnf_process = mock.MagicMock()
483         udp_replay_approx_vnf._vnf_process.terminate = mock.Mock()
484         udp_replay_approx_vnf.used_drivers = {"01:01.0": "i40e", "01:01.1": "i40e"}
485         udp_replay_approx_vnf.dpdk_nic_bind = "dpdk_nic_bind.py"
486         self.assertEqual(None, udp_replay_approx_vnf.terminate())
487
488 if __name__ == '__main__':
489     unittest.main()