1 # Copyright (c) 2019 Viosoft 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.
16 from multiprocessing import Process
19 from trex_stl_lib.trex_stl_exceptions import STLError
21 from yardstick.benchmark.contexts import base as ctx_base
22 from yardstick.network_services.traffic_profile import base as tp_base
23 from yardstick.network_services.traffic_profile import rfc2544
24 from yardstick.network_services.vnf_generic.vnf import base, sample_vnf, \
26 from yardstick.tests.unit.network_services.vnf_generic.vnf.test_base import \
30 class TestTrexVppResourceHelper(unittest.TestCase):
32 "schema": "isb:traffic_profile:0.1",
34 "description": "Fixed traffic profile to run UDP traffic",
36 "traffic_type": "FixedTraffic",
37 "frame_rate": 100, # pps
43 def test_fmt_latency(self):
44 mock_setup_helper = mock.Mock()
45 vpp_rfc = tg_trex_vpp.TrexVppResourceHelper(mock_setup_helper)
46 self.assertEqual('10/90/489', vpp_rfc.fmt_latency(10, 90, 489))
48 def test_fmt_latency_error(self):
49 mock_setup_helper = mock.Mock()
50 vpp_rfc = tg_trex_vpp.TrexVppResourceHelper(mock_setup_helper)
51 self.assertEqual('-1/-1/-1', vpp_rfc.fmt_latency('err', 'err', 'err'))
53 def test_generate_samples(self):
62 "rx_bps": 104339032.0,
63 "rx_bps_L1": 136944984.0,
65 "rx_util": 1.36944984,
66 "tx_bps": 134126008.0,
67 "tx_bps_L1": 176040392.0,
69 "tx_util": 1.7604039200000001
78 "rx_bps": 134119648.0,
79 "rx_bps_L1": 176032032.0,
81 "rx_util": 1.76032032,
82 "tx_bps": 104338192.0,
83 "tx_bps_L1": 136943872.0,
207 "rx_bps": 238458672.0,
208 "rx_cpu_util": 4.751e-05,
211 "tx_bps": 238464208.0,
277 "rx_bps": 238458680.0,
278 "rx_bps_L1": 312977016.0,
280 "rx_util": 3.1297701599999996,
281 "tx_bps": 238464200.0,
282 "tx_bps_L1": 312984264.0,
284 "tx_util": 3.12984264
289 "in_packets": 867955,
293 "max_latency": 135.0,
297 "out_packets": 867964,
298 "rx_throughput_bps": 104339032.0,
299 "rx_throughput_fps": 203787.2,
300 "tx_throughput_bps": 134126008.0,
301 "tx_throughput_fps": 261964.9
304 "in_packets": 867964,
307 "avg_latency": 63.375,
308 "max_latency": 123.0,
312 "out_packets": 867955,
313 "rx_throughput_bps": 134119648.0,
314 "rx_throughput_fps": 261952.4,
315 "tx_throughput_bps": 104338192.0,
316 "tx_throughput_fps": 203785.5
319 mock_setup_helper = mock.Mock()
320 vpp_rfc = tg_trex_vpp.TrexVppResourceHelper(mock_setup_helper)
321 vpp_rfc.vnfd_helper = base.VnfdHelper(TestTrexTrafficGenVpp.VNFD_0)
322 port_pg_id = rfc2544.PortPgIDMap()
323 port_pg_id.add_port(1)
324 port_pg_id.increase_pg_id()
325 port_pg_id.add_port(0)
326 port_pg_id.increase_pg_id()
327 self.assertEqual(expected,
328 vpp_rfc.generate_samples(stats, [0, 1], port_pg_id,
331 def test_generate_samples_error(self):
340 "rx_bps": 104339032.0,
341 "rx_bps_L1": 136944984.0,
343 "rx_util": 1.36944984,
344 "tx_bps": 134126008.0,
345 "tx_bps_L1": 176040392.0,
347 "tx_util": 1.7604039200000001
356 "rx_bps": 134119648.0,
357 "rx_bps_L1": 176032032.0,
359 "rx_util": 1.76032032,
360 "tx_bps": 104338192.0,
361 "tx_bps_L1": 136943872.0,
363 "tx_util": 1.36943872
485 "rx_bps": 238458672.0,
486 "rx_cpu_util": 4.751e-05,
489 "tx_bps": 238464208.0,
555 "rx_bps": 238458680.0,
556 "rx_bps_L1": 312977016.0,
558 "rx_util": 3.1297701599999996,
559 "tx_bps": 238464200.0,
560 "tx_bps_L1": 312984264.0,
562 "tx_util": 3.12984264
565 expected = {'xe0': {'in_packets': 867955,
566 'latency': {2: {'avg_latency': 74.0,
567 'max_latency': 135.0,
568 'min_latency': 60.0}},
569 'out_packets': 867964,
570 'rx_throughput_bps': 104339032.0,
571 'rx_throughput_fps': 203787.2,
572 'tx_throughput_bps': 134126008.0,
573 'tx_throughput_fps': 261964.9},
574 'xe1': {'in_packets': 867964,
575 'latency': {1: {'avg_latency': -1.0,
577 'min_latency': -1.0}},
578 'out_packets': 867955,
579 'rx_throughput_bps': 134119648.0,
580 'rx_throughput_fps': 261952.4,
581 'tx_throughput_bps': 104338192.0,
582 'tx_throughput_fps': 203785.5}}
583 mock_setup_helper = mock.Mock()
584 vpp_rfc = tg_trex_vpp.TrexVppResourceHelper(mock_setup_helper)
585 vpp_rfc.vnfd_helper = base.VnfdHelper(TestTrexTrafficGenVpp.VNFD_0)
586 vpp_rfc.get_stats = mock.Mock()
587 vpp_rfc.get_stats.return_value = stats
588 port_pg_id = rfc2544.PortPgIDMap()
589 port_pg_id.add_port(1)
590 port_pg_id.increase_pg_id()
591 port_pg_id.add_port(0)
592 port_pg_id.increase_pg_id()
593 self.assertEqual(expected,
594 vpp_rfc.generate_samples(stats=None, ports=[0, 1],
595 port_pg_id=port_pg_id,
598 def test__run_traffic_once(self):
599 mock_setup_helper = mock.Mock()
600 mock_traffic_profile = mock.Mock()
601 vpp_rfc = tg_trex_vpp.TrexVppResourceHelper(mock_setup_helper)
602 vpp_rfc.TRANSIENT_PERIOD = 0
603 vpp_rfc.rfc2544_helper = mock.Mock()
605 self.assertTrue(vpp_rfc._run_traffic_once(mock_traffic_profile))
606 mock_traffic_profile.execute_traffic.assert_called_once_with(vpp_rfc)
608 def test_run_traffic(self):
609 mock_traffic_profile = mock.Mock(autospec=tp_base.TrafficProfile)
610 mock_traffic_profile.get_traffic_definition.return_value = "64"
611 mock_traffic_profile.params = self.TRAFFIC_PROFILE
612 mock_setup_helper = mock.Mock()
613 vpp_rfc = tg_trex_vpp.TrexVppResourceHelper(mock_setup_helper)
614 vpp_rfc.ssh_helper = mock.Mock()
615 vpp_rfc.ssh_helper.run = mock.Mock()
616 vpp_rfc._traffic_runner = mock.Mock(return_value=0)
617 vpp_rfc._build_ports = mock.Mock()
618 vpp_rfc._connect = mock.Mock()
619 vpp_rfc.run_traffic(mock_traffic_profile)
621 def test_send_traffic_on_tg(self):
630 "rx_bps": 104339032.0,
631 "rx_bps_L1": 136944984.0,
633 "rx_util": 1.36944984,
634 "tx_bps": 134126008.0,
635 "tx_bps_L1": 176040392.0,
637 "tx_util": 1.7604039200000001
646 "rx_bps": 134119648.0,
647 "rx_bps_L1": 176032032.0,
649 "rx_util": 1.76032032,
650 "tx_bps": 104338192.0,
651 "tx_bps_L1": 136943872.0,
653 "tx_util": 1.36943872
775 "rx_bps": 238458672.0,
776 "rx_cpu_util": 4.751e-05,
779 "tx_bps": 238464208.0,
845 "rx_bps": 238458680.0,
846 "rx_bps_L1": 312977016.0,
848 "rx_util": 3.1297701599999996,
849 "tx_bps": 238464200.0,
850 "tx_bps_L1": 312984264.0,
852 "tx_util": 3.12984264
855 mock_setup_helper = mock.Mock()
856 vpp_rfc = tg_trex_vpp.TrexVppResourceHelper(mock_setup_helper)
857 vpp_rfc.vnfd_helper = base.VnfdHelper(TestTrexTrafficGenVpp.VNFD_0)
858 vpp_rfc.client = mock.Mock()
859 vpp_rfc.client.get_warnings.return_value = 'get_warnings'
860 vpp_rfc.client.get_stats.return_value = stats
861 port_pg_id = rfc2544.PortPgIDMap()
862 port_pg_id.add_port(1)
863 port_pg_id.increase_pg_id()
864 port_pg_id.add_port(0)
865 port_pg_id.increase_pg_id()
866 self.assertEqual(stats,
867 vpp_rfc.send_traffic_on_tg([0, 1], port_pg_id, 30,
870 def test_send_traffic_on_tg_error(self):
871 mock_setup_helper = mock.Mock()
872 vpp_rfc = tg_trex_vpp.TrexVppResourceHelper(mock_setup_helper)
873 vpp_rfc.vnfd_helper = base.VnfdHelper(TestTrexTrafficGenVpp.VNFD_0)
874 vpp_rfc.client = mock.Mock()
875 vpp_rfc.client.get_warnings.return_value = 'get_warnings'
876 vpp_rfc.client.get_stats.side_effect = STLError('get_stats')
877 vpp_rfc.client.wait_on_traffic.side_effect = STLError(
879 port_pg_id = rfc2544.PortPgIDMap()
880 port_pg_id.add_port(1)
881 port_pg_id.increase_pg_id()
882 port_pg_id.add_port(0)
883 port_pg_id.increase_pg_id()
884 # with self.assertRaises(RuntimeError) as raised:
885 vpp_rfc.send_traffic_on_tg([0, 1], port_pg_id, 30, 10000, True)
886 # self.assertIn('TRex stateless runtime error', str(raised.exception))
889 class TestTrexTrafficGenVpp(unittest.TestCase):
895 "tx_throughput_mbps",
896 "rx_throughput_mbps",
904 "description": "TRex stateless traffic verifier",
905 "id": "TrexTrafficGenVpp",
910 "vdu-id": "trexgen-baremetal"
912 "name": "trexverifier",
913 "short-name": "trexverifier",
916 "description": "TRex stateless traffic verifier",
917 "external-interface": [
920 "virtual-interface": {
923 "dst_ip": "192.168.100.2",
924 "dst_mac": "90:e2:ba:7c:41:a8",
926 "local_ip": "192.168.100.1",
927 "local_mac": "90:e2:ba:7c:30:e8",
928 "netmask": "255.255.255.0",
930 "node_name": "tg__0",
931 "peer_ifname": "xe0",
934 "dst_ip": "192.168.100.1",
935 "dst_mac": "90:e2:ba:7c:30:e8",
937 "local_ip": "192.168.100.2",
938 "local_mac": "90:e2:ba:7c:41:a8",
939 "netmask": "255.255.255.0",
941 "node_name": "vnf__0",
942 "peer_ifname": "xe0",
943 "peer_name": "tg__0",
944 "vld_id": "uplink_0",
945 "vpci": "0000:ff:06.0"
947 "peer_name": "vnf__0",
948 "vld_id": "uplink_0",
949 "vpci": "0000:81:00.0"
951 "vnfd-connection-point-ref": "xe0"
955 "virtual-interface": {
958 "dst_ip": "192.168.101.2",
959 "dst_mac": "90:e2:ba:7c:41:a9",
961 "local_ip": "192.168.101.1",
962 "local_mac": "90:e2:ba:7c:30:e9",
963 "netmask": "255.255.255.0",
965 "node_name": "tg__0",
966 "peer_ifname": "xe0",
969 "dst_ip": "192.168.101.1",
970 "dst_mac": "90:e2:ba:7c:30:e9",
972 "local_ip": "192.168.101.2",
973 "local_mac": "90:e2:ba:7c:41:a9",
974 "netmask": "255.255.255.0",
976 "node_name": "vnf__1",
977 "peer_ifname": "xe1",
978 "peer_name": "tg__0",
979 "vld_id": "downlink_0",
980 "vpci": "0000:ff:06.0"
982 "peer_name": "vnf__1",
983 "vld_id": "downlink_0",
984 "vpci": "0000:81:00.1"
986 "vnfd-connection-point-ref": "xe1"
989 "id": "trexgen-baremetal",
990 "name": "trexgen-baremetal"
996 'vnfd:vnfd-catalog': {
1004 self._mock_ssh_helper = mock.patch.object(sample_vnf, 'VnfSshHelper')
1005 self.mock_ssh_helper = self._mock_ssh_helper.start()
1006 self.addCleanup(self._stop_mocks)
1008 def _stop_mocks(self):
1009 self._mock_ssh_helper.stop()
1011 def test___init__(self):
1012 trex_traffic_gen = tg_trex_vpp.TrexTrafficGenVpp(
1014 self.assertIsNotNone(
1015 trex_traffic_gen.resource_helper._terminated.value)
1017 def test__check_status(self):
1018 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
1019 trex_traffic_gen = tg_trex_vpp.TrexTrafficGenVpp('tg0', vnfd)
1020 trex_traffic_gen.ssh_helper = mock.MagicMock()
1021 trex_traffic_gen.resource_helper.ssh_helper = mock.MagicMock()
1022 trex_traffic_gen.resource_helper.ssh_helper.execute.return_value = 0, '', ''
1023 trex_traffic_gen.scenario_helper.scenario_cfg = {}
1024 self.assertEqual(0, trex_traffic_gen._check_status())
1026 def test__start_server(self):
1027 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
1028 trex_traffic_gen = tg_trex_vpp.TrexTrafficGenVpp('tg0', vnfd)
1029 trex_traffic_gen.ssh_helper = mock.MagicMock()
1030 trex_traffic_gen.resource_helper.ssh_helper = mock.MagicMock()
1031 trex_traffic_gen.scenario_helper.scenario_cfg = {}
1032 self.assertIsNone(trex_traffic_gen._start_server())
1034 @mock.patch.object(ctx_base.Context, 'get_physical_node_from_server',
1035 return_value='mock_node')
1036 def test_collect_kpi(self, *args):
1037 trex_traffic_gen = tg_trex_vpp.TrexTrafficGenVpp(
1039 trex_traffic_gen.scenario_helper.scenario_cfg = {
1040 'nodes': {trex_traffic_gen.name: "mock"}
1043 'physical_node': 'mock_node',
1044 'collect_stats': {},
1046 self.assertEqual(trex_traffic_gen.collect_kpi(), expected)
1048 @mock.patch.object(ctx_base.Context, 'get_context_from_server',
1049 return_value='fake_context')
1050 def test_instantiate(self, *args):
1051 trex_traffic_gen = tg_trex_vpp.TrexTrafficGenVpp(
1053 trex_traffic_gen._start_server = mock.Mock(return_value=0)
1054 trex_traffic_gen.resource_helper = mock.MagicMock()
1055 trex_traffic_gen.setup_helper.setup_vnf_environment = mock.MagicMock()
1058 "tc": "tc_baremetal_rfc2544_ipv4_1flow_64B",
1059 "topology": 'nsb_test_case.yaml',
1064 'allowed_drop_rate': '0.8 - 1',
1067 'rules': 'acl_1rule.yaml',
1071 'worker_config': '1C/1T',
1077 tg_trex_vpp.WAIT_TIME = 3
1078 scenario_cfg.update({"nodes": {"tg0": {}, "vnf0": {}}})
1079 self.assertIsNone(trex_traffic_gen.instantiate(scenario_cfg, {}))
1081 @mock.patch.object(ctx_base.Context, 'get_context_from_server',
1082 return_value='fake_context')
1083 def test_instantiate_error(self, *args):
1084 trex_traffic_gen = tg_trex_vpp.TrexTrafficGenVpp(
1086 trex_traffic_gen.resource_helper = mock.MagicMock()
1087 trex_traffic_gen.setup_helper.setup_vnf_environment = mock.MagicMock()
1089 "tc": "tc_baremetal_rfc2544_ipv4_1flow_64B",
1094 "topology": 'nsb_test_case.yaml',
1099 'allowed_drop_rate': '0.8 - 1',
1102 'rules': 'acl_1rule.yaml',
1106 'worker_config': '1C/1T',
1107 'worker_threads': 1,
1112 trex_traffic_gen.instantiate(scenario_cfg, {})
1115 'yardstick.network_services.vnf_generic.vnf.sample_vnf.VnfSshHelper')
1116 def test_wait_for_instantiate(self, ssh, *args):
1119 mock_process = mock.Mock(autospec=Process)
1120 mock_process.is_alive.return_value = True
1121 mock_process.exitcode = 432
1123 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
1124 trex_traffic_gen = tg_trex_vpp.TrexTrafficGenVpp('tg0', vnfd)
1125 trex_traffic_gen.ssh_helper = mock.MagicMock()
1126 trex_traffic_gen.resource_helper.ssh_helper = mock.MagicMock()
1127 trex_traffic_gen.resource_helper.ssh_helper.execute.return_value = 0, '', ''
1128 trex_traffic_gen.scenario_helper.scenario_cfg = {}
1129 trex_traffic_gen._tg_process = mock_process
1130 self.assertEqual(432, trex_traffic_gen.wait_for_instantiate())