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 result = '152.16.100.2-152.16.100.254'
362 self.assertEqual(result, self.s._get_ip_flow_range(
363 '152.16.100.2-152.16.100.254'))
365 def test__get_ip_flow_range_no_nodes(self):
366 self.assertEqual('0.0.0.0', self.s._get_ip_flow_range({}))
368 def test__get_ip_flow_range_no_node_data(self):
369 node_data = {'tg__0': 'xe0'}
370 self.s.context_cfg['nodes']['tg__0'] = {}
371 result = self.s._get_ip_flow_range(node_data)
372 self.assertEqual('0.0.0.2-0.0.0.254', result)
374 def test__et_ip_flow_range_ipv4(self):
375 node_data = {'tg__0': 'xe0'}
376 self.s.context_cfg['nodes']['tg__0'] = {
378 'xe0': {'local_ip': '192.168.1.15',
379 'netmask': '255.255.255.128'}
382 result = self.s._get_ip_flow_range(node_data)
383 self.assertEqual('192.168.1.2-192.168.1.126', result)
385 def test__get_ip_flow_range_ipv4_mask_30(self):
386 node_data = {'tg__0': 'xe0'}
387 self.s.context_cfg['nodes']['tg__0'] = {
389 'xe0': {'local_ip': '192.168.1.15', 'netmask': 30}
392 result = self.s._get_ip_flow_range(node_data)
393 self.assertEqual('192.168.1.15', result)
395 def test__get_ip_flow_range_ipv6(self):
396 node_data = {'tg__0': 'xe0'}
397 self.s.context_cfg['nodes']['tg__0'] = {
399 'xe0': {'local_ip': '2001::11', 'netmask': 64}
402 result = self.s._get_ip_flow_range(node_data)
403 self.assertEqual('2001::2-2001::ffff:ffff:ffff:fffe', result)
405 def test___get_traffic_flow(self):
406 self.scenario_cfg["traffic_options"]["flow"] = \
407 self._get_file_abspath("ipv4_1flow_Packets_vpe.yaml")
408 self.scenario_cfg['options'] = {
420 'public_ip': ['1.1.1.1'],
423 expected_flow = {'flow': {'dst_ip_0': '152.16.40.2-152.16.40.254',
424 'public_ip_0': '1.1.1.1',
425 'src_ip_0': '152.16.100.2-152.16.100.254'}}
426 self.assertEqual(expected_flow, self.s._get_traffic_flow())
428 def test___get_traffic_flow_error(self):
429 self.scenario_cfg["traffic_options"]["flow"] = \
430 "ipv4_1flow_Packets_vpe.yaml1"
431 self.assertEqual({'flow': {}}, self.s._get_traffic_flow())
433 def test_get_vnf_imp(self):
434 vnfd = COMPLETE_TREX_VNFD['vnfd:vnfd-catalog']['vnfd'][0]['class-name']
435 with mock.patch.dict(sys.modules, tests.STL_MOCKS):
436 self.assertIsNotNone(self.s.get_vnf_impl(vnfd))
438 with self.assertRaises(exceptions.IncorrectConfig) as raised:
439 self.s.get_vnf_impl('NonExistentClass')
441 exc_str = str(raised.exception)
443 self.assertIn('No implementation', exc_str)
444 self.assertIn('found in', exc_str)
446 def test_load_vnf_models_invalid(self):
447 self.context_cfg["nodes"]['tg__1']['VNF model'] = \
448 self._get_file_abspath("tg_trex_tpl.yaml")
449 self.context_cfg["nodes"]['vnf__1']['VNF model'] = \
450 self._get_file_abspath("tg_trex_tpl.yaml")
451 self.context_cfg['task_id'] = 'fake_task_id'
453 vnf = mock.Mock(autospec=GenericVNF)
454 self.s.get_vnf_impl = mock.Mock(return_value=vnf)
456 self.assertIsNotNone(
457 self.s.load_vnf_models(self.scenario_cfg, self.context_cfg))
459 def test_load_vnf_models_no_model(self):
460 vnf = mock.Mock(autospec=GenericVNF)
461 self.s.get_vnf_impl = mock.Mock(return_value=vnf)
463 self.assertIsNotNone(
464 self.s.load_vnf_models(self.scenario_cfg, self.context_cfg))
466 def test_map_topology_to_infrastructure(self):
467 self.s.map_topology_to_infrastructure()
469 nodes = self.context_cfg["nodes"]
470 self.assertEqual('../../vnf_descriptors/tg_rfc2544_tpl.yaml',
471 nodes['tg__1']['VNF model'])
472 self.assertEqual('../../vnf_descriptors/vpe_vnf.yaml',
473 nodes['vnf__1']['VNF model'])
475 def test_map_topology_to_infrastructure_insufficient_nodes(self):
476 cfg = deepcopy(self.context_cfg)
477 del cfg['nodes']['vnf__1']
479 cfg_patch = mock.patch.object(self.s, 'context_cfg', cfg)
481 with self.assertRaises(exceptions.IncorrectConfig):
482 self.s.map_topology_to_infrastructure()
484 def test_map_topology_to_infrastructure_config_invalid(self):
485 ssh_mock = mock.Mock()
486 ssh_mock.execute.return_value = 0, SYS_CLASS_NET + IP_ADDR_SHOW, ""
488 cfg = deepcopy(self.s.context_cfg)
490 # delete all, we don't know which will come first
491 del cfg['nodes']['vnf__1']['interfaces']['xe0']['local_mac']
492 del cfg['nodes']['vnf__1']['interfaces']['xe1']['local_mac']
493 del cfg['nodes']['tg__1']['interfaces']['xe0']['local_mac']
494 del cfg['nodes']['tg__1']['interfaces']['xe1']['local_mac']
496 config_patch = mock.patch.object(self.s, 'context_cfg', cfg)
498 with self.assertRaises(exceptions.IncorrectConfig):
499 self.s.map_topology_to_infrastructure()
501 def test__resolve_topology_invalid_config(self):
502 with mock.patch("yardstick.ssh.SSH") as ssh:
503 ssh_mock = mock.Mock(autospec=ssh.SSH)
505 mock.Mock(return_value=(0, SYS_CLASS_NET + IP_ADDR_SHOW, ""))
506 ssh.from_node.return_value = ssh_mock
508 # purge an important key from the data structure
509 for interface in self.tg__1['interfaces'].values():
510 del interface['local_mac']
512 with self.assertRaises(exceptions.IncorrectConfig) as raised:
513 self.s._resolve_topology()
515 self.assertIn('not found', str(raised.exception))
518 for index, interface in enumerate(self.tg__1['interfaces'].values()):
519 interface['local_mac'] = '00:00:00:00:00:{:2x}'.format(index)
521 # make a connection point ref with 3 points
522 self.s.topology["vld"][0]['vnfd-connection-point-ref'].append(
523 self.s.topology["vld"][0]['vnfd-connection-point-ref'][0])
525 with self.assertRaises(exceptions.IncorrectConfig) as raised:
526 self.s._resolve_topology()
528 self.assertIn('wrong endpoint count', str(raised.exception))
530 # make a connection point ref with 1 point
531 self.s.topology["vld"][0]['vnfd-connection-point-ref'] = \
532 self.s.topology["vld"][0]['vnfd-connection-point-ref'][:1]
534 with self.assertRaises(exceptions.IncorrectConfig) as raised:
535 self.s._resolve_topology()
537 self.assertIn('wrong endpoint count', str(raised.exception))
540 tgen = mock.Mock(autospec=GenericTrafficGen)
541 tgen.traffic_finished = True
542 verified_dict = {"verified": True}
543 tgen.verify_traffic = lambda x: verified_dict
544 tgen.name = "tgen__1"
545 vnf = mock.Mock(autospec=GenericVNF)
546 vnf.runs_traffic = False
547 self.s.vnfs = [tgen, vnf]
548 self.s.traffic_profile = mock.Mock()
549 self.s.collector = mock.Mock(autospec=Collector)
550 self.s.collector.get_kpi = \
551 mock.Mock(return_value={tgen.name: verified_dict})
554 self.assertDictEqual(result, {tgen.name: verified_dict})
556 def test_setup(self):
557 with mock.patch("yardstick.ssh.SSH") as ssh:
558 ssh_mock = mock.Mock(autospec=ssh.SSH)
560 mock.Mock(return_value=(0, SYS_CLASS_NET + IP_ADDR_SHOW, ""))
561 ssh.from_node.return_value = ssh_mock
563 tgen = mock.Mock(autospec=GenericTrafficGen)
564 tgen.traffic_finished = True
565 verified_dict = {"verified": True}
566 tgen.verify_traffic = lambda x: verified_dict
567 tgen.terminate = mock.Mock(return_value=True)
568 tgen.name = "tgen__1"
569 tgen.run_traffic.return_value = 'tg_id'
570 vnf = mock.Mock(autospec=GenericVNF)
571 vnf.runs_traffic = False
572 vnf.terminate = mock.Mock(return_value=True)
573 self.s.vnfs = [tgen, vnf]
574 self.s.traffic_profile = mock.Mock()
575 self.s.collector = mock.Mock(autospec=Collector)
576 self.s.collector.get_kpi = \
577 mock.Mock(return_value={tgen.name: verified_dict})
578 self.s.map_topology_to_infrastructure = mock.Mock(return_value=0)
579 self.s.load_vnf_models = mock.Mock(return_value=self.s.vnfs)
580 self.s._fill_traffic_profile = \
581 mock.Mock(return_value=TRAFFIC_PROFILE)
583 def test_setup_exception(self):
584 with mock.patch("yardstick.ssh.SSH") as ssh:
585 ssh_mock = mock.Mock(autospec=ssh.SSH)
587 mock.Mock(return_value=(0, SYS_CLASS_NET + IP_ADDR_SHOW, ""))
588 ssh.from_node.return_value = ssh_mock
590 tgen = mock.Mock(autospec=GenericTrafficGen)
591 tgen.traffic_finished = True
592 verified_dict = {"verified": True}
593 tgen.verify_traffic = lambda x: verified_dict
594 tgen.terminate = mock.Mock(return_value=True)
595 tgen.name = "tgen__1"
596 vnf = mock.Mock(autospec=GenericVNF)
597 vnf.runs_traffic = False
598 vnf.instantiate.side_effect = RuntimeError(
599 "error during instantiate")
600 vnf.terminate = mock.Mock(return_value=True)
601 self.s.vnfs = [tgen, vnf]
602 self.s.traffic_profile = mock.Mock()
603 self.s.collector = mock.Mock(autospec=Collector)
604 self.s.collector.get_kpi = \
605 mock.Mock(return_value={tgen.name: verified_dict})
606 self.s.map_topology_to_infrastructure = mock.Mock(return_value=0)
607 self.s.load_vnf_models = mock.Mock(return_value=self.s.vnfs)
608 self.s._fill_traffic_profile = \
609 mock.Mock(return_value=TRAFFIC_PROFILE)
610 with self.assertRaises(RuntimeError):
613 def test__get_traffic_profile(self):
614 self.scenario_cfg["traffic_profile"] = \
615 self._get_file_abspath("ipv4_throughput_vpe.yaml")
616 self.assertIsNotNone(self.s._get_traffic_profile())
618 def test__get_traffic_profile_exception(self):
619 with mock.patch.dict(self.scenario_cfg, {'traffic_profile': ''}):
620 with self.assertRaises(IOError):
621 self.s._get_traffic_profile()
623 def test___get_traffic_imix_exception(self):
624 with mock.patch.dict(self.scenario_cfg["traffic_options"], {'imix': ''}):
625 self.assertEqual({'imix': {'64B': 100}},
626 self.s._get_traffic_imix())
628 @mock.patch.object(base.TrafficProfile, 'get')
629 @mock.patch.object(vnfdgen, 'generate_vnfd')
630 def test__fill_traffic_profile(self, mock_generate, mock_tprofile_get):
631 fake_tprofile = mock.Mock()
632 fake_vnfd = mock.Mock()
633 with mock.patch.object(self.s, '_get_traffic_profile',
634 return_value=fake_tprofile) as mock_get_tp:
635 mock_generate.return_value = fake_vnfd
636 self.s._fill_traffic_profile()
637 mock_get_tp.assert_called_once()
638 mock_generate.assert_called_once_with(
641 'extra_args': {'arg1': 'value1', 'arg2': 'value2'},
642 'flow': {'flow': {}},
643 'imix': {'imix': {'64B': 100}},
647 mock_tprofile_get.assert_called_once_with(fake_vnfd)
649 @mock.patch.object(utils, 'open_relative_file')
650 def test__get_topology(self, mock_open_path):
651 self.s.scenario_cfg['topology'] = 'fake_topology'
652 self.s.scenario_cfg['task_path'] = 'fake_path'
653 mock_open_path.side_effect = mock.mock_open(read_data='fake_data')
654 self.assertEqual('fake_data', self.s._get_topology())
655 mock_open_path.assert_called_once_with('fake_topology', 'fake_path')
657 @mock.patch.object(vnfdgen, 'generate_vnfd')
658 def test__render_topology(self, mock_generate):
659 fake_topology = 'fake_topology'
660 mock_generate.return_value = {'nsd:nsd-catalog': {'nsd': ['fake_nsd']}}
661 with mock.patch.object(self.s, '_get_topology',
662 return_value=fake_topology) as mock_get_topology:
663 self.s._render_topology()
664 mock_get_topology.assert_called_once()
666 mock_generate.assert_called_once_with(
668 {'extra_args': {'arg1': 'value1', 'arg2': 'value2'}}
670 self.assertEqual(self.s.topology, 'fake_nsd')
672 def test_get_mq_ids(self):
673 self.assertEqual(self.s._mq_ids, self.s.get_mq_ids())
675 def test_teardown(self):
676 vnf = mock.Mock(autospec=GenericVNF)
677 vnf.terminate = mock.Mock(return_value=True)
680 self.s.traffic_profile = mock.Mock()
681 self.s.collector = mock.Mock(autospec=Collector)
682 self.s.collector.stop = \
683 mock.Mock(return_value=True)
684 self.assertIsNone(self.s.teardown())
686 def test_teardown_exception(self):
687 vnf = mock.Mock(autospec=GenericVNF)
688 vnf.terminate = mock.Mock(
689 side_effect=RuntimeError("error duing terminate"))
692 self.s.traffic_profile = mock.Mock()
693 self.s.collector = mock.Mock(autospec=Collector)
694 self.s.collector.stop = \
695 mock.Mock(return_value=True)
696 with self.assertRaises(RuntimeError):