From 48506cfc5d7514a2b292cb42c6b15af83bd11dbc Mon Sep 17 00:00:00 2001 From: Alessandro De Maria Date: Sat, 31 Aug 2024 13:59:22 +0000 Subject: [PATCH] Fix 1210 and improves loading of inventory backends --- kapitan/cli.py | 4 +- kapitan/inventory/__init__.py | 58 +++++++++++++-- kapitan/resources.py | 9 +-- poetry.lock | 128 ++++++++++++++++++-------------- pyproject.toml | 3 +- tests/test_compile.py | 3 +- tests/test_compose_node_name.py | 8 +- tests/test_from_dot_kapitan.py | 5 +- tests/test_inventory.py | 15 ++-- tests/test_omegaconf.py | 4 +- 10 files changed, 153 insertions(+), 84 deletions(-) diff --git a/kapitan/cli.py b/kapitan/cli.py index 5cd5a7917..062a78c54 100644 --- a/kapitan/cli.py +++ b/kapitan/cli.py @@ -21,7 +21,7 @@ from kapitan import cached, defaults, setup_logging from kapitan.initialiser import initialise_skeleton from kapitan.inputs.jsonnet import jsonnet_file -from kapitan.inventory import AVAILABLE_BACKENDS +from kapitan.inventory import AVAILABLE_BACKENDS, InventoryBackends from kapitan.lint import start_lint from kapitan.refs.base import RefController, Revealer from kapitan.refs.cmd_parser import handle_refs_command @@ -108,7 +108,7 @@ def build_parser(): inventory_backend_parser.add_argument( "--inventory-backend", action="store", - default=from_dot_kapitan("inventory_backend", "inventory-backend", "reclass"), + default=from_dot_kapitan("inventory_backend", "inventory-backend", InventoryBackends.RECLASS), choices=AVAILABLE_BACKENDS.keys(), help="Select the inventory backend to use (default=reclass)", ) diff --git a/kapitan/inventory/__init__.py b/kapitan/inventory/__init__.py index a95912b07..be670c104 100644 --- a/kapitan/inventory/__init__.py +++ b/kapitan/inventory/__init__.py @@ -1,14 +1,58 @@ +from .inventory import Inventory + +try: + from enum import StrEnum +except ImportError: + from strenum import StrEnum + from typing import Type -from .inv_reclass import ReclassInventory -from .inv_reclass_rs import ReclassRsInventory -from .inv_omegaconf.inv_omegaconf import OmegaConfInventory -from .inventory import Inventory + +class InventoryBackends(StrEnum): + """ + Enumeration of available inventory backends. + """ + RECLASS = "reclass" + RECLASS_RS = "reclass-rs" + OMEGACONF = "omegaconf" + DEFAULT = RECLASS + + +def load_reclass_backend(): + """ + Enable the reclass inventory backend. + """ + from .inv_reclass import ReclassInventory + return ReclassInventory + + +def load_reclass_rs_backend(): + """ + Enable the reclass-rs inventory backend. + """ + from .inv_reclass_rs import ReclassRsInventory + return ReclassRsInventory + + +def load_omegaconf_backend(): + """ + Enable the omegaconf inventory backend. + """ + from .inv_omegaconf.inv_omegaconf import OmegaConfInventory + return OmegaConfInventory + # Dict mapping values for command line flag `--inventory-backend` to the # associated `Inventory` subclass. AVAILABLE_BACKENDS: dict[str, Type[Inventory]] = { - "reclass": ReclassInventory, - "reclass-rs": ReclassRsInventory, - "omegaconf": OmegaConfInventory, + InventoryBackends.RECLASS: load_reclass_backend, + InventoryBackends.RECLASS_RS: load_reclass_rs_backend, + InventoryBackends.OMEGACONF: load_omegaconf_backend, } + + +def get_inventory_backend(backend_name: str) -> Type[Inventory]: + """ + Get the `Inventory` subclass associated with the given `backend_name`. + """ + return AVAILABLE_BACKENDS.get(backend_name, AVAILABLE_BACKENDS[InventoryBackends.DEFAULT])() \ No newline at end of file diff --git a/kapitan/resources.py b/kapitan/resources.py index 3bc358f16..dc02f04eb 100644 --- a/kapitan/resources.py +++ b/kapitan/resources.py @@ -24,7 +24,7 @@ from kapitan import __file__ as kapitan_install_path from kapitan.inputs.kadet import Dict from kapitan.errors import CompileError, InventoryError, KapitanError -from kapitan.inventory import Inventory, ReclassInventory, AVAILABLE_BACKENDS +from kapitan.inventory import Inventory, get_inventory_backend from kapitan.utils import PrettyDumper, deep_get, flatten_dict, render_jinja2_file, sha256_string logger = logging.getLogger(__name__) @@ -327,10 +327,9 @@ def get_inventory(inventory_path, ignore_class_not_found: bool = False) -> Inven # select inventory backend backend_id = hasattr(cached.args, "inventory_backend") and cached.args.inventory_backend compose_target_name = hasattr(cached.args, "compose_target_name") and cached.args.compose_target_name - backend = AVAILABLE_BACKENDS.get(backend_id, AVAILABLE_BACKENDS.get("reclass")) - inventory_backend: Inventory = None + backend = get_inventory_backend(backend_id) - logger.debug(f"Using {backend_id} as inventory backend") + logger.debug(f"Using {backend.__name__} as inventory backend") inventory_backend = backend(inventory_path=inventory_path, compose_target_name=compose_target_name, ignore_class_not_found=ignore_class_not_found) cached.inv = inventory_backend @@ -339,7 +338,7 @@ def get_inventory(inventory_path, ignore_class_not_found: bool = False) -> Inven # if we use forked processes, we need to load the inventory for kadet once # and pass it to the children, to avoid re-reading the inventory for each child # TODO(adenaria): Improve to only do it for kadet - if cached.args.mp_method != "spawn": + if hasattr(cached.args, "mp_method") and cached.args.mp_method != "spawn": cached.inventory_global_kadet = Dict(cached.global_inv) # migrate inventory to selected inventory backend diff --git a/poetry.lock b/poetry.lock index f8bb8efbd..0db858b91 100644 --- a/poetry.lock +++ b/poetry.lock @@ -97,17 +97,17 @@ typing-extensions = ">=4.0.1" [[package]] name = "boto3" -version = "1.35.0" +version = "1.35.10" description = "The AWS SDK for Python" optional = false python-versions = ">=3.8" files = [ - {file = "boto3-1.35.0-py3-none-any.whl", hash = "sha256:ada32dab854c46a877cf967b8a55ab1a7d356c3c87f1c8bd556d446ff03dfd95"}, - {file = "boto3-1.35.0.tar.gz", hash = "sha256:bdc242e3ea81decc6ea551b04b2c122f088c29269d8e093b55862946aa0fcfc6"}, + {file = "boto3-1.35.10-py3-none-any.whl", hash = "sha256:add26dd58e076dfd387013da4704716d5cff215cf14f6d4347c4b9b7fc1f0b8e"}, + {file = "boto3-1.35.10.tar.gz", hash = "sha256:189ab1e2b4cd86df56f82438d89b4040eb140c92683f1bda7cb2e62624f20ea5"}, ] [package.dependencies] -botocore = ">=1.35.0,<1.36.0" +botocore = ">=1.35.10,<1.36.0" jmespath = ">=0.7.1,<2.0.0" s3transfer = ">=0.10.0,<0.11.0" @@ -116,13 +116,13 @@ crt = ["botocore[crt] (>=1.21.0,<2.0a0)"] [[package]] name = "botocore" -version = "1.35.0" +version = "1.35.10" description = "Low-level, data-driven core of boto 3." optional = false python-versions = ">=3.8" files = [ - {file = "botocore-1.35.0-py3-none-any.whl", hash = "sha256:a3c96fe0b6afe7d00bad6ffbe73f2610953065fcdf0ed697eba4e1e5287cc84f"}, - {file = "botocore-1.35.0.tar.gz", hash = "sha256:6ab2f5a5cbdaa639599e3478c65462c6d6a10173dc8b941bfc69b0c9eb548f45"}, + {file = "botocore-1.35.10-py3-none-any.whl", hash = "sha256:0d96d023b9b0cea99a0a428a431d011329d3a958730aee6ed6a6fec5d9bfbc03"}, + {file = "botocore-1.35.10.tar.gz", hash = "sha256:6c8a1377b6636a0d80218115e1cd41bcceba0a2f050b79c206f4cf8d002c54d7"}, ] [package.dependencies] @@ -181,24 +181,24 @@ redis = ["redis (>=2.10.5)"] [[package]] name = "cachetools" -version = "5.4.0" +version = "5.5.0" description = "Extensible memoizing collections and decorators" optional = false python-versions = ">=3.7" files = [ - {file = "cachetools-5.4.0-py3-none-any.whl", hash = "sha256:3ae3b49a3d5e28a77a0be2b37dbcb89005058959cb2323858c2657c4a8cab474"}, - {file = "cachetools-5.4.0.tar.gz", hash = "sha256:b8adc2e7c07f105ced7bc56dbb6dfbe7c4a00acce20e2227b3f355be89bc6827"}, + {file = "cachetools-5.5.0-py3-none-any.whl", hash = "sha256:02134e8439cdc2ffb62023ce1debca2944c3f289d66bb17ead3ab3dede74b292"}, + {file = "cachetools-5.5.0.tar.gz", hash = "sha256:2cc24fb4cbe39633fb7badd9db9ca6295d766d9c2995f245725a46715d050f2a"}, ] [[package]] name = "certifi" -version = "2024.7.4" +version = "2024.8.30" description = "Python package for providing Mozilla's CA Bundle." optional = false python-versions = ">=3.6" files = [ - {file = "certifi-2024.7.4-py3-none-any.whl", hash = "sha256:c198e21b1289c2ab85ee4e67bb4b4ef3ead0892059901a8d5b622f24a1101e90"}, - {file = "certifi-2024.7.4.tar.gz", hash = "sha256:5a1e7645bc0ec61a09e26c36f6106dd4cf40c6db3a1fb6352b0244e7fb057c7b"}, + {file = "certifi-2024.8.30-py3-none-any.whl", hash = "sha256:922820b53db7a7257ffbda3f597266d435245903d80737e34f8a45ff3e3230d8"}, + {file = "certifi-2024.8.30.tar.gz", hash = "sha256:bec941d2aa8195e248a60b31ff9f0558284cf01a52591ceda73ea9afffd69fd9"}, ] [[package]] @@ -785,13 +785,13 @@ files = [ [[package]] name = "google-api-core" -version = "2.19.1" +version = "2.19.2" description = "Google API client core library" optional = false python-versions = ">=3.7" files = [ - {file = "google-api-core-2.19.1.tar.gz", hash = "sha256:f4695f1e3650b316a795108a76a1c416e6afb036199d1c1f1f110916df479ffd"}, - {file = "google_api_core-2.19.1-py3-none-any.whl", hash = "sha256:f12a9b8309b5e21d92483bbd47ce2c445861ec7d269ef6784ecc0ea8c1fa6125"}, + {file = "google_api_core-2.19.2-py3-none-any.whl", hash = "sha256:53ec0258f2837dd53bbd3d3df50f5359281b3cc13f800c941dd15a9b5a415af4"}, + {file = "google_api_core-2.19.2.tar.gz", hash = "sha256:ca07de7e8aa1c98a8bfca9321890ad2340ef7f2eb136e558cee68f24b94b0a8f"}, ] [package.dependencies] @@ -808,13 +808,13 @@ grpcio-gcp = ["grpcio-gcp (>=0.2.2,<1.0.dev0)"] [[package]] name = "google-api-python-client" -version = "2.141.0" +version = "2.143.0" description = "Google API Client Library for Python" optional = false python-versions = ">=3.7" files = [ - {file = "google_api_python_client-2.141.0-py2.py3-none-any.whl", hash = "sha256:43c05322b91791204465291b3852718fae38d4f84b411d8be847c4f86882652a"}, - {file = "google_api_python_client-2.141.0.tar.gz", hash = "sha256:0f225b1f45d5a6f8c2a400f48729f5d6da9a81138e81e0478d61fdd8edf6563a"}, + {file = "google_api_python_client-2.143.0-py2.py3-none-any.whl", hash = "sha256:d5654134522b9b574b82234e96f7e0aeeabcbf33643fbabcd449ef0068e3a476"}, + {file = "google_api_python_client-2.143.0.tar.gz", hash = "sha256:6a75441f9078e6e2fcdf4946a153fda1e2cc81b5e9c8d6e8c0750c85c7f8a566"}, ] [package.dependencies] @@ -826,13 +826,13 @@ uritemplate = ">=3.0.1,<5" [[package]] name = "google-auth" -version = "2.33.0" +version = "2.34.0" description = "Google Authentication Library" optional = false python-versions = ">=3.7" files = [ - {file = "google_auth-2.33.0-py2.py3-none-any.whl", hash = "sha256:8eff47d0d4a34ab6265c50a106a3362de6a9975bb08998700e389f857e4d39df"}, - {file = "google_auth-2.33.0.tar.gz", hash = "sha256:d6a52342160d7290e334b4d47ba390767e4438ad0d45b7630774533e82655b95"}, + {file = "google_auth-2.34.0-py2.py3-none-any.whl", hash = "sha256:72fd4733b80b6d777dcde515628a9eb4a577339437012874ea286bca7261ee65"}, + {file = "google_auth-2.34.0.tar.gz", hash = "sha256:8eb87396435c19b20d32abd2f984e31c191a15284af72eb922f10e5bde9c04cc"}, ] [package.dependencies] @@ -842,7 +842,7 @@ rsa = ">=3.1.4,<5" [package.extras] aiohttp = ["aiohttp (>=3.6.2,<4.0.0.dev0)", "requests (>=2.20.0,<3.0.0.dev0)"] -enterprise-cert = ["cryptography (==36.0.2)", "pyopenssl (==22.0.0)"] +enterprise-cert = ["cryptography", "pyopenssl"] pyopenssl = ["cryptography (>=38.0.3)", "pyopenssl (>=20.0.0)"] reauth = ["pyu2f (>=0.1.5)"] requests = ["requests (>=2.20.0,<3.0.0.dev0)"] @@ -864,13 +864,13 @@ httplib2 = ">=0.19.0" [[package]] name = "googleapis-common-protos" -version = "1.63.2" +version = "1.65.0" description = "Common protobufs used in Google APIs" optional = false python-versions = ">=3.7" files = [ - {file = "googleapis-common-protos-1.63.2.tar.gz", hash = "sha256:27c5abdffc4911f28101e635de1533fb4cfd2c37fbaa9174587c799fac90aa87"}, - {file = "googleapis_common_protos-1.63.2-py2.py3-none-any.whl", hash = "sha256:27a2499c7e8aff199665b22741997e485eccc8645aa9176c7c988e6fae507945"}, + {file = "googleapis_common_protos-1.65.0-py2.py3-none-any.whl", hash = "sha256:2972e6c496f435b92590fd54045060867f3fe9be2c82ab148fc8885035479a63"}, + {file = "googleapis_common_protos-1.65.0.tar.gz", hash = "sha256:334a29d07cddc3aa01dee4988f9afd9b2916ee2ff49d6b757155dc0d197852c0"}, ] [package.dependencies] @@ -912,24 +912,24 @@ parser = ["pyhcl (>=0.4.4,<0.5.0)"] [[package]] name = "idna" -version = "3.7" +version = "3.8" description = "Internationalized Domain Names in Applications (IDNA)" optional = false -python-versions = ">=3.5" +python-versions = ">=3.6" files = [ - {file = "idna-3.7-py3-none-any.whl", hash = "sha256:82fee1fc78add43492d3a1898bfa6d8a904cc97d8427f683ed8e798d07761aa0"}, - {file = "idna-3.7.tar.gz", hash = "sha256:028ff3aadf0609c1fd278d8ea3089299412a7a8b9bd005dd08b9f8285bcb5cfc"}, + {file = "idna-3.8-py3-none-any.whl", hash = "sha256:050b4e5baadcd44d760cedbd2b8e639f2ff89bbc7a5730fcc662954303377aac"}, + {file = "idna-3.8.tar.gz", hash = "sha256:d838c2c0ed6fced7693d5e8ab8e734d5f8fda53a039c0164afb0b82e771e3603"}, ] [[package]] name = "importlib-metadata" -version = "8.2.0" +version = "8.4.0" description = "Read metadata from Python packages" optional = false python-versions = ">=3.8" files = [ - {file = "importlib_metadata-8.2.0-py3-none-any.whl", hash = "sha256:11901fa0c2f97919b288679932bb64febaeacf289d18ac84dd68cb2e74213369"}, - {file = "importlib_metadata-8.2.0.tar.gz", hash = "sha256:72e8d4399996132204f9a16dcc751af254a48f8d1b20b9ff0f98d4a8f901e73d"}, + {file = "importlib_metadata-8.4.0-py3-none-any.whl", hash = "sha256:66f342cc6ac9818fc6ff340576acd24d65ba0b3efabb2b4ac08b598965a4a2f1"}, + {file = "importlib_metadata-8.4.0.tar.gz", hash = "sha256:9a547d3bc3608b025f93d403fdd1aae741c24fbb8314df4b155675742ce303c5"}, ] [package.dependencies] @@ -1514,22 +1514,22 @@ testing = ["google-api-core (>=1.31.5)"] [[package]] name = "protobuf" -version = "5.27.3" +version = "5.28.0" description = "" optional = false python-versions = ">=3.8" files = [ - {file = "protobuf-5.27.3-cp310-abi3-win32.whl", hash = "sha256:dcb307cd4ef8fec0cf52cb9105a03d06fbb5275ce6d84a6ae33bc6cf84e0a07b"}, - {file = "protobuf-5.27.3-cp310-abi3-win_amd64.whl", hash = "sha256:16ddf3f8c6c41e1e803da7abea17b1793a97ef079a912e42351eabb19b2cffe7"}, - {file = "protobuf-5.27.3-cp38-abi3-macosx_10_9_universal2.whl", hash = "sha256:68248c60d53f6168f565a8c76dc58ba4fa2ade31c2d1ebdae6d80f969cdc2d4f"}, - {file = "protobuf-5.27.3-cp38-abi3-manylinux2014_aarch64.whl", hash = "sha256:b8a994fb3d1c11156e7d1e427186662b64694a62b55936b2b9348f0a7c6625ce"}, - {file = "protobuf-5.27.3-cp38-abi3-manylinux2014_x86_64.whl", hash = "sha256:a55c48f2a2092d8e213bd143474df33a6ae751b781dd1d1f4d953c128a415b25"}, - {file = "protobuf-5.27.3-cp38-cp38-win32.whl", hash = "sha256:043853dcb55cc262bf2e116215ad43fa0859caab79bb0b2d31b708f128ece035"}, - {file = "protobuf-5.27.3-cp38-cp38-win_amd64.whl", hash = "sha256:c2a105c24f08b1e53d6c7ffe69cb09d0031512f0b72f812dd4005b8112dbe91e"}, - {file = "protobuf-5.27.3-cp39-cp39-win32.whl", hash = "sha256:c84eee2c71ed83704f1afbf1a85c3171eab0fd1ade3b399b3fad0884cbcca8bf"}, - {file = "protobuf-5.27.3-cp39-cp39-win_amd64.whl", hash = "sha256:af7c0b7cfbbb649ad26132e53faa348580f844d9ca46fd3ec7ca48a1ea5db8a1"}, - {file = "protobuf-5.27.3-py3-none-any.whl", hash = "sha256:8572c6533e544ebf6899c360e91d6bcbbee2549251643d32c52cf8a5de295ba5"}, - {file = "protobuf-5.27.3.tar.gz", hash = "sha256:82460903e640f2b7e34ee81a947fdaad89de796d324bcbc38ff5430bcdead82c"}, + {file = "protobuf-5.28.0-cp310-abi3-win32.whl", hash = "sha256:66c3edeedb774a3508ae70d87b3a19786445fe9a068dd3585e0cefa8a77b83d0"}, + {file = "protobuf-5.28.0-cp310-abi3-win_amd64.whl", hash = "sha256:6d7cc9e60f976cf3e873acb9a40fed04afb5d224608ed5c1a105db4a3f09c5b6"}, + {file = "protobuf-5.28.0-cp38-abi3-macosx_10_9_universal2.whl", hash = "sha256:532627e8fdd825cf8767a2d2b94d77e874d5ddb0adefb04b237f7cc296748681"}, + {file = "protobuf-5.28.0-cp38-abi3-manylinux2014_aarch64.whl", hash = "sha256:018db9056b9d75eb93d12a9d35120f97a84d9a919bcab11ed56ad2d399d6e8dd"}, + {file = "protobuf-5.28.0-cp38-abi3-manylinux2014_x86_64.whl", hash = "sha256:6206afcb2d90181ae8722798dcb56dc76675ab67458ac24c0dd7d75d632ac9bd"}, + {file = "protobuf-5.28.0-cp38-cp38-win32.whl", hash = "sha256:eef7a8a2f4318e2cb2dee8666d26e58eaf437c14788f3a2911d0c3da40405ae8"}, + {file = "protobuf-5.28.0-cp38-cp38-win_amd64.whl", hash = "sha256:d001a73c8bc2bf5b5c1360d59dd7573744e163b3607fa92788b7f3d5fefbd9a5"}, + {file = "protobuf-5.28.0-cp39-cp39-win32.whl", hash = "sha256:dde9fcaa24e7a9654f4baf2a55250b13a5ea701493d904c54069776b99a8216b"}, + {file = "protobuf-5.28.0-cp39-cp39-win_amd64.whl", hash = "sha256:853db610214e77ee817ecf0514e0d1d052dff7f63a0c157aa6eabae98db8a8de"}, + {file = "protobuf-5.28.0-py3-none-any.whl", hash = "sha256:510ed78cd0980f6d3218099e874714cdf0d8a95582e7b059b06cabad855ed0a0"}, + {file = "protobuf-5.28.0.tar.gz", hash = "sha256:dde74af0fa774fa98892209992295adbfb91da3fa98c8f67a88afe8f5a349add"}, ] [[package]] @@ -1721,13 +1721,13 @@ tests = ["coverage[toml] (==5.0.4)", "pytest (>=6.0.0,<7.0.0)"] [[package]] name = "pyparsing" -version = "3.1.2" +version = "3.1.4" description = "pyparsing module - Classes and methods to define and execute parsing grammars" optional = false python-versions = ">=3.6.8" files = [ - {file = "pyparsing-3.1.2-py3-none-any.whl", hash = "sha256:f9db75911801ed778fe61bb643079ff86601aca99fcae6345aa67292038fb742"}, - {file = "pyparsing-3.1.2.tar.gz", hash = "sha256:a1bac0ce561155ecc3ed78ca94d3c9378656ad4c94c1270de543f621420f94ad"}, + {file = "pyparsing-3.1.4-py3-none-any.whl", hash = "sha256:a6a7ee4235a3f944aa1fa2249307708f893fe5717dc603503c6c7969c070fb7c"}, + {file = "pyparsing-3.1.4.tar.gz", hash = "sha256:f86ec8d1a83f11977c9a6ea7598e8c27fc5cddfa5b07ea2241edbbde1d7bc032"}, ] [package.extras] @@ -2488,6 +2488,22 @@ files = [ {file = "smmap-5.0.1.tar.gz", hash = "sha256:dceeb6c0028fdb6734471eb07c0cd2aae706ccaecab45965ee83f11c8d3b1f62"}, ] +[[package]] +name = "strenum" +version = "0.4.15" +description = "An Enum that inherits from str." +optional = false +python-versions = "*" +files = [ + {file = "StrEnum-0.4.15-py3-none-any.whl", hash = "sha256:a30cda4af7cc6b5bf52c8055bc4bf4b2b6b14a93b574626da33df53cf7740659"}, + {file = "StrEnum-0.4.15.tar.gz", hash = "sha256:878fb5ab705442070e4dd1929bb5e2249511c0bcf2b0eeacf3bcd80875c82eff"}, +] + +[package.extras] +docs = ["myst-parser[linkify]", "sphinx", "sphinx-rtd-theme"] +release = ["twine"] +test = ["pylint", "pytest", "pytest-black", "pytest-cov", "pytest-pylint"] + [[package]] name = "toml" version = "0.10.2" @@ -2702,18 +2718,22 @@ dev = ["doc8", "flake8", "flake8-import-order", "rstcheck[sphinx]", "sphinx"] [[package]] name = "zipp" -version = "3.20.0" +version = "3.20.1" description = "Backport of pathlib-compatible object wrapper for zip files" optional = false python-versions = ">=3.8" files = [ - {file = "zipp-3.20.0-py3-none-any.whl", hash = "sha256:58da6168be89f0be59beb194da1250516fdaa062ccebd30127ac65d30045e10d"}, - {file = "zipp-3.20.0.tar.gz", hash = "sha256:0145e43d89664cfe1a2e533adc75adafed82fe2da404b4bbb6b026c0157bdb31"}, + {file = "zipp-3.20.1-py3-none-any.whl", hash = "sha256:9960cd8967c8f85a56f920d5d507274e74f9ff813a0ab8889a5b5be2daf44064"}, + {file = "zipp-3.20.1.tar.gz", hash = "sha256:c22b14cc4763c5a5b04134207736c107db42e9d3ef2d9779d465f5f1bcba572b"}, ] [package.extras] +check = ["pytest-checkdocs (>=2.4)", "pytest-ruff (>=0.2.1)"] +cover = ["pytest-cov"] doc = ["furo", "jaraco.packaging (>=9.3)", "jaraco.tidelift (>=1.4)", "rst.linker (>=1.9)", "sphinx (>=3.5)", "sphinx-lint"] -test = ["big-O", "importlib-resources", "jaraco.functools", "jaraco.itertools", "jaraco.test", "more-itertools", "pytest (>=6,!=8.1.*)", "pytest-checkdocs (>=2.4)", "pytest-cov", "pytest-enabler (>=2.2)", "pytest-ignore-flaky", "pytest-mypy", "pytest-ruff (>=0.2.1)"] +enabler = ["pytest-enabler (>=2.2)"] +test = ["big-O", "importlib-resources", "jaraco.functools", "jaraco.itertools", "jaraco.test", "more-itertools", "pytest (>=6,!=8.1.*)", "pytest-ignore-flaky"] +type = ["pytest-mypy"] [extras] gojsonnet = ["gojsonnet"] @@ -2722,5 +2742,5 @@ test = ["docker", "reclass-rs"] [metadata] lock-version = "2.0" -python-versions = "^3.10,<3.12" -content-hash = "9b633a77abd182a48677c02195dd64c555517309279efba92b8a97648a887597" +python-versions = "^3.10,<3.13" +content-hash = "08ad7a65e8df9d9171bbef522a6ff4c34291c03c5be8eefe33e62a26e999fd3d" diff --git a/pyproject.toml b/pyproject.toml index dfedb9bf2..cdd1c1655 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -45,7 +45,8 @@ pytest-emoji = "^0.2.0" source = "git-tag" [tool.poetry.dependencies] -python = "^3.10,<3.12" +python = "^3.10,<3.13" +strenum = { version = "^0.4.15", python = "^3.10" } addict = "^2.4.0" azure-keyvault-keys = "^4.7.0" boto3 = "^1.18.17" diff --git a/tests/test_compile.py b/tests/test_compile.py index 41734ca9d..c5f0ded85 100644 --- a/tests/test_compile.py +++ b/tests/test_compile.py @@ -20,6 +20,7 @@ from kapitan.cli import main from kapitan.utils import directory_hash from kapitan.cached import reset_cache +from kapitan.inventory import InventoryBackends TEST_PWD = os.getcwd() TEST_RESOURCES_PATH = os.path.join(os.getcwd(), "tests/test_resources") @@ -219,7 +220,7 @@ def tearDown(self): class CompileKubernetesTestReclassRs(CompileKubernetesTest): def setUp(self): super().setUp() - self.extraArgv = ["--inventory-backend=reclass-rs"] + self.extraArgv = [f"--inventory-backend={(InventoryBackends.RECLASS_RS)}"] @unittest.skip("Already tested") def test_compile_not_enough_args(self): diff --git a/tests/test_compose_node_name.py b/tests/test_compose_node_name.py index 63340aaaf..5f471d0d7 100644 --- a/tests/test_compose_node_name.py +++ b/tests/test_compose_node_name.py @@ -5,15 +5,13 @@ import tempfile import unittest -from kapitan.inventory import ReclassInventory -from kapitan.inventory import ReclassRsInventory +from kapitan.inventory import get_inventory_backend, InventoryBackends from kapitan.inventory.inventory import InventoryError -from kapitan import setup_logging class ReclassComposeNodeNameTest(unittest.TestCase): def setUp(self): - self.inventory = ReclassInventory + self.inventory = get_inventory_backend(InventoryBackends.RECLASS) def test_compose_target_name(self): inventory_path = "examples/kubernetes/inventory" @@ -70,4 +68,4 @@ def test_compose_target_name(self): class ReclassRsComposeNodeNameTest(ReclassComposeNodeNameTest): def setUp(self): - self.inventory = ReclassRsInventory + self.inventory = get_inventory_backend(InventoryBackends.RECLASS_RS) diff --git a/tests/test_from_dot_kapitan.py b/tests/test_from_dot_kapitan.py index b24e73366..22843f988 100644 --- a/tests/test_from_dot_kapitan.py +++ b/tests/test_from_dot_kapitan.py @@ -6,6 +6,7 @@ from kapitan.utils import from_dot_kapitan from kapitan.cached import reset_cache +from kapitan.inventory import InventoryBackends class FromDotKapitanTest(unittest.TestCase): @@ -25,13 +26,13 @@ def test_no_file(self): def test_no_option(self): self._setup_dot_kapitan( - {"global": {"inventory-backend": "reclass"}, "compile": {"inventory-path": "./path/to/inv"}} + {"global": {"inventory-backend": str(InventoryBackends.RECLASS)}, "compile": {"inventory-path": "./path/to/inv"}} ) assert from_dot_kapitan("inventory", "inventory-path", "./some/fallback") == "./some/fallback" def test_cmd_option(self): self._setup_dot_kapitan( - {"global": {"inventory-backend": "reclass"}, "compile": {"inventory-path": "./path/to/inv"}} + {"global": {"inventory-backend": str(InventoryBackends.RECLASS)}, "compile": {"inventory-path": "./path/to/inv"}} ) assert from_dot_kapitan("compile", "inventory-path", "./some/fallback") == "./path/to/inv" diff --git a/tests/test_inventory.py b/tests/test_inventory.py index e0f1e5cb6..2b1558f50 100644 --- a/tests/test_inventory.py +++ b/tests/test_inventory.py @@ -6,6 +6,8 @@ # SPDX-License-Identifier: Apache-2.0 "inventory tests" +from kapitan.resources import inventory +from kapitan.inventory import InventoryBackends import importlib import unittest @@ -15,7 +17,6 @@ import shutil logger = logging.getLogger(__name__) -from kapitan.resources import inventory TEST_PWD = os.getcwd() TEST_KUBERNETES_INVENTORY = os.path.join(TEST_PWD, "examples/kubernetes/") @@ -28,8 +29,10 @@ def setUp(self) -> None: from kapitan.cached import reset_cache, args reset_cache() - if not importlib.util.find_spec(self.backend_id): - self.skipTest(f"backend module {self.backend_id} not available") + # Fix inconsistency between reclass-rs (option name) and reclass_rs (module name) + backend_module_name = self.backend_id.replace("-", "_") + if not importlib.util.find_spec(backend_module_name): + self.skipTest(f"backend module {backend_module_name} not available") args.inventory_backend = self.backend_id def test_inventory_target(self): @@ -45,7 +48,7 @@ def test_inventory_all_targets(self): class InventoryTargetTestReclass(InventoryTargetTestBase): def setUp(self): - self.backend_id = "reclass" + self.backend_id = InventoryBackends.RECLASS self.expected_targets_count = 10 self.inventory_path = "examples/kubernetes/inventory" super().setUp() @@ -53,7 +56,7 @@ def setUp(self): class InventoryTargetTestReclassRs(InventoryTargetTestBase): def setUp(self): - self.backend_id = "reclass_rs" + self.backend_id = InventoryBackends.RECLASS_RS self.expected_targets_count = 10 self.inventory_path = "examples/kubernetes/inventory" super().setUp() @@ -64,7 +67,7 @@ class InventoryTargetTestOmegaConf(InventoryTargetTestBase): def setUp(self) -> None: shutil.copytree(TEST_KUBERNETES_INVENTORY, self.temp_dir, dirs_exist_ok=True) - self.backend_id = "omegaconf" + self.backend_id = InventoryBackends.OMEGACONF self.expected_targets_count = 10 from kapitan.inventory.inv_omegaconf import migrate self.inventory_path = os.path.join(self.temp_dir, "inventory") diff --git a/tests/test_omegaconf.py b/tests/test_omegaconf.py index 8c51f9fac..660972d6b 100644 --- a/tests/test_omegaconf.py +++ b/tests/test_omegaconf.py @@ -6,6 +6,7 @@ # SPDX-License-Identifier: Apache-2.0 "inventory tests" +from kapitan.inventory import get_inventory_backend import unittest import logging @@ -18,13 +19,14 @@ TEST_PWD = os.getcwd() TEST_KUBERNETES_INVENTORY = os.path.join(TEST_PWD, "examples/kubernetes/") -from kapitan.inventory.inv_omegaconf.inv_omegaconf import OmegaConfInventory as inventory_backend + class InventoryTestOmegaConf(unittest.TestCase): temp_dir = tempfile.mkdtemp() def setUp(self) -> None: shutil.copytree(TEST_KUBERNETES_INVENTORY, self.temp_dir, dirs_exist_ok=True) + inventory_backend = get_inventory_backend("omegaconf") self.inventory_path = self.temp_dir self.extraArgv = ["--inventory-backend=omegaconf"] from kapitan.inventory.inv_omegaconf import migrate