Skip to content

Commit 4e89239

Browse files
phlogistonjohnmergify[bot]
authored andcommitted
tests: add test_grpc_backend.py to test server backend module
Signed-off-by: John Mulligan <[email protected]>
1 parent 1b4208a commit 4e89239

File tree

1 file changed

+246
-0
lines changed

1 file changed

+246
-0
lines changed

tests/test_grpc_backend.py

Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
#
2+
# sambacc: a samba container configuration tool
3+
# Copyright (C) 2025 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 io
20+
import os
21+
22+
import pytest
23+
24+
import sambacc.grpc.backend
25+
26+
27+
config1 = """
28+
{
29+
"samba-container-config": "v0",
30+
"configs": {
31+
"foobar": {
32+
"shares": [
33+
"share",
34+
"stuff"
35+
],
36+
"globals": ["global0"],
37+
"instance_name": "GANDOLPH"
38+
}
39+
},
40+
"shares": {
41+
"share": {
42+
"options": {
43+
"path": "/share",
44+
"read only": "no",
45+
"valid users": "sambauser",
46+
"guest ok": "no",
47+
"force user": "root"
48+
}
49+
},
50+
"stuff": {
51+
"options": {
52+
"path": "/mnt/stuff"
53+
}
54+
}
55+
},
56+
"globals": {
57+
"global0": {
58+
"options": {
59+
"workgroup": "SAMBA",
60+
"security": "user",
61+
"server min protocol": "SMB2",
62+
"load printers": "no",
63+
"printing": "bsd",
64+
"printcap name": "/dev/null",
65+
"disable spoolss": "yes",
66+
"guest ok": "no"
67+
}
68+
}
69+
},
70+
"_extra_junk": 0
71+
}
72+
"""
73+
74+
json1 = """
75+
{
76+
"timestamp": "2025-05-08T20:41:57.273489+0000",
77+
"version": "4.23.0pre1-UNKNOWN",
78+
"smb_conf": "/etc/samba/smb.conf",
79+
"sessions": {
80+
"2891148582": {
81+
"session_id": "2891148582",
82+
"server_id": {
83+
"pid": "1243",
84+
"task_id": "0",
85+
"vnn": "2",
86+
"unique_id": "1518712196307698939"
87+
},
88+
"uid": 103107,
89+
"gid": 102513,
90+
"username": "DOMAIN1\\\\bwayne",
91+
"groupname": "DOMAIN1\\\\domain users",
92+
"creation_time": "2025-05-08T20:39:36.456835+00:00",
93+
"expiration_time": "30828-09-14T02:48:05.477581+00:00",
94+
"auth_time": "2025-05-08T20:39:36.457633+00:00",
95+
"remote_machine": "127.0.0.1",
96+
"hostname": "ipv4:127.0.0.1:59396",
97+
"session_dialect": "SMB3_11",
98+
"client_guid": "adc145fe-0677-4ab6-9d61-c25b30211174",
99+
"encryption": {
100+
"cipher": "-",
101+
"degree": "none"
102+
},
103+
"signing": {
104+
"cipher": "AES-128-GMAC",
105+
"degree": "partial"
106+
},
107+
"channels": {
108+
"1": {
109+
"channel_id": "1",
110+
"creation_time": "2025-05-08T20:39:36.456835+00:00",
111+
"local_address": "ipv4:127.0.0.1:445",
112+
"remote_address": "ipv4:127.0.0.1:59396",
113+
"transport": "tcp"
114+
}
115+
}
116+
}
117+
},
118+
"tcons": {
119+
"3757739897": {
120+
"service": "cephomatic",
121+
"server_id": {
122+
"pid": "1243",
123+
"task_id": "0",
124+
"vnn": "2",
125+
"unique_id": "1518712196307698939"
126+
},
127+
"tcon_id": "3757739897",
128+
"session_id": "2891148582",
129+
"machine": "127.0.0.1",
130+
"connected_at": "2025-05-08T20:39:36.464088+00:00",
131+
"encryption": {
132+
"cipher": "-",
133+
"degree": "none"
134+
},
135+
"signing": {
136+
"cipher": "-",
137+
"degree": "none"
138+
}
139+
}
140+
},
141+
"open_files": {}
142+
}
143+
"""
144+
145+
146+
def _status_json1_check(status):
147+
assert status.timestamp == "2025-05-08T20:41:57.273489+0000"
148+
assert len(status.sessions) == 1
149+
s1 = status.sessions[0]
150+
assert s1.session_id == "2891148582"
151+
assert s1.username == "DOMAIN1\\bwayne"
152+
assert s1.groupname == "DOMAIN1\\domain users"
153+
assert s1.remote_machine == "127.0.0.1"
154+
assert s1.hostname == "ipv4:127.0.0.1:59396"
155+
assert s1.session_dialect == "SMB3_11"
156+
assert s1.uid == 103107
157+
assert s1.gid == 102513
158+
assert len(status.tcons) == 1
159+
t1 = status.tcons[0]
160+
assert t1.tcon_id == "3757739897"
161+
assert t1.session_id == "2891148582"
162+
assert t1.service_name == "cephomatic"
163+
164+
165+
def _fake_command(tmp_path, monkeypatch, *, output="", exitcode=0):
166+
fakedir = tmp_path / "fake"
167+
fake = fakedir / "fake.sh"
168+
outfile = fakedir / "stdout"
169+
170+
print(fakedir)
171+
print(fakedir.mkdir, fakedir.mkdir.__doc__)
172+
fakedir.mkdir(parents=True, exist_ok=True)
173+
monkeypatch.setattr(sambacc.samba_cmds, "_GLOBAL_PREFIX", [str(fake)])
174+
175+
if output:
176+
outfile.write_text(output)
177+
fake.write_text(
178+
"#!/bin/sh\n"
179+
f"test -f {outfile} && cat {outfile}\n"
180+
f"exit {exitcode}\n"
181+
)
182+
os.chmod(fake, 0o755)
183+
184+
185+
def _instance_config():
186+
fh = io.StringIO(config1)
187+
g = sambacc.config.GlobalConfig(fh)
188+
return g.get("foobar")
189+
190+
191+
def test_parse_status():
192+
status = sambacc.grpc.backend.Status.parse(json1)
193+
_status_json1_check(status)
194+
195+
196+
def test_backend_versions(tmp_path, monkeypatch):
197+
_fake_command(tmp_path, monkeypatch, output="Version 4.99.99\n")
198+
backend = sambacc.grpc.backend.ControlBackend(_instance_config())
199+
v = backend.get_versions()
200+
assert v.samba_version == "Version 4.99.99"
201+
202+
203+
def test_backend_is_clustered(tmp_path, monkeypatch):
204+
_fake_command(tmp_path, monkeypatch)
205+
backend = sambacc.grpc.backend.ControlBackend(_instance_config())
206+
assert not backend.is_clustered()
207+
208+
209+
def test_backend_status(tmp_path, monkeypatch):
210+
_fake_command(tmp_path, monkeypatch, output=json1)
211+
backend = sambacc.grpc.backend.ControlBackend(_instance_config())
212+
status = backend.get_status()
213+
_status_json1_check(status)
214+
215+
216+
def test_backend_status_error(tmp_path, monkeypatch):
217+
_fake_command(tmp_path, monkeypatch, exitcode=2)
218+
backend = sambacc.grpc.backend.ControlBackend(_instance_config())
219+
with pytest.raises(Exception):
220+
backend.get_status()
221+
222+
223+
def test_backend_close_share(tmp_path, monkeypatch):
224+
_fake_command(tmp_path, monkeypatch)
225+
backend = sambacc.grpc.backend.ControlBackend(_instance_config())
226+
backend.close_share("share", denied_users=False)
227+
228+
229+
def test_backend_close_share_error(tmp_path, monkeypatch):
230+
_fake_command(tmp_path, monkeypatch, exitcode=2)
231+
backend = sambacc.grpc.backend.ControlBackend(_instance_config())
232+
with pytest.raises(Exception):
233+
backend.close_share("share", denied_users=False)
234+
235+
236+
def test_backend_kill_client(tmp_path, monkeypatch):
237+
_fake_command(tmp_path, monkeypatch)
238+
backend = sambacc.grpc.backend.ControlBackend(_instance_config())
239+
backend.kill_client("127.0.0.1")
240+
241+
242+
def test_backend_kill_client_error(tmp_path, monkeypatch):
243+
_fake_command(tmp_path, monkeypatch, exitcode=2)
244+
backend = sambacc.grpc.backend.ControlBackend(_instance_config())
245+
with pytest.raises(Exception):
246+
backend.kill_client("127.0.0.1")

0 commit comments

Comments
 (0)