Skip to content

Commit

Permalink
[WIP] tests: multi version tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathias Brulatout committed Aug 28, 2023
1 parent 7267570 commit a4be9e7
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 90 deletions.
68 changes: 48 additions & 20 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,17 @@
import py
import pytest
import requests
from packaging import version

collect_ignore = []

CONSUL_BINARIES = {
"1.1.0": "consul",
"1.13.8": "consul-1.13.8",
"1.15.4": "consul-1.15.4",
"1.16.1": "consul-1.16.1",
}


def get_free_ports(num, host=None):
if not host:
Expand All @@ -31,7 +39,7 @@ def get_free_ports(num, host=None):
return ret


def start_consul_instance(acl_master_token=None):
def start_consul_instance(binary_name, acl_master_token=None):
"""
starts a consul instance. if acl_master_token is None, acl will be disabled
for this server, otherwise it will be enabled and the master token will be
Expand All @@ -52,8 +60,8 @@ def start_consul_instance(acl_master_token=None):
tmpdir.chdir()

(system, _node, _release, _version, _machine, _processor) = platform.uname()
postfix = "osx" if system == "Darwin" else "linux64"
binary = os.path.join(os.path.dirname(__file__), "consul." + postfix)
ext = "osx" if system == "Darwin" else "linux64"
binary = os.path.join(os.path.dirname(__file__), f"{binary_name}.{ext}")
command = "{bin} agent -dev -bind=127.0.0.1 -config-dir=."
command = command.format(bin=binary).strip()
command = shlex.split(command)
Expand Down Expand Up @@ -97,31 +105,51 @@ def clean_consul(port):
requests.put(base_uri + f"agent/service/deregister/{s}", timeout=10)


@pytest.fixture(scope="module")
def consul_instance():
p, port = start_consul_instance()
yield port
p.terminate()
def get_consul_version(port):
base_uri = f"http://127.0.0.1:{port}/v1/"
response = requests.get(base_uri + "agent/self", timeout=10)
return response.json()["Config"]["Version"].strip()


@pytest.fixture()
def consul_port(consul_instance):
port = consul_instance
yield port
clean_consul(port)
@pytest.fixture(scope="module", params=CONSUL_BINARIES.keys())
def consul_instance(request):
p, port = start_consul_instance(binary_name=CONSUL_BINARIES[request.param])
version = get_consul_version(port)
yield port, version
p.terminate()


@pytest.fixture(scope="module")
def acl_consul_instance():
@pytest.fixture(scope="module", params=CONSUL_BINARIES.keys())
def acl_consul_instance(request):
acl_master_token = uuid.uuid4().hex
p, port = start_consul_instance(acl_master_token=acl_master_token)
yield port, acl_master_token
p, port = start_consul_instance(binary_name=CONSUL_BINARIES[request.param], acl_master_token=acl_master_token)
version = get_consul_version(port)
yield port, acl_master_token, version
p.terminate()


@pytest.fixture()
def consul_port(consul_instance):
port, version = consul_instance
yield port, version
clean_consul(port)


@pytest.fixture()
def acl_consul(acl_consul_instance):
ACLConsul = collections.namedtuple("ACLConsul", ["port", "token"])
port, token = acl_consul_instance
yield ACLConsul(port, token)
ACLConsul = collections.namedtuple("ACLConsul", ["port", "token", "version"])
port, token, version = acl_consul_instance
yield ACLConsul(port, token, version)
clean_consul(port)


def should_skip(version_str, comparator, ref_version_str):
v = version.parse(version_str)
ref_version = version.parse(ref_version_str)

if comparator == "<" and v >= ref_version:
return f"Requires version {comparator} {ref_version_str}"
if comparator == ">" and v <= ref_version:
return f"Requires version {comparator} {ref_version_str}"
# You can add other comparators if needed
return None
Binary file added tests/consul-1.13.8.linux64
Binary file not shown.
Binary file added tests/consul-1.15.4.linux64
Binary file not shown.
Binary file added tests/consul-1.16.1.linux64
Binary file not shown.
88 changes: 59 additions & 29 deletions tests/test_aio.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,34 @@
import struct

import pytest
from packaging import version

import consul
import consul.aio
from tests.conftest import should_skip

Check = consul.Check


@pytest.fixture
async def consul_obj(consul_port):
consul_port, consul_version = consul_port
c = consul.aio.Consul(port=consul_port)
yield c
yield c, consul_version
await c.close()


@pytest.fixture
async def consul_acl_obj(acl_consul):
c = consul.aio.Consul(port=acl_consul.port, token=acl_consul.token)
yield c
consul_port, token, consul_version = acl_consul
c = consul.aio.Consul(port=consul_port, token=token)
yield c, consul_version
await c.close()


class TestAsyncioConsul:
async def test_kv(self, consul_obj):
c = consul_obj
c, _consul_version = consul_obj
_index, data = await c.kv.get("foo")
assert data is None
response = await c.kv.put("foo", "bar")
Expand All @@ -35,19 +39,19 @@ async def test_kv(self, consul_obj):
assert data["Value"] == b"bar"

async def test_consul_ctor(self, consul_obj):
c = consul_obj
c, _consul_version = consul_obj
await c.kv.put("foo", struct.pack("i", 1000))
_index, data = await c.kv.get("foo")
assert struct.unpack("i", data["Value"]) == (1000,)

async def test_kv_binary(self, consul_obj):
c = consul_obj
c, _consul_version = consul_obj
await c.kv.put("foo", struct.pack("i", 1000))
_index, data = await c.kv.get("foo")
assert struct.unpack("i", data["Value"]) == (1000,)

async def test_kv_missing(self, consul_obj):
c = consul_obj
c, _consul_version = consul_obj

async def put():
await asyncio.sleep(2.0 / 100)
Expand All @@ -63,7 +67,7 @@ async def put():
await c.close()

async def test_kv_put_flags(self, consul_obj):
c = consul_obj
c, _consul_version = consul_obj
await c.kv.put("foo", "bar")
_index, data = await c.kv.get("foo")
assert data["Flags"] == 0
Expand All @@ -74,7 +78,7 @@ async def test_kv_put_flags(self, consul_obj):
assert data["Flags"] == 50

async def test_kv_delete(self, consul_obj):
c = consul_obj
c, _consul_version = consul_obj
await c.kv.put("foo1", "1")
await c.kv.put("foo2", "2")
await c.kv.put("foo3", "3")
Expand All @@ -91,7 +95,7 @@ async def test_kv_delete(self, consul_obj):
assert data is None

async def test_kv_subscribe(self, consul_obj):
c = consul_obj
c, _consul_version = consul_obj

async def put():
await asyncio.sleep(1.0 / 100)
Expand All @@ -106,7 +110,7 @@ async def put():
await fut

async def test_transaction(self, consul_obj):
c = consul_obj
c, _consul_version = consul_obj
value = base64.b64encode(b"1").decode("utf8")
d = {"KV": {"Verb": "set", "Key": "asdf", "Value": value}}
r = await c.txn.put([d])
Expand All @@ -117,32 +121,53 @@ async def test_transaction(self, consul_obj):
assert r["Results"][0]["KV"]["Value"] == value

async def test_agent_services(self, consul_obj):
c = consul_obj
c, _consul_version = consul_obj
EXPECTED = {
"v1": {
"foo": {
"Port": 0,
"ID": "foo",
"CreateIndex": 0,
"ModifyIndex": 0,
"EnableTagOverride": False,
"Service": "foo",
"Tags": [],
"Meta": {},
"Address": "",
}
},
"v2": {
"foo": {
"Address": "",
"Datacenter": "dc1",
"EnableTagOverride": False,
"ID": "foo",
"Meta": {},
"Port": 0,
"Service": "foo",
"Tags": [],
"Weights": {"Passing": 1, "Warning": 1},
}
},
}
expected = EXPECTED["v1"]
if version.parse(_consul_version) >= version.parse("1.13.8"):
expected = EXPECTED["v2"]

services = await c.agent.services()
assert services == {}
response = await c.agent.service.register("foo")
assert response is True
services = await c.agent.services()
assert services == {
"foo": {
"Port": 0,
"ID": "foo",
"CreateIndex": 0,
"ModifyIndex": 0,
"EnableTagOverride": False,
"Service": "foo",
"Tags": [],
"Meta": {},
"Address": "",
},
}

assert services == expected
response = await c.agent.service.deregister("foo")
assert response is True
services = await c.agent.services()
assert services == {}

async def test_catalog(self, consul_obj):
c = consul_obj
c, _consul_version = consul_obj

async def register():
await asyncio.sleep(1.0 / 100)
Expand All @@ -167,7 +192,7 @@ async def register():
await fut

async def test_session(self, consul_obj):
c = consul_obj
c, _consul_version = consul_obj

async def register():
await asyncio.sleep(1.0 / 100)
Expand All @@ -188,8 +213,13 @@ async def register():
assert services == []
await fut

async def test_acl(self, consul_acl_obj):
c = consul_acl_obj
async def test_acl_old(self, consul_acl_obj):
c, _consul_version = consul_acl_obj

# TODO

if should_skip(_consul_version, "<", "1.11.0"):
pytest.skip("Endpoint /v1/acl/create for the legacy ACL system was removed in Consul 1.11.")

rules = """
key "" {
Expand Down
Loading

0 comments on commit a4be9e7

Please sign in to comment.