osbuild: allow json data to come from a {stage}-meta.json file

Instead of always parsing the python stage to load meta information
allow the user of a new `{stage}-meta.json` file. This is a first
step towards allowing modules to be written in a different language
than python. It also has some practical advantages:
- slightly faster as it avoids calling python to output the schemas
- easier to write schemas as this can be done in a real json editor
  now
- more extensible in a future where stages maybe binaries with
  shlib dependencies that are only satisfied in the buildroot
  but not on the host
This commit is contained in:
Michael Vogt 2023-11-21 12:51:02 +01:00 committed by Simon de Vlieger
parent 3dd12931e4
commit 9b09ed9eb4
4 changed files with 139 additions and 22 deletions

View file

@ -1,4 +1,6 @@
import os
import pathlib
import textwrap
import pytest
@ -168,3 +170,80 @@ def test_schema():
assert not res
res = schema.validate([1, 2, 3])
assert res
def make_fake_meta_json(tmp_path, name):
meta_json_path = pathlib.Path(f"{tmp_path}/stages/{name}-meta.json")
meta_json_path.parent.mkdir(exist_ok=True)
meta_json_path.write_text("""
{
"summary": "some json summary",
"description": [
"long text",
"with newlines"
],
"capabilities": ["CAP_MAC_ADMIN", "CAP_BIG_MAC"],
"schema": {
"properties": {
"json_filename": {
"type": "string"
}
}
},
"schema_2": {
"json_devices": {
"type": "object"
}
}
}
""".replace("\n", " "), encoding="utf-8")
return meta_json_path
def make_fake_py_module(tmp_path, name):
py_path = pathlib.Path(f"{tmp_path}/stages/{name}")
py_path.parent.mkdir(exist_ok=True)
fake_py = '"""some py summary\nlong description\nwith newline"""'
fake_py += textwrap.dedent("""
SCHEMA = '"properties": {"py_filename":{"type": "string"}}'
SCHEMA_2 = '"py_devices": {"type":"object"}'
CAPABILITIES = ['CAP_MAC_ADMIN']
""")
py_path.write_text(fake_py, encoding="utf-8")
def test_load_from_json(tmp_path):
make_fake_meta_json(tmp_path, "org.osbuild.noop")
modinfo = osbuild.meta.ModuleInfo.load(tmp_path, "Stage", "org.osbuild.noop")
assert modinfo.desc == "some json summary"
assert modinfo.info == "long text\nwith newlines"
assert modinfo.caps == ["CAP_MAC_ADMIN", "CAP_BIG_MAC"]
assert modinfo.opts == {
"1": {"properties": {"json_filename": {"type": "string"}}},
"2": {"json_devices": {"type": "object"}},
}
def test_load_from_py(tmp_path):
make_fake_py_module(tmp_path, "org.osbuild.noop")
modinfo = osbuild.meta.ModuleInfo.load(tmp_path, "Stage", "org.osbuild.noop")
assert modinfo.desc == "some py summary"
assert modinfo.info == "long description\nwith newline"
assert modinfo.caps == set(["CAP_MAC_ADMIN"])
assert modinfo.opts == {
"1": {"properties": {"py_filename": {"type": "string"}}},
"2": {"py_devices": {"type": "object"}},
}
def test_load_from_json_prefered(tmp_path):
make_fake_meta_json(tmp_path, "org.osbuild.noop")
make_fake_py_module(tmp_path, "org.osbuild.noop")
modinfo = osbuild.meta.ModuleInfo.load(tmp_path, "Stage", "org.osbuild.noop")
assert modinfo.desc == "some json summary"
assert modinfo.info == "long text\nwith newlines"
assert modinfo.caps == ["CAP_MAC_ADMIN", "CAP_BIG_MAC"]
assert modinfo.opts == {
"1": {"properties": {"json_filename": {"type": "string"}}},
"2": {"json_devices": {"type": "object"}},
}