Files
FabledSteward/tests/test_playbook_variables.py
bvandeusen 42f7840c26
CI / lint (push) Successful in 2s
CI / unit (push) Successful in 8s
CI / integration (push) Successful in 2m14s
CI / publish (push) Successful in 59s
feat(ansible): steward:category + steward:confirm playbook metadata
Extend the playbook metadata convention with a namespaced `# steward:<key>:`
comment block:

- steward:category — free-text grouping label, shown as a badge in the browse
  list and on the run form.
- steward:confirm — true/yes/1/on marks a playbook destructive; the run form
  then requires a confirmation tick (required checkbox in the shared vars
  fragment) before it can launch.

sources.discover_playbook_meta() parses description + category + confirm (first
match per key; `# description:` still primary, `# steward:description:` alias).
discover_playbook_description() now delegates to it. The browse list reads
per-playbook meta to show category badges + descriptions; the run-form and
playbook-vars fragments render the badge + confirm gate.

Bundled playbooks tagged: docker_prune → category maintenance + confirm true;
provision/install/update → category host-agent.

Docs: docs/reference/playbook-authoring.md updated (keys now implemented) and a
quick reference added next to the code at steward/ansible/PLAYBOOK_CONVENTIONS.md.
Tests added for category/confirm/alias parsing.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-17 11:35:35 -04:00

169 lines
5.2 KiB
Python

"""Unit tests for playbook variable discovery + run-time extra-vars file."""
import json
from steward.ansible.executor import build_extra_vars_file
from steward.ansible.sources import (
discover_playbook_description,
discover_playbook_meta,
discover_playbook_variables,
)
def _names(variables):
return [v["name"] for v in variables]
def test_vars_block_defaults_surface_as_fields():
content = """
- hosts: all
vars:
steward_url: ""
agent_interval: 30
enabled: true
tasks: []
"""
variables = discover_playbook_variables(content)
assert _names(variables) == ["steward_url", "agent_interval", "enabled"]
interval = next(v for v in variables if v["name"] == "agent_interval")
assert interval["default"] == 30
assert interval["required"] is False
assert interval["secret"] is False
def test_secretish_names_flagged():
content = """
- hosts: all
vars:
db_password: ""
api_token: ""
vault_secret: ""
plain_value: "x"
"""
by = {v["name"]: v for v in discover_playbook_variables(content)}
assert by["db_password"]["secret"] is True
assert by["api_token"]["secret"] is True
assert by["vault_secret"]["secret"] is True
assert by["plain_value"]["secret"] is False
def test_vars_prompt_required_and_private():
content = """
- hosts: all
vars_prompt:
- name: release_tag
prompt: "Which release?"
- name: admin_pw
prompt: "Admin password"
private: true
default: ""
tasks: []
"""
by = {v["name"]: v for v in discover_playbook_variables(content)}
# No default → required; vars_prompt defaults to private=yes → secret.
assert by["release_tag"]["required"] is True
assert by["release_tag"]["secret"] is True
assert by["release_tag"]["prompt"] == "Which release?"
# Explicit private + has default → secret but not required.
assert by["admin_pw"]["secret"] is True
assert by["admin_pw"]["required"] is False
def test_vars_prompt_wins_on_name_collision():
content = """
- hosts: all
vars_prompt:
- name: dup
prompt: "from prompt"
private: false
vars:
dup: "from vars"
"""
variables = discover_playbook_variables(content)
assert _names(variables) == ["dup"]
assert variables[0]["prompt"] == "from prompt"
def test_non_scalar_vars_skipped():
content = """
- hosts: all
vars:
scalar: 1
a_list: [1, 2]
a_map: {k: v}
"""
assert _names(discover_playbook_variables(content)) == ["scalar"]
def test_malformed_yaml_returns_empty():
assert discover_playbook_variables("this: : : not valid") == []
assert discover_playbook_variables("just a string") == []
def test_extra_vars_file_roundtrip_and_space_safe():
merged = {"agent_interval": "30", "msg": "hello world", "q": 'a "quote"'}
args, files = build_extra_vars_file(merged, "/td")
assert args == ["-e", "@/td/extravars.json"]
path, content = files[0]
assert path == "/td/extravars.json"
# JSON keeps spaces/quotes intact — no shlex hazard like -e key=value.
assert json.loads(content) == merged
def test_extra_vars_file_empty_is_noop():
assert build_extra_vars_file({}, "/td") == ([], [])
assert build_extra_vars_file(None, "/td") == ([], [])
# ── Playbook description discovery ────────────────────────────────────────────
def test_description_from_magic_comment():
content = """---
# description: Reclaim disk by pruning Docker.
- hosts: all
name: Docker prune
tasks: []
"""
assert discover_playbook_description(content) == "Reclaim disk by pruning Docker."
def test_description_case_insensitive_key():
assert discover_playbook_description("# DESCRIPTION: trimmed \n- hosts: all") == "trimmed"
def test_description_falls_back_to_play_name():
content = "- hosts: all\n name: Configure web servers\n tasks: []\n"
assert discover_playbook_description(content) == "Configure web servers"
def test_description_empty_when_neither_present():
assert discover_playbook_description("- hosts: all\n tasks: []\n") == ""
assert discover_playbook_description("not: : valid") == ""
# ── steward:category / steward:confirm metadata ───────────────────────────────
def test_meta_category_and_confirm():
content = """---
# description: Prune docker.
# steward:category: maintenance
# steward:confirm: true
- hosts: all
tasks: []
"""
meta = discover_playbook_meta(content)
assert meta["description"] == "Prune docker."
assert meta["category"] == "maintenance"
assert meta["confirm"] is True
def test_meta_confirm_falsey_and_absent():
assert discover_playbook_meta("# steward:confirm: false\n- hosts: all")["confirm"] is False
assert discover_playbook_meta("- hosts: all\n name: x")["confirm"] is False
assert discover_playbook_meta("- hosts: all\n name: x")["category"] == ""
def test_meta_steward_description_alias():
# `# steward:description:` works when there's no plain `# description:`.
meta = discover_playbook_meta("# steward:description: via namespace\n- hosts: all")
assert meta["description"] == "via namespace"