diff options
Diffstat (limited to 'tests/unit/benchmark/scenarios')
21 files changed, 424 insertions, 220 deletions
diff --git a/tests/unit/benchmark/scenarios/availability/test_monitor_command.py b/tests/unit/benchmark/scenarios/availability/test_monitor_command.py index 6a9b3b157..b84cef23c 100644 --- a/tests/unit/benchmark/scenarios/availability/test_monitor_command.py +++ b/tests/unit/benchmark/scenarios/availability/test_monitor_command.py @@ -19,30 +19,26 @@ import unittest from yardstick.benchmark.scenarios.availability.monitor import monitor_command -@mock.patch( - 'yardstick.benchmark.scenarios.availability.monitor.monitor_command' - '.subprocess') +@mock.patch('subprocess.check_output') class ExecuteShellTestCase(unittest.TestCase): - def test__fun_execute_shell_command_successful(self, mock_subprocess): + def test__fun_execute_shell_command_successful(self, mock_subprocess_check_output): cmd = "env" - mock_subprocess.check_output.return_value = (0, 'unittest') - exitcode, output = monitor_command._execute_shell_command(cmd) + mock_subprocess_check_output.return_value = (0, 'unittest') + exitcode, _ = monitor_command._execute_shell_command(cmd) self.assertEqual(exitcode, 0) @mock.patch('yardstick.benchmark.scenarios.availability.monitor.monitor_command.LOG') def test__fun_execute_shell_command_fail_cmd_exception(self, mock_log, - mock_subprocess): + mock_subprocess_check_output): cmd = "env" - mock_subprocess.check_output.side_effect = RuntimeError - exitcode, output = monitor_command._execute_shell_command(cmd) + mock_subprocess_check_output.side_effect = RuntimeError + exitcode, _ = monitor_command._execute_shell_command(cmd) self.assertEqual(exitcode, -1) mock_log.error.assert_called_once() -@mock.patch( - 'yardstick.benchmark.scenarios.availability.monitor.monitor_command' - '.subprocess') +@mock.patch('subprocess.check_output') class MonitorOpenstackCmdTestCase(unittest.TestCase): def setUp(self): @@ -59,24 +55,24 @@ class MonitorOpenstackCmdTestCase(unittest.TestCase): 'sla': {'max_outage_time': 5} } - def test__monitor_command_monitor_func_successful(self, mock_subprocess): + def test__monitor_command_monitor_func_successful(self, mock_subprocess_check_output): instance = monitor_command.MonitorOpenstackCmd(self.config, None, {"nova-api": 10}) instance.setup() - mock_subprocess.check_output.return_value = (0, 'unittest') + mock_subprocess_check_output.return_value = (0, 'unittest') ret = instance.monitor_func() - self.assertEqual(ret, True) + self.assertTrue(ret) instance._result = {"outage_time": 0} instance.verify_SLA() @mock.patch('yardstick.benchmark.scenarios.availability.monitor.monitor_command.LOG') - def test__monitor_command_monitor_func_failure(self, mock_log, mock_subprocess): - mock_subprocess.check_output.return_value = (1, 'unittest') + def test__monitor_command_monitor_func_failure(self, mock_log, mock_subprocess_check_output): + mock_subprocess_check_output.return_value = (1, 'unittest') instance = monitor_command.MonitorOpenstackCmd(self.config, None, {"nova-api": 10}) instance.setup() - mock_subprocess.check_output.side_effect = RuntimeError + mock_subprocess_check_output.side_effect = RuntimeError ret = instance.monitor_func() - self.assertEqual(ret, False) + self.assertFalse(ret) mock_log.error.assert_called_once() instance._result = {"outage_time": 10} instance.verify_SLA() @@ -84,12 +80,13 @@ class MonitorOpenstackCmdTestCase(unittest.TestCase): @mock.patch( 'yardstick.benchmark.scenarios.availability.monitor.monitor_command' '.ssh') - def test__monitor_command_ssh_monitor_successful(self, mock_ssh, - mock_subprocess): + def test__monitor_command_ssh_monitor_successful(self, mock_ssh, mock_subprocess_check_output): + mock_subprocess_check_output.return_value = (0, 'unittest') self.config["host"] = "node1" instance = monitor_command.MonitorOpenstackCmd( self.config, self.context, {"nova-api": 10}) instance.setup() mock_ssh.SSH.from_node().execute.return_value = (0, "0", '') ret = instance.monitor_func() + self.assertTrue(ret) diff --git a/tests/unit/benchmark/scenarios/availability/test_serviceha.py b/tests/unit/benchmark/scenarios/availability/test_serviceha.py index 4ae508958..97d534894 100644 --- a/tests/unit/benchmark/scenarios/availability/test_serviceha.py +++ b/tests/unit/benchmark/scenarios/availability/test_serviceha.py @@ -18,9 +18,6 @@ import unittest from yardstick.benchmark.scenarios.availability import serviceha -@mock.patch('yardstick.benchmark.scenarios.availability.serviceha.basemonitor') -@mock.patch( - 'yardstick.benchmark.scenarios.availability.serviceha.baseattacker') class ServicehaTestCase(unittest.TestCase): def setUp(self): @@ -51,27 +48,32 @@ class ServicehaTestCase(unittest.TestCase): sla = {"outage_time": 5} self.args = {"options": options, "sla": sla} - def test__serviceha_setup_run_successful(self, mock_attacker, + @mock.patch('yardstick.benchmark.scenarios.availability.serviceha.basemonitor') + @mock.patch( + 'yardstick.benchmark.scenarios.availability.serviceha.baseattacker') + def test__serviceha_setup_run_successful(self, _, mock_monitor): p = serviceha.ServiceHA(self.args, self.ctx) p.setup() - self.assertEqual(p.setup_done, True) + self.assertTrue(p.setup_done) mock_monitor.MonitorMgr().verify_SLA.return_value = True ret = {} p.run(ret) p.teardown() -""" - def test__serviceha_run_sla_error(self, mock_attacker, mock_monitor): - p = serviceha.ServiceHA(self.args, self.ctx) p.setup() - self.assertEqual(p.setup_done, True) + self.assertTrue(p.setup_done) - result = {} - result["outage_time"] = 10 - mock_monitor.Monitor().get_result.return_value = result +# def test__serviceha_run_sla_error(self, mock_attacker, mock_monitor): +# p = serviceha.ServiceHA(self.args, self.ctx) - ret = {} - self.assertRaises(AssertionError, p.run, ret) -""" +# p.setup() +# self.assertTrue(p.setup_done) +# +# result = {} +# result["outage_time"] = 10 +# mock_monitor.Monitor().get_result.return_value = result + +# ret = {} +# self.assertRaises(AssertionError, p.run, ret) diff --git a/tests/unit/benchmark/scenarios/availability/test_util.py b/tests/unit/benchmark/scenarios/availability/test_util.py index 0974f385a..548efe91b 100644 --- a/tests/unit/benchmark/scenarios/availability/test_util.py +++ b/tests/unit/benchmark/scenarios/availability/test_util.py @@ -16,36 +16,43 @@ import unittest from yardstick.benchmark.scenarios.availability import util -@mock.patch('yardstick.benchmark.scenarios.availability.util.subprocess') + class ExecuteShellTestCase(unittest.TestCase): def setUp(self): self.param_config = {'serviceName': '@serviceName', 'value': 1} self.intermediate_variables = {'@serviceName': 'nova-api'} self.std_output = '| id | 1 |' - self.cmd_config = {'cmd':'ls','param':'-a'} + self.cmd_config = {'cmd': 'ls', 'param': '-a'} + + self._mock_subprocess = mock.patch.object(util, 'subprocess') + self.mock_subprocess = self._mock_subprocess.start() + self.addCleanup(self._stop_mock) + + def _stop_mock(self): + self._mock_subprocess.stop() - def test_util_build_command_shell(self,mock_subprocess): + def test_util_build_command_shell(self): result = util.build_shell_command(self.param_config, True, self.intermediate_variables) - self.assertEqual("nova-api" in result, True) + self.assertIn("nova-api", result) - def test_read_stdout_item(self,mock_subprocess): - result = util.read_stdout_item(self.std_output,'id') - self.assertEquals('1',result) + def test_read_stdout_item(self): + result = util.read_stdout_item(self.std_output, 'id') + self.assertEqual('1', result) - def test_buildshellparams(self,mock_subprocess): - result = util.buildshellparams(self.cmd_config,True) - self.assertEquals('/bin/bash -s {0} {1}', result) + def test_buildshellparams(self): + result = util.buildshellparams(self.cmd_config, True) + self.assertEqual('/bin/bash -s {0} {1}', result) - def test__fun_execute_shell_command_successful(self, mock_subprocess): + def test__fun_execute_shell_command_successful(self): cmd = "env" - mock_subprocess.check_output.return_value = (0, 'unittest') - exitcode, output = util.execute_shell_command(cmd) + self.mock_subprocess.check_output.return_value = (0, 'unittest') + exitcode, _ = util.execute_shell_command(cmd) self.assertEqual(exitcode, 0) - def test__fun_execute_shell_command_fail_cmd_exception(self, mock_subprocess): + def test__fun_execute_shell_command_fail_cmd_exception(self): cmd = "env" - mock_subprocess.check_output.side_effect = RuntimeError - exitcode, output = util.execute_shell_command(cmd) + self.mock_subprocess.check_output.side_effect = RuntimeError + exitcode, _ = util.execute_shell_command(cmd) self.assertEqual(exitcode, -1) diff --git a/tests/unit/benchmark/scenarios/compute/test_cyclictest.py b/tests/unit/benchmark/scenarios/compute/test_cyclictest.py index dc52a80c7..51ffd2488 100644 --- a/tests/unit/benchmark/scenarios/compute/test_cyclictest.py +++ b/tests/unit/benchmark/scenarios/compute/test_cyclictest.py @@ -74,7 +74,7 @@ class CyclictestTestCase(unittest.TestCase): c.setup() self.assertIsNotNone(c.guest) self.assertIsNotNone(c.host) - self.assertEqual(c.setup_done, True) + self.assertTrue(c.setup_done) def test_cyclictest_successful_no_sla(self, mock_ssh): result = {} diff --git a/tests/unit/benchmark/scenarios/compute/test_qemumigrate.py b/tests/unit/benchmark/scenarios/compute/test_qemumigrate.py index 1f0ff3c29..fb55b809f 100644 --- a/tests/unit/benchmark/scenarios/compute/test_qemumigrate.py +++ b/tests/unit/benchmark/scenarios/compute/test_qemumigrate.py @@ -71,7 +71,7 @@ class QemuMigrateTestCase(unittest.TestCase): q.setup() self.assertIsNotNone(q.host) - self.assertEqual(q.setup_done, True) + self.assertTrue(q.setup_done) def test_qemu_migrate_successful_no_sla(self, mock_ssh): result = {} diff --git a/tests/unit/benchmark/scenarios/compute/test_spec_cpu_for_vm.py b/tests/unit/benchmark/scenarios/compute/test_spec_cpu_for_vm.py new file mode 100644 index 000000000..c428e1fb8 --- /dev/null +++ b/tests/unit/benchmark/scenarios/compute/test_spec_cpu_for_vm.py @@ -0,0 +1,84 @@ +#!/usr/bin/env python + +############################################################################## +# Copyright (c) 2017 Huawei Technologies Co.,Ltd and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + +# Unittest for yardstick.benchmark.scenarios.compute.spec_cpu_for_vm.SpecCPUforVM + +from __future__ import absolute_import + +import unittest + +import mock + +from yardstick.benchmark.scenarios.compute import spec_cpu_for_vm + + +@mock.patch('yardstick.benchmark.scenarios.compute.spec_cpu_for_vm.ssh') +class SpecCPUforVMTestCase(unittest.TestCase): + + def setUp(self): + self.ctx = { + 'host': { + 'ip': '172.16.0.137', + 'user': 'root', + 'key_filename': "mykey.key" + } + } + + self.result = {} + + def test_spec_cpu_successful_setup(self, mock_ssh): + + options = { + "SPECint_benchmark": "perlbench", + "runspec_tune": "all", + "output_format": "all", + "runspec_iterations": "1", + "runspec_size": "test" + } + args = {"options": options} + s = spec_cpu_for_vm.SpecCPUforVM(args, self.ctx) + mock_ssh.SSH.from_node().execute.return_value = (0, '', '') + + s.setup() + self.assertIsNotNone(s.client) + self.assertTrue(s.setup_done, True) + + def test_spec_cpu_successful__run_no_sla(self, mock_ssh): + + options = { + "SPECint_benchmark": "perlbench", + "runspec_tune": "all", + "output_format": "all" + } + args = {"options": options} + s = spec_cpu_for_vm.SpecCPUforVM(args, self.ctx) + + mock_ssh.SSH.from_node().execute.return_value = (0, '', '') + mock_ssh.SSH.from_node().get.return_value = (0, '', '') + s.run(self.result) + expected_result = {'SPEC_CPU_result': ''} + self.assertEqual(self.result, expected_result) + + def test_spec_cpu_unsuccessful_script_error(self, mock_ssh): + options = { + "benchmark_subset": "int" + } + args = {"options": options} + s = spec_cpu_for_vm.SpecCPUforVM(args, self.ctx) + + mock_ssh.SSH.from_node().execute.return_value = (1, '', 'FOOBAR') + self.assertRaises(RuntimeError, s.run, self.result) + +def main(): + unittest.main() + +if __name__ == '__main__': + main() diff --git a/tests/unit/benchmark/scenarios/compute/test_unixbench.py b/tests/unit/benchmark/scenarios/compute/test_unixbench.py index 7d071e91c..fec355b45 100644 --- a/tests/unit/benchmark/scenarios/compute/test_unixbench.py +++ b/tests/unit/benchmark/scenarios/compute/test_unixbench.py @@ -40,7 +40,7 @@ class UnixbenchTestCase(unittest.TestCase): mock_ssh.SSH.from_node().execute.return_value = (0, '', '') self.assertIsNotNone(u.client) - self.assertEqual(u.setup_done, True) + self.assertTrue(u.setup_done) def test_unixbench_successful_no_sla(self, mock_ssh): diff --git a/tests/unit/benchmark/scenarios/dummy/test_dummy.py b/tests/unit/benchmark/scenarios/dummy/test_dummy.py index 560675d09..bc5131806 100644 --- a/tests/unit/benchmark/scenarios/dummy/test_dummy.py +++ b/tests/unit/benchmark/scenarios/dummy/test_dummy.py @@ -24,11 +24,11 @@ class DummyTestCase(unittest.TestCase): self.assertIsNone(self.test_context.scenario_cfg) self.assertIsNone(self.test_context.context_cfg) - self.assertEqual(self.test_context.setup_done, False) + self.assertFalse(self.test_context.setup_done) def test_run(self): result = {} self.test_context.run(result) self.assertEqual(result["hello"], "yardstick") - self.assertEqual(self.test_context.setup_done, True) + self.assertTrue(self.test_context.setup_done) diff --git a/tests/unit/benchmark/scenarios/lib/test_check_numa_info.py b/tests/unit/benchmark/scenarios/lib/test_check_numa_info.py index bdf1e66e5..1dd461d41 100644 --- a/tests/unit/benchmark/scenarios/lib/test_check_numa_info.py +++ b/tests/unit/benchmark/scenarios/lib/test_check_numa_info.py @@ -14,7 +14,8 @@ from yardstick.benchmark.scenarios.lib.check_numa_info import CheckNumaInfo class CheckNumaInfoTestCase(unittest.TestCase): - @mock.patch('yardstick.benchmark.scenarios.lib.check_numa_info.CheckNumaInfo._check_vm2_status') + @mock.patch( + 'yardstick.benchmark.scenarios.lib.check_numa_info.CheckNumaInfo._check_vm2_status') def test_check_numa_info(self, mock_check_vm2): scenario_cfg = {'info1': {}, 'info2': {}} obj = CheckNumaInfo(scenario_cfg, {}) @@ -37,7 +38,7 @@ class CheckNumaInfoTestCase(unittest.TestCase): scenario_cfg = {'info1': info1, 'info2': info2} obj = CheckNumaInfo(scenario_cfg, {}) status = obj._check_vm2_status(info1, info2) - self.assertEqual(status, True) + self.assertTrue(status) def test_check_vm2_status_length_gt_1(self): info1 = { @@ -55,7 +56,7 @@ class CheckNumaInfoTestCase(unittest.TestCase): scenario_cfg = {'info1': info1, 'info2': info2} obj = CheckNumaInfo(scenario_cfg, {}) status = obj._check_vm2_status(info1, info2) - self.assertEqual(status, False) + self.assertFalse(status) def test_check_vm2_status_length_not_in_set(self): info1 = { @@ -73,7 +74,7 @@ class CheckNumaInfoTestCase(unittest.TestCase): scenario_cfg = {'info1': info1, 'info2': info2} obj = CheckNumaInfo(scenario_cfg, {}) status = obj._check_vm2_status(info1, info2) - self.assertEqual(status, False) + self.assertFalse(status) def main(): diff --git a/tests/unit/benchmark/scenarios/lib/test_create_volume.py b/tests/unit/benchmark/scenarios/lib/test_create_volume.py index fc633139e..ef2c0ccaf 100644 --- a/tests/unit/benchmark/scenarios/lib/test_create_volume.py +++ b/tests/unit/benchmark/scenarios/lib/test_create_volume.py @@ -9,28 +9,79 @@ import unittest import mock -from yardstick.benchmark.scenarios.lib.create_volume import CreateVolume +import yardstick.benchmark.scenarios.lib.create_volume class CreateVolumeTestCase(unittest.TestCase): + def setUp(self): + self._mock_cinder_client = mock.patch( + 'yardstick.common.openstack_utils.get_cinder_client') + self.mock_cinder_client = self._mock_cinder_client.start() + self._mock_glance_client = mock.patch( + 'yardstick.common.openstack_utils.get_glance_client') + self.mock_glance_client = self._mock_glance_client.start() + self.addCleanup(self._stop_mock) + + self.scenario_cfg = { + "options" : + { + 'volume_name': 'yardstick_test_volume_01', + 'size': '256', + 'image': 'cirros-0.3.5' + } + } + + self.scenario = ( + yardstick.benchmark.scenarios.lib.create_volume.CreateVolume( + scenario_cfg=self.scenario_cfg, + context_cfg={})) + + def _stop_mock(self): + self._mock_cinder_client.stop() + self._mock_glance_client.stop() + + def test_init(self): + self.mock_cinder_client.return_value = "All volumes are equal" + self.mock_glance_client.return_value = "Images are more equal" + + expected_vol_name = self.scenario_cfg["options"]["volume_name"] + expected_vol_size = self.scenario_cfg["options"]["size"] + expected_im_name = self.scenario_cfg["options"]["image"] + expected_im_id = None + + scenario = ( + yardstick.benchmark.scenarios.lib.create_volume.CreateVolume( + scenario_cfg=self.scenario_cfg, + context_cfg={})) + + self.assertEqual(expected_vol_name, scenario.volume_name) + self.assertEqual(expected_vol_size, scenario.volume_size) + self.assertEqual(expected_im_name, scenario.image_name) + self.assertEqual(expected_im_id, scenario.image_id) + self.assertEqual("All volumes are equal", scenario.cinder_client) + self.assertEqual("Images are more equal", scenario.glance_client) + + def test_setup(self): + self.assertFalse(self.scenario.setup_done) + self.scenario.setup() + self.assertTrue(self.scenario.setup_done) + @mock.patch('yardstick.common.openstack_utils.create_volume') @mock.patch('yardstick.common.openstack_utils.get_image_id') - @mock.patch('yardstick.common.openstack_utils.get_cinder_client') - @mock.patch('yardstick.common.openstack_utils.get_glance_client') - def test_create_volume(self, mock_get_glance_client, mock_get_cinder_client, mock_image_id, mock_create_volume): - options = { - 'volume_name': 'yardstick_test_volume_01', - 'size': '256', - 'image': 'cirros-0.3.5' - } - args = {"options": options} - obj = CreateVolume(args, {}) - obj.run({}) - self.assertTrue(mock_create_volume.called) - self.assertTrue(mock_image_id.called) - self.assertTrue(mock_get_glance_client.called) - self.assertTrue(mock_get_cinder_client.called) + def test_run(self, mock_image_id, mock_create_volume): + self.scenario.run() + + mock_image_id.assert_called_once() + mock_create_volume.assert_called_once() + + @mock.patch.object( + yardstick.benchmark.scenarios.lib.create_volume.CreateVolume, 'setup') + def test_run_no_setup(self, scenario_setup): + self.scenario.setup_done = False + self.scenario.run() + scenario_setup.assert_called_once() + def main(): unittest.main() diff --git a/tests/unit/benchmark/scenarios/networking/test_netperf.py b/tests/unit/benchmark/scenarios/networking/test_netperf.py index d0f862fb5..d82a00931 100755 --- a/tests/unit/benchmark/scenarios/networking/test_netperf.py +++ b/tests/unit/benchmark/scenarios/networking/test_netperf.py @@ -48,7 +48,7 @@ class NetperfTestCase(unittest.TestCase): p.setup() self.assertIsNotNone(p.server) self.assertIsNotNone(p.client) - self.assertEqual(p.setup_done, True) + self.assertTrue(p.setup_done) def test_netperf_successful_no_sla(self, mock_ssh): diff --git a/tests/unit/benchmark/scenarios/networking/test_netperf_node.py b/tests/unit/benchmark/scenarios/networking/test_netperf_node.py index 62874cc44..8be9bb94d 100755 --- a/tests/unit/benchmark/scenarios/networking/test_netperf_node.py +++ b/tests/unit/benchmark/scenarios/networking/test_netperf_node.py @@ -48,7 +48,7 @@ class NetperfNodeTestCase(unittest.TestCase): p.setup() self.assertIsNotNone(p.server) self.assertIsNotNone(p.client) - self.assertEqual(p.setup_done, True) + self.assertTrue(p.setup_done) def test_netperf_node_successful_no_sla(self, mock_ssh): diff --git a/tests/unit/benchmark/scenarios/networking/test_nstat.py b/tests/unit/benchmark/scenarios/networking/test_nstat.py index fe44cfdf4..4b58e06c1 100644 --- a/tests/unit/benchmark/scenarios/networking/test_nstat.py +++ b/tests/unit/benchmark/scenarios/networking/test_nstat.py @@ -19,6 +19,7 @@ import mock from yardstick.benchmark.scenarios.networking import nstat + @mock.patch('yardstick.benchmark.scenarios.networking.nstat.ssh') class NstatTestCase(unittest.TestCase): @@ -38,7 +39,7 @@ class NstatTestCase(unittest.TestCase): mock_ssh.SSH.from_node().execute.return_value = (0, '', '') self.assertIsNotNone(n.client) - self.assertEqual(n.setup_done, True) + self.assertTrue(n.setup_done) def test_nstat_successful_no_sla(self, mock_ssh): @@ -51,17 +52,17 @@ class NstatTestCase(unittest.TestCase): n = nstat.Nstat(args, self.ctx) result = {} - sample_output = '#kernel\nIpInReceives 1837 0.0\nIpInHdrErrors 0 0.0\nIpInAddrErrors 2 0.0\nIcmpInMsgs 319 0.0\nIcmpInErrors 0 0.0\nTcpInSegs 36 0.0\nTcpInErrs 0 0.0\nUdpInDatagrams 1318 0.0\nUdpInErrors 0 0.0\n' + sample_output = '#kernel\nIpInReceives 1837 0.0\nIpInHdrErrors 0 0.0\nIpInAddrErrors 2 0.0\nIcmpInMsgs 319 0.0\nIcmpInErrors 0 0.0\nTcpInSegs 36 0.0\nTcpInErrs 0 0.0\nUdpInDatagrams 1318 0.0\nUdpInErrors 0 0.0\n' # pylint: disable=line-too-long mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '') n.run(result) expected_result = {"TcpInErrs": 0, "UdpInDatagrams": 1318, - "Tcp_segment_error_rate": 0.0, "IpInAddrErrors": 2, - "IpInHdrErrors": 0, "IcmpInErrors": 0, "IpErrors": 2, - "TcpInSegs": 36, "IpInReceives": 1837, "IcmpInMsgs": 319, - "IP_datagram_error_rate": 0.001, "Udp_datagram_error_rate": 0.0, - "Icmp_message_error_rate": 0.0, "UdpInErrors": 0} + "Tcp_segment_error_rate": 0.0, "IpInAddrErrors": 2, + "IpInHdrErrors": 0, "IcmpInErrors": 0, "IpErrors": 2, + "TcpInSegs": 36, "IpInReceives": 1837, "IcmpInMsgs": 319, + "IP_datagram_error_rate": 0.001, "Udp_datagram_error_rate": 0.0, + "Icmp_message_error_rate": 0.0, "UdpInErrors": 0} self.assertEqual(result, expected_result) def test_nstat_successful_sla(self, mock_ssh): @@ -79,17 +80,17 @@ class NstatTestCase(unittest.TestCase): n = nstat.Nstat(args, self.ctx) result = {} - sample_output = '#kernel\nIpInReceives 1837 0.0\nIpInHdrErrors 0 0.0\nIpInAddrErrors 2 0.0\nIcmpInMsgs 319 0.0\nIcmpInErrors 0 0.0\nTcpInSegs 36 0.0\nTcpInErrs 0 0.0\nUdpInDatagrams 1318 0.0\nUdpInErrors 0 0.0\n' + sample_output = '#kernel\nIpInReceives 1837 0.0\nIpInHdrErrors 0 0.0\nIpInAddrErrors 2 0.0\nIcmpInMsgs 319 0.0\nIcmpInErrors 0 0.0\nTcpInSegs 36 0.0\nTcpInErrs 0 0.0\nUdpInDatagrams 1318 0.0\nUdpInErrors 0 0.0\n' # pylint: disable=line-too-long mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '') n.run(result) expected_result = {"TcpInErrs": 0, "UdpInDatagrams": 1318, - "Tcp_segment_error_rate": 0.0, "IpInAddrErrors": 2, - "IpInHdrErrors": 0, "IcmpInErrors": 0, "IpErrors": 2, - "TcpInSegs": 36, "IpInReceives": 1837, "IcmpInMsgs": 319, - "IP_datagram_error_rate": 0.001, "Udp_datagram_error_rate": 0.0, - "Icmp_message_error_rate": 0.0, "UdpInErrors": 0} + "Tcp_segment_error_rate": 0.0, "IpInAddrErrors": 2, + "IpInHdrErrors": 0, "IcmpInErrors": 0, "IpErrors": 2, + "TcpInSegs": 36, "IpInReceives": 1837, "IcmpInMsgs": 319, + "IP_datagram_error_rate": 0.001, "Udp_datagram_error_rate": 0.0, + "Icmp_message_error_rate": 0.0, "UdpInErrors": 0} self.assertEqual(result, expected_result) def test_nstat_unsuccessful_cmd_error(self, mock_ssh): @@ -114,5 +115,6 @@ class NstatTestCase(unittest.TestCase): def main(): unittest.main() + if __name__ == '__main__': main() diff --git a/tests/unit/benchmark/scenarios/networking/test_ping6.py b/tests/unit/benchmark/scenarios/networking/test_ping6.py index ecce7cee5..d2be6f576 100644 --- a/tests/unit/benchmark/scenarios/networking/test_ping6.py +++ b/tests/unit/benchmark/scenarios/networking/test_ping6.py @@ -59,7 +59,7 @@ class PingTestCase(unittest.TestCase): mock_ssh.SSH.from_node().execute.return_value = (0, '0', '') p.setup() - self.assertEqual(p.setup_done, True) + self.assertTrue(p.setup_done) @mock.patch('yardstick.benchmark.scenarios.networking.ping6.ssh') def test_ping_successful_no_sla(self, mock_ssh): diff --git a/tests/unit/benchmark/scenarios/networking/test_pktgen.py b/tests/unit/benchmark/scenarios/networking/test_pktgen.py index 3928aacde..005b53177 100644 --- a/tests/unit/benchmark/scenarios/networking/test_pktgen.py +++ b/tests/unit/benchmark/scenarios/networking/test_pktgen.py @@ -50,7 +50,7 @@ class PktgenTestCase(unittest.TestCase): mock_ssh.SSH.from_node().execute.return_value = (0, '', '') self.assertIsNotNone(p.server) self.assertIsNotNone(p.client) - self.assertEqual(p.setup_done, True) + self.assertTrue(p.setup_done) def test_pktgen_successful_iptables_setup(self, mock_ssh): @@ -632,9 +632,13 @@ class PktgenTestCase(unittest.TestCase): def test_pktgen_run_with_setup_done(self, mock_ssh): args = { - 'options': {'packetsize': 60, 'number_of_ports': 10, 'duration': 20, 'multiqueue': True}, - 'sla': {'max_ppm': 1} - } + 'options': { + 'packetsize': 60, + 'number_of_ports': 10, + 'duration': 20, + 'multiqueue': True}, + 'sla': { + 'max_ppm': 1}} result = {} p = pktgen.Pktgen(args, self.ctx) p.server = mock_ssh.SSH.from_node() @@ -659,9 +663,13 @@ class PktgenTestCase(unittest.TestCase): def test_pktgen_run_with_ovs_multiqueque(self, mock_ssh): args = { - 'options': {'packetsize': 60, 'number_of_ports': 10, 'duration': 20, 'multiqueue': True}, - 'sla': {'max_ppm': 1} - } + 'options': { + 'packetsize': 60, + 'number_of_ports': 10, + 'duration': 20, + 'multiqueue': True}, + 'sla': { + 'max_ppm': 1}} result = {} p = pktgen.Pktgen(args, self.ctx) @@ -683,7 +691,7 @@ class PktgenTestCase(unittest.TestCase): mock_result3 = mock.Mock() mock_result3.return_value = 4 - p._enable_ovs_multiqueue = mock_result3 + p._enable_ovs_multiqueue = mock_result3 mock_result4 = mock.Mock() p._setup_irqmapping_ovs = mock_result4 @@ -704,9 +712,13 @@ class PktgenTestCase(unittest.TestCase): def test_pktgen_run_with_sriov_multiqueque(self, mock_ssh): args = { - 'options': {'packetsize': 60, 'number_of_ports': 10, 'duration': 20, 'multiqueue': True}, - 'sla': {'max_ppm': 1} - } + 'options': { + 'packetsize': 60, + 'number_of_ports': 10, + 'duration': 20, + 'multiqueue': True}, + 'sla': { + 'max_ppm': 1}} result = {} p = pktgen.Pktgen(args, self.ctx) @@ -739,8 +751,10 @@ class PktgenTestCase(unittest.TestCase): expected_result["packetsize"] = 60 self.assertEqual(result, expected_result) + def main(): unittest.main() + if __name__ == '__main__': main() diff --git a/tests/unit/benchmark/scenarios/networking/test_pktgen_dpdk.py b/tests/unit/benchmark/scenarios/networking/test_pktgen_dpdk.py index b4b87522c..c9eec4b94 100644 --- a/tests/unit/benchmark/scenarios/networking/test_pktgen_dpdk.py +++ b/tests/unit/benchmark/scenarios/networking/test_pktgen_dpdk.py @@ -20,8 +20,6 @@ import yardstick.common.utils as utils from yardstick.benchmark.scenarios.networking import pktgen_dpdk -@mock.patch('yardstick.benchmark.scenarios.networking.pktgen_dpdk.time') -@mock.patch('yardstick.benchmark.scenarios.networking.pktgen_dpdk.ssh') class PktgenDPDKLatencyTestCase(unittest.TestCase): def setUp(self): @@ -39,7 +37,20 @@ class PktgenDPDKLatencyTestCase(unittest.TestCase): } } - def test_pktgen_dpdk_successful_setup(self, mock_ssh, mock_time): + self._mock_ssh = mock.patch( + 'yardstick.benchmark.scenarios.networking.pktgen_dpdk.ssh') + self.mock_ssh = self._mock_ssh.start() + self._mock_time = mock.patch( + 'yardstick.benchmark.scenarios.networking.pktgen_dpdk.time') + self.mock_time = self._mock_time.start() + + self.addCleanup(self._stop_mock) + + def _stop_mock(self): + self._mock_ssh.stop() + self._mock_time.stop() + + def test_pktgen_dpdk_successful_setup(self): args = { 'options': {'packetsize': 60}, @@ -47,66 +58,66 @@ class PktgenDPDKLatencyTestCase(unittest.TestCase): p = pktgen_dpdk.PktgenDPDKLatency(args, self.ctx) p.setup() - mock_ssh.SSH.from_node().execute.return_value = (0, '', '') + self.mock_ssh.SSH.from_node().execute.return_value = (0, '', '') self.assertIsNotNone(p.server) self.assertIsNotNone(p.client) - self.assertEqual(p.setup_done, True) + self.assertTrue(p.setup_done) - def test_pktgen_dpdk_successful_get_port_ip(self, mock_ssh, mock_time): + def test_pktgen_dpdk_successful_get_port_ip(self): args = { 'options': {'packetsize': 60}, } p = pktgen_dpdk.PktgenDPDKLatency(args, self.ctx) - p.server = mock_ssh.SSH.from_node() + p.server = self.mock_ssh.SSH.from_node() - mock_ssh.SSH.from_node().execute.return_value = (0, '', '') + self.mock_ssh.SSH.from_node().execute.return_value = (0, '', '') utils.get_port_ip(p.server, "eth1") - mock_ssh.SSH.from_node().execute.assert_called_with( + self.mock_ssh.SSH.from_node().execute.assert_called_with( "ifconfig eth1 |grep 'inet addr' |awk '{print $2}' |cut -d ':' -f2 ") - def test_pktgen_dpdk_unsuccessful_get_port_ip(self, mock_ssh, mock_time): + def test_pktgen_dpdk_unsuccessful_get_port_ip(self): args = { 'options': {'packetsize': 60}, } p = pktgen_dpdk.PktgenDPDKLatency(args, self.ctx) - p.server = mock_ssh.SSH.from_node() + p.server = self.mock_ssh.SSH.from_node() - mock_ssh.SSH.from_node().execute.return_value = (1, '', 'FOOBAR') + self.mock_ssh.SSH.from_node().execute.return_value = (1, '', 'FOOBAR') self.assertRaises(RuntimeError, utils.get_port_ip, p.server, "eth1") - def test_pktgen_dpdk_successful_get_port_mac(self, mock_ssh, mock_time): + def test_pktgen_dpdk_successful_get_port_mac(self): args = { 'options': {'packetsize': 60}, } p = pktgen_dpdk.PktgenDPDKLatency(args, self.ctx) - p.server = mock_ssh.SSH.from_node() + p.server = self.mock_ssh.SSH.from_node() - mock_ssh.SSH.from_node().execute.return_value = (0, '', '') + self.mock_ssh.SSH.from_node().execute.return_value = (0, '', '') utils.get_port_mac(p.server, "eth1") - mock_ssh.SSH.from_node().execute.assert_called_with( + self.mock_ssh.SSH.from_node().execute.assert_called_with( "ifconfig |grep HWaddr |grep eth1 |awk '{print $5}' ") - def test_pktgen_dpdk_unsuccessful_get_port_mac(self, mock_ssh, mock_time): + def test_pktgen_dpdk_unsuccessful_get_port_mac(self): args = { 'options': {'packetsize': 60}, } p = pktgen_dpdk.PktgenDPDKLatency(args, self.ctx) - p.server = mock_ssh.SSH.from_node() + p.server = self.mock_ssh.SSH.from_node() - mock_ssh.SSH.from_node().execute.return_value = (1, '', 'FOOBAR') + self.mock_ssh.SSH.from_node().execute.return_value = (1, '', 'FOOBAR') self.assertRaises(RuntimeError, utils.get_port_mac, p.server, "eth1") - def test_pktgen_dpdk_successful_no_sla(self, mock_ssh, mock_time): + def test_pktgen_dpdk_successful_no_sla(self): args = { 'options': {'packetsize': 60}, @@ -116,7 +127,7 @@ class PktgenDPDKLatencyTestCase(unittest.TestCase): p = pktgen_dpdk.PktgenDPDKLatency(args, self.ctx) sample_output = '100\n110\n112\n130\n149\n150\n90\n150\n200\n162\n' - mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '') + self.mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '') p.run(result) # with python 3 we get float, might be due python division changes @@ -125,7 +136,7 @@ class PktgenDPDKLatencyTestCase(unittest.TestCase): delta = result['avg_latency'] - 132 self.assertLessEqual(delta, 1) - def test_pktgen_dpdk_successful_sla(self, mock_ssh, mock_time): + def test_pktgen_dpdk_successful_sla(self): args = { 'options': {'packetsize': 60}, @@ -136,13 +147,13 @@ class PktgenDPDKLatencyTestCase(unittest.TestCase): p = pktgen_dpdk.PktgenDPDKLatency(args, self.ctx) sample_output = '100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n' - mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '') + self.mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '') p.run(result) self.assertEqual(result, {"avg_latency": 100}) - def test_pktgen_dpdk_unsuccessful_sla(self, mock_ssh, mock_time): + def test_pktgen_dpdk_unsuccessful_sla(self): args = { 'options': {'packetsize': 60}, @@ -152,14 +163,14 @@ class PktgenDPDKLatencyTestCase(unittest.TestCase): p = pktgen_dpdk.PktgenDPDKLatency(args, self.ctx) - p.server = mock_ssh.SSH.from_node() - p.client = mock_ssh.SSH.from_node() + p.server = self.mock_ssh.SSH.from_node() + p.client = self.mock_ssh.SSH.from_node() sample_output = '100\n110\n112\n130\n149\n150\n90\n150\n200\n162\n' - mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '') + self.mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '') self.assertRaises(AssertionError, p.run, result) - def test_pktgen_dpdk_unsuccessful_script_error(self, mock_ssh, mock_time): + def test_pktgen_dpdk_unsuccessful_script_error(self): args = { 'options': {'packetsize': 60}, @@ -169,7 +180,7 @@ class PktgenDPDKLatencyTestCase(unittest.TestCase): p = pktgen_dpdk.PktgenDPDKLatency(args, self.ctx) - mock_ssh.SSH.from_node().execute.return_value = (1, '', 'FOOBAR') + self.mock_ssh.SSH.from_node().execute.return_value = (1, '', 'FOOBAR') self.assertRaises(RuntimeError, p.run, result) diff --git a/tests/unit/benchmark/scenarios/networking/test_pktgen_dpdk_throughput.py b/tests/unit/benchmark/scenarios/networking/test_pktgen_dpdk_throughput.py index d34097008..c2e35af75 100644 --- a/tests/unit/benchmark/scenarios/networking/test_pktgen_dpdk_throughput.py +++ b/tests/unit/benchmark/scenarios/networking/test_pktgen_dpdk_throughput.py @@ -20,7 +20,6 @@ from yardstick.benchmark.scenarios.networking import pktgen_dpdk_throughput @mock.patch('yardstick.benchmark.scenarios.networking.pktgen_dpdk_throughput.ssh') -@mock.patch('yardstick.benchmark.scenarios.networking.pktgen_dpdk_throughput.time') class PktgenDPDKTestCase(unittest.TestCase): def setUp(self): @@ -37,7 +36,16 @@ class PktgenDPDKTestCase(unittest.TestCase): } } - def test_pktgen_dpdk_throughput_successful_setup(self, mock__time, mock_ssh): + self._mock_time = mock.patch( + 'yardstick.benchmark.scenarios.networking.pktgen_dpdk_throughput.time') + self.mock_time = self._mock_time.start() + + self.addCleanup(self._cleanup) + + def _cleanup(self): + self._mock_time.stop() + + def test_pktgen_dpdk_throughput_successful_setup(self, mock_ssh): args = { 'options': {'packetsize': 60}, } @@ -47,9 +55,9 @@ class PktgenDPDKTestCase(unittest.TestCase): mock_ssh.SSH().execute.return_value = (0, '', '') self.assertIsNotNone(p.server) self.assertIsNotNone(p.client) - self.assertEqual(p.setup_done, True) + self.assertTrue(p.setup_done) - def test_pktgen_dpdk_throughput_successful_no_sla(self, mock__time, mock_ssh): + def test_pktgen_dpdk_throughput_successful_no_sla(self, mock_ssh): args = { 'options': {'packetsize': 60, 'number_of_ports': 10}, } @@ -75,7 +83,7 @@ class PktgenDPDKTestCase(unittest.TestCase): expected_result["packetsize"] = 60 self.assertEqual(result, expected_result) - def test_pktgen_dpdk_throughput_successful_sla(self, mock__time, mock_ssh): + def test_pktgen_dpdk_throughput_successful_sla(self, mock_ssh): args = { 'options': {'packetsize': 60, 'number_of_ports': 10}, 'sla': {'max_ppm': 10000} @@ -101,7 +109,7 @@ class PktgenDPDKTestCase(unittest.TestCase): expected_result["packetsize"] = 60 self.assertEqual(result, expected_result) - def test_pktgen_dpdk_throughput_unsuccessful_sla(self, mock__time, mock_ssh): + def test_pktgen_dpdk_throughput_unsuccessful_sla(self, mock_ssh): args = { 'options': {'packetsize': 60, 'number_of_ports': 10}, 'sla': {'max_ppm': 1000} @@ -122,7 +130,8 @@ class PktgenDPDKTestCase(unittest.TestCase): mock_ssh.SSH().execute.return_value = (0, sample_output, '') self.assertRaises(AssertionError, p.run, result) - def test_pktgen_dpdk_throughput_unsuccessful_script_error(self, mock__time, mock_ssh): + def test_pktgen_dpdk_throughput_unsuccessful_script_error( + self, mock_ssh): args = { 'options': {'packetsize': 60, 'number_of_ports': 10}, 'sla': {'max_ppm': 1000} @@ -137,7 +146,7 @@ class PktgenDPDKTestCase(unittest.TestCase): mock_ssh.SSH().execute.return_value = (1, '', 'FOOBAR') self.assertRaises(RuntimeError, p.run, result) - def test_pktgen_dpdk_throughput_is_dpdk_setup(self, mock__time, mock_ssh): + def test_pktgen_dpdk_throughput_is_dpdk_setup(self, mock_ssh): args = { 'options': {'packetsize': 60}, } @@ -151,7 +160,7 @@ class PktgenDPDKTestCase(unittest.TestCase): mock_ssh.SSH().execute.assert_called_with( "ip a | grep eth1 2>/dev/null") - def test_pktgen_dpdk_throughput_dpdk_setup(self, mock__time, mock_ssh): + def test_pktgen_dpdk_throughput_dpdk_setup(self, mock_ssh): args = { 'options': {'packetsize': 60}, } @@ -163,9 +172,9 @@ class PktgenDPDKTestCase(unittest.TestCase): p.dpdk_setup() - self.assertEqual(p.dpdk_setup_done, True) + self.assertTrue(p.dpdk_setup_done) - def test_pktgen_dpdk_throughput_dpdk_get_result(self, mock__time, mock_ssh): + def test_pktgen_dpdk_throughput_dpdk_get_result(self, mock_ssh): args = { 'options': {'packetsize': 60}, } @@ -180,8 +189,10 @@ class PktgenDPDKTestCase(unittest.TestCase): mock_ssh.SSH().execute.assert_called_with( "sudo /dpdk/destdir/bin/dpdk-procinfo -- --stats-reset > /dev/null 2>&1") + def main(): unittest.main() + if __name__ == '__main__': main() diff --git a/tests/unit/benchmark/scenarios/networking/test_vsperf.py b/tests/unit/benchmark/scenarios/networking/test_vsperf.py index cbbfc2b34..be8ac55d0 100644 --- a/tests/unit/benchmark/scenarios/networking/test_vsperf.py +++ b/tests/unit/benchmark/scenarios/networking/test_vsperf.py @@ -66,7 +66,7 @@ class VsperfTestCase(unittest.TestCase): p.setup() self.assertIsNotNone(p.client) - self.assertEqual(p.setup_done, True) + self.assertTrue(p.setup_done) def test_vsperf_teardown(self, mock_ssh, mock_subprocess): p = vsperf.Vsperf(self.args, self.ctx) @@ -77,10 +77,10 @@ class VsperfTestCase(unittest.TestCase): p.setup() self.assertIsNotNone(p.client) - self.assertEqual(p.setup_done, True) + self.assertTrue(p.setup_done) p.teardown() - self.assertEqual(p.setup_done, False) + self.assertFalse(p.setup_done) def test_vsperf_run_ok(self, mock_ssh, mock_subprocess): p = vsperf.Vsperf(self.args, self.ctx) diff --git a/tests/unit/benchmark/scenarios/networking/test_vsperf_dpdk.py b/tests/unit/benchmark/scenarios/networking/test_vsperf_dpdk.py index 5759f0a90..fbe3ed804 100644 --- a/tests/unit/benchmark/scenarios/networking/test_vsperf_dpdk.py +++ b/tests/unit/benchmark/scenarios/networking/test_vsperf_dpdk.py @@ -27,7 +27,6 @@ from yardstick.benchmark.scenarios.networking import vsperf_dpdk @mock.patch('yardstick.benchmark.scenarios.networking.vsperf_dpdk.subprocess') -@mock.patch('yardstick.benchmark.scenarios.networking.vsperf_dpdk.ssh') class VsperfDPDKTestCase(unittest.TestCase): def setUp(self): @@ -63,7 +62,16 @@ class VsperfDPDKTestCase(unittest.TestCase): } } - def test_vsperf_dpdk_setup(self, mock_ssh, mock_subprocess): + self._mock_ssh = mock.patch( + 'yardstick.benchmark.scenarios.networking.vsperf_dpdk.ssh') + self.mock_ssh = self._mock_ssh.start() + + self.addCleanup(self._cleanup) + + def _cleanup(self): + self._mock_ssh.stop() + + def test_vsperf_dpdk_setup(self, mock_subprocess): p = vsperf_dpdk.VsperfDPDK(self.args, self.ctx) # setup() specific mocks @@ -71,9 +79,9 @@ class VsperfDPDKTestCase(unittest.TestCase): p.setup() self.assertIsNotNone(p.client) - self.assertEqual(p.setup_done, True) + self.assertTrue(p.setup_done) - def test_vsperf_dpdk_teardown(self, mock_ssh, mock_subprocess): + def test_vsperf_dpdk_teardown(self, mock_subprocess): p = vsperf_dpdk.VsperfDPDK(self.args, self.ctx) # setup() specific mocks @@ -81,12 +89,12 @@ class VsperfDPDKTestCase(unittest.TestCase): p.setup() self.assertIsNotNone(p.client) - self.assertEqual(p.setup_done, True) + self.assertTrue(p.setup_done) p.teardown() - self.assertEqual(p.setup_done, False) + self.assertFalse(p.setup_done) - def test_vsperf_dpdk_is_dpdk_setup_no(self, mock_ssh, mock_subprocess): + def test_vsperf_dpdk_is_dpdk_setup_no(self, mock_subprocess): p = vsperf_dpdk.VsperfDPDK(self.args, self.ctx) # setup() specific mocks @@ -94,15 +102,15 @@ class VsperfDPDKTestCase(unittest.TestCase): p.setup() self.assertIsNotNone(p.client) - self.assertEqual(p.setup_done, True) + self.assertTrue(p.setup_done) # is_dpdk_setup() specific mocks - mock_ssh.SSH.from_node().execute.return_value = (0, 'dummy', '') + self.mock_ssh.SSH.from_node().execute.return_value = (0, 'dummy', '') result = p._is_dpdk_setup() - self.assertEqual(result, False) + self.assertFalse(result) - def test_vsperf_dpdk_is_dpdk_setup_yes(self, mock_ssh, mock_subprocess): + def test_vsperf_dpdk_is_dpdk_setup_yes(self, mock_subprocess): p = vsperf_dpdk.VsperfDPDK(self.args, self.ctx) # setup() specific mocks @@ -110,16 +118,16 @@ class VsperfDPDKTestCase(unittest.TestCase): p.setup() self.assertIsNotNone(p.client) - self.assertEqual(p.setup_done, True) + self.assertTrue(p.setup_done) # is_dpdk_setup() specific mocks - mock_ssh.SSH.from_node().execute.return_value = (0, '', '') + self.mock_ssh.SSH.from_node().execute.return_value = (0, '', '') result = p._is_dpdk_setup() - self.assertEqual(result, True) + self.assertTrue(result) - @mock.patch('yardstick.benchmark.scenarios.networking.vsperf_dpdk.time') - def test_vsperf_dpdk_dpdk_setup_first(self, mock_time, mock_ssh, mock_subprocess): + @mock.patch('time.sleep') + def test_vsperf_dpdk_dpdk_setup_first(self, _, mock_subprocess): p = vsperf_dpdk.VsperfDPDK(self.args, self.ctx) # setup() specific mocks @@ -127,61 +135,61 @@ class VsperfDPDKTestCase(unittest.TestCase): p.setup() self.assertIsNotNone(p.client) - self.assertEqual(p.setup_done, True) + self.assertTrue(p.setup_done) # is_dpdk_setup() specific mocks - mock_ssh.SSH.from_node().execute.return_value = (0, 'dummy', '') + self.mock_ssh.SSH.from_node().execute.return_value = (0, 'dummy', '') p.dpdk_setup() - self.assertEqual(p._is_dpdk_setup(), False) - self.assertEqual(p.dpdk_setup_done, True) + self.assertFalse(p._is_dpdk_setup()) + self.assertTrue(p.dpdk_setup_done) - @mock.patch('yardstick.benchmark.scenarios.networking.vsperf_dpdk.time') - def test_vsperf_dpdk_dpdk_setup_next(self, mock_time, mock_ssh, mock_subprocess): + @mock.patch('time.sleep') + def test_vsperf_dpdk_dpdk_setup_next(self, _, mock_subprocess): p = vsperf_dpdk.VsperfDPDK(self.args, self.ctx) # setup() specific mocks - mock_ssh.SSH.from_node().execute.return_value = (0, '', '') + self.mock_ssh.SSH.from_node().execute.return_value = (0, '', '') mock_subprocess.call().execute.return_value = None p.setup() self.assertIsNotNone(p.client) - self.assertEqual(p.setup_done, True) + self.assertTrue(p.setup_done) p.dpdk_setup() - self.assertEqual(p._is_dpdk_setup(), True) - self.assertEqual(p.dpdk_setup_done, True) + self.assertTrue(p._is_dpdk_setup()) + self.assertTrue(p.dpdk_setup_done) - @mock.patch('yardstick.benchmark.scenarios.networking.vsperf_dpdk.time') - def test_vsperf_dpdk_dpdk_setup_fail(self, mock_time, mock_ssh, mock_subprocess): + @mock.patch('time.sleep') + def test_vsperf_dpdk_dpdk_setup_fail(self, _, mock_subprocess): p = vsperf_dpdk.VsperfDPDK(self.args, self.ctx) # setup() specific mocks - mock_ssh.SSH.from_node().execute.return_value = (0, '', '') + self.mock_ssh.SSH.from_node().execute.return_value = (0, '', '') mock_subprocess.call().execute.return_value = None p.setup() self.assertIsNotNone(p.client) - mock_ssh.SSH.from_node().execute.return_value = (1, '', '') - self.assertEqual(p.setup_done, True) + self.mock_ssh.SSH.from_node().execute.return_value = (1, '', '') + self.assertTrue(p.setup_done) self.assertRaises(RuntimeError, p.dpdk_setup) - @mock.patch('yardstick.benchmark.scenarios.networking.vsperf_dpdk.time') - def test_vsperf_dpdk_run_ok(self, mock_time, mock_ssh, mock_subprocess): + @mock.patch('time.sleep') + def test_vsperf_dpdk_run_ok(self, _, mock_subprocess): p = vsperf_dpdk.VsperfDPDK(self.args, self.ctx) # setup() specific mocks - mock_ssh.SSH.from_node().execute.return_value = (0, '', '') + self.mock_ssh.SSH.from_node().execute.return_value = (0, '', '') mock_subprocess.call().execute.return_value = None p.setup() self.assertIsNotNone(p.client) - self.assertEqual(p.setup_done, True) + self.assertTrue(p.setup_done) # run() specific mocks mock_subprocess.call().execute.return_value = None - mock_ssh.SSH.from_node().execute.return_value = ( + self.mock_ssh.SSH.from_node().execute.return_value = ( 0, 'throughput_rx_fps\r\n14797660.000\r\n', '') result = {} @@ -189,42 +197,41 @@ class VsperfDPDKTestCase(unittest.TestCase): self.assertEqual(result['throughput_rx_fps'], '14797660.000') - def test_vsperf_dpdk_run_falied_vsperf_execution(self, mock_ssh, - mock_subprocess): + def test_vsperf_dpdk_run_falied_vsperf_execution(self, mock_subprocess): p = vsperf_dpdk.VsperfDPDK(self.args, self.ctx) # setup() specific mocks - mock_ssh.SSH.from_node().execute.return_value = (0, '', '') + self.mock_ssh.SSH.from_node().execute.return_value = (0, '', '') mock_subprocess.call().execute.return_value = None p.setup() self.assertIsNotNone(p.client) - self.assertEqual(p.setup_done, True) + self.assertTrue(p.setup_done) # run() specific mocks mock_subprocess.call().execute.return_value = None mock_subprocess.call().execute.return_value = None - mock_ssh.SSH.from_node().execute.return_value = (1, '', '') + self.mock_ssh.SSH.from_node().execute.return_value = (1, '', '') result = {} self.assertRaises(RuntimeError, p.run, result) - def test_vsperf_dpdk_run_falied_csv_report(self, mock_ssh, mock_subprocess): + def test_vsperf_dpdk_run_falied_csv_report(self, mock_subprocess): p = vsperf_dpdk.VsperfDPDK(self.args, self.ctx) # setup() specific mocks - mock_ssh.SSH.from_node().execute.return_value = (0, '', '') + self.mock_ssh.SSH.from_node().execute.return_value = (0, '', '') mock_subprocess.call().execute.return_value = None p.setup() self.assertIsNotNone(p.client) - self.assertEqual(p.setup_done, True) + self.assertTrue(p.setup_done) # run() specific mocks mock_subprocess.call().execute.return_value = None mock_subprocess.call().execute.return_value = None - mock_ssh.SSH.from_node().execute.return_value = (0, '', '') - mock_ssh.SSH.from_node().execute.return_value = (1, '', '') + self.mock_ssh.SSH.from_node().execute.return_value = (0, '', '') + self.mock_ssh.SSH.from_node().execute.return_value = (1, '', '') result = {} self.assertRaises(RuntimeError, p.run, result) diff --git a/tests/unit/benchmark/scenarios/parser/test_parser.py b/tests/unit/benchmark/scenarios/parser/test_parser.py index 59b98a092..ee2bbc07d 100644 --- a/tests/unit/benchmark/scenarios/parser/test_parser.py +++ b/tests/unit/benchmark/scenarios/parser/test_parser.py @@ -9,50 +9,67 @@ # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## -# Unittest for yardstick.benchmark.scenarios.parser.Parser - -from __future__ import absolute_import +import subprocess import unittest - import mock + from oslo_serialization import jsonutils from yardstick.benchmark.scenarios.parser import parser -@mock.patch('yardstick.benchmark.scenarios.parser.parser.subprocess') class ParserTestCase(unittest.TestCase): def setUp(self): - pass - - def test_parser_successful_setup(self, mock_subprocess): - - p = parser.Parser({}, {}) - mock_subprocess.call().return_value = 0 - p.setup() - self.assertEqual(p.setup_done, True) - - def test_parser_successful(self, mock_subprocess): args = { 'options': {'yangfile': '/root/yardstick/samples/yang.yaml', 'toscafile': '/root/yardstick/samples/tosca.yaml'}, } - p = parser.Parser(args, {}) + self.scenario = parser.Parser(scenario_cfg=args, context_cfg={}) + + self._mock_popen = mock.patch.object(subprocess, 'Popen') + self.mock_popen = self._mock_popen.start() + self._mock_call = mock.patch.object(subprocess, 'call') + self.mock_call = self._mock_call.start() + + self.addCleanup(self._stop_mock) + + def _stop_mock(self): + self._mock_popen.stop() + self._mock_call.stop() + + def test_setup_successful(self): + + self.mock_call.return_value = 0 + self.scenario.setup() + self.assertTrue(self.scenario.setup_done) + + def test_run_successful(self): + result = {} - mock_subprocess.call().return_value = 0 - sample_output = '{"yangtotosca": "success"}' - p.run(result) - expected_result = jsonutils.loads(sample_output) + self.mock_popen().returncode = 0 + + expected_result = jsonutils.loads('{"yangtotosca": "success"}') + + self.scenario.run(result) + self.assertEqual(result, expected_result) + + def test_run_fail(self): + result = {} + + self.mock_popen().returncode = 1 + expected_result = jsonutils.loads('{"yangtotosca": "fail"}') + + self.scenario.run(result) + self.assertEqual(result, expected_result) - def test_parser_teardown_successful(self, mock_subprocess): + def test_teardown_successful(self): - p = parser.Parser({}, {}) - mock_subprocess.call().return_value = 0 - p.teardown() - self.assertEqual(p.teardown_done, True) + self.mock_call.return_value = 0 + self.scenario.teardown() + self.assertTrue(self.scenario.teardown_done) def main(): diff --git a/tests/unit/benchmark/scenarios/storage/test_fio.py b/tests/unit/benchmark/scenarios/storage/test_fio.py index 17594b9f4..0cffea224 100644 --- a/tests/unit/benchmark/scenarios/storage/test_fio.py +++ b/tests/unit/benchmark/scenarios/storage/test_fio.py @@ -53,7 +53,7 @@ class FioTestCase(unittest.TestCase): mock_ssh.SSH.from_node().execute.return_value = (0, '', '') self.assertIsNotNone(p.client) - self.assertEqual(p.setup_done, True) + self.assertTrue(p.setup_done) def test_fio_job_file_successful_setup(self, mock_ssh): @@ -67,7 +67,7 @@ class FioTestCase(unittest.TestCase): mock_ssh.SSH.from_node().execute.return_value = (0, '', '') self.assertIsNotNone(p.client) - self.assertEqual(p.setup_done, True) + self.assertTrue(p.setup_done) def test_fio_successful_no_sla(self, mock_ssh): |