Create a SampleVNF MQ consumer class
[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 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                                                    'task_id')
322         self.assertIsNone(udp_replay_approx_vnf._vnf_process)
323
324     @mock.patch("yardstick.network_services.vnf_generic.vnf.sample_vnf.time")
325     @mock.patch.object(ctx_base.Context, 'get_physical_node_from_server', return_value='mock_node')
326     @mock.patch(SSH_HELPER)
327     def test_collect_kpi(self, ssh, *args):
328         mock_ssh(ssh)
329
330         vnfd = self.VNFD_0
331         get_stats_ret_val = \
332             "stats\r\r\n\r\nUDP_Replay stats:\r\n--------------\r\n" \
333             "Port\t\tRx Packet\t\tTx Packet\t\tRx Pkt Drop\t\tTx Pkt Drop \r\n"\
334             "0\t\t7374156\t\t7374136\t\t\t0\t\t\t0\r\n" \
335             "1\t\t7374316\t\t7374315\t\t\t0\t\t\t0\r\n\r\nReplay>\r\r\nReplay>"
336         udp_replay_approx_vnf = UdpReplayApproxVnf(NAME, vnfd, 'task_id')
337         udp_replay_approx_vnf.scenario_helper.scenario_cfg = {
338             'nodes': {udp_replay_approx_vnf.name: "mock"}
339         }
340         udp_replay_approx_vnf.q_in = mock.MagicMock()
341         udp_replay_approx_vnf.q_out = mock.MagicMock()
342         udp_replay_approx_vnf.q_out.qsize = mock.Mock(return_value=0)
343         udp_replay_approx_vnf.all_ports = ["xe0", "xe1"]
344         udp_replay_approx_vnf.get_stats = mock.Mock(return_value=get_stats_ret_val)
345         result = {
346             'physical_node': 'mock_node',
347             'collect_stats': {},
348             'packets_dropped': 0,
349             'packets_fwd': 14748451,
350             'packets_in': 14748472
351         }
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                                                    'task_id')
360         udp_replay_approx_vnf.q_in = mock.MagicMock()
361         udp_replay_approx_vnf.q_out = mock.MagicMock()
362         udp_replay_approx_vnf.q_out.qsize = mock.Mock(return_value=0)
363         mock_result = \
364             "CG-NAPT(.*\n)*Received 100, Missed 0, Dropped 0,Translated 100,ingress"
365
366         udp_replay_approx_vnf.vnf_execute = mock.Mock(return_value=mock_result)
367
368         self.assertEqual(mock_result,
369                          udp_replay_approx_vnf.get_stats())
370
371     def _get_file_abspath(self, filename):
372         curr_path = os.path.dirname(os.path.abspath(__file__))
373         file_path = os.path.join(curr_path, filename)
374         return file_path
375
376     @mock.patch.object(ctx_base.Context, 'get_context_from_server')
377     @mock.patch(SSH_HELPER)
378     def test__build_config(self, ssh, mock_get_ctx, *args):
379         mock_ssh(ssh)
380
381         nfvi_context = mock.Mock()
382         nfvi_context.attrs = {'nfvi_type': 'baremetal'}
383         mock_get_ctx.return_value = nfvi_context
384
385         udp_replay_approx_vnf = UdpReplayApproxVnf(NAME, self.VNFD_0,
386                                                    'task_id')
387         udp_replay_approx_vnf.queue_wrapper = mock.MagicMock()
388         udp_replay_approx_vnf.nfvi_context = mock_get_ctx
389         udp_replay_approx_vnf.nfvi_context.attrs = {'nfvi_type': 'baremetal'}
390         udp_replay_approx_vnf.setup_helper.bound_pci = []
391         udp_replay_approx_vnf.ssh_helper.provision_tool = mock.MagicMock(return_value="tool_path")
392         udp_replay_approx_vnf.scenario_helper = ScenarioHelper(name='vnf__1')
393         udp_replay_approx_vnf.scenario_helper.scenario_cfg = self.SCENARIO_CFG
394
395         cmd_line = udp_replay_approx_vnf._build_config()
396
397         expected = \
398             "sudo tool_path --log-level=5 -c 0x7 -n 4 -w  --  -p 0x3 --config='(0,0,1),(1,0,2)'"
399         self.assertEqual(cmd_line, expected)
400
401     @mock.patch('yardstick.network_services.vnf_generic.vnf.udp_replay.open')
402     @mock.patch.object(ctx_base.Context, 'get_context_from_server')
403     @mock.patch(SSH_HELPER)
404     def test__build_pipeline_kwargs(self, ssh, mock_get_ctx, *args):
405         mock_ssh(ssh)
406
407         nfvi_context = mock.Mock()
408         nfvi_context.attrs = {'nfvi_type': "baremetal"}
409         mock_get_ctx.return_value = nfvi_context
410
411         udp_replay_approx_vnf = UdpReplayApproxVnf(NAME, self.VNFD_0,
412                                                    'task_id')
413         udp_replay_approx_vnf.setup_helper.bound_pci = ['0000:00:0.1', '0000:00:0.3']
414         udp_replay_approx_vnf.all_ports = ["xe0", "xe1"]
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             'port_mask_hex': '0x3',
426             'tool_path': 'tool_path',
427             'whitelist': '0000:00:0.1 -w 0000:00:0.3'
428         })
429
430     @mock.patch(SSH_HELPER)
431     def test_run_udp_replay(self, ssh, *args):
432         mock_ssh(ssh)
433
434         udp_replay_approx_vnf = UdpReplayApproxVnf(NAME, self.VNFD_0,
435                                                    'task_id')
436         udp_replay_approx_vnf._build_config = mock.MagicMock()
437         udp_replay_approx_vnf.queue_wrapper = mock.MagicMock()
438         udp_replay_approx_vnf.scenario_helper = mock.MagicMock()
439
440         udp_replay_approx_vnf._run()
441
442         udp_replay_approx_vnf.ssh_helper.run.assert_called_once()
443
444     @mock.patch.object(ctx_base.Context, 'get_context_from_server')
445     @mock.patch(SSH_HELPER)
446     def test_instantiate(self, ssh, *args):
447         mock_ssh(ssh)
448
449         udp_replay_approx_vnf = UdpReplayApproxVnf(NAME, self.VNFD_0,
450                                                    'task_id')
451         udp_replay_approx_vnf.q_out.put("Replay>")
452         udp_replay_approx_vnf.WAIT_TIME = 0
453         udp_replay_approx_vnf.setup_helper.setup_vnf_environment = mock.Mock()
454
455         udp_replay_approx_vnf.deploy_helper = mock.MagicMock()
456         udp_replay_approx_vnf.deploy_vnfs = mock.MagicMock()
457         self.assertIsNone(udp_replay_approx_vnf.instantiate(self.SCENARIO_CFG, self.CONTEXT_CFG))
458
459         udp_replay_approx_vnf._vnf_process.is_alive = mock.Mock(return_value=1)
460         udp_replay_approx_vnf._vnf_process.exitcode = 0
461
462         self.assertEqual(udp_replay_approx_vnf.wait_for_instantiate(), 0)
463
464     @mock.patch.object(ctx_base.Context, 'get_context_from_server')
465     @mock.patch('yardstick.ssh.SSH')
466     @mock.patch(SSH_HELPER)
467     def test_instantiate_panic(self, *args):
468         udp_replay_approx_vnf = UdpReplayApproxVnf(NAME, self.VNFD_0,
469                                                    'task_id')
470         udp_replay_approx_vnf.WAIT_TIME = 0
471         udp_replay_approx_vnf.q_out.put("some text PANIC some text")
472         udp_replay_approx_vnf.setup_helper.setup_vnf_environment = mock.Mock()
473
474         udp_replay_approx_vnf.deploy_helper = mock.MagicMock()
475         self.assertIsNone(udp_replay_approx_vnf.instantiate(self.SCENARIO_CFG, self.CONTEXT_CFG))
476         with self.assertRaises(RuntimeError):
477             udp_replay_approx_vnf.wait_for_instantiate()