Files
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

359 lines
15 KiB
YAML

---
# The input guards. Both fail after the role's prereqs (packages already present), and the
# collision case re-renders traefik.yml from the same group_vars with the dashboard still off,
# so neither changes the host. They run first, so every assertion below also shows that; the
# remove path stays last, since after it the role would install Traefik again.
- name: Refuse an enabled dashboard without basicAuth users
hosts: all
gather_facts: false
tasks:
- name: Run the role with the dashboard on and no users
block:
- name: Include traefik
ansible.builtin.include_role:
name: mortis.kamino.traefik
vars:
traefik_dashboard_enabled: true
traefik_dashboard_basicauth_users: []
- name: Unreachable when the guard works
ansible.builtin.fail:
msg: traefik accepted an enabled dashboard without users
rescue:
- name: The dashboard guard, not something else, stopped the play
ansible.builtin.assert:
that: "'unauthenticated Traefik dashboard' in ansible_failed_result.msg | default('')"
quiet: true
- name: Refuse a dashboard-auth middleware collision
hosts: all
gather_facts: false
tasks:
- name: Run the role with a colliding middleware
block:
- name: Include traefik
ansible.builtin.include_role:
name: mortis.kamino.traefik
vars:
traefik_dashboard_basicauth_users:
- "admin:$apr1$x$y"
traefik_dynamic_middlewares:
dashboard-auth:
basicAuth: {users: []}
- name: Unreachable when the check works
ansible.builtin.fail:
msg: traefik accepted a colliding dashboard-auth middleware
rescue:
- name: The collision check, not something else, stopped the play
ansible.builtin.assert:
that: "'collides with the middleware' in ansible_failed_result.msg | default('')"
quiet: true
- name: Verify
hosts: all
gather_facts: false
vars_files:
- ../../defaults/main.yml
tasks:
- name: Check the installed Traefik version
ansible.builtin.command: /usr/local/bin/traefik version
register: traefik_version_out
changed_when: false
- name: Stat the secret-bearing files and the binary
ansible.builtin.stat:
path: "{{ traefik_verify_file }}"
loop:
- /etc/traefik/traefik.env
- /etc/traefik/dynamic/dynamic.yml
- /etc/traefik/traefik.yml
- /usr/local/bin/traefik
loop_control:
loop_var: traefik_verify_file
register: traefik_files
- name: Read the dynamic configuration
ansible.builtin.slurp:
src: /etc/traefik/dynamic/dynamic.yml
register: traefik_dynamic
- name: List listening TCP sockets
ansible.builtin.command: ss -ltnH
register: traefik_sockets
changed_when: false
- name: Ask the web entrypoint for its redirect
ansible.builtin.uri:
url: http://127.0.0.1:80/
follow_redirects: none
status_code: [301, 302, 307, 308]
register: traefik_http
retries: 5
delay: 2
until: traefik_http.status is defined and traefik_http.status in [301, 302, 307, 308]
- name: Read the ufw rules
ansible.builtin.command: ufw status
register: traefik_ufw
changed_when: false
- name: Read the rules ufw has stored
ansible.builtin.command: ufw show added
register: traefik_ufw_added
changed_when: false
environment: {LC_ALL: C}
- name: Read the effective unit settings
ansible.builtin.systemd:
name: traefik
register: traefik_unit
- name: Let the ACME failures against the dummy credentials play out
ansible.builtin.pause:
seconds: 15
- name: Read the service state after the wait
ansible.builtin.service_facts:
- name: The pinned version is installed
ansible.builtin.assert:
that:
- traefik_version_out.stdout is search('(?m)^Version:')
- traefik_version | regex_replace('^v', '') in traefik_version_out.stdout
quiet: true
- name: Stat the archive leftovers
ansible.builtin.stat:
path: "{{ traefik_verify_leftover }}"
loop:
- /usr/local/bin/CHANGELOG.md
- /usr/local/bin/LICENSE.md
- /tmp/traefik.tar.gz
- /tmp/traefik_checksums.txt
loop_control:
loop_var: traefik_verify_leftover
register: traefik_leftovers
# Only the binary is extracted, and the archive and checksum file are removed.
- name: The install left nothing but the binary behind
ansible.builtin.assert:
that: traefik_leftovers.results | selectattr('stat.exists') | list | length == 0
fail_msg: >-
Left behind: {{ traefik_leftovers.results | selectattr('stat.exists') | map(attribute='traefik_verify_leftover') | list }}
quiet: true
- name: Secrets are root-only and the config belongs to the traefik user
ansible.builtin.assert:
that:
- (traefik_files.results | selectattr('traefik_verify_file', 'eq', '/etc/traefik/traefik.env') | first).stat.mode == '0600'
- (traefik_files.results | selectattr('traefik_verify_file', 'eq', '/etc/traefik/traefik.env') | first).stat.pw_name == 'root'
- (traefik_files.results | selectattr('traefik_verify_file', 'eq', '/etc/traefik/dynamic/dynamic.yml') | first).stat.mode == '0600'
- (traefik_files.results | selectattr('traefik_verify_file', 'eq', '/etc/traefik/dynamic/dynamic.yml') | first).stat.pw_name == 'traefik'
- (traefik_files.results | selectattr('traefik_verify_file', 'eq', '/etc/traefik/traefik.yml') | first).stat.mode == '0600'
- (traefik_files.results | selectattr('traefik_verify_file', 'eq', '/usr/local/bin/traefik') | first).stat.mode == '0755'
quiet: true
- name: Read the logrotate configuration
ansible.builtin.slurp:
src: /etc/logrotate.d/traefik
register: traefik_logrotate
- name: Let logrotate parse its configuration (debug mode changes nothing)
ansible.builtin.command: logrotate --debug /etc/logrotate.d/traefik
register: traefik_logrotate_debug
changed_when: false
- name: The Traefik logs are rotated by copying and truncating, not by signal
ansible.builtin.assert:
that:
- "'/etc/traefik/logs/*.log {' in traefik_logrotate.content | b64decode"
- "'copytruncate' in traefik_logrotate.content | b64decode"
- "'rotate 14' in traefik_logrotate.content | b64decode"
- "'su traefik traefik' in traefik_logrotate.content | b64decode"
- traefik_logrotate_debug.rc == 0
quiet: true
- name: Read the static configuration
ansible.builtin.slurp:
src: /etc/traefik/traefik.yml
register: traefik_static
- name: The ACME resolvers render when set and are absent when empty
ansible.builtin.assert:
that:
- "'resolvers:' in traefik_static_render"
- "'- \"192.0.2.53:53\"' in traefik_static_render"
- "'resolvers:' not in traefik_empty_render"
- "'dnsChallenge:' in traefik_empty_render"
quiet: true
vars:
traefik_static_render: "{{ traefik_static.content | b64decode }}"
traefik_empty_render: "{{ lookup('ansible.builtin.template', playbook_dir ~ '/../../templates/traefik.yml.j2') }}"
traefik_dns_challenge_resolvers: []
traefik_cloudflare_email: [email protected]
- name: The dashboard is off by default and the extra app is still routed
ansible.builtin.assert:
that:
- "'Host(`traefik.example.test`)' not in traefik_dyn_render"
- "'api@internal' not in traefik_dyn_render"
- "'dashboard-auth' not in traefik_dyn_render"
- "'Host(`whoami.example.test`)' in traefik_dyn_render"
- "'http://127.0.0.1:8081' in traefik_dyn_render"
- "'dashboard: false' in traefik_static.content | b64decode"
quiet: true
vars:
traefik_dyn_render: "{{ traefik_dynamic.content | b64decode }}"
- name: An enabled dashboard renders behind basicAuth
ansible.builtin.assert:
that:
- "'dashboard: true' in traefik_on_static"
- "'Host(`traefik.example.test`)' in traefik_on_dynamic"
- "'api@internal' in traefik_on_dynamic"
- "'dashboard-auth@file' in traefik_on_dynamic"
- "'admin:hash' in traefik_on_dynamic"
quiet: true
vars:
traefik_dashboard_enabled: true
traefik_dashboard_basicauth_users: ["admin:hash"]
traefik_on_static: "{{ lookup('ansible.builtin.template', playbook_dir ~ '/../../templates/traefik.yml.j2') }}"
traefik_on_dynamic: "{{ lookup('ansible.builtin.template', playbook_dir ~ '/../../templates/dynamic.yml.j2') }}"
- name: The service stays running with the role's unit settings
ansible.builtin.assert:
that:
- ansible_facts.services['traefik.service'].state == 'running'
- ansible_facts.services['traefik.service'].status == 'enabled'
- traefik_unit.status.NoNewPrivileges == 'yes'
- traefik_unit.status.MemoryDenyWriteExecute == 'yes'
- "'cap_net_bind_service' in traefik_unit.status.AmbientCapabilities | lower"
# the molecule drop-in took effect, so the unit was started without the sandbox
- traefik_unit.status.ProtectSystem == 'no'
quiet: true
- name: Traefik listens on 80 and 443 and redirects http to https
ansible.builtin.assert:
that:
- traefik_sockets.stdout is search(':80\s')
- traefik_sockets.stdout is search(':443\s')
- traefik_http.status in [301, 302, 307, 308]
quiet: true
- name: Ufw opens 80 and 443 next to SSH
ansible.builtin.assert:
that:
- traefik_ufw.stdout is search('(?m)^22/tcp\s+ALLOW')
- traefik_ufw.stdout is search('(?m)^80\s+ALLOW')
- traefik_ufw.stdout is search('(?m)^443\s+ALLOW')
quiet: true
- name: The traefik rules carry the traefik owner tag, in order
ansible.builtin.assert:
that:
- traefik_ufw_tagged | length == 2
- 'traefik_ufw_tagged[0] is search("80 comment .ansible-ufw\[traefik:[0-9a-f]{10}\]: Traefik http")'
- 'traefik_ufw_tagged[1] is search("443 comment .ansible-ufw\[traefik:[0-9a-f]{10}\]: Traefik https")'
- traefik_ufw_tagged[0] is search('\[traefik:' ~ traefik_ufw_key_80 ~ '\]')
- traefik_ufw_tagged[1] is search('\[traefik:' ~ traefik_ufw_key_443 ~ '\]')
- traefik_ufw_key_80 == '56ae54fb00'
- traefik_ufw_key_443 == '6d1cd18d60'
quiet: true
vars:
# sha1 of rule|port|proto|from|to|interface|direction, first ten hex characters
traefik_ufw_key_80: "{{ ('allow|80|||||in' | hash('sha1'))[:10] }}"
traefik_ufw_key_443: "{{ ('allow|443|||||in' | hash('sha1'))[:10] }}"
traefik_ufw_tagged: "{{ traefik_ufw_added.stdout_lines | select('match', '^ufw ') | select('search', 'ansible-ufw') | list }}"
# A real version bump through the update path and mortis.kamino._binary_update: down to another
# release and back to the role default, both with traefik_update. After the idempotence step,
# because each run changes the binary, and before the remove path, which deletes it.
- name: Verify the update path with real Traefik releases
hosts: all
gather_facts: false
vars_files:
- ../../defaults/main.yml
vars:
# Any published release other than the role default, with a checksum file.
traefik_verify_other_version: v3.7.18
tasks:
- name: The other release differs from the role default
ansible.builtin.assert:
that: traefik_verify_other_version != traefik_version
fail_msg: Move traefik_verify_other_version off the role default ({{ traefik_version }}).
quiet: true
- name: Update to the other release, then back to the role default
ansible.builtin.include_tasks: tasks/update_and_check.yml
loop:
- "{{ traefik_verify_other_version }}"
- "{{ traefik_version }}"
loop_control:
loop_var: traefik_verify_target
# The remove path. Last on purpose: it destroys what the plays above verify, and it runs
# once, outside the idempotence step (a converge play that changed state would fail that step).
# Nothing runs after verify except molecule's own cleanup and destroy of the container.
# Assumptions: the plays above left the two tagged traefik rules in place, ufw active and the
# unit installed; traefik_remove_data stays false, so the configuration directory is not asserted.
# With traefik_remove set, the role skips prereqs, install, configure and update, so a reinstall
# or re-render would show as the binary or the unit coming back.
- name: Verify the remove path
hosts: all
gather_facts: false
vars_files:
- ../../defaults/main.yml
tasks:
- 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: Read the rules ufw has stored before the removal
ansible.builtin.command: ufw show added
register: traefik_rm_before
changed_when: false
environment: {LC_ALL: C}
- name: The traefik rules exist before the removal
ansible.builtin.assert:
that: traefik_rm_before.stdout_lines | select('search', 'ansible-ufw\[traefik:') | list | length == 2
quiet: true
- name: Run the role with the remove flag
ansible.builtin.include_role:
name: mortis.kamino.traefik
vars:
traefik_remove: true
- name: Read the rules ufw has stored after the removal
ansible.builtin.command: ufw show added
register: traefik_rm_after
changed_when: false
environment: {LC_ALL: C}
- name: Stat the unit and the binary the removal deletes
ansible.builtin.stat:
path: "{{ traefik_rm_path }}"
loop:
- "{{ traefik_service_unit }}"
- "{{ traefik_bin_path }}"
loop_control:
loop_var: traefik_rm_path
register: traefik_rm_files
- name: Ask systemd whether the service is still active
ansible.builtin.systemd:
name: traefik
register: traefik_rm_active
failed_when: false
- name: Only the traefik rules are gone, and nothing was installed again
ansible.builtin.assert:
that:
- traefik_rm_after.stdout_lines | select('search', 'ansible-ufw\[traefik:') | list | length == 0
- traefik_rm_after.stdout_lines | select('search', '(^|[ ])22/tcp') | list | length > 0
- traefik_rm_after.stdout_lines | select('search', '8123/tcp') | list | length == 1
- traefik_rm_files.results | map(attribute='stat') | map(attribute='exists') | select | list | length == 0
# the module errors for a unit that no longer exists, which is also a pass
- (traefik_rm_active.status | default({})).ActiveState | default('inactive') != 'active'
quiet: true