mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-29 06:17:25 +00:00
refactor(test-e2e-cli): guard the CLI runner's tasks with one block each
run_one.yml repeated `when: _cli_runner == inventory_hostname` six times, in two runs of sibling tasks, which the when-duplication and repeated-when lints reject. The three staging tasks and the render-and-run pair now sit in one block each under that guard; the task bodies, delegate_to, register and throttle are unchanged. The unit tests of the file credit a task with its enclosing blocks' when and find the shell task inside a block. 🎯 Fix: 90 % 💥 Break: 5 % 💸 Tokens: 100 % ⏱️ Velocity: 100 % 🛡️ Security: 100 % 🏆 Score: 86 🚦 Gate: ⏳ provisional, quality-high pending on this tree Created-with: https://s.veen.world/skills Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
423ac738e6
commit
6625f0df30
3 files changed
+62
-56
No files matched your search
@@ -63,29 +63,29 @@
|
||||
| map(attribute='inventory_hostname')
|
||||
| list }}
|
||||
|
||||
- name: 📁 Create stage directory for {{ application_id }}
|
||||
ansible.builtin.file:
|
||||
path: "{{ _cli_stage_dir }}"
|
||||
state: directory
|
||||
mode: "0700"
|
||||
delegate_to: "{{ CLI_HOST_NODE }}"
|
||||
- name: 📦 Stage the CLI test of {{ application_id }} on its runner host
|
||||
when: _cli_runner == inventory_hostname
|
||||
block:
|
||||
- name: 📁 Create stage directory for {{ application_id }}
|
||||
ansible.builtin.file:
|
||||
path: "{{ _cli_stage_dir }}"
|
||||
state: directory
|
||||
mode: "0700"
|
||||
delegate_to: "{{ CLI_HOST_NODE }}"
|
||||
|
||||
- name: 📦 Stage CLI test assets for {{ application_id }}
|
||||
ansible.builtin.copy:
|
||||
src: "{{ lookup('path_absolute', 'roles', application_id, 'files/test') }}/"
|
||||
dest: "{{ _cli_stage_dir }}/"
|
||||
mode: "0755"
|
||||
delegate_to: "{{ CLI_HOST_NODE }}"
|
||||
when: _cli_runner == inventory_hostname
|
||||
- name: 📦 Stage CLI test assets for {{ application_id }}
|
||||
ansible.builtin.copy:
|
||||
src: "{{ lookup('path_absolute', 'roles', application_id, 'files/test') }}/"
|
||||
dest: "{{ _cli_stage_dir }}/"
|
||||
mode: "0755"
|
||||
delegate_to: "{{ CLI_HOST_NODE }}"
|
||||
|
||||
- name: 📦 Stage the shared CLI harness for {{ application_id }}
|
||||
ansible.builtin.copy:
|
||||
src: "{{ lookup('path_absolute', 'roles/test-e2e-cli/files/shared') }}/"
|
||||
dest: "{{ _cli_stage_dir }}/shared/"
|
||||
mode: "0755"
|
||||
delegate_to: "{{ CLI_HOST_NODE }}"
|
||||
when: _cli_runner == inventory_hostname
|
||||
- name: 📦 Stage the shared CLI harness for {{ application_id }}
|
||||
ansible.builtin.copy:
|
||||
src: "{{ lookup('path_absolute', 'roles/test-e2e-cli/files/shared') }}/"
|
||||
dest: "{{ _cli_stage_dir }}/shared/"
|
||||
mode: "0755"
|
||||
delegate_to: "{{ CLI_HOST_NODE }}"
|
||||
|
||||
- name: 🧭 Reset the per-role deployment-mode override
|
||||
ansible.builtin.include_vars:
|
||||
@@ -117,31 +117,32 @@
|
||||
file: "{{ lookup('path_absolute', 'roles', application_id, 'vars/main.yml') }}"
|
||||
when: _cli_app_vars_stat.stat.exists
|
||||
|
||||
- name: 📄 Render test.env from templates/test.env.j2 for {{ application_id }}
|
||||
ansible.builtin.copy:
|
||||
dest: "{{ _cli_stage_dir }}/test.env"
|
||||
mode: "0600"
|
||||
content: "{{ lookup('template', _cli_env_template) }}"
|
||||
delegate_to: "{{ CLI_HOST_NODE }}"
|
||||
- name: 🚀 Render and run the CLI test of {{ application_id }} on its runner host
|
||||
when: _cli_runner == inventory_hostname
|
||||
block:
|
||||
- name: 📄 Render test.env from templates/test.env.j2 for {{ application_id }}
|
||||
ansible.builtin.copy:
|
||||
dest: "{{ _cli_stage_dir }}/test.env"
|
||||
mode: "0600"
|
||||
content: "{{ lookup('template', _cli_env_template) }}"
|
||||
delegate_to: "{{ CLI_HOST_NODE }}"
|
||||
|
||||
- name: 🚀 Run CLI test script for {{ application_id }}
|
||||
ansible.builtin.shell:
|
||||
cmd: "set -ea; . {{ _cli_stage_dir }}/test.env; set +ea; export CLI_DEADLINE=$(( $(date +%s) + {{ _cli_timeout }} )); timeout {{ _cli_timeout }} bash {{ _cli_test_script
|
||||
}}"
|
||||
executable: /bin/bash
|
||||
vars:
|
||||
_cli_timeout_base: "{{ lookup('role_tests', application_id, 'cli.timeout', default='') }}"
|
||||
_cli_timeout: >-
|
||||
{{ (lookup('timeout', _cli_timeout_base | int) | int)
|
||||
if (_cli_timeout_base | string | trim | length > 0)
|
||||
else (TEST_E2E_CLI_TIMEOUT | int) }}
|
||||
register: _cli_run
|
||||
changed_when: false
|
||||
failed_when: false
|
||||
delegate_to: "{{ CLI_HOST_NODE }}"
|
||||
throttle: 1
|
||||
when: _cli_runner == inventory_hostname
|
||||
- name: 🚀 Run CLI test script for {{ application_id }}
|
||||
ansible.builtin.shell:
|
||||
cmd: "set -ea; . {{ _cli_stage_dir }}/test.env; set +ea; export CLI_DEADLINE=$(( $(date +%s) + {{ _cli_timeout }} )); timeout {{ _cli_timeout }} bash {{ _cli_test_script
|
||||
}}"
|
||||
executable: /bin/bash
|
||||
vars:
|
||||
_cli_timeout_base: "{{ lookup('role_tests', application_id, 'cli.timeout', default='') }}"
|
||||
_cli_timeout: >-
|
||||
{{ (lookup('timeout', _cli_timeout_base | int) | int)
|
||||
if (_cli_timeout_base | string | trim | length > 0)
|
||||
else (TEST_E2E_CLI_TIMEOUT | int) }}
|
||||
register: _cli_run
|
||||
changed_when: false
|
||||
failed_when: false
|
||||
delegate_to: "{{ CLI_HOST_NODE }}"
|
||||
throttle: 1
|
||||
|
||||
- name: ❌ Handle the failed CLI test run of {{ application_id }}
|
||||
when: hostvars[_cli_runner]._cli_run.rc != 0
|
||||
|
||||
@@ -14,18 +14,20 @@ RUN_ONE = PROJECT_ROOT / "roles" / "test-e2e-cli" / "tasks" / "run_one.yml"
|
||||
RUNNER_GUARD = "_cli_runner == inventory_hostname"
|
||||
|
||||
|
||||
def _tasks() -> list[dict]:
|
||||
def _tasks() -> list[tuple[dict, str]]:
|
||||
"""Flatten ``run_one.yml`` into its tasks.
|
||||
|
||||
Returns:
|
||||
Every task, the tasks inside a ``block`` included, in file order.
|
||||
Every task, the tasks inside a ``block`` included, in file order,
|
||||
each with the ``when`` it runs under, its enclosing blocks' included.
|
||||
"""
|
||||
pending = list(load_yaml_any(str(RUN_ONE)))
|
||||
flat: list[dict] = []
|
||||
pending = [(task, "") for task in load_yaml_any(str(RUN_ONE))]
|
||||
flat: list[tuple[dict, str]] = []
|
||||
while pending:
|
||||
task = pending.pop(0)
|
||||
flat.append(task)
|
||||
pending[:0] = task.get("block", [])
|
||||
task, inherited = pending.pop(0)
|
||||
guard = f"{inherited} {task.get('when', '')}"
|
||||
flat.append((task, guard))
|
||||
pending[:0] = [(child, guard) for child in task.get("block", [])]
|
||||
return flat
|
||||
|
||||
|
||||
@@ -38,7 +40,7 @@ def _task(prefix: str) -> dict:
|
||||
Returns:
|
||||
The first task whose name starts with ``prefix``.
|
||||
"""
|
||||
for task in _tasks():
|
||||
for task, _ in _tasks():
|
||||
if task.get("name", "").startswith(prefix):
|
||||
return task
|
||||
raise LookupError(f"{RUN_ONE}: no task named {prefix}")
|
||||
@@ -112,14 +114,14 @@ class TestRunOneCliNodeOnce(unittest.TestCase):
|
||||
|
||||
def test_every_task_on_the_cli_node_runs_on_the_runner_only(self) -> None:
|
||||
delegated = [
|
||||
task
|
||||
for task in _tasks()
|
||||
(task, guard)
|
||||
for task, guard in _tasks()
|
||||
if task.get("delegate_to") == "{{ CLI_HOST_NODE }}"
|
||||
]
|
||||
self.assertGreaterEqual(len(delegated), 6)
|
||||
for task in delegated:
|
||||
for task, guard in delegated:
|
||||
with self.subTest(task=task["name"]):
|
||||
self.assertIn(RUNNER_GUARD, str(task.get("when", "")))
|
||||
self.assertIn(RUNNER_GUARD, guard)
|
||||
|
||||
def test_every_peer_records_the_failure_of_the_run_it_shares(self) -> None:
|
||||
handler = _task("❌ Handle the failed CLI test run")
|
||||
|
||||
@@ -25,7 +25,10 @@ RUN_ONE = PROJECT_ROOT / "roles" / "test-e2e-cli" / "tasks" / "run_one.yml"
|
||||
|
||||
|
||||
def _run_command() -> str:
|
||||
for task in load_yaml_any(str(RUN_ONE)):
|
||||
pending = list(load_yaml_any(str(RUN_ONE)))
|
||||
while pending:
|
||||
task = pending.pop(0)
|
||||
pending[:0] = task.get("block", [])
|
||||
shell = task.get("ansible.builtin.shell")
|
||||
if isinstance(shell, dict) and "test.env" in str(shell.get("cmd", "")):
|
||||
return shell["cmd"]
|
||||
|
||||
Reference in new issue
Block a user