File: test_aio.py

package info (click to toggle)
python-consul 0.7.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 47,052 kB
  • sloc: python: 3,081; makefile: 147
file content (284 lines) | stat: -rw-r--r-- 9,360 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import pytest
import six
import struct
import sys

import asyncio
import consul
import consul.aio


Check = consul.Check


@pytest.fixture
def loop(request):
    asyncio.set_event_loop(None)
    loop = asyncio.new_event_loop()

    def fin():
        loop.close()

    request.addfinalizer(fin)
    return loop


class TestAsyncioConsul(object):

    def test_kv(self, loop, consul_port):

        @asyncio.coroutine
        def main():
            c = consul.aio.Consul(port=consul_port, loop=loop)
            print(c)
            index, data = yield from c.kv.get('foo')

            print(index, data)
            assert data is None
            response = yield from c.kv.put('foo', 'bar')
            assert response is True
            index, data = yield from c.kv.get('foo')
            assert data['Value'] == six.b('bar')
            c.close()

        loop.run_until_complete(main())

    def test_consul_ctor(self, loop, consul_port):
        # same as previous but with global event loop
        @asyncio.coroutine
        def main():
            c = consul.aio.Consul(port=consul_port)
            assert c._loop is loop
            yield from c.kv.put('foo', struct.pack('i', 1000))
            index, data = yield from c.kv.get('foo')
            assert struct.unpack('i', data['Value']) == (1000,)
            c.close()

        asyncio.set_event_loop(loop)
        loop.run_until_complete(main())

    def test_kv_binary(self, loop, consul_port):
        @asyncio.coroutine
        def main():
            c = consul.aio.Consul(port=consul_port, loop=loop)
            yield from c.kv.put('foo', struct.pack('i', 1000))
            index, data = yield from c.kv.get('foo')
            assert struct.unpack('i', data['Value']) == (1000,)
            c.close()

        loop.run_until_complete(main())

    def test_kv_missing(self, loop, consul_port):
        c = consul.aio.Consul(port=consul_port, loop=loop)

        @asyncio.coroutine
        def main():
            fut = asyncio.async(put(), loop=loop)
            yield from c.kv.put('index', 'bump')
            index, data = yield from c.kv.get('foo')
            assert data is None
            index, data = yield from c.kv.get('foo', index=index)
            assert data['Value'] == six.b('bar')
            yield from fut
            c.close()

        @asyncio.coroutine
        def put():
            yield from asyncio.sleep(2.0/100, loop=loop)
            yield from c.kv.put('foo', 'bar')

        loop.run_until_complete(main())

    def test_kv_put_flags(self, loop, consul_port):
        @asyncio.coroutine
        def main():
            c = consul.aio.Consul(port=consul_port, loop=loop)
            yield from c.kv.put('foo', 'bar')
            index, data = yield from c.kv.get('foo')
            assert data['Flags'] == 0

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

        loop.run_until_complete(main())

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

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

        loop.run_until_complete(main())

    def test_kv_subscribe(self, loop, consul_port):
        c = consul.aio.Consul(port=consul_port, loop=loop)

        @asyncio.coroutine
        def get():
            fut = asyncio.async(put(), loop=loop)
            index, data = yield from c.kv.get('foo')
            assert data is None
            index, data = yield from c.kv.get('foo', index=index)
            assert data['Value'] == six.b('bar')
            yield from fut
            c.close()

        @asyncio.coroutine
        def put():
            yield from asyncio.sleep(1.0/100, loop=loop)
            response = yield from c.kv.put('foo', 'bar')
            assert response is True

        loop.run_until_complete(get())

    def test_agent_services(self, loop, consul_port):
        @asyncio.coroutine
        def main():
            c = consul.aio.Consul(port=consul_port, loop=loop)
            services = yield from c.agent.services()
            del services['consul']
            assert services == {}
            response = yield from c.agent.service.register('foo')
            assert response is True
            services = yield from c.agent.services()
            del services['consul']
            assert services == {
                'foo': {
                    'Port': 0,
                    'ID': 'foo',
                    'CreateIndex': 0,
                    'ModifyIndex': 0,
                    'EnableTagOverride': False,
                    'Service': 'foo',
                    'Tags': None,
                    'Address': ''}, }
            response = yield from c.agent.service.deregister('foo')
            assert response is True
            services = yield from c.agent.services()
            del services['consul']
            assert services == {}
            c.close()

        loop.run_until_complete(main())

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

        @asyncio.coroutine
        def nodes():
            fut = asyncio.async(register(), loop=loop)
            index, nodes = yield from c.catalog.nodes()
            assert len(nodes) == 1
            current = nodes[0]

            index, nodes = yield from c.catalog.nodes(index=index)
            nodes.remove(current)
            assert [x['Node'] for x in nodes] == ['n1']

            index, nodes = yield from c.catalog.nodes(index=index)
            nodes.remove(current)
            assert [x['Node'] for x in nodes] == []
            yield from fut
            c.close()

        @asyncio.coroutine
        def register():
            yield from asyncio.sleep(1.0/100, loop=loop)
            response = yield from c.catalog.register('n1', '10.1.10.11')
            assert response is True
            yield from asyncio.sleep(50/1000.0, loop=loop)
            response = yield from c.catalog.deregister('n1')
            assert response is True

        loop.run_until_complete(nodes())

    def test_session(self, loop, consul_port):
        c = consul.aio.Consul(port=consul_port, loop=loop)

        @asyncio.coroutine
        def monitor():
            fut = asyncio.async(register(), loop=loop)
            index, services = yield from c.session.list()
            assert services == []
            yield from asyncio.sleep(20/1000.0, loop=loop)

            index, services = yield from c.session.list(index=index)
            assert len(services)

            index, services = yield from c.session.list(index=index)
            assert services == []
            yield from fut
            c.close()

        @asyncio.coroutine
        def register():
            yield from asyncio.sleep(1.0/100, loop=loop)
            session_id = yield from c.session.create()
            yield from asyncio.sleep(50/1000.0, loop=loop)
            response = yield from c.session.destroy(session_id)
            assert response is True

        loop.run_until_complete(monitor())

    def test_acl(self, loop, acl_consul):
        @asyncio.coroutine
        def main():
            c = consul.aio.Consul(
                port=acl_consul.port, token=acl_consul.token, loop=loop)

            rules = """
                key "" {
                    policy = "read"
                }
                key "private/" {
                    policy = "deny"
                }
            """
            token = yield from c.acl.create(rules=rules)

            try:
                yield from c.acl.list(token=token)
            except consul.ACLPermissionDenied:
                raised = True
            assert raised

            destroyed = yield from c.acl.destroy(token)
            assert destroyed is True
            c.close()

        loop.run_until_complete(main())

    @pytest.mark.skipif(sys.version_info < (3, 4, 1),
                        reason="Python <3.4.1 doesnt support __del__ calls "
                               "from GC")
    def test_httpclient__del__method(self, loop, consul_port, recwarn):

        @asyncio.coroutine
        def main():
            c = consul.aio.Consul(port=consul_port, loop=loop)
            _, _ = yield from c.kv.get('foo')
            del c
            import gc
            # run gc to ensure c is collected
            gc.collect()
            w = recwarn.pop(ResourceWarning)
            assert issubclass(w.category, ResourceWarning)

        loop.run_until_complete(main())