Files
FabledSteward/tests/test_ansible_command.py
T
bvandeusen 8b62eb2ca3
CI / lint (push) Successful in 2s
CI / unit (push) Successful in 7s
CI / integration (push) Successful in 2m16s
feat(ansible): parameterized runs — extra-vars, limit, tags, dry-run (task 546)
Run form + executor now support optional params, passed as argv (never
shell-interpolated):
- extra_vars: one key=value per line -> repeated -e
- limit -> --limit; tags -> --tags
- dry-run checkbox -> --check --diff

- executor.py: pure build_ansible_command(playbook, inventory, params); start_run
  gains an optional params arg (backward-compatible)
- models/ansible.py + migration 0013: nullable params JSON column
- routes.py: create_run parses + validates (extra-var lines need '='), stores
  params on the run, passes to the executor
- browse.html run form: Extra vars / Limit / Tags / Dry-run fields
- run_detail.html: shows the params used
- tests/test_ansible_command.py: unit coverage of the builder; integration test
  runs a real playbook with extra-var + limit + check and asserts substitution

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-02 13:31:16 -04:00

43 lines
1.4 KiB
Python

"""Unit tests for the ansible-playbook command builder (task 546)."""
from steward.ansible.executor import build_ansible_command
BASE = ["ansible-playbook", "site.yml", "-i", "inv.ini"]
def test_base_no_params():
assert build_ansible_command("site.yml", "inv.ini") == BASE
assert build_ansible_command("site.yml", "inv.ini", None) == BASE
assert build_ansible_command("site.yml", "inv.ini", {}) == BASE
def test_extra_vars_each_passed_as_argv():
cmd = build_ansible_command("site.yml", "inv.ini", {"extra_vars": ["a=1", "b=two words"]})
assert cmd == BASE + ["-e", "a=1", "-e", "b=two words"]
def test_limit_and_tags():
cmd = build_ansible_command("site.yml", "inv.ini", {"limit": "web*", "tags": "deploy,cfg"})
assert cmd[cmd.index("--limit") + 1] == "web*"
assert cmd[cmd.index("--tags") + 1] == "deploy,cfg"
def test_check_adds_check_and_diff():
cmd = build_ansible_command("site.yml", "inv.ini", {"check": True})
assert cmd == BASE + ["--check", "--diff"]
def test_falsey_values_are_skipped():
cmd = build_ansible_command(
"site.yml", "inv.ini",
{"extra_vars": [], "limit": "", "tags": "", "check": False},
)
assert cmd == BASE
def test_all_combined_order():
cmd = build_ansible_command(
"site.yml", "inv.ini",
{"extra_vars": ["v=1"], "limit": "h1", "tags": "t1", "check": True},
)
assert cmd == BASE + ["-e", "v=1", "--limit", "h1", "--tags", "t1", "--check", "--diff"]