import base64
import operator
import struct
import time

import pytest
import six

import consul
import consul.std


Check = consul.Check


class TestHTTPClient(object):
    def test_uri(self):
        http = consul.std.HTTPClient()
        assert http.uri('/v1/kv') == 'http://127.0.0.1:8500/v1/kv'
        assert http.uri('/v1/kv', params={'index': 1}) == \
            'http://127.0.0.1:8500/v1/kv?index=1'


class TestConsul(object):
    def test_kv(self, consul_port):
        c = consul.Consul(port=consul_port)
        index, data = c.kv.get('foo')
        assert data is None
        assert c.kv.put('foo', 'bar') is True
        index, data = c.kv.get('foo')
        assert data['Value'] == six.b('bar')

    def test_kv_wait(self, consul_port):
        c = consul.Consul(port=consul_port)
        assert c.kv.put('foo', 'bar') is True
        index, data = c.kv.get('foo')
        check, data = c.kv.get('foo', index=index, wait='20ms')
        assert index == check

    def test_kv_encoding(self, consul_port):
        c = consul.Consul(port=consul_port)

        # test binary
        c.kv.put('foo', struct.pack('i', 1000))
        index, data = c.kv.get('foo')
        assert struct.unpack('i', data['Value']) == (1000,)

        # test unicode
        c.kv.put('foo', u'bar')
        index, data = c.kv.get('foo')
        assert data['Value'] == six.b('bar')

        # test empty-string comes back as `None`
        c.kv.put('foo', '')
        index, data = c.kv.get('foo')
        assert data['Value'] is None

        # test None
        c.kv.put('foo', None)
        index, data = c.kv.get('foo')
        assert data['Value'] is None

        # check unencoded values raises assert
        pytest.raises(AssertionError, c.kv.put, 'foo', {1: 2})

    def test_kv_put_cas(self, consul_port):
        c = consul.Consul(port=consul_port)
        assert c.kv.put('foo', 'bar', cas=50) is False
        assert c.kv.put('foo', 'bar', cas=0) is True
        index, data = c.kv.get('foo')

        assert c.kv.put('foo', 'bar2', cas=data['ModifyIndex']-1) is False
        assert c.kv.put('foo', 'bar2', cas=data['ModifyIndex']) is True
        index, data = c.kv.get('foo')
        assert data['Value'] == six.b('bar2')

    def test_kv_put_flags(self, consul_port):
        c = consul.Consul(port=consul_port)
        c.kv.put('foo', 'bar')
        index, data = c.kv.get('foo')
        assert data['Flags'] == 0

        assert c.kv.put('foo', 'bar', flags=50) is True
        index, data = c.kv.get('foo')
        assert data['Flags'] == 50

    def test_kv_recurse(self, consul_port):
        c = consul.Consul(port=consul_port)
        index, data = c.kv.get('foo/', recurse=True)
        assert data is None

        c.kv.put('foo/', None)
        index, data = c.kv.get('foo/', recurse=True)
        assert len(data) == 1

        c.kv.put('foo/bar1', '1')
        c.kv.put('foo/bar2', '2')
        c.kv.put('foo/bar3', '3')
        index, data = c.kv.get('foo/', recurse=True)
        assert [x['Key'] for x in data] == [
            'foo/', 'foo/bar1', 'foo/bar2', 'foo/bar3']
        assert [x['Value'] for x in data] == [
            None, six.b('1'), six.b('2'), six.b('3')]

    def test_kv_delete(self, consul_port):
        c = consul.Consul(port=consul_port)
        c.kv.put('foo1', '1')
        c.kv.put('foo2', '2')
        c.kv.put('foo3', '3')
        index, data = c.kv.get('foo', recurse=True)
        assert [x['Key'] for x in data] == ['foo1', 'foo2', 'foo3']

        assert c.kv.delete('foo2') is True
        index, data = c.kv.get('foo', recurse=True)
        assert [x['Key'] for x in data] == ['foo1', 'foo3']
        assert c.kv.delete('foo', recurse=True) is True
        index, data = c.kv.get('foo', recurse=True)
        assert data is None

    def test_kv_delete_cas(self, consul_port):
        c = consul.Consul(port=consul_port)

        c.kv.put('foo', 'bar')
        index, data = c.kv.get('foo')

        assert c.kv.delete('foo', cas=data['ModifyIndex']-1) is False
        assert c.kv.get('foo') == (index, data)

        assert c.kv.delete('foo', cas=data['ModifyIndex']) is True
        index, data = c.kv.get('foo')
        assert data is None

    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)

    def test_kv_keys_only(self, consul_port):
        c = consul.Consul(port=consul_port)

        assert c.kv.put('bar', '4') is True
        assert c.kv.put('base/foo', '1') is True
        assert c.kv.put('base/base/foo', '5') is True

        index, data = c.kv.get('base/', keys=True, separator='/')
        assert data == ['base/base/', 'base/foo']

    def test_transaction(self, consul_port):
        c = consul.Consul(port=consul_port)
        value = base64.b64encode(b"1").decode("utf8")
        d = {"KV": {"Verb": "set", "Key": "asdf", "Value": value}}
        r = c.txn.put([d])
        assert r["Errors"] is None

        d = {"KV": {"Verb": "get", "Key": "asdf"}}
        r = c.txn.put([d])
        assert r["Results"][0]["KV"]["Value"] == value

    def test_event(self, consul_port):
        c = consul.Consul(port=consul_port)

        assert c.event.fire("fooname", "foobody")
        index, events = c.event.list()
        assert [x['Name'] == 'fooname' for x in events]
        assert [x['Payload'] == 'foobody' for x in events]

    def test_event_targeted(self, consul_port):
        c = consul.Consul(port=consul_port)

        assert c.event.fire("fooname", "foobody")
        index, events = c.event.list(name="othername")
        assert events == []

        index, events = c.event.list(name="fooname")
        assert [x['Name'] == 'fooname' for x in events]
        assert [x['Payload'] == 'foobody' for x in events]

    def test_agent_checks(self, consul_port):
        c = consul.Consul(port=consul_port)

        def verify_and_dereg_check(check_id):
            assert set(c.agent.checks().keys()) == set([check_id])
            assert c.agent.check.deregister(check_id) is True
            assert set(c.agent.checks().keys()) == set([])

        def verify_check_status(check_id, status, notes=None):
            checks = c.agent.checks()
            assert checks[check_id]['Status'] == status
            if notes:
                assert checks[check_id]['Output'] == notes

        # test setting notes on a check
        c.agent.check.register('check', Check.ttl('1s'), notes='foo')
        assert c.agent.checks()['check']['Notes'] == 'foo'
        c.agent.check.deregister('check')

        assert set(c.agent.checks().keys()) == set([])
        assert c.agent.check.register(
            'script_check', Check.script('/bin/true', 10)) is True
        verify_and_dereg_check('script_check')

        assert c.agent.check.register(
            'check name',
            Check.script('/bin/true', 10),
            check_id='check_id') is True
        verify_and_dereg_check('check_id')

        http_addr = "http://127.0.0.1:{0}".format(consul_port)
        assert c.agent.check.register(
            'http_check', Check.http(http_addr, '10ms')) is True
        time.sleep(1)
        verify_check_status('http_check', 'passing')
        verify_and_dereg_check('http_check')

        assert c.agent.check.register(
            'http_timeout_check',
            Check.http(http_addr, '100ms', timeout='2s')) is True
        verify_and_dereg_check('http_timeout_check')

        assert c.agent.check.register('ttl_check', Check.ttl('100ms')) is True

        assert c.agent.check.ttl_warn('ttl_check') is True
        verify_check_status('ttl_check', 'warning')
        assert c.agent.check.ttl_warn(
            'ttl_check', notes='its not quite right') is True
        verify_check_status('ttl_check', 'warning', 'its not quite right')

        assert c.agent.check.ttl_fail('ttl_check') is True
        verify_check_status('ttl_check', 'critical')
        assert c.agent.check.ttl_fail(
            'ttl_check', notes='something went boink!') is True
        verify_check_status(
            'ttl_check', 'critical', notes='something went boink!')

        assert c.agent.check.ttl_pass('ttl_check') is True
        verify_check_status('ttl_check', 'passing')
        assert c.agent.check.ttl_pass(
            'ttl_check', notes='all hunky dory!') is True
        verify_check_status('ttl_check', 'passing', notes='all hunky dory!')
        # wait for ttl to expire
        time.sleep(120/1000.0)
        verify_check_status('ttl_check', 'critical')
        verify_and_dereg_check('ttl_check')

    def test_service_dereg_issue_156(self, consul_port):
        # https://github.com/cablehead/python-consul/issues/156
        service_name = 'app#127.0.0.1#3000'
        c = consul.Consul(port=consul_port)
        c.agent.service.register(service_name)

        time.sleep(80/1000.0)

        index, nodes = c.health.service(service_name)
        assert [node['Service']['ID'] for node in nodes] == [service_name]

        # Clean up tasks
        assert c.agent.service.deregister(service_name) is True

        time.sleep(40/1000.0)

        index, nodes = c.health.service(service_name)
        assert [node['Service']['ID'] for node in nodes] == []

    def test_agent_checks_service_id(self, consul_port):
        c = consul.Consul(port=consul_port)
        c.agent.service.register('foo1')

        time.sleep(40/1000.0)

        index, nodes = c.health.service('foo1')
        assert [node['Service']['ID'] for node in nodes] == ['foo1']

        c.agent.check.register('foo', Check.ttl('100ms'), service_id='foo1')

        time.sleep(40/1000.0)

        index, nodes = c.health.service('foo1')
        assert set([
            check['ServiceID'] for node in nodes
            for check in node['Checks']]) == set(['foo1', ''])
        assert set([
            check['CheckID'] for node in nodes
            for check in node['Checks']]) == set(['foo', 'serfHealth'])

        # Clean up tasks
        assert c.agent.check.deregister('foo') is True

        time.sleep(40/1000.0)

        assert c.agent.service.deregister('foo1') is True

        time.sleep(40/1000.0)

    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)

    def test_agent_register_enable_tag_override(self, consul_port):
        c = consul.Consul(port=consul_port)
        index, nodes = c.health.service("foo1")
        assert nodes == []

        c.agent.service.register('foo', enable_tag_override=True)

        assert c.agent.services()['foo']['EnableTagOverride']
        # Cleanup tasks
        c.agent.check.deregister('foo')

    def test_agent_service_maintenance(self, consul_port):
        c = consul.Consul(port=consul_port)

        c.agent.service.register('foo', check=Check.ttl('100ms'))

        time.sleep(40/1000.0)

        c.agent.service.maintenance('foo', 'true', "test")

        time.sleep(40/1000.0)

        checks_pre = c.agent.checks()
        assert '_service_maintenance:foo' in checks_pre.keys()
        assert 'test' == checks_pre['_service_maintenance:foo']['Notes']

        c.agent.service.maintenance('foo', 'false')

        time.sleep(40/1000.0)

        checks_post = c.agent.checks()
        assert '_service_maintenance:foo' not in checks_post.keys()

        # Cleanup
        c.agent.service.deregister('foo')

        time.sleep(40/1000.0)

    def test_agent_node_maintenance(self, consul_port):
        c = consul.Consul(port=consul_port)

        c.agent.maintenance('true', "test")

        time.sleep(40/1000.0)

        checks_pre = c.agent.checks()
        assert '_node_maintenance' in checks_pre.keys()
        assert 'test' == checks_pre['_node_maintenance']['Notes']

        c.agent.maintenance('false')

        time.sleep(40/1000.0)

        checks_post = c.agent.checks()
        assert '_node_maintenance' not in checks_post.keys()

    def test_agent_members(self, consul_port):
        c = consul.Consul(port=consul_port)
        members = c.agent.members()
        for x in members:
            assert x['Status'] == 1
            assert not x['Name'] is None
            assert not x['Tags'] is None
        assert c.agent.self()['Member'] in members

        wan_members = c.agent.members(wan=True)
        for x in wan_members:
            assert 'dc1' in x['Name']

    def test_agent_self(self, consul_port):
        c = consul.Consul(port=consul_port)
        assert set(c.agent.self().keys()) == set(['Member', 'Stats', 'Config',
                                                  'Coord', 'DebugConfig',
                                                  'Meta'])

    def test_agent_services(self, consul_port):
        c = consul.Consul(port=consul_port)
        assert c.agent.service.register('foo') is True
        assert set(c.agent.services().keys()) == set(['foo'])
        assert c.agent.service.deregister('foo') is True
        assert set(c.agent.services().keys()) == set()

        # test address param
        assert c.agent.service.register('foo', address='10.10.10.1') is True
        assert [
            v['Address'] for k, v in c.agent.services().items()
            if k == 'foo'][0] == '10.10.10.1'
        assert c.agent.service.deregister('foo') is True

    def test_catalog(self, consul_port):
        c = consul.Consul(port=consul_port)

        # grab the node our server created, so we can ignore it
        _, nodes = c.catalog.nodes()
        assert len(nodes) == 1
        current = nodes[0]

        # test catalog.datacenters
        assert c.catalog.datacenters() == ['dc1']

        # test catalog.register
        pytest.raises(
            consul.ConsulException,
            c.catalog.register, 'foo', '10.1.10.11', dc='dc2')

        assert c.catalog.register(
            'n1',
            '10.1.10.11',
            service={'service': 's1'},
            check={'name': 'c1'}) is True
        assert c.catalog.register(
            'n1', '10.1.10.11', service={'service': 's2'}) is True
        assert c.catalog.register(
            'n2', '10.1.10.12',
            service={'service': 's1', 'tags': ['master']}) is True

        # test catalog.nodes
        pytest.raises(consul.ConsulException, c.catalog.nodes, dc='dc2')
        _, nodes = c.catalog.nodes()
        nodes.remove(current)
        assert [x['Node'] for x in nodes] == ['n1', 'n2']

        # test catalog.services
        pytest.raises(consul.ConsulException, c.catalog.services, dc='dc2')
        _, services = c.catalog.services()
        assert services == {'s1': [u'master'], 's2': [], 'consul': []}

        # test catalog.node
        pytest.raises(consul.ConsulException, c.catalog.node, 'n1', dc='dc2')
        _, node = c.catalog.node('n1')
        assert set(node['Services'].keys()) == set(['s1', 's2'])
        _, node = c.catalog.node('n3')
        assert node is None

        # test catalog.service
        pytest.raises(
            consul.ConsulException, c.catalog.service, 's1', dc='dc2')
        _, nodes = c.catalog.service('s1')
        assert set([x['Node'] for x in nodes]) == set(['n1', 'n2'])
        _, nodes = c.catalog.service('s1', tag='master')
        assert set([x['Node'] for x in nodes]) == set(['n2'])

        # test catalog.deregister
        pytest.raises(
            consul.ConsulException, c.catalog.deregister, 'n2', dc='dc2')
        assert c.catalog.deregister('n1', check_id='c1') is True
        assert c.catalog.deregister('n2', service_id='s1') is True
        # check the nodes weren't removed
        _, nodes = c.catalog.nodes()
        nodes.remove(current)
        assert [x['Node'] for x in nodes] == ['n1', 'n2']
        # check n2's s1 service was removed though
        _, nodes = c.catalog.service('s1')
        assert set([x['Node'] for x in nodes]) == set(['n1'])

        # cleanup
        assert c.catalog.deregister('n1') is True
        assert c.catalog.deregister('n2') is True
        _, nodes = c.catalog.nodes()
        nodes.remove(current)
        assert [x['Node'] for x in nodes] == []

    def test_health_service(self, consul_port):
        c = consul.Consul(port=consul_port)

        # check there are no nodes for the service 'foo'
        index, nodes = c.health.service('foo')
        assert nodes == []

        # register two nodes, one with a long ttl, the other shorter
        c.agent.service.register(
            'foo',
            service_id='foo:1',
            check=Check.ttl('10s'),
            tags=['tag:foo:1'])
        c.agent.service.register(
            'foo', service_id='foo:2', check=Check.ttl('100ms'))

        time.sleep(40/1000.0)

        # check the nodes show for the /health/service endpoint
        index, nodes = c.health.service('foo')
        assert [node['Service']['ID'] for node in nodes] == ['foo:1', 'foo:2']

        # but that they aren't passing their health check
        index, nodes = c.health.service('foo', passing=True)
        assert nodes == []

        # ping the two node's health check
        c.agent.check.ttl_pass('service:foo:1')
        c.agent.check.ttl_pass('service:foo:2')

        time.sleep(40/1000.0)

        # both nodes are now available
        index, nodes = c.health.service('foo', passing=True)
        assert [node['Service']['ID'] for node in nodes] == ['foo:1', 'foo:2']

        # wait until the short ttl node fails
        time.sleep(120/1000.0)

        # only one node available
        index, nodes = c.health.service('foo', passing=True)
        assert [node['Service']['ID'] for node in nodes] == ['foo:1']

        # ping the failed node's health check
        c.agent.check.ttl_pass('service:foo:2')

        time.sleep(40/1000.0)

        # check both nodes are available
        index, nodes = c.health.service('foo', passing=True)
        assert [node['Service']['ID'] for node in nodes] == ['foo:1', 'foo:2']

        # check that tag works
        index, nodes = c.health.service('foo', tag='tag:foo:1')
        assert [node['Service']['ID'] for node in nodes] == ['foo:1']

        # deregister the nodes
        c.agent.service.deregister('foo:1')
        c.agent.service.deregister('foo:2')

        time.sleep(40/1000.0)

        index, nodes = c.health.service('foo')
        assert nodes == []

    def test_health_state(self, consul_port):
        c = consul.Consul(port=consul_port)

        # The empty string is for the Serf Health Status check, which has an
        # empty ServiceID
        index, nodes = c.health.state('any')
        assert [node['ServiceID'] for node in nodes] == ['']

        # register two nodes, one with a long ttl, the other shorter
        c.agent.service.register(
            'foo', service_id='foo:1', check=Check.ttl('10s'))
        c.agent.service.register(
            'foo', service_id='foo:2', check=Check.ttl('100ms'))

        time.sleep(40/1000.0)

        # check the nodes show for the /health/state/any endpoint
        index, nodes = c.health.state('any')
        assert set([node['ServiceID'] for node in nodes]) == set(
            ['', 'foo:1', 'foo:2'])

        # but that they aren't passing their health check
        index, nodes = c.health.state('passing')
        assert [node['ServiceID'] for node in nodes] != 'foo'

        # ping the two node's health check
        c.agent.check.ttl_pass('service:foo:1')
        c.agent.check.ttl_pass('service:foo:2')

        time.sleep(40/1000.0)

        # both nodes are now available
        index, nodes = c.health.state('passing')
        assert set([node['ServiceID'] for node in nodes]) == set(
            ['', 'foo:1', 'foo:2'])

        # wait until the short ttl node fails
        time.sleep(2200/1000.0)

        # only one node available
        index, nodes = c.health.state('passing')
        assert set([node['ServiceID'] for node in nodes]) == set(
            ['', 'foo:1'])

        # ping the failed node's health check
        c.agent.check.ttl_pass('service:foo:2')

        time.sleep(40/1000.0)

        # check both nodes are available
        index, nodes = c.health.state('passing')
        assert set([node['ServiceID'] for node in nodes]) == set(
            ['', 'foo:1', 'foo:2'])

        # deregister the nodes
        c.agent.service.deregister('foo:1')
        c.agent.service.deregister('foo:2')

        time.sleep(40/1000.0)

        index, nodes = c.health.state('any')
        assert [node['ServiceID'] for node in nodes] == ['']

    def test_health_node(self, consul_port):
        c = consul.Consul(port=consul_port)
        # grab local node name
        node = c.agent.self()['Config']['NodeName']
        index, checks = c.health.node(node)
        assert node in [check["Node"] for check in checks]

    def test_health_checks(self, consul_port):
        c = consul.Consul(port=consul_port)

        c.agent.service.register(
            'foobar', service_id='foobar', check=Check.ttl('10s'))

        time.sleep(40/1000.00)

        index, checks = c.health.checks('foobar')

        assert [check['ServiceID'] for check in checks] == ['foobar']
        assert [check['CheckID'] for check in checks] == ['service:foobar']

        c.agent.service.deregister('foobar')

        time.sleep(40/1000.0)

        index, checks = c.health.checks('foobar')
        assert len(checks) == 0

    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 == []

    def test_session_delete_ttl_renew(self, consul_port):
        c = consul.Consul(port=consul_port)

        s = c.session.create(behavior='delete', ttl=20)

        # attempt to renew an unknown session
        pytest.raises(consul.NotFound, c.session.renew, '1'*36)

        session = c.session.renew(s)
        assert session['Behavior'] == 'delete'
        assert session['TTL'] == '20s'

        # trying out the behavior
        assert c.kv.put('foo', '1', acquire=s) is True
        index, data = c.kv.get('foo')
        assert data['Value'] == six.b('1')

        c.session.destroy(s)
        index, data = c.kv.get('foo')
        assert data is None

    def test_acl_disabled(self, consul_port):
        c = consul.Consul(port=consul_port)
        pytest.raises(consul.ACLDisabled, c.acl.list)
        pytest.raises(consul.ACLDisabled, c.acl.info, '1'*36)
        pytest.raises(consul.ACLDisabled, c.acl.create)
        pytest.raises(consul.ACLDisabled, c.acl.update, 'foo')
        pytest.raises(consul.ACLDisabled, c.acl.clone, 'foo')
        pytest.raises(consul.ACLDisabled, c.acl.destroy, 'foo')

    def test_acl_permission_denied(self, acl_consul):
        c = consul.Consul(port=acl_consul.port)
        pytest.raises(consul.ACLPermissionDenied, c.acl.list)
        pytest.raises(consul.ACLPermissionDenied, c.acl.create)
        pytest.raises(consul.ACLPermissionDenied, c.acl.update, 'anonymous')
        pytest.raises(consul.ACLPermissionDenied, c.acl.clone, 'anonymous')
        pytest.raises(consul.ACLPermissionDenied, c.acl.destroy, 'anonymous')

    def test_acl_explict_token_use(self, acl_consul):
        c = consul.Consul(port=acl_consul.port)
        master_token = acl_consul.token

        acls = c.acl.list(token=master_token)
        assert set([x['ID'] for x in acls]) == \
            set(['anonymous', master_token])

        assert c.acl.info('1'*36) is None
        compare = [c.acl.info(master_token), c.acl.info('anonymous')]
        compare.sort(key=operator.itemgetter('ID'))
        assert acls == compare

        rules = """
            key "" {
                policy = "read"
            }
            key "private/" {
                policy = "deny"
            }
            service "foo-" {
                policy = "write"
            }
            service "bar-" {
                policy = "read"
            }
        """

        token = c.acl.create(rules=rules, token=master_token)
        assert c.acl.info(token)['Rules'] == rules

        token2 = c.acl.clone(token, token=master_token)
        assert c.acl.info(token2)['Rules'] == rules

        assert c.acl.update(token2, name='Foo', token=master_token) == token2
        assert c.acl.info(token2)['Name'] == 'Foo'

        assert c.acl.destroy(token2, token=master_token) is True
        assert c.acl.info(token2) is None

        c.kv.put('foo', 'bar')
        c.kv.put('private/foo', 'bar')

        assert c.kv.get('foo', token=token)[1]['Value'] == six.b('bar')
        pytest.raises(
            consul.ACLPermissionDenied, c.kv.put, 'foo', 'bar2', token=token)
        pytest.raises(
            consul.ACLPermissionDenied, c.kv.delete, 'foo', token=token)

        assert c.kv.get('private/foo')[1]['Value'] == six.b('bar')
        pytest.raises(
            consul.ACLPermissionDenied,
            c.kv.get, 'private/foo', token=token)
        pytest.raises(
            consul.ACLPermissionDenied,
            c.kv.put, 'private/foo', 'bar2', token=token)
        pytest.raises(
            consul.ACLPermissionDenied,
            c.kv.delete, 'private/foo', token=token)

        # test token pass through for service registration
        pytest.raises(
            consul.ACLPermissionDenied,
            c.agent.service.register, "bar-1", token=token)
        c.agent.service.register("foo-1", token=token)
        index, data = c.health.service('foo-1', token=token)
        assert data[0]['Service']['ID'] == "foo-1"
        index, data = c.health.checks('foo-1', token=token)
        assert data == []
        index, data = c.health.service('bar-1', token=token)
        assert not data

        # clean up
        assert c.agent.service.deregister('foo-1') is True
        c.acl.destroy(token, token=master_token)
        acls = c.acl.list(token=master_token)
        assert set([x['ID'] for x in acls]) == \
            set(['anonymous', master_token])

    def test_acl_implicit_token_use(self, acl_consul):
        # configure client to use the master token by default
        c = consul.Consul(port=acl_consul.port, token=acl_consul.token)
        master_token = acl_consul.token

        acls = c.acl.list()
        assert set([x['ID'] for x in acls]) == \
            set(['anonymous', master_token])

        assert c.acl.info('foo') is None
        compare = [c.acl.info(master_token), c.acl.info('anonymous')]
        compare.sort(key=operator.itemgetter('ID'))
        assert acls == compare

        rules = """
            key "" {
                policy = "read"
            }
            key "private/" {
                policy = "deny"
            }
        """
        token = c.acl.create(rules=rules)
        assert c.acl.info(token)['Rules'] == rules

        token2 = c.acl.clone(token)
        assert c.acl.info(token2)['Rules'] == rules

        assert c.acl.update(token2, name='Foo') == token2
        assert c.acl.info(token2)['Name'] == 'Foo'

        assert c.acl.destroy(token2) is True
        assert c.acl.info(token2) is None

        c.kv.put('foo', 'bar')
        c.kv.put('private/foo', 'bar')

        c_limited = consul.Consul(port=acl_consul.port, token=token)
        assert c_limited.kv.get('foo')[1]['Value'] == six.b('bar')
        pytest.raises(
            consul.ACLPermissionDenied, c_limited.kv.put, 'foo', 'bar2')
        pytest.raises(
            consul.ACLPermissionDenied, c_limited.kv.delete, 'foo')

        assert c.kv.get('private/foo')[1]['Value'] == six.b('bar')
        pytest.raises(
            consul.ACLPermissionDenied,
            c_limited.kv.get, 'private/foo')
        pytest.raises(
            consul.ACLPermissionDenied,
            c_limited.kv.put, 'private/foo', 'bar2')
        pytest.raises(
            consul.ACLPermissionDenied,
            c_limited.kv.delete, 'private/foo')

        # check we can override the client's default token
        pytest.raises(
            consul.ACLPermissionDenied,
            c.kv.get, 'private/foo', token=token
        )
        pytest.raises(
            consul.ACLPermissionDenied,
            c.kv.put, 'private/foo', 'bar2', token=token)
        pytest.raises(
            consul.ACLPermissionDenied,
            c.kv.delete, 'private/foo', token=token)

        # clean up
        c.acl.destroy(token)
        acls = c.acl.list()
        assert set([x['ID'] for x in acls]) == \
            set(['anonymous', master_token])

    def test_status_leader(self, consul_port):
        c = consul.Consul(port=consul_port)

        agent_self = c.agent.self()
        leader = c.status.leader()
        addr_port = agent_self['Stats']['consul']['leader_addr']

        assert leader == addr_port, \
            "Leader value was {0}, expected value " \
            "was {1}".format(leader, addr_port)

    def test_status_peers(self, consul_port):

        c = consul.Consul(port=consul_port)

        agent_self = c.agent.self()

        addr_port = agent_self['Stats']['consul']['leader_addr']
        peers = c.status.peers()

        assert addr_port in peers, \
            "Expected value '{0}' " \
            "in peer list but it was not present".format(addr_port)

    def test_query(self, consul_port):
        c = consul.Consul(port=consul_port)

        # check that query list is empty
        queries = c.query.list()
        assert queries == []

        # create a new named query
        query_service = 'foo'
        query_name = 'fooquery'
        query = c.query.create(query_service, query_name)

        # assert response contains query ID
        assert 'ID' in query \
            and query['ID'] is not None \
            and str(query['ID']) != ''

        # retrieve query using id and name
        queries = c.query.get(query['ID'])
        assert queries != [] \
            and len(queries) == 1
        assert queries[0]['Name'] == query_name \
            and queries[0]['ID'] == query['ID']

        # explain query
        assert c.query.explain(query_name)['Query']

        # delete query
        assert c.query.delete(query['ID'])

    def test_coordinate(self, consul_port):
        c = consul.Consul(port=consul_port)
        c.coordinate.nodes()
        c.coordinate.datacenters()
        assert set(c.coordinate.datacenters()[0].keys()) == \
            set(['Datacenter', 'Coordinates', 'AreaID'])

    def test_operator(self, consul_port):
        c = consul.Consul(port=consul_port)
        config = c.operator.raft_config()
        assert config["Index"] == 1
        leader = False
        voter = False
        for server in config["Servers"]:
            if server["Leader"]:
                leader = True
            if server["Voter"]:
                voter = True
        assert leader
        assert voter

