Make --image parameter optional in "server rebuild"
The command will now default to the image currently in-use by the server, effectively making the --image parameter optional. This commit also adds basic tests for ServerRebuild since there wasn't any. Will add more full tests for it. Change-Id: I733fd3ad5a825f06563c72aa430122e1a0e3b3b0 Closes-bug: #1524406 Co-Authored-By: David Moreau Simard <dms@redhat.com> Co-Authored-By: Tang Chen <tangchen@cn.fujitsu.com>
This commit is contained in:
parent
f65f82e435
commit
a526777218
@ -375,14 +375,15 @@ Rebuild server
|
|||||||
.. code:: bash
|
.. code:: bash
|
||||||
|
|
||||||
os server rebuild
|
os server rebuild
|
||||||
--image <image>
|
[--image <image>]
|
||||||
[--password <password>]
|
[--password <password>]
|
||||||
[--wait]
|
[--wait]
|
||||||
<server>
|
<server>
|
||||||
|
|
||||||
.. option:: --image <image>
|
.. option:: --image <image>
|
||||||
|
|
||||||
Recreate server from this image
|
Recreate server from the specified image (name or ID). Defaults to the
|
||||||
|
currently used one.
|
||||||
|
|
||||||
.. option:: --password <password>
|
.. option:: --password <password>
|
||||||
|
|
||||||
|
@ -1096,8 +1096,8 @@ class RebuildServer(show.ShowOne):
|
|||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--image',
|
'--image',
|
||||||
metavar='<image>',
|
metavar='<image>',
|
||||||
required=True,
|
help=_('Recreate server from the specified image (name or ID).'
|
||||||
help=_('Recreate server from this image'),
|
' Defaults to the currently used one.'),
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--password',
|
'--password',
|
||||||
@ -1115,12 +1115,13 @@ class RebuildServer(show.ShowOne):
|
|||||||
def take_action(self, parsed_args):
|
def take_action(self, parsed_args):
|
||||||
compute_client = self.app.client_manager.compute
|
compute_client = self.app.client_manager.compute
|
||||||
|
|
||||||
# Lookup parsed_args.image
|
|
||||||
image = utils.find_resource(compute_client.images, parsed_args.image)
|
|
||||||
|
|
||||||
server = utils.find_resource(
|
server = utils.find_resource(
|
||||||
compute_client.servers, parsed_args.server)
|
compute_client.servers, parsed_args.server)
|
||||||
|
|
||||||
|
# If parsed_args.image is not set, default to the currently used one.
|
||||||
|
image_id = parsed_args.image or server._info.get('image', {}).get('id')
|
||||||
|
image = utils.find_resource(compute_client.images, image_id)
|
||||||
|
|
||||||
server = server.rebuild(image, parsed_args.password)
|
server = server.rebuild(image, parsed_args.password)
|
||||||
if parsed_args.wait:
|
if parsed_args.wait:
|
||||||
if utils.wait_for_status(
|
if utils.wait_for_status(
|
||||||
|
@ -736,6 +736,76 @@ class TestServerPause(TestServer):
|
|||||||
self.run_method_with_servers('pause', 3)
|
self.run_method_with_servers('pause', 3)
|
||||||
|
|
||||||
|
|
||||||
|
class TestServerRebuild(TestServer):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
super(TestServerRebuild, self).setUp()
|
||||||
|
|
||||||
|
# Return value for utils.find_resource for image
|
||||||
|
self.image = image_fakes.FakeImage.create_one_image()
|
||||||
|
self.cimages_mock.get.return_value = self.image
|
||||||
|
|
||||||
|
# Fake the rebuilt new server.
|
||||||
|
new_server = compute_fakes.FakeServer.create_one_server()
|
||||||
|
|
||||||
|
# Fake the server to be rebuilt. The IDs of them should be the same.
|
||||||
|
attrs = {
|
||||||
|
'id': new_server.id,
|
||||||
|
'image': {
|
||||||
|
'id': self.image.id
|
||||||
|
},
|
||||||
|
'networks': {},
|
||||||
|
'adminPass': 'passw0rd',
|
||||||
|
}
|
||||||
|
methods = {
|
||||||
|
'rebuild': new_server,
|
||||||
|
}
|
||||||
|
self.server = compute_fakes.FakeServer.create_one_server(
|
||||||
|
attrs=attrs,
|
||||||
|
methods=methods
|
||||||
|
)
|
||||||
|
|
||||||
|
# Return value for utils.find_resource for server.
|
||||||
|
self.servers_mock.get.return_value = self.server
|
||||||
|
|
||||||
|
self.cmd = server.RebuildServer(self.app, None)
|
||||||
|
|
||||||
|
def test_rebuild_with_current_image(self):
|
||||||
|
arglist = [
|
||||||
|
self.server.id,
|
||||||
|
]
|
||||||
|
verifylist = [
|
||||||
|
('server', self.server.id)
|
||||||
|
]
|
||||||
|
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
||||||
|
|
||||||
|
# Get the command object to test.
|
||||||
|
self.cmd.take_action(parsed_args)
|
||||||
|
|
||||||
|
self.servers_mock.get.assert_called_with(self.server.id)
|
||||||
|
self.cimages_mock.get.assert_called_with(self.image.id)
|
||||||
|
self.server.rebuild.assert_called_with(self.image, None)
|
||||||
|
|
||||||
|
def test_rebuild_with_current_image_and_password(self):
|
||||||
|
password = 'password-xxx'
|
||||||
|
arglist = [
|
||||||
|
self.server.id,
|
||||||
|
'--password', password
|
||||||
|
]
|
||||||
|
verifylist = [
|
||||||
|
('server', self.server.id),
|
||||||
|
('password', password)
|
||||||
|
]
|
||||||
|
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
||||||
|
|
||||||
|
# Get the command object to test
|
||||||
|
self.cmd.take_action(parsed_args)
|
||||||
|
|
||||||
|
self.servers_mock.get.assert_called_with(self.server.id)
|
||||||
|
self.cimages_mock.get.assert_called_with(self.image.id)
|
||||||
|
self.server.rebuild.assert_called_with(self.image, password)
|
||||||
|
|
||||||
|
|
||||||
class TestServerResize(TestServer):
|
class TestServerResize(TestServer):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user