Merge "Enable iommu support in grub"
[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             ],
135         },
136         'connection-point': [
137             {
138                 'type': 'VPORT',
139                 'name': 'xe0',
140             },
141             {
142                 'type': 'VPORT',
143                 'name': 'xe1',
144             },
145         ],
146         'id': 'ProxApproxVnf',
147         'name': 'ProxVnf',
148     }
149
150     VNFD = {
151         'vnfd:vnfd-catalog': {
152             'vnfd': [
153                 VNFD0,
154             ],
155         },
156     }
157
158     SCENARIO_CFG = {
159         'task_path': "",
160         'nodes': {
161             'tg__1': 'trafficgen_1.yardstick',
162             'vnf__1': 'vnf.yardstick'},
163         'runner': {
164             'duration': 600, 'type': 'Duration'},
165         'topology': 'prox-tg-topology-2.yaml',
166         'traffic_profile': '../../traffic_profiles/prox_binsearch.yaml',
167         'type': 'NSPerf',
168         'options': {
169             'tg__1': {'prox_args': {'-e': '',
170                                     '-t': ''},
171                       'prox_config': 'configs/l3-gen-2.cfg',
172                       'prox_path':
173                           '/root/dppd-PROX-v035/build/prox'},
174             'vnf__1': {
175                 'prox_args': {'-t': ''},
176                 'prox_config': 'configs/l3-swap-2.cfg',
177                 'prox_path': '/root/dppd-PROX-v035/build/prox'}}}
178
179     CONTEXT_CFG = {
180         'nodes': {
181             'tg__2': {
182                 'member-vnf-index': '3',
183                 'role': 'TrafficGen',
184                 'name': 'trafficgen_2.yardstick',
185                 'vnfd-id-ref': 'tg__2',
186                 'ip': '1.2.1.1',
187                 'interfaces': {
188                     'xe0': {
189                         'local_iface_name': 'ens513f0',
190                         'vld_id': ProxApproxVnf.DOWNLINK,
191                         'netmask': '255.255.255.0',
192                         'local_ip': '152.16.40.20',
193                         'dst_mac': '00:00:00:00:00:01',
194                         'local_mac': '00:00:00:00:00:03',
195                         'dst_ip': '152.16.40.19',
196                         'driver': 'ixgbe',
197                         'vpci': '0000:02:00.0',
198                         'dpdk_port_num': 0,
199                     },
200                     'xe1': {
201                         'local_iface_name': 'ens513f1',
202                         'netmask': '255.255.255.0',
203                         'network': '202.16.100.0',
204                         'local_ip': '202.16.100.20',
205                         'local_mac': '00:1e:67:d0:60:5d',
206                         'driver': 'ixgbe',
207                         'vpci': '0000:02:00.1',
208                         'dpdk_port_num': 1,
209                     },
210                 },
211                 'password': 'r00t',
212                 'VNF model': 'l3fwd_vnf.yaml',
213                 'user': 'root',
214             },
215             'tg__1': {
216                 'member-vnf-index': '1',
217                 'role': 'TrafficGen',
218                 'name': 'trafficgen_1.yardstick',
219                 'vnfd-id-ref': 'tg__1',
220                 'ip': '1.2.1.1',
221                 'interfaces': {
222                     'xe0': {
223                         'local_iface_name': 'ens785f0',
224                         'vld_id': ProxApproxVnf.UPLINK,
225                         'netmask': '255.255.255.0',
226                         'local_ip': '152.16.100.20',
227                         'dst_mac': '00:00:00:00:00:02',
228                         'local_mac': '00:00:00:00:00:04',
229                         'dst_ip': '152.16.100.19',
230                         'driver': 'i40e',
231                         'vpci': '0000:05:00.0',
232                         'dpdk_port_num': 0,
233                     },
234                     'xe1': {
235                         'local_iface_name': 'ens785f1',
236                         'netmask': '255.255.255.0',
237                         'local_ip': '152.16.100.21',
238                         'local_mac': '00:00:00:00:00:01',
239                         'driver': 'i40e',
240                         'vpci': '0000:05:00.1',
241                         'dpdk_port_num': 1,
242                     },
243                 },
244                 'password': 'r00t',
245                 'VNF model': 'tg_rfc2544_tpl.yaml',
246                 'user': 'root',
247             },
248             'vnf__1': {
249                 'name': 'vnf.yardstick',
250                 'vnfd-id-ref': 'vnf__1',
251                 'ip': '1.2.1.1',
252                 'interfaces': {
253                     'xe0': {
254                         'local_iface_name': 'ens786f0',
255                         'vld_id': ProxApproxVnf.UPLINK,
256                         'netmask': '255.255.255.0',
257                         'local_ip': '152.16.100.19',
258                         'dst_mac': '00:00:00:00:00:04',
259                         'local_mac': '00:00:00:00:00:02',
260                         'dst_ip': '152.16.100.20',
261                         'driver': 'i40e',
262                         'vpci': '0000:05:00.0',
263                         'dpdk_port_num': 0,
264                     },
265                     'xe1': {
266                         'local_iface_name': 'ens786f1',
267                         'vld_id': ProxApproxVnf.DOWNLINK,
268                         'netmask': '255.255.255.0',
269                         'local_ip': '152.16.40.19',
270                         'dst_mac': '00:00:00:00:00:03',
271                         'local_mac': '00:00:00:00:00:01',
272                         'dst_ip': '152.16.40.20',
273                         'driver': 'i40e',
274                         'vpci': '0000:05:00.1',
275                         'dpdk_port_num': 1,
276                     },
277                 },
278                 'routing_table': [
279                     {
280                         'netmask': '255.255.255.0',
281                         'gateway': '152.16.100.20',
282                         'network': '152.16.100.20',
283                         'if': 'xe0',
284                     },
285                     {
286                         'netmask': '255.255.255.0',
287                         'gateway': '152.16.40.20',
288                         'network': '152.16.40.20',
289                         'if': 'xe1',
290                     },
291                 ],
292                 'member-vnf-index': '2',
293                 'host': '1.2.1.1',
294                 'role': 'vnf',
295                 'user': 'root',
296                 'nd_route_tbl': [
297                     {
298                         'netmask': '112',
299                         'gateway': '0064:ff9b:0:0:0:0:9810:6414',
300                         'network': '0064:ff9b:0:0:0:0:9810:6414',
301                         'if': 'xe0',
302                     },
303                     {
304                         'netmask': '112',
305                         'gateway': '0064:ff9b:0:0:0:0:9810:2814',
306                         'network': '0064:ff9b:0:0:0:0:9810:2814',
307                         'if': 'xe1',
308                     },
309                 ],
310                 'password': 'r00t',
311                 'VNF model': 'prox_vnf.yaml',
312             },
313         },
314     }
315
316     @mock.patch(SSH_HELPER)
317     def test___init__(self, ssh, *args):
318         mock_ssh(ssh)
319         prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
320         self.assertIsNone(prox_approx_vnf._vnf_process)
321
322     @mock.patch(SSH_HELPER)
323     def test_collect_kpi_no_client(self, ssh, *args):
324         mock_ssh(ssh)
325
326         prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
327         prox_approx_vnf.resource_helper = None
328         expected = {
329             'packets_in': 0,
330             'packets_dropped': 0,
331             'packets_fwd': 0,
332             'collect_stats': {'core': {}},
333         }
334         result = prox_approx_vnf.collect_kpi()
335         self.assertEqual(result, expected)
336
337     @mock.patch(SSH_HELPER)
338     def test_collect_kpi(self, ssh, *args):
339         mock_ssh(ssh)
340
341         resource_helper = mock.MagicMock()
342         resource_helper.execute.return_value = list(range(12))
343         resource_helper.collect_collectd_kpi.return_value = {'core': {'result': 234}}
344
345         prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
346         prox_approx_vnf.resource_helper = resource_helper
347
348         expected = {
349             'packets_in': 6,
350             'packets_dropped': 1,
351             'packets_fwd': 7,
352             'collect_stats': {'core': {'result': 234}},
353         }
354         result = prox_approx_vnf.collect_kpi()
355         self.assertEqual(result, expected)
356
357     @mock.patch(SSH_HELPER)
358     def test_collect_kpi_error(self, ssh, *args):
359         mock_ssh(ssh)
360
361         resource_helper = mock.MagicMock()
362
363         prox_approx_vnf = ProxApproxVnf(NAME, deepcopy(self.VNFD0))
364         prox_approx_vnf.resource_helper = resource_helper
365         prox_approx_vnf.vnfd_helper['vdu'][0]['external-interface'] = []
366         prox_approx_vnf.vnfd_helper.port_pairs.interfaces = []
367
368         with self.assertRaises(RuntimeError):
369             prox_approx_vnf.collect_kpi()
370
371     def _get_file_abspath(self, filename, *args):
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(SSH_HELPER)
377     def bad_test_instantiate(self, *args):
378         prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
379         prox_approx_vnf.scenario_helper = mock.MagicMock()
380         prox_approx_vnf.setup_helper = mock.MagicMock()
381         # we can't mock super
382         prox_approx_vnf.instantiate(self.SCENARIO_CFG, self.CONTEXT_CFG)
383         prox_approx_vnf.setup_helper.build_config.assert_called_once()
384
385     @mock.patch(SSH_HELPER)
386     def test_wait_for_instantiate_panic(self, ssh, *args):
387         mock_ssh(ssh, exec_result=(1, "", ""))
388         prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
389         prox_approx_vnf._vnf_process = mock.MagicMock(**{"is_alive.return_value": True})
390         prox_approx_vnf._run_prox = mock.Mock(return_value=0)
391         prox_approx_vnf.WAIT_TIME = 0
392         prox_approx_vnf.q_out.put("PANIC")
393         with self.assertRaises(RuntimeError):
394             prox_approx_vnf.wait_for_instantiate()
395
396     @mock.patch('yardstick.network_services.vnf_generic.vnf.prox_helpers.socket')
397     @mock.patch(SSH_HELPER)
398     def test_terminate(self, ssh, *args):
399         mock_ssh(ssh)
400         prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
401         prox_approx_vnf._vnf_process = mock.MagicMock()
402         prox_approx_vnf._vnf_process.terminate = mock.Mock()
403         prox_approx_vnf.ssh_helper = mock.MagicMock()
404         prox_approx_vnf.setup_helper = mock.Mock()
405         prox_approx_vnf.resource_helper = mock.MagicMock()
406
407         self.assertIsNone(prox_approx_vnf.terminate())
408
409     @mock.patch(SSH_HELPER)
410     def test__vnf_up_post(self, ssh, *args):
411         mock_ssh(ssh)
412         prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
413         prox_approx_vnf.resource_helper = resource_helper = mock.Mock()
414
415         prox_approx_vnf._vnf_up_post()
416         self.assertEqual(resource_helper.up_post.call_count, 1)
417
418     @mock.patch(SSH_HELPER)
419     def test_vnf_execute_oserror(self, ssh, *args):
420         mock_ssh(ssh)
421         prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
422         prox_approx_vnf.resource_helper = resource_helper = mock.Mock()
423
424         resource_helper.execute.side_effect = OSError(errno.EPIPE, "")
425         prox_approx_vnf.vnf_execute("", _ignore_errors=True)
426
427         resource_helper.execute.side_effect = OSError(errno.ESHUTDOWN, "")
428         prox_approx_vnf.vnf_execute("", _ignore_errors=True)
429
430         resource_helper.execute.side_effect = OSError(errno.EADDRINUSE, "")
431         with self.assertRaises(OSError):
432             prox_approx_vnf.vnf_execute("", _ignore_errors=True)