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