From bea378f1911fde604f16258a87e69ef1d3cb12cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Hozza?= Date: Fri, 11 Apr 2025 14:16:41 +0200 Subject: [PATCH] Stages/rhsm: add unit test for the stage schema MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I forgot to add the file to https://github.com/osbuild/osbuild/pull/2070. Signed-off-by: Tomáš Hozza --- stages/test/test_rhsm.py | 143 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 stages/test/test_rhsm.py diff --git a/stages/test/test_rhsm.py b/stages/test/test_rhsm.py new file mode 100644 index 00000000..a91d2f75 --- /dev/null +++ b/stages/test/test_rhsm.py @@ -0,0 +1,143 @@ +#!/usr/bin/python3 + +import re + +import pytest + +from osbuild.testutil import ( + assert_jsonschema_error_contains, +) + +STAGE_NAME = "org.osbuild.rhsm" + + +@pytest.mark.parametrize("test_data,expected_err", [ + # bad + ({ + "dnf-plugins": { + "random": { + "enabled": False, + }, + "subscription-manager": { + "enabled": False, + }, + }, + }, r"Additional properties are not allowed \('random' was unexpected\)"), + ({ + "dnf-plugins": { + "product-id": { + "enhanced": False, + }, + "subscription-manager": { + "enabled": False, + }, + }, + }, r"Additional properties are not allowed \('enhanced' was unexpected\)"), + ({ + "subscription-manager": { + "rhsm": { + "manage_repos": 45, + "auto_enable_yum_plugins": False + }, + "rhsmcertd": { + "auto_registration": False, + }, + } + }, r"45 is not of type 'boolean'"), + ({ + "subscription-manager": { + "rhsm": { + "random": False + }, + "rhsmcertd": { + "auto_registration": False, + }, + } + }, r"Additional properties are not allowed \('random' was unexpected\)"), + ({ + "subscription-manager": { + "random": {}, + } + }, r"Additional properties are not allowed \('random' was unexpected\)"), + ({ + "subscription-manager": { + "rhsmcertd": { + "random": False, + }, + } + }, r"Additional properties are not allowed \('random' was unexpected\)"), + # good + ({}, ""), + ({ + "dnf-plugins": {}, + "yum-plugins": {}, + "subscription-manager": {}, + }, ""), + ({ + "dnf-plugins": { + "product-id": { + "enabled": False, + }, + "subscription-manager": { + "enabled": False, + }, + }, + "yum-plugins": { + "product-id": { + "enabled": False, + }, + "subscription-manager": { + "enabled": False, + }, + }, + "subscription-manager": { + "rhsm": { + "manage_repos": False, + "auto_enable_yum_plugins": False, + }, + "rhsmcertd": { + "auto_registration": False, + }, + }, + }, ""), + ({ + "dnf-plugins": { + "product-id": { + "enabled": True, + }, + "subscription-manager": { + "enabled": True, + }, + }, + "yum-plugins": { + "product-id": { + "enabled": True, + }, + "subscription-manager": { + "enabled": True, + }, + }, + "subscription-manager": { + "rhsm": { + "manage_repos": True, + "auto_enable_yum_plugins": True, + }, + "rhsmcertd": { + "auto_registration": True, + }, + }, + }, ""), +]) +def test_schema_validation(stage_schema, test_data, expected_err): + test_input = { + "type": STAGE_NAME, + "options": {}, + } + test_input["options"].update(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 + assert_jsonschema_error_contains(res, re.compile(expected_err), expected_num_errs=1)