Files
Kamino/roles/gitea/molecule/default/verify.yml
T
Defame1297 fdb3f8c1df
Build / build (pull_request) Successful in 25s
Lint / lint (pull_request) Successful in 1m16s
Molecule / molecule (pull_request) Successful in 9m29s
test(molecule): move refusal plays from converge to verify
molecule's idempotence step reruns converge.yml, so plays that only prove a
role refuses bad input ran twice and added nothing the second time. The ufw,
sshd, gitea and traefik scenarios now keep only the plays that apply the role
in converge.yml; the refusal plays and gitea's child ansible-playbook guard
checks run once, first in verify.yml. Every existing assertion runs after
them, so verify still proves a refused run left the host unchanged.

Assisted-by: Claude Code:claude-opus-5-5
2026-11-22 22:15:37 +00:00

422 lines
16 KiB
YAML

---
# Guard and no-op paths of the mint entry point (nothing is installed). Each failure case
# wraps the include in block/rescue, or runs ci_guard.yml as a child process, so the play
# passes when the guard fires and fails when the include returns or fails with another message.
# First, so the assertions below also show the guards changed nothing.
- name: Verify the mint guards
hosts: all
gather_facts: false
tasks:
- name: Mint without confirmation
block:
- name: Include the mint entry point with gitea_mint_confirm unset
ansible.builtin.include_role:
name: mortis.kamino.gitea
tasks_from: mint
# Only reached when the include returned, so the guard did not fire. The message
# must not contain the guard's text, so the rescue below cannot mistake it for it.
- name: Fail because the include returned
ansible.builtin.fail:
msg: The include returned without the confirm guard firing
rescue:
- name: The confirm guard fired, and nothing else failed first
ansible.builtin.assert:
that:
- "'mint is opt-in' in ansible_failed_result.msg"
- "'gitea_mint_confirm=true' in ansible_failed_result.msg"
quiet: true
- name: Mint under each CI variable, run as a child process with only that one set
delegate_to: localhost
run_once: true
ansible.builtin.command:
argv:
- ansible-playbook
- -i
- localhost,
- "{{ playbook_dir }}/ci_guard.yml"
environment: "{{ {'GITHUB_ACTIONS': '', 'GITEA_ACTIONS': '', 'CI': '', 'ANSIBLE_NOCOLOR': '1'} | combine({item: 'true'}) }}"
loop:
- GITHUB_ACTIONS
- GITEA_ACTIONS
- CI
register: gitea_test_mint_ci
# The child is expected to fail (the CI guard fires). Its rc and output are asserted in the next task.
failed_when: false
changed_when: false
# Ansible 2.19 prints task failures to stderr, so both streams are checked.
- name: The CI guard fired for every variable
ansible.builtin.assert:
that:
- item.rc != 0
- "'refuses to run under CI' in (item.stdout ~ item.stderr)"
- "'mint is opt-in' not in (item.stdout ~ item.stderr)"
quiet: true
loop: "{{ gitea_test_mint_ci.results }}"
loop_control:
label: "{{ item.item }}"
- name: Mint outside CI with the token already defined, run as a child process
delegate_to: localhost
run_once: true
ansible.builtin.command:
argv:
- ansible-playbook
- -i
- localhost,
- "{{ playbook_dir }}/ci_guard.yml"
- -e
- gitea_api_token=molecule-not-a-token
environment:
GITHUB_ACTIONS: ""
GITEA_ACTIONS: ""
CI: ""
ANSIBLE_NOCOLOR: "1"
register: gitea_test_mint_noop
# A child failure must not abort here; the next task asserts rc and output.
failed_when: false
changed_when: false
- name: The mint was a no-op that registered no token fact
ansible.builtin.assert:
that:
- gitea_test_mint_noop.rc == 0
- "'nothing to mint' in (gitea_test_mint_noop.stdout ~ gitea_test_mint_noop.stderr)"
quiet: true
# An empty token counts as missing (configure.yml refuses it), so the mint must not report
# "nothing to mint". In check mode it creates nothing and registers no fact, and ends green
# (ci_guard.yml asserts that gitea_minted_api_token is not defined).
- name: Mint outside CI with an empty token in check mode, run as a child process
delegate_to: localhost
run_once: true
ansible.builtin.command:
argv:
- ansible-playbook
- -i
- localhost,
- "{{ playbook_dir }}/ci_guard.yml"
- --check
- -e
- gitea_api_token=
environment:
GITHUB_ACTIONS: ""
GITEA_ACTIONS: ""
CI: ""
ANSIBLE_NOCOLOR: "1"
register: gitea_test_mint_empty
# A child failure must not abort here; the next task asserts rc and output.
failed_when: false
changed_when: false
- name: An empty token was treated as missing and nothing was minted
ansible.builtin.assert:
that:
- gitea_test_mint_empty.rc == 0
- "'nothing to mint' not in (gitea_test_mint_empty.stdout ~ gitea_test_mint_empty.stderr)"
quiet: true
- name: Verify
hosts: all
gather_facts: false
tasks:
- name: Stat the Gitea binary
ansible.builtin.stat:
path: /usr/local/bin/gitea
register: gitea_verify_binary
- name: Gitea was not installed
ansible.builtin.assert:
that: not gitea_verify_binary.stat.exists
quiet: true
# The signature check of the download, against local files: two throwaway keys made here,
# A pinned and B not, both in the key file the role imports, so B's signature is checked in the
# same keyring as the pinned key. Only a binary signed by A is placed. The keys exist only in
# this container. gpg is preinstalled in the image (the role's prereqs install it on a real
# host).
- name: Verify the signature check
hosts: all
gather_facts: false
vars:
gitea_sig_dir: /tmp/molecule-signature
gitea_sig_home: /tmp/molecule-signature/gnupg
tasks:
- name: Create the test directories
ansible.builtin.file:
path: "{{ gitea_sig_path }}"
state: directory
mode: "0700"
loop: ["{{ gitea_sig_dir }}", "{{ gitea_sig_home }}"]
loop_control:
loop_var: gitea_sig_path
- name: Make the test keys
ansible.builtin.command:
argv:
- gpg
- --homedir
- "{{ gitea_sig_home }}"
- --batch
- --pinentry-mode
- loopback
- --passphrase
- ""
- --quick-gen-key
- "Molecule {{ gitea_sig_key }} <{{ gitea_sig_key }}@molecule.invalid>"
- ed25519
- sign
- never
loop: [a, b]
loop_control:
loop_var: gitea_sig_key
changed_when: true
- name: Read the test key fingerprints
ansible.builtin.command:
argv: [gpg, --homedir, "{{ gitea_sig_home }}", --batch, --with-colons, --fingerprint, "{{ gitea_sig_key }}@molecule.invalid"]
loop: [a, b]
loop_control:
loop_var: gitea_sig_key
register: gitea_sig_listing
changed_when: false
- name: Keep the test key fingerprints
ansible.builtin.set_fact:
gitea_sig_fpr: >-
{{ dict(['a', 'b'] | zip(gitea_sig_listing.results
| map(attribute='stdout')
| map('regex_findall', '(?m)^fpr:(?:[^:]*:){8}([0-9A-F]+):')
| map('first'))) }}
- name: Write the stub binary and a tampered copy
ansible.builtin.copy:
dest: "{{ gitea_sig_dir }}/{{ gitea_sig_stub.name }}"
content: "{{ gitea_sig_stub.content }}"
mode: "0755"
loop:
- {name: stub, content: "#!/bin/sh\necho molecule\n"}
- {name: tampered, content: "#!/bin/sh\necho tampered\n"}
loop_control:
loop_var: gitea_sig_stub
label: "{{ gitea_sig_stub.name }}"
- name: Sign the stub with each key
ansible.builtin.command:
argv:
- gpg
- --homedir
- "{{ gitea_sig_home }}"
- --batch
- --yes
- --local-user
- "{{ gitea_sig_fpr[gitea_sig_key] }}"
- --armor
- --output
- "{{ gitea_sig_dir }}/stub.{{ gitea_sig_key }}.asc"
- --detach-sign
- "{{ gitea_sig_dir }}/stub"
loop: [a, b]
loop_control:
loop_var: gitea_sig_key
changed_when: true
- name: Export the key files the role imports
ansible.builtin.command:
argv: >-
{{ ['gpg', '--homedir', gitea_sig_home, '--batch', '--yes', '--armor',
'--output', gitea_sig_dir ~ '/' ~ gitea_sig_export.file, '--export']
+ (gitea_sig_export.key_names | map('extract', gitea_sig_fpr) | list) }}
loop:
- {file: keys-ab.asc, key_names: [a, b]}
- {file: keys-b.asc, key_names: [b]}
loop_control:
loop_var: gitea_sig_export
label: "{{ gitea_sig_export.file }}"
changed_when: true
- name: Run each case
ansible.builtin.include_tasks: tasks/signature_case.yml
loop:
- {name: good, binary: stub, signature: stub.a.asc, key: keys-ab.asc, expect: placed}
- {name: other-key, binary: stub, signature: stub.b.asc, key: keys-ab.asc, expect: Verify Gitea binary signature}
- {name: tampered, binary: tampered, signature: stub.a.asc, key: keys-ab.asc, expect: Verify Gitea binary signature}
- {name: pinned-key-missing, binary: stub, signature: stub.a.asc, key: keys-b.asc, expect: Require the pinned release key}
loop_control:
loop_var: gitea_sig_case
label: "{{ gitea_sig_case.name }}"
- name: Find download directories left behind
ansible.builtin.find:
paths: /tmp
patterns: gitea-download.*
file_type: directory
register: gitea_sig_leftovers
- name: Every case removed its download directory
ansible.builtin.assert:
that: gitea_sig_leftovers.files | length == 0
fail_msg: "Left behind: {{ gitea_sig_leftovers.files | map(attribute='path') | join(', ') }}"
quiet: true
- name: Stop the gpg daemons of the test keyring
ansible.builtin.command:
argv: [gpgconf, --homedir, "{{ gitea_sig_home }}", --kill, all]
changed_when: false
# The remove path. Last on purpose: it runs once, outside the idempotence step (a converge
# play that changed state would fail that step), and nothing runs after verify except molecule's
# own cleanup and destroy of the container. Gitea itself is never installed, so this play seeds
# what the removal acts on: a stub unit and binary (the systemd stop of a unit that does not
# exist fails), and the firewall, set up through the real entry points (mortis.kamino.ufw, then
# tasks_from: service_rules with the role's own gitea_ufw_service_rules).
# gitea_remove_data stays false, so the data directories and the user are not touched; their
# absence below shows that prereqs, install and configure (which create them) did not run.
- name: Verify the remove path
hosts: all
gather_facts: true
vars_files:
- ../../defaults/main.yml
tasks:
- name: Enable ufw with the SSH rule, as the base play does
ansible.builtin.include_role:
name: mortis.kamino.ufw
vars:
ufw_rules:
- {rule: allow, port: 22, proto: tcp, comment: ssh}
- name: Add the Gitea rules through the service_rules entry point
ansible.builtin.include_role:
name: mortis.kamino.ufw
tasks_from: service_rules
vars:
ufw_service_name: gitea
ufw_service_rules: "{{ gitea_ufw_service_rules }}"
- name: Seed a rule without an owner tag, which the removal must keep
ansible.builtin.command: ufw allow 8123/tcp comment 'molecule manual rule'
changed_when: true
- name: Seed a stub binary and a stub unit for the removal to delete
ansible.builtin.copy:
dest: "{{ gitea_rm_stub.dest }}"
content: "{{ gitea_rm_stub.content }}"
mode: "{{ gitea_rm_stub.mode }}"
loop:
- dest: "{{ gitea_install_dir }}/gitea"
mode: "0755"
content: "#!/bin/sh\nexit 0\n"
- dest: "{{ gitea_service_unit }}"
mode: "0644"
content: |
[Unit]
Description=Gitea stub for molecule
[Service]
Type=oneshot
ExecStart=/bin/true
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
loop_control:
loop_var: gitea_rm_stub
label: "{{ gitea_rm_stub.dest }}"
- name: Let systemd read the stub unit
ansible.builtin.systemd:
daemon_reload: true
- name: Start and enable the stub unit, so the removal has something to stop and disable
ansible.builtin.systemd:
name: gitea
state: started
enabled: true
- name: Ask systemd about the stub unit before the removal
ansible.builtin.systemd:
name: gitea
register: gitea_rm_active_before
- name: Read the rules ufw has stored before the removal
ansible.builtin.command: ufw show added
register: gitea_rm_before
changed_when: false
environment: {LC_ALL: C}
- name: The Gitea rules, the manual rule and the SSH rule exist before the removal
ansible.builtin.assert:
that:
- gitea_rm_before.stdout_lines | select('search', 'ansible-ufw\[gitea:') | list | length == gitea_ufw_service_rules | length
- gitea_rm_before.stdout_lines | select('search', '8123/tcp') | list | length == 1
- gitea_rm_before.stdout_lines | select('search', '(^|[ ])22/tcp') | list | length > 0
quiet: true
- name: The stub unit is active and enabled before the removal
ansible.builtin.assert:
that:
- gitea_rm_active_before.status.ActiveState == 'active'
- gitea_rm_active_before.status.UnitFileState == 'enabled'
quiet: true
- name: Run the role with the remove flag
ansible.builtin.include_role:
name: mortis.kamino.gitea
vars:
gitea_remove: true
# Required by the argument spec of the main entry point; the values are never used.
gitea_domain: gitea.example.test
gitea_admin_user: molecule-admin
gitea_admin_email: [email protected]
gitea_admin_password: molecule-not-a-password
gitea_secret_key: molecule-not-a-key
gitea_internal_token: molecule-not-a-token
gitea_jwt_secret: molecule-not-a-secret
gitea_ssh_keys: []
- name: Read the rules ufw has stored after the removal
ansible.builtin.command: ufw show added
register: gitea_rm_after
changed_when: false
environment: {LC_ALL: C}
- name: Stat what the removal deletes and what install or configure would create
ansible.builtin.stat:
path: "{{ gitea_rm_path }}"
loop:
- "{{ gitea_service_unit }}"
- "{{ gitea_install_dir }}/gitea"
- "{{ gitea_app_dir }}"
- "{{ gitea_data_dirs | first }}"
- /etc/systemd/system/multi-user.target.wants/gitea.service
loop_control:
loop_var: gitea_rm_path
register: gitea_rm_files
- name: Look up the Gitea system user
ansible.builtin.getent:
database: passwd
key: "{{ gitea_user }}"
fail_key: false
register: gitea_rm_user
- name: Ask systemd whether the service is still active
ansible.builtin.systemd:
name: gitea
register: gitea_rm_active
failed_when: false
- name: Only the Gitea rules are gone, and nothing was installed or configured
ansible.builtin.assert:
that:
- gitea_rm_after.stdout_lines | select('search', 'ansible-ufw\[gitea:') | list | length == 0
- gitea_rm_after.stdout_lines | select('search', '(^|[ ])22/tcp') | list | length > 0
- gitea_rm_after.stdout_lines | select('search', '8123/tcp') | list | length == 1
# unit and binary are deleted, and the config and data directories never came into being
- gitea_rm_files.results | map(attribute='stat') | map(attribute='exists') | select | list | length == 0
# the account is only created by prereqs, which the removal skips
- gitea_rm_user.ansible_facts.getent_passwd[gitea_user] is none
# the module errors for a unit that no longer exists, which is also a pass
- (gitea_rm_active.status | default({})).ActiveState | default('inactive') != 'active'
quiet: true