diff --git a/openstackclient/compute/v2/console.py b/openstackclient/compute/v2/console.py index 110b21b81e..f0abaf4cf4 100644 --- a/openstackclient/compute/v2/console.py +++ b/openstackclient/compute/v2/console.py @@ -44,19 +44,18 @@ class ShowConsoleLog(command.Command): return parser def take_action(self, parsed_args): - compute_client = self.app.client_manager.compute + compute_client = self.app.client_manager.sdk_connection.compute - server = utils.find_resource( - compute_client.servers, - parsed_args.server, + server = compute_client.find_server( + name_or_id=parsed_args.server, + ignore_missing=False ) - length = parsed_args.lines - if length: - # NOTE(dtroyer): get_console_output() appears to shortchange the - # output by one line - length += 1 - data = server.get_console_output(length=length) + output = compute_client.get_server_console_output( + server.id, length=parsed_args.lines) + data = None + if output: + data = output.get('output', None) if data and data[-1] != '\n': data += '\n' diff --git a/openstackclient/tests/unit/compute/v2/test_console.py b/openstackclient/tests/unit/compute/v2/test_console.py index 99a14f048d..1c6d658bc1 100644 --- a/openstackclient/tests/unit/compute/v2/test_console.py +++ b/openstackclient/tests/unit/compute/v2/test_console.py @@ -17,16 +17,95 @@ from unittest import mock from openstackclient.compute.v2 import console from openstackclient.tests.unit.compute.v2 import fakes as compute_fakes +from openstackclient.tests.unit import utils class TestConsole(compute_fakes.TestComputev2): def setUp(self): super(TestConsole, self).setUp() + + # SDK mock + self.app.client_manager.sdk_connection = mock.Mock() + self.app.client_manager.sdk_connection.compute = mock.Mock() + self.sdk_client = self.app.client_manager.sdk_connection.compute + self.sdk_client.find_server = mock.Mock() + self.sdk_client.get_server_console_output = mock.Mock() + self.servers_mock = self.app.client_manager.compute.servers self.servers_mock.reset_mock() +class TestConsoleLog(TestConsole): + _server = compute_fakes.FakeServer.create_one_server() + + def setUp(self): + super(TestConsoleLog, self).setUp() + + self.sdk_client.find_server.return_value = self._server + + self.cmd = console.ShowConsoleLog(self.app, None) + + def test_show_no_args(self): + arglist = [ + ] + verifylist = [ + ] + self.assertRaises(utils.ParserException, + self.check_parser, + self.cmd, + arglist, + verifylist) + + def test_show(self): + arglist = [ + 'fake_server' + ] + verifylist = [ + ('server', 'fake_server') + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + output = { + 'output': '1st line\n2nd line\n' + } + self.sdk_client.get_server_console_output.return_value = output + self.cmd.take_action(parsed_args) + + self.sdk_client.find_server.assert_called_with( + name_or_id='fake_server', ignore_missing=False) + self.sdk_client.get_server_console_output.assert_called_with( + self._server.id, + length=None + ) + stdout = self.app.stdout.content + self.assertEqual(stdout[0], output['output']) + + def test_show_lines(self): + arglist = [ + 'fake_server', + '--lines', '15' + ] + verifylist = [ + ('server', 'fake_server'), + ('lines', 15) + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + output = { + 'output': '1st line\n2nd line' + } + self.sdk_client.get_server_console_output.return_value = output + self.cmd.take_action(parsed_args) + + self.sdk_client.find_server.assert_called_with( + name_or_id='fake_server', ignore_missing=False) + self.sdk_client.get_server_console_output.assert_called_with( + self._server.id, + length=15 + ) + + class TestConsoleUrlShow(TestConsole): def setUp(self): diff --git a/releasenotes/notes/switch-console-log-to-sdk-6ee92b7833364d3d.yaml b/releasenotes/notes/switch-console-log-to-sdk-6ee92b7833364d3d.yaml new file mode 100644 index 0000000000..2bd0bab49b --- /dev/null +++ b/releasenotes/notes/switch-console-log-to-sdk-6ee92b7833364d3d.yaml @@ -0,0 +1,4 @@ +--- +features: + - | + Switch console logs operation to use SDK