1 # Copyright (c) 2016-2017 Intel Corporation
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 from copy import deepcopy
22 from yardstick import tests
23 from yardstick.common import exceptions
24 from yardstick.common import utils
25 from yardstick.network_services.collector.subscriber import Collector
26 from yardstick.network_services.traffic_profile import base
27 from yardstick.network_services.vnf_generic import vnfdgen
28 from yardstick.network_services.vnf_generic.vnf.base import GenericTrafficGen
29 from yardstick.network_services.vnf_generic.vnf.base import GenericVNF
32 stl_patch = mock.patch.dict(sys.modules, tests.STL_MOCKS)
36 from yardstick.benchmark.scenarios.networking import vnf_generic
38 # pylint: disable=unused-argument
39 # disable this for now because I keep forgetting mock patch arg ordering
42 COMPLETE_TREX_VNFD = {
43 'vnfd:vnfd-catalog': {
52 'tx_throughput_pc_linerate',
53 'rx_throughput_pc_linerate',
69 'description': 'TRex stateless traffic generator for RFC2544',
70 'id': 'TrexTrafficGen',
75 'vdu-id': 'trexgen-baremetal',
78 'short-name': 'trexgen',
79 'class-name': 'TrexTrafficGen',
82 'description': 'TRex stateless traffic generator for RFC2544',
83 'external-interface': [
86 'virtual-interface': {
87 'bandwidth': '10 Gbps',
89 'dst_mac': '00:01:02:03:04:05',
90 'local_ip': '1.1.1.2',
91 'local_mac': '00:01:02:03:05:05',
92 'type': 'PCI-PASSTHROUGH',
93 'netmask': "255.255.255.0",
95 'vpci': '0000:00:10.2',
97 'vnfd-connection-point-ref': 'xe0',
101 'virtual-interface': {
102 'bandwidth': '10 Gbps',
104 'dst_mac': '00:01:02:03:04:06',
105 'local_ip': '2.1.1.2',
106 'local_mac': '00:01:02:03:05:06',
107 'type': 'PCI-PASSTHROUGH',
108 'netmask': "255.255.255.0",
110 'vpci': '0000:00:10.1',
112 'vnfd-connection-point-ref': 'xe1',
115 'id': 'trexgen-baremetal',
116 'name': 'trexgen-baremetal',
125 28: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP \
126 group default qlen 1000
127 link/ether 90:e2:ba:a7:6a:c8 brd ff:ff:ff:ff:ff:ff
128 inet 1.1.1.1/8 brd 1.255.255.255 scope global eth1
129 inet6 fe80::92e2:baff:fea7:6ac8/64 scope link
130 valid_lft forever preferred_lft forever
131 29: eth5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP \
132 group default qlen 1000
133 link/ether 90:e2:ba:a7:6a:c9 brd ff:ff:ff:ff:ff:ff
134 inet 2.1.1.1/8 brd 2.255.255.255 scope global eth5
135 inet6 fe80::92e2:baff:fea7:6ac9/64 scope link tentative
136 valid_lft forever preferred_lft forever
140 lrwxrwxrwx 1 root root 0 sie 10 14:16 eth1 -> \
141 ../../devices/pci0000:80/0000:80:02.2/0000:84:00.1/net/eth1
142 lrwxrwxrwx 1 root root 0 sie 3 10:37 eth2 -> \
143 ../../devices/pci0000:00/0000:00:01.1/0000:84:00.2/net/eth5
147 "schema": "isb:traffic_profile:0.1",
149 "description": "Fixed traffic profile to run UDP traffic",
151 "traffic_type": "FixedTraffic",
152 "frame_rate": 100, # pps
159 class TestNetworkServiceTestCase(unittest.TestCase):
163 'name': 'trafficgen_1.yardstick',
165 'role': 'TrafficGen',
170 'netmask': '255.255.255.0',
171 'local_ip': '152.16.100.20',
172 'local_mac': '00:00:00:00:00:01',
174 'vpci': '0000:07:00.0',
178 'netmask': '255.255.255.0',
179 'local_ip': '152.16.40.20',
180 'local_mac': '00:00:00:00:00:02',
182 'vpci': '0000:07:00.1',
189 'name': 'vnf.yardstick',
191 'host': '10.223.197.164',
197 'netmask': '255.255.255.0',
198 'local_ip': '152.16.100.19',
199 'local_mac': '00:00:00:00:00:03',
201 'vpci': '0000:07:00.0',
205 'netmask': '255.255.255.0',
206 'local_ip': '152.16.40.19',
207 'local_mac': '00:00:00:00:00:04',
209 'vpci': '0000:07:00.1',
215 'netmask': '255.255.255.0',
216 'gateway': '152.16.100.20',
217 'network': '152.16.100.20',
221 'netmask': '255.255.255.0',
222 'gateway': '152.16.40.20',
223 'network': '152.16.40.20',
230 'gateway': '0064:ff9b:0:0:0:0:9810:6414',
231 'network': '0064:ff9b:0:0:0:0:9810:6414',
236 'gateway': '0064:ff9b:0:0:0:0:9810:2814',
237 'network': '0064:ff9b:0:0:0:0:9810:2814',
246 'vnf__1': self.vnf__1,
250 'vld_id': GenericVNF.UPLINK,
252 GenericVNF.DOWNLINK: {
253 'vld_id': GenericVNF.DOWNLINK,
259 'vnfd-connection-point-ref': [
261 'vnfd-connection-point-ref': 'xe0',
262 'member-vnf-index-ref': '1',
263 'vnfd-id-ref': 'trexgen'
266 'vnfd-connection-point-ref': 'xe0',
267 'member-vnf-index-ref': '2',
268 'vnfd-id-ref': 'trexgen'
272 'id': GenericVNF.UPLINK,
273 'name': 'tg__1 to vnf__1 link 1'
277 'vnfd-connection-point-ref': [
279 'vnfd-connection-point-ref': 'xe1',
280 'member-vnf-index-ref': '1',
281 'vnfd-id-ref': 'trexgen'
284 'vnfd-connection-point-ref': 'xe1',
285 'member-vnf-index-ref': '2',
286 'vnfd-id-ref': 'trexgen'
290 'id': GenericVNF.DOWNLINK,
291 'name': 'vnf__1 to tg__1 link 2'
295 'id': 'trex-tg-topology',
296 'short-name': 'trex-tg-topology',
297 'name': 'trex-tg-topology',
298 'description': 'trex-tg-topology',
299 'constituent-vnfd': [
301 'member-vnf-index': '1',
302 'VNF model': 'tg_trex_tpl.yaml',
303 'vnfd-id-ref': 'tg__1',
306 'member-vnf-index': '2',
307 'VNF model': 'tg_trex_tpl.yaml',
308 'vnfd-id-ref': 'vnf__1',
311 'vld': [self.vld0, self.vld1],
314 self.scenario_cfg = {
316 "topology": self._get_file_abspath("vpe_vnf_topology.yaml"),
317 'task_id': 'a70bdf4a-8e67-47a3-9dc1-273c14506eb7',
318 'tc': 'tc_ipv4_1Mflow_64B_packetsize',
319 'traffic_profile': 'ipv4_throughput_vpe.yaml',
320 'extra_args': {'arg1': 'value1', 'arg2': 'value2'},
324 'allowed_drop_rate': '0.8 - 1',
328 'framesize': {'64B': 100}
331 'object': 'NetworkServiceTestCase',
333 'output_filename': 'yardstick.out',
339 'flow': 'ipv4_1flow_Packets_vpe.yaml',
340 'imix': 'imix_voice.yaml'
343 'tg__2': 'trafficgen_2.yardstick',
344 'tg__1': 'trafficgen_1.yardstick',
345 'vnf__1': 'vnf.yardstick',
349 self.s = vnf_generic.NetworkServiceTestCase(self.scenario_cfg,
352 def _get_file_abspath(self, filename):
353 curr_path = os.path.dirname(os.path.abspath(__file__))
354 file_path = os.path.join(curr_path, filename)
357 def test___init__(self):
358 self.assertIsNotNone(self.topology)
360 def test__get_ip_flow_range_string(self):
361 self.scenario_cfg["traffic_options"]["flow"] = \
362 self._get_file_abspath("ipv4_1flow_Packets_vpe.yaml")
363 result = '152.16.100.2-152.16.100.254'
364 self.assertEqual(result, self.s._get_ip_flow_range(
365 '152.16.100.2-152.16.100.254'))
367 def test__get_ip_flow_range(self):
368 self.scenario_cfg["traffic_options"]["flow"] = \
369 self._get_file_abspath("ipv4_1flow_Packets_vpe.yaml")
370 result = '152.16.100.2-152.16.100.254'
371 self.assertEqual(result, self.s._get_ip_flow_range({"tg__1": 'xe0'}))
373 @mock.patch('yardstick.benchmark.scenarios.networking.vnf_generic.ipaddress')
374 def test__get_ip_flow_range_no_node_data(self, mock_ipaddress):
375 scenario_cfg = deepcopy(self.scenario_cfg)
376 scenario_cfg["traffic_options"]["flow"] = \
377 self._get_file_abspath("ipv4_1flow_Packets_vpe.yaml")
379 mock_ipaddress.ip_network.return_value = ipaddr = mock.Mock()
380 ipaddr.hosts.return_value = []
383 result = self.s._get_ip_flow_range({"tg__2": 'xe0'})
384 self.assertEqual(result, expected)
386 def test__get_ip_flow_range_no_nodes(self):
388 result = self.s._get_ip_flow_range({})
389 self.assertEqual(result, expected)
391 def test___get_traffic_flow(self):
392 self.scenario_cfg["traffic_options"]["flow"] = \
393 self._get_file_abspath("ipv4_1flow_Packets_vpe.yaml")
394 self.scenario_cfg["options"] = {}
395 self.scenario_cfg['options'] = {
407 'public_ip': ['1.1.1.1'],
410 # NOTE(ralonsoh): check the expected output. This test could be
412 # result = {'flow': {'dst_ip0': '152.16.40.2-152.16.40.254',
413 # 'src_ip0': '152.16.100.2-152.16.100.254'}}
414 self.assertEqual({'flow': {}}, self.s._get_traffic_flow())
416 def test___get_traffic_flow_error(self):
417 self.scenario_cfg["traffic_options"]["flow"] = \
418 "ipv4_1flow_Packets_vpe.yaml1"
419 self.assertEqual({'flow': {}}, self.s._get_traffic_flow())
421 def test_get_vnf_imp(self):
422 vnfd = COMPLETE_TREX_VNFD['vnfd:vnfd-catalog']['vnfd'][0]['class-name']
423 with mock.patch.dict(sys.modules, tests.STL_MOCKS):
424 self.assertIsNotNone(self.s.get_vnf_impl(vnfd))
426 with self.assertRaises(exceptions.IncorrectConfig) as raised:
427 self.s.get_vnf_impl('NonExistentClass')
429 exc_str = str(raised.exception)
431 self.assertIn('No implementation', exc_str)
432 self.assertIn('found in', exc_str)
434 def test_load_vnf_models_invalid(self):
435 self.context_cfg["nodes"]['tg__1']['VNF model'] = \
436 self._get_file_abspath("tg_trex_tpl.yaml")
437 self.context_cfg["nodes"]['vnf__1']['VNF model'] = \
438 self._get_file_abspath("tg_trex_tpl.yaml")
440 vnf = mock.Mock(autospec=GenericVNF)
441 self.s.get_vnf_impl = mock.Mock(return_value=vnf)
443 self.assertIsNotNone(
444 self.s.load_vnf_models(self.scenario_cfg, self.context_cfg))
446 def test_load_vnf_models_no_model(self):
447 vnf = mock.Mock(autospec=GenericVNF)
448 self.s.get_vnf_impl = mock.Mock(return_value=vnf)
450 self.assertIsNotNone(
451 self.s.load_vnf_models(self.scenario_cfg, self.context_cfg))
453 def test_map_topology_to_infrastructure(self):
454 self.s.map_topology_to_infrastructure()
456 nodes = self.context_cfg["nodes"]
457 self.assertEqual('../../vnf_descriptors/tg_rfc2544_tpl.yaml',
458 nodes['tg__1']['VNF model'])
459 self.assertEqual('../../vnf_descriptors/vpe_vnf.yaml',
460 nodes['vnf__1']['VNF model'])
462 def test_map_topology_to_infrastructure_insufficient_nodes(self):
463 cfg = deepcopy(self.context_cfg)
464 del cfg['nodes']['vnf__1']
466 cfg_patch = mock.patch.object(self.s, 'context_cfg', cfg)
468 with self.assertRaises(exceptions.IncorrectConfig):
469 self.s.map_topology_to_infrastructure()
471 def test_map_topology_to_infrastructure_config_invalid(self):
472 ssh_mock = mock.Mock()
473 ssh_mock.execute.return_value = 0, SYS_CLASS_NET + IP_ADDR_SHOW, ""
475 cfg = deepcopy(self.s.context_cfg)
477 # delete all, we don't know which will come first
478 del cfg['nodes']['vnf__1']['interfaces']['xe0']['local_mac']
479 del cfg['nodes']['vnf__1']['interfaces']['xe1']['local_mac']
480 del cfg['nodes']['tg__1']['interfaces']['xe0']['local_mac']
481 del cfg['nodes']['tg__1']['interfaces']['xe1']['local_mac']
483 config_patch = mock.patch.object(self.s, 'context_cfg', cfg)
485 with self.assertRaises(exceptions.IncorrectConfig):
486 self.s.map_topology_to_infrastructure()
488 def test__resolve_topology_invalid_config(self):
489 with mock.patch("yardstick.ssh.SSH") as ssh:
490 ssh_mock = mock.Mock(autospec=ssh.SSH)
492 mock.Mock(return_value=(0, SYS_CLASS_NET + IP_ADDR_SHOW, ""))
493 ssh.from_node.return_value = ssh_mock
495 # purge an important key from the data structure
496 for interface in self.tg__1['interfaces'].values():
497 del interface['local_mac']
499 with self.assertRaises(exceptions.IncorrectConfig) as raised:
500 self.s._resolve_topology()
502 self.assertIn('not found', str(raised.exception))
505 for index, interface in enumerate(self.tg__1['interfaces'].values()):
506 interface['local_mac'] = '00:00:00:00:00:{:2x}'.format(index)
508 # make a connection point ref with 3 points
509 self.s.topology["vld"][0]['vnfd-connection-point-ref'].append(
510 self.s.topology["vld"][0]['vnfd-connection-point-ref'][0])
512 with self.assertRaises(exceptions.IncorrectConfig) as raised:
513 self.s._resolve_topology()
515 self.assertIn('wrong endpoint count', str(raised.exception))
517 # make a connection point ref with 1 point
518 self.s.topology["vld"][0]['vnfd-connection-point-ref'] = \
519 self.s.topology["vld"][0]['vnfd-connection-point-ref'][:1]
521 with self.assertRaises(exceptions.IncorrectConfig) as raised:
522 self.s._resolve_topology()
524 self.assertIn('wrong endpoint count', str(raised.exception))
527 tgen = mock.Mock(autospec=GenericTrafficGen)
528 tgen.traffic_finished = True
529 verified_dict = {"verified": True}
530 tgen.verify_traffic = lambda x: verified_dict
531 tgen.name = "tgen__1"
532 vnf = mock.Mock(autospec=GenericVNF)
533 vnf.runs_traffic = False
534 self.s.vnfs = [tgen, vnf]
535 self.s.traffic_profile = mock.Mock()
536 self.s.collector = mock.Mock(autospec=Collector)
537 self.s.collector.get_kpi = \
538 mock.Mock(return_value={tgen.name: verified_dict})
541 self.assertDictEqual(result, {tgen.name: verified_dict})
543 def test_setup(self):
544 with mock.patch("yardstick.ssh.SSH") as ssh:
545 ssh_mock = mock.Mock(autospec=ssh.SSH)
547 mock.Mock(return_value=(0, SYS_CLASS_NET + IP_ADDR_SHOW, ""))
548 ssh.from_node.return_value = ssh_mock
550 tgen = mock.Mock(autospec=GenericTrafficGen)
551 tgen.traffic_finished = True
552 verified_dict = {"verified": True}
553 tgen.verify_traffic = lambda x: verified_dict
554 tgen.terminate = mock.Mock(return_value=True)
555 tgen.name = "tgen__1"
556 vnf = mock.Mock(autospec=GenericVNF)
557 vnf.runs_traffic = False
558 vnf.terminate = mock.Mock(return_value=True)
559 self.s.vnfs = [tgen, vnf]
560 self.s.traffic_profile = mock.Mock()
561 self.s.collector = mock.Mock(autospec=Collector)
562 self.s.collector.get_kpi = \
563 mock.Mock(return_value={tgen.name: verified_dict})
564 self.s.map_topology_to_infrastructure = mock.Mock(return_value=0)
565 self.s.load_vnf_models = mock.Mock(return_value=self.s.vnfs)
566 self.s._fill_traffic_profile = \
567 mock.Mock(return_value=TRAFFIC_PROFILE)
568 self.assertIsNone(self.s.setup())
570 def test_setup_exception(self):
571 with mock.patch("yardstick.ssh.SSH") as ssh:
572 ssh_mock = mock.Mock(autospec=ssh.SSH)
574 mock.Mock(return_value=(0, SYS_CLASS_NET + IP_ADDR_SHOW, ""))
575 ssh.from_node.return_value = ssh_mock
577 tgen = mock.Mock(autospec=GenericTrafficGen)
578 tgen.traffic_finished = True
579 verified_dict = {"verified": True}
580 tgen.verify_traffic = lambda x: verified_dict
581 tgen.terminate = mock.Mock(return_value=True)
582 tgen.name = "tgen__1"
583 vnf = mock.Mock(autospec=GenericVNF)
584 vnf.runs_traffic = False
585 vnf.instantiate.side_effect = RuntimeError(
586 "error during instantiate")
587 vnf.terminate = mock.Mock(return_value=True)
588 self.s.vnfs = [tgen, vnf]
589 self.s.traffic_profile = mock.Mock()
590 self.s.collector = mock.Mock(autospec=Collector)
591 self.s.collector.get_kpi = \
592 mock.Mock(return_value={tgen.name: verified_dict})
593 self.s.map_topology_to_infrastructure = mock.Mock(return_value=0)
594 self.s.load_vnf_models = mock.Mock(return_value=self.s.vnfs)
595 self.s._fill_traffic_profile = \
596 mock.Mock(return_value=TRAFFIC_PROFILE)
597 with self.assertRaises(RuntimeError):
600 def test__get_traffic_profile(self):
601 self.scenario_cfg["traffic_profile"] = \
602 self._get_file_abspath("ipv4_throughput_vpe.yaml")
603 self.assertIsNotNone(self.s._get_traffic_profile())
605 def test__get_traffic_profile_exception(self):
606 with mock.patch.dict(self.scenario_cfg, {'traffic_profile': ''}):
607 with self.assertRaises(IOError):
608 self.s._get_traffic_profile()
610 def test___get_traffic_imix_exception(self):
611 with mock.patch.dict(self.scenario_cfg["traffic_options"], {'imix': ''}):
612 self.assertEqual({'imix': {'64B': 100}},
613 self.s._get_traffic_imix())
615 @mock.patch.object(base.TrafficProfile, 'get')
616 @mock.patch.object(vnfdgen, 'generate_vnfd')
617 def test__fill_traffic_profile(self, mock_generate, mock_tprofile_get):
618 fake_tprofile = mock.Mock()
619 fake_vnfd = mock.Mock()
620 with mock.patch.object(self.s, '_get_traffic_profile',
621 return_value=fake_tprofile) as mock_get_tp:
622 mock_generate.return_value = fake_vnfd
623 self.s._fill_traffic_profile()
624 mock_get_tp.assert_called_once()
625 mock_generate.assert_called_once_with(
628 'extra_args': {'arg1': 'value1', 'arg2': 'value2'},
629 'flow': {'flow': {}},
630 'imix': {'imix': {'64B': 100}},
634 mock_tprofile_get.assert_called_once_with(fake_vnfd)
636 @mock.patch.object(utils, 'open_relative_file')
637 def test__get_topology(self, mock_open_path):
638 self.s.scenario_cfg['topology'] = 'fake_topology'
639 self.s.scenario_cfg['task_path'] = 'fake_path'
640 mock_open_path.side_effect = mock.mock_open(read_data='fake_data')
641 self.assertEqual('fake_data', self.s._get_topology())
642 mock_open_path.assert_called_once_with('fake_topology', 'fake_path')
644 @mock.patch.object(vnfdgen, 'generate_vnfd')
645 def test__render_topology(self, mock_generate):
646 fake_topology = 'fake_topology'
647 mock_generate.return_value = {'nsd:nsd-catalog': {'nsd': ['fake_nsd']}}
648 with mock.patch.object(self.s, '_get_topology',
649 return_value=fake_topology) as mock_get_topology:
650 self.s._render_topology()
651 mock_get_topology.assert_called_once()
653 mock_generate.assert_called_once_with(
655 {'extra_args': {'arg1': 'value1', 'arg2': 'value2'}}
657 self.assertEqual(self.s.topology, 'fake_nsd')
659 def test_teardown(self):
660 vnf = mock.Mock(autospec=GenericVNF)
661 vnf.terminate = mock.Mock(return_value=True)
664 self.s.traffic_profile = mock.Mock()
665 self.s.collector = mock.Mock(autospec=Collector)
666 self.s.collector.stop = \
667 mock.Mock(return_value=True)
668 self.assertIsNone(self.s.teardown())
670 def test_teardown_exception(self):
671 vnf = mock.Mock(autospec=GenericVNF)
672 vnf.terminate = mock.Mock(
673 side_effect=RuntimeError("error duing terminate"))
676 self.s.traffic_profile = mock.Mock()
677 self.s.collector = mock.Mock(autospec=Collector)
678 self.s.collector.stop = \
679 mock.Mock(return_value=True)
680 with self.assertRaises(RuntimeError):