Debian Codegraph @

tests/test_aio.py

raw symbols SCIP
1 import asyncio
2 import base64
3 import struct
4
5 import pytest
6 from packaging import version
7
8 import consul
9 import consul.aio
10 import consul.check
11
12 Check = consul.check.Check
13
14
15 @pytest.fixture
16 async def consul_obj(consul_port):
17 consul_port, consul_version = consul_port
18 c = consul.aio.Consul(port=consul_port)
19 yield c, consul_version
20 await c.close()
21
22
23 @pytest.fixture
24 async def consul_acl_obj(acl_consul):
25 consul, token, consul_version = acl_consul
26 consul.token = token
27 yield consul, consul_version
28 await consul.close()
29
30
31 class TestAsyncioConsul:
32 async def test_kv(self, consul_obj) -> None:
33 c, _consul_version = consul_obj
34 _index, data = await c.kv.get("foo")
35 assert data is None
36 response = await c.kv.put("foo", "bar")
37 assert response is True
38 _index, data = await c.kv.get("foo")
39 assert data["Value"] == b"bar"
40
41 async def test_consul_ctor(self, consul_obj) -> None:
42 c, _consul_version = consul_obj
43 await c.kv.put("foo", struct.pack("i", 1000))
44 _index, data = await c.kv.get("foo")
45 assert struct.unpack("i", data["Value"]) == (1000,)
46
47 async def test_kv_binary(self, consul_obj) -> None:
48 c, _consul_version = consul_obj
49 await c.kv.put("foo", struct.pack("i", 1000))
50 _index, data = await c.kv.get("foo")
51 assert struct.unpack("i", data["Value"]) == (1000,)
52
53 async def test_kv_missing(self, consul_obj) -> None:
54 c, _consul_version = consul_obj
55
56 async def put() -> None:
57 await asyncio.sleep(2.0 / 100)
58 await c.kv.put("foo", "bar")
59
60 fut = asyncio.ensure_future(put())
61 await c.kv.put("index", "bump")
62 index, data = await c.kv.get("foo")
63 assert data is None
64 index, data = await c.kv.get("foo", index=index)
65 assert data["Value"] == b"bar"
66 await fut
67 await c.close()
68
69 async def test_kv_put_flags(self, consul_obj) -> None:
70 c, _consul_version = consul_obj
71 await c.kv.put("foo", "bar")
72 _index, data = await c.kv.get("foo")
73 assert data["Flags"] == 0
74
75 response = await c.kv.put("foo", "bar", flags=50)
76 assert response is True
77 _index, data = await c.kv.get("foo")
78 assert data["Flags"] == 50
79
80 async def test_kv_delete(self, consul_obj) -> None:
81 c, _consul_version = consul_obj
82 await c.kv.put("foo1", "1")
83 await c.kv.put("foo2", "2")
84 await c.kv.put("foo3", "3")
85 _index, data = await c.kv.get("foo", recurse=True)
86 assert [x["Key"] for x in data] == ["foo1", "foo2", "foo3"]
87
88 response = await c.kv.delete("foo2")
89 assert response is True
90 _index, data = await c.kv.get("foo", recurse=True)
91 assert [x["Key"] for x in data] == ["foo1", "foo3"]
92 response = await c.kv.delete("foo", recurse=True)
93 assert response is True
94 _index, data = await c.kv.get("foo", recurse=True)
95 assert data is None
96
97 async def test_kv_subscribe(self, consul_obj) -> None:
98 c, _consul_version = consul_obj
99
100 async def put() -> None:
101 await asyncio.sleep(1.0 / 100)
102 response = await c.kv.put("foo", "bar")
103 assert response is True
104
105 fut = asyncio.ensure_future(put())
106 index, data = await c.kv.get("foo")
107 assert data is None
108 index, data = await c.kv.get("foo", index=index)
109 assert data["Value"] == b"bar"
110 await fut
111
112 async def test_transaction(self, consul_obj) -> None:
113 c, _consul_version = consul_obj
114 value = base64.b64encode(b"1").decode("utf8")
115 d = {"KV": {"Verb": "set", "Key": "asdf", "Value": value}}
116 r = await c.txn.put([d])
117 assert r["Errors"] is None
118
119 d = {"KV": {"Verb": "get", "Key": "asdf"}}
120 r = await c.txn.put([d])
121 assert r["Results"][0]["KV"]["Value"] == value
122
123 async def test_agent_services(self, consul_obj) -> None:
124 c, _consul_version = consul_obj
125 EXPECTED = {
126 "v1": {
127 "foo": {
128 "Port": 0,
129 "ID": "foo",
130 "CreateIndex": 0,
131 "ModifyIndex": 0,
132 "EnableTagOverride": False,
133 "Service": "foo",
134 "Tags": [],
135 "Meta": {},
136 "Address": "",
137 }
138 },
139 "v2": {
140 "foo": {
141 "Address": "",
142 "Datacenter": "dc1",
143 "EnableTagOverride": False,
144 "ID": "foo",
145 "Meta": {},
146 "Port": 0,
147 "Service": "foo",
148 "Tags": [],
149 "Weights": {"Passing": 1, "Warning": 1},
150 }
151 },
152 }
153 expected = EXPECTED["v1"]
154 if version.parse(_consul_version) >= version.parse("1.13.8"):
155 expected = EXPECTED["v2"]
156
157 services = await c.agent.services()
158 assert services == {}
159 response = await c.agent.service.register("foo")
160 assert response is True
161 services = await c.agent.services()
162
163 assert services == expected
164 response = await c.agent.service.deregister("foo")
165 assert response is True
166 services = await c.agent.services()
167 assert services == {}
168
169 # async def test_catalog(self, acl_consul):
170 # port, token, _consul_version = acl_consul
171 # if should_skip(_consul_version, "<", "1.11.0"):
172 # pytest.skip("Endpoint /v1/acl/create for the legacy ACL system was removed in Consul 1.11.")
173 #
174 # c = consul.aio.Consul(port=port, token=token)
175 #
176 # async def register():
177 # await asyncio.sleep(1.0 / 100)
178 # response = await c.catalog.register("n1", "10.1.10.11")
179 # assert response is True
180 # await asyncio.sleep(500 / 1000.0)
181 # response = await c.catalog.deregister("n1")
182 # assert response is True
183 #
184 # fut = asyncio.ensure_future(register())
185 # index, nodes = await c.catalog.nodes()
186 # assert len(nodes) == 1
187 # current = nodes[0]
188 #
189 # index, nodes = await c.catalog.nodes(index=index)
190 # nodes.remove(current)
191 # assert [x["Node"] for x in nodes] == ["n1"]
192 #
193 # index, nodes = await c.catalog.nodes(index=index)
194 # nodes.remove(current)
195 # assert [x["Node"] for x in nodes] == []
196 # await fut
197
198 async def test_session(self, consul_obj) -> None:
199 c, _consul_version = consul_obj
200
201 async def register() -> None:
202 await asyncio.sleep(1.0 / 100)
203 session_id = await c.session.create()
204 await asyncio.sleep(50 / 1000.0)
205 response = await c.session.destroy(session_id)
206 assert response is True
207
208 fut = asyncio.ensure_future(register())
209 index, services = await c.session.list()
210 assert services == []
211 await asyncio.sleep(20 / 1000.0)
212
213 index, services = await c.session.list(index=index)
214 assert len(services)
215
216 index, services = await c.session.list(index=index)
217 assert services == []
218 await fut
219
220 # async def test_acl_old(self, acl_consul):
221 # port, token, _consul_version = acl_consul
222 # if should_skip(_consul_version, "<", "1.11.0"):
223 # pytest.skip("Endpoint /v1/acl/create for the legacy ACL system was removed in Consul 1.11.")
224 #
225 # c = consul.aio.Consul(port=port, token=token)
226 #
227 # rules = """
228 # key "" {
229 # policy = "read"
230 # }
231 # key "private/" {
232 # policy = "deny"
233 # }
234 # """
235 #
236 # token = await c.acl.create(rules=rules)
237 #
238 # with pytest.raises(consul.ACLPermissionDenied):
239 # await c.acl.list(token=token)
240 #
241 # destroyed = await c.acl.destroy(token)
242 # assert destroyed is True