|
| 1 | +# Copyright 2023-present MongoDB, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +from __future__ import annotations |
| 15 | + |
| 16 | +import os |
| 17 | +from test import unittest |
| 18 | +from test.asynchronous import AsyncIntegrationTest |
| 19 | +from test.utils import async_single_client |
| 20 | +from unittest.mock import patch |
| 21 | + |
| 22 | +from bson import json_util |
| 23 | +from pymongo.errors import OperationFailure |
| 24 | +from pymongo.logger import _DEFAULT_DOCUMENT_LENGTH |
| 25 | + |
| 26 | +_IS_SYNC = False |
| 27 | + |
| 28 | + |
| 29 | +# https://github.com/mongodb/specifications/tree/master/source/command-logging-and-monitoring/tests#prose-tests |
| 30 | +class TestLogger(AsyncIntegrationTest): |
| 31 | + async def test_default_truncation_limit(self): |
| 32 | + docs = [{"x": "y"} for _ in range(100)] |
| 33 | + db = self.db |
| 34 | + |
| 35 | + with patch.dict("os.environ"): |
| 36 | + os.environ.pop("MONGOB_LOG_MAX_DOCUMENT_LENGTH", None) |
| 37 | + with self.assertLogs("pymongo.command", level="DEBUG") as cm: |
| 38 | + await db.test.insert_many(docs) |
| 39 | + |
| 40 | + cmd_started_log = json_util.loads(cm.records[0].message) |
| 41 | + self.assertEqual(len(cmd_started_log["command"]), _DEFAULT_DOCUMENT_LENGTH + 3) |
| 42 | + |
| 43 | + cmd_succeeded_log = json_util.loads(cm.records[1].message) |
| 44 | + self.assertLessEqual(len(cmd_succeeded_log["reply"]), _DEFAULT_DOCUMENT_LENGTH + 3) |
| 45 | + |
| 46 | + with self.assertLogs("pymongo.command", level="DEBUG") as cm: |
| 47 | + await db.test.find({}).to_list() |
| 48 | + cmd_succeeded_log = json_util.loads(cm.records[1].message) |
| 49 | + self.assertEqual(len(cmd_succeeded_log["reply"]), _DEFAULT_DOCUMENT_LENGTH + 3) |
| 50 | + |
| 51 | + async def test_configured_truncation_limit(self): |
| 52 | + cmd = {"hello": True} |
| 53 | + db = self.db |
| 54 | + with patch.dict("os.environ", {"MONGOB_LOG_MAX_DOCUMENT_LENGTH": "5"}): |
| 55 | + with self.assertLogs("pymongo.command", level="DEBUG") as cm: |
| 56 | + await db.command(cmd) |
| 57 | + |
| 58 | + cmd_started_log = json_util.loads(cm.records[0].message) |
| 59 | + self.assertEqual(len(cmd_started_log["command"]), 5 + 3) |
| 60 | + |
| 61 | + cmd_succeeded_log = json_util.loads(cm.records[1].message) |
| 62 | + self.assertLessEqual(len(cmd_succeeded_log["reply"]), 5 + 3) |
| 63 | + with self.assertRaises(OperationFailure): |
| 64 | + await db.command({"notARealCommand": True}) |
| 65 | + cmd_failed_log = json_util.loads(cm.records[-1].message) |
| 66 | + self.assertEqual(len(cmd_failed_log["failure"]), 5 + 3) |
| 67 | + |
| 68 | + async def test_truncation_multi_byte_codepoints(self): |
| 69 | + document_lengths = ["20000", "20001", "20002"] |
| 70 | + multi_byte_char_str_len = 50_000 |
| 71 | + str_to_repeat = "界" |
| 72 | + |
| 73 | + multi_byte_char_str = "" |
| 74 | + for i in range(multi_byte_char_str_len): |
| 75 | + multi_byte_char_str += str_to_repeat |
| 76 | + |
| 77 | + for length in document_lengths: |
| 78 | + with patch.dict("os.environ", {"MONGOB_LOG_MAX_DOCUMENT_LENGTH": length}): |
| 79 | + with self.assertLogs("pymongo.command", level="DEBUG") as cm: |
| 80 | + await self.db.test.insert_one({"x": multi_byte_char_str}) |
| 81 | + cmd_started_log = json_util.loads(cm.records[0].message)["command"] |
| 82 | + |
| 83 | + cmd_started_log = cmd_started_log[:-3] |
| 84 | + last_3_bytes = cmd_started_log.encode()[-3:].decode() |
| 85 | + |
| 86 | + self.assertEqual(last_3_bytes, str_to_repeat) |
| 87 | + |
| 88 | + async def test_logging_without_listeners(self): |
| 89 | + c = await async_single_client() |
| 90 | + self.assertEqual(len(c._event_listeners.event_listeners()), 0) |
| 91 | + with self.assertLogs("pymongo.connection", level="DEBUG") as cm: |
| 92 | + await c.db.test.insert_one({"x": "1"}) |
| 93 | + self.assertGreater(len(cm.records), 0) |
| 94 | + with self.assertLogs("pymongo.command", level="DEBUG") as cm: |
| 95 | + await c.db.test.insert_one({"x": "1"}) |
| 96 | + self.assertGreater(len(cm.records), 0) |
| 97 | + with self.assertLogs("pymongo.serverSelection", level="DEBUG") as cm: |
| 98 | + await c.db.test.insert_one({"x": "1"}) |
| 99 | + self.assertGreater(len(cm.records), 0) |
| 100 | + |
| 101 | + |
| 102 | +if __name__ == "__main__": |
| 103 | + unittest.main() |
0 commit comments