Skip to content

Commit

Permalink
test: Fix deprecation test failures (#5466)
Browse files Browse the repository at this point in the history
8906e17 is causing some test failures on older releases.
Fix them
  • Loading branch information
TheRealFalcon authored Jun 28, 2024
1 parent 5ce2ee3 commit 0a698a5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
14 changes: 6 additions & 8 deletions cloudinit/config/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand Down Expand Up @@ -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))

Expand Down
17 changes: 13 additions & 4 deletions cloudinit/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
15 changes: 13 additions & 2 deletions tests/unittests/distros/test_create_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 0a698a5

Please sign in to comment.