File: test_server_description.py

package info (click to toggle)
pymongo 4.15.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 23,376 kB
  • sloc: python: 107,945; ansic: 4,601; javascript: 137; makefile: 38; sh: 10
file content (208 lines) | stat: -rw-r--r-- 7,440 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# Copyright 2014-present MongoDB, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Test the server_description module."""
from __future__ import annotations

import sys

sys.path[0:0] = [""]

from test import unittest

from bson.int64 import Int64
from bson.objectid import ObjectId
from pymongo import common
from pymongo.hello import Hello, HelloCompat
from pymongo.server_description import ServerDescription
from pymongo.server_type import SERVER_TYPE

address = ("localhost", 27017)


def parse_hello_response(doc):
    hello_response = Hello(doc)
    return ServerDescription(address, hello_response)


class TestServerDescription(unittest.TestCase):
    def test_unknown(self):
        # Default, no hello_response.
        s = ServerDescription(address)
        self.assertEqual(SERVER_TYPE.Unknown, s.server_type)
        self.assertFalse(s.is_writable)
        self.assertFalse(s.is_readable)

    def test_mongos(self):
        s = parse_hello_response({"ok": 1, "msg": "isdbgrid"})
        self.assertEqual(SERVER_TYPE.Mongos, s.server_type)
        self.assertEqual("Mongos", s.server_type_name)
        self.assertTrue(s.is_writable)
        self.assertTrue(s.is_readable)

    def test_primary(self):
        s = parse_hello_response({"ok": 1, HelloCompat.LEGACY_CMD: True, "setName": "rs"})

        self.assertEqual(SERVER_TYPE.RSPrimary, s.server_type)
        self.assertEqual("RSPrimary", s.server_type_name)
        self.assertTrue(s.is_writable)
        self.assertTrue(s.is_readable)

    def test_secondary(self):
        s = parse_hello_response(
            {"ok": 1, HelloCompat.LEGACY_CMD: False, "secondary": True, "setName": "rs"}
        )

        self.assertEqual(SERVER_TYPE.RSSecondary, s.server_type)
        self.assertEqual("RSSecondary", s.server_type_name)
        self.assertFalse(s.is_writable)
        self.assertTrue(s.is_readable)

    def test_arbiter(self):
        s = parse_hello_response(
            {"ok": 1, HelloCompat.LEGACY_CMD: False, "arbiterOnly": True, "setName": "rs"}
        )

        self.assertEqual(SERVER_TYPE.RSArbiter, s.server_type)
        self.assertEqual("RSArbiter", s.server_type_name)
        self.assertFalse(s.is_writable)
        self.assertFalse(s.is_readable)

    def test_other(self):
        s = parse_hello_response({"ok": 1, HelloCompat.LEGACY_CMD: False, "setName": "rs"})

        self.assertEqual(SERVER_TYPE.RSOther, s.server_type)
        self.assertEqual("RSOther", s.server_type_name)

        s = parse_hello_response(
            {
                "ok": 1,
                HelloCompat.LEGACY_CMD: False,
                "secondary": True,
                "hidden": True,
                "setName": "rs",
            }
        )

        self.assertEqual(SERVER_TYPE.RSOther, s.server_type)
        self.assertFalse(s.is_writable)
        self.assertFalse(s.is_readable)

    def test_ghost(self):
        s = parse_hello_response({"ok": 1, "isreplicaset": True})

        self.assertEqual(SERVER_TYPE.RSGhost, s.server_type)
        self.assertEqual("RSGhost", s.server_type_name)
        self.assertFalse(s.is_writable)
        self.assertFalse(s.is_readable)

    def test_fields(self):
        s = parse_hello_response(
            {
                "ok": 1,
                HelloCompat.LEGACY_CMD: False,
                "secondary": True,
                "primary": "a:27017",
                "tags": {"a": "foo", "b": "baz"},
                "maxMessageSizeBytes": 1,
                "maxBsonObjectSize": 2,
                "maxWriteBatchSize": 3,
                "minWireVersion": 4,
                "maxWireVersion": 25,
                "setName": "rs",
            }
        )

        self.assertEqual(SERVER_TYPE.RSSecondary, s.server_type)
        self.assertEqual(("a", 27017), s.primary)
        self.assertEqual({"a": "foo", "b": "baz"}, s.tags)
        self.assertEqual(1, s.max_message_size)
        self.assertEqual(2, s.max_bson_size)
        self.assertEqual(3, s.max_write_batch_size)
        self.assertEqual(4, s.min_wire_version)
        self.assertEqual(25, s.max_wire_version)

    def test_defaults(self):
        s = parse_hello_response({"ok": 1, HelloCompat.LEGACY_CMD: True})
        self.assertEqual(common.MAX_BSON_SIZE, s.max_bson_size)
        self.assertEqual(common.MAX_MESSAGE_SIZE, s.max_message_size)
        self.assertEqual(common.MIN_WIRE_VERSION, s.min_wire_version)
        self.assertEqual(common.MAX_WIRE_VERSION, s.max_wire_version)
        self.assertEqual(common.MAX_WRITE_BATCH_SIZE, s.max_write_batch_size)

    def test_standalone(self):
        s = parse_hello_response({"ok": 1, HelloCompat.LEGACY_CMD: True})
        self.assertEqual(SERVER_TYPE.Standalone, s.server_type)

        # Mongod started with --slave.
        # master-slave replication was removed in MongoDB 4.0.
        s = parse_hello_response({"ok": 1, HelloCompat.LEGACY_CMD: False})
        self.assertEqual(SERVER_TYPE.Standalone, s.server_type)
        self.assertTrue(s.is_writable)
        self.assertTrue(s.is_readable)

    def test_ok_false(self):
        s = parse_hello_response({"ok": 0, HelloCompat.LEGACY_CMD: True})
        self.assertEqual(SERVER_TYPE.Unknown, s.server_type)
        self.assertFalse(s.is_writable)
        self.assertFalse(s.is_readable)

    def test_all_hosts(self):
        s = parse_hello_response(
            {
                "ok": 1,
                HelloCompat.LEGACY_CMD: True,
                "hosts": ["a"],
                "passives": ["b:27018"],
                "arbiters": ["c"],
            }
        )

        self.assertEqual([("a", 27017), ("b", 27018), ("c", 27017)], sorted(s.all_hosts))

    def test_repr(self):
        s = parse_hello_response({"ok": 1, "msg": "isdbgrid"})
        self.assertEqual(
            repr(s), "<ServerDescription ('localhost', 27017) server_type: Mongos, rtt: None>"
        )

    def test_topology_version(self):
        topology_version = {"processId": ObjectId(), "counter": Int64("0")}
        s = parse_hello_response(
            {
                "ok": 1,
                HelloCompat.LEGACY_CMD: True,
                "setName": "rs",
                "topologyVersion": topology_version,
            }
        )

        self.assertEqual(SERVER_TYPE.RSPrimary, s.server_type)
        self.assertEqual(topology_version, s.topology_version)

        # Resetting a server to unknown preserves topology_version.
        s_unknown = s.to_unknown()
        self.assertEqual(SERVER_TYPE.Unknown, s_unknown.server_type)
        self.assertEqual(topology_version, s_unknown.topology_version)

    def test_topology_version_not_present(self):
        # No topologyVersion field.
        s = parse_hello_response({"ok": 1, HelloCompat.LEGACY_CMD: True, "setName": "rs"})

        self.assertEqual(SERVER_TYPE.RSPrimary, s.server_type)
        self.assertEqual(None, s.topology_version)


if __name__ == "__main__":
    unittest.main()