def get_console_output(self, instance_name): if self._vmutils.is_secure_vm(instance_name): err = _("Shielded/Encrypted VMs don't support serial console.") raise exception.ConsoleNotAvailable(err) console_log_paths = self._pathutils.get_vm_console_log_paths( instance_name) try: log = b'' # Start with the oldest console log file. for log_path in reversed(console_log_paths): if os.path.exists(log_path): with open(log_path, 'rb') as fp: log += fp.read() return log except IOError as err: raise exception.ConsoleLogOutputException( instance_id=instance_name, reason=six.text_type(err))
class ConsoleOutputExtensionTestV21(test.NoDBTestCase): controller_class = console_output_v21 validation_error = exception.ValidationError def setUp(self): super(ConsoleOutputExtensionTestV21, self).setUp() self.stub_out('nova.compute.api.API.get_console_output', fake_get_console_output) self.stub_out('nova.compute.api.API.get', fake_get) self.controller = self.controller_class.ConsoleOutputController() self.req = fakes.HTTPRequest.blank('') def _get_console_output(self, length_dict=None): length_dict = length_dict or {} body = {'os-getConsoleOutput': length_dict} return self.controller.get_console_output(self.req, fakes.FAKE_UUID, body=body) def _check_console_output_failure(self, exception, body): self.assertRaises(exception, self.controller.get_console_output, self.req, fakes.FAKE_UUID, body=body) def test_get_text_console_instance_action(self): output = self._get_console_output() self.assertEqual({'output': '0\n1\n2\n3\n4'}, output) def test_get_console_output_with_tail(self): output = self._get_console_output(length_dict={'length': 3}) self.assertEqual({'output': '2\n3\n4'}, output) def test_get_console_output_with_none_length(self): output = self._get_console_output(length_dict={'length': None}) self.assertEqual({'output': '0\n1\n2\n3\n4'}, output) def test_get_console_output_with_length_as_str(self): output = self._get_console_output(length_dict={'length': '3'}) self.assertEqual({'output': '2\n3\n4'}, output) def test_get_console_output_filtered_characters(self): self.stub_out('nova.compute.api.API.get_console_output', fake_get_console_output_all_characters) output = self._get_console_output() expect = string.digits + string.letters + string.punctuation + ' \t\n' self.assertEqual({'output': expect}, output) def test_get_text_console_no_instance(self): self.stub_out('nova.compute.api.API.get', fake_get_not_found) body = {'os-getConsoleOutput': {}} self._check_console_output_failure(webob.exc.HTTPNotFound, body) def test_get_text_console_no_instance_on_get_output(self): self.stub_out('nova.compute.api.API.get_console_output', fake_get_not_found) body = {'os-getConsoleOutput': {}} self._check_console_output_failure(webob.exc.HTTPNotFound, body) def test_get_console_output_with_non_integer_length(self): body = {'os-getConsoleOutput': {'length': 'NaN'}} self._check_console_output_failure(self.validation_error, body) def test_get_text_console_bad_body(self): body = {} self._check_console_output_failure(self.validation_error, body) def test_get_console_output_with_length_as_float(self): body = {'os-getConsoleOutput': {'length': 2.5}} self._check_console_output_failure(self.validation_error, body) def test_get_console_output_not_ready(self): self.stub_out('nova.compute.api.API.get_console_output', fake_get_console_output_not_ready) body = {'os-getConsoleOutput': {}} self._check_console_output_failure(webob.exc.HTTPConflict, body) def test_not_implemented(self): self.stub_out('nova.compute.api.API.get_console_output', fakes.fake_not_implemented) body = {'os-getConsoleOutput': {}} self._check_console_output_failure(webob.exc.HTTPNotImplemented, body) def test_get_console_output_with_boolean_length(self): body = {'os-getConsoleOutput': {'length': True}} self._check_console_output_failure(self.validation_error, body) @mock.patch.object( compute_api.API, 'get_console_output', side_effect=exception.ConsoleNotAvailable(instance_uuid='fake_uuid')) def test_get_console_output_not_available(self, mock_get_console_output): body = {'os-getConsoleOutput': {}} self._check_console_output_failure(webob.exc.HTTPNotFound, body)