Skip to content

Commit 4542939

Browse files
tests: add tests for the update_config command function
Signed-off-by: John Mulligan <[email protected]>
1 parent b755c5b commit 4542939

File tree

1 file changed

+282
-0
lines changed

1 file changed

+282
-0
lines changed

tests/test_commands_config.py

Lines changed: 282 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,282 @@
1+
#
2+
# sambacc: a samba container configuration tool
3+
# Copyright (C) 2022 John Mulligan
4+
#
5+
# This program is free software: you can redistribute it and/or modify
6+
# it under the terms of the GNU General Public License as published by
7+
# the Free Software Foundation, either version 3 of the License, or
8+
# (at your option) any later version.
9+
#
10+
# This program is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU General Public License
16+
# along with this program. If not, see <http://www.gnu.org/licenses/>
17+
#
18+
19+
import argparse
20+
import functools
21+
import os
22+
23+
import sambacc.config
24+
import sambacc.paths
25+
26+
import sambacc.commands.config
27+
28+
29+
config1 = """
30+
{
31+
"samba-container-config": "v0",
32+
"configs": {
33+
"updateme":{
34+
"shares": ["uno"],
35+
"globals": ["global0"]
36+
}
37+
},
38+
"shares": {
39+
"uno": {
40+
"options": {
41+
"path": "/srv/data/uno"
42+
}
43+
}
44+
},
45+
"globals": {
46+
"global0": {
47+
"options": {
48+
"server min protocol": "SMB2"
49+
}
50+
}
51+
}
52+
}
53+
"""
54+
55+
config2 = """
56+
{
57+
"samba-container-config": "v0",
58+
"configs": {
59+
"updateme":{
60+
"shares": ["uno", "dos"],
61+
"globals": ["global0"]
62+
}
63+
},
64+
"shares": {
65+
"uno": {
66+
"options": {
67+
"path": "/srv/data/uno"
68+
}
69+
},
70+
"dos": {
71+
"options": {
72+
"path": "/srv/data/dos"
73+
}
74+
}
75+
},
76+
"globals": {
77+
"global0": {
78+
"options": {
79+
"server min protocol": "SMB2"
80+
}
81+
}
82+
}
83+
}
84+
"""
85+
86+
87+
class FakeContext:
88+
cli: argparse.Namespace
89+
instance_config: sambacc.config.InstanceConfig
90+
91+
def __init__(self, opts, instance_config):
92+
self.cli = argparse.Namespace()
93+
self.instance_config = instance_config
94+
for k, v in opts.items():
95+
setattr(self.cli, k, v)
96+
97+
@classmethod
98+
def defaults(cls, cfg_path, watch=False):
99+
with open(cfg_path, "w") as fh:
100+
fh.write(config1)
101+
102+
config = [cfg_path]
103+
identity = "updateme"
104+
ctx = cls(
105+
{
106+
"watch": watch,
107+
"config": config,
108+
"identity": identity,
109+
},
110+
sambacc.config.read_config_files(config).get(identity),
111+
)
112+
return ctx
113+
114+
115+
class FakeWaiter:
116+
def __init__(self, attempts=None):
117+
self.count = 0
118+
self.on_count = {}
119+
self.attempts = attempts
120+
121+
def acted(self):
122+
pass
123+
124+
def wait(self):
125+
if self.attempts is not None and self.count >= self.attempts:
126+
raise KeyboardInterrupt()
127+
wf = self.on_count.get(self.count, None)
128+
if wf is not None:
129+
wf()
130+
self.count += 1
131+
132+
133+
def _gen_fake_cmd(fake_path, chkpath, pnn="0"):
134+
with open(fake_path, "w") as fh:
135+
fh.write("#!/bin/sh\n")
136+
fh.write(f'echo "$@" >> {chkpath}\n')
137+
fh.write(f'[ "$1" = ctdb ] && echo {pnn}" " ; \n')
138+
fh.write("exit 0\n")
139+
os.chmod(fake_path, 0o755)
140+
141+
142+
def test_update_config_changed(tmp_path, monkeypatch):
143+
cfg_path = str(tmp_path / "config")
144+
fake = tmp_path / "fake.sh"
145+
chkpath = tmp_path / ".executed"
146+
_gen_fake_cmd(fake, str(chkpath))
147+
monkeypatch.setattr(sambacc.samba_cmds, "_GLOBAL_PREFIX", [str(fake)])
148+
149+
ctx = FakeContext.defaults(cfg_path)
150+
with open(cfg_path, "w") as fh:
151+
fh.write(config2)
152+
monkeypatch.setattr(
153+
sambacc.paths,
154+
"ensure_share_dirs",
155+
functools.partial(
156+
sambacc.paths.ensure_share_dirs, root=str(tmp_path / "_root")
157+
),
158+
)
159+
sambacc.commands.config.update_config(ctx)
160+
161+
assert os.path.exists(chkpath)
162+
chk = open(chkpath).readlines()
163+
assert any(("net" in line) for line in chk)
164+
assert any(("smbcontrol" in line) for line in chk)
165+
166+
167+
def test_update_config_changed_ctdb(tmp_path, monkeypatch):
168+
cfg_path = str(tmp_path / "config")
169+
fake = tmp_path / "fake.sh"
170+
chkpath = tmp_path / ".executed"
171+
_gen_fake_cmd(fake, str(chkpath))
172+
monkeypatch.setattr(sambacc.samba_cmds, "_GLOBAL_PREFIX", [str(fake)])
173+
174+
ctx = FakeContext.defaults(cfg_path)
175+
ctx.instance_config.iconfig["instance_features"] = ["ctdb"]
176+
assert ctx.instance_config.with_ctdb
177+
with open(cfg_path, "w") as fh:
178+
fh.write(config2)
179+
monkeypatch.setattr(
180+
sambacc.paths,
181+
"ensure_share_dirs",
182+
functools.partial(
183+
sambacc.paths.ensure_share_dirs, root=str(tmp_path / "_root")
184+
),
185+
)
186+
sambacc.commands.config.update_config(ctx)
187+
188+
assert os.path.exists(chkpath)
189+
chk = open(chkpath).readlines()
190+
assert any(("net" in line) for line in chk)
191+
assert any(("smbcontrol" in line) for line in chk)
192+
193+
194+
def test_update_config_ctdb_notleader(tmp_path, monkeypatch):
195+
cfg_path = str(tmp_path / "config")
196+
fake = tmp_path / "fake.sh"
197+
chkpath = tmp_path / ".executed"
198+
_gen_fake_cmd(fake, str(chkpath), pnn="")
199+
monkeypatch.setattr(sambacc.samba_cmds, "_GLOBAL_PREFIX", [str(fake)])
200+
201+
ctx = FakeContext.defaults(cfg_path)
202+
ctx.instance_config.iconfig["instance_features"] = ["ctdb"]
203+
assert ctx.instance_config.with_ctdb
204+
with open(cfg_path, "w") as fh:
205+
fh.write(config2)
206+
monkeypatch.setattr(
207+
sambacc.paths,
208+
"ensure_share_dirs",
209+
functools.partial(
210+
sambacc.paths.ensure_share_dirs, root=str(tmp_path / "_root")
211+
),
212+
)
213+
sambacc.commands.config.update_config(ctx)
214+
215+
assert os.path.exists(chkpath)
216+
chk = open(chkpath).readlines()
217+
assert not any(("net" in line) for line in chk)
218+
assert not any(("smbcontrol" in line) for line in chk)
219+
220+
221+
def test_update_config_watch_waiter_expires(tmp_path, monkeypatch):
222+
cfg_path = str(tmp_path / "config")
223+
fake = tmp_path / "fake.sh"
224+
chkpath = tmp_path / ".executed"
225+
_gen_fake_cmd(fake, str(chkpath))
226+
monkeypatch.setattr(sambacc.samba_cmds, "_GLOBAL_PREFIX", [str(fake)])
227+
228+
fake_waiter = FakeWaiter(attempts=5)
229+
230+
def _fake_waiter(*args, **kwargs):
231+
return fake_waiter
232+
233+
monkeypatch.setattr(sambacc.commands.config, "best_waiter", _fake_waiter)
234+
235+
ctx = FakeContext.defaults(cfg_path, watch=True)
236+
monkeypatch.setattr(
237+
sambacc.paths,
238+
"ensure_share_dirs",
239+
functools.partial(
240+
sambacc.paths.ensure_share_dirs, root=str(tmp_path / "_root")
241+
),
242+
)
243+
sambacc.commands.config.update_config(ctx)
244+
245+
assert not os.path.exists(chkpath)
246+
assert fake_waiter.count == 5
247+
248+
249+
def test_update_config_watch_waiter_trigger3(tmp_path, monkeypatch):
250+
cfg_path = str(tmp_path / "config")
251+
fake = tmp_path / "fake.sh"
252+
chkpath = tmp_path / ".executed"
253+
_gen_fake_cmd(fake, str(chkpath))
254+
monkeypatch.setattr(sambacc.samba_cmds, "_GLOBAL_PREFIX", [str(fake)])
255+
256+
fake_waiter = FakeWaiter(attempts=5)
257+
258+
def _fake_waiter(*args, **kwargs):
259+
return fake_waiter
260+
261+
def _new_conf():
262+
with open(cfg_path, "w") as fh:
263+
fh.write(config2)
264+
265+
monkeypatch.setattr(sambacc.commands.config, "best_waiter", _fake_waiter)
266+
fake_waiter.on_count[3] = _new_conf
267+
268+
ctx = FakeContext.defaults(cfg_path, watch=True)
269+
monkeypatch.setattr(
270+
sambacc.paths,
271+
"ensure_share_dirs",
272+
functools.partial(
273+
sambacc.paths.ensure_share_dirs, root=str(tmp_path / "_root")
274+
),
275+
)
276+
sambacc.commands.config.update_config(ctx)
277+
278+
assert os.path.exists(chkpath)
279+
chk = open(chkpath).readlines()
280+
assert any(("net" in line) for line in chk)
281+
assert any(("smbcontrol" in line) for line in chk)
282+
assert fake_waiter.count == 5

0 commit comments

Comments
 (0)