From e9ca30e4de8c5b3353ef2a0e40390b2954ff04fd Mon Sep 17 00:00:00 2001 From: "Chris St. Pierre" Date: Wed, 17 Jun 2015 11:54:22 -0500 Subject: [PATCH] Fix EC2 utils and tests The EC2 utils included an unnecessary implementation of rally.benchmark.utils.resource_is(), which has been removed. The unit tests were redundant and overwrought, too; they've been fixed. Change-Id: Id69ae64580756ab33e3b1a9cfed01daf1d93a023 --- .../plugins/openstack/scenarios/ec2/utils.py | 8 +---- .../openstack/scenarios/ec2/test_servers.py | 27 ++++------------- .../openstack/scenarios/ec2/test_utils.py | 30 +++++-------------- 3 files changed, 14 insertions(+), 51 deletions(-) diff --git a/rally/plugins/openstack/scenarios/ec2/utils.py b/rally/plugins/openstack/scenarios/ec2/utils.py index 392bf15cf0..6beed2bc36 100644 --- a/rally/plugins/openstack/scenarios/ec2/utils.py +++ b/rally/plugins/openstack/scenarios/ec2/utils.py @@ -44,12 +44,6 @@ benchmark_group = cfg.OptGroup(name="benchmark", CONF.register_opts(EC2_BENCHMARK_OPTS, group=benchmark_group) -def ec2_resource_is(status): - """Check status for EC2.""" - - return lambda resource: resource.state.upper() == status.upper() - - class EC2Scenario(base.Scenario): """Base class for EC2 scenarios with basic atomic actions.""" @@ -74,7 +68,7 @@ class EC2Scenario(base.Scenario): time.sleep(CONF.benchmark.ec2_server_boot_prepoll_delay) server = utils.wait_for( server, - is_ready=ec2_resource_is("RUNNING"), + is_ready=utils.resource_is("RUNNING"), update_resource=self._update_resource, timeout=CONF.benchmark.ec2_server_boot_timeout, check_interval=CONF.benchmark.ec2_server_boot_poll_interval diff --git a/tests/unit/plugins/openstack/scenarios/ec2/test_servers.py b/tests/unit/plugins/openstack/scenarios/ec2/test_servers.py index 19882b0e2b..2197a73bb8 100644 --- a/tests/unit/plugins/openstack/scenarios/ec2/test_servers.py +++ b/tests/unit/plugins/openstack/scenarios/ec2/test_servers.py @@ -18,28 +18,11 @@ from rally.plugins.openstack.scenarios.ec2 import servers from tests.unit import test -UTILS = "rally.plugins.openstack.scenarios.ec2.utils." - - class EC2ServersTestCase(test.ScenarioTestCase): - @mock.patch(UTILS + "ec2_resource_is", return_value="foo_state") - @mock.patch(UTILS + "CONF") - def test_boot_server(self, mock_conf, mock_ec2_resource_is): - mock_conf.benchmark.ec2_server_boot_prepoll_delay = "foo_delay" - mock_conf.benchmark.ec2_server_boot_timeout = "foo_timeout" - mock_conf.benchmark.ec2_server_boot_poll_interval = "foo_interval" - + def test_boot_server(self): scenario = servers.EC2Servers() - scenario._update_resource = "foo_update" - mock_instances = mock.Mock(instances=["foo_inst"]) - self.clients("ec2").run_instances.return_value = mock_instances - server = scenario._boot_server("foo_image", "foo_flavor", foo="bar") - - self.mock_wait_for.mock.assert_called_once_with( - "foo_inst", is_ready="foo_state", - update_resource="foo_update", - timeout="foo_timeout", - check_interval="foo_interval") - self.mock_sleep.mock.assert_called_once_with("foo_delay") - self.assertEqual(server, self.mock_wait_for.mock.return_value) + scenario._boot_server = mock.Mock() + scenario.boot_server("foo_image", "foo_flavor", foo="bar") + scenario._boot_server.assert_called_once_with( + "foo_image", "foo_flavor", foo="bar") diff --git a/tests/unit/plugins/openstack/scenarios/ec2/test_utils.py b/tests/unit/plugins/openstack/scenarios/ec2/test_utils.py index 17ff1e3a90..e2c3d71af1 100644 --- a/tests/unit/plugins/openstack/scenarios/ec2/test_utils.py +++ b/tests/unit/plugins/openstack/scenarios/ec2/test_utils.py @@ -14,7 +14,6 @@ import mock from oslo_config import cfg -from oslotest import mockpatch from rally.plugins.openstack.scenarios.ec2 import utils from tests.unit import test @@ -23,44 +22,31 @@ EC2_UTILS = "rally.plugins.openstack.scenarios.ec2.utils" CONF = cfg.CONF -class EC2UtilsTestCase(test.TestCase): - - def test_ec2_resource_is(self): - resource = mock.MagicMock(state="RUNNING") - resource_is = utils.ec2_resource_is("RUNNING") - self.assertTrue(resource_is(resource)) - resource.state = "PENDING" - self.assertFalse(resource_is(resource)) - - def test__update_resource(self): - resource = mock.MagicMock() - utils.EC2Scenario()._update_resource(resource) - resource.update.assert_called_once_with() - - class EC2ScenarioTestCase(test.ScenarioTestCase): def setUp(self): super(EC2ScenarioTestCase, self).setUp() self.server = mock.MagicMock() self.reservation = mock.MagicMock(instances=[self.server]) - self.mock_resource_is = mockpatch.Patch(EC2_UTILS + ".ec2_resource_is") - self.mock_update_resource = mockpatch.Patch( - EC2_UTILS + ".EC2Scenario._update_resource") - self.useFixture(self.mock_resource_is) - self.useFixture(self.mock_update_resource) def test__boot_server(self): self.clients("ec2").run_instances.return_value = self.reservation ec2_scenario = utils.EC2Scenario(context={}) + ec2_scenario._update_resource = mock.Mock() return_server = ec2_scenario._boot_server("image", "flavor") self.mock_wait_for.mock.assert_called_once_with( self.server, is_ready=self.mock_resource_is.mock.return_value, - update_resource=self.mock_update_resource.mock, + update_resource=ec2_scenario._update_resource, check_interval=CONF.benchmark.ec2_server_boot_poll_interval, timeout=CONF.benchmark.ec2_server_boot_timeout) self.mock_resource_is.mock.assert_called_once_with("RUNNING") self.assertEqual(self.mock_wait_for.mock.return_value, return_server) self._test_atomic_action_timer(ec2_scenario.atomic_actions(), "ec2.boot_server") + + def test__update_resource(self): + resource = mock.MagicMock() + scenario = utils.EC2Scenario() + self.assertEqual(scenario._update_resource(resource), resource) + resource.update.assert_called_once_with()