本文整理汇总了Python中consul.ConsulException方法的典型用法代码示例。如果您正苦于以下问题:Python consul.ConsulException方法的具体用法?Python consul.ConsulException怎么用?Python consul.ConsulException使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类consul
的用法示例。
在下文中一共展示了consul.ConsulException方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_kv_acquire_release
# 需要导入模块: import consul [as 别名]
# 或者: from consul import ConsulException [as 别名]
def test_kv_acquire_release(self, consul_port):
c = consul.Consul(port=consul_port)
pytest.raises(
consul.ConsulException, c.kv.put, 'foo', 'bar', acquire='foo')
s1 = c.session.create()
s2 = c.session.create()
assert c.kv.put('foo', '1', acquire=s1) is True
assert c.kv.put('foo', '2', acquire=s2) is False
assert c.kv.put('foo', '1', acquire=s1) is True
assert c.kv.put('foo', '1', release='foo') is False
assert c.kv.put('foo', '2', release=s2) is False
assert c.kv.put('foo', '2', release=s1) is True
c.session.destroy(s1)
c.session.destroy(s2)
示例2: test_agent_register_check_no_service_id
# 需要导入模块: import consul [as 别名]
# 或者: from consul import ConsulException [as 别名]
def test_agent_register_check_no_service_id(self, consul_port):
c = consul.Consul(port=consul_port)
index, nodes = c.health.service("foo1")
assert nodes == []
pytest.raises(consul.std.base.ConsulException,
c.agent.check.register,
'foo', Check.ttl('100ms'),
service_id='foo1')
time.sleep(40 / 1000.0)
assert c.agent.checks() == {}
# Cleanup tasks
c.agent.check.deregister('foo')
time.sleep(40 / 1000.0)
示例3: test_acl_translate
# 需要导入模块: import consul [as 别名]
# 或者: from consul import ConsulException [as 别名]
def test_acl_translate(self, acl_consul):
c = consul.Consul(port=acl_consul.port, token=acl_consul.token)
payload = """
agent "" {
policy = "write"
}
"""
translate = c.acl.create_translate(
payload=payload, token=acl_consul.token)
assert translate == b'agent_prefix "" {\n policy = "write"\n}'
# fixme
pytest.raises(consul.ConsulException,
c.acl.get_translate,
c.acl.self()['AccessorID'],
acl_consul.token)
示例4: test_kv_acquire_release
# 需要导入模块: import consul [as 别名]
# 或者: from consul import ConsulException [as 别名]
def test_kv_acquire_release(self, acl_consul):
c = consul.Consul(port=acl_consul.port, token=acl_consul.token)
pytest.raises(
consul.ConsulException, c.kv.put, 'foo', 'bar', acquire='foo')
s1 = c.session.create()
s2 = c.session.create()
assert c.kv.put('foo', '1', acquire=s1) is True
assert c.kv.put('foo', '2', acquire=s2) is False
assert c.kv.put('foo', '1', acquire=s1) is True
assert c.kv.put('foo', '1', release='foo') is False
assert c.kv.put('foo', '2', release=s2) is False
assert c.kv.put('foo', '2', release=s1) is True
c.session.destroy(s1)
c.session.destroy(s2)
示例5: test_agent_register_check_no_service_id
# 需要导入模块: import consul [as 别名]
# 或者: from consul import ConsulException [as 别名]
def test_agent_register_check_no_service_id(self, consul_port):
c = consul.Consul(port=consul_port)
index, nodes = c.health.service("foo1")
assert nodes == []
pytest.raises(consul.std.base.ConsulException,
c.agent.check.register,
'foo', Check.ttl('100ms'),
service_id='foo1')
time.sleep(40/1000.0)
assert c.agent.checks() == {}
# Cleanup tasks
c.agent.check.deregister('foo')
time.sleep(40/1000.0)
示例6: get_primary
# 需要导入模块: import consul [as 别名]
# 或者: from consul import ConsulException [as 别名]
def get_primary(self, timeout=10):
"""
Returns the (name, IP) tuple for the instance that Consul thinks
is the healthy primary.
"""
while timeout > 0:
try:
nodes = self.client.health.service(PRIMARY_KEY, passing=True)[1]
log.debug(nodes)
instances = [service['Service'] for service in nodes]
if len(instances) > 1:
raise UnknownPrimary('Multiple primaries detected! %s', instances)
return instances[0]['ID'], instances[0]['Address']
except pyconsul.ConsulException as ex:
log.debug(ex)
timeout = timeout - 1
time.sleep(1)
except (IndexError, KeyError):
raise UnknownPrimary('No primary found')
raise WaitTimeoutError('Could not find primary before timeout.')
示例7: has_snapshot
# 需要导入模块: import consul [as 别名]
# 或者: from consul import ConsulException [as 别名]
def has_snapshot(self, timeout=600):
""" Ask Consul for 'last backup' key. """
while timeout > 0:
try:
result = self.client.kv.get(LAST_BACKUP_KEY)
if result[1]:
return json.loads(result[1]['Value'])['id']
return None
except pyconsul.ConsulException:
# Consul isn't up yet
timeout -= 1
time.sleep(1)
except (KeyError, TypeError, ValueError):
raise # unexpected value / invalid JSON in Consul
raise WaitTimeoutError('Could not contact Consul to check '
'for snapshot after %s seconds', timeout)
示例8: test_pre_start_snapshot_incomplete
# 需要导入模块: import consul [as 别名]
# 或者: from consul import ConsulException [as 别名]
def test_pre_start_snapshot_incomplete(self):
"""
Given a snapshot that has been marked successful but not
completed, a new node will wait and not crash.
"""
self.node.consul = Consul(get_environ())
self.node.consul.client = mock.MagicMock()
def kv_gets(*args, **kwargs):
yield pyconsul.ConsulException()
yield [0, {'Value': '{"id": "xxxx", "dt": "yyyyy"}'}]
self.node.consul.client.kv.get.side_effect = kv_gets()
manage.pre_start(self.node)
self.node.snaps.get_backup.assert_called_once()
self.assertEqual(self.node.consul.client.kv.get.call_count, 2)
self.node.mysql.restore_from_snapshot.assert_called_once()
self.assertFalse(self.node.mysql.initialize_db.called)
示例9: watch
# 需要导入模块: import consul [as 别名]
# 或者: from consul import ConsulException [as 别名]
def watch(self, leader_index, timeout):
self._last_session_refresh = 0
if self.__do_not_watch:
self.__do_not_watch = False
return True
if leader_index:
end_time = time.time() + timeout
while timeout >= 1:
try:
idx, _ = self._client.kv.get(self.leader_path, index=leader_index, wait=str(timeout) + 's')
return str(idx) != str(leader_index)
except (ConsulException, HTTPException, HTTPError, socket.error, socket.timeout):
logger.exception('watch')
timeout = end_time - time.time()
try:
return super(Consul, self).watch(None, timeout)
finally:
self.event.clear()
示例10: _retry
# 需要导入模块: import consul [as 别名]
# 或者: from consul import ConsulException [as 别名]
def _retry(self, operation, *args, **kw):
while 1:
try:
consul = self._get_consul()
self.log.debug('consul', consul=consul, operation=operation,
args=args)
if operation == 'GET':
index, result = consul.kv.get(*args, **kw)
elif operation == 'PUT':
result = consul.kv.put(*args, **kw)
elif operation == 'DELETE':
result = consul.kv.delete(*args, **kw)
else:
# Default case - consider operation as a function call
result = operation(*args, **kw)
self._clear_backoff()
break
except ConsulException, e:
self.log.exception('consul-not-up', e=e)
self._backoff('consul-not-up')
except ConnectionError, e:
self.log.exception('cannot-connect-to-consul', e=e)
self._backoff('cannot-connect-to-consul')
示例11: test_session
# 需要导入模块: import consul [as 别名]
# 或者: from consul import ConsulException [as 别名]
def test_session(self, consul_port):
c = consul.Consul(port=consul_port)
# session.create
pytest.raises(consul.ConsulException, c.session.create, node='n2')
pytest.raises(consul.ConsulException, c.session.create, dc='dc2')
session_id = c.session.create('my-session')
# session.list
pytest.raises(consul.ConsulException, c.session.list, dc='dc2')
_, sessions = c.session.list()
assert [x['Name'] for x in sessions] == ['my-session']
# session.info
pytest.raises(
consul.ConsulException, c.session.info, session_id, dc='dc2')
index, session = c.session.info('1' * 36)
assert session is None
index, session = c.session.info(session_id)
assert session['Name'] == 'my-session'
# session.node
node = session['Node']
pytest.raises(
consul.ConsulException, c.session.node, node, dc='dc2')
_, sessions = c.session.node(node)
assert [x['Name'] for x in sessions] == ['my-session']
# session.destroy
pytest.raises(
consul.ConsulException, c.session.destroy, session_id, dc='dc2')
assert c.session.destroy(session_id) is True
_, sessions = c.session.list()
assert sessions == []
示例12: test_snapshot
# 需要导入模块: import consul [as 别名]
# 或者: from consul import ConsulException [as 别名]
def test_snapshot(self, consul_port):
c = consul.Consul(port=consul_port)
current_path = os.path.abspath(
os.path.join(os.path.dirname(__file__), '.'))
assert c.snapshot.save(os.path.join(current_path, 'snapshot'))
assert c.snapshot.restore(os.path.join(current_path, 'snapshot'))
pytest.raises(consul.ConsulException,
c.snapshot.restore,
os.path.join(current_path, 'test_std.py'))
示例13: test_operator_keyring
# 需要导入模块: import consul [as 别名]
# 或者: from consul import ConsulException [as 别名]
def test_operator_keyring(self, consul_port):
c = consul.Consul(port=consul_port)
Key = "pUqJrVyVRj5jsiYEkM/tFQYfWyJIv4s3XkvDwy7Cu5s="
Key2 = "pUqJrVyVRj5jsiYEkM/tFQYfWyJIv4s3XkvDwy7Cu55="
assert c.operator.keyring.create(Key)
assert Key in {list(keyring['Keys'].keys())
[0] for keyring in c.operator.keyring.list()}
pytest.raises(consul.ConsulException, c.operator.keyring.update, Key2)
pytest.raises(consul.ConsulException, c.operator.keyring.delete, Key)
示例14: test_agent_register_check_no_service_id
# 需要导入模块: import consul [as 别名]
# 或者: from consul import ConsulException [as 别名]
def test_agent_register_check_no_service_id(self, acl_consul):
c = consul.Consul(port=acl_consul.port, token=acl_consul.token)
index, nodes = c.health.service("foo1")
assert nodes == []
pytest.raises(consul.std.base.ConsulException,
c.agent.check.register,
'foo', Check.ttl('100ms'),
service_id='foo1')
time.sleep(40 / 1000.0)
assert c.agent.checks() == {}
time.sleep(40 / 1000.0)
示例15: test_session
# 需要导入模块: import consul [as 别名]
# 或者: from consul import ConsulException [as 别名]
def test_session(self, acl_consul):
c = consul.Consul(port=acl_consul.port, token=acl_consul.token)
# session.create
pytest.raises(consul.ConsulException, c.session.create, node='n2')
pytest.raises(consul.ConsulException, c.session.create, dc='dc2')
session_id = c.session.create('my-session')
# session.list
pytest.raises(consul.ConsulException, c.session.list, dc='dc2')
_, sessions = c.session.list()
assert [x['Name'] for x in sessions] == ['my-session']
# session.info
pytest.raises(
consul.ConsulException, c.session.info, session_id, dc='dc2')
index, session = c.session.info('1' * 36)
assert session is None
index, session = c.session.info(session_id)
assert session['Name'] == 'my-session'
# session.node
node = session['Node']
pytest.raises(
consul.ConsulException, c.session.node, node, dc='dc2')
_, sessions = c.session.node(node)
assert [x['Name'] for x in sessions] == ['my-session']
# session.destroy
pytest.raises(
consul.ConsulException, c.session.destroy, session_id, dc='dc2')
assert c.session.destroy(session_id) is True
_, sessions = c.session.list()
assert sessions == []