From 70ae62560a0f27b9232187949779fa415b8264ad Mon Sep 17 00:00:00 2001 From: Maksym Iarmak Date: Wed, 15 Jan 2014 14:37:53 +0200 Subject: [PATCH] Fix tests/benchmark/scenarios/nova/test_utils.py One test per method with proper mocking. Change-Id: Ib0762d1034c71efc9751e81b75a6f2f0755d40d7 Related: blueprint unit-tests-refactoring --- tests/benchmark/scenarios/nova/test_utils.py | 106 +++++++++---------- 1 file changed, 50 insertions(+), 56 deletions(-) diff --git a/tests/benchmark/scenarios/nova/test_utils.py b/tests/benchmark/scenarios/nova/test_utils.py index 5f700cc26e..32e2938d2f 100644 --- a/tests/benchmark/scenarios/nova/test_utils.py +++ b/tests/benchmark/scenarios/nova/test_utils.py @@ -55,59 +55,53 @@ class NovaScenarioTestCase(test.TestCase): butils.get_from_manager(), server_manager.create('fails', '1', '2')) - @mock.patch("rally.utils") - @mock.patch(BM_UTILS + ".osclients") - def test_server_helper_methods(self, mock_osclients, mock_rally_utils): - def _is_ready(resource): - return resource.status == "ACTIVE" + @mock.patch(NOVA_UTILS + '.NovaScenario.clients') + def test__boot_server(self, mock_clients): + mock_clients("nova").servers.create.return_value = self.server + return_server = utils.NovaScenario()._boot_server('server_name', + 'image_id', + 'flavor_id') + self.wait_for.mock.assert_called_once_with(self.server, + update_resource=self.gfm(), + is_ready=self.res_is.mock(), + check_interval=3, + timeout=600) + self.res_is.mock.assert_has_calls(mock.call('ACTIVE')) + self.assertEqual(self.wait_for.mock(), return_server) - self.res_is.mock.return_value = _is_ready - get_from_mgr = butils.get_from_manager() + def test__suspend_server(self): + utils.NovaScenario()._suspend_server(self.server) + self.server.suspend.assert_called_once_with() + self.wait_for.mock.assert_called_once_with(self.server, + update_resource=self.gfm(), + is_ready=self.res_is.mock(), + check_interval=3, + timeout=600) + self.res_is.mock.assert_has_calls(mock.call('SUSPENDED')) - fc = fakes.FakeClients() - mock_osclients.Clients.return_value = fc - fake_nova = fakes.FakeNovaClient() - fc.get_nova_client = lambda: fake_nova - fsm = fakes.FakeServerManager(fake_nova.images) - fake_server = fsm.create("s1", "i1", 1) - fsm.create = lambda name, iid, fid, **kwargs: fake_server - fake_nova.servers = fsm - fake_image_id = fsm.create_image(fake_server, 'img') - fake_image = fsm.images.get(fake_image_id) - fsm.create_image = lambda svr, name: fake_image.id - temp_keys = ["username", "password", "tenant_name", "auth_url"] - users_endpoints = [dict(zip(temp_keys, temp_keys))] + @mock.patch(NOVA_UTILS + '.NovaScenario.clients') + def test__create_image(self, mock_clients): + mock_clients("nova").images.get.return_value = self.image + return_image = utils.NovaScenario()._create_image(self.server) + self.wait_for.mock.assert_called_once_with(self.image, + update_resource=self.gfm(), + is_ready=self.res_is.mock(), + check_interval=3, + timeout=600) + self.res_is.mock.assert_has_calls(mock.call('ACTIVE')) + self.assertEqual(self.wait_for.mock(), return_image) - tmp_clients = butils.create_openstack_clients(users_endpoints, - temp_keys)[0] - novascenario = utils.NovaScenario(clients=tmp_clients) + @mock.patch(BM_UTILS + ".is_none") + def test__delete_server(self, mock_isnone): + utils.NovaScenario()._delete_server(self.server) + self.server.delete.assert_called_once_with() + self.wait_for.mock.assert_called_once_with(self.server, + is_ready=mock_isnone, + update_resource=self.gfm(), + check_interval=3, + timeout=600) - utils.utils = mock_rally_utils - utils.bench_utils.get_from_manager = lambda: get_from_mgr - - novascenario._boot_server("s1", "i1", 1) - novascenario._create_image(fake_server) - novascenario._suspend_server(fake_server) - novascenario._delete_server(fake_server) - - expected = [ - mock.call.wait_for(fake_server, is_ready=_is_ready, - update_resource=butils.get_from_manager(), - check_interval=3, timeout=600), - mock.call.wait_for(fake_image, is_ready=_is_ready, - update_resource=butils.get_from_manager(), - check_interval=3, timeout=600), - mock.call.wait_for(fake_server, is_ready=_is_ready, - update_resource=butils.get_from_manager(), - check_interval=3, timeout=600), - mock.call.wait_for(fake_server, is_ready=butils.is_none, - update_resource=butils.get_from_manager(), - check_interval=3, timeout=600) - ] - - self.assertEqual(expected, mock_rally_utils.mock_calls) - - def test_server_reboot(self): + def test__reboot_server(self): utils.NovaScenario()._reboot_server(self.server) self.server.reboot.assert_called_once_with(reboot_type='SOFT') self.wait_for.mock.assert_called_once_with(self.server, @@ -117,7 +111,7 @@ class NovaScenarioTestCase(test.TestCase): timeout=600) self.res_is.mock.assert_has_calls(mock.call('ACTIVE')) - def test_server_start(self): + def test__start_server(self): utils.NovaScenario()._start_server(self.server) self.server.start.assert_called_once_with() self.wait_for.mock.assert_called_once_with(self.server, @@ -127,7 +121,7 @@ class NovaScenarioTestCase(test.TestCase): timeout=600) self.res_is.mock.assert_has_calls(mock.call('ACTIVE')) - def test_server_stop(self): + def test__stop_server(self): utils.NovaScenario()._stop_server(self.server) self.server.stop.assert_called_once_with() self.wait_for.mock.assert_called_once_with(self.server, @@ -137,7 +131,7 @@ class NovaScenarioTestCase(test.TestCase): timeout=600) self.res_is.mock.assert_has_calls(mock.call('SHUTOFF')) - def test_server_rescue(self): + def test__rescue_server(self): utils.NovaScenario()._rescue_server(self.server) self.server.rescue.assert_called_once_with() self.wait_for.mock.assert_called_once_with(self.server, @@ -147,7 +141,7 @@ class NovaScenarioTestCase(test.TestCase): timeout=600) self.res_is.mock.assert_has_calls(mock.call('RESCUE')) - def test_server_unrescue(self): + def test__unresque_server(self): utils.NovaScenario()._unrescue_server(self.server) self.server.unrescue.assert_called_once_with() self.wait_for.mock.assert_called_once_with(self.server, @@ -159,7 +153,7 @@ class NovaScenarioTestCase(test.TestCase): @mock.patch(BM_UTILS + ".is_none") @mock.patch(NOVA_UTILS + '.NovaScenario.clients') - def test_delete_all_servers(self, mock_clients, mock_isnone): + def test__delete_all_servers(self, mock_clients, mock_isnone): mock_clients("nova").servers.list.return_value = [self.server, self.server1] utils.NovaScenario()._delete_all_servers() @@ -173,7 +167,7 @@ class NovaScenarioTestCase(test.TestCase): ] self.assertEqual(expected, self.wait_for.mock.mock_calls) - def test_delete_image(self): + def test__delete_image(self): utils.NovaScenario()._delete_image(self.image) self.image.delete.assert_called_once_with() self.wait_for.mock.assert_called_once_with(self.image, @@ -184,7 +178,7 @@ class NovaScenarioTestCase(test.TestCase): self.res_is.mock.assert_has_calls(mock.call('DELETED')) @mock.patch(NOVA_UTILS + '.NovaScenario.clients') - def test_boot_servers(self, mock_clients): + def test__boot_servers(self, mock_clients): mock_clients("nova").servers.list.return_value = [self.server, self.server1] utils.NovaScenario()._boot_servers('prefix', 'image', 'flavor', 2)