Merge "Cleanup CGNAPT unit tests"
[yardstick.git] / yardstick / tests / unit / network_services / vnf_generic / vnf / test_udp_replay.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
23
24 SSH_HELPER = 'yardstick.network_services.vnf_generic.vnf.sample_vnf.VnfSshHelper'
25
26 STLClient = mock.MagicMock()
27 stl_patch = mock.patch.dict("sys.modules", STL_MOCKS)
28 stl_patch.start()
29
30 if stl_patch:
31     from yardstick.network_services.vnf_generic.vnf.udp_replay import UdpReplayApproxVnf
32     from yardstick.network_services.vnf_generic.vnf.sample_vnf import ScenarioHelper
33
34
35 TEST_FILE_YAML = 'nsb_test_case.yaml'
36
37
38 NAME = "vnf__1"
39
40
41 @mock.patch("yardstick.network_services.vnf_generic.vnf.sample_vnf.Process")
42 class TestUdpReplayApproxVnf(unittest.TestCase):
43
44     VNFD_0 = {
45         'short-name': 'UdpReplayVnf',
46         'vdu': [
47             {
48                 'description': 'UDPReplay approximation using DPDK',
49                 'routing_table': [
50                     {
51                         'netmask': '255.255.255.0',
52                         'if': 'xe0',
53                         'network': '152.16.100.20',
54                         'gateway': '152.16.100.20',
55                     },
56                     {
57                         'netmask': '255.255.255.0',
58                         'if': 'xe1',
59                         'network': '152.16.40.20',
60                         'gateway': '152.16.40.20',
61                     }
62                 ],
63                 'external-interface': [
64                     {
65                         'virtual-interface': {
66                             'dst_mac': '00:00:00:00:00:04',
67                             'driver': 'i40e',
68                             'local_iface_name': 'xe0',
69                             'bandwidth': '10 Gbps',
70                             'local_ip': '152.16.100.19',
71                             'local_mac': '00:00:00:00:00:02',
72                             'vpci': '0000:05:00.0',
73                             'dpdk_port_num': 0,
74                             'netmask': '255.255.255.0',
75                             'dst_ip': '152.16.100.20',
76                             'type': 'PCI-PASSTHROUGH',
77                             'vld_id': 'uplink_0',
78                             'ifname': 'xe0',
79                         },
80                         'vnfd-connection-point-ref': 'xe0',
81                         'name': 'xe0',
82                     },
83                     {
84                         'virtual-interface': {
85                             'dst_mac': '00:00:00:00:00:03',
86                             'driver': 'i40e',
87                             'local_iface_name': 'xe1',
88                             'bandwidth': '10 Gbps',
89                             'local_ip': '152.16.40.19',
90                             'local_mac': '00:00:00:00:00:01',
91                             'vpci': '0000:05:00.1',
92                             'dpdk_port_num': 1,
93                             'netmask': '255.255.255.0',
94                             'dst_ip': '152.16.40.20',
95                             'type': 'PCI-PASSTHROUGH',
96                             'vld_id': 'downlink_0',
97                             'ifname': 'xe1',
98                         },
99                         'vnfd-connection-point-ref': 'xe1',
100                         'name': 'xe1',
101                     }
102                 ],
103                 'nd_route_tbl': [
104                     {
105                         'netmask': '112',
106                         'if': 'xe0',
107                         'network': '0064:ff9b:0:0:0:0:9810:6414',
108                         'gateway': '0064:ff9b:0:0:0:0:9810:6414',
109                     },
110                     {
111                         'netmask': '112',
112                         'if': 'xe1',
113                         'network': '0064:ff9b:0:0:0:0:9810:2814',
114                         'gateway': '0064:ff9b:0:0:0:0:9810:2814',
115                     }
116                 ],
117                 'id': 'udpreplayvnf-baremetal',
118                 'name': 'udpreplayvnf-baremetal',
119             }
120         ],
121         'description': 'UDPReplay approximation using DPDK',
122         'name': 'VPEVnfSsh',
123         'mgmt-interface': {
124             'vdu-id': 'udpreplay-baremetal',
125             'host': '1.2.1.1',
126             'password': 'r00t',
127             'user': 'root',
128             'ip': '1.2.1.1',
129         },
130         'benchmark': {
131             'kpi': [
132                 'packets_in',
133                 'packets_fwd',
134                 'packets_dropped',
135             ]
136         },
137         'connection-point': [
138             {
139                 'type': 'VPORT',
140                 'name': 'xe0',
141             },
142             {
143                 'type': 'VPORT',
144                 'name': 'xe1',
145             }
146         ],
147         'id': 'UdpReplayApproxVnf',
148     }
149
150     SCENARIO_CFG = {
151         "options": {
152             "packetsize": 64,
153             "traffic_type": 4,
154             "rfc2544": {
155                 "allowed_drop_rate": "0.8 - 1",
156             },
157             "vnf__1": {
158                 "rules": "acl_1rule.yaml",
159                 "vnf_config": {
160                     "lb_config": "SW",
161                     "lb_count": 1,
162                     "worker_config": "1C/1T",
163                     "worker_threads": 1,
164                 },
165                 "hw_csum": "false",
166             }
167         },
168         "task_id": "a70bdf4a-8e67-47a3-9dc1-273c14506eb7",
169         "tc": "tc_ipv4_1Mflow_64B_packetsize",
170         "runner": {
171             "object": "NetworkServiceTestCase",
172             "interval": 35,
173             "output_filename": "/tmp/yardstick.out",
174             "runner_id": 74476, "duration": 400,
175             "type": "Duration"
176         },
177         "traffic_profile": "ipv4_throughput_acl.yaml",
178         "traffic_options": {
179             "flow": "ipv4_Packets_acl.yaml",
180             "imix": "imix_voice.yaml"
181         },
182         "type": "ISB",
183         "nodes": {
184             "tg__2": "trafficgen_2.yardstick",
185             "tg__1": "trafficgen_1.yardstick",
186             "vnf__1": "vnf.yardstick"
187         },
188         "topology": "udpreplay-tg-topology-baremetal.yaml"
189     }
190
191     CONTEXT_CFG = {
192         "nodes": {
193             "vnf__1": {
194                 "vnfd-id-ref": "vnf__1",
195                 "ip": "1.2.1.1",
196                 "interfaces": {
197                     "xe0": {
198                         "local_iface_name": "ens786f0",
199                         "vld_id": UdpReplayApproxVnf.UPLINK,
200                         "netmask": "255.255.255.0",
201                         "vpci": "0000:05:00.0",
202                         "local_ip": "152.16.100.19",
203                         "driver": "i40e",
204                         "dst_ip": "152.16.100.20",
205                         "local_mac": "00:00:00:00:00:02",
206                         "dst_mac": "00:00:00:00:00:04",
207                         "dpdk_port_num": 0
208                     },
209                     "xe1": {
210                         "local_iface_name": "ens786f1",
211                         "vld_id": UdpReplayApproxVnf.DOWNLINK,
212                         "netmask": "255.255.255.0",
213                         "vpci": "0000:05:00.1",
214                         "local_ip": "152.16.40.19",
215                         "driver": "i40e",
216                         "dst_ip": "152.16.40.20",
217                         "local_mac": "00:00:00:00:00:01",
218                         "dst_mac": "00:00:00:00:00:03",
219                         "dpdk_port_num": 1
220                     }
221                 },
222                 "host": "1.2.1.1",
223                 "user": "root",
224                 "nd_route_tbl": [
225                     {
226                         "netmask": "112",
227                         "if": "xe0",
228                         "gateway": "0064:ff9b:0:0:0:0:9810:6414",
229                         "network": "0064:ff9b:0:0:0:0:9810:6414"
230                     },
231                     {
232                         "netmask": "112",
233                         "if": "xe1",
234                         "gateway": "0064:ff9b:0:0:0:0:9810:2814",
235                         "network": "0064:ff9b:0:0:0:0:9810:2814"
236                     }
237                 ],
238                 "password": "r00t",
239                 "VNF model": "udp_replay.yaml",
240                 "name": "vnf.yardstick",
241                 "member-vnf-index": "2",
242                 "routing_table": [
243                     {
244                         "netmask": "255.255.255.0",
245                         "if": "xe0",
246                         "gateway": "152.16.100.20",
247                         "network": "152.16.100.20"
248                     },
249                     {
250                         "netmask": "255.255.255.0",
251                         "if": "xe1",
252                         "gateway": "152.16.40.20",
253                         "network": "152.16.40.20"
254                     }
255                 ],
256                 "role": "vnf"
257             },
258             "trafficgen_2.yardstick": {
259                 "member-vnf-index": "3",
260                 "role": "TrafficGen",
261                 "name": "trafficgen_2.yardstick",
262                 "vnfd-id-ref": "tg__2",
263                 "ip": "1.2.1.1",
264                 "interfaces": {
265                     "xe0": {
266                         "local_iface_name": "ens513f0",
267                         "vld_id": UdpReplayApproxVnf.DOWNLINK,
268                         "netmask": "255.255.255.0",
269                         "vpci": "0000:02:00.0",
270                         "local_ip": "152.16.40.20",
271                         "driver": "ixgbe",
272                         "dst_ip": "152.16.40.19",
273                         "local_mac": "00:00:00:00:00:03",
274                         "dst_mac": "00:00:00:00:00:01",
275                         "dpdk_port_num": 0
276                     },
277                     "xe1": {
278                         "local_iface_name": "ens513f1",
279                         "netmask": "255.255.255.0",
280                         "network": "202.16.100.0",
281                         "local_ip": "202.16.100.20",
282                         "driver": "ixgbe",
283                         "local_mac": "00:1e:67:d0:60:5d",
284                         "vpci": "0000:02:00.1",
285                         "dpdk_port_num": 1
286                     }
287                 },
288                 "password": "r00t",
289                 "VNF model": "l3fwd_vnf.yaml",
290                 "user": "root"
291             },
292             "trafficgen_1.yardstick": {
293                 "member-vnf-index": "1",
294                 "role": "TrafficGen",
295                 "name": "trafficgen_1.yardstick",
296                 "vnfd-id-ref": "tg__1",
297                 "ip": "1.2.1.1",
298                 "interfaces": {
299                     "xe0": {
300                         "local_iface_name": "ens785f0",
301                         "vld_id": UdpReplayApproxVnf.UPLINK,
302                         "netmask": "255.255.255.0",
303                         "vpci": "0000:05:00.0",
304                         "local_ip": "152.16.100.20",
305                         "driver": "i40e",
306                         "dst_ip": "152.16.100.19",
307                         "local_mac": "00:00:00:00:00:04",
308                         "dst_mac": "00:00:00:00:00:02",
309                         "dpdk_port_num": 0
310                     },
311                     "xe1": {
312                         "local_ip": "152.16.100.21",
313                         "driver": "i40e",
314                         "vpci": "0000:05:00.1",
315                         "dpdk_port_num": 1,
316                         "local_iface_name": "ens785f1",
317                         "netmask": "255.255.255.0",
318                         "local_mac": "00:00:00:00:00:01"
319                     }
320                 },
321                 "password": "r00t",
322                 "VNF model": "tg_rfc2544_tpl.yaml",
323                 "user": "root"
324             }
325         }
326     }
327
328     def test___init__(self, *args):
329         udp_replay_approx_vnf = UdpReplayApproxVnf(NAME, self.VNFD_0)
330         self.assertIsNone(udp_replay_approx_vnf._vnf_process)
331
332     @mock.patch("yardstick.network_services.vnf_generic.vnf.sample_vnf.time")
333     @mock.patch(SSH_HELPER)
334     def test_collect_kpi(self, ssh, *args):
335         mock_ssh(ssh)
336
337         vnfd = self.VNFD_0
338         get_stats_ret_val = \
339             "stats\r\r\n\r\nUDP_Replay stats:\r\n--------------\r\n" \
340             "Port\t\tRx Packet\t\tTx Packet\t\tRx Pkt Drop\t\tTx Pkt Drop \r\n"\
341             "0\t\t7374156\t\t7374136\t\t\t0\t\t\t0\r\n" \
342             "1\t\t7374316\t\t7374315\t\t\t0\t\t\t0\r\n\r\nReplay>\r\r\nReplay>"
343         udp_replay_approx_vnf = UdpReplayApproxVnf(NAME, vnfd)
344         udp_replay_approx_vnf.q_in = mock.MagicMock()
345         udp_replay_approx_vnf.q_out = mock.MagicMock()
346         udp_replay_approx_vnf.q_out.qsize = mock.Mock(return_value=0)
347         udp_replay_approx_vnf.all_ports = ["xe0", "xe1"]
348         udp_replay_approx_vnf.get_stats = mock.Mock(return_value=get_stats_ret_val)
349
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()