Files
Kamino/roles/pihole/molecule/default/verify.yml
T
Defame1297 bf6f9b5800
Build / build (pull_request) Successful in 27s
Lint / lint (pull_request) Successful in 2m7s
Molecule / molecule (pull_request) Failing after 2m27s
test(pihole): add the default molecule scenario
Runs on noble and resolute without the real Pi-hole: the clone target
is a local git repository whose installer is a stub that logs its
arguments and creates the database file.

Covers a check run before the converge and after idempotence, the
merge, the rendered pihole.toml (upstreams, interface, hashed
password, owner and mode), the system user, the installer sequence
(install once, then reconfigure from the handler) and a negative test
for every validation: no Ubuntu facts, a missing pihole_web_password,
an unset interface, empty upstreams, a non-string and a placeholder
password. No pihole-FTL service runs, so the loaded configuration is
not verified.

Assisted-by: Claude Code:claude-opus-5-5
2026-11-25 18:11:00 +00:00

133 lines
4.8 KiB
YAML

---
# Runs first, before any play gathers facts: the role refuses a host it cannot identify as
# Ubuntu, before any change.
- name: Refuse a host without Ubuntu facts
hosts: all
gather_facts: false
tasks:
- name: Run the role without gathered facts
ansible.builtin.include_tasks: tasks/failed_run.yml
vars:
pihole_test_case:
name: no-facts
overrides: "{{ pihole_test_overrides }}"
expect: pihole supports Ubuntu only
- name: Verify
hosts: all
gather_facts: true
tasks:
- name: Read the rendered pihole.toml
ansible.builtin.slurp:
src: /etc/pihole/pihole.toml
register: pihole_test_toml
- name: Stat pihole.toml
ansible.builtin.stat:
path: /etc/pihole/pihole.toml
register: pihole_test_toml_stat
- name: Read the stub installer log
ansible.builtin.slurp:
src: /var/log/pihole-stub-installer.log
register: pihole_test_installer_log
- name: Look up the pihole user
ansible.builtin.getent:
database: passwd
key: pihole
- name: The config carries the overridden upstreams and a hashed password
ansible.builtin.assert:
that:
- "'9.9.9.9' in pihole_test_toml_content"
- "'149.112.112.112' in pihole_test_toml_content"
- "'1.1.1.1' not in pihole_test_toml_content"
- pihole_test_toml_content is search('(?m)^\\s*interface = \"molecule0\"')
- "'\"eth0\"' not in pihole_test_toml_content"
- "'molecule-test-password' not in pihole_test_toml_content"
- pihole_test_toml_content is search('(?m)^\\s*pwhash = \"[0-9a-f]{64}\"')
quiet: true
vars:
pihole_test_toml_content: "{{ pihole_test_toml.content | b64decode }}"
- name: The config belongs to the pihole user, group-writable
ansible.builtin.assert:
that:
- pihole_test_toml_stat.stat.pw_name == 'pihole'
- pihole_test_toml_stat.stat.mode == '0664'
quiet: true
- name: The user is a system account without a login shell
ansible.builtin.assert:
that:
- getent_passwd.pihole[5] == '/usr/sbin/nologin'
- getent_passwd.pihole[1] | int < 1000
quiet: true
# Neither check run (before and after the converge) ran the installer or the handler.
- name: The installer ran once, then the reconfigure handler ran it again
ansible.builtin.assert:
that:
- pihole_test_installer_log_text.splitlines() == ['--unattended', '--reconfigure --unattended']
quiet: true
vars:
pihole_test_installer_log_text: "{{ pihole_test_installer_log.content | b64decode }}"
# Negative tests: each validation stops the role before any change.
- name: Refuse invalid inputs
hosts: all
gather_facts: true
tasks:
- name: Run the role without pihole_web_password
block:
- name: Include the role without pihole_web_password
ansible.builtin.include_role:
name: mortis.kamino.pihole
vars:
pihole_overrides: "{{ pihole_test_overrides }}"
- name: Fail because the include returned
ansible.builtin.fail:
msg: The role accepted the input
rescue:
- name: The argument spec, not something else, stopped the run
ansible.builtin.assert:
that:
- "'missing required arguments' in (ansible_failed_result.msg | default(''))"
- "'pihole_web_password' in (ansible_failed_result.msg | default(''))"
fail_msg: "Got: {{ ansible_failed_result.msg | default('') }}"
quiet: true
- name: Run each failing case
ansible.builtin.include_tasks: tasks/failed_run.yml
loop_control:
loop_var: pihole_test_case
label: "{{ pihole_test_case.name }}"
loop:
# no interface key: this also fails if a role default for the interface is reintroduced
- name: interface-unset
overrides:
config:
dns: {upstreams: [9.9.9.9]}
webserver: {api: {password: molecule-test-password}}
expect: config.dns.interface is empty
- name: upstreams-empty
overrides:
config:
dns: {interface: molecule0, upstreams: []}
webserver: {api: {password: molecule-test-password}}
expect: upstreams must be a non-empty list
- name: password-not-string
overrides:
config:
dns: {interface: molecule0}
webserver: {api: {password: 12345}}
expect: password must be a string
- name: password-placeholder
overrides:
config:
dns: {interface: molecule0}
webserver: {api: {password: changetoyourownpassword}}
expect: placeholder changetoyourownpassword