Skip to content

Commit b4fafb5

Browse files
phlogistonjohnmergify[bot]
authored andcommitted
tests: add test cases for toml formatted configuration
Signed-off-by: John Mulligan <[email protected]>
1 parent 5751af5 commit b4fafb5

File tree

1 file changed

+167
-1
lines changed

1 file changed

+167
-1
lines changed

tests/test_config.py

Lines changed: 167 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ def test_read_config_files_realerr(tmpdir):
448448

449449

450450
def test_tesd_config_files_realerr_rootok(monkeypatch):
451-
def err_open(p):
451+
def err_open(*args):
452452
raise OSError("test!")
453453

454454
monkeypatch.setattr(sambacc.config, "_open", err_open)
@@ -912,3 +912,169 @@ def test_jsonschema_validation(json_str, ok):
912912
else:
913913
with pytest.raises((ValueError, jsonschema.ValidationError)):
914914
cfg.load(io.StringIO(json_str), require_validation=True)
915+
916+
917+
@pytest.mark.parametrize(
918+
"toml_str,ok",
919+
[
920+
pytest.param("", False, id="empty"),
921+
pytest.param("#####FOO", False, id="just-a-comment"),
922+
pytest.param(
923+
"""
924+
samba-container-config = "v0"
925+
""",
926+
True,
927+
id="minimal",
928+
),
929+
pytest.param(
930+
"""
931+
samba-container-config = "v0"
932+
933+
# Define configurations
934+
[configs.foobar]
935+
shares = ["foobar"]
936+
937+
# Define share options
938+
[shares.foobar.options]
939+
a = "b"
940+
""",
941+
True,
942+
id="one-share",
943+
),
944+
],
945+
)
946+
def test_toml_configs_no_validation(toml_str, ok):
947+
pytest.importorskip("tomllib")
948+
949+
cfg = sambacc.config.GlobalConfig()
950+
fh = io.BytesIO(toml_str.encode("utf8"))
951+
if ok:
952+
cfg.load(
953+
fh,
954+
require_validation=False,
955+
config_format=sambacc.config.ConfigFormat.TOML,
956+
)
957+
else:
958+
with pytest.raises(ValueError):
959+
cfg.load(
960+
fh,
961+
require_validation=False,
962+
config_format=sambacc.config.ConfigFormat.TOML,
963+
)
964+
965+
966+
@pytest.mark.parametrize(
967+
"toml_str,ok",
968+
[
969+
pytest.param("", False, id="empty"),
970+
pytest.param("#####FOO", False, id="just-a-comment"),
971+
pytest.param(
972+
"""
973+
samba-container-config = "v0"
974+
""",
975+
True,
976+
id="minimal",
977+
),
978+
pytest.param(
979+
"""
980+
samba-container-config = "v0"
981+
982+
# Define configurations
983+
[configs.foobar]
984+
shares = ["foobar"]
985+
986+
# Define share options
987+
[shares.foobar.options]
988+
a = "b"
989+
""",
990+
True,
991+
id="one-share",
992+
),
993+
pytest.param(
994+
"""
995+
samba-container-config = "v0"
996+
997+
# Define configurations
998+
[configs.foobar]
999+
shares = ["foobar"]
1000+
instance_features = "Kibbe"
1001+
1002+
# Define share options
1003+
[shares.foobar.options]
1004+
a = "b"
1005+
""",
1006+
False,
1007+
id="bad-instance_features",
1008+
),
1009+
pytest.param(
1010+
"""
1011+
samba-container-config = "v0"
1012+
1013+
# Define configurations
1014+
[configs.foobar]
1015+
shares = ["foobar"]
1016+
instance_features = ["ctdb"]
1017+
1018+
# Define share options
1019+
[shares.foobar.options]
1020+
a = "b"
1021+
""",
1022+
True,
1023+
id="ok-instance_features",
1024+
),
1025+
pytest.param(
1026+
"""
1027+
samba-container-config = "v0"
1028+
1029+
[configs.demo]
1030+
shares = ["share"]
1031+
globals = ["default"]
1032+
instance_features = ["ctdb"]
1033+
instance_name = "SAMBA"
1034+
1035+
[shares.share.options]
1036+
"path" = "/share"
1037+
"read only" = "no"
1038+
"valid users" = "sambauser, otheruser"
1039+
1040+
[globals.default.options]
1041+
"security" = "user"
1042+
"server min protocol" = "SMB2"
1043+
"load printers" = "no"
1044+
"printing" = "bsd"
1045+
"printcap name" = "/dev/null"
1046+
"disable spoolss" = "yes"
1047+
"guest ok" = "no"
1048+
1049+
[[users.all_entries]]
1050+
name = "sambauser"
1051+
password = "samba"
1052+
1053+
[[users.all_entries]]
1054+
name = "otheruser"
1055+
password = "insecure321"
1056+
""",
1057+
True,
1058+
id="complex",
1059+
),
1060+
],
1061+
)
1062+
def test_toml_configs_validation(toml_str, ok):
1063+
pytest.importorskip("tomllib")
1064+
jsonschema = pytest.importorskip("jsonschema")
1065+
1066+
cfg = sambacc.config.GlobalConfig()
1067+
fh = io.BytesIO(toml_str.encode("utf8"))
1068+
if ok:
1069+
cfg.load(
1070+
fh,
1071+
require_validation=True,
1072+
config_format=sambacc.config.ConfigFormat.TOML,
1073+
)
1074+
else:
1075+
with pytest.raises((ValueError, jsonschema.ValidationError)):
1076+
cfg.load(
1077+
fh,
1078+
require_validation=True,
1079+
config_format=sambacc.config.ConfigFormat.TOML,
1080+
)

0 commit comments

Comments
 (0)