Skip to content

Commit

Permalink
stages(kickstart): ensure test inputs pass schema validation
Browse files Browse the repository at this point in the history
  • Loading branch information
mvo5 committed Nov 9, 2023
1 parent b9ad7dd commit a2e2217
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions stages/test/test_kickstart.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -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",),
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit a2e2217

Please sign in to comment.