|
| 1 | +# Copyright 2021-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 connections to RSGhost nodes.""" |
| 16 | + |
| 17 | +import datetime |
| 18 | + |
| 19 | +from mockupdb import going, MockupDB |
| 20 | +from pymongo import MongoClient |
| 21 | +from pymongo.errors import ServerSelectionTimeoutError |
| 22 | + |
| 23 | +import unittest |
| 24 | + |
| 25 | + |
| 26 | +class TestRSGhost(unittest.TestCase): |
| 27 | + |
| 28 | + def test_rsghost(self): |
| 29 | + rsother_response = { |
| 30 | + 'ok': 1.0, 'ismaster': False, 'secondary': False, |
| 31 | + 'info': 'Does not have a valid replica set config', |
| 32 | + 'isreplicaset': True, 'maxBsonObjectSize': 16777216, |
| 33 | + 'maxMessageSizeBytes': 48000000, 'maxWriteBatchSize': 100000, |
| 34 | + 'localTime': datetime.datetime(2021, 11, 30, 0, 53, 4, 99000), |
| 35 | + 'logicalSessionTimeoutMinutes': 30, 'connectionId': 3, |
| 36 | + 'minWireVersion': 0, 'maxWireVersion': 15, 'readOnly': False} |
| 37 | + server = MockupDB(auto_ismaster=rsother_response) |
| 38 | + server.run() |
| 39 | + self.addCleanup(server.stop) |
| 40 | + # Default auto discovery yields a server selection timeout. |
| 41 | + with MongoClient(server.uri, serverSelectionTimeoutMS=250) as client: |
| 42 | + with self.assertRaises(ServerSelectionTimeoutError): |
| 43 | + client.test.command('ping') |
| 44 | + # Direct connection succeeds. |
| 45 | + with MongoClient(server.uri, directConnection=True) as client: |
| 46 | + with going(client.test.command, 'ping'): |
| 47 | + request = server.receives(ping=1) |
| 48 | + request.reply() |
| 49 | + |
| 50 | + |
| 51 | +if __name__ == '__main__': |
| 52 | + unittest.main() |
0 commit comments