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