diff --git a/cloudinit/config/schema.py b/cloudinit/config/schema.py index c7c23731aac..c72c4ed5f1c 100644 --- a/cloudinit/config/schema.py +++ b/cloudinit/config/schema.py @@ -31,18 +31,18 @@ import yaml -from cloudinit import features, importer, safeyaml +from cloudinit import importer, safeyaml from cloudinit.cmd.devel import read_cfg_paths from cloudinit.handlers import INCLUSION_TYPES_MAP, type_from_starts_with from cloudinit.helpers import Paths from cloudinit.sources import DataSourceNotFoundException from cloudinit.temp_utils import mkdtemp from cloudinit.util import ( - Version, error, get_modules_from_dir, load_text_file, load_yaml, + should_log_deprecation, write_file, ) @@ -795,16 +795,14 @@ def validate_cloudconfig_schema( if isinstance( schema_error, SchemaDeprecationError ): # pylint: disable=W1116 - if "devel" != features.DEPRECATION_INFO_BOUNDARY and ( - schema_error.version == "devel" - or Version.from_str(schema_error.version) - > Version.from_str(features.DEPRECATION_INFO_BOUNDARY) + if schema_error.version == "devel" or should_log_deprecation( + schema_error.version ): + deprecations.append(SchemaProblem(path, schema_error.message)) + else: info_deprecations.append( SchemaProblem(path, schema_error.message) ) - else: - deprecations.append(SchemaProblem(path, schema_error.message)) else: errors.append(SchemaProblem(path, schema_error.message)) diff --git a/cloudinit/util.py b/cloudinit/util.py index 8cac7dc8b2d..4380cce3535 100644 --- a/cloudinit/util.py +++ b/cloudinit/util.py @@ -3210,6 +3210,18 @@ def _compare_version(self, other: "Version") -> int: return -1 +def should_log_deprecation(version: str) -> bool: + """Determine if a deprecation message should be logged. + + :param version: The version in which the thing was deprecated. + + :return: True if the message should be logged, else False. + """ + return features.DEPRECATION_INFO_BOUNDARY == "devel" or Version.from_str( + version + ) <= Version.from_str(features.DEPRECATION_INFO_BOUNDARY) + + def deprecate( *, deprecated: str, @@ -3251,10 +3263,7 @@ def deprecate( f"{deprecated_version} and scheduled to be removed in " f"{version_removed}. {message}" ).rstrip() - if ( - "devel" != features.DEPRECATION_INFO_BOUNDARY - and Version.from_str(features.DEPRECATION_INFO_BOUNDARY) < version - ): + if not should_log_deprecation(deprecated_version): level = logging.INFO elif hasattr(LOG, "deprecated"): level = log.DEPRECATED diff --git a/tests/unittests/distros/test_create_users.py b/tests/unittests/distros/test_create_users.py index 039723aaad2..86c79fb1775 100644 --- a/tests/unittests/distros/test_create_users.py +++ b/tests/unittests/distros/test_create_users.py @@ -5,6 +5,7 @@ import pytest from cloudinit import distros, ssh_util +from cloudinit.util import should_log_deprecation from tests.unittests.helpers import mock from tests.unittests.util import abstract_to_concrete @@ -142,7 +143,12 @@ def test_create_groups_with_dict_deprecated( ] assert m_subp.call_args_list == expected - assert caplog.records[0].levelname in ["WARNING", "DEPRECATED"] + expected_levels = ( + ["WARNING", "DEPRECATED"] + if should_log_deprecation("23.1") + else ["INFO"] + ) + assert caplog.records[0].levelname in expected_levels assert ( "The user foo_user has a 'groups' config value of type dict" in caplog.records[0].message @@ -170,7 +176,12 @@ def test_explicit_sudo_false(self, m_subp, dist, caplog): mock.call(["passwd", "-l", USER]), ] - assert caplog.records[1].levelname in ["WARNING", "DEPRECATED"] + expected_levels = ( + ["WARNING", "DEPRECATED"] + if should_log_deprecation("22.3") + else ["INFO"] + ) + assert caplog.records[1].levelname in expected_levels assert ( "The value of 'false' in user foo_user's 'sudo' " "config is deprecated in 22.3 and scheduled to be removed"