From a2e22177e68cecc5ad2907badfb0da6e19e7e933 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Thu, 9 Nov 2023 12:27:24 +0100 Subject: [PATCH] stages(kickstart): ensure test inputs pass schema validation --- stages/test/test_kickstart.py | 38 ++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/stages/test/test_kickstart.py b/stages/test/test_kickstart.py index 1bd64ace68..f8ec46d8d3 100644 --- a/stages/test/test_kickstart.py +++ b/stages/test/test_kickstart.py @@ -9,6 +9,21 @@ from osbuild.testutil.imports import import_module_from_path +def schema_kickstart_validate(test_data): + name = "org.osbuild.kickstart" + root = os.path.join(os.path.dirname(__file__), "../..") + mod_info = osbuild.meta.ModuleInfo.load(root, "Stage", name) + schema = osbuild.meta.Schema(mod_info.get_schema(), name) + test_input = { + "name": "org.osbuild.kickstart", + "options": { + "path": "some-path", + } + } + test_input["options"].update(test_data) + return schema.validate(test_input) + + @pytest.mark.parametrize("test_input,expected", [ ({"lang": "en_US.UTF-8"}, "lang en_US.UTF-8"), ({"keyboard": "us"}, "keyboard us"), @@ -54,7 +69,7 @@ "user --name someusr --password $1$notreally --iscrypted --shell /bin/ksh --uid 1337 --gid 1337 --groups grp1,grp2 --homedir /other/home/someusr\n" + 'sshkey --username someusr "ssh-rsa not-really-a-real-key"' ), - ({"zerombr": "true"}, "zerombr"), + ({"zerombr": True}, "zerombr"), ({"clearpart": {}}, "clearpart"), ({"clearpart": {"all": True}}, "clearpart --all"), ({"clearpart": {"drives": ["hda", "hdb"]}}, "clearpart --drives=hda,hdb",), @@ -107,6 +122,13 @@ def test_kickstart(tmp_path, test_input, expected): # double check with pykickstart if the file looks valid subprocess.check_call(["ksvalidator", ks_path]) + # ensure the test inputs are valid + res = schema_kickstart_validate(test_input) + if not res.valid: + print(f"input: {test_input}") + print(f"error: {[e.as_dict()['message'] for e in res.errors]}") + assert res.valid == True + @pytest.mark.parametrize("test_data,expected_err", [ # BAD pattern, ensure some obvious ways to write arbitrary @@ -124,19 +146,7 @@ def test_kickstart(tmp_path, test_input, expected): ({"clearpart": {"list": ["sda2", "sda3", "sdb1"]}}, ""), ]) def test_schema_validation_smoke(test_data, expected_err): - name = "org.osbuild.kickstart" - root = os.path.join(os.path.dirname(__file__), "../..") - mod_info = osbuild.meta.ModuleInfo.load(root, "Stage", name) - schema = osbuild.meta.Schema(mod_info.get_schema(), name) - - test_input = { - "name": "org.osbuild.kickstart", - "options": { - "path": "some-path", - } - } - test_input["options"].update(test_data) - res = schema.validate(test_input) + res = schema_kickstart_validate(test_data) if expected_err == "": assert res.valid is True