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