本文整理汇总了Python中lib.rs.ReplicaSet.connection方法的典型用法代码示例。如果您正苦于以下问题:Python ReplicaSet.connection方法的具体用法?Python ReplicaSet.connection怎么用?Python ReplicaSet.connection使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类lib.rs.ReplicaSet
的用法示例。
在下文中一共展示了ReplicaSet.connection方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ReplicaSetAuthTestCase
# 需要导入模块: from lib.rs import ReplicaSet [as 别名]
# 或者: from lib.rs.ReplicaSet import connection [as 别名]
class ReplicaSetAuthTestCase(unittest.TestCase):
def setUp(self):
PortPool().change_range()
self.hosts = Hosts()
self.hosts.set_settings(os.environ.get('MONGOBIN', None))
self.repl_cfg = {'auth_key': 'secret', 'login': 'admin', 'password': 'admin', 'members': [{}, {}]}
self.repl = ReplicaSet(self.repl_cfg)
def tearDown(self):
if len(self.repl) > 0:
self.repl.cleanup()
def test_auth_connection(self):
self.assertTrue(isinstance(self.repl.connection().admin.collection_names(), list))
c = pymongo.MongoReplicaSetClient(self.repl.primary(), replicaSet=self.repl.repl_id)
self.assertRaises(pymongo.errors.OperationFailure, c.admin.collection_names)
def test_auth_admin(self):
c = pymongo.MongoReplicaSetClient(self.repl.primary(), replicaSet=self.repl.repl_id)
self.assertRaises(pymongo.errors.OperationFailure, c.admin.collection_names)
self.assertTrue(c.admin.authenticate('admin', 'admin'))
self.assertTrue(isinstance(c.admin.collection_names(), list))
self.assertTrue(c.admin.logout() is None)
self.assertRaises(pymongo.errors.OperationFailure, c.admin.collection_names)
def test_auth_collection(self):
c = pymongo.MongoReplicaSetClient(self.repl.primary(), replicaSet=self.repl.repl_id)
self.assertTrue(c.admin.authenticate('admin', 'admin'))
db = c.test_auth
db.add_user('user', 'userpass', roles=['readWrite'])
c.admin.logout()
self.assertTrue(db.authenticate('user', 'userpass'))
self.assertTrue(db.foo.insert({'foo': 'bar'}, safe=True, w=2, wtimeout=1000))
self.assertTrue(isinstance(db.foo.find_one(), dict))
db.logout()
self.assertRaises(pymongo.errors.OperationFailure, db.foo.find_one)
示例2: ReplicaSetTestCase
# 需要导入模块: from lib.rs import ReplicaSet [as 别名]
# 或者: from lib.rs.ReplicaSet import connection [as 别名]
#.........这里部分代码省略.........
self.assertTrue(self.repl.member_info(_id)['procInfo']['alive'])
def test_member_freeze(self):
self.repl_cfg = {"members": [{"rsParams": {"priority": 19}}, {"rsParams": {"priority": 7}}, {}]}
self.repl = ReplicaSet(self.repl_cfg)
primary_next = self.repl.member_info(2)['uri']
self.assertTrue(self.repl.member_freeze(1, 30))
self.repl.member_command(0, 'stop')
self.assertEqual(self.repl.primary(), primary_next)
time.sleep(40)
self.assertEqual(self.repl.primary(), self.repl.member_info(1)['uri'])
def test_members(self):
self.repl_cfg = {'members': [{}, {}]}
self.repl = ReplicaSet(self.repl_cfg)
members1 = sorted(self.repl.config['members'], key=lambda item: item['_id'])
members2 = sorted(self.repl.members(), key=lambda item: item['_id'])
self.assertEqual(len(members1), len(members2))
for i in xrange(len(members1)):
self.assertEqual(members1[i]['host'], members2[i]['host'])
self.assertEqual(members1[i]['_id'], members2[i]['_id'])
def test_stepdown(self):
self.repl_cfg = {'members': [{}, {}]}
self.repl = ReplicaSet(self.repl_cfg)
primary = self.repl.primary()
self.assertTrue(self.repl.stepdown())
self.assertNotEqual(primary, self.repl.primary())
def test_primary(self):
self.repl_cfg = {'members': [{}, {}]}
self.repl = ReplicaSet(self.repl_cfg)
primary = self.repl.primary()
self.assertTrue(Hosts().info(Hosts().id_by_hostname(primary))['statuses']['primary'])
def test_get_members_in_state(self):
self.repl_cfg = {'members': [{}, {}]}
self.repl = ReplicaSet(self.repl_cfg)
primaries = self.repl.get_members_in_state(1)
self.assertEqual(len(primaries), 1)
self.assertEqual(primaries[0], self.repl.primary())
def test_connection(self):
self.repl_cfg = {'members': [{}, {}]}
self.repl = ReplicaSet(self.repl_cfg)
_id = 1
hostname = self.repl.id2host(_id)
self.assertTrue(self.repl.connection(timeout=5))
self.assertTrue(self.repl.connection(hostname=hostname, timeout=5))
self.repl.member_command(_id, 'stop')
self.assertRaises(pymongo.errors.AutoReconnect, lambda: self.repl.connection(hostname=hostname, timeout=5))
def test_secondaries(self):
self.repl_cfg = {'members': [{}, {}]}
self.repl = ReplicaSet(self.repl_cfg)
secondaries = [item['host'] for item in self.repl.secondaries()]
self.assertEqual(secondaries, self.repl.get_members_in_state(2))
def test_arbiters(self):
self.repl = ReplicaSet(self.repl_cfg)
arbiters = [item['host'] for item in self.repl.arbiters()]
self.assertEqual(arbiters, self.repl.get_members_in_state(7))
def test_hidden(self):
self.repl = ReplicaSet(self.repl_cfg)
for _ in self.repl.hidden():
self.assertTrue(self.repl.run_command('serverStatus', arg=None, is_eval=False, member_id=2)['repl']['hidden'])
def test_passives(self):
self.repl = ReplicaSet(self.repl_cfg)
self.repl.repl_member_add({"rsParams": {"priority": 0}})
for member in self.repl.passives():
self.assertTrue(member['host'] in self.repl.run_command('isMaster', is_eval=False).get('passives'))
def test_hosts(self):
self.repl_cfg = {'members': [{}, {}]}
self.repl = ReplicaSet(self.repl_cfg)
self.repl.repl_member_add({"rsParams": {"priority": 0}})
for member in self.repl.hosts():
self.assertTrue(member['host'] in self.repl.run_command('isMaster', is_eval=False).get('hosts'))
def test_compare_hosts_passives(self):
self.repl = ReplicaSet(self.repl_cfg)
self.repl.repl_member_add({"rsParams": {"priority": 0}})
self.repl.repl_member_add({})
hosts = self.repl.hosts()
passives = self.repl.passives()
for item in hosts:
self.assertTrue(item not in passives)
for item in passives:
self.assertTrue(item not in hosts)
def test_wait_while_reachable(self):
self.repl_cfg = {'members': [{}, {}]}
self.repl = ReplicaSet(self.repl_cfg)
hosts = [member['host'] for member in self.repl.members()]
self.assertTrue(self.repl.wait_while_reachable(hosts, timeout=10))
self.repl.member_command(1, 'stop')
self.assertFalse(self.repl.wait_while_reachable(hosts, timeout=10))