From ece618d0d0aa16d8c7d6ffce1267ae07b7ac4df4 Mon Sep 17 00:00:00 2001 From: Achilleas Koutsou Date: Tue, 25 Feb 2025 15:16:39 +0100 Subject: [PATCH] stages/test: add unit tests for the dnf4.versionlock stage --- stages/test/test_dnf4_versionlock.py | 51 ++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 stages/test/test_dnf4_versionlock.py diff --git a/stages/test/test_dnf4_versionlock.py b/stages/test/test_dnf4_versionlock.py new file mode 100644 index 00000000..85a36a20 --- /dev/null +++ b/stages/test/test_dnf4_versionlock.py @@ -0,0 +1,51 @@ +#!/usr/bin/python3 +import os + +import pytest + +from osbuild import testutil + +STAGE_NAME = "org.osbuild.dnf4.versionlock" + + +@pytest.mark.parametrize("test_data,expected_err", [ + # bad + ({}, "'add' is a required property"), + # good + ({"add": ["shim-x64-*"]}, ""), + ({"add": ["proto-1:1.1", "deftero-0:2.2", "trito-3:3.3-3.fc33"]}, ""), +]) +def test_schema_validation_dnf4_versionlock(stage_schema, test_data, expected_err): + test_input = { + "type": STAGE_NAME, + "options": test_data + } + res = stage_schema.validate(test_input) + + if expected_err == "": + assert res.valid is True, f"err: {[e.as_dict() for e in res.errors]}" + else: + assert res.valid is False + testutil.assert_jsonschema_error_contains(res, expected_err, expected_num_errs=1) + + +@pytest.mark.parametrize("test_data", [ + ({"add": ["shim-x64-*"]}), + ({"add": ["proto-1:1.1", "deftero-0:2.2", "trito-3:3.3-3.fc33"]}), +]) +def test_locklist_dnf4_versionlock(tmp_path, stage_module, test_data): + plugins_dir = os.path.join(tmp_path, "etc/dnf/plugins/") + locklist_path = os.path.join(plugins_dir, "versionlock.list") + os.makedirs(plugins_dir) + stage_module.main(tmp_path, test_data) + + with open(locklist_path, mode="r", encoding="utf-8") as locklist_fp: + locklist_data = locklist_fp.readlines() + + for idx, package in enumerate(test_data["add"]): + assert locklist_data[idx * 3] == "\n" + + # let's ignore the timestamp, just check that the comment was written + assert locklist_data[idx * 3 + 1].startswith("# Added lock on") + + assert locklist_data[idx * 3 + 2] == package + "\n"