Merge "add scenario opnfv_os-nosdn-bar-ha_daily.yaml for Barometer"
[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                             'netmask': '255.255.255.0',
90                             'dpdk_port_num': 0,
91                             'bandwidth': '10 Gbps',
92                             'driver': "i40e",
93                             'dst_ip': '152.16.100.20',
94                             'local_iface_name': 'xe0',
95                             'local_mac': '00:00:00:00:00:02',
96                         },
97                         'vnfd-connection-point-ref': 'xe0',
98                         'name': 'xe0',
99                     },
100                     {
101                         'virtual-interface': {
102                             'dst_mac': '00:00:00:00:00:03',
103                             'vpci': '0000:05:00.1',
104                             'local_ip': '152.16.40.19',
105                             'type': 'PCI-PASSTHROUGH',
106                             'vld_id': '',
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, mock_time):
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, mock_time):
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, mock_time):
339         mock_ssh(ssh)
340
341         resource_helper = mock.MagicMock()
342         resource_helper.execute.return_value = list(range(12))
343         resource_helper.collect_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': 7,
350             'packets_dropped': 1,
351             'packets_fwd': 6,
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, mock_time):
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
367         with self.assertRaises(RuntimeError):
368             prox_approx_vnf.collect_kpi()
369
370     def _get_file_abspath(self, filename, mock_time):
371         curr_path = os.path.dirname(os.path.abspath(__file__))
372         file_path = os.path.join(curr_path, filename)
373         return file_path
374
375     @mock.patch(SSH_HELPER)
376     def test_run_prox(self, ssh, mock_time):
377         mock_ssh(ssh)
378
379         prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
380         prox_approx_vnf.scenario_helper.scenario_cfg = self.SCENARIO_CFG
381         prox_approx_vnf.ssh_helper.provision_tool.return_value = '/tool_path12/tool_file34'
382         prox_approx_vnf.setup_helper.remote_path = 'configs/file56.cfg'
383
384         expected = "sudo bash -c 'cd /tool_path12; " \
385                    "/tool_path12/tool_file34 -o cli -t  -f configs/file56.cfg '"
386
387         prox_approx_vnf._run()
388         result = prox_approx_vnf.ssh_helper.run.call_args[0][0]
389         self.assertEqual(result, expected)
390
391     @mock.patch(SSH_HELPER)
392     def bad_test_instantiate(self, ssh, mock_time):
393         prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
394         prox_approx_vnf.scenario_helper = mock.MagicMock()
395         prox_approx_vnf.setup_helper = mock.MagicMock()
396         # we can't mock super
397         prox_approx_vnf.instantiate(self.SCENARIO_CFG, self.CONTEXT_CFG)
398         prox_approx_vnf.setup_helper.build_config.assert_called_once
399
400     @mock.patch(SSH_HELPER)
401     def test_wait_for_instantiate_panic(self, ssh, mock_time):
402         mock_ssh(ssh, exec_result=(1, "", ""))
403         prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
404         prox_approx_vnf._vnf_process = mock.MagicMock(**{"is_alive.return_value": True})
405         prox_approx_vnf._run_prox = mock.Mock(return_value=0)
406         prox_approx_vnf.WAIT_TIME = 0
407         prox_approx_vnf.q_out.put("PANIC")
408         with self.assertRaises(RuntimeError):
409             prox_approx_vnf.wait_for_instantiate()
410
411     @mock.patch(SSH_HELPER)
412     def test_scale(self, ssh, mock_time):
413         mock_ssh(ssh)
414         prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
415         with self.assertRaises(NotImplementedError):
416             prox_approx_vnf.scale()
417
418     @mock.patch('yardstick.network_services.vnf_generic.vnf.prox_helpers.socket')
419     @mock.patch(SSH_HELPER)
420     def test_terminate(self, ssh, mock_socket, mock_time):
421         mock_ssh(ssh)
422         prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
423         prox_approx_vnf._vnf_process = mock.MagicMock()
424         prox_approx_vnf._vnf_process.terminate = mock.Mock()
425         prox_approx_vnf.ssh_helper = mock.MagicMock()
426         prox_approx_vnf.setup_helper = mock.Mock()
427         prox_approx_vnf.resource_helper = mock.MagicMock()
428
429         self.assertIsNone(prox_approx_vnf.terminate())
430
431     @mock.patch(SSH_HELPER)
432     def test__vnf_up_post(self, ssh, mock_time):
433         mock_ssh(ssh)
434         prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
435         prox_approx_vnf.resource_helper = resource_helper = mock.Mock()
436
437         prox_approx_vnf._vnf_up_post()
438         self.assertEqual(resource_helper.up_post.call_count, 1)
439
440     @mock.patch(SSH_HELPER)
441     def test_vnf_execute_oserror(self, ssh, mock_time):
442         mock_ssh(ssh)
443         prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0)
444         prox_approx_vnf.resource_helper = resource_helper = mock.Mock()
445
446         resource_helper.execute.side_effect = OSError(errno.EPIPE, "")
447         prox_approx_vnf.vnf_execute("", _ignore_errors=True)
448
449         resource_helper.execute.side_effect = OSError(errno.ESHUTDOWN, "")
450         prox_approx_vnf.vnf_execute("", _ignore_errors=True)
451
452         resource_helper.execute.side_effect = OSError(errno.EADDRINUSE, "")
453         with self.assertRaises(OSError):
454             prox_approx_vnf.vnf_execute("", _ignore_errors=True)
455
456 if __name__ == '__main__':
457     unittest.main()