Skip to content

Commit

Permalink
supervisorctl and supervisord.conf path
Browse files Browse the repository at this point in the history
  • Loading branch information
romainx authored and philpep committed Mar 18, 2021
1 parent 45d9753 commit 7a3a2e5
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 15 deletions.
32 changes: 27 additions & 5 deletions test/test_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,14 @@ def test_ansible_module_options(host):


@pytest.mark.destructive
def test_supervisor(host):
@pytest.mark.parametrize(
"supervisorctl_path,supervisorctl_conf",
[
("supervisorctl", None),
("/usr/bin/supervisorctl", "/etc/supervisor/supervisord.conf"),
],
)
def test_supervisor(host, supervisorctl_path, supervisorctl_conf):
# Wait supervisord is running
for _ in range(20):
if host.service("supervisor").is_running:
Expand All @@ -433,7 +440,11 @@ def test_supervisor(host):
raise RuntimeError("No running supervisor")

for _ in range(20):
service = host.supervisor("tail")
service = host.supervisor(
"tail",
supervisorctl_path=supervisorctl_path,
supervisorctl_conf=supervisorctl_conf,
)
if service.status == "RUNNING":
break
else:
Expand All @@ -446,22 +457,33 @@ def test_supervisor(host):
proc = host.process.get(pid=service.pid)
assert proc.comm == "tail"

services = host.supervisor.get_services()
services = host.supervisor.get_services(supervisorctl_path, supervisorctl_conf)
assert len(services) == 1
assert services[0].name == "tail"
assert services[0].is_running
assert services[0].pid == service.pid
# Checking if conf is propagated
assert services[0].supervisorctl_path == supervisorctl_path
assert services[0].supervisorctl_conf == supervisorctl_conf

host.run("supervisorctl stop tail")
service = host.supervisor("tail")
service = host.supervisor(
"tail",
supervisorctl_path=supervisorctl_path,
supervisorctl_conf=supervisorctl_conf,
)
assert not service.is_running
assert service.status == "STOPPED"
assert service.pid is None

host.run("service supervisor stop")
assert not host.service("supervisor").is_running
with pytest.raises(RuntimeError) as excinfo:
host.supervisor("tail").is_running
host.supervisor(
"tail",
supervisorctl_path=supervisorctl_path,
supervisorctl_conf=supervisorctl_conf,
).is_running
assert "Is supervisor running" in str(excinfo.value)


Expand Down
59 changes: 49 additions & 10 deletions testinfra/modules/supervisor.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,24 @@ class Supervisor(Module):
True
>>> gunicorn.pid
4242
The path where supervisorctl and its configuration file reside can be specified.
>>> gunicorn = host.supervisor("gunicorn", "/usr/bin/supervisorctl", "/etc/supervisor/supervisord.conf")
>>> gunicorn.status
'RUNNING'
"""

def __init__(self, name, _attrs_cache=None):
def __init__(
self,
name,
supervisorctl_path="supervisorctl",
supervisorctl_conf=None,
_attrs_cache=None,
):
self.name = name
self.supervisorctl_path = supervisorctl_path
self.supervisorctl_conf = supervisorctl_conf
self._attrs_cache = _attrs_cache
super().__init__()

Expand All @@ -64,7 +78,17 @@ def _parse_status(line):
@property
def _attrs(self):
if self._attrs_cache is None:
line = self.check_output("supervisorctl status %s", self.name)
if self.supervisorctl_conf:
line = self.check_output(
"%s -c %s status %s",
self.supervisorctl_path,
self.supervisorctl_conf,
self.name,
)
else:
line = self.check_output(
"%s status %s", self.supervisorctl_path, self.name
)
attrs = self._parse_status(line)
assert attrs["name"] == self.name
self._attrs_cache = attrs
Expand Down Expand Up @@ -92,23 +116,38 @@ def pid(self):
return self._attrs["pid"]

@classmethod
def get_services(cls):
def get_services(
cls,
supervisorctl_path="supervisorctl",
supervisorctl_conf=None,
):
"""Get a list of services running under supervisor
>>> host.supervisor.get_services()
[<Supervisor(name="gunicorn", status="RUNNING", pid=4232)>
<Supervisor(name="celery", status="FATAL", pid=None)>]
The path where supervisorctl and its configuration file reside can be specified.
>>> host.supervisor.get_services("/usr/bin/supervisorctl", "/etc/supervisor/supervisord.conf")
[<Supervisor(name="gunicorn", status="RUNNING", pid=4232)>
<Supervisor(name="celery", status="FATAL", pid=None)>]
"""
services = []
for line in (
cls(None)
.check_output(
"supervisorctl status",
if supervisorctl_conf:
out = cls.check_output(
"%s -c %s status", supervisorctl_path, supervisorctl_conf
)
.splitlines()
):
else:
out = cls.check_output("%s status", supervisorctl_path)
for line in out.splitlines():
attrs = cls._parse_status(line)
service = cls(attrs["name"], attrs)
service = cls(
attrs["name"],
supervisorctl_path=supervisorctl_path,
supervisorctl_conf=supervisorctl_conf,
_attrs_cache=attrs,
)
services.append(service)
return services

Expand Down

0 comments on commit 7a3a2e5

Please sign in to comment.