Add common openstack opertation scenarios: network
[yardstick.git] / tests / unit / benchmark / scenarios / networking / test_vnf_generic.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 # Unittest for yardstick.benchmark.scenarios.networking.test_vnf_generic
19
20 from __future__ import absolute_import
21
22 import os
23 import errno
24 import unittest
25 import mock
26
27 from yardstick.benchmark.scenarios.networking.vnf_generic import \
28     SshManager, NetworkServiceTestCase, IncorrectConfig, \
29     IncorrectSetup, open_relative_file
30 from yardstick.network_services.collector.subscriber import Collector
31 from yardstick.network_services.vnf_generic.vnf.base import \
32     GenericTrafficGen, GenericVNF
33
34 STL_MOCKS = {
35     'stl': mock.MagicMock(),
36     'stl.trex_stl_lib': mock.MagicMock(),
37     'stl.trex_stl_lib.base64': mock.MagicMock(),
38     'stl.trex_stl_lib.binascii': mock.MagicMock(),
39     'stl.trex_stl_lib.collections': mock.MagicMock(),
40     'stl.trex_stl_lib.copy': mock.MagicMock(),
41     'stl.trex_stl_lib.datetime': mock.MagicMock(),
42     'stl.trex_stl_lib.functools': mock.MagicMock(),
43     'stl.trex_stl_lib.imp': mock.MagicMock(),
44     'stl.trex_stl_lib.inspect': mock.MagicMock(),
45     'stl.trex_stl_lib.json': mock.MagicMock(),
46     'stl.trex_stl_lib.linecache': mock.MagicMock(),
47     'stl.trex_stl_lib.math': mock.MagicMock(),
48     'stl.trex_stl_lib.os': mock.MagicMock(),
49     'stl.trex_stl_lib.platform': mock.MagicMock(),
50     'stl.trex_stl_lib.pprint': mock.MagicMock(),
51     'stl.trex_stl_lib.random': mock.MagicMock(),
52     'stl.trex_stl_lib.re': mock.MagicMock(),
53     'stl.trex_stl_lib.scapy': mock.MagicMock(),
54     'stl.trex_stl_lib.socket': mock.MagicMock(),
55     'stl.trex_stl_lib.string': mock.MagicMock(),
56     'stl.trex_stl_lib.struct': mock.MagicMock(),
57     'stl.trex_stl_lib.sys': mock.MagicMock(),
58     'stl.trex_stl_lib.threading': mock.MagicMock(),
59     'stl.trex_stl_lib.time': mock.MagicMock(),
60     'stl.trex_stl_lib.traceback': mock.MagicMock(),
61     'stl.trex_stl_lib.trex_stl_async_client': mock.MagicMock(),
62     'stl.trex_stl_lib.trex_stl_client': mock.MagicMock(),
63     'stl.trex_stl_lib.trex_stl_exceptions': mock.MagicMock(),
64     'stl.trex_stl_lib.trex_stl_ext': mock.MagicMock(),
65     'stl.trex_stl_lib.trex_stl_jsonrpc_client': mock.MagicMock(),
66     'stl.trex_stl_lib.trex_stl_packet_builder_interface': mock.MagicMock(),
67     'stl.trex_stl_lib.trex_stl_packet_builder_scapy': mock.MagicMock(),
68     'stl.trex_stl_lib.trex_stl_port': mock.MagicMock(),
69     'stl.trex_stl_lib.trex_stl_stats': mock.MagicMock(),
70     'stl.trex_stl_lib.trex_stl_streams': mock.MagicMock(),
71     'stl.trex_stl_lib.trex_stl_types': mock.MagicMock(),
72     'stl.trex_stl_lib.types': mock.MagicMock(),
73     'stl.trex_stl_lib.utils': mock.MagicMock(),
74     'stl.trex_stl_lib.utils.argparse': mock.MagicMock(),
75     'stl.trex_stl_lib.utils.collections': mock.MagicMock(),
76     'stl.trex_stl_lib.utils.common': mock.MagicMock(),
77     'stl.trex_stl_lib.utils.json': mock.MagicMock(),
78     'stl.trex_stl_lib.utils.os': mock.MagicMock(),
79     'stl.trex_stl_lib.utils.parsing_opts': mock.MagicMock(),
80     'stl.trex_stl_lib.utils.pwd': mock.MagicMock(),
81     'stl.trex_stl_lib.utils.random': mock.MagicMock(),
82     'stl.trex_stl_lib.utils.re': mock.MagicMock(),
83     'stl.trex_stl_lib.utils.string': mock.MagicMock(),
84     'stl.trex_stl_lib.utils.sys': mock.MagicMock(),
85     'stl.trex_stl_lib.utils.text_opts': mock.MagicMock(),
86     'stl.trex_stl_lib.utils.text_tables': mock.MagicMock(),
87     'stl.trex_stl_lib.utils.texttable': mock.MagicMock(),
88     'stl.trex_stl_lib.warnings': mock.MagicMock(),
89     'stl.trex_stl_lib.yaml': mock.MagicMock(),
90     'stl.trex_stl_lib.zlib': mock.MagicMock(),
91     'stl.trex_stl_lib.zmq': mock.MagicMock(),
92 }
93
94 COMPLETE_TREX_VNFD = {
95     'vnfd:vnfd-catalog': {
96         'vnfd': [
97             {
98                 'benchmark': {
99                     'kpi': [
100                         'rx_throughput_fps',
101                         'tx_throughput_fps',
102                         'tx_throughput_mbps',
103                         'rx_throughput_mbps',
104                         'tx_throughput_pc_linerate',
105                         'rx_throughput_pc_linerate',
106                         'min_latency',
107                         'max_latency',
108                         'avg_latency',
109                     ],
110                 },
111                 'connection-point': [
112                     {
113                         'name': 'xe0',
114                         'type': 'VPORT',
115                     },
116                     {
117                         'name': 'xe1',
118                         'type': 'VPORT',
119                     },
120                 ],
121                 'description': 'TRex stateless traffic generator for RFC2544',
122                 'id': 'TrexTrafficGen',
123                 'mgmt-interface': {
124                     'ip': '1.1.1.1',
125                     'password': 'berta',
126                     'user': 'berta',
127                     'vdu-id': 'trexgen-baremetal',
128                 },
129                 'name': 'trexgen',
130                 'short-name': 'trexgen',
131                 'class-name': 'TrexTrafficGen',
132                 'vdu': [
133                     {
134                         'description': 'TRex stateless traffic generator for RFC2544',
135                         'external-interface': [
136                             {
137                                 'name': 'xe0',
138                                 'virtual-interface': {
139                                     'bandwidth': '10 Gbps',
140                                     'dst_ip': '1.1.1.1',
141                                     'dst_mac': '00:01:02:03:04:05',
142                                     'local_ip': '1.1.1.2',
143                                     'local_mac': '00:01:02:03:05:05',
144                                     'type': 'PCI-PASSTHROUGH',
145                                     'netmask': "255.255.255.0",
146                                     'driver': 'i40',
147                                     'vpci': '0000:00:10.2',
148                                 },
149                                 'vnfd-connection-point-ref': 'xe0',
150                             },
151                             {
152                                 'name': 'xe1',
153                                 'virtual-interface': {
154                                     'bandwidth': '10 Gbps',
155                                     'dst_ip': '2.1.1.1',
156                                     'dst_mac': '00:01:02:03:04:06',
157                                     'local_ip': '2.1.1.2',
158                                     'local_mac': '00:01:02:03:05:06',
159                                     'type': 'PCI-PASSTHROUGH',
160                                     'netmask': "255.255.255.0",
161                                     'driver': 'i40',
162                                     'vpci': '0000:00:10.1',
163                                 },
164                                 'vnfd-connection-point-ref': 'xe1',
165                             },
166                         ],
167                         'id': 'trexgen-baremetal',
168                         'name': 'trexgen-baremetal',
169                     },
170                 ],
171             },
172         ],
173     },
174 }
175
176 IP_ADDR_SHOW = """
177 28: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP \
178 group default qlen 1000
179     link/ether 90:e2:ba:a7:6a:c8 brd ff:ff:ff:ff:ff:ff
180     inet 1.1.1.1/8 brd 1.255.255.255 scope global eth1
181     inet6 fe80::92e2:baff:fea7:6ac8/64 scope link
182        valid_lft forever preferred_lft forever
183 29: eth5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP \
184 group default qlen 1000
185     link/ether 90:e2:ba:a7:6a:c9 brd ff:ff:ff:ff:ff:ff
186     inet 2.1.1.1/8 brd 2.255.255.255 scope global eth5
187     inet6 fe80::92e2:baff:fea7:6ac9/64 scope link tentative
188        valid_lft forever preferred_lft forever
189 """
190
191 SYS_CLASS_NET = """
192 lrwxrwxrwx 1 root root 0 sie 10 14:16 eth1 -> \
193 ../../devices/pci0000:80/0000:80:02.2/0000:84:00.1/net/eth1
194 lrwxrwxrwx 1 root root 0 sie  3 10:37 eth2 -> \
195 ../../devices/pci0000:00/0000:00:01.1/0000:84:00.2/net/eth5
196 """
197
198 TRAFFIC_PROFILE = {
199     "schema": "isb:traffic_profile:0.1",
200     "name": "fixed",
201     "description": "Fixed traffic profile to run UDP traffic",
202     "traffic_profile": {
203         "traffic_type": "FixedTraffic",
204         "frame_rate": 100,  # pps
205         "flow_number": 10,
206         "frame_size": 64,
207     },
208 }
209
210
211 class TestNetworkServiceTestCase(unittest.TestCase):
212
213     def setUp(self):
214         self.tg__1 = {
215             'name': 'trafficgen_1.yardstick',
216             'ip': '10.10.10.11',
217             'role': 'TrafficGen',
218             'user': 'root',
219             'password': 'r00t',
220             'interfaces': {
221                 'xe0': {
222                     'netmask': '255.255.255.0',
223                     'local_ip': '152.16.100.20',
224                     'local_mac': '00:00:00:00:00:01',
225                     'driver': 'i40e',
226                     'vpci': '0000:07:00.0',
227                     'dpdk_port_num': 0,
228                 },
229                 'xe1': {
230                     'netmask': '255.255.255.0',
231                     'local_ip': '152.16.40.20',
232                     'local_mac': '00:00:00:00:00:02',
233                     'driver': 'i40e',
234                     'vpci': '0000:07:00.1',
235                     'dpdk_port_num': 1,
236                 },
237             },
238         }
239
240         self.vnf__1 = {
241             'name': 'vnf.yardstick',
242             'ip': '10.10.10.12',
243             'host': '10.223.197.164',
244             'role': 'vnf',
245             'user': 'root',
246             'password': 'r00t',
247             'interfaces': {
248                 'xe0': {
249                     'netmask': '255.255.255.0',
250                     'local_ip': '152.16.100.19',
251                     'local_mac': '00:00:00:00:00:03',
252                     'driver': 'i40e',
253                     'vpci': '0000:07:00.0',
254                     'dpdk_port_num': 0,
255                 },
256                 'xe1': {
257                     'netmask': '255.255.255.0',
258                     'local_ip': '152.16.40.19',
259                     'local_mac': '00:00:00:00:00:04',
260                     'driver': 'i40e',
261                     'vpci': '0000:07:00.1',
262                     'dpdk_port_num': 1,
263                 },
264             },
265             'routing_table': [
266                 {
267                     'netmask': '255.255.255.0',
268                     'gateway': '152.16.100.20',
269                     'network': '152.16.100.20',
270                     'if': 'xe0',
271                 },
272                 {
273                     'netmask': '255.255.255.0',
274                     'gateway': '152.16.40.20',
275                     'network': '152.16.40.20',
276                     'if': 'xe1',
277                 },
278             ],
279             'nd_route_tbl': [
280                 {
281                     'netmask': '112',
282                     'gateway': '0064:ff9b:0:0:0:0:9810:6414',
283                     'network': '0064:ff9b:0:0:0:0:9810:6414',
284                     'if': 'xe0',
285                 },
286                 {
287                     'netmask': '112',
288                     'gateway': '0064:ff9b:0:0:0:0:9810:2814',
289                     'network': '0064:ff9b:0:0:0:0:9810:2814',
290                     'if': 'xe1',
291                 },
292             ],
293         }
294
295         self.context_cfg = {
296             'nodes': {
297                 'tg__1': self.tg__1,
298                 'vnf__1': self.vnf__1,
299             },
300             'networks': {
301                 'private': {
302                     'vld_id': 'private',
303                 },
304                 'public': {
305                     'vld_id': 'public',
306                 },
307             },
308         }
309
310         self.vld0 = {
311             'vnfd-connection-point-ref': [
312                 {
313                     'vnfd-connection-point-ref': 'xe0',
314                     'member-vnf-index-ref': '1',
315                     'vnfd-id-ref': 'trexgen'
316                 },
317                 {
318                     'vnfd-connection-point-ref': 'xe0',
319                     'member-vnf-index-ref': '2',
320                     'vnfd-id-ref': 'trexgen'
321                 }
322             ],
323             'type': 'ELAN',
324             'id': 'private',
325             'name': 'tg__1 to vnf__1 link 1'
326         }
327
328         self.vld1 = {
329             'vnfd-connection-point-ref': [
330                 {
331                     'vnfd-connection-point-ref': 'xe1',
332                     'member-vnf-index-ref': '1',
333                     'vnfd-id-ref': 'trexgen'
334                 },
335                 {
336                     'vnfd-connection-point-ref': 'xe1',
337                     'member-vnf-index-ref': '2',
338                     'vnfd-id-ref': 'trexgen'
339                 }
340             ],
341             'type': 'ELAN',
342             'id': 'public',
343             'name': 'vnf__1 to tg__1 link 2'
344         }
345
346         self.topology = {
347             'id': 'trex-tg-topology',
348             'short-name': 'trex-tg-topology',
349             'name': 'trex-tg-topology',
350             'description': 'trex-tg-topology',
351             'constituent-vnfd': [
352                 {
353                     'member-vnf-index': '1',
354                     'VNF model': 'tg_trex_tpl.yaml',
355                     'vnfd-id-ref': 'tg__1',
356                 },
357                 {
358                     'member-vnf-index': '2',
359                     'VNF model': 'tg_trex_tpl.yaml',
360                     'vnfd-id-ref': 'vnf__1',
361                 },
362             ],
363             'vld': [self.vld0, self.vld1],
364         }
365
366         self.scenario_cfg = {
367             'task_path': "",
368             "topology": self._get_file_abspath("vpe_vnf_topology.yaml"),
369             'task_id': 'a70bdf4a-8e67-47a3-9dc1-273c14506eb7',
370             'tc': 'tc_ipv4_1Mflow_64B_packetsize',
371             'traffic_profile': 'ipv4_throughput_vpe.yaml',
372             'type': 'ISB',
373             'tc_options': {
374                 'rfc2544': {
375                     'allowed_drop_rate': '0.8 - 1',
376                 },
377             },
378             'runner': {
379                 'object': 'NetworkServiceTestCase',
380                 'interval': 35,
381                 'output_filename': 'yardstick.out',
382                 'runner_id': 74476,
383                 'duration': 400,
384                 'type': 'Duration',
385             },
386             'traffic_options': {
387                 'flow': 'ipv4_1flow_Packets_vpe.yaml',
388                 'imix': 'imix_voice.yaml'
389             },
390             'nodes': {
391                 'tg__2': 'trafficgen_2.yardstick',
392                 'tg__1': 'trafficgen_1.yardstick',
393                 'vnf__1': 'vnf.yardstick',
394             },
395         }
396
397         self.s = NetworkServiceTestCase(self.scenario_cfg, self.context_cfg)
398
399     def _get_file_abspath(self, filename):
400         curr_path = os.path.dirname(os.path.abspath(__file__))
401         file_path = os.path.join(curr_path, filename)
402         return file_path
403
404     def test_ssh_manager(self):
405         with mock.patch("yardstick.ssh.SSH") as ssh:
406             ssh_mock = mock.Mock(autospec=ssh.SSH)
407             ssh_mock.execute = \
408                 mock.Mock(return_value=(0, SYS_CLASS_NET + IP_ADDR_SHOW, ""))
409             ssh.from_node.return_value = ssh_mock
410             for node, node_dict in self.context_cfg["nodes"].items():
411                 with SshManager(node_dict) as conn:
412                     self.assertIsNotNone(conn)
413
414     def test___init__(self):
415         assert self.topology
416
417     def test___get_traffic_flow(self):
418         self.scenario_cfg["traffic_options"]["flow"] = \
419             self._get_file_abspath("ipv4_1flow_Packets_vpe.yaml")
420         result = {'flow': {'dstip4_range': '152.40.0.20',
421                            'srcip4_range': '152.16.0.20', 'count': 1}}
422         self.assertEqual(result, self.s._get_traffic_flow())
423
424     def test___get_traffic_flow_error(self):
425         self.scenario_cfg["traffic_options"]["flow"] = \
426             "ipv4_1flow_Packets_vpe.yaml1"
427         self.assertEqual({}, self.s._get_traffic_flow())
428
429     def test_get_vnf_imp(self):
430         vnfd = COMPLETE_TREX_VNFD['vnfd:vnfd-catalog']['vnfd'][0]['class-name']
431         with mock.patch.dict("sys.modules", STL_MOCKS):
432             self.assertIsNotNone(self.s.get_vnf_impl(vnfd))
433
434             with self.assertRaises(IncorrectConfig) as raised:
435                 self.s.get_vnf_impl('NonExistentClass')
436
437             exc_str = str(raised.exception)
438             print(exc_str)
439             self.assertIn('No implementation', exc_str)
440             self.assertIn('found in', exc_str)
441
442     def test_load_vnf_models_invalid(self):
443         self.context_cfg["nodes"]['tg__1']['VNF model'] = \
444             self._get_file_abspath("tg_trex_tpl.yaml")
445         self.context_cfg["nodes"]['vnf__1']['VNF model'] = \
446             self._get_file_abspath("tg_trex_tpl.yaml")
447
448         vnf = mock.Mock(autospec=GenericVNF)
449         self.s.get_vnf_impl = mock.Mock(return_value=vnf)
450
451         self.assertIsNotNone(
452             self.s.load_vnf_models(self.scenario_cfg, self.context_cfg))
453
454     def test_map_topology_to_infrastructure(self):
455         with mock.patch("yardstick.ssh.SSH") as ssh:
456             ssh_mock = mock.Mock(autospec=ssh.SSH)
457             ssh_mock.execute = \
458                 mock.Mock(return_value=(0, SYS_CLASS_NET + IP_ADDR_SHOW, ""))
459             ssh.from_node.return_value = ssh_mock
460             self.s.map_topology_to_infrastructure()
461
462         nodes = self.context_cfg["nodes"]
463         self.assertEqual("../../vnf_descriptors/tg_rfc2544_tpl.yaml", nodes['tg__1']['VNF model'])
464         self.assertEqual("../../vnf_descriptors/vpe_vnf.yaml", nodes['vnf__1']['VNF model'])
465
466     def test_map_topology_to_infrastructure_insufficient_nodes(self):
467         del self.context_cfg['nodes']['vnf__1']
468         with mock.patch("yardstick.ssh.SSH") as ssh:
469             ssh_mock = mock.Mock(autospec=ssh.SSH)
470             ssh_mock.execute = \
471                 mock.Mock(return_value=(1, SYS_CLASS_NET + IP_ADDR_SHOW, ""))
472             ssh.from_node.return_value = ssh_mock
473
474             with self.assertRaises(IncorrectSetup):
475                 self.s.map_topology_to_infrastructure()
476
477     def test_map_topology_to_infrastructure_config_invalid(self):
478         cfg = dict(self.context_cfg)
479         del cfg['nodes']['vnf__1']['interfaces']['xe0']['local_mac']
480         with mock.patch("yardstick.ssh.SSH") as ssh:
481             ssh_mock = mock.Mock(autospec=ssh.SSH)
482             ssh_mock.execute = \
483                 mock.Mock(return_value=(0, SYS_CLASS_NET + IP_ADDR_SHOW, ""))
484             ssh.from_node.return_value = ssh_mock
485
486             with self.assertRaises(IncorrectConfig):
487                 self.s.map_topology_to_infrastructure()
488
489     def test__resolve_topology_invalid_config(self):
490         with mock.patch("yardstick.ssh.SSH") as ssh:
491             ssh_mock = mock.Mock(autospec=ssh.SSH)
492             ssh_mock.execute = \
493                 mock.Mock(return_value=(0, SYS_CLASS_NET + IP_ADDR_SHOW, ""))
494             ssh.from_node.return_value = ssh_mock
495
496             # purge an important key from the data structure
497             for interface in self.tg__1['interfaces'].values():
498                 del interface['local_mac']
499
500             with mock.patch(
501                 "yardstick.benchmark.scenarios.networking.vnf_generic.LOG") as mock_log:
502                 with self.assertRaises(IncorrectConfig) as raised:
503                     self.s._resolve_topology()
504
505             self.assertIn('not found', str(raised.exception))
506
507             # restore local_mac
508             for index, interface in enumerate(self.tg__1['interfaces'].values()):
509                 interface['local_mac'] = '00:00:00:00:00:{:2x}'.format(index)
510
511             # make a connection point ref with 3 points
512             self.s.topology["vld"][0]['vnfd-connection-point-ref'].append(
513                 self.s.topology["vld"][0]['vnfd-connection-point-ref'][0])
514
515             with mock.patch(
516                 "yardstick.benchmark.scenarios.networking.vnf_generic.LOG") as mock_log:
517                 with self.assertRaises(IncorrectConfig) as raised:
518                     self.s._resolve_topology()
519
520             self.assertIn('wrong endpoint count', str(raised.exception))
521
522             # make a connection point ref with 1 point
523             self.s.topology["vld"][0]['vnfd-connection-point-ref'] = \
524                 self.s.topology["vld"][0]['vnfd-connection-point-ref'][:1]
525
526             with mock.patch(
527                 "yardstick.benchmark.scenarios.networking.vnf_generic.LOG") as mock_log:
528                 with self.assertRaises(IncorrectConfig) as raised:
529                     self.s._resolve_topology()
530
531             self.assertIn('wrong endpoint count', str(raised.exception))
532
533     def test_run(self):
534         tgen = mock.Mock(autospec=GenericTrafficGen)
535         tgen.traffic_finished = True
536         verified_dict = {"verified": True}
537         tgen.verify_traffic = lambda x: verified_dict
538         tgen.name = "tgen__1"
539         vnf = mock.Mock(autospec=GenericVNF)
540         vnf.runs_traffic = False
541         self.s.vnfs = [tgen, vnf]
542         self.s.traffic_profile = mock.Mock()
543         self.s.collector = mock.Mock(autospec=Collector)
544         self.s.collector.get_kpi = \
545             mock.Mock(return_value={tgen.name: verified_dict})
546         result = {}
547         self.s.run(result)
548         self.assertDictEqual(result, {tgen.name: verified_dict})
549
550     def test_setup(self):
551         with mock.patch("yardstick.ssh.SSH") as ssh:
552             ssh_mock = mock.Mock(autospec=ssh.SSH)
553             ssh_mock.execute = \
554                 mock.Mock(return_value=(0, SYS_CLASS_NET + IP_ADDR_SHOW, ""))
555             ssh.from_node.return_value = ssh_mock
556
557             tgen = mock.Mock(autospec=GenericTrafficGen)
558             tgen.traffic_finished = True
559             verified_dict = {"verified": True}
560             tgen.verify_traffic = lambda x: verified_dict
561             tgen.terminate = mock.Mock(return_value=True)
562             tgen.name = "tgen__1"
563             vnf = mock.Mock(autospec=GenericVNF)
564             vnf.runs_traffic = False
565             vnf.terminate = mock.Mock(return_value=True)
566             self.s.vnfs = [tgen, vnf]
567             self.s.traffic_profile = mock.Mock()
568             self.s.collector = mock.Mock(autospec=Collector)
569             self.s.collector.get_kpi = \
570                 mock.Mock(return_value={tgen.name: verified_dict})
571             self.s.map_topology_to_infrastructure = mock.Mock(return_value=0)
572             self.s.load_vnf_models = mock.Mock(return_value=self.s.vnfs)
573             self.s._fill_traffic_profile = \
574                 mock.Mock(return_value=TRAFFIC_PROFILE)
575             self.assertEqual(None, self.s.setup())
576
577     def test__get_traffic_profile(self):
578         self.scenario_cfg["traffic_profile"] = \
579             self._get_file_abspath("ipv4_throughput_vpe.yaml")
580         self.assertIsNotNone(self.s._get_traffic_profile())
581
582     def test__get_traffic_profile_exception(self):
583         with mock.patch.dict(self.scenario_cfg, {'traffic_profile': ''}):
584             with self.assertRaises(IOError):
585                 self.s._get_traffic_profile()
586
587     def test___get_traffic_imix_exception(self):
588         with mock.patch.dict(self.scenario_cfg["traffic_options"], {'imix': ''}):
589             self.assertEqual({}, self.s._get_traffic_imix())
590
591     def test__fill_traffic_profile(self):
592         with mock.patch.dict("sys.modules", STL_MOCKS):
593             self.scenario_cfg["traffic_profile"] = \
594                 self._get_file_abspath("ipv4_throughput_vpe.yaml")
595             self.scenario_cfg["traffic_options"]["flow"] = \
596                 self._get_file_abspath("ipv4_1flow_Packets_vpe.yaml")
597             self.scenario_cfg["traffic_options"]["imix"] = \
598                 self._get_file_abspath("imix_voice.yaml")
599             self.assertIsNotNone(self.s._fill_traffic_profile())
600
601     def test_teardown(self):
602         vnf = mock.Mock(autospec=GenericVNF)
603         vnf.terminate = \
604             mock.Mock(return_value=True)
605         self.s.vnfs = [vnf]
606         self.s.traffic_profile = mock.Mock()
607         self.s.collector = mock.Mock(autospec=Collector)
608         self.s.collector.stop = \
609             mock.Mock(return_value=True)
610         self.assertIsNone(self.s.teardown())
611
612     SAMPLE_NETDEVS = {
613         'enp11s0': {
614             'address': '0a:de:ad:be:ef:f5',
615             'device': '0x1533',
616             'driver': 'igb',
617             'ifindex': '2',
618             'interface_name': 'enp11s0',
619             'operstate': 'down',
620             'pci_bus_id': '0000:0b:00.0',
621             'subsystem_device': '0x1533',
622             'subsystem_vendor': '0x15d9',
623             'vendor': '0x8086'
624         },
625         'lan': {
626             'address': '0a:de:ad:be:ef:f4',
627             'device': '0x153a',
628             'driver': 'e1000e',
629             'ifindex': '3',
630             'interface_name': 'lan',
631             'operstate': 'up',
632             'pci_bus_id': '0000:00:19.0',
633             'subsystem_device': '0x153a',
634             'subsystem_vendor': '0x15d9',
635             'vendor': '0x8086'
636         }
637     }
638
639     SAMPLE_VM_NETDEVS = {
640         'eth1': {
641             'address': 'fa:de:ad:be:ef:5b',
642             'device': '0x0001',
643             'driver': 'virtio_net',
644             'ifindex': '3',
645             'interface_name': 'eth1',
646             'operstate': 'down',
647             'pci_bus_id': '0000:00:04.0',
648             'vendor': '0x1af4'
649         }
650     }
651
652     def test_parse_netdev_info(self):
653         output = """\
654 /sys/devices/pci0000:00/0000:00:1c.3/0000:0b:00.0/net/enp11s0/ifindex:2
655 /sys/devices/pci0000:00/0000:00:1c.3/0000:0b:00.0/net/enp11s0/address:0a:de:ad:be:ef:f5
656 /sys/devices/pci0000:00/0000:00:1c.3/0000:0b:00.0/net/enp11s0/operstate:down
657 /sys/devices/pci0000:00/0000:00:1c.3/0000:0b:00.0/net/enp11s0/device/vendor:0x8086
658 /sys/devices/pci0000:00/0000:00:1c.3/0000:0b:00.0/net/enp11s0/device/device:0x1533
659 /sys/devices/pci0000:00/0000:00:1c.3/0000:0b:00.0/net/enp11s0/device/subsystem_vendor:0x15d9
660 /sys/devices/pci0000:00/0000:00:1c.3/0000:0b:00.0/net/enp11s0/device/subsystem_device:0x1533
661 /sys/devices/pci0000:00/0000:00:1c.3/0000:0b:00.0/net/enp11s0/driver:igb
662 /sys/devices/pci0000:00/0000:00:1c.3/0000:0b:00.0/net/enp11s0/pci_bus_id:0000:0b:00.0
663 /sys/devices/pci0000:00/0000:00:19.0/net/lan/ifindex:3
664 /sys/devices/pci0000:00/0000:00:19.0/net/lan/address:0a:de:ad:be:ef:f4
665 /sys/devices/pci0000:00/0000:00:19.0/net/lan/operstate:up
666 /sys/devices/pci0000:00/0000:00:19.0/net/lan/device/vendor:0x8086
667 /sys/devices/pci0000:00/0000:00:19.0/net/lan/device/device:0x153a
668 /sys/devices/pci0000:00/0000:00:19.0/net/lan/device/subsystem_vendor:0x15d9
669 /sys/devices/pci0000:00/0000:00:19.0/net/lan/device/subsystem_device:0x153a
670 /sys/devices/pci0000:00/0000:00:19.0/net/lan/driver:e1000e
671 /sys/devices/pci0000:00/0000:00:19.0/net/lan/pci_bus_id:0000:00:19.0
672 """
673         res = NetworkServiceTestCase.parse_netdev_info(output)
674         assert res == self.SAMPLE_NETDEVS
675
676     def test_parse_netdev_info_virtio(self):
677         output = """\
678 /sys/devices/pci0000:00/0000:00:04.0/virtio1/net/eth1/ifindex:3
679 /sys/devices/pci0000:00/0000:00:04.0/virtio1/net/eth1/address:fa:de:ad:be:ef:5b
680 /sys/devices/pci0000:00/0000:00:04.0/virtio1/net/eth1/operstate:down
681 /sys/devices/pci0000:00/0000:00:04.0/virtio1/net/eth1/device/vendor:0x1af4
682 /sys/devices/pci0000:00/0000:00:04.0/virtio1/net/eth1/device/device:0x0001
683 /sys/devices/pci0000:00/0000:00:04.0/virtio1/net/eth1/driver:virtio_net
684 """
685         res = NetworkServiceTestCase.parse_netdev_info(output)
686         assert res == self.SAMPLE_VM_NETDEVS
687
688     def test_sort_dpdk_port_num(self):
689         netdevs = self.SAMPLE_NETDEVS.copy()
690         NetworkServiceTestCase._sort_dpdk_port_num(netdevs)
691         assert netdevs['lan']['dpdk_port_num'] == 0
692         assert netdevs['enp11s0']['dpdk_port_num'] == 1
693
694     def test_probe_missing_values(self):
695         netdevs = self.SAMPLE_NETDEVS.copy()
696         network = {'local_mac': '0a:de:ad:be:ef:f5'}
697         NetworkServiceTestCase._probe_missing_values(netdevs, network, set())
698         assert network['vpci'] == '0000:0b:00.0'
699
700         network = {'local_mac': '0a:de:ad:be:ef:f4'}
701         NetworkServiceTestCase._probe_missing_values(netdevs, network, set())
702         assert network['vpci'] == '0000:00:19.0'
703
704     def test_open_relative_path(self):
705         mock_open = mock.mock_open()
706         mock_open_result = mock_open()
707         mock_open_call_count = 1  # initial call to get result
708
709         module_name = \
710             'yardstick.benchmark.scenarios.networking.vnf_generic.open'
711
712         # test
713         with mock.patch(module_name, mock_open, create=True):
714             self.assertEqual(open_relative_file('foo', 'bar'), mock_open_result)
715
716             mock_open_call_count += 1  # one more call expected
717             self.assertEqual(mock_open.call_count, mock_open_call_count)
718             self.assertIn('foo', mock_open.call_args_list[-1][0][0])
719             self.assertNotIn('bar', mock_open.call_args_list[-1][0][0])
720
721             def open_effect(*args, **kwargs):
722                 if kwargs.get('name', args[0]) == os.path.join('bar', 'foo'):
723                     return mock_open_result
724                 raise IOError(errno.ENOENT, 'not found')
725
726             mock_open.side_effect = open_effect
727             self.assertEqual(open_relative_file('foo', 'bar'), mock_open_result)
728
729             mock_open_call_count += 2  # two more calls expected
730             self.assertEqual(mock_open.call_count, mock_open_call_count)
731             self.assertIn('foo', mock_open.call_args_list[-1][0][0])
732             self.assertIn('bar', mock_open.call_args_list[-1][0][0])
733
734             # test an IOError of type ENOENT
735             mock_open.side_effect = IOError(errno.ENOENT, 'not found')
736             with self.assertRaises(IOError):
737                 # the second call still raises
738                 open_relative_file('foo', 'bar')
739
740             mock_open_call_count += 2  # two more calls expected
741             self.assertEqual(mock_open.call_count, mock_open_call_count)
742             self.assertIn('foo', mock_open.call_args_list[-1][0][0])
743             self.assertIn('bar', mock_open.call_args_list[-1][0][0])
744
745             # test an IOError other than ENOENT
746             mock_open.side_effect = IOError(errno.EBUSY, 'busy')
747             with self.assertRaises(IOError):
748                 open_relative_file('foo', 'bar')
749
750             mock_open_call_count += 1  # one more call expected
751             self.assertEqual(mock_open.call_count, mock_open_call_count)