From 6c743d0685b53052969357b8a3b82f382aae68c7 Mon Sep 17 00:00:00 2001 From: Chris Peterson Date: Mon, 16 Sep 2024 19:20:34 -0700 Subject: [PATCH] ui: fix journal parsing in ProgressView The ProgressView.other_event function splits messages on the ":" character to get the context type and the message from journal messages. This code will crash if the message has more than one ":" character. When passing cloud-config with invalid top level keys, subiquity will check these keys and report on the journal with a message: "cloud-init schema validation failure for: [, , ...]", which was causing the UI to crash repeatedly. LP: #2080918 --- subiquity/ui/views/installprogress.py | 2 +- .../ui/views/tests/test_installprogress.py | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/subiquity/ui/views/installprogress.py b/subiquity/ui/views/installprogress.py index 4a16f81f6..a5566083c 100644 --- a/subiquity/ui/views/installprogress.py +++ b/subiquity/ui/views/installprogress.py @@ -123,7 +123,7 @@ def event_other(self, message: str, event_type: str) -> None: # # and we want to insert the event type between the colon and the # event description - context, text = message.split(":") + context, text = message.split(":", maxsplit=1) text = text.lstrip() message = f"{context}: {event_type.upper()}: {text}" diff --git a/subiquity/ui/views/tests/test_installprogress.py b/subiquity/ui/views/tests/test_installprogress.py index f16fca43e..ec53c45d8 100644 --- a/subiquity/ui/views/tests/test_installprogress.py +++ b/subiquity/ui/views/tests/test_installprogress.py @@ -1,5 +1,6 @@ import unittest from unittest import mock +from unittest.mock import patch from subiquity.client.controllers.progress import ProgressController from subiquity.common.types import ApplicationState @@ -53,3 +54,29 @@ def test_error_disambiguation(self): self.assertIsNone(btn) btn = view_helpers.find_button_matching(view, "^Restart Installer$") self.assertIsNotNone(btn) + + @patch("subiquity.ui.views.installprogress.Columns") + @patch("subiquity.ui.views.installprogress.Text") + def test_event_other_formatting(self, text_mock, columns_mock): + """Test formatting of the other_event function.""" + view = self.make_view() + text_mock.return_value = "mock text" + view.event_other("MOCK CONTEXT: message", "mock") + text_mock.assert_called_with("MOCK CONTEXT: MOCK: message") + columns_mock.assert_called_with( + [ + ("pack", "mock text"), + ], + dividechars=1, + ) + + @patch("subiquity.ui.views.installprogress.Text") + def test_event_other_robust_splitting(self, text_mock): + """Test that messages containing a colon don't fail to split. + + event_other uses str.split(":"), make sure it doesn't cause an + error if more than one colon is present in the message. + """ + view = self.make_view() + view.event_other("MOCK CONTEXT: bad keys: 1, 2, 3", "mock") + text_mock.assert_called_with("MOCK CONTEXT: MOCK: bad keys: 1, 2, 3")