Path: blob/develop/tests/unit/customizations/logs/test_startlivetail.py
1569 views
# Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.1#2# Licensed under the Apache License, Version 2.0 (the "License"). You3# may not use this file except in compliance with the License. A copy of4# the License is located at5#6# http://aws.amazon.com/apache2.0/7#8# or in the "license" file accompanying this file. This file is9# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF10# ANY KIND, either express or implied. See the License for the specific11# language governing permissions and limitations under the License.12from awscli.compat import StringIO13from awscli.customizations.logs.startlivetail import (14LiveTailLogEventsCollector,15LiveTailSessionMetadata,16PrintOnlyPrinter,17PrintOnlyUI,18)19from awscli.testutils import mock, unittest202122class LiveTailSessionMetadataTest(unittest.TestCase):23def setUp(self):24self.session_metadata = LiveTailSessionMetadata()2526def test_metadata_update(self):27metadata_update = {"sampled": True}28self.session_metadata.update_metadata(metadata_update)2930self.assertEqual(metadata_update["sampled"], self.session_metadata.is_sampled)313233class PrintOnlyPrinterTest(unittest.TestCase):34def setUp(self):35self.output = StringIO()36self.log_events = []37self.printer = PrintOnlyPrinter(self.output, self.log_events)3839def test_print_log_events(self):40self.log_events.extend(["First LogEvent", "Second LogEvent", "Third LogEvent"])41expected_msg = "\n".join(self.log_events) + "\n"4243self.printer._print_log_events()4445self.output.seek(0)46self.assertEqual(expected_msg, self.output.read())47self.assertEqual(0, len(self.log_events))4849def test_session_interrupt_while_printing(self):50self.log_events.extend(["First LogEvent", "Second LogEvent", "Third LogEvent"])51expected_msg = "\n".join(self.log_events) + "\n"5253self.printer.interrupt_session = True54self.printer.run()5556self.output.seek(0)57self.assertEqual(expected_msg, self.output.read())58self.assertEqual(0, len(self.log_events))5960def test_exception_while_printing(self):61self.log_events.extend(["First LogEvent", "Second LogEvent", "Third LogEvent"])62self.printer._print_log_events = mock.MagicMock(63side_effect=BrokenPipeError("BrokenPipe")64)6566# No exception should be raised67self.printer.run()686970class PrintOnlyUITest(unittest.TestCase):71def setUp(self):72self.output = StringIO()73self.log_events = []74self.ui = PrintOnlyUI(self.output, self.log_events)7576def test_exit(self):77self.ui.exit()7879self.assertEqual(True, self.ui._printer.interrupt_session)808182class LiveTailLogEventsCollectorTest(unittest.TestCase):83def setUp(self):84self.output = StringIO()85self.log_events = []86self.response_stream = mock.Mock()87self.ui = PrintOnlyUI(self.output, self.log_events)88self.session_metadata = LiveTailSessionMetadata()89self.log_events_collector = LiveTailLogEventsCollector(90self.output,91self.ui,92self.response_stream,93self.log_events,94self.session_metadata,95)9697def test_log_event_collection(self):98updates = [99{"sessionStart": "The session is started"},100{101"sessionUpdate": {102"sessionMetadata": {"sampled": False},103"sessionResults": [104{"message": "LogEvent1"},105{"message": "LogEvent2"},106],107}108},109]110self.response_stream.__iter__ = mock.MagicMock(return_value=iter(updates))111112self.log_events_collector._collect_log_events()113114self.assertEqual(2, len(self.log_events))115self.assertEqual(["LogEvent1", "LogEvent2"], self.log_events)116self.assertIsNone(self.log_events_collector._exception)117118def test_log_event_collection_with_unexpected_update(self):119updates = [{"unexpectedUpdate": "The session is started"}]120self.response_stream.extend(updates)121self.ui.exit = mock.MagicMock()122123self.log_events_collector._collect_log_events()124125self.assertIsNotNone(self.log_events_collector._exception)126127def test_stop_with_exception(self):128exception_message = "SessionStreamingException"129self.log_events_collector._exception = Exception(exception_message)130131self.log_events_collector.stop()132133self.output.seek(0)134self.assertEqual(exception_message + "\n", self.output.read())135136def test_stop_without_exception(self):137self.response_stream.close = mock.MagicMock()138139self.log_events_collector.stop()140self.response_stream.close.assert_not_called()141142143