Skip to content

Commit 79cf607

Browse files
committed
PYTHON-1714 Add c extension use to client metadata
- Fix metadata and handshakek tests - Factor out client connection into _test_metadata - Compare metadata keys, values instead of dictionary comparison
1 parent 0b9de8e commit 79cf607

File tree

2 files changed

+47
-53
lines changed

2 files changed

+47
-53
lines changed

test/asynchronous/test_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ async def test_read_preference(self):
343343

344344
async def test_metadata(self):
345345
metadata = copy.deepcopy(_METADATA)
346-
metadata["driver"]["name"] = "PyMongo|c|async"
346+
metadata["driver"]["name"] = "PyMongo|async"
347347
metadata["application"] = {"name": "foobar"}
348348
client = self.simple_client("mongodb://foo:27017/?appname=foobar&connect=false")
349349
options = client.options

test/test_client.py

Lines changed: 46 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -333,76 +333,55 @@ def test_read_preference(self):
333333
)
334334
self.assertEqual(c.read_preference, ReadPreference.NEAREST)
335335

336-
def test_metadata(self):
336+
def _test_metadata(self, add_meta, *args, **kwargs):
337337
metadata = copy.deepcopy(_METADATA)
338-
metadata["driver"]["name"] = "PyMongo|c"
339-
metadata["application"] = {"name": "foobar"}
340-
client = self.simple_client("mongodb://foo:27017/?appname=foobar&connect=false")
341-
options = client.options
342-
self.assertEqual(options.pool_options.metadata, metadata)
343-
client = self.simple_client("foo", 27017, appname="foobar", connect=False)
338+
metadata["driver"]["name"] = "PyMongo"
339+
metadata.update(add_meta)
340+
client = self.simple_client(*args, **kwargs)
344341
options = client.options
345-
self.assertEqual(options.pool_options.metadata, metadata)
342+
self.assertIn(metadata["driver"]["name"], options.pool_options.metadata["driver"]["name"])
343+
if "application" in metadata.keys():
344+
self.assertEqual(metadata["application"]["name"], options.pool_options.metadata["application"]["name"])
345+
346+
self.assertLessEqual(
347+
len(bson.encode(options.pool_options.metadata)),
348+
_MAX_METADATA_SIZE,
349+
)
350+
351+
def test_metadata(self):
352+
self._test_metadata({"driver": {"name": "PyMongo"}, "application": {"name": "foobar"}}, "mongodb://foo:27017/?appname=foobar&connect=false")
353+
self._test_metadata({"driver": {"name": "PyMongo"}}, "foo", 27017, appname="foobar", connect=False)
354+
346355
# No error
347-
self.simple_client(appname="x" * 128)
356+
self._test_metadata({}, appname="x" * 128)
357+
348358
with self.assertRaises(ValueError):
349-
self.simple_client(appname="x" * 129)
359+
self._test_metadata({}, appname="x" * 129)
360+
350361
# Bad "driver" options.
351362
self.assertRaises(TypeError, DriverInfo, "Foo", 1, "a")
352363
self.assertRaises(TypeError, DriverInfo, version="1", platform="a")
353364
self.assertRaises(TypeError, DriverInfo)
365+
354366
with self.assertRaises(TypeError):
355367
self.simple_client(driver=1)
356368
with self.assertRaises(TypeError):
357369
self.simple_client(driver="abc")
358370
with self.assertRaises(TypeError):
359371
self.simple_client(driver=("Foo", "1", "a"))
372+
360373
# Test appending to driver info.
361-
metadata["driver"]["name"] = "PyMongo|c|FooDriver"
362-
metadata["driver"]["version"] = "{}|1.2.3".format(_METADATA["driver"]["version"])
363-
client = self.simple_client(
364-
"foo",
365-
27017,
366-
appname="foobar",
367-
driver=DriverInfo("FooDriver", "1.2.3", None),
368-
connect=False,
369-
)
370-
options = client.options
371-
self.assertEqual(options.pool_options.metadata, metadata)
372-
metadata["platform"] = "{}|FooPlatform".format(_METADATA["platform"])
373-
client = self.simple_client(
374-
"foo",
375-
27017,
376-
appname="foobar",
377-
driver=DriverInfo("FooDriver", "1.2.3", "FooPlatform"),
378-
connect=False,
379-
)
380-
options = client.options
381-
self.assertEqual(options.pool_options.metadata, metadata)
374+
self._test_metadata({"driver": {"name": "FooDriver", "version": "{}|1.2.3".format(_METADATA["driver"]["version"])}}, "foo", 27017, appname="foobar", driver=DriverInfo("FooDriver", "1.2.3", None), connect=False)
375+
self._test_metadata({"platform": "{}|FooPlatform".format(_METADATA["platform"])}, "foo", 27017, appname="foobar", driver=DriverInfo("FooDriver", "1.2.3", "FooPlatform"), connect=False)
376+
382377
# Test truncating driver info metadata.
383-
client = self.simple_client(
384-
driver=DriverInfo(name="s" * _MAX_METADATA_SIZE),
385-
connect=False,
386-
)
387-
options = client.options
388-
self.assertLessEqual(
389-
len(bson.encode(options.pool_options.metadata)),
390-
_MAX_METADATA_SIZE,
391-
)
392-
client = self.simple_client(
393-
driver=DriverInfo(name="s" * _MAX_METADATA_SIZE, version="s" * _MAX_METADATA_SIZE),
394-
connect=False,
395-
)
396-
options = client.options
397-
self.assertLessEqual(
398-
len(bson.encode(options.pool_options.metadata)),
399-
_MAX_METADATA_SIZE,
400-
)
378+
self._test_metadata({}, driver=DriverInfo(name="s" * _MAX_METADATA_SIZE), connect=False)
379+
self._test_metadata({}, driver=DriverInfo(name="s" * _MAX_METADATA_SIZE, version="s" * _MAX_METADATA_SIZE), connect=False)
401380

