From ea2ac77a46b9bed87bbff902c093c1e8a43693af Mon Sep 17 00:00:00 2001 From: Dean Troyer Date: Mon, 7 Jul 2014 11:42:37 -0500 Subject: [PATCH] Fix server resize So apparently we've never resized a server??? Fixed command args and add some tests. Change-Id: I6c3f6fec22390e9d269b7117a42a190d2b4b80ba --- openstackclient/compute/v2/server.py | 19 ++- openstackclient/tests/compute/v2/fakes.py | 14 ++ .../tests/compute/v2/test_server.py | 135 ++++++++++++++++++ openstackclient/tests/utils.py | 8 ++ 4 files changed, 169 insertions(+), 7 deletions(-) diff --git a/openstackclient/compute/v2/server.py b/openstackclient/compute/v2/server.py index 2dcc7ae9a0..a4ed6fa47a 100644 --- a/openstackclient/compute/v2/server.py +++ b/openstackclient/compute/v2/server.py @@ -938,27 +938,32 @@ class RescueServer(show.ShowOne): class ResizeServer(command.Command): - """Convert server to a new flavor""" + """Scale server to a new flavor""" log = logging.getLogger(__name__ + '.ResizeServer') def get_parser(self, prog_name): parser = super(ResizeServer, self).get_parser(prog_name) phase_group = parser.add_mutually_exclusive_group() + parser.add_argument( + 'server', + metavar='', + help='Server (name or ID)', + ) phase_group.add_argument( '--flavor', metavar='', - help='Resize server to this flavor', + help='Resize server to specified flavor', ) phase_group.add_argument( '--verify', action="store_true", - help='Verify previous server resize', + help='Verify server resize is complete', ) phase_group.add_argument( '--revert', action="store_true", - help='Restore server before resize', + help='Restore server state before resize', ) parser.add_argument( '--wait', @@ -980,7 +985,7 @@ class ResizeServer(command.Command): compute_client.flavors, parsed_args.flavor, ) - server.resize(flavor) + compute_client.servers.resize(server, flavor) if parsed_args.wait: if utils.wait_for_status( compute_client.servers.get, @@ -993,9 +998,9 @@ class ResizeServer(command.Command): sys.stdout.write('\nError resizing server') raise SystemExit elif parsed_args.verify: - server.confirm_resize() + compute_client.servers.confirm_resize(server) elif parsed_args.revert: - server.revert_resize() + compute_client.servers.revert_resize(server) class ResumeServer(command.Command): diff --git a/openstackclient/tests/compute/v2/fakes.py b/openstackclient/tests/compute/v2/fakes.py index cef5ee9052..9a7964db0d 100644 --- a/openstackclient/tests/compute/v2/fakes.py +++ b/openstackclient/tests/compute/v2/fakes.py @@ -47,6 +47,18 @@ EXTENSION = { 'links': extension_links, } +flavor_id = 'm1.large' +flavor_name = 'Large' +flavor_ram = 8192 +flavor_vcpus = 4 + +FLAVOR = { + 'id': flavor_id, + 'name': flavor_name, + 'ram': flavor_ram, + 'vcpus': flavor_vcpus, +} + class FakeComputev2Client(object): def __init__(self, **kwargs): @@ -56,6 +68,8 @@ class FakeComputev2Client(object): self.servers.resource_class = fakes.FakeResource(None, {}) self.extensions = mock.Mock() self.extensions.resource_class = fakes.FakeResource(None, {}) + self.flavors = mock.Mock() + self.flavors.resource_class = fakes.FakeResource(None, {}) self.auth_token = kwargs['token'] self.management_url = kwargs['endpoint'] diff --git a/openstackclient/tests/compute/v2/test_server.py b/openstackclient/tests/compute/v2/test_server.py index efe4c58b98..a98cd15690 100644 --- a/openstackclient/tests/compute/v2/test_server.py +++ b/openstackclient/tests/compute/v2/test_server.py @@ -30,6 +30,10 @@ class TestServer(compute_fakes.TestComputev2): self.servers_mock = self.app.client_manager.compute.servers self.servers_mock.reset_mock() + # Get a shortcut to the FlavorManager Mock + self.flavors_mock = self.app.client_manager.compute.flavors + self.flavors_mock.reset_mock() + # Get a shortcut to the ImageManager Mock self.images_mock = self.app.client_manager.image.images self.images_mock.reset_mock() @@ -148,3 +152,134 @@ class TestServerImageCreate(TestServer): image_fakes.image_owner, ) self.assertEqual(data, datalist) + + +class TestServerResize(TestServer): + + def setUp(self): + super(TestServerResize, self).setUp() + + # This is the return value for utils.find_resource() + self.servers_get_return_value = fakes.FakeResource( + None, + copy.deepcopy(compute_fakes.SERVER), + loaded=True, + ) + self.servers_mock.get.return_value = self.servers_get_return_value + + self.servers_mock.resize.return_value = None + self.servers_mock.confirm_resize.return_value = None + self.servers_mock.revert_resize.return_value = None + + # This is the return value for utils.find_resource() + self.flavors_get_return_value = fakes.FakeResource( + None, + copy.deepcopy(compute_fakes.FLAVOR), + loaded=True, + ) + self.flavors_mock.get.return_value = self.flavors_get_return_value + + # Get the command object to test + self.cmd = server.ResizeServer(self.app, None) + + def test_server_resize_no_options(self): + arglist = [ + compute_fakes.server_id, + ] + verifylist = [ + ('verify', False), + ('revert', False), + ('server', compute_fakes.server_id), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + # DisplayCommandBase.take_action() returns two tuples + self.cmd.take_action(parsed_args) + + self.servers_mock.get.assert_called_with( + compute_fakes.server_id, + ) + + self.assertNotCalled(self.servers_mock.resize) + self.assertNotCalled(self.servers_mock.confirm_resize) + self.assertNotCalled(self.servers_mock.revert_resize) + + def test_server_resize(self): + arglist = [ + '--flavor', compute_fakes.flavor_id, + compute_fakes.server_id, + ] + verifylist = [ + ('flavor', compute_fakes.flavor_id), + ('verify', False), + ('revert', False), + ('server', compute_fakes.server_id), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + # DisplayCommandBase.take_action() returns two tuples + self.cmd.take_action(parsed_args) + + self.servers_mock.get.assert_called_with( + compute_fakes.server_id, + ) + self.flavors_mock.get.assert_called_with( + compute_fakes.flavor_id, + ) + + self.servers_mock.resize.assert_called_with( + self.servers_get_return_value, + self.flavors_get_return_value, + ) + self.assertNotCalled(self.servers_mock.confirm_resize) + self.assertNotCalled(self.servers_mock.revert_resize) + + def test_server_resize_confirm(self): + arglist = [ + '--verify', + compute_fakes.server_id, + ] + verifylist = [ + ('verify', True), + ('revert', False), + ('server', compute_fakes.server_id), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + # DisplayCommandBase.take_action() returns two tuples + self.cmd.take_action(parsed_args) + + self.servers_mock.get.assert_called_with( + compute_fakes.server_id, + ) + + self.assertNotCalled(self.servers_mock.resize) + self.servers_mock.confirm_resize.assert_called_with( + self.servers_get_return_value, + ) + self.assertNotCalled(self.servers_mock.revert_resize) + + def test_server_resize_revert(self): + arglist = [ + '--revert', + compute_fakes.server_id, + ] + verifylist = [ + ('verify', False), + ('revert', True), + ('server', compute_fakes.server_id), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + # DisplayCommandBase.take_action() returns two tuples + self.cmd.take_action(parsed_args) + + self.servers_mock.get.assert_called_with( + compute_fakes.server_id, + ) + + self.assertNotCalled(self.servers_mock.resize) + self.assertNotCalled(self.servers_mock.confirm_resize) + self.servers_mock.revert_resize.assert_called_with( + self.servers_get_return_value, + ) diff --git a/openstackclient/tests/utils.py b/openstackclient/tests/utils.py index 48385d138a..3755fa2647 100644 --- a/openstackclient/tests/utils.py +++ b/openstackclient/tests/utils.py @@ -37,6 +37,14 @@ class TestCase(testtools.TestCase): stderr = self.useFixture(fixtures.StringStream("stderr")).stream self.useFixture(fixtures.MonkeyPatch("sys.stderr", stderr)) + def assertNotCalled(self, m, msg=None): + """Assert a function was not called""" + + if m.called: + if not msg: + msg = 'method %s should not have been called' % m + self.fail(msg) + # 2.6 doesn't have the assert dict equals so make sure that it exists if tuple(sys.version_info)[0:2] < (2, 7):