diff --git a/roles/pihole/molecule/default/converge.yml b/roles/pihole/molecule/default/converge.yml new file mode 100644 index 0000000..6d24f61 --- /dev/null +++ b/roles/pihole/molecule/default/converge.yml @@ -0,0 +1,11 @@ +--- +# The password is overridden, but pihole_web_password is still required: pihole_default +# references it. +- name: Converge + hosts: all + gather_facts: true + vars: + pihole_web_password: not-used + pihole_overrides: "{{ pihole_test_overrides }}" + roles: + - role: mortis.kamino.pihole diff --git a/roles/pihole/molecule/default/molecule.yml b/roles/pihole/molecule/default/molecule.yml new file mode 100644 index 0000000..2e7d148 --- /dev/null +++ b/roles/pihole/molecule/default/molecule.yml @@ -0,0 +1,79 @@ +--- +# Runs on the repository's own runner (label kamino-molecule), on both declared platforms: +# molecule-ubuntu2404 (MOLECULE_IMAGE) and molecule-ubuntu2604 (MOLECULE_IMAGE_2604), the pins in +# .gitea/workflows/molecule.yml. Needs outbound network for the apt packages. The upstream +# Pi-hole repository is replaced by a local stub whose installer only logs its arguments +# (prepare.yml), so the scenario tests the role's own logic, not Pi-hole itself. +# The containers run systemd as PID 1 with a writable cgroup; no privileged mode. +driver: + name: docker +platforms: + - name: ${KAMINO_RUN_ID:-local}-pihole-noble + labels: + net.rkdr.mortis.molecule-run: "${KAMINO_RUN_ID:-local}" + image: ${MOLECULE_IMAGE} + pre_build_image: true + pull: true + command: /sbin/init + cgroupns_mode: host + volumes: + - /sys/fs/cgroup:/sys/fs/cgroup:rw + tmpfs: + - /run + - /run/lock + security_opts: + - apparmor=unconfined + - name: ${KAMINO_RUN_ID:-local}-pihole-resolute + labels: + net.rkdr.mortis.molecule-run: "${KAMINO_RUN_ID:-local}" + image: ${MOLECULE_IMAGE_2604} + pre_build_image: true + pull: true + command: /sbin/init + cgroupns_mode: host + volumes: + - /sys/fs/cgroup:/sys/fs/cgroup:rw + tmpfs: + - /run + - /run/lock + security_opts: + - apparmor=unconfined +provisioner: + name: ansible + # One source for every play, so the converge and the negative tests in verify.yml start from + # the same valid input. + inventory: + group_vars: + all: + pihole_test_overrides: + repo: + remote: file:///opt/pihole-stub.git + config: + dns: + # not eth0, so verify proves the variable reaches pihole.toml + interface: molecule0 + upstreams: + - 9.9.9.9 + - 149.112.112.112 + webserver: + api: + password: molecule-test-password +verifier: + name: ansible +scenario: + # molecule's default sequence minus both destroys (scripts/run_molecule_scenarios.sh removes the + # containers as soon as this scenario ends; after a local run, run `molecule destroy` yourself), + # with a check run before the converge (first-run preview) and after the idempotence run. + test_sequence: + - dependency + - cleanup + - syntax + - create + - prepare + - check + - converge + - idempotence + - check + - side_effect + - verify + - cleanup diff --git a/roles/pihole/molecule/default/prepare.yml b/roles/pihole/molecule/default/prepare.yml new file mode 100644 index 0000000..36b52ad --- /dev/null +++ b/roles/pihole/molecule/default/prepare.yml @@ -0,0 +1,36 @@ +--- +# The role clones the upstream Pi-hole repository and runs its installer, which needs a real +# DNS stack (pihole-FTL, port 53, the gravity database). A container cannot honestly test that, +# so the clone target is a local git repository whose installer is a stub that logs its +# arguments and creates the database file the role checks for an existing install. +- name: Prepare + hosts: all + gather_facts: false + tasks: + - name: Install git + ansible.builtin.apt: + name: git + state: present + update_cache: true + + - name: Build the stub upstream repository + ansible.builtin.shell: + cmd: | + set -e + rm -rf /opt/pihole-stub /opt/pihole-stub.git /var/log/pihole-stub-installer.log + mkdir -p "/opt/pihole-stub/automated install" + cat > "/opt/pihole-stub/automated install/basic-install.sh" <<'STUB' + #!/bin/sh + echo "$*" >> /var/log/pihole-stub-installer.log + mkdir -p /etc/pihole + touch /etc/pihole/pihole-FTL.db + STUB + chmod 0755 "/opt/pihole-stub/automated install/basic-install.sh" + cd /opt/pihole-stub + git init -q -b main + git -c user.name=molecule -c user.email=molecule@localhost add -A + git -c user.name=molecule -c user.email=molecule@localhost commit -q -m stub + git tag v6.4 + git clone -q --bare /opt/pihole-stub /opt/pihole-stub.git + executable: /bin/bash + changed_when: true diff --git a/roles/pihole/molecule/default/tasks/failed_run.yml b/roles/pihole/molecule/default/tasks/failed_run.yml new file mode 100644 index 0000000..3674357 --- /dev/null +++ b/roles/pihole/molecule/default/tasks/failed_run.yml @@ -0,0 +1,24 @@ +--- +# One failing run: runs the role with the inputs of the case and asserts that the expected +# message, not something else, stopped it. Input: pihole_test_case (name, overrides, expect); +# the web password is always set here, so the validation under test is the first to fail. +- name: "Run the role, case {{ pihole_test_case.name }}" + block: + - name: "Include the role, case {{ pihole_test_case.name }}" + ansible.builtin.include_role: + name: mortis.kamino.pihole + vars: + pihole_web_password: not-used + pihole_overrides: "{{ pihole_test_case.overrides }}" + + # Only reached when the include returned. The message must not contain the expected + # text, so the rescue below cannot mistake it for the failure under test. + - name: "Fail because the include returned, case {{ pihole_test_case.name }}" + ansible.builtin.fail: + msg: The role accepted the input + rescue: + - name: "The expected failure stopped the run, case {{ pihole_test_case.name }}" + ansible.builtin.assert: + that: pihole_test_case.expect in (ansible_failed_result.msg | default('')) + fail_msg: "Expected '{{ pihole_test_case.expect }}', got: {{ ansible_failed_result.msg | default('') }}" + quiet: true diff --git a/roles/pihole/molecule/default/verify.yml b/roles/pihole/molecule/default/verify.yml new file mode 100644 index 0000000..0da56e7 --- /dev/null +++ b/roles/pihole/molecule/default/verify.yml @@ -0,0 +1,132 @@ +--- +# 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