diff --git a/roles/test-e2e-cli/tasks/run_one.yml b/roles/test-e2e-cli/tasks/run_one.yml index e7a59b78bc..a5af6fd58a 100644 --- a/roles/test-e2e-cli/tasks/run_one.yml +++ b/roles/test-e2e-cli/tasks/run_one.yml @@ -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 diff --git a/tests/unit/python/roles/test-e2e-cli/test_run_one_cli_node_once.py b/tests/unit/python/roles/test-e2e-cli/test_run_one_cli_node_once.py index ad00115dd2..a820d9b33e 100644 --- a/tests/unit/python/roles/test-e2e-cli/test_run_one_cli_node_once.py +++ b/tests/unit/python/roles/test-e2e-cli/test_run_one_cli_node_once.py @@ -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") diff --git a/tests/unit/python/roles/test-e2e-cli/test_run_one_env_sourcing.py b/tests/unit/python/roles/test-e2e-cli/test_run_one_env_sourcing.py index 6317304ceb..9a98a6a597 100644 --- a/tests/unit/python/roles/test-e2e-cli/test_run_one_env_sourcing.py +++ b/tests/unit/python/roles/test-e2e-cli/test_run_one_env_sourcing.py @@ -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"]