[((18, 15, 18, 38), 'consul.std.HTTPClient', 'consul.std.HTTPClient', ({}, {}), '()', False, 'import consul\n'), ((26, 12, 26, 43), 'consul.Consul', 'consul.Consul', (), '', False, 'import consul\n'), ((34, 12, 34, 43), 'consul.Consul', 'consul.Consul', (), '', False, 'import consul\n'), ((41, 12, 41, 43), 'consul.Consul', 'consul.Consul', (), '', False, 'import consul\n'), ((64, 8, 64, 62), 'pytest.raises', 'pytest.raises', ({(64, 22, 64, 36): 'AssertionError', (64, 38, 64, 46): 'c.kv.put', (64, 48, 64, 53): '"""foo"""', (64, 55, 64, 61): '{(1): 2}'}, {}), "(AssertionError, c.kv.put, 'foo', {(1): 2})", False, 'import pytest\n'), ((67, 12, 67, 43), 'consul.Consul', 'consul.Consul', (), '', False, 'import consul\n'), ((78, 12, 78, 43), 'consul.Consul', 'consul.Consul', (), '', False, 'import consul\n'), ((88, 12, 88, 43), 'consul.Consul', 'consul.Consul', (), '', False, 'import consul\n'), ((106, 12, 106, 43), 'consul.Consul', 'consul.Consul', (), '', False, 'import consul\n'), ((121, 12, 121, 43), 'consul.Consul', 'consul.Consul', (), '', False, 'import consul\n'), ((134, 12, 134, 43), 'consul.Consul', 'consul.Consul', (), '', False, 'import consul\n'), ((136, 8, 137, 74), 'pytest.raises', 'pytest.raises', (), '', False, 'import pytest\n'), ((153, 12, 153, 43), 'consul.Consul', 'consul.Consul', (), '', False, 'import consul\n'), ((163, 12, 163, 43), 'consul.Consul', 'consul.Consul', (), '', False, 'import consul\n'), ((174, 12, 174, 43), 'consul.Consul', 'consul.Consul', (), '', False, 'import consul\n'), ((182, 12, 182, 43), 'consul.Consul', 'consul.Consul', (), '', False, 'import consul\n'), ((193, 12, 193, 43), 'consul.Consul', 'consul.Consul', (), '', False, 'import consul\n'), ((225, 8, 225, 21), 'time.sleep', 'time.sleep', ({(225, 19, 225, 20): '(1)'}, {}), '(1)', False, 'import time\n'), ((255, 8, 255, 30), 'time.sleep', 'time.sleep', ({(255, 19, 255, 29): '(120 / 1000.0)'}, {}), '(120 / 1000.0)', False, 'import time\n'), ((262, 12, 262, 43), 'consul.Consul', 'consul.Consul', (), '', False, 'import consul\n'), ((265, 8, 265, 29), 'time.sleep', 'time.sleep', ({(265, 19, 265, 28): '(80 / 1000.0)'}, {}), '(80 / 1000.0)', False, 'import time\n'), ((273, 8, 273, 29), 'time.sleep', 'time.sleep', ({(273, 19, 273, 28): '(40 / 1000.0)'}, {}), '(40 / 1000.0)', False, 'import time\n'), ((279, 12, 279, 43), 'consul.Consul', 'consul.Consul', (), '', False, 'import consul\n'), ((282, 8, 282, 29), 'time.sleep', 'time.sleep', ({(282, 19, 282, 28): '(40 / 1000.0)'}, {}), '(40 / 1000.0)', False, 'import time\n'), ((289, 8, 289, 29), 'time.sleep', 'time.sleep', ({(289, 19, 289, 28): '(40 / 1000.0)'}, {}), '(40 / 1000.0)', False, 'import time\n'), ((302, 8, 302, 29), 'time.sleep', 'time.sleep', ({(302, 19, 302, 28): '(40 / 1000.0)'}, {}), '(40 / 1000.0)', False, 'import time\n'), ((306, 8, 306, 29), 'time.sleep', 'time.sleep', ({(306, 19, 306, 28): '(40 / 1000.0)'}, {}), '(40 / 1000.0)', False, 'import time\n'), ((309, 12, 309, 43), 'consul.Consul', 'consul.Consul', (), '', False, 'import consul\n'), ((318, 8, 318, 29), 'time.sleep', 'time.sleep', ({(318, 19, 318, 28): '(40 / 1000.0)'}, {}), '(40 / 1000.0)', False, 'import time\n'), ((325, 8, 325, 29), 'time.sleep', 'time.sleep', ({(325, 19, 325, 28): '(40 / 1000.0)'}, {}), '(40 / 1000.0)', False, 'import time\n'), ((328, 12, 328, 43), 'consul.Consul', 'consul.Consul', (), '', False, 'import consul\n'), ((339, 12, 339, 43), 'consul.Consul', 'consul.Consul', (), '', False, 'import consul\n'), ((343, 8, 343, 29), 'time.sleep', 'time.sleep', ({(343, 19, 343, 28): '(40 / 1000.0)'}, {}), '(40 / 1000.0)', False, 'import time\n'), ((347, 8, 347, 29), 'time.sleep', 'time.sleep', ({(347, 19, 347, 28): '(40 / 1000.0)'}, {}), '(40 / 1000.0)', False, 'import time\n'), ((355, 8, 355, 29), 'time.sleep', 'time.sleep', ({(355, 19, 355, 28): '(40 / 1000.0)'}, {}), '(40 / 1000.0)', False, 'import time\n'), ((363, 8, 363, 29), 'time.sleep', 'time.sleep', ({(363, 19, 363, 28): '(40 / 1000.0)'}, {}), '(40 / 1000.0)', False, 'import time\n'), ((366, 12, 366, 43), 'consul.Consul', 'consul.Consul', (), '', False, 'import consul\n'), ((370, 8, 370, 29), 'time.sleep', 'time.sleep', ({(370, 19, 370, 28): '(40 / 1000.0)'}, {}), '(40 / 1000.0)', False, 'import time\n'), ((378, 8, 378, 29), 'time.sleep', 'time.sleep', ({(378, 19, 378, 28): '(40 / 1000.0)'}, {}), '(40 / 1000.0)', False, 'import time\n'), ((384, 12, 384, 43), 'consul.Consul', 'consul.Consul', (), '', False, 'import consul\n'), ((397, 12, 397, 43), 'consul.Consul', 'consul.Consul', (), '', False, 'import consul\n'), ((403, 12, 403, 43), 'consul.Consul', 'consul.Consul', (), '', False, 'import consul\n'), ((417, 12, 417, 43), 'consul.Consul', 'consul.Consul', (), '', False, 'import consul\n'), ((428, 8, 430, 62), 'pytest.raises', 'pytest.raises', (), '', False, 'import pytest\n'), ((444, 8, 444, 72), 'pytest.raises', 'pytest.raises', (), '', False, 'import pytest\n'), ((450, 8, 450, 75), 'pytest.raises', 'pytest.raises', (), '', False, 'import pytest\n'), ((455, 8, 455, 77), 'pytest.raises', 'pytest.raises', (), '', False, 'import pytest\n'), ((462, 8, 463, 70), 'pytest.raises', 'pytest.raises', (), '', False, 'import pytest\n'), ((470, 8, 471, 73), 'pytest.raises', 'pytest.raises', (), '', False, 'import pytest\n'), ((490, 12, 490, 43), 'consul.Consul', 'consul.Consul', (), '', False, 'import consul\n'), ((505, 8, 505, 29), 'time.sleep', 'time.sleep', ({(505, 19, 505, 28): '(40 / 1000.0)'}, {}), '(40 / 1000.0)', False, 'import time\n'), ((519, 8, 519, 29), 'time.sleep', 'time.sleep', ({(519, 19, 519, 28): '(40 / 1000.0)'}, {}), '(40 / 1000.0)', False, 'import time\n'), ((526, 8, 526, 30), 'time.sleep', 'time.sleep', ({(526, 19, 526, 29): '(120 / 1000.0)'}, {}), '(120 / 1000.0)', False, 'import time\n'), ((535, 8, 535, 29), 'time.sleep', 'time.sleep', ({(535, 19, 535, 28): '(40 / 1000.0)'}, {}), '(40 / 1000.0)', False, 'import time\n'), ((549, 8, 549, 29), 'time.sleep', 'time.sleep', ({(549, 19, 549, 28): '(40 / 1000.0)'}, {}), '(40 / 1000.0)', False, 'import time\n'), ((555, 12, 555, 43), 'consul.Consul', 'consul.Consul', (), '', False, 'import consul\n'), ((568, 8, 568, 29), 'time.sleep', 'time.sleep', ({(568, 19, 568, 28): '(40 / 1000.0)'}, {}), '(40 / 1000.0)', False, 'import time\n'), ((583, 8, 583, 29), 'time.sleep', 'time.sleep', ({(583, 19, 583, 28): '(40 / 1000.0)'}, {}), '(40 / 1000.0)', False, 'import time\n'), ((591, 8, 591, 31), 'time.sleep', 'time.sleep', ({(591, 19, 591, 30): '(2200 / 1000.0)'}, {}), '(2200 / 1000.0)', False, 'import time\n'), ((601, 8, 601, 29), 'time.sleep', 'time.sleep', ({(601, 19, 601, 28): '(40 / 1000.0)'}, {}), '(40 / 1000.0)', False, 'import time\n'), ((612, 8, 612, 29), 'time.sleep', 'time.sleep', ({(612, 19, 612, 28): '(40 / 1000.0)'}, {}), '(40 / 1000.0)', False, 'import time\n'), ((618, 12, 618, 43), 'consul.Consul', 'consul.Consul', (), '', False, 'import consul\n'), ((625, 12, 625, 43), 'consul.Consul', 'consul.Consul', (), '', False, 'import consul\n'), ((630, 8, 630, 30), 'time.sleep', 'time.sleep', ({(630, 19, 630, 29): '(40 / 1000.0)'}, {}), '(40 / 1000.0)', False, 'import time\n'), ((639, 8, 639, 29), 'time.sleep', 'time.sleep', ({(639, 19, 639, 28): '(40 / 1000.0)'}, {}), '(40 / 1000.0)', False, 'import time\n'), ((645, 12, 645, 43), 'consul.Consul', 'consul.Consul', (), '', False, 'import consul\n'), ((648, 8, 648, 74), 'pytest.raises', 'pytest.raises', (), '', False, 'import pytest\n'), ((649, 8, 649, 73), 'pytest.raises', 'pytest.raises', (), '', False, 'import pytest\n'), ((653, 8, 653, 71), 'pytest.raises', 'pytest.raises', (), '', False, 'import pytest\n'), ((658, 8, 659, 73), 'pytest.raises', 'pytest.raises', (), '', False, 'import pytest\n'), ((667, 8, 668, 67), 'pytest.raises', 'pytest.raises', (), '', False, 'import pytest\n'), ((673, 8, 674, 76), 'pytest.raises', 'pytest.raises', (), '', False, 'import pytest\n'), ((680, 12, 680, 43), 'consul.Consul', 'consul.Consul', (), '', False, 'import consul\n'), ((685, 8, 685, 63), 'pytest.raises', 'pytest.raises', ({(685, 22, 685, 37): 'consul.NotFound', (685, 39, 685, 54): 'c.session.renew', (685, 56, 685, 62): "('1' * 36)"}, {}), "(consul.NotFound, c.session.renew, '1' * 36)", False, 'import pytest\n'), ((701, 12, 701, 43), 'consul.Consul', 'consul.Consul', (), '', False, 'import consul\n'), ((702, 8, 702, 53), 'pytest.raises', 'pytest.raises', ({(702, 22, 702, 40): 'consul.ACLDisabled', (702, 42, 702, 52): 'c.acl.list'}, {}), '(consul.ACLDisabled, c.acl.list)', False, 'import pytest\n'), ((703, 8, 703, 61), 'pytest.raises', 'pytest.raises', ({(703, 22, 703, 40): 'consul.ACLDisabled', (703, 42, 703, 52): 'c.acl.info', (703, 54, 703, 60): "('1' * 36)"}, {}), "(consul.ACLDisabled, c.acl.info, '1' * 36)", False, 'import pytest\n'), ((704, 8, 704, 55), 'pytest.raises', 'pytest.raises', ({(704, 22, 704, 40): 'consul.ACLDisabled', (704, 42, 704, 54): 'c.acl.create'}, {}), '(consul.ACLDisabled, c.acl.create)', False, 'import pytest\n'), ((705, 8, 705, 62), 'pytest.raises', 'pytest.raises', ({(705, 22, 705, 40): 'consul.ACLDisabled', (705, 42, 705, 54): 'c.acl.update', (705, 56, 705, 61): '"""foo"""'}, {}), "(consul.ACLDisabled, c.acl.update, 'foo')", False, 'import pytest\n'), ((706, 8, 706, 61), 'pytest.raises', 'pytest.raises', ({(706, 22, 706, 40): 'consul.ACLDisabled', (706, 42, 706, 53): 'c.acl.clone', (706, 55, 706, 60): '"""foo"""'}, {}), "(consul.ACLDisabled, c.acl.clone, 'foo')", False, 'import pytest\n'), ((707, 8, 707, 63), 'pytest.raises', 'pytest.raises', ({(707, 22, 707, 40): 'consul.ACLDisabled', (707, 42, 707, 55): 'c.acl.destroy', (707, 57, 707, 62): '"""foo"""'}, {}), "(consul.ACLDisabled, c.acl.destroy, 'foo')", False, 'import pytest\n'), ((710, 12, 710, 47), 'consul.Consul', 'consul.Consul', (), '', False, 'import consul\n'), ((711, 8, 711, 61), 'pytest.raises', 'pytest.raises', ({(711, 22, 711, 48): 'consul.ACLPermissionDenied', (711, 50, 711, 60): 'c.acl.list'}, {}), '(consul.ACLPermissionDenied, c.acl.list)', False, 'import pytest\n'), ((712, 8, 712, 63), 'pytest.raises', 'pytest.raises', ({(712, 22, 712, 48): 'consul.ACLPermissionDenied', (712, 50, 712, 62): 'c.acl.create'}, {}), '(consul.ACLPermissionDenied, c.acl.create)', False, 'import pytest\n'), ((713, 8, 713, 76), 'pytest.raises', 'pytest.raises', ({(713, 22, 713, 48): 'consul.ACLPermissionDenied', (713, 50, 713, 62): 'c.acl.update', (713, 64, 713, 75): '"""anonymous"""'}, {}), "(consul.ACLPermissionDenied, c.acl.update, 'anonymous')", False, 'import pytest\n'), ((714, 8, 714, 75), 'pytest.raises', 'pytest.raises', ({(714, 22, 714, 48): 'consul.ACLPermissionDenied', (714, 50, 714, 61): 'c.acl.clone', (714, 63, 714, 74): '"""anonymous"""'}, {}), "(consul.ACLPermissionDenied, c.acl.clone, 'anonymous')", False, 'import pytest\n'), ((715, 8, 715, 77), 'pytest.raises', 'pytest.raises', ({(715, 22, 715, 48): 'consul.ACLPermissionDenied', (715, 50, 715, 63): 'c.acl.destroy', (715, 65, 715, 76): '"""anonymous"""'}, {}), "(consul.ACLPermissionDenied, c.acl.destroy, 'anonymous')", False, 'import pytest\n'), ((718, 12, 718, 47), 'consul.Consul', 'consul.Consul', (), '', False, 'import consul\n'), ((761, 8, 762, 77), 'pytest.raises', 'pytest.raises', (), '', False, 'import pytest\n'), ((763, 8, 764, 72), 'pytest.raises', 'pytest.raises', (), '', False, 'import pytest\n'), ((767, 8, 769, 49), 'pytest.raises', 'pytest.raises', (), '', False, 'import pytest\n'), ((770, 8, 772, 57), 'pytest.raises', 'pytest.raises', (), '', False, 'import pytest\n'), ((773, 8, 775, 52), 'pytest.raises', 'pytest.raises', (), '', False, 'import pytest\n'), ((778, 8, 780, 59), 'pytest.raises', 'pytest.raises', (), '', False, 'import pytest\n'), ((798, 12, 798, 71), 'consul.Consul', 'consul.Consul', (), '', False, 'import consul\n'), ((833, 20, 833, 68), 'consul.Consul', 'consul.Consul', (), '', False, 'import consul\n'), ((835, 8, 836, 72), 'pytest.raises', 'pytest.raises', ({(836, 12, 836, 38): 'consul.ACLPermissionDenied', (836, 40, 836, 56): 'c_limited.kv.put', (836, 58, 836, 63): '"""foo"""', (836, 65, 836, 71): '"""bar2"""'}, {}), "(consul.ACLPermissionDenied, c_limited.kv.put, 'foo', 'bar2')", False, 'import pytest\n'), ((837, 8, 838, 67), 'pytest.raises', 'pytest.raises', ({(838, 12, 838, 38): 'consul.ACLPermissionDenied', (838, 40, 838, 59): 'c_limited.kv.delete', (838, 61, 838, 66): '"""foo"""'}, {}), "(consul.ACLPermissionDenied, c_limited.kv.delete, 'foo')", False, 'import pytest\n'), ((841, 8, 843, 44), 'pytest.raises', 'pytest.raises', ({(842, 12, 842, 38): 'consul.ACLPermissionDenied', (843, 12, 843, 28): 'c_limited.kv.get', (843, 30, 843, 43): '"""private/foo"""'}, {}), "(consul.ACLPermissionDenied, c_limited.kv.get, 'private/foo')", False, 'import pytest\n'), ((844, 8, 846, 52), 'pytest.raises', 'pytest.raises', ({(845, 12, 845, 38): 'consul.ACLPermissionDenied', (846, 12, 846, 28): 'c_limited.kv.put', (846, 30, 846, 43): '"""private/foo"""', (846, 45, 846, 51): '"""bar2"""'}, {}), "(consul.ACLPermissionDenied, c_limited.kv.put, 'private/foo',\n    'bar2')", False, 'import pytest\n'), ((847, 8, 849, 47), 'pytest.raises', 'pytest.raises', ({(848, 12, 848, 38): 'consul.ACLPermissionDenied', (849, 12, 849, 31): 'c_limited.kv.delete', (849, 33, 849, 46): '"""private/foo"""'}, {}), "(consul.ACLPermissionDenied, c_limited.kv.delete, 'private/foo')", False, 'import pytest\n'), ((852, 8, 855, 9), 'pytest.raises', 'pytest.raises', (), '', False, 'import pytest\n'), ((856, 8, 858, 57), 'pytest.raises', 'pytest.raises', (), '', False, 'import pytest\n'), ((859, 8, 861, 52), 'pytest.raises', 'pytest.raises', (), '', False, 'import pytest\n'), ((870, 12, 870, 43), 'consul.Consul', 'consul.Consul', (), '', False, 'import consul\n'), ((882, 12, 882, 43), 'consul.Consul', 'consul.Consul', (), '', False, 'import consul\n'), ((894, 12, 894, 43), 'consul.Consul', 'consul.Consul', (), '', False, 'import consul\n'), ((924, 12, 924, 43), 'consul.Consul', 'consul.Consul', (), '', False, 'import consul\n'), ((931, 12, 931, 43), 'consul.Consul', 'consul.Consul', (), '', False, 'import consul\n'), ((31, 32, 31, 44), 'six.b', 'six.b', ({(31, 38, 31, 43): '"""bar"""'}, {}), "('bar')", False, 'import six\n'), ((44, 24, 44, 46), 'struct.pack', 'struct.pack', ({(44, 36, 44, 39): '"""i"""', (44, 41, 44, 45): '(1000)'}, {}), "('i', 1000)", False, 'import struct\n'), ((46, 15, 46, 48), 'struct.unpack', 'struct.unpack', ({(46, 29, 46, 32): '"""i"""', (46, 34, 46, 47): "data['Value']"}, {}), "('i', data['Value'])", False, 'import struct\n'), ((51, 32, 51, 44), 'six.b', 'six.b', ({(51, 38, 51, 43): '"""bar"""'}, {}), "('bar')", False, 'import six\n'), ((75, 32, 75, 45), 'six.b', 'six.b', ({(75, 38, 75, 44): '"""bar2"""'}, {}), "('bar2')", False, 'import six\n'), ((694, 32, 694, 42), 'six.b', 'six.b', ({(694, 38, 694, 41): '"""1"""'}, {}), "('1')", False, 'import six\n'), ((760, 59, 760, 71), 'six.b', 'six.b', ({(760, 65, 760, 70): '"""bar"""'}, {}), "('bar')", False, 'import six\n'), ((766, 54, 766, 66), 'six.b', 'six.b', ({(766, 60, 766, 65): '"""bar"""'}, {}), "('bar')", False, 'import six\n'), ((834, 54, 834, 66), 'six.b', 'six.b', ({(834, 60, 834, 65): '"""bar"""'}, {}), "('bar')", False, 'import six\n'), ((840, 54, 840, 66), 'six.b', 'six.b', ({(840, 60, 840, 65): '"""bar"""'}, {}), "('bar')", False, 'import six\n'), ((103, 18, 103, 28), 'six.b', 'six.b', ({(103, 24, 103, 27): '"""1"""'}, {}), "('1')", False, 'import six\n'), ((103, 30, 103, 40), 'six.b', 'six.b', ({(103, 36, 103, 39): '"""2"""'}, {}), "('2')", False, 'import six\n'), ((103, 42, 103, 52), 'six.b', 'six.b', ({(103, 48, 103, 51): '"""3"""'}, {}), "('3')", False, 'import six\n'), ((164, 16, 164, 38), 'base64.b64encode', 'base64.b64encode', ({(164, 33, 164, 37): "b'1'"}, {}), "(b'1')", False, 'import base64\n'), ((727, 25, 727, 50), 'operator.itemgetter', 'operator.itemgetter', ({(727, 45, 727, 49): '"""ID"""'}, {}), "('ID')", False, 'import operator\n'), ((807, 25, 807, 50), 'operator.itemgetter', 'operator.itemgetter', ({(807, 45, 807, 49): '"""ID"""'}, {}), "('ID')", False, 'import operator\n')]