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