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