Skip to content

Commit

Permalink
Add more test coverage and fix previous failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Megan Wilhite committed Apr 7, 2023
1 parent 17dcb42 commit e29485e
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 11 deletions.
1 change: 0 additions & 1 deletion salt/modules/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ def _load(formula):
"""

# Compute possibilities
_mk_client()
paths = []
for ext in ("yaml", "json"):
source_url = salt.utils.url.create(formula + "/defaults." + ext)
Expand Down
7 changes: 3 additions & 4 deletions salt/renderers/pyobjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,9 +430,6 @@ def render(template, saltenv="base", sls="", salt_data=True, **kwargs):
if not salt_data:
return _globals

# this will be used to fetch any import files
client = get_file_client(__opts__)

# process our sls imports
#
# we allow pyobjects users to use a special form of the import statement
Expand Down Expand Up @@ -461,7 +458,9 @@ def process_template(template):
# that we're importing everything
imports = None

with client:
# this will be used to fetch any import files
# For example salt://test.sls
with get_file_client(__opts__) as client:
state_file = client.cache_file(import_file, saltenv)
if not state_file:
raise ImportError(
Expand Down
3 changes: 1 addition & 2 deletions salt/utils/extmods.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ def sync(opts, form, saltenv=None, extmod_whitelist=None, extmod_blacklist=None)
"Cannot create cache module directory %s. Check permissions.",
mod_dir,
)
fileclient = salt.fileclient.get_file_client(opts)
with fileclient:
with salt.fileclient.get_file_client(opts) as fileclient:
for sub_env in saltenv:
log.info("Syncing %s for environment '%s'", form, sub_env)
cache = []
Expand Down
3 changes: 1 addition & 2 deletions salt/utils/jinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ def cache_file(self, template):
"""
saltpath = salt.utils.url.create(template)
fcl = self.file_client()
with fcl:
return fcl.get_file(saltpath, "", True, self.saltenv)
return fcl.get_file(saltpath, "", True, self.saltenv)

def check_cache(self, template):
"""
Expand Down
5 changes: 3 additions & 2 deletions salt/utils/mako.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,6 @@ def get_template(self, uri, relativeto=None):

def cache_file(self, fpath):
if fpath not in self.cache:
with self.file_client() as client:
self.cache[fpath] = client.get_file(fpath, "", True, self.saltenv)
self.cache[fpath] = self.file_client().get_file(
fpath, "", True, self.saltenv
)
8 changes: 8 additions & 0 deletions salt/utils/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,10 @@ def opt_jinja_env_helper(opts, optname):
raise SaltRenderError(
"Jinja error: {}{}".format(exc, out), line, tmplstr, trace=tracestr
)
finally:
if loader and hasattr(loader, "_file_client"):
if hasattr(loader._file_client, "destroy"):
loader._file_client.destroy()

# Workaround a bug in Jinja that removes the final newline
# (https://github.com/mitsuhiko/jinja2/issues/75)
Expand Down Expand Up @@ -564,6 +568,10 @@ def render_mako_tmpl(tmplstr, context, tmplpath=None):
).render(**context)
except Exception: # pylint: disable=broad-except
raise SaltRenderError(mako.exceptions.text_error_template().render())
finally:
if lookup and hasattr(lookup, "_file_client"):
if hasattr(lookup._file_client, "destroy"):
lookup._file_client.destroy()


def render_wempy_tmpl(tmplstr, context, tmplpath=None):
Expand Down
23 changes: 23 additions & 0 deletions tests/pytests/functional/modules/state/test_mako_renderer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import pytest

pytestmark = [
pytest.mark.windows_whitelisted,
]


def test_mako_renderer(state, state_tree):
"""
Test mako renderer when running state.sls
"""
sls_contents = """
#!mako|yaml
%for a in [1,2,3]:
echo ${a}:
cmd.run
%endfor
"""
with pytest.helpers.temp_file("issue-55124.sls", sls_contents, state_tree):
ret = state.sls("issue-55124")
for state_return in ret:
assert state_return.result is True
assert "echo" in state_return.id
32 changes: 32 additions & 0 deletions tests/pytests/functional/modules/state/test_pyobjects_renderer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import pytest

pytestmark = [
pytest.mark.windows_whitelisted,
]


def test_pyobjects_renderer(state, state_tree, tmp_path):
"""
Test pyobjects renderer when running state.sls
"""
sls1_contents = f"""
#!pyobjects
import pathlib
import salt://test_pyobjects2.sls
test_file = pathlib.Path("{str(tmp_path)}", "test")
File.managed(str(test_file), user='root', group='root', mode='1777')
"""
sls2_contents = f"""
#!pyobjects
import pathlib
test_file = pathlib.Path("{str(tmp_path)}", "test2")
File.managed(str(test_file), user='root', group='root', mode='1777')
"""

with pytest.helpers.temp_file("test_pyobjects.sls", sls1_contents, state_tree):
with pytest.helpers.temp_file("test_pyobjects2.sls", sls2_contents, state_tree):
ret = state.sls("test_pyobjects")
assert not ret.errors
for state_return in ret:
assert state_return.result is True
assert str(tmp_path) in state_return.name
40 changes: 40 additions & 0 deletions tests/pytests/functional/modules/test_defaults.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import pytest

pytestmark = [pytest.mark.skip_unless_on_linux]


@pytest.fixture(scope="module")
def defaults(modules):
return modules.defaults


def test_defaults_get(defaults, state_tree, caplog):
"""
test defaults.get
"""

json_contents = """
{
"users": {
"root": 1
}
}
"""
path = state_tree / "core"
with pytest.helpers.temp_file("defaults.json", json_contents, path):
assert defaults.get("core:users:root") == 1


def test_defaults_merge(defaults):
"""
test defaults.merge
"""
assert defaults.merge({"a": "b"}, {"d": "e"}) == {"a": "b", "d": "e"}


def test_defaults_deepcopy(defaults):
"""
test defaults.deepcopy
"""
test_dict = {"1": "one"}
assert defaults.deepcopy(test_dict) == test_dict

0 comments on commit e29485e

Please sign in to comment.