|
| 1 | +# Copyright 2020-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 | + |
| 15 | +"""Test Atlas Data Lake.""" |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +import os |
| 19 | +import sys |
| 20 | +from pathlib import Path |
| 21 | + |
| 22 | +import pytest |
| 23 | + |
| 24 | +sys.path[0:0] = [""] |
| 25 | + |
| 26 | +from test.asynchronous import AsyncIntegrationTest, AsyncUnitTest, async_client_context, unittest |
| 27 | +from test.asynchronous.unified_format import generate_test_classes |
| 28 | +from test.utils import ( |
| 29 | + OvertCommandListener, |
| 30 | +) |
| 31 | + |
| 32 | +from pymongo.asynchronous.helpers import anext |
| 33 | + |
| 34 | +_IS_SYNC = False |
| 35 | + |
| 36 | +pytestmark = pytest.mark.data_lake |
| 37 | + |
| 38 | + |
| 39 | +class TestDataLakeMustConnect(AsyncUnitTest): |
| 40 | + async def test_connected_to_data_lake(self): |
| 41 | + data_lake = os.environ.get("TEST_DATA_LAKE") |
| 42 | + if not data_lake: |
| 43 | + self.skipTest("TEST_DATA_LAKE is not set") |
| 44 | + |
| 45 | + self.assertTrue( |
| 46 | + async_client_context.is_data_lake and async_client_context.connected, |
| 47 | + "client context must be connected to data lake when DATA_LAKE is set. Failed attempts:\n{}".format( |
| 48 | + async_client_context.connection_attempt_info() |
| 49 | + ), |
| 50 | + ) |
| 51 | + |
| 52 | + |
| 53 | +class TestDataLakeProse(AsyncIntegrationTest): |
| 54 | + # Default test database and collection names. |
| 55 | + TEST_DB = "test" |
| 56 | + TEST_COLLECTION = "driverdata" |
| 57 | + |
| 58 | + @async_client_context.require_data_lake |
| 59 | + async def asyncSetUp(self): |
| 60 | + await super().asyncSetUp() |
| 61 | + |
| 62 | + # Test killCursors |
| 63 | + async def test_1(self): |
| 64 | + listener = OvertCommandListener() |
| 65 | + client = await self.async_rs_or_single_client(event_listeners=[listener]) |
| 66 | + cursor = client[self.TEST_DB][self.TEST_COLLECTION].find({}, batch_size=2) |
| 67 | + await anext(cursor) |
| 68 | + |
| 69 | + # find command assertions |
| 70 | + find_cmd = listener.succeeded_events[-1] |
| 71 | + self.assertEqual(find_cmd.command_name, "find") |
| 72 | + cursor_id = find_cmd.reply["cursor"]["id"] |
| 73 | + cursor_ns = find_cmd.reply["cursor"]["ns"] |
| 74 | + |
| 75 | + # killCursors command assertions |
| 76 | + await cursor.close() |
| 77 | + started = listener.started_events[-1] |
| 78 | + self.assertEqual(started.command_name, "killCursors") |
| 79 | + succeeded = listener.succeeded_events[-1] |
| 80 | + self.assertEqual(succeeded.command_name, "killCursors") |
| 81 | + |
| 82 | + self.assertIn(cursor_id, started.command["cursors"]) |
| 83 | + target_ns = ".".join([started.command["$db"], started.command["killCursors"]]) |
| 84 | + self.assertEqual(cursor_ns, target_ns) |
| 85 | + |
| 86 | + self.assertIn(cursor_id, succeeded.reply["cursorsKilled"]) |
| 87 | + |
| 88 | + # Test no auth |
| 89 | + async def test_2(self): |
| 90 | + client = await self.async_rs_client_noauth() |
| 91 | + await client.admin.command("ping") |
| 92 | + |
| 93 | + # Test with auth |
| 94 | + async def test_3(self): |
| 95 | + for mechanism in ["SCRAM-SHA-1", "SCRAM-SHA-256"]: |
| 96 | + client = await self.async_rs_or_single_client(authMechanism=mechanism) |
| 97 | + await client[self.TEST_DB][self.TEST_COLLECTION].find_one() |
| 98 | + |
| 99 | + |
| 100 | +# Location of JSON test specifications. |
| 101 | +if _IS_SYNC: |
| 102 | + TEST_PATH = Path(__file__).parent / "data_lake/unified" |
| 103 | +else: |
| 104 | + TEST_PATH = Path(__file__).parent.parent / "data_lake/unified" |
| 105 | + |
| 106 | +# Generate unified tests. |
| 107 | +globals().update(generate_test_classes(TEST_PATH, module=__name__)) |
| 108 | + |
| 109 | + |
| 110 | +if __name__ == "__main__": |
| 111 | + unittest.main() |
0 commit comments