Skip to content

Commit 6116096

Browse files
committed
add support for sharded-replicaset topology
1 parent f6ce79c commit 6116096

File tree

2 files changed

+53
-46
lines changed

2 files changed

+53
-46
lines changed

test/__init__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,13 +573,22 @@ def check_auth_with_sharding(self, func):
573573
func=func)
574574

575575
def is_topology_type(self, topologies):
576-
# TODO: add support for 'sharded-replicaset' topology type.
577576
if 'single' in topologies and not (self.is_mongos or self.is_rs):
578577
return True
579578
if 'replicaset' in topologies and self.is_rs:
580579
return True
581580
if 'sharded' in topologies and self.is_mongos:
582581
return True
582+
if 'sharded-replicaset' in topologies and self.is_mongos:
583+
shards = list(client_context.client.config.shards.find())
584+
for shard in shards:
585+
# For a 3-member RS-backed sharded cluster, shard['host']
586+
# will be 'replicaName/ip1:port1,ip2:port2,ip3:port3'
587+
# Otherwise it will be 'ip1:port1'
588+
host_spec = shard['host']
589+
if not len(host_spec.split('/')) > 1:
590+
return False
591+
return True
583592
return False
584593

585594
def require_cluster_type(self, topologies=[]):

test/unified_format.py

Lines changed: 43 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -89,50 +89,48 @@ def __prepare__(cls, name, this_bases):
8989
return type.__new__(metaclass, 'temporary_class', (), {})
9090

9191

92-
class SpecTestUtil(object):
93-
@staticmethod
94-
def is_run_on_requirement_satisfied(requirement):
95-
topology_satisfied = True
96-
req_topologies = requirement.get('topologies')
97-
if req_topologies:
98-
topology_satisfied = client_context.is_topology_type(
99-
req_topologies)
100-
101-
min_version_satisfied = True
102-
req_min_server_version = requirement.get('minServerVersion')
103-
if req_min_server_version:
104-
min_version_satisfied = Version.from_string(
105-
req_min_server_version) <= client_context.version
106-
107-
max_version_satisfied = True
108-
req_max_server_version = requirement.get('maxServerVersion')
109-
if req_max_server_version:
110-
max_version_satisfied = Version.from_string(
111-
req_max_server_version) >= client_context.version
112-
113-
return (topology_satisfied and min_version_satisfied and
114-
max_version_satisfied)
92+
def is_run_on_requirement_satisfied(requirement):
93+
topology_satisfied = True
94+
req_topologies = requirement.get('topologies')
95+
if req_topologies:
96+
topology_satisfied = client_context.is_topology_type(
97+
req_topologies)
11598

116-
@staticmethod
117-
def parse_collection_or_database_options(options):
118-
return parse_collection_options(options)
99+
min_version_satisfied = True
100+
req_min_server_version = requirement.get('minServerVersion')
101+
if req_min_server_version:
102+
min_version_satisfied = Version.from_string(
103+
req_min_server_version) <= client_context.version
119104

120-
@staticmethod
121-
def parse_bulk_write_result(result):
122-
upserted_ids = {str(int_idx): result.upserted_ids[int_idx]
123-
for int_idx in result.upserted_ids}
124-
return {
125-
'deletedCount': result.deleted_count,
126-
'insertedCount': result.inserted_count,
127-
'matchedCount': result.matched_count,
128-
'modifiedCount': result.modified_count,
129-
'upsertedCount': result.upserted_count,
130-
'upsertedIds': upserted_ids}
105+
max_version_satisfied = True
106+
req_max_server_version = requirement.get('maxServerVersion')
107+
if req_max_server_version:
108+
max_version_satisfied = Version.from_string(
109+
req_max_server_version) >= client_context.version
131110

132-
@staticmethod
133-
def parse_bulk_write_error_result(error):
134-
write_result = BulkWriteResult(error.details, True)
135-
return SpecTestUtil.parse_bulk_write_result(write_result)
111+
return (topology_satisfied and min_version_satisfied and
112+
max_version_satisfied)
113+
114+
115+
def parse_collection_or_database_options(options):
116+
return parse_collection_options(options)
117+
118+
119+
def parse_bulk_write_result(result):
120+
upserted_ids = {str(int_idx): result.upserted_ids[int_idx]
121+
for int_idx in result.upserted_ids}
122+
return {
123+
'deletedCount': result.deleted_count,
124+
'insertedCount': result.inserted_count,
125+
'matchedCount': result.matched_count,
126+
'modifiedCount': result.modified_count,
127+
'upsertedCount': result.upserted_count,
128+
'upsertedIds': upserted_ids}
129+
130+
131+
def parse_bulk_write_error_result(error):
132+
write_result = BulkWriteResult(error.details, True)
133+
return parse_bulk_write_result(write_result)
136134

137135

138136
class EventListenerUtil(CommandListener):
@@ -213,7 +211,7 @@ def _create_entity(self, entity_spec):
213211
self._test_class.fail(
214212
'Expected entity %s to be of type MongoClient, got %s' % (
215213
spec['client'], type(client)))
216-
options = SpecTestUtil.parse_collection_or_database_options(
214+
options = parse_collection_or_database_options(
217215
spec.get('databaseOptions', {}))
218216
self[spec['id']] = client.get_database(
219217
spec['databaseName'], **options)
@@ -224,7 +222,7 @@ def _create_entity(self, entity_spec):
224222
self._test_class.fail(
225223
'Expected entity %s to be of type Database, got %s' % (
226224
spec['database'], type(database)))
227-
options = SpecTestUtil.parse_collection_or_database_options(
225+
options = parse_collection_or_database_options(
228226
spec.get('collectionOptions', {}))
229227
self[spec['id']] = database.get_collection(
230228
spec['collectionName'], **options)
@@ -250,7 +248,7 @@ def _create_entity(self, entity_spec):
250248
elif entity_type == 'bucket':
251249
# TODO: implement the 'bucket' entity type
252250
self._test_class.skipTest(
253-
'GridFS entity types are not currently supported.')
251+
'GridFS is not currently supported (PYTHON-2459)')
254252
self._test_class.fail(
255253
'Unable to create entity of unknown type %s' % (entity_type,))
256254

@@ -515,7 +513,7 @@ def should_run_on(run_on_spec):
515513
return True
516514

517515
for req in run_on_spec:
518-
if SpecTestUtil.is_run_on_requirement_satisfied(req):
516+
if is_run_on_requirement_satisfied(req):
519517
return True
520518
return False
521519

0 commit comments

Comments
 (0)