Skip to content
This repository has been archived by the owner on Aug 2, 2024. It is now read-only.

Fix sdb paths so that Salt loader picks them up properly #19

Merged
merged 10 commits into from
Nov 18, 2022
28 changes: 23 additions & 5 deletions src/saltext/cli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
from saltext.cli import PACKAGE_ROOT
from saltext.cli.templates import LOADER_MODULE_INTEGRATION_TEST_TEMPLATE
from saltext.cli.templates import LOADER_MODULE_UNIT_TEST_TEMPLATE
from saltext.cli.templates import LOADER_SDB_UNIT_TEST_TEMPLATE
from saltext.cli.templates import LOADER_STATE_UNIT_TEST_TEMPLATE
from saltext.cli.templates import LOADER_TEMPLATE
from saltext.cli.templates import LOADER_UNIT_TEST_TEMPLATE
from saltext.cli.templates import LOADERS_TEMPLATE
from saltext.cli.templates import MODULE_LOADER_TEMPLATE
from saltext.cli.templates import PACKAGE_INIT
from saltext.cli.templates import SDB_LOADER_TEMPLATE
from saltext.cli.templates import STATE_LOADER_TEMPLATE

LICENSES: Dict[str, str] = {
Expand Down Expand Up @@ -250,7 +252,11 @@ def main(
loaders_integration_tests_path = destdir / "tests" / "integration"
for loader_name in loader:
templating_context["loader"] = loader_name
loader_dir = loaders_package_path / (loader_name.rstrip("s") + "s")
loader_dir = None
if loader_name in ["sdb"]:
loader_dir = loaders_package_path / loader_name.rstrip("s")
else:
loader_dir = loaders_package_path / (loader_name.rstrip("s") + "s")
loader_dir.mkdir(0o755)
loader_dir_init = loader_dir / "__init__.py"
if not loader_dir_init.exists():
Expand All @@ -259,6 +265,8 @@ def main(
loader_template = MODULE_LOADER_TEMPLATE
elif loader_name == "states":
loader_template = STATE_LOADER_TEMPLATE
elif loader_name == "sdb":
loader_template = SDB_LOADER_TEMPLATE
else:
loader_template = LOADER_TEMPLATE
loader_module_contents = Template(loader_template).render(**templating_context)
Expand All @@ -267,7 +275,11 @@ def main(
loader_dir_module = loader_dir_module.with_suffix(".new")
loader_dir_module.write_text(loader_module_contents.rstrip() + "\n")

loader_unit_tests_dir = loaders_unit_tests_path / (loader_name.rstrip("s") + "s")
loader_unit_tests_dir = None
if loader_name in ["sdb"]:
loader_unit_tests_dir = loaders_unit_tests_path / loader_name.rstrip("s")
else:
loader_unit_tests_dir = loaders_unit_tests_path / (loader_name.rstrip("s") + "s")
loader_unit_tests_dir.mkdir(0o755, exist_ok=True)
loader_unit_tests_dir_init = loader_unit_tests_dir / "__init__.py"
if not loader_unit_tests_dir_init.exists():
Expand All @@ -276,6 +288,8 @@ def main(
loader_test_template = LOADER_MODULE_UNIT_TEST_TEMPLATE
elif loader_name == "states":
loader_test_template = LOADER_STATE_UNIT_TEST_TEMPLATE
elif loader_name == "sdb":
loader_test_template = LOADER_SDB_UNIT_TEST_TEMPLATE
else:
loader_test_template = LOADER_UNIT_TEST_TEMPLATE
loader_unit_test_contents = Template(loader_test_template).render(**templating_context)
Expand All @@ -284,9 +298,13 @@ def main(
loader_unit_test_module = loader_unit_test_module.with_suffix(".new")
loader_unit_test_module.write_text(loader_unit_test_contents.rstrip() + "\n")

loader_integration_tests_dir = loaders_integration_tests_path / (
loader_name.rstrip("s") + "s"
)
loader_integration_tests_dir = None
if loader_name in ["sdb"]:
loader_integration_tests_dir = loaders_integration_tests_path / loader_name.rstrip("s")
else:
loader_integration_tests_dir = loaders_integration_tests_path / (
loader_name.rstrip("s") + "s"
)
loader_integration_tests_dir.mkdir(0o755, exist_ok=True)
loader_integration_tests_dir_init = loader_integration_tests_dir / "__init__.py"
if not loader_integration_tests_dir_init.exists():
Expand Down
56 changes: 56 additions & 0 deletions src/saltext/cli/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,41 @@ def exampled(name):
return ret
'''

SDB_LOADER_TEMPLATE = '''\
{%- set loader_name = loader.rstrip("s") %}
"""
Salt {{ loader_name }} module
"""
import logging

log = logging.getLogger(__name__)

__virtualname__ = "{{ package_name }}"


def __virtual__():
# To force a module not to load return something like:
# return (False, "The {{ project_name }} {{ loader_name }} module is not implemented yet")

# Replace this with your own logic
if "{{package_name}}.example_function" not in __salt__:
return False, "The '{{package_name}}' execution module is not available"
return __virtualname__


def get(key, profile=None):
"""
This example function should be replaced

CLI Example:

.. code-block:: bash

salt '*' sdb.get "sdb://{{ package_name}}/foo"
"""
return key
'''

LOADER_MODULE_UNIT_TEST_TEMPLATE = """\
import pytest
import salt.modules.test as testmod
Expand Down Expand Up @@ -189,6 +224,27 @@ def test_replace_this_this_with_something_meaningful():
assert {{ package_name }}_state.exampled(echo_str) == expected
"""

LOADER_SDB_UNIT_TEST_TEMPLATE = """\
{%- set loader_name = loader.rstrip("s") %}
import pytest
import {{ package_namespace_pkg }}{{ package_name }}.{{ loader.rstrip("s") }}.{{ package_name }}_mod as {{ package_name }}_{{ loader_name }}


@pytest.fixture
def configure_loader_modules():
module_globals = {
"__salt__": {"this_does_not_exist.please_replace_it": lambda: True},
}
return {
{{ package_name }}_{{ loader_name }}: module_globals,
}


def test_replace_this_this_with_something_meaningful():
assert "this_does_not_exist.please_replace_it" in {{ package_name }}_{{ loader_name }}.__salt__
assert {{ package_name }}_{{ loader_name }}.__salt__["this_does_not_exist.please_replace_it"]() is True
"""

LOADER_UNIT_TEST_TEMPLATE = """\
{%- set loader_name = loader.rstrip("s") %}
import pytest
Expand Down