Skip to content

Commit e5f9de2

Browse files
committed
Fix systemd exporter tests
1 parent cf1205a commit e5f9de2

File tree

1 file changed

+123
-63
lines changed

1 file changed

+123
-63
lines changed

tests/test_export_systemd.py

Lines changed: 123 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55

66
from honcho.export.systemd import Export
77

8-
FakeProcess = collections.namedtuple('FakeProcess', 'name')
8+
FakeProcess = collections.namedtuple("FakeProcess", "name")
99

10-
FIX_1PROC = [FakeProcess('web.1')]
11-
FIX_NPROC = [FakeProcess('web.1'),
12-
FakeProcess('worker.1'),
13-
FakeProcess('worker.2')]
10+
FIX_1PROC = [FakeProcess("web.1")]
11+
FIX_NPROC = [FakeProcess("web.1"), FakeProcess("worker.1"), FakeProcess("worker.2")]
1412

1513

1614
class TestExportSystemd(object):
@@ -23,104 +21,166 @@ def exporter(self, request):
2321
self.process = MagicMock()
2422

2523
def _get_template(name):
26-
if name.endswith('process_master.target'):
24+
if name.endswith("process_master.target"):
2725
return self.process_master
28-
elif name.endswith('process.service'):
26+
elif name.endswith("process.service"):
2927
return self.process
30-
elif name.endswith('master.target'):
28+
elif name.endswith("master.target"):
3129
return self.master
3230
else:
3331
raise RuntimeError("tests don't know about that template")
3432

35-
get_template_patcher = patch.object(Export, 'get_template')
33+
get_template_patcher = patch.object(Export, "get_template")
3634
self.get_template = get_template_patcher.start()
3735
self.get_template.side_effect = _get_template
3836
request.addfinalizer(get_template_patcher.stop)
3937

4038
@pytest.fixture(autouse=True)
4139
def file(self, request):
42-
file_patcher = patch('honcho.export.systemd.File')
40+
file_patcher = patch("honcho.export.systemd.File")
4341
self.File = file_patcher.start()
4442
request.addfinalizer(file_patcher.stop)
4543

4644
def test_render_master(self):
47-
out = list(self.export.render(FIX_1PROC, {'app': 'elephant'}))
45+
out = list(self.export.render(FIX_1PROC, {"app": "elephant"}))
4846

49-
master = self.File('elephant.target', self.master.render.return_value)
47+
master = self.File("elephant.target", self.master.render.return_value)
5048
assert master in out
5149

52-
expected = {'app': 'elephant',
53-
'master_wants': 'elephant-web.target',
54-
'process_groups': [('elephant-web', [('elephant-web-1', FIX_1PROC[0])])],
55-
'process_master_wants': 'elephant-web-1.service', 'process': FIX_1PROC[0]}
50+
expected = {
51+
"app": "elephant",
52+
"master_wants": "elephant-web.target",
53+
"process_groups": [("elephant-web", [("elephant-web-1", FIX_1PROC[0])])],
54+
"process_master_name": "elephant-web",
55+
"process_master_wants": "elephant-web-1.service",
56+
"process": FIX_1PROC[0],
57+
}
5658

5759
self.master.render.assert_called_once_with(expected)
5860

5961
def test_render_process_master(self):
60-
out = list(self.export.render(FIX_1PROC, {'app': 'elephant'}))
62+
out = list(self.export.render(FIX_1PROC, {"app": "elephant"}))
6163

62-
process_master = self.File('elephant-web.target',
63-
self.process_master.render.return_value)
64+
process_master = self.File(
65+
"elephant-web.target", self.process_master.render.return_value
66+
)
6467
assert process_master in out
6568

66-
expected = {'app': 'elephant',
67-
'master_wants': 'elephant-web.target',
68-
'process_groups': [('elephant-web', [('elephant-web-1', FIX_1PROC[0])])],
69-
'process_master_wants': 'elephant-web-1.service', 'process': FIX_1PROC[0]}
69+
expected = {
70+
"app": "elephant",
71+
"master_wants": "elephant-web.target",
72+
"process_groups": [("elephant-web", [("elephant-web-1", FIX_1PROC[0])])],
73+
"process_master_name": "elephant-web",
74+
"process_master_wants": "elephant-web-1.service",
75+
"process": FIX_1PROC[0],
76+
}
7077
self.process_master.render.assert_called_once_with(expected)
7178

7279
def test_render_process(self):
73-
out = list(self.export.render(FIX_1PROC, {'app': 'elephant'}))
80+
out = list(self.export.render(FIX_1PROC, {"app": "elephant"}))
7481

75-
process = self.File('elephant-web-1.service',
76-
self.process.render.return_value)
82+
process = self.File("elephant-web-1.service", self.process.render.return_value)
7783
assert process in out
7884

79-
expected = {'app': 'elephant',
80-
'master_wants': 'elephant-web.target',
81-
'process_groups': [('elephant-web', [('elephant-web-1', FIX_1PROC[0])])],
82-
'process_master_wants': 'elephant-web-1.service', 'process': FIX_1PROC[0]}
85+
expected = {
86+
"app": "elephant",
87+
"master_wants": "elephant-web.target",
88+
"process_groups": [("elephant-web", [("elephant-web-1", FIX_1PROC[0])])],
89+
"process_master_name": "elephant-web",
90+
"process_master_wants": "elephant-web-1.service",
91+
"process": FIX_1PROC[0],
92+
}
8393
self.process.render.assert_called_once_with(expected)
8494

