Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Service registration with multiple checks #59

Merged
merged 1 commit into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion consul/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,7 @@ def register(
http=None,
timeout=None,
enable_tag_override=False,
extra_checks=None,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not define an empty list instead of None?

):
"""
Add a new service to the local agent. There is more
Expand Down Expand Up @@ -877,6 +878,8 @@ def register(
https://www.consul.io/docs/agent/services.html
"""

if extra_checks is None:
extra_checks = []
payload = {}

payload["name"] = name
Expand All @@ -893,7 +896,7 @@ def register(
if meta:
payload["meta"] = meta
if check:
payload["check"] = check
payload["checks"] = [check] + extra_checks
if weights:
payload["weights"] = weights

Expand Down
33 changes: 32 additions & 1 deletion tests/test_std.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,35 @@ def verify_check_status(check_id, status, notes=None):
verify_check_status("ttl_check", "critical")
verify_and_dereg_check("ttl_check")

def test_service_multi_check(self, consul_port):
c = consul.Consul(port=consul_port)
http_addr = f"http://127.0.0.1:{consul_port}"
c.agent.service.register(
"foo1",
check=Check.http(http_addr, "10ms"),
extra_checks=[
Check.http(http_addr, "20ms"),
Check.http(http_addr, "30ms"),
],
)

time.sleep(200 / 1000.0)

_index, nodes = c.health.service("foo1")
assert {check["ServiceID"] for node in nodes for check in node["Checks"]} == {"foo1", ""}

assert {check["CheckID"] for node in nodes for check in node["Checks"]} == {
"service:foo1:1",
"service:foo1:2",
"service:foo1:3",
"serfHealth",
}
time.sleep(1)

_index, checks = c.health.checks(service="foo1")
assert [check["CheckID"] for check in checks] == ["service:foo1:1", "service:foo1:2", "service:foo1:3"]
assert [check["Status"] for check in checks] == ["passing", "passing", "passing"]

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"
Expand Down Expand Up @@ -274,15 +303,17 @@ def test_agent_checks_service_id(self, consul_obj):
assert [node["Service"]["ID"] for node in nodes] == ["foo1"]

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

time.sleep(40 / 1000.0)

_index, nodes = c.health.service("foo1")
assert {check["ServiceID"] for node in nodes for check in node["Checks"]} == {"foo1", ""}
assert {check["CheckID"] for node in nodes for check in node["Checks"]} == {"foo", "serfHealth"}
assert {check["CheckID"] for node in nodes for check in node["Checks"]} == {"foo", "foo2", "serfHealth"}

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

time.sleep(40 / 1000.0)

Expand Down
Loading