Merge "KVMFORNFV: Update Grafana config for Packet Forwarding test cases."
[yardstick.git] / tests / unit / network_services / vnf_generic / vnf / test_prox_vnf.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 from __future__ import absolute_import
19
20 import os
21 import unittest
22
23 import mock
24 from copy import deepcopy
25
26 SSH_HELPER = 'yardstick.network_services.vnf_generic.vnf.sample_vnf.VnfSshHelper'
27
28 STL_MOCKS = {
29     'stl': mock.MagicMock(),
30     'stl.trex_stl_lib': mock.MagicMock(),
31     'stl.trex_stl_lib.base64': mock.MagicMock(),
32     'stl.trex_stl_lib.binascii': mock.MagicMock(),
33     'stl.trex_stl_lib.collections': mock.MagicMock(),
34     'stl.trex_stl_lib.copy': mock.MagicMock(),
35     'stl.trex_stl_lib.datetime': mock.MagicMock(),
36     'stl.trex_stl_lib.functools': mock.MagicMock(),
37     'stl.trex_stl_lib.imp': mock.MagicMock(),
38     'stl.trex_stl_lib.inspect': mock.MagicMock(),
39     'stl.trex_stl_lib.json': mock.MagicMock(),
40     'stl.trex_stl_lib.linecache': mock.MagicMock(),
41     'stl.trex_stl_lib.math': mock.MagicMock(),
42     'stl.trex_stl_lib.os': mock.MagicMock(),
43     'stl.trex_stl_lib.platform': mock.MagicMock(),
44     'stl.trex_stl_lib.pprint': mock.MagicMock(),
45     'stl.trex_stl_lib.random': mock.MagicMock(),
46     'stl.trex_stl_lib.re': mock.MagicMock(),
47     'stl.trex_stl_lib.scapy': mock.MagicMock(),
48     'stl.trex_stl_lib.socket': mock.MagicMock(),
49     'stl.trex_stl_lib.string': mock.MagicMock(),
50     'stl.trex_stl_lib.struct': mock.MagicMock(),
51     'stl.trex_stl_lib.sys': mock.MagicMock(),
52     'stl.trex_stl_lib.threading': mock.MagicMock(),
53     'stl.trex_stl_lib.time': mock.MagicMock(),
54     'stl.trex_stl_lib.traceback': mock.MagicMock(),
55     'stl.trex_stl_lib.trex_stl_async_client': mock.MagicMock(),
56     'stl.trex_stl_lib.trex_stl_client': mock.MagicMock(),
57     'stl.trex_stl_lib.trex_stl_exceptions': mock.MagicMock(),
58     'stl.trex_stl_lib.trex_stl_ext': mock.MagicMock(),
59     'stl.trex_stl_lib.trex_stl_jsonrpc_client': mock.MagicMock(),
60     'stl.trex_stl_lib.trex_stl_packet_builder_interface': mock.MagicMock(),
61     'stl.trex_stl_lib.trex_stl_packet_builder_scapy': mock.MagicMock(),
62     'stl.trex_stl_lib.trex_stl_port': mock.MagicMock(),
63     'stl.trex_stl_lib.trex_stl_stats': mock.MagicMock(),
64     'stl.trex_stl_lib.trex_stl_streams': mock.MagicMock(),
65     'stl.trex_stl_lib.trex_stl_types': mock.MagicMock(),
66     'stl.trex_stl_lib.types': mock.MagicMock(),
67     'stl.trex_stl_lib.utils': mock.MagicMock(),
68     'stl.trex_stl_lib.utils.argparse': mock.MagicMock(),
69     'stl.trex_stl_lib.utils.collections': mock.MagicMock(),
70     'stl.trex_stl_lib.utils.common': mock.MagicMock(),
71     'stl.trex_stl_lib.utils.json': mock.MagicMock(),
72     'stl.trex_stl_lib.utils.os': mock.MagicMock(),
73     'stl.trex_stl_lib.utils.parsing_opts': mock.MagicMock(),
74     'stl.trex_stl_lib.utils.pwd': mock.MagicMock(),
75     'stl.trex_stl_lib.utils.random': mock.MagicMock(),
76     'stl.trex_stl_lib.utils.re': mock.MagicMock(),
77     'stl.trex_stl_lib.utils.string': mock.MagicMock(),
78     'stl.trex_stl_lib.utils.sys': mock.MagicMock(),
79     'stl.trex_stl_lib.utils.text_opts': mock.MagicMock(),
80     'stl.trex_stl_lib.utils.text_tables': mock.MagicMock(),
81     'stl.trex_stl_lib.utils.texttable': mock.MagicMock(),
82     'stl.trex_stl_lib.warnings': mock.MagicMock(),
83     'stl.trex_stl_lib.yaml': mock.MagicMock(),
84     'stl.trex_stl_lib.zlib': mock.MagicMock(),
85     'stl.trex_stl_lib.zmq': mock.MagicMock(),
86 }
87
88 STLClient = mock.MagicMock()
89 stl_patch = mock.patch.dict("sys.modules", STL_MOCKS)
90 stl_patch.start()
91
92 if stl_patch:
93     from yardstick.network_services.vnf_generic.vnf.prox_vnf import ProxApproxVnf
94     from tests.unit.network_services.vnf_generic.vnf.test_base import mock_ssh
95
96
97 NAME = "vnf__1"
98
99
100 @mock.patch('yardstick.network_services.vnf_generic.vnf.prox_helpers.time')
101 class TestProxApproxVnf(unittest.TestCase):
102
103     VNFD0 = {
104         'short-name': 'ProxVnf',
105         'vdu': [
106             {
107                 'routing_table': [
108                     {
109                         'network': '152.16.100.20',
110                         'netmask': '255.255.255.0',
111                         'gateway': '152.16.100.20',
112                         'if': 'xe0',
113                     },
114                     {
115                         'network': '152.16.40.20',
116                         'netmask': '255.255.255.0',
117                         'gateway': '152.16.40.20',
118                         'if': 'xe1',
119                     },
120                 ],
121                 'description': 'PROX approximation using DPDK',
122                 'name': 'proxvnf-baremetal',
123                 'nd_route_tbl': [
124                     {
125                         'network': '0064:ff9b:0:0:0:0:9810:6414',
126                         'netmask': '112',
127                         'gateway': '0064:ff9b:0:0:0:0:9810:6414',
128                         'if': 'xe0',
129                     },
130                     {
131                         'network': '0064:ff9b:0:0:0:0:9810:2814',
132                         'netmask': '112',
133                         'gateway': '0064:ff9b:0:0:0:0:9810:2814',
134                         'if': 'xe1',
135                     },
136                 ],
137                 'id': 'proxvnf-baremetal',
138                 'external-interface': [
139                     {
140                         'virtual-interface': {
141                             'dst_mac': '00:00:00:00:00:04',
142                             'vpci': '0000:05:00.0',
143                             'local_ip': '152.16.100.19',
144                             'type': 'PCI-PASSTHROUGH',
145                             'vld_id': '',
146                             'netmask': '255.255.255.0',
147                             'dpdk_port_num': '0',
148                             'bandwidth': '10 Gbps',
149                             'driver': "i40e",
150                             'dst_ip': '152.16.100.20',
151                             'local_iface_name': 'xe0',
152                             'local_mac': '00:00:00:00:00:02',
153                         },
154                         'vnfd-connection-point-ref': 'xe0',
155                         'name': 'xe0',
156                     },
157                     {
158                         'virtual-interface': {
159                             'dst_mac': '00:00:00:00:00:03',
160                             'vpci': '0000:05:00.1',
161                             'local_ip': '152.16.40.19',
162                             'type': 'PCI-PASSTHROUGH',
163                             'vld_id': '',
164                             'driver': "i40e",
165                             'netmask': '255.255.255.0',
166                             'dpdk_port_num': '1',
167                             'bandwidth': '10 Gbps',
168                             'dst_ip': '152.16.40.20',
169                             'local_iface_name': 'xe1',
170                             'local_mac': '00:00:00:00:00:01',
171                         },
172                         'vnfd-connection-point-ref': 'xe1',
173                         'name': 'xe1',
174                     },
175                 ],
176             },
177         ],
178         'description': 'PROX approximation using DPDK',
179         'mgmt-interface': {
180             'vdu-id': 'proxvnf-baremetal',
181             'host': '1.2.1.1',
182             'password': 'r00t',
183             'user': 'root',
184             'ip': '1.2.1.1',
185         },
186         'benchmark': {
187             'kpi': [
188                 'packets_in',
189                 'packets_fwd',
190                 'packets_dropped',
191             ],
192         },
193         'connection-point': [
194             {
195                 'type': 'VPORT',
196                 'name': 'xe0',
197             },
198             {
199                 'type': 'VPORT',
200                 'name': 'xe1',
201             },
202         ],
203         'id': 'ProxApproxVnf',
204         'name': 'ProxVnf',
205     }
206
207     VNFD = {
208         'vnfd:vnfd-catalog': {
209             'vnfd': [
210                 VNFD0,
211             ],
212         },
213     }
214
215     SCENARIO_CFG = {
216         'task_path': "",
217         'nodes': {
218             'tg__1': 'trafficgen_1.yardstick',
219             'vnf__1': 'vnf.yardstick'},
220         'runner': {
221             'duration': 600, 'type': 'Duration'},
222         'topology': 'prox-tg-topology-2.yaml',
223         'traffic_profile': '../../traffic_profiles/prox_binsearch.yaml',
224         'type': 'NSPerf',
225         'options': {
226             'tg__1': {'prox_args': {'-e': '',
227                                     '-t': ''},
228                       'prox_config': 'configs/l3-gen-2.cfg',
229                       'prox_path':
230                           '/root/dppd-PROX-v035/build/prox'},
231             'vnf__1': {
232                 'prox_args': {'-t': ''},
233                 'prox_config': 'configs/l3-swap-2.cfg',
234                 'prox_path': '/root/dppd-PROX-v035/build/prox'}}}
235
236     CONTEXT_CFG = {
237         'nodes': {
238             'tg__2': {
239                 'member-vnf-index': '3',
240                 'role': 'TrafficGen',
241                 'name': 'trafficgen_2.yardstick',
242                 'vnfd-id-ref': 'tg__2',
243                 'ip': '1.2.1.1',
244                 'interfaces': {
245                     'xe0': {
246                         'local_iface_name': 'ens513f0',
247                         'vld_id': 'public',
248                         'netmask': '255.255.255.0',
249                         'local_ip': '152.16.40.20',
250                         'dst_mac': '00:00:00:00:00:01',
251                         'local_mac': '00:00:00:00:00:03',
252                         'dst_ip': '152.16.40.19',
253                         'driver': 'ixgbe',
254                         'vpci': '0000:02:00.0',
255                         'dpdk_port_num': 0,
256                     },
257                     'xe1': {
258                         'local_iface_name': 'ens513f1',
259                         'netmask': '255.255.255.0',
260                         'network': '202.16.100.0',
261                         'local_ip': '202.16.100.20',
262                         'local_mac': '00:1e:67:d0:60:5d',
263                         'driver': 'ixgbe',
264                         'vpci': '0000:02:00.1',
265                         'dpdk_port_num': 1,
266                     },
267                 },
268                 'password': 'r00t',
269                 'VNF model': 'l3fwd_vnf.yaml',
270                 'user': 'root',
271             },
272             'tg__1': {
273                 'member-vnf-index': '1',
274                 'role': 'TrafficGen',
275                 'name': 'trafficgen_1.yardstick',
276                 'vnfd-id-ref': 'tg__1',
277                 'ip': '1.2.1.1',
278                 'interfaces': {
279                     'xe0': {
280                         'local_iface_name': 'ens785f0',
281                         'vld_id': 'private',
282                         'netmask': '255.255.255.0',
283                         'local_ip': '152.16.100.20',
284                         'dst_mac': '00:00:00:00:00:02',
285                         'local_mac': '00:00:00:00:00:04',
286                         'dst_ip': '152.16.100.19',
287                         'driver': 'i40e',
288                         'vpci': '0000:05:00.0',
289                         'dpdk_port_num': 0,
290                     },
291                     'xe1': {
292                         'local_iface_name': 'ens785f1',
293                         'netmask': '255.255.255.0',
294                         'local_ip': '152.16.100.21',
295                         'local_mac': '00:00:00:00:00:01',
296                         'driver': 'i40e',
297                         'vpci': '0000:05:00.1',
298                         'dpdk_port_num': 1,
299                     },
300                 },
301                 'password': 'r00t',
302                 'VNF model': 'tg_rfc2544_tpl.yaml',
303                 'user': 'root',
304             },
305             'vnf__1': {
306                 'name': 'vnf.yardstick',
307                 'vnfd-id-ref': 'vnf__1',
308                 'ip': '1.2.1.1',
309                 'interfaces': {
310                     'xe0': {
311                         'local_iface_name': 'ens786f0',
312                         'vld_id': 'private',
313                         'netmask': '255.255.255.0',
314                         'local_ip': '152.16.100.19',
315                         'dst_mac': '00:00:00:00:00:04',
316                         'local_mac': '00:00:00:00:00:02',
317                         'dst_ip': '152.16.100.20',
318                         'driver': 'i40e',
319                         'vpci': '0000:05:00.0',
320                         'dpdk_port_num': 0,
321                     },
322                     'xe1': {
323                         'local_iface_name': 'ens786f1',
324                         'vld_id': 'public',
325                         'netmask': '255.255.255.0',
326                         'local_ip': '152.16.40.19',
327                         'dst_mac': '00:00:00:00:00:03',
328                         'local_mac': '00:00:00:00:00:01',
329                         'dst_ip': '152.16.40.20',
330                         'driver': 'i40e',
331                         'vpci': '0000:05:00.1',
332                         'dpdk_port_num': 1,
333                     },
334                 },
335                 'routing_table': [
336                     {
337                         'netmask': '255.255.255.0',
338                         'gateway': '152.16.100.20',
339                         'network': '152.16.100.20',
340                         'if': 'xe0',
341                     },
342                     {
343                         'netmask': '255.255.255.0',
344                         'gateway': '152.16.40.20',
345                         'network': '152.16.40.20',
346                         'if': 'xe1',
347                     },
348                 ],
349                 'member-vnf-index': '2',
350                 'host': '1.2.1.1',
351                 'role': 'vnf',
352                 'user': 'root',
353                 'nd_route_tbl': [
354                     {
355                         'netmask': '112',
356                         'gateway': '0064:ff9b:0:0:0:0:9810:6414',
357                         'network': '0064:ff9b:0:0:0:0:9810:6414',
358                         'if': 'xe0',
359                     },
360                     {
361                         'netmask': '112',
362                         'gateway': '0064:ff9b:0:0:0:0:9810:2814',
363                         'network': '0064:ff9b:0:0:0:0:9810:2814',
364                         'if': 'xe1',
365                     },
366                 ],
367                 'password': 'r00t',
368                 'VNF model': 'prox_vnf.yaml',
369             },
370         },
371     }
372
373     @mock.patch(SSH_HELPER)
374     def test___init__(self, ssh, mock_time):
375         mock_ssh(ssh)
376         prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
377         self.assertIsNone(prox_approx_vnf._vnf_process)
378
379     @mock.patch(SSH_HELPER)
380     def test_collect_kpi_no_client(self, ssh, mock_time):
381         mock_ssh(ssh)
382
383         prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
384         prox_approx_vnf.resource_helper = None
385         expected = {
386             'packets_in': 0,
387             'packets_dropped': 0,
388             'packets_fwd': 0,
389             'collect_stats': {'core': {}},
390         }
391         result = prox_approx_vnf.collect_kpi()
392         self.assertEqual(result, expected)
393
394     @mock.patch(SSH_HELPER)
395     def test_collect_kpi(self, ssh, mock_time):
396         mock_ssh(ssh)
397
398         resource_helper = mock.MagicMock()
399         resource_helper.execute.return_value = list(range(12))
400         resource_helper.collect_kpi.return_value = {'core': {'result': 234}}
401
402         prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
403         prox_approx_vnf.resource_helper = resource_helper
404
405         expected = {
406             'packets_in': 7,
407             'packets_dropped': 1,
408             'packets_fwd': 6,
409             'collect_stats': {'core': {'result': 234}},
410         }
411         result = prox_approx_vnf.collect_kpi()
412         self.assertEqual(result, expected)
413
414     @mock.patch(SSH_HELPER)
415     def test_collect_kpi_error(self, ssh, mock_time):
416         mock_ssh(ssh)
417
418         resource_helper = mock.MagicMock()
419
420         prox_approx_vnf = ProxApproxVnf(NAME, deepcopy(self.VNFD0))
421         prox_approx_vnf.resource_helper = resource_helper
422         prox_approx_vnf.vnfd_helper['vdu'][0]['external-interface'] = []
423
424         with self.assertRaises(RuntimeError):
425             prox_approx_vnf.collect_kpi()
426
427     def _get_file_abspath(self, filename, mock_time):
428         curr_path = os.path.dirname(os.path.abspath(__file__))
429         file_path = os.path.join(curr_path, filename)
430         return file_path
431
432     @mock.patch(SSH_HELPER)
433     def test_run_prox(self, ssh, mock_time):
434         mock_ssh(ssh)
435
436         prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
437
438         filewrapper = mock.MagicMock()
439         config_path = self.SCENARIO_CFG['options']["vnf__1"]["prox_config"]
440         prox_path = self.SCENARIO_CFG['options']["vnf__1"]["prox_path"]
441         prox_args = self.SCENARIO_CFG['options']["vnf__1"]["prox_args"]
442         prox_approx_vnf.WAIT_TIME = 0
443         prox_approx_vnf._run_prox(filewrapper, config_path, prox_path, prox_args)
444
445         self.assertEqual(prox_approx_vnf.ssh_helper.run.call_args[0][0],
446                          "sudo bash -c 'cd /root/dppd-PROX-v035/build; "
447                          "/root/dppd-PROX-v035/build/prox -o cli -t  -f configs/l3-swap-2.cfg '")
448
449     @mock.patch('yardstick.network_services.vnf_generic.vnf.sample_vnf.CpuSysCores')
450     @mock.patch('yardstick.network_services.vnf_generic.vnf.prox_helpers.find_relative_file')
451     @mock.patch(SSH_HELPER)
452     def test_instantiate(self, ssh, mock_find, mock_cpu_sys_cores, mock_time):
453         mock_ssh(ssh)
454
455         mock_cpu_sys_cores.get_core_socket.return_value = {'0': '01234'}
456
457         prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
458         prox_approx_vnf.ssh_helper = mock.MagicMock(
459             **{"execute.return_value": (0, "", ""), "bin_path": ""})
460         prox_approx_vnf.setup_helper._setup_resources = mock.MagicMock()
461         prox_approx_vnf.setup_helper._find_used_drivers = mock.MagicMock()
462         prox_approx_vnf.setup_helper.used_drivers = {}
463         prox_approx_vnf.setup_helper.bound_pci = []
464         prox_approx_vnf._run_prox = mock.MagicMock(return_value=0)
465         prox_approx_vnf.resource_helper = mock.MagicMock()
466         prox_approx_vnf.resource_helper.get_process_args.return_value = {
467                     '-e': '',
468                     '-t': '',
469                 }, 'configs/l3-gen-2.cfg', '/root/dppd-PROX-v035/build/prox'
470
471         prox_approx_vnf.copy_to_target = mock.MagicMock()
472         prox_approx_vnf.upload_prox_config = mock.MagicMock()
473         prox_approx_vnf.generate_prox_config_file = mock.MagicMock()
474         prox_approx_vnf.q_out.put("PROX started")
475         prox_approx_vnf.WAIT_TIME = 0
476
477         # if process it still running exitcode will be None
478         expected = 0, None
479         result = prox_approx_vnf.instantiate(self.SCENARIO_CFG, self.CONTEXT_CFG)
480         self.assertIn(result, expected)
481
482     @mock.patch(SSH_HELPER)
483     def test_wait_for_instantiate_panic(self, ssh, mock_time):
484         mock_ssh(ssh, exec_result=(1, "", ""))
485         prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
486         prox_approx_vnf._vnf_process = mock.MagicMock(**{"is_alive.return_value": True})
487         prox_approx_vnf._run_prox = mock.Mock(return_value=0)
488         prox_approx_vnf.WAIT_TIME = 0
489         prox_approx_vnf.q_out.put("PANIC")
490         with self.assertRaises(RuntimeError):
491             prox_approx_vnf.wait_for_instantiate()
492
493     @mock.patch(SSH_HELPER)
494     def test_scale(self, ssh, mock_time):
495         mock_ssh(ssh)
496         prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
497         with self.assertRaises(NotImplementedError):
498             prox_approx_vnf.scale('')
499
500     @mock.patch('yardstick.network_services.vnf_generic.vnf.prox_helpers.socket')
501     @mock.patch(SSH_HELPER)
502     def test_terminate(self, ssh, mock_socket, mock_time):
503         mock_ssh(ssh)
504         prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
505         prox_approx_vnf._vnf_process = mock.MagicMock()
506         prox_approx_vnf._vnf_process.terminate = mock.Mock()
507         prox_approx_vnf.ssh_helper = mock.MagicMock()
508         prox_approx_vnf.setup_helper = mock.Mock()
509         prox_approx_vnf.resource_helper = mock.MagicMock()
510
511         self.assertIsNone(prox_approx_vnf.terminate())
512
513     @mock.patch(SSH_HELPER)
514     def test__vnf_up_post(self, ssh, mock_time):
515         mock_ssh(ssh)
516         prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
517         prox_approx_vnf.resource_helper = resource_helper = mock.Mock()
518
519         prox_approx_vnf._vnf_up_post()
520         self.assertEqual(resource_helper.up_post.call_count, 1)
521
522
523 if __name__ == '__main__':
524     unittest.main()