8595
def test_render_multiple_process_groups(self):
86-
out = list(self.export.render(FIX_NPROC, {'app': 'elephant'}))
87-
88-
assert self.File('elephant-web.target',
89-
self.process_master.render.return_value) in out
90-
assert self.File('elephant-worker.target',
91-
self.process_master.render.return_value) in out
92-
93-
expected = [call({'app': 'elephant', 'master_wants': 'elephant-web.target elephant-worker.target',
94-
'process_groups': [('elephant-web', [('elephant-web-1', FIX_NPROC[0])]), (
95-
'elephant-worker', [('elephant-worker-1', FIX_NPROC[1]),
96-
('elephant-worker-2', FIX_NPROC[2])])],
97-
'process_master_wants': 'elephant-worker-1.service elephant-worker-2.service',
98-
'process': FIX_NPROC[2]}),
99-
call({'app': 'elephant', 'master_wants': 'elephant-web.target elephant-worker.target',
100-
'process_groups': [('elephant-web', [('elephant-web-1', FIX_NPROC[0])]), (
101-
'elephant-worker', [('elephant-worker-1', FIX_NPROC[1]),
102-
('elephant-worker-2', FIX_NPROC[2])])],
103-
'process_master_wants': 'elephant-worker-1.service elephant-worker-2.service',
104-
'process': FIX_NPROC[2]})]
96+
out = list(self.export.render(FIX_NPROC, {"app": "elephant"}))
97+
98+
assert (
99+
self.File("elephant-web.target", self.process_master.render.return_value)
100+
in out
101+
)
102+
assert (
103+
self.File("elephant-worker.target", self.process_master.render.return_value)
104+
in out
105+
)
106+
107+
expected = [
108+
call(
109+
{
110+
"app": "elephant",
111+
"master_wants": "elephant-web.target elephant-worker.target",
112+
"process_groups": [
113+
("elephant-web", [("elephant-web-1", FIX_NPROC[0])]),
114+
(
115+
"elephant-worker",
116+
[
117+
("elephant-worker-1", FIX_NPROC[1]),
118+
("elephant-worker-2", FIX_NPROC[2]),
119+
],
120+
),
121+
],
122+
"process_master_name": "elephant-worker",
123+
"process_master_wants": "elephant-worker-1.service elephant-worker-2.service",
124+
"process": FIX_NPROC[2],
125+
}
126+
),
127+
call(
128+
{
129+
"app": "elephant",
130+
"master_wants": "elephant-web.target elephant-worker.target",
131+
"process_groups": [
132+
("elephant-web", [("elephant-web-1", FIX_NPROC[0])]),
133+
(
134+
"elephant-worker",
135+
[
136+
("elephant-worker-1", FIX_NPROC[1]),
137+
("elephant-worker-2", FIX_NPROC[2]),
138+
],
139+
),
140+
],
141+
"process_master_name": "elephant-worker",
142+
"process_master_wants": "elephant-worker-1.service elephant-worker-2.service",
143+
"process": FIX_NPROC[2],
144+
}
145+
),
146+
]
105147

106148
assert self.process_master.render.call_args_list == expected
107149

108150
def test_render_multiple_processes(self):
109-
out = list(self.export.render(FIX_NPROC, {'app': 'elephant'}))
110-
111-
assert self.File('elephant-web-1.service',
112-
self.process.render.return_value) in out
113-
assert self.File('elephant-worker-1.service',
114-
self.process.render.return_value) in out
115-
assert self.File('elephant-worker-2.service',
116-
self.process.render.return_value) in out
151+
out = list(self.export.render(FIX_NPROC, {"app": "elephant"}))
152+
153+
assert (
154+
self.File("elephant-web-1.service", self.process.render.return_value) in out
155+
)
156+
assert (
157+
self.File("elephant-worker-1.service", self.process.render.return_value)
158+
in out
159+
)
160+
assert (
161+
self.File("elephant-worker-2.service", self.process.render.return_value)
162+
in out
163+
)
117164

118165
print(self.process.render.call_args_list)
119-
expected = call({'app': 'elephant', 'master_wants': 'elephant-web.target elephant-worker.target',
120-
'process_groups': [('elephant-web', [('elephant-web-1', FIX_NPROC[0])]), (
121-
'elephant-worker', [('elephant-worker-1', FIX_NPROC[1]),
122-
('elephant-worker-2', FIX_NPROC[2])])],
123-
'process_master_wants': 'elephant-worker-1.service elephant-worker-2.service',
124-
'process': FIX_NPROC[2]})
166+
expected = call(
167+
{
168+
"app": "elephant",
169+
"master_wants": "elephant-web.target elephant-worker.target",
170+
"process_groups": [
171+
("elephant-web", [("elephant-web-1", FIX_NPROC[0])]),
172+
(
173+
"elephant-worker",
174+
[
175+
("elephant-worker-1", FIX_NPROC[1]),
176+
("elephant-worker-2", FIX_NPROC[2]),
177+
],
178+
),
179+
],
180+
"process_master_name": "elephant-worker",
181+
"process_master_wants": "elephant-worker-1.service elephant-worker-2.service",
182+
"process": FIX_NPROC[2],
183+
}
184+
)
125185
expected = [expected] * 3
126186
assert self.process.render.call_args_list == expected

0 commit comments

Comments
 (0)