Skip to content

Commit 83d5863

Browse files
phlogistonjohnmergify[bot]
authored andcommitted
tests: add test cases for yaml formatted configuration
Signed-off-by: John Mulligan <[email protected]>
1 parent b0c8e39 commit 83d5863

File tree

1 file changed

+174
-0
lines changed

1 file changed

+174
-0
lines changed

tests/test_config.py

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,3 +1078,177 @@ def test_toml_configs_validation(toml_str, ok):
10781078
require_validation=True,
10791079
config_format=sambacc.config.ConfigFormat.TOML,
10801080
)
1081+
1082+
1083+
@pytest.mark.parametrize(
1084+
"yaml_str,ok",
1085+
[
1086+
pytest.param("", False, id="empty"),
1087+
pytest.param("#####FOO", False, id="just-a-comment"),
1088+
pytest.param(
1089+
"""
1090+
samba-container-config: "v0"
1091+
""",
1092+
True,
1093+
id="minimal",
1094+
),
1095+
pytest.param(
1096+
"""
1097+
samba-container-config: "v0"
1098+
# Define configurations
1099+
configs:
1100+
foobar:
1101+
shares:
1102+
- foobar
1103+
shares:
1104+
foobar:
1105+
options:
1106+
a: b
1107+
""",
1108+
True,
1109+
id="one-share",
1110+
),
1111+
],
1112+
)
1113+
def test_yaml_configs_no_validation(yaml_str, ok):
1114+
pytest.importorskip("yaml")
1115+
1116+
cfg = sambacc.config.GlobalConfig()
1117+
fh = io.BytesIO(yaml_str.encode("utf8"))
1118+
if ok:
1119+
cfg.load(
1120+
fh,
1121+
require_validation=False,
1122+
config_format=sambacc.config.ConfigFormat.YAML,
1123+
)
1124+
else:
1125+
with pytest.raises(ValueError):
1126+
cfg.load(
1127+
fh,
1128+
require_validation=False,
1129+
config_format=sambacc.config.ConfigFormat.YAML,
1130+
)
1131+
1132+
1133+
@pytest.mark.parametrize(
1134+
"yaml_str,ok",
1135+
[
1136+
pytest.param("", False, id="empty"),
1137+
pytest.param("#####FOO", False, id="just-a-comment"),
1138+
pytest.param(
1139+
"""
1140+
samba-container-config: v0
1141+
""",
1142+
True,
1143+
id="minimal",
1144+
),
1145+
pytest.param(
1146+
"""
1147+
samba-container-config: v0
1148+
# Define configurations
1149+
configs:
1150+
foobar:
1151+
shares:
1152+
- foobar
1153+
# Define shares
1154+
shares:
1155+
foobar:
1156+
options:
1157+
a: b
1158+
""",
1159+
True,
1160+
id="one-share",
1161+
),
1162+
pytest.param(
1163+
"""
1164+
samba-container-config: v0
1165+
configs:
1166+
foobar:
1167+
instance_features: baroof
1168+
shares:
1169+
- foobar
1170+
shares:
1171+
foobar:
1172+
options:
1173+
a: b
1174+
""",
1175+
False,
1176+
id="bad-instance_features",
1177+
),
1178+
pytest.param(
1179+
"""
1180+
samba-container-config: v0
1181+
configs:
1182+
foobar:
1183+
instance_features:
1184+
- ctdb
1185+
shares:
1186+
- foobar
1187+
shares:
1188+
foobar:
1189+
options:
1190+
a: b
1191+
""",
1192+
True,
1193+
id="ok-instance_features",
1194+
),
1195+
pytest.param(
1196+
"""
1197+
samba-container-config: "v0"
1198+
# Configure our demo instance
1199+
configs:
1200+
demo:
1201+
shares: ["share"]
1202+
globals: ["default"]
1203+
instance_features: ["ctdb"]
1204+
instance_name: "SAMBA"
1205+
# Configure the share
1206+
shares:
1207+
share:
1208+
options:
1209+
"path": "/share"
1210+
"read only": "no"
1211+
"valid users": "sambauser, otheruser"
1212+
# Configure globals
1213+
globals:
1214+
default:
1215+
options:
1216+
"security": "user"
1217+
"server min protocol": "SMB2"
1218+
"load printers": "no"
1219+
"printing": "bsd"
1220+
"printcap name": "/dev/null"
1221+
"disable spoolss": "yes"
1222+
"guest ok": "no"
1223+
# Configure users
1224+
users:
1225+
all_entries:
1226+
- {"name": "sambauser", "password": "samba"}
1227+
- {"name": "otheruser", "password": "insecure321"}
1228+
- name: cooluser
1229+
password: snarf
1230+
""",
1231+
True,
1232+
id="complex",
1233+
),
1234+
],
1235+
)
1236+
def test_yaml_configs_validation(yaml_str, ok):
1237+
pytest.importorskip("yaml")
1238+
jsonschema = pytest.importorskip("jsonschema")
1239+
1240+
cfg = sambacc.config.GlobalConfig()
1241+
fh = io.BytesIO(yaml_str.encode("utf8"))
1242+
if ok:
1243+
cfg.load(
1244+
fh,
1245+
require_validation=True,
1246+
config_format=sambacc.config.ConfigFormat.YAML,
1247+
)
1248+
else:
1249+
with pytest.raises((ValueError, jsonschema.ValidationError)):
1250+
cfg.load(
1251+
fh,
1252+
require_validation=True,
1253+
config_format=sambacc.config.ConfigFormat.YAML,
1254+
)

0 commit comments

Comments
 (0)