Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix failure when get_meta_doc() is called #5953

Merged
merged 2 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cloudinit/cmd/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ def _should_wait_via_user_data(
version="24.4",
requested_level=logging.WARNING,
msg="Unexpected failure parsing userdata: %s",
args=e,
args=(e,),
)
return True, "failed to parse user data as yaml"

Expand Down
20 changes: 20 additions & 0 deletions cloudinit/config/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -1457,6 +1457,26 @@ def handle_schema_args(name, args):
)


def get_meta_doc(*_args, **_kwargs) -> str:
"""Provide a stub for backwards compatibility.

This function is no longer used, but earlier versions of modules
required this function for documentation purposes. This is a stub so
that custom modules do not break on upgrade.
"""
lifecycle.log_with_downgradable_level(
logger=LOG,
version="24.4",
requested_level=logging.WARNING,
msg=(
"The 'get_meta_doc()' function is deprecated and will be removed "
"in a future version of cloud-init."
),
args=(),
)
return ""


def main():
"""Tool to validate schema of a cloud-config file or print schema docs."""
parser = get_parser()
Expand Down
6 changes: 3 additions & 3 deletions cloudinit/lifecycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def log_with_downgradable_level(
version: str,
requested_level: int,
msg: str,
args,
args: tuple,
):
"""Log a message at the requested level, if that is acceptable.
Expand All @@ -145,9 +145,9 @@ def log_with_downgradable_level(
:return: True if the message should be logged, else False.
"""
if should_log_deprecation(version, features.DEPRECATION_INFO_BOUNDARY):
logger.log(requested_level, msg, args)
logger.log(requested_level, msg, *args)
else:
logger.debug(msg, args)
logger.debug(msg, *args)


def deprecate(
Expand Down
32 changes: 32 additions & 0 deletions tests/unittests/cloudinit/test_lifecycle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import logging

import pytest

from cloudinit import lifecycle

LOG = logging.getLogger()


class TestLogWithDowngradableLevel:
@pytest.mark.parametrize(
"version,expected",
[
("9", logging.ERROR),
("11", logging.DEBUG),
],
)
def test_log_with_downgradable_level(
self, mocker, caplog, version, expected
):
mocker.patch("cloudinit.features.DEPRECATION_INFO_BOUNDARY", "10")
lifecycle.log_with_downgradable_level(
logger=LOG,
version=version,
requested_level=logging.ERROR,
msg="look at me %s %s!",
args=("one", "two"),
)
records = caplog.record_tuples
assert len(records) == 1
assert records[0][1] == expected
assert records[0][2] == "look at me one two!"
12 changes: 12 additions & 0 deletions tests/unittests/config/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
SchemaValidationError,
annotated_cloudconfig_file,
get_jsonschema_validator,
get_meta_doc,
get_schema,
get_schema_dir,
handle_schema_args,
Expand Down Expand Up @@ -2278,3 +2279,14 @@ def test_handle_schema_args_unknown_header(
assert read_cfg_paths.call_args_list == [
mock.call(fetch_existing_datasource="trust")
]


class TestDeprecation:
def test_get_meta_doc_deprecation(self, caplog):
"""Test that calling get_meta_doc() emits deprecation.

Ensures that custom modules calling `get_meta_doc()` can still
function but receive deprecation warning.
"""
get_meta_doc("some", "random", "arguments", plus="kwargs")
assert "The 'get_meta_doc()' function is deprecated" in caplog.text
Loading