diff options
Diffstat (limited to 'tests/unit')
15 files changed, 647 insertions, 220 deletions
diff --git a/tests/unit/__init__.py b/tests/unit/__init__.py index a468b272b..5935abbac 100644 --- a/tests/unit/__init__.py +++ b/tests/unit/__init__.py @@ -12,65 +12,9 @@ # See the License for the specific language governing permissions and
# limitations under the License.
-from __future__ import absolute_import
-import mock
+from yardstick import tests
-STL_MOCKS = {
- 'trex_stl_lib': mock.MagicMock(),
- 'trex_stl_lib.base64': mock.MagicMock(),
- 'trex_stl_lib.binascii': mock.MagicMock(),
- 'trex_stl_lib.collections': mock.MagicMock(),
- 'trex_stl_lib.copy': mock.MagicMock(),
- 'trex_stl_lib.datetime': mock.MagicMock(),
- 'trex_stl_lib.functools': mock.MagicMock(),
- 'trex_stl_lib.imp': mock.MagicMock(),
- 'trex_stl_lib.inspect': mock.MagicMock(),
- 'trex_stl_lib.json': mock.MagicMock(),
- 'trex_stl_lib.linecache': mock.MagicMock(),
- 'trex_stl_lib.math': mock.MagicMock(),
- 'trex_stl_lib.os': mock.MagicMock(),
- 'trex_stl_lib.platform': mock.MagicMock(),
- 'trex_stl_lib.pprint': mock.MagicMock(),
- 'trex_stl_lib.random': mock.MagicMock(),
- 'trex_stl_lib.re': mock.MagicMock(),
- 'trex_stl_lib.scapy': mock.MagicMock(),
- 'trex_stl_lib.socket': mock.MagicMock(),
- 'trex_stl_lib.string': mock.MagicMock(),
- 'trex_stl_lib.struct': mock.MagicMock(),
- 'trex_stl_lib.sys': mock.MagicMock(),
- 'trex_stl_lib.threading': mock.MagicMock(),
- 'trex_stl_lib.time': mock.MagicMock(),
- 'trex_stl_lib.traceback': mock.MagicMock(),
- 'trex_stl_lib.trex_stl_async_client': mock.MagicMock(),
- 'trex_stl_lib.trex_stl_client': mock.MagicMock(),
- 'trex_stl_lib.trex_stl_exceptions': mock.MagicMock(),
- 'trex_stl_lib.trex_stl_ext': mock.MagicMock(),
- 'trex_stl_lib.trex_stl_jsonrpc_client': mock.MagicMock(),
- 'trex_stl_lib.trex_stl_packet_builder_interface': mock.MagicMock(),
- 'trex_stl_lib.trex_stl_packet_builder_scapy': mock.MagicMock(),
- 'trex_stl_lib.trex_stl_port': mock.MagicMock(),
- 'trex_stl_lib.trex_stl_stats': mock.MagicMock(),
- 'trex_stl_lib.trex_stl_streams': mock.MagicMock(),
- 'trex_stl_lib.trex_stl_types': mock.MagicMock(),
- 'trex_stl_lib.types': mock.MagicMock(),
- 'trex_stl_lib.utils': mock.MagicMock(),
- 'trex_stl_lib.utils.argparse': mock.MagicMock(),
- 'trex_stl_lib.utils.collections': mock.MagicMock(),
- 'trex_stl_lib.utils.common': mock.MagicMock(),
- 'trex_stl_lib.utils.json': mock.MagicMock(),
- 'trex_stl_lib.utils.os': mock.MagicMock(),
- 'trex_stl_lib.utils.parsing_opts': mock.MagicMock(),
- 'trex_stl_lib.utils.pwd': mock.MagicMock(),
- 'trex_stl_lib.utils.random': mock.MagicMock(),
- 'trex_stl_lib.utils.re': mock.MagicMock(),
- 'trex_stl_lib.utils.string': mock.MagicMock(),
- 'trex_stl_lib.utils.sys': mock.MagicMock(),
- 'trex_stl_lib.utils.text_opts': mock.MagicMock(),
- 'trex_stl_lib.utils.text_tables': mock.MagicMock(),
- 'trex_stl_lib.utils.texttable': mock.MagicMock(),
- 'trex_stl_lib.warnings': mock.MagicMock(),
- 'trex_stl_lib.yaml': mock.MagicMock(),
- 'trex_stl_lib.zlib': mock.MagicMock(),
- 'trex_stl_lib.zmq': mock.MagicMock(),
-}
+# NOTE(ralonsoh): to be removed. Replace all occurrences of
+# tests.unit.STL_MOCKS with yardstick.tests.STL_MOCKS
+STL_MOCKS = tests.STL_MOCKS
diff --git a/tests/unit/network_services/helpers/test_dpdkbindnic_helper.py b/tests/unit/network_services/helpers/test_dpdkbindnic_helper.py index e30aee854..cc980640c 100644 --- a/tests/unit/network_services/helpers/test_dpdkbindnic_helper.py +++ b/tests/unit/network_services/helpers/test_dpdkbindnic_helper.py @@ -16,6 +16,14 @@ import mock import unittest + +import os + +from yardstick.error import IncorrectConfig, SSHError +from yardstick.error import IncorrectNodeSetup +from yardstick.error import IncorrectSetup +from yardstick.network_services.helpers.dpdkbindnic_helper import DpdkInterface +from yardstick.network_services.helpers.dpdkbindnic_helper import DpdkNode from yardstick.network_services.helpers.dpdkbindnic_helper import DpdkBindHelper from yardstick.network_services.helpers.dpdkbindnic_helper import DpdkBindHelperException from yardstick.network_services.helpers.dpdkbindnic_helper import NETWORK_KERNEL @@ -26,7 +34,269 @@ from yardstick.network_services.helpers.dpdkbindnic_helper import NETWORK_OTHER from yardstick.network_services.helpers.dpdkbindnic_helper import CRYPTO_OTHER +NAME = "tg_0" + + +class TestDpdkInterface(unittest.TestCase): + + SAMPLE_NETDEVS = { + 'enp11s0': { + 'address': '0a:de:ad:be:ef:f5', + 'device': '0x1533', + 'driver': 'igb', + 'ifindex': '2', + 'interface_name': 'enp11s0', + 'operstate': 'down', + 'pci_bus_id': '0000:0b:00.0', + 'subsystem_device': '0x1533', + 'subsystem_vendor': '0x15d9', + 'vendor': '0x8086' + }, + 'lan': { + 'address': '0a:de:ad:be:ef:f4', + 'device': '0x153a', + 'driver': 'e1000e', + 'ifindex': '3', + 'interface_name': 'lan', + 'operstate': 'up', + 'pci_bus_id': '0000:00:19.0', + 'subsystem_device': '0x153a', + 'subsystem_vendor': '0x15d9', + 'vendor': '0x8086' + } + } + + SAMPLE_VM_NETDEVS = { + 'eth1': { + 'address': 'fa:de:ad:be:ef:5b', + 'device': '0x0001', + 'driver': 'virtio_net', + 'ifindex': '3', + 'interface_name': 'eth1', + 'operstate': 'down', + 'pci_bus_id': '0000:00:04.0', + 'vendor': '0x1af4' + } + } + + def test_parse_netdev_info(self): + output = """\ +/sys/devices/pci0000:00/0000:00:1c.3/0000:0b:00.0/net/enp11s0/ifindex:2 +/sys/devices/pci0000:00/0000:00:1c.3/0000:0b:00.0/net/enp11s0/address:0a:de:ad:be:ef:f5 +/sys/devices/pci0000:00/0000:00:1c.3/0000:0b:00.0/net/enp11s0/operstate:down +/sys/devices/pci0000:00/0000:00:1c.3/0000:0b:00.0/net/enp11s0/device/vendor:0x8086 +/sys/devices/pci0000:00/0000:00:1c.3/0000:0b:00.0/net/enp11s0/device/device:0x1533 +/sys/devices/pci0000:00/0000:00:1c.3/0000:0b:00.0/net/enp11s0/device/subsystem_vendor:0x15d9 +/sys/devices/pci0000:00/0000:00:1c.3/0000:0b:00.0/net/enp11s0/device/subsystem_device:0x1533 +/sys/devices/pci0000:00/0000:00:1c.3/0000:0b:00.0/net/enp11s0/driver:igb +/sys/devices/pci0000:00/0000:00:1c.3/0000:0b:00.0/net/enp11s0/pci_bus_id:0000:0b:00.0 +/sys/devices/pci0000:00/0000:00:19.0/net/lan/ifindex:3 +/sys/devices/pci0000:00/0000:00:19.0/net/lan/address:0a:de:ad:be:ef:f4 +/sys/devices/pci0000:00/0000:00:19.0/net/lan/operstate:up +/sys/devices/pci0000:00/0000:00:19.0/net/lan/device/vendor:0x8086 +/sys/devices/pci0000:00/0000:00:19.0/net/lan/device/device:0x153a +/sys/devices/pci0000:00/0000:00:19.0/net/lan/device/subsystem_vendor:0x15d9 +/sys/devices/pci0000:00/0000:00:19.0/net/lan/device/subsystem_device:0x153a +/sys/devices/pci0000:00/0000:00:19.0/net/lan/driver:e1000e +/sys/devices/pci0000:00/0000:00:19.0/net/lan/pci_bus_id:0000:00:19.0 +""" + res = DpdkBindHelper.parse_netdev_info(output) + self.assertDictEqual(res, self.SAMPLE_NETDEVS) + + def test_parse_netdev_info_virtio(self): + output = """\ +/sys/devices/pci0000:00/0000:00:04.0/virtio1/net/eth1/ifindex:3 +/sys/devices/pci0000:00/0000:00:04.0/virtio1/net/eth1/address:fa:de:ad:be:ef:5b +/sys/devices/pci0000:00/0000:00:04.0/virtio1/net/eth1/operstate:down +/sys/devices/pci0000:00/0000:00:04.0/virtio1/net/eth1/device/vendor:0x1af4 +/sys/devices/pci0000:00/0000:00:04.0/virtio1/net/eth1/device/device:0x0001 +/sys/devices/pci0000:00/0000:00:04.0/virtio1/net/eth1/driver:virtio_net +""" + res = DpdkBindHelper.parse_netdev_info(output) + self.assertDictEqual(res, self.SAMPLE_VM_NETDEVS) + + def test_probe_missing_values(self): + mock_dpdk_node = mock.Mock() + mock_dpdk_node.netdevs = self.SAMPLE_NETDEVS.copy() + + interface = {'local_mac': '0a:de:ad:be:ef:f5'} + dpdk_intf = DpdkInterface(mock_dpdk_node, interface) + + dpdk_intf.probe_missing_values() + self.assertEqual(interface['vpci'], '0000:0b:00.0') + + interface['local_mac'] = '0a:de:ad:be:ef:f4' + dpdk_intf.probe_missing_values() + self.assertEqual(interface['vpci'], '0000:00:19.0') + + def test_probe_missing_values_no_update(self): + mock_dpdk_node = mock.Mock() + mock_dpdk_node.netdevs = self.SAMPLE_NETDEVS.copy() + del mock_dpdk_node.netdevs['enp11s0']['driver'] + del mock_dpdk_node.netdevs['lan']['driver'] + + interface = {'local_mac': '0a:de:ad:be:ef:f5'} + dpdk_intf = DpdkInterface(mock_dpdk_node, interface) + + dpdk_intf.probe_missing_values() + self.assertNotIn('vpci', interface) + self.assertNotIn('driver', interface) + + def test_probe_missing_values_negative(self): + mock_dpdk_node = mock.Mock() + mock_dpdk_node.netdevs.values.side_effect = IncorrectNodeSetup + + interface = {'local_mac': '0a:de:ad:be:ef:f5'} + dpdk_intf = DpdkInterface(mock_dpdk_node, interface) + + with self.assertRaises(IncorrectConfig): + dpdk_intf.probe_missing_values() + + +class TestDpdkNode(unittest.TestCase): + + INTERFACES = [ + {'name': 'name1', + 'virtual-interface': { + 'local_mac': 404, + 'vpci': 'pci10', + }}, + {'name': 'name2', + 'virtual-interface': { + 'local_mac': 404, + 'vpci': 'pci2', + }}, + {'name': 'name3', + 'virtual-interface': { + 'local_mac': 404, + 'vpci': 'some-pci1', + }}, + ] + + def test_probe_dpdk_drivers(self): + mock_ssh_helper = mock.Mock() + mock_ssh_helper.execute.return_value = 0, '', '' + + interfaces = [ + {'name': 'name1', + 'virtual-interface': { + 'local_mac': 404, + 'vpci': 'pci10', + }}, + {'name': 'name2', + 'virtual-interface': { + 'local_mac': 404, + 'vpci': 'pci2', + }}, + {'name': 'name3', + 'virtual-interface': { + 'local_mac': 404, + 'vpci': 'some-pci1', + }}, + ] + + dpdk_node = DpdkNode(NAME, interfaces, mock_ssh_helper) + dpdk_helper = dpdk_node.dpdk_helper + + dpdk_helper.probe_real_kernel_drivers = mock.Mock() + dpdk_helper.real_kernel_interface_driver_map = { + 'pci1': 'driver1', + 'pci2': 'driver2', + 'pci3': 'driver3', + 'pci4': 'driver1', + 'pci6': 'driver3', + } + + dpdk_node._probe_dpdk_drivers() + self.assertNotIn('driver', interfaces[0]['virtual-interface']) + self.assertEqual(interfaces[1]['virtual-interface']['driver'], 'driver2') + self.assertEqual(interfaces[2]['virtual-interface']['driver'], 'driver1') + + def test_check(self): + def update(): + if not mock_force_rebind.called: + raise IncorrectConfig + + interfaces[0]['virtual-interface'].update({ + 'vpci': '0000:01:02.1', + 'local_ip': '10.20.30.40', + 'netmask': '255.255.0.0', + 'driver': 'ixgbe', + }) + + mock_ssh_helper = mock.Mock() + mock_ssh_helper.execute.return_value = 0, '', '' + + interfaces = [ + {'name': 'name1', + 'virtual-interface': { + 'local_mac': 404, + }}, + ] + + dpdk_node = DpdkNode(NAME, interfaces, mock_ssh_helper) + dpdk_node._probe_missing_values = mock_probe_missing = mock.Mock(side_effect=update) + dpdk_node._force_rebind = mock_force_rebind = mock.Mock() + + self.assertIsNone(dpdk_node.check()) + self.assertEqual(mock_probe_missing.call_count, 2) + + @mock.patch('yardstick.network_services.helpers.dpdkbindnic_helper.DpdkInterface') + def test_check_negative(self, mock_intf_type): + mock_ssh_helper = mock.Mock() + mock_ssh_helper.execute.return_value = 0, '', '' + + mock_intf_type().check.side_effect = SSHError + + dpdk_node = DpdkNode(NAME, self.INTERFACES, mock_ssh_helper) + + with self.assertRaises(IncorrectSetup): + dpdk_node.check() + + def test_probe_netdevs(self): + mock_ssh_helper = mock.Mock() + mock_ssh_helper.execute.return_value = 0, '', '' + + expected = {'key1': 500, 'key2': 'hello world'} + update = {'key1': 1000, 'key3': []} + + dpdk_node = DpdkNode(NAME, self.INTERFACES, mock_ssh_helper) + dpdk_helper = dpdk_node.dpdk_helper + dpdk_helper.find_net_devices = mock.Mock(side_effect=[expected, update]) + + self.assertDictEqual(dpdk_node.netdevs, {}) + dpdk_node._probe_netdevs() + self.assertDictEqual(dpdk_node.netdevs, expected) + + expected = {'key1': 1000, 'key2': 'hello world', 'key3': []} + dpdk_node._probe_netdevs() + self.assertDictEqual(dpdk_node.netdevs, expected) + + def test_probe_netdevs_setup_negative(self): + mock_ssh_helper = mock.Mock() + mock_ssh_helper.execute.return_value = 0, '', '' + + dpdk_node = DpdkNode(NAME, self.INTERFACES, mock_ssh_helper) + dpdk_helper = dpdk_node.dpdk_helper + dpdk_helper.find_net_devices = mock.Mock(side_effect=DpdkBindHelperException) + + with self.assertRaises(DpdkBindHelperException): + dpdk_node._probe_netdevs() + + def test_force_rebind(self): + mock_ssh_helper = mock.Mock() + mock_ssh_helper.execute.return_value = 0, '', '' + + dpdk_node = DpdkNode(NAME, self.INTERFACES, mock_ssh_helper) + dpdk_helper = dpdk_node.dpdk_helper + dpdk_helper.force_dpdk_rebind = mock_helper_func = mock.Mock() + + dpdk_node._force_rebind() + self.assertEqual(mock_helper_func.call_count, 1) + + class TestDpdkBindHelper(unittest.TestCase): + bin_path = "/opt/nsb_bin" EXAMPLE_OUTPUT = """ Network devices using DPDK-compatible driver @@ -111,13 +381,15 @@ Other crypto devices def test___init__(self): conn = mock.Mock() conn.provision_tool = mock.Mock(return_value='path_to_tool') + conn.join_bin_path.return_value = os.path.join(self.bin_path, DpdkBindHelper.DPDK_DEVBIND) dpdk_bind_helper = DpdkBindHelper(conn) self.assertEqual(conn, dpdk_bind_helper.ssh_helper) self.assertEqual(self.CLEAN_STATUS, dpdk_bind_helper.dpdk_status) self.assertIsNone(dpdk_bind_helper.status_nic_row_re) - self.assertIsNone(dpdk_bind_helper._dpdk_devbind) + self.assertEqual(dpdk_bind_helper.dpdk_devbind, + os.path.join(self.bin_path, dpdk_bind_helper.DPDK_DEVBIND)) self.assertIsNone(dpdk_bind_helper._status_cmd_attr) def test__dpdk_execute(self): @@ -125,8 +397,7 @@ Other crypto devices conn.execute = mock.Mock(return_value=(0, 'output', 'error')) conn.provision_tool = mock.Mock(return_value='tool_path') dpdk_bind_helper = DpdkBindHelper(conn) - self.assertEqual((0, 'output', 'error'), - dpdk_bind_helper._dpdk_execute('command')) + self.assertEqual((0, 'output', 'error'), dpdk_bind_helper._dpdk_execute('command')) def test__dpdk_execute_failure(self): conn = mock.Mock() @@ -141,7 +412,7 @@ Other crypto devices dpdk_bind_helper = DpdkBindHelper(conn) - dpdk_bind_helper._addline(NETWORK_KERNEL, self.ONE_INPUT_LINE) + dpdk_bind_helper._add_line(NETWORK_KERNEL, self.ONE_INPUT_LINE) self.assertIsNotNone(dpdk_bind_helper.dpdk_status) self.assertEqual(self.ONE_INPUT_LINE_PARSED, dpdk_bind_helper.dpdk_status[NETWORK_KERNEL]) @@ -161,11 +432,35 @@ Other crypto devices dpdk_bind_helper = DpdkBindHelper(conn) - dpdk_bind_helper.parse_dpdk_status_output(self.EXAMPLE_OUTPUT) + dpdk_bind_helper._parse_dpdk_status_output(self.EXAMPLE_OUTPUT) self.maxDiff = None self.assertEqual(self.PARSED_EXAMPLE, dpdk_bind_helper.dpdk_status) + def test_kernel_bound_pci_addresses(self): + mock_ssh_helper = mock.Mock() + mock_ssh_helper.execute.return_value = 0, '', '' + + expected = ['a', 'b', 3] + + dpdk_helper = DpdkBindHelper(mock_ssh_helper) + dpdk_helper.dpdk_status = { + NETWORK_DPDK: [{'vpci': 4}, {'vpci': 5}, {'vpci': 'g'}], + NETWORK_KERNEL: [{'vpci': 'a'}, {'vpci': 'b'}, {'vpci': 3}], + CRYPTO_DPDK: [], + } + + result = dpdk_helper.kernel_bound_pci_addresses + self.assertEqual(result, expected) + + def test_find_net_devices_negative(self): + mock_ssh_helper = mock.Mock() + mock_ssh_helper.execute.return_value = 1, 'error', 'debug' + + dpdk_helper = DpdkBindHelper(mock_ssh_helper) + + self.assertDictEqual(dpdk_helper.find_net_devices(), {}) + def test_read_status(self): conn = mock.Mock() conn.execute = mock.Mock(return_value=(0, self.EXAMPLE_OUTPUT, '')) @@ -180,7 +475,7 @@ Other crypto devices dpdk_bind_helper = DpdkBindHelper(conn) - dpdk_bind_helper.parse_dpdk_status_output(self.EXAMPLE_OUTPUT) + dpdk_bind_helper._parse_dpdk_status_output(self.EXAMPLE_OUTPUT) self.assertEqual(['0000:00:04.0', '0000:00:05.0'], dpdk_bind_helper._get_bound_pci_addresses(NETWORK_DPDK)) @@ -192,18 +487,18 @@ Other crypto devices dpdk_bind_helper = DpdkBindHelper(conn) - dpdk_bind_helper.parse_dpdk_status_output(self.EXAMPLE_OUTPUT) + dpdk_bind_helper._parse_dpdk_status_output(self.EXAMPLE_OUTPUT) self.assertEqual({'0000:00:04.0': 'igb_uio', - '0000:00:03.0': 'virtio-pci', - '0000:00:05.0': 'igb_uio', - }, - dpdk_bind_helper.interface_driver_map) + '0000:00:03.0': 'virtio-pci', + '0000:00:05.0': 'igb_uio', + }, + dpdk_bind_helper.interface_driver_map) def test_bind(self): conn = mock.Mock() conn.execute = mock.Mock(return_value=(0, '', '')) - conn.provision_tool = mock.Mock(return_value='/opt/nsb_bin/dpdk-devbind.py') + conn.join_bin_path.return_value = os.path.join(self.bin_path, DpdkBindHelper.DPDK_DEVBIND) dpdk_bind_helper = DpdkBindHelper(conn) dpdk_bind_helper.read_status = mock.Mock() @@ -217,7 +512,7 @@ Other crypto devices def test_bind_single_pci(self): conn = mock.Mock() conn.execute = mock.Mock(return_value=(0, '', '')) - conn.provision_tool = mock.Mock(return_value='/opt/nsb_bin/dpdk-devbind.py') + conn.join_bin_path.return_value = os.path.join(self.bin_path, DpdkBindHelper.DPDK_DEVBIND) dpdk_bind_helper = DpdkBindHelper(conn) dpdk_bind_helper.read_status = mock.Mock() @@ -257,3 +552,84 @@ Other crypto devices } self.assertDictEqual(expected, dpdk_bind_helper.used_drivers) + + def test_force_dpdk_rebind(self): + mock_ssh_helper = mock.Mock() + mock_ssh_helper.execute.return_value = 0, '', '' + + dpdk_helper = DpdkBindHelper(mock_ssh_helper, 'driver2') + dpdk_helper.dpdk_status = { + NETWORK_DPDK: [ + { + 'vpci': 'pci1', + }, + { + 'vpci': 'pci3', + }, + { + 'vpci': 'pci6', + }, + { + 'vpci': 'pci3', + }, + ] + } + dpdk_helper.real_kernel_interface_driver_map = { + 'pci1': 'real_driver1', + 'pci2': 'real_driver2', + 'pci3': 'real_driver1', + 'pci4': 'real_driver4', + 'pci6': 'real_driver6', + } + dpdk_helper.load_dpdk_driver = mock.Mock() + dpdk_helper.read_status = mock.Mock() + dpdk_helper.save_real_kernel_interface_driver_map = mock.Mock() + dpdk_helper.save_used_drivers = mock.Mock() + dpdk_helper.bind = mock_bind = mock.Mock() + + dpdk_helper.force_dpdk_rebind() + self.assertEqual(mock_bind.call_count, 2) + + def test_save_real_kernel_drivers(self): + mock_ssh_helper = mock.Mock() + mock_ssh_helper.execute.return_value = 0, '', '' + + dpdk_helper = DpdkBindHelper(mock_ssh_helper) + dpdk_helper.real_kernel_drivers = { + 'abc': '123', + } + dpdk_helper.real_kernel_interface_driver_map = { + 'abc': 'AAA', + 'def': 'DDD', + 'abs': 'AAA', + 'ghi': 'GGG', + } + + # save_used_drivers must be called before save_real_kernel_drivers can be + with self.assertRaises(AttributeError): + dpdk_helper.save_real_kernel_drivers() + + dpdk_helper.save_used_drivers() + + expected_used_drivers = { + 'AAA': ['abc', 'abs'], + 'DDD': ['def'], + 'GGG': ['ghi'], + } + dpdk_helper.save_real_kernel_drivers() + self.assertDictEqual(dpdk_helper.used_drivers, expected_used_drivers) + self.assertDictEqual(dpdk_helper.real_kernel_drivers, {}) + + def test_get_real_kernel_driver(self): + mock_ssh_helper = mock.Mock() + mock_ssh_helper.execute.side_effect = [ + (0, 'non-matching text', ''), + (0, 'pre Kernel modules: real_driver1', ''), + (0, 'before Ethernet middle Virtio network device after', ''), + ] + + dpdk_helper = DpdkBindHelper(mock_ssh_helper) + + self.assertIsNone(dpdk_helper.get_real_kernel_driver('abc')) + self.assertEqual(dpdk_helper.get_real_kernel_driver('abc'), 'real_driver1') + self.assertEqual(dpdk_helper.get_real_kernel_driver('abc'), DpdkBindHelper.VIRTIO_DRIVER) diff --git a/tests/unit/network_services/traffic_profile/test_base.py b/tests/unit/network_services/traffic_profile/test_base.py index 290610361..33f803286 100644 --- a/tests/unit/network_services/traffic_profile/test_base.py +++ b/tests/unit/network_services/traffic_profile/test_base.py @@ -13,16 +13,16 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -# -# Unittest for yardstick.network_services.traffic_profile.test_base +import sys -from __future__ import absolute_import -import unittest import mock +import unittest -from yardstick.network_services.traffic_profile.base import \ - TrafficProfile, DummyProfile +from yardstick.common import exceptions +from yardstick.network_services import traffic_profile as tprofile_package +from yardstick.network_services.traffic_profile import base +from yardstick import tests as y_tests class TestTrafficProfile(unittest.TestCase): @@ -43,20 +43,33 @@ class TestTrafficProfile(unittest.TestCase): return _mock def test___init__(self): - traffic_profile = TrafficProfile(self.TRAFFIC_PROFILE) + traffic_profile = base.TrafficProfile(self.TRAFFIC_PROFILE) self.assertEqual(self.TRAFFIC_PROFILE, traffic_profile.params) def test_execute(self): - traffic_profile = TrafficProfile(self.TRAFFIC_PROFILE) - self.assertRaises(NotImplementedError, traffic_profile.execute_traffic, {}) + traffic_profile = base.TrafficProfile(self.TRAFFIC_PROFILE) + self.assertRaises(NotImplementedError, + traffic_profile.execute_traffic, {}) + + def test_get_existing_traffic_profile(self): + traffic_profile_list = [ + 'RFC2544Profile', 'FixedProfile', 'TrafficProfileGenericHTTP', + 'IXIARFC2544Profile', 'ProxACLProfile', 'ProxBinSearchProfile', + 'ProxProfile', 'ProxRampProfile'] + with mock.patch.dict(sys.modules, y_tests.STL_MOCKS): + tprofile_package.register_modules() + + for tp in traffic_profile_list: + traffic_profile = base.TrafficProfile.get( + {'traffic_profile': {'traffic_type': tp}}) + self.assertEqual(tp, traffic_profile.__class__.__name__) - def test_get(self): - traffic_profile = TrafficProfile(self.TRAFFIC_PROFILE) - self.assertRaises(RuntimeError, traffic_profile.get, - self.TRAFFIC_PROFILE) + def test_get_non_existing_traffic_profile(self): + self.assertRaises(exceptions.TrafficProfileNotImplemented, + base.TrafficProfile.get, self.TRAFFIC_PROFILE) class TestDummyProfile(unittest.TestCase): def test_execute(self): - dummy_profile = DummyProfile(TrafficProfile) + dummy_profile = base.DummyProfile(base.TrafficProfile) self.assertIsNone(dummy_profile.execute({})) diff --git a/tests/unit/network_services/traffic_profile/test_ixia_rfc2544.py b/tests/unit/network_services/traffic_profile/test_ixia_rfc2544.py index f13945abf..f83d3341d 100644 --- a/tests/unit/network_services/traffic_profile/test_ixia_rfc2544.py +++ b/tests/unit/network_services/traffic_profile/test_ixia_rfc2544.py @@ -29,7 +29,7 @@ stl_patch = mock.patch.dict("sys.modules", STL_MOCKS) stl_patch.start() if stl_patch: - from yardstick.network_services.traffic_profile.traffic_profile \ + from yardstick.network_services.traffic_profile.trex_traffic_profile \ import TrexProfile from yardstick.network_services.traffic_profile.ixia_rfc2544 import \ IXIARFC2544Profile diff --git a/tests/unit/network_services/traffic_profile/test_prox_binsearch.py b/tests/unit/network_services/traffic_profile/test_prox_binsearch.py index c1f1c825b..1b4189b48 100644 --- a/tests/unit/network_services/traffic_profile/test_prox_binsearch.py +++ b/tests/unit/network_services/traffic_profile/test_prox_binsearch.py @@ -32,7 +32,7 @@ if stl_patch: class TestProxBinSearchProfile(unittest.TestCase): def test_execute_1(self): - def target(*args, **kwargs): + def target(*args, **_): runs.append(args[2]) if args[2] < 0 or args[2] > 100: raise RuntimeError(' '.join([str(args), str(runs)])) @@ -43,6 +43,8 @@ class TestProxBinSearchProfile(unittest.TestCase): tp_config = { 'traffic_profile': { 'packet_sizes': [200], + 'test_precision': 2.0, + 'tolerated_loss': 0.001, }, } @@ -61,11 +63,47 @@ class TestProxBinSearchProfile(unittest.TestCase): profile.execute_traffic(traffic_generator) self.assertEqual(round(profile.current_lower, 2), 74.69) - self.assertEqual(round(profile.current_upper, 2), 75.39) - self.assertEqual(len(runs), 8) + self.assertEqual(round(profile.current_upper, 2), 76.09) + self.assertEqual(len(runs), 7) + + # Result Samples inc theor_max + result_tuple = {"Result_Actual_throughput": 7.5e-07, + "Result_theor_max_throughput": 0.00012340000000000002, + "Result_pktSize": 200} + profile.queue.put.assert_called_with(result_tuple) + + success_result_tuple = {"Success_CurrentDropPackets": 0.5, + "Success_DropPackets": 0.5, + "Success_LatencyAvg": 5.3, + "Success_LatencyMax": 5.2, + "Success_LatencyMin": 5.1, + "Success_PktSize": 200, + "Success_RxThroughput": 7.5e-07, + "Success_Throughput": 7.5e-07, + "Success_TxThroughput": 0.00012340000000000002} + + calls = profile.queue.put(success_result_tuple) + profile.queue.put.assert_has_calls(calls) + + success_result_tuple2 = {"Success_CurrentDropPackets": 0.5, + "Success_DropPackets": 0.5, + "Success_LatencyAvg": 5.3, + "Success_LatencyMax": 5.2, + "Success_LatencyMin": 5.1, + "Success_PktSize": 200, + "Success_RxThroughput": 7.5e-07, + "Success_Throughput": 7.5e-07, + "Success_TxThroughput": 123.4, + "Success_can_be_lost": 409600, + "Success_drop_total": 20480, + "Success_rx_total": 4075520, + "Success_tx_total": 4096000} + + calls = profile.queue.put(success_result_tuple2) + profile.queue.put.assert_has_calls(calls) def test_execute_2(self): - def target(*args, **kwargs): + def target(*args, **_): runs.append(args[2]) if args[2] < 0 or args[2] > 100: raise RuntimeError(' '.join([str(args), str(runs)])) @@ -77,6 +115,7 @@ class TestProxBinSearchProfile(unittest.TestCase): 'traffic_profile': { 'packet_sizes': [200], 'test_precision': 2.0, + 'tolerated_loss': 0.001, }, } @@ -97,3 +136,50 @@ class TestProxBinSearchProfile(unittest.TestCase): self.assertEqual(round(profile.current_lower, 2), 24.06) self.assertEqual(round(profile.current_upper, 2), 25.47) self.assertEqual(len(runs), 7) + + def test_execute_3(self): + def target(*args, **_): + runs.append(args[2]) + if args[2] < 0 or args[2] > 100: + raise RuntimeError(' '.join([str(args), str(runs)])) + if args[2] > 75.0: + return fail_tuple, {} + return success_tuple, {} + + tp_config = { + 'traffic_profile': { + 'packet_sizes': [200], + 'test_precision': 2.0, + 'tolerated_loss': 0.001, + }, + } + + runs = [] + success_tuple = ProxTestDataTuple(10.0, 1, 2, 3, 4, [5.1, 5.2, 5.3], 995, 1000, 123.4) + fail_tuple = ProxTestDataTuple(10.0, 1, 2, 3, 4, [5.6, 5.7, 5.8], 850, 1000, 123.4) + + traffic_generator = mock.MagicMock() + + profile_helper = mock.MagicMock() + profile_helper.run_test = target + + profile = ProxBinSearchProfile(tp_config) + profile.init(mock.MagicMock()) + profile._profile_helper = profile_helper + + profile.upper_bound = 100.0 + profile.lower_bound = 99.0 + profile.execute_traffic(traffic_generator) + + + # Result Samples + result_tuple = {"Result_theor_max_throughput": 0, "Result_pktSize": 200} + profile.queue.put.assert_called_with(result_tuple) + + # Check for success_ tuple (None expected) + calls = profile.queue.put.mock_calls + for call in calls: + for call_detail in call[1]: + for k in call_detail: + if "Success_" in k: + self.assertRaises(AttributeError) diff --git a/tests/unit/network_services/traffic_profile/test_rfc2544.py b/tests/unit/network_services/traffic_profile/test_rfc2544.py index 221233710..fe563e9fd 100644 --- a/tests/unit/network_services/traffic_profile/test_rfc2544.py +++ b/tests/unit/network_services/traffic_profile/test_rfc2544.py @@ -29,7 +29,7 @@ stl_patch = mock.patch.dict("sys.modules", STL_MOCKS) stl_patch.start() if stl_patch: - from yardstick.network_services.traffic_profile.traffic_profile \ + from yardstick.network_services.traffic_profile.trex_traffic_profile \ import TrexProfile from yardstick.network_services.traffic_profile.rfc2544 import \ RFC2544Profile @@ -164,8 +164,6 @@ class TestRFC2544Profile(unittest.TestCase): "in_packets": 1000, "out_packets": 1002, } - tol_min = 0.0 - tolerance = 1.0 expected = { 'DropPercentage': 0.1996, 'RxThroughput': 33.333333333333336, @@ -208,8 +206,6 @@ class TestRFC2544Profile(unittest.TestCase): "tx_throughput_mbps": 10, "in_packets": 1000, "out_packets": 0} - tol_min = 0.0 - tolerance = 0.0 r_f_c2544_profile.throughput_max = 0 expected = { 'DropPercentage': 100.0, 'RxThroughput': 100 / 3.0, diff --git a/tests/unit/network_services/traffic_profile/test_traffic_profile.py b/tests/unit/network_services/traffic_profile/test_trex_traffic_profile.py index 37b9a08d0..c34b97531 100644 --- a/tests/unit/network_services/traffic_profile/test_traffic_profile.py +++ b/tests/unit/network_services/traffic_profile/test_trex_traffic_profile.py @@ -29,16 +29,16 @@ stl_patch.start() if stl_patch: from yardstick.network_services.traffic_profile.base import TrafficProfile - from yardstick.network_services.traffic_profile.traffic_profile import TrexProfile - from yardstick.network_services.traffic_profile.traffic_profile import SRC - from yardstick.network_services.traffic_profile.traffic_profile import DST - from yardstick.network_services.traffic_profile.traffic_profile import ETHERNET - from yardstick.network_services.traffic_profile.traffic_profile import IP - from yardstick.network_services.traffic_profile.traffic_profile import IPv6 - from yardstick.network_services.traffic_profile.traffic_profile import UDP - from yardstick.network_services.traffic_profile.traffic_profile import SRC_PORT - from yardstick.network_services.traffic_profile.traffic_profile import DST_PORT - from yardstick.network_services.traffic_profile.traffic_profile import TYPE_OF_SERVICE + from yardstick.network_services.traffic_profile.trex_traffic_profile import TrexProfile + from yardstick.network_services.traffic_profile.trex_traffic_profile import SRC + from yardstick.network_services.traffic_profile.trex_traffic_profile import DST + from yardstick.network_services.traffic_profile.trex_traffic_profile import ETHERNET + from yardstick.network_services.traffic_profile.trex_traffic_profile import IP + from yardstick.network_services.traffic_profile.trex_traffic_profile import IPv6 + from yardstick.network_services.traffic_profile.trex_traffic_profile import UDP + from yardstick.network_services.traffic_profile.trex_traffic_profile import SRC_PORT + from yardstick.network_services.traffic_profile.trex_traffic_profile import DST_PORT + from yardstick.network_services.traffic_profile.trex_traffic_profile import TYPE_OF_SERVICE class TestTrexProfile(unittest.TestCase): diff --git a/tests/unit/network_services/vnf_generic/vnf/test_acl_vnf.py b/tests/unit/network_services/vnf_generic/vnf/test_acl_vnf.py index f9a10149e..a4055bf02 100644 --- a/tests/unit/network_services/vnf_generic/vnf/test_acl_vnf.py +++ b/tests/unit/network_services/vnf_generic/vnf/test_acl_vnf.py @@ -21,6 +21,7 @@ import os from tests.unit import STL_MOCKS from tests.unit.network_services.vnf_generic.vnf.test_base import mock_ssh +from yardstick.common import utils STLClient = mock.MagicMock() @@ -312,7 +313,7 @@ class TestAclApproxVnf(unittest.TestCase): acl_approx_vnf.ssh_helper.run.assert_called_once() @mock.patch("yardstick.network_services.vnf_generic.vnf.acl_vnf.YangModel") - @mock.patch("yardstick.network_services.vnf_generic.vnf.acl_vnf.find_relative_file") + @mock.patch.object(utils, 'find_relative_file') @mock.patch("yardstick.network_services.vnf_generic.vnf.sample_vnf.Context") @mock.patch(SSH_HELPER) def test_instantiate(self, ssh, *args): diff --git a/tests/unit/network_services/vnf_generic/vnf/test_prox_helpers.py b/tests/unit/network_services/vnf_generic/vnf/test_prox_helpers.py index 0ac46c632..ce905182c 100644 --- a/tests/unit/network_services/vnf_generic/vnf/test_prox_helpers.py +++ b/tests/unit/network_services/vnf_generic/vnf/test_prox_helpers.py @@ -24,6 +24,7 @@ import mock import unittest from tests.unit import STL_MOCKS +from yardstick.common import utils from yardstick.network_services.vnf_generic.vnf.base import VnfdHelper @@ -962,7 +963,7 @@ class TestProxDpdkVnfSetupEnvHelper(unittest.TestCase): result = setup_helper.prox_config_data self.assertEqual(result, expected) - @mock.patch('yardstick.network_services.vnf_generic.vnf.prox_helpers.find_relative_file') + @mock.patch.object(utils, 'find_relative_file') def test_build_config_file_no_additional_file(self, mock_find_path): vnf1 = { 'prox_args': {'-c': ""}, @@ -996,7 +997,7 @@ class TestProxDpdkVnfSetupEnvHelper(unittest.TestCase): self.assertEqual(helper._prox_config_data, '4') self.assertEqual(helper.remote_path, '5') - @mock.patch('yardstick.network_services.vnf_generic.vnf.prox_helpers.find_relative_file') + @mock.patch.object(utils, 'find_relative_file') def test_build_config_file_additional_file_string(self, mock_find_path): vnf1 = { 'prox_args': {'-c': ""}, @@ -1028,7 +1029,7 @@ class TestProxDpdkVnfSetupEnvHelper(unittest.TestCase): helper.build_config_file() self.assertDictEqual(helper.additional_files, expected) - @mock.patch('yardstick.network_services.vnf_generic.vnf.prox_helpers.find_relative_file') + @mock.patch.object(utils, 'find_relative_file') def test_build_config_file_additional_file(self, mock_find_path): vnf1 = { 'prox_args': {'-c': ""}, @@ -1730,7 +1731,7 @@ class TestProxProfileHelper(unittest.TestCase): } self.assertIsNone(helper._test_cores) - expected = [12, 23] + expected = [3, 4] result = helper.test_cores self.assertEqual(result, expected) self.assertIs(result, helper._test_cores) @@ -1787,7 +1788,7 @@ class TestProxProfileHelper(unittest.TestCase): } self.assertIsNone(helper._latency_cores) - expected = [12, 23] + expected = [3, 4] result = helper.latency_cores self.assertEqual(result, expected) self.assertIs(result, helper._latency_cores) @@ -1842,7 +1843,7 @@ class TestProxProfileHelper(unittest.TestCase): } } - expected = [7, 8] + expected = [3, 4] result = helper.get_cores(helper.PROX_CORE_GEN_MODE) self.assertEqual(result, expected) @@ -1984,8 +1985,8 @@ class TestProxMplsProfileHelper(unittest.TestCase): } } - expected_tagged = [7] - expected_plain = [8] + expected_tagged = [3] + expected_plain = [4] self.assertIsNone(helper._cores_tuple) self.assertEqual(helper.tagged_cores, expected_tagged) self.assertEqual(helper.plain_cores, expected_plain) @@ -2060,10 +2061,10 @@ class TestProxBngProfileHelper(unittest.TestCase): } } - expected_cpe = [7] - expected_inet = [8] - expected_arp = [4, 3] - expected_arp_task = [0, 4] + expected_cpe = [3] + expected_inet = [4] + expected_arp = [6, 9] + expected_arp_task = [0, 6] expected_combined = (expected_cpe, expected_inet, expected_arp, expected_arp_task) self.assertIsNone(helper._cores_tuple) @@ -2131,8 +2132,8 @@ class TestProxVpeProfileHelper(unittest.TestCase): } } - expected_cpe = [7] - expected_inet = [8] + expected_cpe = [3] + expected_inet = [4] expected_combined = (expected_cpe, expected_inet) self.assertIsNone(helper._cores_tuple) @@ -2245,8 +2246,8 @@ class TestProxlwAFTRProfileHelper(unittest.TestCase): } } - expected_tun = [7] - expected_inet = [8] + expected_tun = [3] + expected_inet = [4] expected_combined = (expected_tun, expected_inet) self.assertIsNone(helper._cores_tuple) diff --git a/tests/unit/network_services/vnf_generic/vnf/test_prox_vnf.py b/tests/unit/network_services/vnf_generic/vnf/test_prox_vnf.py index 769279066..46786a304 100644 --- a/tests/unit/network_services/vnf_generic/vnf/test_prox_vnf.py +++ b/tests/unit/network_services/vnf_generic/vnf/test_prox_vnf.py @@ -131,6 +131,8 @@ class TestProxApproxVnf(unittest.TestCase): 'packets_in', 'packets_fwd', 'packets_dropped', + 'curr_packets_fwd', + 'curr_packets_in' ], }, 'connection-point': [ @@ -329,7 +331,7 @@ class TestProxApproxVnf(unittest.TestCase): 'packets_in': 0, 'packets_dropped': 0, 'packets_fwd': 0, - 'collect_stats': {'core': {}}, + 'collect_stats': {'core': {}} } result = prox_approx_vnf.collect_kpi() self.assertEqual(result, expected) @@ -352,7 +354,11 @@ class TestProxApproxVnf(unittest.TestCase): 'collect_stats': {'core': {'result': 234}}, } result = prox_approx_vnf.collect_kpi() - self.assertEqual(result, expected) + self.assertEqual(result['packets_in'], expected['packets_in']) + self.assertEqual(result['packets_dropped'], expected['packets_dropped']) + self.assertEqual(result['packets_fwd'], expected['packets_fwd']) + self.assertNotEqual(result['packets_fwd'], 0) + self.assertNotEqual(result['packets_fwd'], 0) @mock.patch(SSH_HELPER) def test_collect_kpi_error(self, ssh, *args): @@ -373,6 +379,25 @@ class TestProxApproxVnf(unittest.TestCase): file_path = os.path.join(curr_path, filename) return file_path + @mock.patch('yardstick.common.utils.open', create=True) + @mock.patch('yardstick.benchmark.scenarios.networking.vnf_generic.open', create=True) + @mock.patch('yardstick.network_services.helpers.iniparser.open', create=True) + @mock.patch(SSH_HELPER) + def test_run_prox(self, ssh, *_): + mock_ssh(ssh) + + prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0) + prox_approx_vnf.scenario_helper.scenario_cfg = self.SCENARIO_CFG + prox_approx_vnf.ssh_helper.join_bin_path.return_value = '/tool_path12/tool_file34' + prox_approx_vnf.setup_helper.remote_path = 'configs/file56.cfg' + + expected = "sudo bash -c 'cd /tool_path12; " \ + "/tool_path12/tool_file34 -o cli -t -f /tmp/l3-swap-2.cfg '" + + prox_approx_vnf._run() + result = prox_approx_vnf.ssh_helper.run.call_args[0][0] + self.assertEqual(result, expected) + @mock.patch(SSH_HELPER) def bad_test_instantiate(self, *args): prox_approx_vnf = ProxApproxVnf(NAME, self.VNFD0) diff --git a/tests/unit/network_services/vnf_generic/vnf/test_sample_vnf.py b/tests/unit/network_services/vnf_generic/vnf/test_sample_vnf.py index 25633384e..b2e3fd85b 100644 --- a/tests/unit/network_services/vnf_generic/vnf/test_sample_vnf.py +++ b/tests/unit/network_services/vnf_generic/vnf/test_sample_vnf.py @@ -1,6 +1,6 @@ #!/usr/bin/env python -# Copyright (c) 2017 Intel Corporation +# Copyright (c) 2017-2018 Intel Corporation # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -15,14 +15,17 @@ # limitations under the License. # +from copy import deepcopy + import unittest import mock -from copy import deepcopy +import six from tests.unit.network_services.vnf_generic.vnf.test_base import mock_ssh from tests.unit import STL_MOCKS from yardstick.benchmark.contexts.base import Context from yardstick.common import exceptions as y_exceptions +from yardstick.common import utils from yardstick.network_services.nfvi.resource import ResourceProfile from yardstick.network_services.vnf_generic.vnf.base import VnfdHelper @@ -36,7 +39,8 @@ stl_patch = mock.patch.dict("sys.modules", STL_MOCKS) stl_patch.start() if stl_patch: - from yardstick.network_services.vnf_generic.vnf.sample_vnf import VnfSshHelper + from yardstick.network_services.vnf_generic.vnf import sample_vnf + from yardstick.network_services.vnf_generic.vnf.vnf_ssh_helper import VnfSshHelper from yardstick.network_services.vnf_generic.vnf.sample_vnf import SampleVNFDeployHelper from yardstick.network_services.vnf_generic.vnf.sample_vnf import ScenarioHelper from yardstick.network_services.vnf_generic.vnf.sample_vnf import ResourceHelper @@ -528,46 +532,25 @@ class TestDpdkVnfSetupEnvHelper(unittest.TestCase): result = DpdkVnfSetupEnvHelper._update_traffic_type(ip_pipeline_cfg, traffic_options) self.assertEqual(result, expected) - def test__setup_hugepages(self): - vnfd_helper = VnfdHelper(self.VNFD_0) - ssh_helper = mock.Mock() - ssh_helper.execute.return_value = 0, '', '' - scenario_helper = mock.Mock() - dpdk_setup_helper = DpdkVnfSetupEnvHelper(vnfd_helper, ssh_helper, scenario_helper) - - result = dpdk_setup_helper._setup_hugepages() - expect_start_list = ['awk', 'awk', 'echo'] - expect_in_list = ['meminfo', 'nr_hugepages', '16'] - call_args_iter = (args[0][0] for args in ssh_helper.execute.call_args_list) - self.assertIsNone(result) - self.assertEqual(ssh_helper.execute.call_count, 3) - for expect_start, expect_in, arg0 in zip(expect_start_list, expect_in_list, - call_args_iter): - self.assertTrue(arg0.startswith(expect_start)) - self.assertIn(expect_in, arg0) - - def test__setup_hugepages_2_mb(self): - vnfd_helper = VnfdHelper(self.VNFD_0) + @mock.patch.object(six, 'BytesIO', return_value=six.BytesIO(b'100\n')) + @mock.patch.object(utils, 'read_meminfo', + return_value={'Hugepagesize': '2048'}) + def test__setup_hugepages(self, mock_meminfo, *args): ssh_helper = mock.Mock() - ssh_helper.execute.return_value = 0, '2048kB ', '' - scenario_helper = mock.Mock() - dpdk_setup_helper = DpdkVnfSetupEnvHelper(vnfd_helper, ssh_helper, scenario_helper) - - result = dpdk_setup_helper._setup_hugepages() - expect_start_list = ['awk', 'awk', 'echo'] - expect_in_list = ['meminfo', 'nr_hugepages', '8192'] - call_args_iter = (args[0][0] for args in ssh_helper.execute.call_args_list) - self.assertIsNone(result) - self.assertEqual(ssh_helper.execute.call_count, 3) - for expect_start, expect_in, arg0 in zip(expect_start_list, expect_in_list, - call_args_iter): - self.assertTrue(arg0.startswith(expect_start)) - self.assertIn(expect_in, arg0) + dpdk_setup_helper = DpdkVnfSetupEnvHelper( + mock.ANY, ssh_helper, mock.ANY) + with mock.patch.object(sample_vnf.LOG, 'info') as mock_info: + dpdk_setup_helper._setup_hugepages() + mock_info.assert_called_once_with( + 'Hugepages size (kB): %s, number claimed: %s, number set: ' + '%s', 2048, 8192, 100) + mock_meminfo.assert_called_once_with(ssh_helper) @mock.patch('yardstick.network_services.vnf_generic.vnf.sample_vnf.open') - @mock.patch('yardstick.network_services.vnf_generic.vnf.sample_vnf.find_relative_file') + @mock.patch.object(utils, 'find_relative_file') @mock.patch('yardstick.network_services.vnf_generic.vnf.sample_vnf.MultiPortConfig') - def test_build_config(self, mock_multi_port_config_class, mock_find, *args): + @mock.patch.object(utils, 'open_relative_file') + def test_build_config(self, mock_open_rf, mock_multi_port_config_class, mock_find, *args): mock_multi_port_config = mock_multi_port_config_class() vnfd_helper = VnfdHelper(self.VNFD_0) ssh_helper = mock.Mock() @@ -584,6 +567,20 @@ class TestDpdkVnfSetupEnvHelper(unittest.TestCase): self.assertGreaterEqual(mock_multi_port_config.generate_config.call_count, 1) self.assertGreaterEqual(mock_multi_port_config.generate_script.call_count, 1) + scenario_helper.vnf_cfg = {'file': 'fake_file'} + dpdk_setup_helper = DpdkVnfSetupEnvHelper(vnfd_helper, ssh_helper, scenario_helper) + mock_open_rf.side_effect = mock.mock_open(read_data='fake_data') + dpdk_setup_helper.PIPELINE_COMMAND = expected = 'pipeline command' + + result = dpdk_setup_helper.build_config() + + mock_open_rf.assert_called_once() + self.assertEqual(result, expected) + self.assertGreaterEqual(ssh_helper.upload_config_file.call_count, 2) + self.assertGreaterEqual(mock_find.call_count, 1) + self.assertGreaterEqual(mock_multi_port_config.generate_config.call_count, 1) + self.assertGreaterEqual(mock_multi_port_config.generate_script.call_count, 1) + def test__build_pipeline_kwargs(self): vnfd_helper = VnfdHelper(self.VNFD_0) ssh_helper = mock.Mock() @@ -619,10 +616,15 @@ class TestDpdkVnfSetupEnvHelper(unittest.TestCase): ssh_helper = mock.Mock() ssh_helper.execute = execute - dpdk_vnf_setup_env_helper = DpdkVnfSetupEnvHelper(vnfd_helper, ssh_helper, mock.Mock()) + scenario_helper = mock.Mock() + scenario_helper.nodes = [None, None] + dpdk_vnf_setup_env_helper = DpdkVnfSetupEnvHelper(vnfd_helper, ssh_helper, scenario_helper) dpdk_vnf_setup_env_helper._validate_cpu_cfg = mock.Mock(return_value=[]) - self.assertIsInstance(dpdk_vnf_setup_env_helper.setup_vnf_environment(), ResourceProfile) + with mock.patch.object(dpdk_vnf_setup_env_helper, '_setup_dpdk'): + self.assertIsInstance( + dpdk_vnf_setup_env_helper.setup_vnf_environment(), + ResourceProfile) def test__setup_dpdk(self): ssh_helper = mock.Mock() @@ -638,21 +640,6 @@ class TestDpdkVnfSetupEnvHelper(unittest.TestCase): mock.call('lsmod | grep -i igb_uio') ]) - def test__setup_dpdk_igb_uio_not_loaded(self): - ssh_helper = mock.Mock() - ssh_helper.execute = mock.Mock() - ssh_helper.execute.side_effect = [(0, 0, 0), (1, 0, 0)] - dpdk_setup_helper = DpdkVnfSetupEnvHelper(mock.ANY, ssh_helper, mock.ANY) - with mock.patch.object(dpdk_setup_helper, '_setup_hugepages') as \ - mock_setup_hp: - with self.assertRaises(y_exceptions.DPDKSetupDriverError): - dpdk_setup_helper._setup_dpdk() - mock_setup_hp.assert_called_once() - ssh_helper.execute.assert_has_calls([ - mock.call('sudo modprobe uio && sudo modprobe igb_uio'), - mock.call('lsmod | grep -i igb_uio') - ]) - @mock.patch('yardstick.ssh.SSH') def test__setup_resources(self, _): vnfd_helper = VnfdHelper(deepcopy(self.VNFD_0)) @@ -690,6 +677,7 @@ class TestDpdkVnfSetupEnvHelper(unittest.TestCase): # ssh_helper.execute = mock.Mock(return_value = (0, 'text', '')) # ssh_helper.execute.return_value = 0, 'output', '' scenario_helper = mock.Mock() + scenario_helper.nodes = [None, None] rv = ['0000:05:00.1', '0000:05:00.0'] dpdk_setup_helper = DpdkVnfSetupEnvHelper(vnfd_helper, ssh_helper, scenario_helper) @@ -708,6 +696,7 @@ class TestDpdkVnfSetupEnvHelper(unittest.TestCase): vnfd_helper = VnfdHelper(self.VNFD_0) ssh_helper = mock.Mock() scenario_helper = mock.Mock() + scenario_helper.nodes = [None, None] dpdk_setup_helper = DpdkVnfSetupEnvHelper(vnfd_helper, ssh_helper, scenario_helper) dpdk_setup_helper.dpdk_bind_helper.bind = mock.Mock() dpdk_setup_helper.dpdk_bind_helper.used_drivers = { @@ -1386,7 +1375,7 @@ class TestSampleVNFDeployHelper(unittest.TestCase): @mock.patch('yardstick.network_services.vnf_generic.vnf.sample_vnf.time') @mock.patch('subprocess.check_output') - def test_deploy_vnfs_disabled(self, *args): + def test_deploy_vnfs_disabled(self, *_): vnfd_helper = mock.Mock() ssh_helper = mock.Mock() ssh_helper.join_bin_path.return_value = 'joined_path' diff --git a/tests/unit/network_services/vnf_generic/vnf/test_tg_ixload.py b/tests/unit/network_services/vnf_generic/vnf/test_tg_ixload.py index d77068137..b93f9aad3 100644 --- a/tests/unit/network_services/vnf_generic/vnf/test_tg_ixload.py +++ b/tests/unit/network_services/vnf_generic/vnf/test_tg_ixload.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python - # Copyright (c) 2016-2017 Intel Corporation # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -15,13 +13,15 @@ # limitations under the License. # -from __future__ import absolute_import +import subprocess -import unittest import mock -import subprocess +import unittest +import six from tests.unit import STL_MOCKS +from yardstick import ssh +from yardstick.common import utils STLClient = mock.MagicMock() @@ -147,11 +147,11 @@ class TestIxLoadTrafficGen(unittest.TestCase): ixload_traffic_gen = IxLoadTrafficGen(NAME, vnfd) self.assertEqual(None, ixload_traffic_gen.listen_traffic({})) - @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.makedirs") + @mock.patch.object(utils, 'find_relative_file') + @mock.patch.object(utils, 'makedirs') @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.call") @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.shutil") - def test_instantiate(self, call, shutil, mock_makedirs): - # pylint: disable=unused-argument + def test_instantiate(self, shutil, *args): with mock.patch("yardstick.ssh.SSH") as ssh: ssh_mock = mock.Mock(autospec=ssh.SSH) ssh_mock.execute = \ @@ -175,19 +175,18 @@ class TestIxLoadTrafficGen(unittest.TestCase): '1C/1T', 'worker_threads': 1}} }}) - with mock.patch('yardstick.benchmark.scenarios.networking.vnf_generic.open', - create=True) as mock_open: + with mock.patch.object(six.moves.builtins, 'open', + create=True) as mock_open: mock_open.return_value = mock.MagicMock() ixload_traffic_gen.instantiate(scenario_cfg, {}) @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.call") - @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.shutil") @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.open") @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.min") @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.max") @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.len") - def test_run_traffic(self, call, shutil, main_open, min, max, len): - # pylint: disable=unused-argument + @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.shutil") + def test_run_traffic(self, shutil, *args): mock_traffic_profile = mock.Mock(autospec=TrafficProfile) mock_traffic_profile.get_traffic_definition.return_value = "64" mock_traffic_profile.params = self.TRAFFIC_PROFILE @@ -213,13 +212,12 @@ class TestIxLoadTrafficGen(unittest.TestCase): self.assertIsNone(result) @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.call") - @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.shutil") @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.open") @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.min") @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.max") @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.len") - def test_run_traffic_csv(self, call, shutil, main_open, min, max, len): - # pylint: disable=unused-argument + @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.shutil") + def test_run_traffic_csv(self, shutil, *args): mock_traffic_profile = mock.Mock(autospec=TrafficProfile) mock_traffic_profile.get_traffic_definition.return_value = "64" mock_traffic_profile.params = self.TRAFFIC_PROFILE @@ -247,20 +245,15 @@ class TestIxLoadTrafficGen(unittest.TestCase): self.assertIsNone(result) @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.call") + @mock.patch.object(ssh, 'SSH') def test_terminate(self, *args): - with mock.patch("yardstick.ssh.SSH") as ssh: - vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0] - ssh_mock = mock.Mock(autospec=ssh.SSH) - ssh_mock.execute = \ - mock.Mock(return_value=(0, "", "")) - ssh.from_node.return_value = ssh_mock - ixload_traffic_gen = IxLoadTrafficGen(NAME, vnfd) - self.assertEqual(None, ixload_traffic_gen.terminate()) + vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0] + ixload_traffic_gen = IxLoadTrafficGen(NAME, vnfd) + self.assertEqual(None, ixload_traffic_gen.terminate()) - @mock.patch("yardstick.ssh.SSH") @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.call") - def test_parse_csv_read(self, mock_call, mock_ssh): - # pylint: disable=unused-argument + @mock.patch.object(ssh, 'SSH') + def test_parse_csv_read(self, mock_ssh, *args): vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0] kpi_data = { 'HTTP Total Throughput (Kbps)': 1, @@ -282,10 +275,9 @@ class TestIxLoadTrafficGen(unittest.TestCase): for key_left, key_right in IxLoadResourceHelper.KPI_LIST.items(): self.assertEqual(result[key_left][-1], int(kpi_data[key_right])) - @mock.patch("yardstick.ssh.SSH") @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.call") - def test_parse_csv_read_value_error(self, mock_call, mock_ssh): - # pylint: disable=unused-argument + @mock.patch.object(ssh, 'SSH') + def test_parse_csv_read_value_error(self, mock_ssh, *args): vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0] http_reader = [{ 'HTTP Total Throughput (Kbps)': 1, @@ -305,10 +297,9 @@ class TestIxLoadTrafficGen(unittest.TestCase): ixload_traffic_gen.resource_helper.parse_csv_read(http_reader) self.assertDictEqual(ixload_traffic_gen.resource_helper.result, init_value) - @mock.patch("yardstick.ssh.SSH") @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.call") - def test_parse_csv_read_error(self, mock_call, mock_ssh): - # pylint: disable=unused-argument + @mock.patch.object(ssh, 'SSH') + def test_parse_csv_read_error(self, mock_ssh, *args): vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0] http_reader = [{ 'HTTP Total Throughput (Kbps)': 1, diff --git a/tests/unit/network_services/vnf_generic/vnf/test_tg_ping.py b/tests/unit/network_services/vnf_generic/vnf/test_tg_ping.py index fb26f20b5..ed2274e79 100644 --- a/tests/unit/network_services/vnf_generic/vnf/test_tg_ping.py +++ b/tests/unit/network_services/vnf_generic/vnf/test_tg_ping.py @@ -36,7 +36,7 @@ if stl_patch: from yardstick.network_services.vnf_generic.vnf.tg_ping import PingTrafficGen from yardstick.network_services.vnf_generic.vnf.tg_ping import PingResourceHelper from yardstick.network_services.vnf_generic.vnf.tg_ping import PingSetupEnvHelper - from yardstick.network_services.vnf_generic.vnf.sample_vnf import VnfSshHelper + from yardstick.network_services.vnf_generic.vnf.vnf_ssh_helper import VnfSshHelper class TestPingResourceHelper(unittest.TestCase): diff --git a/tests/unit/network_services/vnf_generic/vnf/test_tg_rfc2544_ixia.py b/tests/unit/network_services/vnf_generic/vnf/test_tg_rfc2544_ixia.py index e9f718cb7..aeabc367a 100644 --- a/tests/unit/network_services/vnf_generic/vnf/test_tg_rfc2544_ixia.py +++ b/tests/unit/network_services/vnf_generic/vnf/test_tg_rfc2544_ixia.py @@ -16,8 +16,10 @@ # import os -import unittest + import mock +import six +import unittest from tests.unit import STL_MOCKS @@ -341,7 +343,7 @@ class TestIXIATrafficGen(unittest.TestCase): 'task_path': '/path/to/task' } - @mock.patch('yardstick.benchmark.scenarios.networking.vnf_generic.open', create=True) + @mock.patch.object(six.moves.builtins, 'open', create=True) @mock.patch('yardstick.network_services.vnf_generic.vnf.tg_rfc2544_ixia.open', mock.mock_open(), create=True) @mock.patch('yardstick.network_services.vnf_generic.vnf.tg_rfc2544_ixia.LOG.exception') diff --git a/tests/unit/network_services/vnf_generic/vnf/test_vfw_vnf.py b/tests/unit/network_services/vnf_generic/vnf/test_vfw_vnf.py index f0a56665c..f2eca196f 100644 --- a/tests/unit/network_services/vnf_generic/vnf/test_vfw_vnf.py +++ b/tests/unit/network_services/vnf_generic/vnf/test_vfw_vnf.py @@ -22,6 +22,9 @@ import os from tests.unit import STL_MOCKS from tests.unit.network_services.vnf_generic.vnf.test_base import mock_ssh +from yardstick.common import utils + + STLClient = mock.MagicMock() stl_patch = mock.patch.dict("sys.modules", STL_MOCKS) stl_patch.start() @@ -331,7 +334,7 @@ pipeline> vfw_approx_vnf._run() vfw_approx_vnf.ssh_helper.run.assert_called_once() - @mock.patch("yardstick.network_services.vnf_generic.vnf.vfw_vnf.find_relative_file") + @mock.patch.object(utils, 'find_relative_file') @mock.patch("yardstick.network_services.vnf_generic.vnf.vfw_vnf.YangModel") @mock.patch("yardstick.network_services.vnf_generic.vnf.sample_vnf.Context") @mock.patch(SSH_HELPER) |