402381
@mock.patch.dict("os.environ", {ENV_VAR_K8S: "1"})
403382
def test_container_metadata(self):
404383
metadata = copy.deepcopy(_METADATA)
405-
metadata["driver"]["name"] = "PyMongo|c"
384+
metadata["driver"]["name"] = "PyMongo"
406385
metadata["env"] = {}
407386
metadata["env"]["container"] = {"orchestrator": "kubernetes"}
408387
client = self.simple_client("mongodb://foo:27017/?appname=foobar&connect=false")
@@ -1885,7 +1864,7 @@ def test_sigstop_sigcont(self):
18851864
def _test_handshake(self, env_vars, expected_env):
18861865
with patch.dict("os.environ", env_vars):
18871866
metadata = copy.deepcopy(_METADATA)
1888-
metadata["driver"]["name"] = "PyMongo|c"
1867+
metadata["driver"]["name"] = "PyMongo"
18891868
if expected_env is not None:
18901869
metadata["env"] = expected_env
18911870

@@ -1894,7 +1873,22 @@ def _test_handshake(self, env_vars, expected_env):
18941873
client = self.rs_or_single_client(serverSelectionTimeoutMS=10000)
18951874
client.admin.command("ping")
18961875
options = client.options
1897-
self.assertEqual(options.pool_options.metadata, metadata)
1876+
self.assertIn(metadata["driver"]["name"], options.pool_options.metadata["driver"]["name"])
1877+
self.assertEqual(metadata["driver"]["version"], options.pool_options.metadata["driver"]["version"])
1878+
self.assertEqual(metadata["os"]["type"], options.pool_options.metadata["os"]["type"])
1879+
self.assertEqual(metadata["os"]["name"], options.pool_options.metadata["os"]["name"])
1880+
self.assertEqual(metadata["os"]["architecture"], options.pool_options.metadata["os"]["architecture"])
1881+
self.assertEqual(metadata["os"]["version"], options.pool_options.metadata["os"]["version"])
1882+
self.assertEqual(metadata["platform"], options.pool_options.metadata["platform"])
1883+
if "env" in options.pool_options.metadata.keys():
1884+
self.assertEqual(metadata["env"]["name"], options.pool_options.metadata["env"]["name"])
1885+
if "region" in options.pool_options.metadata["env"].keys():
1886+
self.assertEqual(metadata["env"]["region"], options.pool_options.metadata["env"]["region"])
1887+
if "memory_mb" in options.pool_options.metadata["env"].keys():
1888+
self.assertEqual(metadata["env"]["memory_mb"], options.pool_options.metadata["env"]["memory_mb"])
1889+
if "timeout_sec" in options.pool_options.metadata["env"].keys():
1890+
self.assertEqual(metadata["env"]["timeout_sec"], options.pool_options.metadata["env"]["timeout_sec"])
1891+
18981892

18991893
def test_handshake_01_aws(self):
19001894
self._test_handshake(

0 commit comments

Comments
 (0)