Skip to content

Commit 7a3a2e5

Browse files
romainxphilpep
authored andcommitted
supervisorctl and supervisord.conf path
1 parent 45d9753 commit 7a3a2e5

File tree

2 files changed

+76
-15
lines changed

2 files changed

+76
-15
lines changed

test/test_modules.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,14 @@ def test_ansible_module_options(host):
423423

424424

425425
@pytest.mark.destructive
426-
def test_supervisor(host):
426+
@pytest.mark.parametrize(
427+
"supervisorctl_path,supervisorctl_conf",
428+
[
429+
("supervisorctl", None),
430+
("/usr/bin/supervisorctl", "/etc/supervisor/supervisord.conf"),
431+
],
432+
)
433+
def test_supervisor(host, supervisorctl_path, supervisorctl_conf):
427434
# Wait supervisord is running
428435
for _ in range(20):
429436
if host.service("supervisor").is_running:
@@ -433,7 +440,11 @@ def test_supervisor(host):
433440
raise RuntimeError("No running supervisor")
434441

435442
for _ in range(20):
436-
service = host.supervisor("tail")
443+
service = host.supervisor(
444+
"tail",
445+
supervisorctl_path=supervisorctl_path,
446+
supervisorctl_conf=supervisorctl_conf,
447+
)
437448
if service.status == "RUNNING":
438449
break
439450
else:
@@ -446,22 +457,33 @@ def test_supervisor(host):
446457
proc = host.process.get(pid=service.pid)
447458
assert proc.comm == "tail"
448459

449-
services = host.supervisor.get_services()
460+
services = host.supervisor.get_services(supervisorctl_path, supervisorctl_conf)
450461
assert len(services) == 1
451462
assert services[0].name == "tail"
452463
assert services[0].is_running
453464
assert services[0].pid == service.pid
465+
# Checking if conf is propagated
466+
assert services[0].supervisorctl_path == supervisorctl_path
467+
assert services[0].supervisorctl_conf == supervisorctl_conf
454468

455469
host.run("supervisorctl stop tail")
456-
service = host.supervisor("tail")
470+
service = host.supervisor(
471+
"tail",
472+
supervisorctl_path=supervisorctl_path,
473+
supervisorctl_conf=supervisorctl_conf,
474+
)
457475
assert not service.is_running
458476
assert service.status == "STOPPED"
459477
assert service.pid is None
460478

461479
host.run("service supervisor stop")
462480
assert not host.service("supervisor").is_running
463481
with pytest.raises(RuntimeError) as excinfo:
464-
host.supervisor("tail").is_running
482+
host.supervisor(
483+
"tail",
484+
supervisorctl_path=supervisorctl_path,
485+
supervisorctl_conf=supervisorctl_conf,
486+
).is_running
465487
assert "Is supervisor running" in str(excinfo.value)
466488

467489

testinfra/modules/supervisor.py

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,24 @@ class Supervisor(Module):
3434
True
3535
>>> gunicorn.pid
3636
4242
37+
38+
The path where supervisorctl and its configuration file reside can be specified.
39+
40+
>>> gunicorn = host.supervisor("gunicorn", "/usr/bin/supervisorctl", "/etc/supervisor/supervisord.conf")
41+
>>> gunicorn.status
42+
'RUNNING'
3743
"""
3844

39-
def __init__(self, name, _attrs_cache=None):
45+
def __init__(
46+
self,
47+
name,
48+
supervisorctl_path="supervisorctl",
49+
supervisorctl_conf=None,
50+
_attrs_cache=None,
51+
):
4052
self.name = name
53+
self.supervisorctl_path = supervisorctl_path
54+
self.supervisorctl_conf = supervisorctl_conf
4155
self._attrs_cache = _attrs_cache
4256
super().__init__()
4357

@@ -64,7 +78,17 @@ def _parse_status(line):
6478
@property
6579
def _attrs(self):
6680
if self._attrs_cache is None:
67-
line = self.check_output("supervisorctl status %s", self.name)
81+
if self.supervisorctl_conf:
82+
line = self.check_output(
83+
"%s -c %s status %s",
84+
self.supervisorctl_path,
85+
self.supervisorctl_conf,
86+
self.name,
87+
)
88+
else:
89+
line = self.check_output(
90+
"%s status %s", self.supervisorctl_path, self.name
91+
)
6892
attrs = self._parse_status(line)
6993
assert attrs["name"] == self.name
7094
self._attrs_cache = attrs
@@ -92,23 +116,38 @@ def pid(self):
92116
return self._attrs["pid"]
93117

94118
@classmethod
95-
def get_services(cls):
119+
def get_services(
120+
cls,
121+
supervisorctl_path="supervisorctl",
122+
supervisorctl_conf=None,
123+
):
96124
"""Get a list of services running under supervisor
97125
98126
>>> host.supervisor.get_services()
127+
[<Supervisor(name="gunicorn", status="RUNNING", pid=4232)>
128+
<Supervisor(name="celery", status="FATAL", pid=None)>]
129+
130+
The path where supervisorctl and its configuration file reside can be specified.
131+
132+
>>> host.supervisor.get_services("/usr/bin/supervisorctl", "/etc/supervisor/supervisord.conf")
99133
[<Supervisor(name="gunicorn", status="RUNNING", pid=4232)>
100134
<Supervisor(name="celery", status="FATAL", pid=None)>]
101135
"""
102136
services = []
103-
for line in (
104-
cls(None)
105-
.check_output(
106-
"supervisorctl status",
137+
if supervisorctl_conf:
138+
out = cls.check_output(
139+
"%s -c %s status", supervisorctl_path, supervisorctl_conf
107140
)
108-
.splitlines()
109-
):
141+
else:
142+
out = cls.check_output("%s status", supervisorctl_path)
143+
for line in out.splitlines():
110144
attrs = cls._parse_status(line)
111-
service = cls(attrs["name"], attrs)
145+
service = cls(
146+
attrs["name"],
147+
supervisorctl_path=supervisorctl_path,
148+
supervisorctl_conf=supervisorctl_conf,
149+
_attrs_cache=attrs,
150+
)
112151
services.append(service)
113152
return services
114153

0 commit comments

Comments
 (0)