Merge "fix pep8 problem in env.py"
[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 import errno
19 import os
20 import unittest
21 import mock
22 from copy import deepcopy
23
24 from tests.unit import STL_MOCKS
25
26
27 SSH_HELPER = 'yardstick.network_services.vnf_generic.vnf.sample_vnf.VnfSshHelper'
28
29 STLClient = mock.MagicMock()
30 stl_patch = mock.patch.dict("sys.modules", STL_MOCKS)
31 stl_patch.start()
32
33 if stl_patch:
34     from yardstick.network_services.vnf_generic.vnf.prox_vnf import ProxApproxVnf
35     from tests.unit.network_services.vnf_generic.vnf.test_base import mock_ssh
36
37
38 NAME = "vnf__1"
39
40
41 @mock.patch('yardstick.network_services.vnf_generic.vnf.prox_helpers.time')
42 class TestProxApproxVnf(unittest.TestCase):
43
44     VNFD0 = {
45         'short-name': 'ProxVnf',
46         'vdu': [
47             {
48                 'routing_table': [
49                     {
50                         'network': '152.16.100.20',
51                         'netmask': '255.255.255.0',
52                         'gateway': '152.16.100.20',
53                         'if': 'xe0',
54                     },
55                     {
56                         'network': '152.16.40.20',
57                         'netmask': '255.255.255.0',
58                         'gateway': '152.16.40.20',
59                         'if': 'xe1',
60                     },
61                 ],
62                 'description': 'PROX approximation using DPDK',
63                 'name': 'proxvnf-baremetal',
64                 'nd_route_tbl': [
65                     {
66                         'network': '0064:ff9b:0:0:0:0:9810:6414',
67                         'netmask': '112',
68                         'gateway': '0064:ff9b:0:0:0:0:9810:6414',
69                         'if': 'xe0',
70                     },
71                     {
72                         'network': '0064:ff9b:0:0:0:0:9810:2814',
73                         'netmask': '112',
74                         'gateway': '0064:ff9b:0:0:0:0:9810:2814',
75                         'if': 'xe1',
76                     },
77                 ],
78                 'id': 'proxvnf-baremetal',
79                 'external-interface': [
80                     {
81                         'virtual-interface': {
82                             'dst_mac': '00:00:00:00:00:04',
83                             'vpci': '0000:05:00.0',
84                             'local_ip': '152.16.100.19',
85                             'type': 'PCI-PASSTHROUGH',
86                             'vld_id': 'downlink_0',
87                             'ifname': 'xe1',
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': 'uplink_0',
106                             'ifname': 'xe1',
107                             'driver': "i40e",
108                             'netmask': '255.255.255.0',
109                             'dpdk_port_num': 1,
110                             'bandwidth': '10 Gbps',
111                             'dst_ip': '152.16.40.20',
112                             'local_iface_name': 'xe1',
113                             'local_mac': '00:00:00:00:00:01',
114                         },
115                         'vnfd-connection-point-ref': 'xe1',
116                         'name': 'xe1',
117                     },
118                 ],
119             },
120         ],
121         'description': 'PROX approximation using DPDK',
122         'mgmt-interface': {
123             'vdu-id': 'proxvnf-baremetal',
124             'host': '1.2.1.1',
125             'password': 'r00t',
126             'user': 'root',
127             'ip': '1.2.1.1',
128         },
129         'benchmark': {
130             'kpi': [
131                 'packets_in',
132                 'packets_fwd',
133                 'packets_dropped',
134                 'curr_packets_fwd',
135                 'curr_packets_in'
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, *args):
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, *args):
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, *args):
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': 6,
352             'packets_dropped': 1,
353             'packets_fwd': 7,
354             'collect_stats': {'core': {'result': 234}},
355         }
356         result = prox_approx_vnf.collect_kpi()
357         self.assertEqual(result['packets_in'], expected['packets_in'])
358         self.assertEqual(result['packets_dropped'], expected['packets_dropped'])
359         self.assertEqual(result['packets_fwd'], expected['packets_fwd'])
360         self.assertNotEqual(result['packets_fwd'], 0)
361         self.assertNotEqual(result['packets_fwd'], 0)
362
363     @mock.patch(SSH_HELPER)
364     def test_collect_kpi_error(self, ssh, *args):
365         mock_ssh(ssh)
366
367         resource_helper = mock.MagicMock()
368
369         prox_approx_vnf = ProxApproxVnf(NAME, deepcopy(self.VNFD0))
370         prox_approx_vnf.resource_helper = resource_helper
371         prox_approx_vnf.vnfd_helper['vdu'][0]['external-interface'] = []
372         prox_approx_vnf.vnfd_helper.port_pairs.interfaces = []
373
374         with self.assertRaises(RuntimeError):
375             prox_approx_vnf.collect_kpi()
376
377     def _get_file_abspath(self, filename, *args):
378         curr_path = os.path.dirname(os.path.abspath(__file__))
379         file_path = os.path.join(curr_path, filename)
380         return file_path
381
382     @mock.patch(SSH_HELPER)
383     def bad_test_instantiate(self, *args):
384         prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
385         prox_approx_vnf.scenario_helper = mock.MagicMock()
386         prox_approx_vnf.setup_helper = mock.MagicMock()
387         # we can't mock super
388         prox_approx_vnf.instantiate(self.SCENARIO_CFG, self.CONTEXT_CFG)
389         prox_approx_vnf.setup_helper.build_config.assert_called_once()
390
391     @mock.patch(SSH_HELPER)
392     def test_wait_for_instantiate_panic(self, ssh, *args):
393         mock_ssh(ssh, exec_result=(1, "", ""))
394         prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
395         prox_approx_vnf._vnf_process = mock.MagicMock(**{"is_alive.return_value": True})
396         prox_approx_vnf._run_prox = mock.Mock(return_value=0)
397         prox_approx_vnf.WAIT_TIME = 0
398         prox_approx_vnf.q_out.put("PANIC")
399         with self.assertRaises(RuntimeError):
400             prox_approx_vnf.wait_for_instantiate()
401
402     @mock.patch('yardstick.network_services.vnf_generic.vnf.prox_helpers.socket')
403     @mock.patch(SSH_HELPER)
404     def test_terminate(self, ssh, *args):
405         mock_ssh(ssh)
406         prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
407         prox_approx_vnf._vnf_process = mock.MagicMock()
408         prox_approx_vnf._vnf_process.terminate = mock.Mock()
409         prox_approx_vnf.ssh_helper = mock.MagicMock()
410         prox_approx_vnf.setup_helper = mock.Mock()
411         prox_approx_vnf.resource_helper = mock.MagicMock()
412
413         self.assertIsNone(prox_approx_vnf.terminate())
414
415     @mock.patch(SSH_HELPER)
416     def test__vnf_up_post(self, ssh, *args):
417         mock_ssh(ssh)
418         prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
419         prox_approx_vnf.resource_helper = resource_helper = mock.Mock()
420
421         prox_approx_vnf._vnf_up_post()
422         self.assertEqual(resource_helper.up_post.call_count, 1)
423
424     @mock.patch(SSH_HELPER)
425     def test_vnf_execute_oserror(self, ssh, *args):
426         mock_ssh(ssh)
427         prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
428         prox_approx_vnf.resource_helper = resource_helper = mock.Mock()
429
430         resource_helper.execute.side_effect = OSError(errno.EPIPE, "")
431         prox_approx_vnf.vnf_execute("", _ignore_errors=True)
432
433         resource_helper.execute.side_effect = OSError(errno.ESHUTDOWN, "")
434         prox_approx_vnf.vnf_execute("", _ignore_errors=True)
435
436         resource_helper.execute.side_effect = OSError(errno.EADDRINUSE, "")
437         with self.assertRaises(OSError):
438             prox_approx_vnf.vnf_execute("", _ignore_errors=True)