本文整理汇总了Python中test.utils.is_mongos函数的典型用法代码示例。如果您正苦于以下问题:Python is_mongos函数的具体用法?Python is_mongos怎么用?Python is_mongos使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了is_mongos函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_authenticate_and_request
def test_authenticate_and_request(self):
if is_mongos(self.client) and not version.at_least(self.client, (2, 0, 0)):
raise SkipTest("Auth with sharding requires MongoDB >= 2.0.0")
if is_mongos(self.client):
raise SkipTest("Auth fails on sharding due to write concern race, see Tokutek/mongo#77")
if not server_started_with_auth(self.client):
raise SkipTest("Authentication is not enabled on server")
# Database.authenticate() needs to be in a request - check that it
# always runs in a request, and that it restores the request state
# (in or not in a request) properly when it's finished.
self.assertFalse(self.client.auto_start_request)
db = self.client.pymongo_test
db.add_user("mike", "password", roles=["userAdmin", "dbAdmin", "readWrite"])
try:
self.assertFalse(self.client.in_request())
self.assertTrue(db.authenticate("mike", "password"))
self.assertFalse(self.client.in_request())
request_cx = get_client(auto_start_request=True)
request_db = request_cx.pymongo_test
self.assertTrue(request_db.authenticate("mike", "password"))
self.assertTrue(request_cx.in_request())
finally:
db.authenticate("mike", "password")
db.remove_user("mike")
db.logout()
request_db.logout()
示例2: setUp
def setUp(self):
client = MongoClient(host, port)
# Sharded auth not supported before MongoDB 2.0
if is_mongos(client) and not version.at_least(client, (2, 0, 0)):
raise SkipTest("Auth with sharding requires MongoDB >= 2.0.0")
if not server_started_with_auth(client):
raise SkipTest('Authentication is not enabled on server')
response = client.admin.command('ismaster')
self.set_name = str(response.get('setName', ''))
client.admin.add_user('admin', 'pass', roles=['userAdminAnyDatabase',
'dbAdminAnyDatabase',
'readWriteAnyDatabase',
'clusterAdmin'])
client.admin.authenticate('admin', 'pass')
client.pymongo_test.add_user('user', 'pass',
roles=['userAdmin', 'readWrite'])
if self.set_name:
# GLE requires authentication.
client.admin.authenticate('admin', 'pass')
# Make sure the admin user is replicated after calling add_user
# above. This avoids a race in the MRSC tests below. Adding a
# user is just an insert into system.users.
client.admin.command('getLastError', w=len(response['hosts']))
self.client = client
示例3: test_auth_from_uri
def test_auth_from_uri(self):
c = Connection(self.host, self.port)
# Sharded auth not supported before MongoDB 2.0
if is_mongos(c) and not version.at_least(c, (2, 0, 0)):
raise SkipTest("Auth with sharding requires MongoDB >= 2.0.0")
c.admin.system.users.remove({})
c.pymongo_test.system.users.remove({})
c.admin.add_user("admin", "pass")
c.admin.authenticate("admin", "pass")
c.pymongo_test.add_user("user", "pass")
self.assertRaises(ConfigurationError, Connection,
"mongodb://foo:[email protected]%s:%d" % (self.host, self.port))
self.assertRaises(ConfigurationError, Connection,
"mongodb://admin:[email protected]%s:%d" % (self.host, self.port))
self.assertRaises(ConfigurationError, Connection,
"mongodb://user:[email protected]%s:%d" % (self.host, self.port))
Connection("mongodb://admin:[email protected]%s:%d" % (self.host, self.port))
self.assertRaises(ConfigurationError, Connection,
"mongodb://admin:[email protected]%s:%d/pymongo_test" %
(self.host, self.port))
self.assertRaises(ConfigurationError, Connection,
"mongodb://user:[email protected]%s:%d/pymongo_test" %
(self.host, self.port))
Connection("mongodb://user:[email protected]%s:%d/pymongo_test" %
(self.host, self.port))
c.admin.system.users.remove({})
c.pymongo_test.system.users.remove({})
示例4: test_copy_db
def test_copy_db(self):
authed_client = auth_context.client
if is_mongos(authed_client):
raise SkipTest("SERVER-6427")
c = MongoClient(host, port)
authed_client.admin.add_user("admin", "password")
c.admin.authenticate("admin", "password")
c.drop_database("pymongo_test")
c.drop_database("pymongo_test1")
c.pymongo_test.test.insert({"foo": "bar"})
try:
c.pymongo_test.add_user("mike", "password")
self.assertRaises(OperationFailure, c.copy_database,
"pymongo_test", "pymongo_test1",
username="foo", password="bar")
self.assertFalse("pymongo_test1" in c.database_names())
self.assertRaises(OperationFailure, c.copy_database,
"pymongo_test", "pymongo_test1",
username="mike", password="bar")
self.assertFalse("pymongo_test1" in c.database_names())
c.copy_database("pymongo_test", "pymongo_test1",
username="mike", password="password")
self.assertTrue("pymongo_test1" in c.database_names())
self.assertEqual("bar", c.pymongo_test1.test.find_one()["foo"])
finally:
# Cleanup
remove_all_users(c.pymongo_test)
c.admin.remove_user("admin")
c.disconnect()
示例5: test_copy_db
def test_copy_db(self):
c = Connection(self.host, self.port)
self.assertTrue(c.in_request())
self.assertRaises(TypeError, c.copy_database, 4, "foo")
self.assertRaises(TypeError, c.copy_database, "foo", 4)
self.assertRaises(InvalidName, c.copy_database, "foo", "$foo")
c.pymongo_test.test.drop()
c.drop_database("pymongo_test1")
c.drop_database("pymongo_test2")
c.pymongo_test.test.insert({"foo": "bar"})
# Due to SERVER-2329, databases may not disappear from a master in a
# master-slave pair
if not server_is_master_with_slave(c):
self.assertFalse("pymongo_test1" in c.database_names())
self.assertFalse("pymongo_test2" in c.database_names())
c.copy_database("pymongo_test", "pymongo_test1")
# copy_database() didn't accidentally end the request
self.assertTrue(c.in_request())
self.assertTrue("pymongo_test1" in c.database_names())
self.assertEqual("bar", c.pymongo_test1.test.find_one()["foo"])
c.end_request()
self.assertFalse(c.in_request())
c.copy_database("pymongo_test", "pymongo_test2", "%s:%d" % (self.host, self.port))
# copy_database() didn't accidentally restart the request
self.assertFalse(c.in_request())
self.assertTrue("pymongo_test2" in c.database_names())
self.assertEqual("bar", c.pymongo_test2.test.find_one()["foo"])
if version.at_least(c, (1, 3, 3, 1)):
c.drop_database("pymongo_test1")
c.pymongo_test.add_user("mike", "password")
self.assertRaises(
OperationFailure, c.copy_database, "pymongo_test", "pymongo_test1", username="foo", password="bar"
)
if not server_is_master_with_slave(c):
self.assertFalse("pymongo_test1" in c.database_names())
self.assertRaises(
OperationFailure, c.copy_database, "pymongo_test", "pymongo_test1", username="mike", password="bar"
)
if not server_is_master_with_slave(c):
self.assertFalse("pymongo_test1" in c.database_names())
if not is_mongos(c):
# See SERVER-6427
c.copy_database("pymongo_test", "pymongo_test1", username="mike", password="password")
self.assertTrue("pymongo_test1" in c.database_names())
self.assertEqual("bar", c.pymongo_test1.test.find_one()["foo"])
示例6: test_authenticate_and_safe
def test_authenticate_and_safe(self):
if (is_mongos(self.client) and not
version.at_least(self.client, (2, 0, 0))):
raise SkipTest("Auth with sharding requires MongoDB >= 2.0.0")
if not server_started_with_auth(self.client):
raise SkipTest('Authentication is not enabled on server')
db = self.client.auth_test
db.add_user("bernie", "password",
roles=["userAdmin", "dbAdmin", "readWrite"])
db.authenticate("bernie", "password")
try:
db.test.remove({})
self.assertTrue(db.test.insert({"bim": "baz"}))
self.assertEqual(1, db.test.count())
self.assertEqual(1,
db.test.update({"bim": "baz"},
{"$set": {"bim": "bar"}}).get('n'))
self.assertEqual(1,
db.test.remove({}).get('n'))
self.assertEqual(0, db.test.count())
finally:
db.remove_user("bernie")
db.logout()
示例7: test_authenticate_and_request
def test_authenticate_and_request(self):
if (is_mongos(self.client) and not
version.at_least(self.client, (2, 0, 0))):
raise SkipTest("Auth with sharding requires MongoDB >= 2.0.0")
# Database.authenticate() needs to be in a request - check that it
# always runs in a request, and that it restores the request state
# (in or not in a request) properly when it's finished.
self.assertFalse(self.client.auto_start_request)
db = self.client.pymongo_test
db.system.users.remove({})
db.remove_user("mike")
db.add_user("mike", "password")
self.assertFalse(self.client.in_request())
self.assertTrue(db.authenticate("mike", "password"))
self.assertFalse(self.client.in_request())
request_cx = get_client(auto_start_request=True)
request_db = request_cx.pymongo_test
self.assertTrue(request_cx.in_request())
self.assertTrue(request_db.authenticate("mike", "password"))
self.assertTrue(request_cx.in_request())
# just make sure there are no exceptions here
db.logout()
db.collection.find_one()
request_db.logout()
request_db.collection.find_one()
示例8: test_only_secondary_ok_commands_have_read_prefs
def test_only_secondary_ok_commands_have_read_prefs(self):
c = get_connection(read_preference=ReadPreference.SECONDARY)
is_mongos = utils.is_mongos(c)
if not is_mongos:
raise SkipTest("Only mongos have read_prefs added to the spec")
# Ensure secondary_ok_commands have readPreference
for cmd in secondary_ok_commands:
if cmd == 'mapreduce': # map reduce is a special case
continue
command = SON([(cmd, 1)])
cursor = c.pymongo_test["$cmd"].find(command.copy())
command['$readPreference'] = {'mode': 'secondary'}
self.assertEqual(command, cursor._Cursor__query_spec())
# map_reduce inline should have read prefs
command = SON([('mapreduce', 'test'), ('out', {'inline': 1})])
cursor = c.pymongo_test["$cmd"].find(command.copy())
command['$readPreference'] = {'mode': 'secondary'}
self.assertEqual(command, cursor._Cursor__query_spec())
# map_reduce that outputs to a collection shouldn't have read prefs
command = SON([('mapreduce', 'test'), ('out', {'mrtest': 1})])
cursor = c.pymongo_test["$cmd"].find(command.copy())
self.assertEqual(command, cursor._Cursor__query_spec())
# Other commands shouldn't be changed
for cmd in ('drop', 'create', 'any-future-cmd'):
command = SON([(cmd, 1)])
cursor = c.pymongo_test["$cmd"].find(command.copy())
self.assertEqual(command, cursor._Cursor__query_spec())
示例9: test_fsync_lock_unlock
def test_fsync_lock_unlock(self):
c = get_client()
if is_mongos(c):
raise SkipTest('fsync/lock not supported by mongos')
if not version.at_least(c, (2, 0)) and server_started_with_auth(c):
raise SkipTest('Requires server >= 2.0 to test with auth')
res = c.admin.command('getCmdLineOpts')
if '--master' in res['argv'] and version.at_least(c, (2, 3, 0)):
raise SkipTest('SERVER-7714')
self.assertFalse(c.is_locked)
# async flushing not supported on windows...
if sys.platform not in ('cygwin', 'win32'):
c.fsync(async=True)
self.assertFalse(c.is_locked)
c.fsync(lock=True)
self.assertTrue(c.is_locked)
locked = True
c.unlock()
for _ in xrange(5):
locked = c.is_locked
if not locked:
break
time.sleep(1)
self.assertFalse(locked)
示例10: test_profiling_info
def test_profiling_info(self):
if is_mongos(self.client):
raise SkipTest('profile is not supported by mongos')
db = self.client.pymongo_test
db.set_profiling_level(ALL)
db.test.find_one()
db.set_profiling_level(OFF)
info = db.profiling_info()
self.assertTrue(isinstance(info, list))
# Check if we're going to fail because of SERVER-4754, in which
# profiling info isn't collected if mongod was started with --auth
if server_started_with_auth(self.client):
raise SkipTest(
"We need SERVER-4754 fixed for the rest of this test to pass"
)
self.assertTrue(len(info) >= 1)
# These basically clue us in to server changes.
if version.at_least(db.connection, (1, 9, 1, -1)):
self.assertTrue(isinstance(info[0]['responseLength'], int))
self.assertTrue(isinstance(info[0]['millis'], int))
self.assertTrue(isinstance(info[0]['client'], basestring))
self.assertTrue(isinstance(info[0]['user'], basestring))
self.assertTrue(isinstance(info[0]['ns'], basestring))
self.assertTrue(isinstance(info[0]['op'], basestring))
else:
self.assertTrue(isinstance(info[0]["info"], basestring))
self.assertTrue(isinstance(info[0]["millis"], float))
self.assertTrue(isinstance(info[0]["ts"], datetime.datetime))
示例11: setUpModule
def setUpModule():
if not auth_context.auth_enabled:
raise SkipTest("Server not started with --auth.")
if (is_mongos(auth_context.client) and
not version.at_least(auth_context.client, (2, 0, 0))):
raise SkipTest("Auth with sharding requires MongoDB >= 2.0.0")
auth_context.add_user_and_log_in()
示例12: test_profiling_levels
def test_profiling_levels(self):
if is_mongos(self.client):
raise SkipTest('profile is not supported by mongos')
db = self.client.pymongo_test
self.assertEqual(db.profiling_level(), OFF) # default
self.assertRaises(ValueError, db.set_profiling_level, 5.5)
self.assertRaises(ValueError, db.set_profiling_level, None)
self.assertRaises(ValueError, db.set_profiling_level, -1)
self.assertRaises(TypeError, db.set_profiling_level, SLOW_ONLY, 5.5)
self.assertRaises(TypeError, db.set_profiling_level, SLOW_ONLY, '1')
db.set_profiling_level(SLOW_ONLY)
self.assertEqual(db.profiling_level(), SLOW_ONLY)
db.set_profiling_level(ALL)
self.assertEqual(db.profiling_level(), ALL)
db.set_profiling_level(OFF)
self.assertEqual(db.profiling_level(), OFF)
db.set_profiling_level(SLOW_ONLY, 50)
self.assertEqual(50, db.command("profile", -1)['slowms'])
db.set_profiling_level(ALL, -1)
self.assertEqual(-1, db.command("profile", -1)['slowms'])
db.set_profiling_level(OFF, 100) # back to default
self.assertEqual(100, db.command("profile", -1)['slowms'])
示例13: test_errors
def test_errors(self):
if is_mongos(self.client):
raise SkipTest('getpreverror not supported by mongos')
db = self.client.pymongo_test
db.reset_error_history()
self.assertEqual(None, db.error())
self.assertEqual(None, db.previous_error())
db.command("forceerror", check=False)
self.assertTrue(db.error())
self.assertTrue(db.previous_error())
db.command("forceerror", check=False)
self.assertTrue(db.error())
prev_error = db.previous_error()
self.assertEqual(prev_error["nPrev"], 1)
del prev_error["nPrev"]
prev_error.pop("lastOp", None)
error = db.error()
error.pop("lastOp", None)
# getLastError includes "connectionId" in recent
# server versions, getPrevError does not.
error.pop("connectionId", None)
self.assertEqual(error, prev_error)
db.test.find_one()
self.assertEqual(None, db.error())
self.assertTrue(db.previous_error())
self.assertEqual(db.previous_error()["nPrev"], 2)
db.reset_error_history()
self.assertEqual(None, db.error())
self.assertEqual(None, db.previous_error())
示例14: test_only_secondary_ok_commands_have_read_prefs
def test_only_secondary_ok_commands_have_read_prefs(self):
c = get_connection(read_preference=ReadPreference.SECONDARY)
is_mongos = utils.is_mongos(c)
if not is_mongos:
raise SkipTest("Only mongos have read_prefs added to the spec")
# Ensure secondary_ok_commands have readPreference
for cmd in secondary_ok_commands:
if cmd == "mapreduce": # map reduce is a special case
continue
command = SON([(cmd, 1)])
cursor = c.pymongo_test["$cmd"].find(command.copy())
# White-listed commands also have to be wrapped in $query
command = SON([("$query", command)])
command["$readPreference"] = {"mode": "secondary"}
self.assertEqual(command, cursor._Cursor__query_spec())
# map_reduce inline should have read prefs
command = SON([("mapreduce", "test"), ("out", {"inline": 1})])
cursor = c.pymongo_test["$cmd"].find(command.copy())
# White-listed commands also have to be wrapped in $query
command = SON([("$query", command)])
command["$readPreference"] = {"mode": "secondary"}
self.assertEqual(command, cursor._Cursor__query_spec())
# map_reduce that outputs to a collection shouldn't have read prefs
command = SON([("mapreduce", "test"), ("out", {"mrtest": 1})])
cursor = c.pymongo_test["$cmd"].find(command.copy())
self.assertEqual(command, cursor._Cursor__query_spec())
# Other commands shouldn't be changed
for cmd in ("drop", "create", "any-future-cmd"):
command = SON([(cmd, 1)])
cursor = c.pymongo_test["$cmd"].find(command.copy())
self.assertEqual(command, cursor._Cursor__query_spec())
示例15: test_mongos_connection
def test_mongos_connection(self):
c = get_connection()
is_mongos = utils.is_mongos(c)
# Test default mode, PRIMARY
cursor = c.pymongo_test.test.find()
if is_mongos:
self.assertEqual(
{'mode': 'primary'},
cursor._Cursor__query_spec().get('$readPreference')
)
else:
self.assertFalse(
'$readPreference' in cursor._Cursor__query_spec())
# Test non-PRIMARY modes which can be combined with tags
for mode, mongos_mode in (
(ReadPreference.PRIMARY_PREFERRED, 'primaryPreferred'),
(ReadPreference.SECONDARY, 'secondary'),
(ReadPreference.SECONDARY_PREFERRED, 'secondaryPreferred'),
(ReadPreference.NEAREST, 'nearest'),
):
for tag_sets in (
None, [{}]
):
c = get_connection(
read_preference=mode,
tag_sets=tag_sets)
self.assertEqual(is_mongos, c.is_mongos)
cursor = c.pymongo_test.test.find()
if is_mongos:
self.assertEqual(
{'mode': mongos_mode},
cursor._Cursor__query_spec().get('$readPreference')
)
else:
self.assertFalse(
'$readPreference' in cursor._Cursor__query_spec())
for tag_sets in (
[{'dc': 'la'}],
[{'dc': 'la'}, {'dc': 'sf'}],
[{'dc': 'la'}, {'dc': 'sf'}, {}],
):
c = get_connection(
read_preference=mode,
tag_sets=tag_sets)
self.assertEqual(is_mongos, c.is_mongos)
cursor = c.pymongo_test.test.find()
if is_mongos:
self.assertEqual(
{'mode': mongos_mode, 'tags': tag_sets},
cursor._Cursor__query_spec().get('$readPreference'))
else:
self.assertFalse(
'$readPreference' in cursor._Cursor__query_spec())