Files
computer-playbook/roles/test-e2e-cli/tasks/run_one.yml
T
kevinveenbirkenbachandClaude Opus 5 c7240d836b fix(test-e2e-cli): run one role's CLI test at a time
An app whose inventory group spans several swarm nodes ran its CLI test once per
node, in parallel. All of them authenticate as the one MCP service account the
role owns, and the adapter keeps every session of a user in a single user-meta
value which it reads, edits and writes back whole, without a lock and without a
key per session. Two simultaneous initialize calls overwrite each other and the
loser's follow-up is answered "Session not found: Invalid or expired session".

Run 34699757369, job 103572236972: three CLI tests finished 6ms apart, the node
journal shows two 202 responses ahead of a 200 inside the same second, and
exactly one 404 among seven 200s across the three replicas. The adapter's
DEFAULT_INACTIVITY_TIMEOUT is DAY_IN_SECONDS, so expiry cannot explain a session
lost seconds after it was made. Compose has one node and never shows this.

throttle: 1 removes the concurrency and keeps every execution. run_once would
have removed executions instead: this task runs the role's own test.sh, not only
the MCP contract, so per-node coverage is worth keeping.

The cost is bounded. In that job one app of ten spanned more than one node, and
its test took 9.29s; the two that run for six minutes each occupy a single host
and pay nothing. Compose pays nothing at all.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-13 15:21:12 +02:00

135 lines
5.0 KiB
YAML

---
- name: 📂 Define per-role paths for {{ application_id }}
ansible.builtin.set_fact:
_cli_env_template: "{{ lookup('path_absolute', 'roles', application_id, 'templates/test.env.j2') }}"
_cli_stage_dir: "{{ TEST_E2E_CLI_STAGE_BASE_DIR }}/{{ application_id }}"
_cli_test_script: "{{ TEST_E2E_CLI_STAGE_BASE_DIR }}/{{ application_id }}/test.sh"
_cli_service_key: "{{ lookup('role_tests', application_id, 'cli.container_service_key', default='') | trim }}"
- name: "✅ Assert CLI test assets exist for {{ application_id }}"
ansible.builtin.stat:
path: "{{ item }}"
register: _cli_asset_stats
delegate_to: localhost
run_once: true
loop:
- "{{ _cli_env_template }}"
- "{{ lookup('path_absolute', 'roles', application_id, 'files/test/test.sh') }}"
- name: "❌ Fail if a CLI test asset is missing for {{ application_id }}"
ansible.builtin.fail:
msg: "Missing CLI test asset: {{ item.item }}"
when: not item.stat.exists
loop: "{{ _cli_asset_stats.results }}"
loop_control:
label: "{{ item.item }}"
- name: 🧭 Default the CLI host to the loop host for {{ application_id }}
ansible.builtin.set_fact:
CLI_HOST_NODE: "{{ inventory_hostname }}"
CLI_LOCAL_CID: ""
- name: "🔍 Resolve the node that holds {{ application_id }}'s test container"
ansible.builtin.include_tasks: "{{ lookup('path_absolute', 'roles/sys-svc-compose/tasks/utils/swarm/resolve_host_cid.yml') }}"
vars:
resolve_service_key: "{{ _cli_service_key }}"
resolve_output_prefix: CLI
resolve_compose_cid: "{{ lookup('container_address', application_id, _cli_service_key) }}"
resolve_converge_retries: 30
resolve_converge_delay: 2
when: _cli_service_key | length > 0
- 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 }}"
- 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:
file: "{{ lookup('path_absolute', 'roles/sys-svc-compose/defaults/main.yml') }}"
- name: 🔍 Check optional application defaults for {{ application_id }}
ansible.builtin.stat:
path: "{{ lookup('path_absolute', 'roles', application_id, 'defaults/main.yml') }}"
register: _cli_app_defaults_stat
delegate_to: localhost
run_once: true
- name: 📦 Load optional application defaults for {{ application_id }}
ansible.builtin.include_vars:
file: "{{ lookup('path_absolute', 'roles', application_id, 'defaults/main.yml') }}"
when: _cli_app_defaults_stat.stat.exists
- name: 🔍 Check optional application vars for {{ application_id }}
ansible.builtin.stat:
path: "{{ lookup('path_absolute', 'roles', application_id, 'vars/main.yml') }}"
register: _cli_app_vars_stat
delegate_to: localhost
run_once: true
- name: 📦 Load optional application vars for {{ application_id }}
ansible.builtin.include_vars:
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: 🚀 Run CLI test script for {{ application_id }}
ansible.builtin.shell:
cmd: "set -a && . {{ _cli_stage_dir }}/test.env && set +a && 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: 📄 Print CLI test output on failure for {{ application_id }}
ansible.builtin.debug:
msg: |
stdout:
{{ _cli_run.stdout }}
stderr:
{{ _cli_run.stderr }}
when: _cli_run.rc != 0
changed_when: false
- name: ❌ Record failure for {{ application_id }}
ansible.builtin.set_fact:
_cli_failures: "{{ (_cli_failures | default([])) + [application_id] }}"
when: _cli_run.rc != 0
- name: 🧹 Remove the delegated stage dir of {{ application_id }}
ansible.builtin.file:
path: "{{ _cli_stage_dir }}"
state: absent
delegate_to: "{{ CLI_HOST_NODE }}"
when: CLI_HOST_NODE != inventory_hostname