Files
computer-playbook/tests/unit/python/plugins/action/test_applications_carrier.py
T
kevinveenbirkenbachandClaude Opus 5.5 67f6c8d7e8 fix(cache): keep the applications carrier valid across token store writes
Since 778c18fb23 the carrier key holds the tokens file's mtime and size.
sys-token-store changed that stat on every inclusion (00_core.yml
touched the file) and on every persisted token, and nothing re-parked a
carrier afterwards. Every later task's fresh worker then rendered the
applications SPOT cold, 30 to 80 s each. On main fea6f31d9f (run
38812964308) seven heavy jobs logged 194 to 287 such tasks and ran out
of the 20400 s budget; the same jobs before 778c18fb23 logged 65 to 83,
all of them real waits.

- 00_core.yml touches the store with modification_time and access_time
  preserved, so it only creates a missing file. In swarm the controller
  sees the store over NFS with attribute caching, and the touch alone
  made the second pass go cold without any write.
- write.yml re-parks the carrier when the persist or the controller
  mirror changed the store. Both run once, so every host that ran the
  include gets the same verdict.
- tasks/utils/setup/role.yml re-parks after every role on every host,
  so hosts that did not run the writing role recover too. Where the
  key still matches this serves the carried payload; measured 0.1 s per
  role in a local compose deploy.
- The tokens file enters the key as the SHA-256 of its content,
  memoised per inode, mtime and size, so a touch no longer changes the
  key. A stat that still matches serves the memo (1.67 us against
  1.41 us for the old stat signature), and a stale stat keeps key and
  YAML cache on the same old content instead of pairing new content
  with an old key.
- applications_carrier opens the tokens file before it computes the
  key, so on the NFS-mounted controller the parked key reflects the
  revalidated attributes rather than a cached stat that changes a
  minute later.

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

156 lines
5.2 KiB
Python

import unittest
from unittest.mock import patch
from ansible.errors import AnsibleActionFail
from plugins.action.applications_carrier import ActionModule
from utils.cache.carrier import APPLICATIONS_RENDERED_FACT
class _FakeTask:
def __init__(self, args=None):
self.args = {} if args is None else dict(args)
class _FakeLookup:
def __init__(self, carrier):
self.carrier = carrier
self.calls = []
def run(self, terms, variables=None, **kwargs):
self.calls.append((terms, variables, kwargs))
return [self.carrier]
def _make_action(task: _FakeTask) -> ActionModule:
action = object.__new__(ActionModule)
action._task = task
action._loader = None
action._templar = None
return action
class TestApplicationsCarrierActionPlugin(unittest.TestCase):
def test_parks_the_carrier_as_a_masked_fact(self):
carrier = {"key": ["roles", ["a", "b", "c", "d"]], "applications": {"x": {}}}
lookup = _FakeLookup(carrier)
action = _make_action(_FakeTask())
with (
patch(
"plugins.action.applications_carrier.ActionBase.run",
autospec=True,
return_value={},
),
patch(
"plugins.action.applications_carrier.lookup_loader.get",
return_value=lookup,
),
):
result = action.run(task_vars={"applications": {}})
self.assertIs(result["ansible_facts"][APPLICATIONS_RENDERED_FACT], carrier)
self.assertTrue(result["_ansible_no_log"])
self.assertFalse(result["changed"])
terms, variables, kwargs = lookup.calls[0]
self.assertEqual(terms, [])
self.assertEqual(variables, {"applications": {}})
self.assertTrue(kwargs["carrier"])
def test_revalidates_the_tokens_file_before_rendering(self):
order = []
lookup = _FakeLookup({"key": ["roles", []], "applications": {}})
lookup.run = lambda *args, **kwargs: order.append("render") or [{}]
action = _make_action(_FakeTask())
with (
patch(
"plugins.action.applications_carrier.ActionBase.run",
autospec=True,
return_value={},
),
patch(
"plugins.action.applications_carrier.lookup_loader.get",
return_value=lookup,
),
patch(
"plugins.action.applications_carrier.revalidate_tokens_file",
side_effect=lambda variables: order.append(("revalidate", variables)),
),
):
action.run(task_vars={"FILE_TOKENS": "/x/tokens.yml"})
self.assertEqual(
order, [("revalidate", {"FILE_TOKENS": "/x/tokens.yml"}), "render"]
)
def test_clear_does_not_revalidate(self):
action = _make_action(_FakeTask(args={"clear": True}))
with (
patch(
"plugins.action.applications_carrier.ActionBase.run",
autospec=True,
return_value={},
),
patch(
"plugins.action.applications_carrier.revalidate_tokens_file"
) as revalidate,
):
action.run(task_vars={})
revalidate.assert_not_called()
def test_rejects_arguments(self):
action = _make_action(_FakeTask(args={"foo": 1}))
with (
patch(
"plugins.action.applications_carrier.ActionBase.run",
autospec=True,
return_value={},
),
self.assertRaises(AnsibleActionFail),
):
action.run(task_vars={})
def test_clear_drops_the_fact_without_rendering(self):
lookup = _FakeLookup({"key": ["roles", []], "applications": {}})
action = _make_action(_FakeTask(args={"clear": True}))
with (
patch(
"plugins.action.applications_carrier.ActionBase.run",
autospec=True,
return_value={},
),
patch(
"plugins.action.applications_carrier.lookup_loader.get",
return_value=lookup,
),
):
result = action.run(task_vars={"applications": {}})
self.assertIsNone(result["ansible_facts"][APPLICATIONS_RENDERED_FACT])
self.assertFalse(result["changed"])
self.assertEqual(lookup.calls, [])
def test_clear_false_still_renders(self):
carrier = {"key": ["roles", ["a", "b", "c", "d"]], "applications": {"x": {}}}
lookup = _FakeLookup(carrier)
action = _make_action(_FakeTask(args={"clear": "no"}))
with (
patch(
"plugins.action.applications_carrier.ActionBase.run",
autospec=True,
return_value={},
),
patch(
"plugins.action.applications_carrier.lookup_loader.get",
return_value=lookup,
),
):
result = action.run(task_vars={"applications": {}})
self.assertIs(result["ansible_facts"][APPLICATIONS_RENDERED_FACT], carrier)
self.assertEqual(len(lookup.calls), 1)
if __name__ == "__main__":
unittest.main()