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