--- - name: Verify the converged runner hosts: all gather_facts: false tasks: - name: Read config.yaml ansible.builtin.slurp: src: /var/lib/gitea-runner/config.yaml register: gitea_runner_test_config_raw - name: Parse config.yaml ansible.builtin.set_fact: gitea_runner_test_config: "{{ gitea_runner_test_config_raw.content | b64decode | from_yaml }}" # Only keys gitea-runner reads: an executor, engine, image, workdir or auto_remove key # would be ignored by the runner and give a false picture. - name: Config.yaml holds the inputs and nothing else ansible.builtin.assert: that: - gitea_runner_test_config.keys() | sort == ['container', 'log', 'runner'] - "gitea_runner_test_config.log == {'level': 'debug'}" - gitea_runner_test_config.runner.keys() | sort == ['capacity', 'labels'] - gitea_runner_test_config.runner.capacity == 2 - gitea_runner_test_config.runner.labels == ['molecule-docker:docker://ubuntu:24.04', 'molecule-shell:host'] - gitea_runner_test_config.container.keys() | sort == ['docker_host', 'privileged', 'volumes'] - gitea_runner_test_config.container.privileged is false - gitea_runner_test_config.container.docker_host == '-' - gitea_runner_test_config.container.volumes == ['/cache:/cache'] quiet: true - name: Stat the registration file, the staged binary and the backup ansible.builtin.stat: path: "{{ gitea_runner_test_path }}" loop: - /var/lib/gitea-runner/.runner - /usr/local/bin/gitea-runner.new - /usr/local/bin/gitea-runner.bak loop_control: loop_var: gitea_runner_test_path register: gitea_runner_test_files - name: The registration is private to the runner user, and the update left nothing behind ansible.builtin.assert: that: - gitea_runner_test_files.results[0].stat.mode == '0600' - gitea_runner_test_files.results[0].stat.pw_name == 'gitea-runner' - not gitea_runner_test_files.results[1].stat.exists - not gitea_runner_test_files.results[2].stat.exists quiet: true - name: Ask the installed binary for its version ansible.builtin.command: argv: [/usr/local/bin/gitea-runner, --version] register: gitea_runner_test_version changed_when: false - name: Ask systemd about the runner ansible.builtin.systemd: name: gitea-runner register: gitea_runner_test_unit # /proc/ belongs to the user the process runs as. - name: Read the owner of the runner's main process ansible.builtin.stat: path: "/proc/{{ gitea_runner_test_unit.status.MainPID }}" register: gitea_runner_test_owner - name: The update swapped in 6.0.1 and the runner runs it as its own user ansible.builtin.assert: that: - "'v6.0.1' in gitea_runner_test_version.stdout" - gitea_runner_test_unit.status.ActiveState == 'active' - gitea_runner_test_unit.status.UnitFileState == 'enabled' - gitea_runner_test_unit.status.ExecStart is search('/usr/local/bin/gitea-runner daemon --config /var/lib/gitea-runner/config.yaml') - gitea_runner_test_owner.stat.pw_name == 'gitea-runner' quiet: true - name: Ask systemd about the prune timer ansible.builtin.systemd: name: gitea-runner-docker-prune.timer register: gitea_runner_test_timer - name: Read the prune service ansible.builtin.slurp: src: /etc/systemd/system/gitea-runner-docker-prune.service register: gitea_runner_test_prune_service - name: The prune timer is armed and the service carries the gate and the ages vars: gitea_runner_test_prune_text: "{{ gitea_runner_test_prune_service.content | b64decode }}" ansible.builtin.assert: that: - gitea_runner_test_timer.status.ActiveState == 'active' - gitea_runner_test_timer.status.UnitFileState == 'enabled' - "'ExecCondition=/usr/local/sbin/gitea-runner-docker-prune-gate 3600' in gitea_runner_test_prune_text" - "'container prune --force --filter until=24h' in gitea_runner_test_prune_text" - "'image prune --all --force --filter until=168h' in gitea_runner_test_prune_text" quiet: true # The prune gate script against a stub docker CLI: no container, a young one (a job may be # running), an old one (a leaked job container), and a failing docker ps. The stub answers # `ps -q` from a file and `inspect` with a start time it computes from the container id. - name: Verify the prune gate script hosts: all gather_facts: false tasks: - name: Stat the docker CLI ansible.builtin.stat: path: /usr/bin/docker register: gitea_runner_test_docker_cli - name: The image has no docker CLI the stub would replace ansible.builtin.assert: that: not gitea_runner_test_docker_cli.stat.exists fail_msg: /usr/bin/docker exists in the test image; this play would overwrite it. quiet: true - name: Write the stub docker CLI ansible.builtin.copy: dest: /usr/bin/docker mode: "0755" content: | #!/bin/sh case "$1" in ps) [ -e /tmp/molecule-docker-fail ] && exit 1; cat /tmp/molecule-docker-ids ;; inspect) case "$4" in young) date -u +%Y-%m-%dT%H:%M:%SZ ;; old) date -u -d '2 hours ago' +%Y-%m-%dT%H:%M:%SZ ;; *) exit 1 ;; esac ;; *) exit 1 ;; esac - name: Run each gate case ansible.builtin.shell: cmd: | rm -f /tmp/molecule-docker-fail printf '%s' '{{ gitea_runner_test_case.ids }}' > /tmp/molecule-docker-ids {{ 'touch /tmp/molecule-docker-fail' if gitea_runner_test_case.fail else ':' }} /usr/local/sbin/gitea-runner-docker-prune-gate 3600 loop: - {name: none, ids: "", fail: false, rc: 0, out: ""} - {name: young, ids: "young", fail: false, rc: 1, out: "skipping the prune"} - {name: old, ids: "old", fail: false, rc: 0, out: "ignoring container old"} - {name: ps-fails, ids: "", fail: true, rc: 1, out: "docker ps failed"} loop_control: loop_var: gitea_runner_test_case label: "{{ gitea_runner_test_case.name }}" register: gitea_runner_test_gate # The rc is the result under test; the next task asserts it. failed_when: false changed_when: false - name: The gate let the prune run only when no young container ran ansible.builtin.assert: that: - item.rc == item.gitea_runner_test_case.rc - item.gitea_runner_test_case.out in item.stdout fail_msg: "case {{ item.gitea_runner_test_case.name }}: rc {{ item.rc }}, output {{ item.stdout }}" quiet: true loop: "{{ gitea_runner_test_gate.results }}" loop_control: label: "{{ item.gitea_runner_test_case.name }}" - name: Remove the stub docker CLI and its files ansible.builtin.file: path: "{{ gitea_runner_test_path }}" state: absent loop: [/usr/bin/docker, /tmp/molecule-docker-ids, /tmp/molecule-docker-fail] loop_control: loop_var: gitea_runner_test_path # 6.0.2 has a wrong checksum file (prepare.yml): the download must fail before anything is # stopped, so the runner keeps running 6.0.1. - name: Verify a failed download leaves the runner running hosts: all gather_facts: false tasks: - name: Update to a release whose checksum does not match block: - name: Include the role with gitea_runner_version 6.0.2 ansible.builtin.include_role: name: mortis.kamino.gitea_runner vars: gitea_runner_version: 6.0.2 - name: Fail because the include returned ansible.builtin.fail: msg: The role accepted a binary with a wrong checksum rescue: - name: The staging failed, and nothing else failed first ansible.builtin.assert: that: - "'Staging gitea-runner 6.0.2 failed' in ansible_failed_result.msg" - "'still runs 6.0.1' in ansible_failed_result.msg" quiet: true - name: Ask the installed binary for its version after the failed update ansible.builtin.command: argv: [/usr/local/bin/gitea-runner, --version] register: gitea_runner_test_version changed_when: false - name: Ask systemd about the runner after the failed update ansible.builtin.systemd: name: gitea-runner register: gitea_runner_test_unit - name: Stat the staged binary after the failed update ansible.builtin.stat: path: /usr/local/bin/gitea-runner.new register: gitea_runner_test_staged - name: The runner still runs 6.0.1 and no staged file is left ansible.builtin.assert: that: - "'v6.0.1' in gitea_runner_test_version.stdout" - gitea_runner_test_unit.status.ActiveState == 'active' - not gitea_runner_test_staged.stat.exists quiet: true # Each input assert, through the main entry point. Every one runs in validate.yml, before any # change. - name: Verify the input asserts hosts: all gather_facts: false tasks: - name: Run each refused input ansible.builtin.include_tasks: tasks/refused_input.yml loop: - name: version with a leading v vars: {gitea_runner_version: v6.0.1} expect: plain release number - name: scope with two slashes vars: {gitea_runner_registration_scope: owner/repo/extra} expect: gitea_runner_registration_scope must be empty - name: prune schedule with a newline vars: {gitea_runner_docker_prune_schedule: "daily\nExecStartPre=/bin/false"} expect: plain OnCalendar value - name: prune age without a unit vars: {gitea_runner_docker_prune_image_age: "168"} expect: plain OnCalendar value - name: molecule tooling without a requirements file vars: {gitea_runner_molecule_enabled: true} expect: gitea_runner_collections_requirements_file must name loop_control: loop_var: gitea_runner_test_refused label: "{{ gitea_runner_test_refused.name }}" # The CI guard and the mint-before-drop order of a forced re-registration, through a child # ansible-playbook against this instance (see force_register.yml). Neither case may touch the # registration or stop the runner. - name: Verify the forced re-registration guards hosts: all gather_facts: false vars: gitea_runner_test_inventory: "{{ lookup('ansible.builtin.env', 'MOLECULE_INVENTORY_FILE') }}" gitea_runner_test_env_blank: {GITHUB_ACTIONS: "", GITEA_ACTIONS: "", CI: "", ANSIBLE_NOCOLOR: "1"} tasks: - name: Require the molecule inventory ansible.builtin.assert: that: gitea_runner_test_inventory | length > 0 fail_msg: MOLECULE_INVENTORY_FILE is not set, so the child play cannot reach this instance. quiet: true - name: Force a re-registration under each CI variable, as a child process with only that one set delegate_to: localhost ansible.builtin.command: argv: - ansible-playbook - -i - "{{ gitea_runner_test_inventory }}" - --limit - "{{ inventory_hostname }}" - "{{ playbook_dir }}/force_register.yml" environment: "{{ gitea_runner_test_env_blank | combine({item: 'true'}) }}" loop: - GITHUB_ACTIONS - GITEA_ACTIONS - CI register: gitea_runner_test_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)" - "'Registration needs' not in (item.stdout ~ item.stderr)" quiet: true loop: "{{ gitea_runner_test_ci.results }}" loop_control: label: "{{ item.item }}" - name: Force a re-registration outside CI with no token host, as a child process delegate_to: localhost ansible.builtin.command: argv: - ansible-playbook - -i - "{{ gitea_runner_test_inventory }}" - --limit - "{{ inventory_hostname }}" - "{{ playbook_dir }}/force_register.yml" environment: "{{ gitea_runner_test_env_blank }}" register: gitea_runner_test_mint # The child is expected to fail (the mint assert fires). Its rc and output are asserted below. failed_when: false changed_when: false - name: The mint assert stopped it ansible.builtin.assert: that: - gitea_runner_test_mint.rc != 0 - "'Registration needs gitea_runner_token_host' in (gitea_runner_test_mint.stdout ~ gitea_runner_test_mint.stderr)" - "'refuses to run under CI' not in (gitea_runner_test_mint.stdout ~ gitea_runner_test_mint.stderr)" quiet: true - name: Read the registration after the forced runs ansible.builtin.slurp: src: /var/lib/gitea-runner/.runner register: gitea_runner_test_registration - name: Find registration backups ansible.builtin.find: paths: /var/lib/gitea-runner patterns: ".runner.bak.*" hidden: true register: gitea_runner_test_backups - name: Ask systemd about the runner after the forced runs ansible.builtin.systemd: name: gitea-runner register: gitea_runner_test_unit - name: The registration is untouched and the runner still runs ansible.builtin.assert: that: - (gitea_runner_test_registration.content | b64decode | from_json) == gitea_runner_test_stand_in quiet: true vars: gitea_runner_test_stand_in: {molecule: not a registration} - name: No backup was taken and the runner still runs ansible.builtin.assert: that: - gitea_runner_test_backups.files | length == 0 - gitea_runner_test_unit.status.ActiveState == 'active' quiet: true # The remove path. Last on purpose: it runs once, outside the idempotence step, and nothing runs # after verify except molecule's own cleanup and destroy of the container. The download URL still # points at a valid 6.0.1 release, so a role that went on to install after the removal would put # the binary back; its absence shows the role ended after the removal. - name: Verify the remove path hosts: all gather_facts: false tasks: - name: Run the role with the remove flags ansible.builtin.include_role: name: mortis.kamino.gitea_runner vars: gitea_runner_remove: true gitea_runner_remove_data: true - name: Stat what the removal deletes ansible.builtin.stat: path: "{{ gitea_runner_test_path }}" loop: - /etc/systemd/system/gitea-runner.service - /usr/local/bin/gitea-runner - /var/lib/gitea-runner - /etc/systemd/system/gitea-runner-docker-prune.service - /etc/systemd/system/gitea-runner-docker-prune.timer - /usr/local/sbin/gitea-runner-docker-prune-gate - /etc/systemd/system/multi-user.target.wants/gitea-runner.service loop_control: loop_var: gitea_runner_test_path register: gitea_runner_test_removed - name: Ask systemd whether the runner and the timer are still active ansible.builtin.systemd: name: "{{ gitea_runner_test_unit_name }}" loop: [gitea-runner, gitea-runner-docker-prune.timer] loop_control: loop_var: gitea_runner_test_unit_name register: gitea_runner_test_after # The module errors for a unit that no longer exists, which is also a pass. failed_when: false - name: Everything is gone, nothing was installed again, and nothing runs ansible.builtin.assert: that: - gitea_runner_test_removed.results | map(attribute='stat') | map(attribute='exists') | select | list | length == 0 - >- gitea_runner_test_after.results | map(attribute='status', default={}) | map(attribute='ActiveState', default='inactive') | select('equalto', 'active') | list | length == 0 quiet: true