--- # What the role is for, as far as stand-in packages can show it: the repository key and the # source line are in place and apt really used them, the packages are installed from that # repository, the unit is enabled and running, and the user joined the group. The second play # runs the removal, which would break the checks of the first, so it comes last. - name: Verify the install hosts: all vars: docker_verify_packages: [docker-ce, docker-ce-cli, containerd.io] tasks: - name: Read the source list and the two keys ansible.builtin.slurp: src: "{{ item }}" loop: - /etc/apt/sources.list.d/docker.list - /etc/apt/keyrings/docker.asc - /srv/docker-stub-repo/docker.asc register: docker_verify_files # amd64 because the Molecule container is x86_64: the role derives the architecture from the host now. - name: The source line carries the arch, the keyring and the release ansible.builtin.assert: that: - >- (docker_verify_files.results[0].content | b64decode) == 'deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.asc] file:///srv/docker-stub-repo ' ~ ansible_facts['distribution_release'] ~ ' stable\n' quiet: true - name: The keyring is the key the repository published ansible.builtin.assert: that: - docker_verify_files.results[1].content == docker_verify_files.results[2].content quiet: true - name: Stat the keyring ansible.builtin.stat: path: /etc/apt/keyrings/docker.asc register: docker_verify_keyring - name: The keyring is world-readable, as apt needs ansible.builtin.assert: that: docker_verify_keyring.stat.mode == "0644" quiet: true - name: Ask apt where docker-ce comes from ansible.builtin.command: apt-cache policy docker-ce register: docker_verify_policy changed_when: false - name: Apt installed docker-ce from the signed stand-in repository ansible.builtin.assert: that: - "'Installed: 0.0.1~molecule' in docker_verify_policy.stdout" - "'file:/srv/docker-stub-repo' in docker_verify_policy.stdout" quiet: true - name: Ask dpkg for the package states ansible.builtin.command: argv: [dpkg-query, --show, --showformat, "${db:Status-Status}\\n", "{{ item }}"] loop: "{{ docker_verify_packages }}" register: docker_verify_dpkg changed_when: false - name: Every package is installed ansible.builtin.assert: that: docker_verify_dpkg.results | map(attribute='stdout') | map('trim') | unique | list == ['installed'] quiet: true - name: Ask systemd about the unit ansible.builtin.systemd: name: docker.service register: docker_verify_unit check_mode: true - name: The unit is enabled and running ansible.builtin.assert: that: - docker_verify_unit.status.UnitFileState == "enabled" - docker_verify_unit.status.ActiveState == "active" quiet: true - name: Ask which groups the user is in ansible.builtin.command: id -nG moleculeuser register: docker_verify_groups changed_when: false - name: The user joined the docker group ansible.builtin.assert: that: "'docker' in docker_verify_groups.stdout.split()" quiet: true # The two units: installed, enabled, ordered as the role promises, and the shutdown unit running (its stop action runs only for an # active unit). Their behaviour at a real boot or shutdown cannot be shown here: the container has no dockerd, no NFS and no reboot, and the # shutdown script is not run because the container shares the runner's cgroup tree. - name: Verify the boot and shutdown units hosts: all gather_facts: false vars: docker_verify_units: [docker-start-missed, docker-stop-at-shutdown] tasks: - name: Stat the scripts and the unit files ansible.builtin.stat: path: "{{ item }}" loop: - /usr/local/sbin/docker-start-missed - /usr/local/sbin/docker-stop-at-shutdown - /etc/systemd/system/docker-start-missed.service - /etc/systemd/system/docker-stop-at-shutdown.service register: docker_verify_unit_files - name: The scripts are executable and the unit files are not ansible.builtin.assert: that: - docker_verify_unit_files.results | map(attribute='stat.exists') | unique | list == [true] - docker_verify_unit_files.results[0].stat.mode == "0755" - docker_verify_unit_files.results[1].stat.mode == "0755" - docker_verify_unit_files.results[2].stat.mode == "0644" - docker_verify_unit_files.results[3].stat.mode == "0644" quiet: true - name: Ask systemd about the units ansible.builtin.command: argv: - systemctl - show - --property=UnitFileState,ActiveState,After,Before - "{{ item }}.service" loop: "{{ docker_verify_units }}" register: docker_verify_unit_show changed_when: false - name: Both units are enabled ansible.builtin.assert: that: "'UnitFileState=enabled' in item.stdout" quiet: true loop: "{{ docker_verify_unit_show.results }}" - name: The boot unit starts after docker, the shutdown unit stops before it ansible.builtin.assert: that: - "'docker.service' in (docker_verify_unit_show.results[0].stdout | regex_search('After=.*'))" - "'docker.service' in (docker_verify_unit_show.results[1].stdout | regex_search('Before=.*'))" - "'network-online.target' in (docker_verify_unit_show.results[1].stdout | regex_search('After=.*'))" - "'remote-fs.target' in (docker_verify_unit_show.results[1].stdout | regex_search('After=.*'))" quiet: true - name: The shutdown unit is active, which is what lets its stop action run ansible.builtin.assert: that: "'ActiveState=active' in docker_verify_unit_show.results[1].stdout" quiet: true - name: Read the shutdown unit ansible.builtin.slurp: src: /etc/systemd/system/docker-stop-at-shutdown.service register: docker_verify_stop_unit - name: The timeout the converge set reaches the unit ansible.builtin.assert: that: - "'Environment=DOCKER_STOP_TIMEOUT=20' in (docker_verify_stop_unit.content | b64decode)" - "'TimeoutStopSec=50' in (docker_verify_stop_unit.content | b64decode)" quiet: true - name: The start script refuses an option it does not know ansible.builtin.command: /usr/local/sbin/docker-start-missed --no-such-option register: docker_verify_start_run changed_when: false failed_when: docker_verify_start_run.rc != 2 # Switching an input back to false removes that unit and script and leaves the other alone. - name: Verify that a unit is removed when its input is false hosts: all vars: docker_gpg_url: file:///srv/docker-stub-repo/docker.asc docker_repo_base_url: file:///srv/docker-stub-repo docker_users: [moleculeuser] docker_start_missed: false docker_stop_at_shutdown: true docker_stop_at_shutdown_timeout: 20 tasks: - name: Apply the role with the boot unit off ansible.builtin.include_role: name: mortis.kamino.docker - name: Stat the boot unit's files and the shutdown unit's script ansible.builtin.stat: path: "{{ item }}" loop: - /usr/local/sbin/docker-start-missed - /etc/systemd/system/docker-start-missed.service - /usr/local/sbin/docker-stop-at-shutdown register: docker_verify_off - name: The boot unit is gone and the shutdown unit is still there ansible.builtin.assert: that: docker_verify_off.results | map(attribute='stat.exists') | list == [false, false, true] quiet: true - name: Apply the role again with both inputs on ansible.builtin.include_role: name: mortis.kamino.docker vars: docker_start_missed: true - name: Verify the daemon settings hosts: all gather_facts: false vars: docker_verify_expected: live-restore: true log-driver: local log-opts: max-file: "3" max-size: 10m max-concurrent-downloads: 3 tasks: - name: Read and stat daemon.json ansible.builtin.slurp: src: /etc/docker/daemon.json register: docker_verify_daemon - name: Stat daemon.json ansible.builtin.stat: path: /etc/docker/daemon.json register: docker_verify_daemon_stat - name: The file holds exactly the keys the converge set, root-owned with mode 0644 ansible.builtin.assert: that: - docker_verify_daemon.content | b64decode | from_json == docker_verify_expected - docker_verify_daemon_stat.stat.mode == '0644' - docker_verify_daemon_stat.stat.pw_name == 'root' quiet: true # The stand-in docker.service runs for real, so a restart shows in its start time: a changed file with docker_daemon_restart false leaves # it alone, and with true restarts it. - name: Verify that dockerd restarts only when asked hosts: all vars: docker_gpg_url: file:///srv/docker-stub-repo/docker.asc docker_repo_base_url: file:///srv/docker-stub-repo docker_users: [moleculeuser] docker_start_missed: true docker_stop_at_shutdown: true docker_stop_at_shutdown_timeout: 20 docker_log_driver: local docker_log_max_size: 10m docker_log_max_file: "3" docker_live_restore: true tasks: - name: Read the start time of the service ansible.builtin.command: systemctl show -p ActiveEnterTimestampMonotonic docker.service # noqa: command-instead-of-module register: docker_verify_before changed_when: false - name: Apply a changed setting with docker_daemon_restart false ansible.builtin.include_role: name: mortis.kamino.docker vars: docker_daemon_options: {max-concurrent-downloads: 5} docker_daemon_restart: false - name: Read the start time again ansible.builtin.command: systemctl show -p ActiveEnterTimestampMonotonic docker.service # noqa: command-instead-of-module register: docker_verify_kept changed_when: false - name: Apply another change with docker_daemon_restart true ansible.builtin.include_role: name: mortis.kamino.docker vars: docker_daemon_options: {max-concurrent-downloads: 6} docker_daemon_restart: true - name: Read the start time a third time ansible.builtin.command: systemctl show -p ActiveEnterTimestampMonotonic docker.service # noqa: command-instead-of-module register: docker_verify_restarted changed_when: false - name: Read daemon.json ansible.builtin.slurp: src: /etc/docker/daemon.json register: docker_verify_daemon_after - name: The first change left dockerd alone and the second restarted it ansible.builtin.assert: that: - docker_verify_kept.stdout == docker_verify_before.stdout - docker_verify_restarted.stdout != docker_verify_kept.stdout - (docker_verify_daemon_after.content | b64decode | from_json)['max-concurrent-downloads'] == 6 quiet: true # docker_remove_data is set, so this also covers the destructive path: the packages are # purged and /var/lib/docker, seeded in prepare.yml, is deleted. The removal stops the unit # before the package takes it away, so it can only run once; a second run would fail on the # missing unit, which is why it is not in the converge. - name: Verify the removal hosts: all gather_facts: false vars: docker_remove: true docker_remove_data: true docker_verify_packages: [docker-ce, docker-ce-cli, containerd.io] tasks: - name: Remove Docker with its data ansible.builtin.include_role: name: mortis.kamino.docker - name: Stat what the removal must delete ansible.builtin.stat: path: "{{ item }}" loop: - /etc/apt/sources.list.d/docker.list - /etc/apt/keyrings/docker.asc - /var/lib/docker - /usr/lib/systemd/system/docker.service - /usr/local/sbin/docker-start-missed - /usr/local/sbin/docker-stop-at-shutdown - /etc/systemd/system/docker-start-missed.service - /etc/systemd/system/docker-stop-at-shutdown.service register: docker_verify_gone - name: The source list, the keyring, the data, the unit and the two extra units are gone ansible.builtin.assert: that: docker_verify_gone.results | map(attribute='stat.exists') | unique | list == [false] quiet: true - name: Ask dpkg for the package states ansible.builtin.command: argv: [dpkg-query, --show, --showformat, "${db:Status-Status}\\n", "{{ item }}"] loop: "{{ docker_verify_packages }}" register: docker_verify_dpkg changed_when: false failed_when: false - name: No package is installed any more ansible.builtin.assert: that: "'installed' not in docker_verify_dpkg.results | map(attribute='stdout') | map('trim') | list" quiet: true