Skip to content

Commit

Permalink
Refactor and format renderer specs
Browse files Browse the repository at this point in the history
* Format with black
* Expose simpler `render` helper function
  • Loading branch information
jgraichen committed Feb 9, 2021
1 parent e6e873b commit a40db01
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 170 deletions.
38 changes: 25 additions & 13 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
except ImportError:
from salt.utils import fopen

ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))

__opts__ = salt.config.client_config(os.path.join(ROOT, 'test/master.yml'))
__opts__['cachedir'] = os.path.join(ROOT, 'tmp/cache')
__opts__ = salt.config.client_config(os.path.join(ROOT, "test/master.yml"))
__opts__["cachedir"] = os.path.join(ROOT, "tmp/cache")

__grains__ = salt.loader.grains(__opts__)
__opts__['grains'] = __grains__
__opts__["grains"] = __grains__
__utils__ = salt.loader.utils(__opts__)
__salt__ = salt.loader.minion_mods(__opts__, utils=__utils__)

Expand All @@ -46,6 +46,17 @@ def env(tmpdir):
return Environment(tmpdir)


@pytest.fixture
def render(env):
def _render(input_data, **kwargs):
input_data = textwrap.dedent(input_data).lstrip()

env.write('unnamed.sls', input_data)
return env.compile_template('unnamed.sls', **kwargs)

return _render


class Environment(object):
def __init__(self, tmpdir):
self.tmpdir = tmpdir
Expand All @@ -54,7 +65,7 @@ def setup(self, files):
for k, v in files.items():
self.write(k, textwrap.dedent(v).strip())

def write(self, name, content, mode='w'):
def write(self, name, content, mode="w"):
template = os.path.join(self.tmpdir, name)
dirname = os.path.dirname(template)

Expand All @@ -64,7 +75,7 @@ def write(self, name, content, mode='w'):
with fopen(template, mode) as f:
f.write(content)

def compile_template(self, template, default='yaml|jinja', **kwargs):
def compile_template(self, template, default="yaml|jinja", **kwargs):
template = os.path.join(self.tmpdir, template)

return salt.template.compile_template(
Expand All @@ -73,15 +84,16 @@ def compile_template(self, template, default='yaml|jinja', **kwargs):
default=default,
blacklist=None,
whitelist=None,
**kwargs)
**kwargs,
)

def ext_pillar(self, **kwargs):
minion_id = kwargs.pop('minion_id', __opts__['id'])
pillar = kwargs.pop('pillar', {})
args = kwargs.pop('args', [os.path.join(self.tmpdir, 'tower.sls')])
minion_id = kwargs.pop("minion_id", __opts__["id"])
pillar = kwargs.pop("pillar", {})
args = kwargs.pop("args", [os.path.join(self.tmpdir, "tower.sls")])

if isinstance(args, dict):
return __pillars__['tower'](minion_id, pillar, **args)
return __pillars__["tower"](minion_id, pillar, **args)
if isinstance(args, list):
return __pillars__['tower'](minion_id, pillar, *args)
return __pillars__['tower'](minion_id, pillar, args)
return __pillars__["tower"](minion_id, pillar, *args)
return __pillars__["tower"](minion_id, pillar, args)
3 changes: 3 additions & 0 deletions test/master.yml
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
id: test_master

features:
enable_slsvars_fixes: True
6 changes: 3 additions & 3 deletions test/pillar/test_tower.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# -*- coding: utf-8 -*-
# pylint: disable=missing-docstring
# pylint: disable=redefined-outer-name

import os
import pytest

from test.conftest import __opts__

from salt.exceptions import SaltRenderError


Expand Down Expand Up @@ -38,7 +38,7 @@ def test_jinja(env):
'''
})

assert env.ext_pillar() == {'minion': {'id': __opts__['id']}}
assert env.ext_pillar() == {'minion': {'id': 'test_master'}}


def test_match_minion_id(env):
Expand Down
40 changes: 13 additions & 27 deletions test/renderers/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,32 @@
# pylint: disable=missing-docstring
# pylint: disable=redefined-outer-name

import textwrap

import pytest
def test_render(render):
template = """
A small text
"""

assert render(template, default="text") == "A small text\n"

@pytest.fixture
def result(env):
def fn():
return env.compile_template('template.sls', default='text')

return fn


def test_render(env, result):
env.write('template.sls', 'A small text')

assert result() == 'A small text'


def test_strip(env, result):
env.write('template.sls', textwrap.dedent(
'''
def test_strip(render):
template = """
#!text strip
Indented text
END
'''
).lstrip())
"""

assert result() == 'Indented text\n\nEND'
assert render(template) == "Indented text\n\nEND"


def test_key(env, result):
env.write('template.sls', textwrap.dedent(
'''
def test_key(render):
template = """
#!text key=a:b:c
text value
'''
).strip())
"""

assert result() == {'a': {'b': {'c': 'text value'}}}
assert render(template) == {"a": {"b": {"c": "text value\n"}}}
Loading

0 comments on commit a40db01

Please sign in to comment.