Files
computer-playbook/tests/unit/python/plugins/lookup/test_cpus.py
T
kevinveenbirkenbachandClaude Opus 5 a05071fe78 refactor(roles): let a role declare its entity name and drop the get_ prefix
The entity name keys a role's instance directory, its compose project and its container name, and was derived only by stripping the longest matching category prefix. Two roles under different categories whose names share a suffix therefore collapse onto one entity and overwrite each other's rendered stack, which no provides: can repair.

A literal entity_name in vars/main.yml now wins over the derivation. web-app-seaweedfs-console takes its shorter name back and declares the entity seaweedfs-console, which frees the seaweedfs key in its own services map for the engine consumer and removes the provides:, covers: and alias entries that stood in for it. A new lint pairs the declaration with the primary key it has to match, because a drift between them would drop the role from the provider set in silence.

Compared across all 288 roles, exactly one entity changes, and it restores what main already keeps on disk, so no instance directory moves.

The function and the filter lose their get_ prefix. Two lints read that prefix as the discriminator between the variable and the filter that fills it; both discriminate on the pipe now. Ansible's templar resolves a filter and a same-named variable independently, verified against ansible-core 2.20.2.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-10-03 19:40:53 +02:00

51 lines
1.7 KiB
Python

import unittest
from unittest.mock import patch
from ansible.errors import AnsibleError
from plugins.lookup.cpus import LookupModule
VARS = {"application_id": "web-app-docs"}
def _run(terms, configured, host_cpus=20):
with (
patch("plugins.lookup.cpus.ConfigLookup") as config,
patch("plugins.lookup.cpus.ResourceLookup") as resource,
):
config.return_value.run.return_value = [configured]
resource.return_value.run.return_value = [host_cpus]
result = LookupModule().run(terms, variables=VARS)
return result, config.return_value.run.call_args
class TestCpusLookup(unittest.TestCase):
def test_a_percentage_becomes_a_share_of_the_host(self):
result, _ = _run(["web-app-docs", "docs"], "95%")
self.assertEqual(result, [19.0])
def test_a_plain_value_passes_through(self):
result, _ = _run(["web-app-docs", "docs"], "2")
self.assertEqual(result, ["2"])
def test_the_service_name_builds_the_config_path(self):
_, call = _run(["web-app-hermes", "hermes"], "50%")
self.assertEqual(call.args[0], ["web-app-hermes", "services.hermes.cpus"])
@patch("plugins.lookup.cpus.entity_name", return_value="docs")
def test_the_entity_name_is_the_default_service(self, _entity):
_, call = _run(["web-app-docs"], "50%")
self.assertEqual(call.args[0], ["web-app-docs", "services.docs.cpus"])
def test_no_terms_is_refused(self):
with self.assertRaises(AnsibleError):
LookupModule().run([], variables=VARS)
def test_a_third_term_is_refused(self):
with self.assertRaises(AnsibleError):
LookupModule().run(["a", "b", "c"], variables=VARS)
if __name__ == "__main__":
unittest.main()