sunbeam-charms/ops-sunbeam/tests/unit_tests/test_templating.py
Samuel Allan 365aed9a21
Remove pypi mock package for tests
'mock' is built in to python as unittest.mock since python 3.3.
The mock package on pypi is a backport, and not required here.

The test code throughout this repo used a mix of unittest.mock and
the mock package.  This makes it consistently unittest.mock now,
and makes the import styles consistent too.

Change-Id: I643247ac4061c095b327a73cba75a1fa67f1c32e
2024-02-21 09:04:17 +10:30

86 lines
2.6 KiB
Python

# Copyright 2021 Canonical Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Test ops_sunbeam.templating."""
import sys
from io import (
BytesIO,
TextIOWrapper,
)
from unittest.mock import (
MagicMock,
patch,
)
import jinja2
sys.path.append("lib") # noqa
sys.path.append("src") # noqa
import ops_sunbeam.core as sunbeam_core
import ops_sunbeam.templating as sunbeam_templating
import ops_sunbeam.test_utils as test_utils
class TestTemplating(test_utils.CharmTestCase):
"""Tests for ops_sunbeam.templating.."""
PATCHES = []
def setUp(self) -> None:
"""Charm test class setup."""
super().setUp(sunbeam_templating, self.PATCHES)
@patch("jinja2.FileSystemLoader")
def test_render(self, fs_loader: "jinja2.FileSystemLoader") -> None:
"""Check rendering templates."""
container_mock = MagicMock()
config = sunbeam_core.ContainerConfigFile(
"/tmp/testfile.txt", "myuser", "mygrp"
)
fs_loader.return_value = jinja2.DictLoader(
{"testfile.txt": "debug = {{ debug }}"}
)
sunbeam_templating.sidecar_config_render(
container_mock, config, "/tmp/templates", {"debug": True}
)
container_mock.push.assert_called_once_with(
"/tmp/testfile.txt",
"debug = True",
user="myuser",
group="mygrp",
permissions=None,
)
@patch("jinja2.FileSystemLoader")
def test_render_no_change(
self, fs_loader: "jinja2.FileSystemLoader"
) -> None:
"""Check rendering template with no content change."""
container_mock = MagicMock()
container_mock.pull.return_value = TextIOWrapper(
BytesIO(b"debug = True")
)
config = sunbeam_core.ContainerConfigFile(
"/tmp/testfile.txt", "myuser", "mygrp"
)
fs_loader.return_value = jinja2.DictLoader(
{"testfile.txt": "debug = {{ debug }}"}
)
sunbeam_templating.sidecar_config_render(
container_mock, config, "/tmp/templates", {"debug": True}
)
self.assertFalse(container_mock.push.called)