def setUp(self): real_client = botocore.session.get_session().create_client("logs", region_name="us-east-1") self.client_stubber = Stubber(real_client) self.fetcher = LogsFetcher(real_client) self.log_group_name = "name" self.stream_name = "stream name" self.timestamp = to_timestamp(datetime.datetime.utcnow()) self.mock_api_response = { "events": [ { "eventId": "id1", "ingestionTime": 0, "logStreamName": self.stream_name, "message": "message 1", "timestamp": self.timestamp, }, { "eventId": "id2", "ingestionTime": 0, "logStreamName": self.stream_name, "message": "message 2", "timestamp": self.timestamp, }, ] } self.expected_events = [ LogEvent( self.log_group_name, { "eventId": "id1", "ingestionTime": 0, "logStreamName": self.stream_name, "message": "message 1", "timestamp": self.timestamp, }, ), LogEvent( self.log_group_name, { "eventId": "id2", "ingestionTime": 0, "logStreamName": self.stream_name, "message": "message 2", "timestamp": self.timestamp, }, ), ]
def test_must_ignore_if_event_is_empty(self): event = LogEvent(self.group_name, {}) self.assertEquals(event.log_group_name, self.group_name) self.assertIsNone(event.log_stream_name) self.assertIsNone(event.message) self.assertIsNone(event.timestamp)
def test_check_for_equality(self): event = LogEvent( self.group_name, { "timestamp": self.timestamp, "logStreamName": self.stream_name, "message": self.message }) other = LogEvent( self.group_name, { "timestamp": self.timestamp, "logStreamName": self.stream_name, "message": self.message }) self.assertEquals(event, other)
def test_must_ignore_other_messages(self): colored = Mock() event = LogEvent("group_name", {"message": "some msg"}) result = LambdaLogMsgFormatters.colorize_errors(event, colored) self.assertEqual(result.message, "some msg") colored.red.assert_not_called()
def test_must_ignore_if_some_fields_are_empty(self): event = LogEvent(self.group_name, {"logStreamName": "stream name"}) self.assertEquals(event.log_group_name, self.group_name) self.assertEquals(event.log_stream_name, self.stream_name) self.assertEquals(event.message, '') self.assertIsNone(event.timestamp)
def test_must_ignore_if_keyword_is_absent(self): colored = Mock() input_msg = "this keyword some keyword other keyword" event = LogEvent("group_name", {"message": input_msg}) result = KeywordHighlighter().highlight_keywords(event, colored) self.assertEqual(result.message, input_msg) colored.underline.assert_not_called()
def test_must_pretty_print_json(self): data = {"a": "b"} input_msg = '{"a": "b"}' expected_msg = json.dumps(data, indent=2) event = LogEvent("group_name", {"message": input_msg}) result = JSONMsgFormatter.format_json(event, None) self.assertEqual(result.message, expected_msg)
def test_must_color_crash_messages(self, input_msg): color_result = "colored messaage" colored = Mock() colored.red.return_value = color_result event = LogEvent("group_name", {"message": input_msg}) result = LambdaLogMsgFormatters.colorize_errors(event, colored) self.assertEqual(result.message, color_result) colored.red.assert_called_with(input_msg)
def setUp(self): self.fetcher = LogsFetcher(Mock()) self.log_group_name = "name" self.filter_pattern = "pattern" self.start_time = to_datetime(10) self.max_retries = 3 self.poll_interval = 1 self.mock_events1 = [ LogEvent(self.log_group_name, {"timestamp": 11}), LogEvent(self.log_group_name, {"timestamp": 12}), ] self.mock_events2 = [ LogEvent(self.log_group_name, {"timestamp": 13}), LogEvent(self.log_group_name, {"timestamp": 14}), ] self.mock_events_empty = []
def test_must_extract_fields_from_event(self): event = LogEvent( self.group_name, { "timestamp": self.timestamp, "logStreamName": self.stream_name, "message": self.message }) self.assertEquals(event.log_group_name, self.group_name) self.assertEquals(event.log_stream_name, self.stream_name) self.assertEquals(event.message, self.message) self.assertEquals(self.timestamp_str, event.timestamp)
def test_must_highlight_all_keywords(self): input_msg = "this keyword some keyword other keyword" keyword = "keyword" color_result = "colored" expected_msg = "this colored some colored other colored" colored = Mock() colored.underline.return_value = color_result event = LogEvent("group_name", {"message": input_msg}) result = KeywordHighlighter(keyword).highlight_keywords(event, colored) self.assertEqual(result.message, expected_msg) colored.underline.assert_called_with(keyword)
def test_must_serialize_event(self): colored_timestamp = "colored timestamp" colored_stream_name = "colored stream name" self.colored_mock.yellow.return_value = colored_timestamp self.colored_mock.cyan.return_value = colored_stream_name event = LogEvent(self.group_name, self.event_dict) expected = " ".join([colored_stream_name, colored_timestamp, self.message]) result = LogsFormatter._pretty_print_event(event, self.colored_mock) self.assertEqual(expected, result) self.colored_mock.yellow.has_calls() self.colored_mock.cyan.assert_called_with(self.stream_name)
def test_check_for_equality_with_other_data_types(self): event = LogEvent(self.group_name, {}) other = "this is not an event" self.assertNotEquals(event, other)
def test_ignore_non_json(self, input_msg): event = LogEvent("group_name", {"message": input_msg}) result = JSONMsgFormatter.format_json(event, None) self.assertEqual(result.message, input_msg)