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