Files
computer-playbook/tests/unit/python/plugins/action/test_stack_host_contents.py
T
kevinveenbirkenbachandClaude Opus 5.5 cfb2f9c70b perf(web-opt-rdr-domains): check every certificate in one task and write every file in one call
Each redirect source went through the full TLS layer on its own: cert check,
CA environment cleanup, re-check, HTTP handler and vhost template, about
nine seconds per source. echoserver has 85 sources, so the role took
thirteen minutes of every deploy.

The role now resolves the tls state and renders both files of every source
on the controller, finds the certificates of all sources in one find, and
includes sys-front-tls only for a source whose certificate is missing, so
issuance still works in every mode. library/stack_host_contents.py writes
all files in one module call, atomically and only where the content
differs, and keeps one file per source under http/ and https/. Its action
plugin carries the IS_STACK_HOST gate like stack_host_template.

The name differs from the stack_host_files module of feature/store-i18n on
purpose: that one syncs a single directory of YAML documents and prunes
what it does not write, which would delete every other vhost here.

HTTPS_REDIRECT_ENABLED is gone: nothing set it, it was true everywhere,
and the redirect role could no longer see the sys-front-tls default it
lived in. Whether a domain gets its HTTP to HTTPS handler now follows from
lookup('tls', domain, 'enabled') alone, in sys-front-tls and here.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-23 16:35:45 +02:00

63 lines
1.9 KiB
Python

import unittest
from unittest.mock import MagicMock, patch
from plugins.action.stack_host_contents import ActionModule
class _FakeTask:
def __init__(self, *, args=None):
self.args = {} if args is None else dict(args)
class _FakeTemplar:
def __init__(self, *, is_stack_host=True):
self._is_stack_host = is_stack_host
self.available_variables: dict[str, object] = {}
def template(self, raw):
return self._is_stack_host
_ARGS = {"files": [{"path": "/etc/x.conf", "content": "x"}], "mode": "0644"}
def _make_action(task, templar):
action = object.__new__(ActionModule)
action._task = task
action._templar = templar
action._execute_module = MagicMock(
return_value={"changed": True, "changed_paths": ["/etc/x.conf"]}
)
return action
class TestStackHostContents(unittest.TestCase):
def test_skips_silently_on_non_stack_host(self):
action = _make_action(_FakeTask(args=_ARGS), _FakeTemplar(is_stack_host=False))
result = action.run(task_vars={})
self.assertTrue(result["skipped"])
self.assertFalse(result["changed"])
self.assertIn("IS_STACK_HOST", result["skip_reason"])
action._execute_module.assert_not_called()
def test_writes_every_file_in_one_module_call_on_stack_host(self):
action = _make_action(_FakeTask(args=_ARGS), _FakeTemplar(is_stack_host=True))
with patch(
"plugins.action.stack_host_contents.ActionBase.run", return_value={}
):
result = action.run(task_vars={"IS_STACK_HOST": True})
action._execute_module.assert_called_once_with(
module_name="stack_host_contents",
module_args=_ARGS,
task_vars={"IS_STACK_HOST": True},
)
self.assertEqual(result["changed_paths"], ["/etc/x.conf"])
if __name__ == "__main__":
unittest.main()