From ffd49b3fd5d631eb74f4ba67845e779f82166eb5 Mon Sep 17 00:00:00 2001 From: James Falcon Date: Wed, 19 Feb 2025 16:51:44 -0600 Subject: [PATCH] test: Ensure pre-24.2 custom modules work (#6034) --- .../assets/dropins/cc_custom_module_24_1.py | 42 +++++++++++++++++++ .../dropins/test_custom_modules.py | 28 +++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 tests/integration_tests/assets/dropins/cc_custom_module_24_1.py create mode 100644 tests/integration_tests/dropins/test_custom_modules.py diff --git a/tests/integration_tests/assets/dropins/cc_custom_module_24_1.py b/tests/integration_tests/assets/dropins/cc_custom_module_24_1.py new file mode 100644 index 00000000000..7eda1c0b30a --- /dev/null +++ b/tests/integration_tests/assets/dropins/cc_custom_module_24_1.py @@ -0,0 +1,42 @@ +# This file is part of cloud-init. See LICENSE file for license information. +"""This was the canonical example module from 24.1 + +Ensure cloud-init still supports it.""" + +import logging + +from cloudinit.cloud import Cloud +from cloudinit.config import Config +from cloudinit.config.schema import MetaSchema, get_meta_doc +from cloudinit.distros import ALL_DISTROS +from cloudinit.settings import PER_INSTANCE + +MODULE_DESCRIPTION = """\ +Description that will be used in module documentation. + +This will likely take multiple lines. +""" + +LOG = logging.getLogger(__name__) + +meta: MetaSchema = { + "id": "cc_example", + "name": "Example Module", + "title": "Shows how to create a module", + "description": MODULE_DESCRIPTION, + "distros": [ALL_DISTROS], + "frequency": PER_INSTANCE, + "activate_by_schema_keys": ["example_key, example_other_key"], + "examples": [ + "example_key: example_value", + "example_other_key: ['value', 2]", + ], +} # type: ignore + +__doc__ = get_meta_doc(meta) + + +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: + LOG.debug(f"Hi from module {name}") # noqa: G004 + # Add one more line for easier testing + print("Hello from module") diff --git a/tests/integration_tests/dropins/test_custom_modules.py b/tests/integration_tests/dropins/test_custom_modules.py new file mode 100644 index 00000000000..a9a6786689f --- /dev/null +++ b/tests/integration_tests/dropins/test_custom_modules.py @@ -0,0 +1,28 @@ +# This file is part of cloud-init. See LICENSE file for license information. + +import pytest + +from tests.integration_tests import releases +from tests.integration_tests.instances import IntegrationInstance +from tests.integration_tests.releases import IS_UBUNTU +from tests.integration_tests.util import ASSETS_DIR + + +@pytest.mark.skipif( + not IS_UBUNTU, reason="module dir tested is ubuntu-specific" +) +def test_custom_module_24_1(client: IntegrationInstance): + """Ensure that modifications to cloud-init don't break old custom modules. + + 24.1 had documentation that differs from current best practices. We want + to ensure modules created from this documentation still work: + https://docs.cloud-init.io/en/24.1/development/module_creation.html + """ + client.push_file( + ASSETS_DIR / "dropins/cc_custom_module_24_1.py", + "/usr/lib/python3/dist-packages/cloudinit/config/cc_custom_module_24_1.py", + ) + output = client.execute("cloud-init single --name cc_custom_module_24_1") + if releases.CURRENT_RELEASE >= releases.PLUCKY: + assert "The 'get_meta_doc()' function is deprecated" in output + assert "Hello from module" in output