| 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 |
|
| 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 |
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 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|