summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/cephadm/tests/test_template.py
blob: f67304348603c21e4f762ae5a828ca40d118b8ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import pathlib

import pytest

from cephadm.template import TemplateMgr, UndefinedError, TemplateNotFoundError


def test_render(cephadm_module, fs):
    template_base = (pathlib.Path(__file__).parent / '../templates').resolve()
    fake_template = template_base / 'foo/bar'
    fs.create_file(fake_template, contents='{{ cephadm_managed }}{{ var }}')

    template_mgr = TemplateMgr(cephadm_module)
    value = 'test'

    # with base context
    expected_text = '{}{}'.format(template_mgr.base_context['cephadm_managed'], value)
    assert template_mgr.render('foo/bar', {'var': value}) == expected_text

    # without base context
    with pytest.raises(UndefinedError):
        template_mgr.render('foo/bar', {'var': value}, managed_context=False)

    # override the base context
    context = {
        'cephadm_managed': 'abc',
        'var': value
    }
    assert template_mgr.render('foo/bar', context) == 'abc{}'.format(value)

    # template not found
    with pytest.raises(TemplateNotFoundError):
        template_mgr.render('foo/bar/2', {})