Skip to content

Commit

Permalink
tests: add unittests for features.DEPRECATION_INFO_BOUNDARY
Browse files Browse the repository at this point in the history
  • Loading branch information
blackboxsw committed Jun 27, 2024
1 parent 49a3e97 commit 01bc508
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 4 deletions.
38 changes: 35 additions & 3 deletions tests/unittests/config/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import pytest
import yaml

from cloudinit import features
from cloudinit.config.schema import (
VERSIONED_USERDATA_SCHEMA_FILE,
MetaSchema,
Expand Down Expand Up @@ -2757,10 +2758,11 @@ def test_handle_schema_unable_to_read_cfg_paths(
assert expected_log in caplog.text

@pytest.mark.parametrize(
"annotate, expected_output",
"annotate, deprecation_info_boundary, expected_output",
[
(
pytest.param(
True,
"devel",
dedent(
"""\
#cloud-config
Expand All @@ -2778,9 +2780,33 @@ def test_handle_schema_unable_to_read_cfg_paths(
Valid schema {cfg_file}
""" # noqa: E501
),
id="test_annotated_deprecation_info_boundary_devel_shows",
),
(
pytest.param(
True,
"22.1",
dedent(
"""\
#cloud-config
packages:
- htop
apt_update: true # D1
apt_upgrade: true # D2
apt_reboot_if_required: true # D3
# Deprecations: -------------
# D1: Default: ``false``. Deprecated in version 22.2. Use ``package_update`` instead.
# D2: Default: ``false``. Deprecated in version 22.2. Use ``package_upgrade`` instead.
# D3: Default: ``false``. Deprecated in version 22.2. Use ``package_reboot_if_required`` instead.
Valid schema {cfg_file}
""" # noqa: E501
),
id="test_annotated_deprecation_info_boundary_below_unredacted",
),
pytest.param(
False,
"18.2",
dedent(
"""\
Cloud config schema deprecations: \
Expand All @@ -2792,6 +2818,7 @@ def test_handle_schema_unable_to_read_cfg_paths(
Valid schema {cfg_file}
""" # noqa: E501
),
id="test_deprecation_info_boundary_does_unannotated_unredacted",
),
],
)
Expand All @@ -2800,11 +2827,13 @@ def test_handle_schema_args_annotate_deprecated_config(
self,
read_cfg_paths,
annotate,
deprecation_info_boundary,
expected_output,
paths,
caplog,
capsys,
tmpdir,
mocker,
):
paths.get_ipath = paths.get_ipath_cur
read_cfg_paths.return_value = paths
Expand All @@ -2822,6 +2851,9 @@ def test_handle_schema_args_annotate_deprecated_config(
"""
)
)
mocker.patch.object(
features, "DEPRECATION_INFO_BOUNDARY", deprecation_info_boundary
)
args = self.Args(
config_file=str(user_data_fn),
schema_type="cloud-config",
Expand Down
43 changes: 42 additions & 1 deletion tests/unittests/test_log.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# This file is part of cloud-init. See LICENSE file for license information.

"""Tests for cloudinit.log"""

import datetime
import io
import logging
import time

import pytest

from cloudinit import log, util
from cloudinit.analyze.dump import CLOUD_INIT_ASCTIME_FMT
from tests.unittests.helpers import CiTestCase
Expand Down Expand Up @@ -66,6 +67,46 @@ def test_deprecated_log_level(self, caplog):
assert "DEPRECATED" == caplog.records[0].levelname
assert "deprecated message" in caplog.text

@pytest.mark.parametrize(
"expected_log_level, deprecation_info_boundary",
(
pytest.param(
"DEPRECATED",
"19.2",
id="test_same_deprecation_info_boundary_is_deprecated_level",
),
pytest.param(
"INFO",
"19.1",
id="test_lower_deprecation_info_boundary_is_info_level",
),
),
)
def test_deprecate_log_level_based_on_features(
self, expected_log_level, deprecation_info_boundary, caplog, mocker
):
"""Deprecation log level is determined based features.
When DEPRECATION_INFO_BOUNDARY is set, and deprecated_version
parameter is provided with a version greater than the boundary
the log level if INFO instead of DEPRECATED.
"""
mocker.patch.object(
util.features,
"DEPRECATION_INFO_BOUNDARY",
deprecation_info_boundary,
)
util.deprecate(
deprecated="some key",
deprecated_version="19.2",
extra_message="dont use it",
)
assert expected_log_level == caplog.records[0].levelname
assert (
"some key is deprecated in 19.2 and scheduled to be removed in"
" 24.2" in caplog.text
)

def test_log_deduplication(self, caplog):
log.define_deprecation_logger()
util.deprecate(
Expand Down

0 comments on commit 01bc508

Please sign in to comment.