added a few tests, corrected various errors

This commit is contained in:
Bertrand Lanson 2023-04-09 23:28:29 +02:00
parent f18b313eba
commit 52eff080b4
13 changed files with 178 additions and 18 deletions

View File

@ -2,8 +2,9 @@
# defaults file for install_docker # defaults file for install_docker
install_docker_edition: ce # can be ce or ee (community or enterprise) install_docker_edition: ce # can be ce or ee (community or enterprise)
install_docker_start_service: true install_docker_start_service: true
install_docker_compose: true install_docker_compose: false
install_docker_compose_version: latest install_docker_compose_version: latest
install_docker_python_packages: true install_docker_python_packages: false
install_docker_python_packages_version: latest install_docker_python_packages_version: latest
install_docker_users: [] install_docker_users: []
install_docker_daemon_options: {}

View File

@ -1,2 +1,14 @@
--- ---
# handlers file for install_docker # handlers file for install_docker
- name: "Enable docker service"
ansible.builtin.service:
name: docker
enabled: true
listen: "systemctl-enable-docker"
- name: "Start docker service"
ansible.builtin.service:
name: docker
state: restarted
listen: "systemctl-restart-docker"
when: install_docker_start_service

View File

@ -3,8 +3,29 @@
def test_hosts_file(host): def test_hosts_file(host):
"""Validate /etc/hosts file.""" """Validate /etc/hosts file."""
f = host.file("/etc/hosts") etc_hosts = host.file("/etc/hosts")
assert etc_hosts.exists
assert etc_hosts.user == "root"
assert etc_hosts.group == "root"
assert f.exists def test_docker_service(host):
assert f.user == "root" """Validate docker service."""
assert f.group == "root" docker_service = host.service("docker.service")
assert docker_service.is_enabled
assert docker_service.is_running
assert docker_service.systemd_properties["Restart"] == "always"
assert docker_service.systemd_properties["FragmentPath"] == "/lib/systemd/system/docker.service"
def test_docker_daemon(host):
"""Validate /etc/docker/daemon.json file."""
docker_daemon_file = host.file("/etc/docker/daemon.json")
assert docker_daemon_file.exists
assert docker_daemon_file.user == "root"
assert docker_daemon_file.group =="root"
assert docker_daemon_file.mode == 0o644
assert docker_daemon_file.contains("{}")
def test_docker_interaction(host):
"""Validate interaction with docker."""
docker_ps = host.check_output("docker ps")
assert docker_ps == "CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES"

View File

@ -0,0 +1,7 @@
---
- name: Converge
hosts: all
tasks:
- name: "Include ednxzu.install_docker"
ansible.builtin.include_role:
name: "ednxzu.install_docker"

View File

@ -0,0 +1,9 @@
---
install_docker_edition: ce # can be ce or ee (community or enterprise)
install_docker_start_service: true
install_docker_compose: true
install_docker_compose_version: latest
install_docker_python_packages: true
install_docker_python_packages_version: latest
install_docker_users: []
install_docker_daemon_options: {}

View File

@ -0,0 +1,41 @@
---
dependency:
name: galaxy
options:
requirements-file: ./requirements.yml
driver:
name: docker
platforms:
- name: instance
image: geerlingguy/docker-${MOLECULE_TEST_OS}-ansible
command: ""
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup
cgroupns_mode: host
privileged: true
pre_build_image: true
provisioner:
name: ansible
config_options:
defaults:
remote_tmp: /tmp/.ansible
verifier:
name: testinfra
lint: |
yamllint -c .yamllint .
ansible-lint
scenario:
name: with_custom_config
test_sequence:
- dependency
- lint
- cleanup
- destroy
- syntax
- create
- prepare
- converge
- idempotence
- verify
- cleanup
- destroy

View File

@ -0,0 +1,6 @@
---
# requirements file for molecule
roles:
- name: ednxzu.manage_repositories
- name: ednxzu.manage_apt_packages
- name: ednxzu.manage_pip_packages

View File

@ -0,0 +1,22 @@
"""PyTest Fixtures."""
from __future__ import absolute_import
import os
import pytest
def pytest_runtest_setup(item):
"""Run tests only when under molecule with testinfra installed."""
try:
import testinfra
except ImportError:
pytest.skip("Test requires testinfra", allow_module_level=True)
if "MOLECULE_INVENTORY_FILE" in os.environ:
pytest.testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
os.environ["MOLECULE_INVENTORY_FILE"]
).get_hosts("all")
else:
pytest.skip(
"Test should run only from inside molecule.", allow_module_level=True
)

View File

@ -0,0 +1,41 @@
"""Role testing files using testinfra."""
def test_hosts_file(host):
"""Validate /etc/hosts file."""
etc_hosts = host.file("/etc/hosts")
assert etc_hosts.exists
assert etc_hosts.user == "root"
assert etc_hosts.group == "root"
def test_docker_service(host):
"""Validate docker service."""
docker_service = host.service("docker.service")
assert docker_service.is_enabled
assert docker_service.is_running
assert docker_service.systemd_properties["Restart"] == "always"
assert docker_service.systemd_properties["FragmentPath"] == "/lib/systemd/system/docker.service"
def test_docker_daemon(host):
"""Validate /etc/docker/daemon.json file."""
docker_daemon_file = host.file("/etc/docker/daemon.json")
assert docker_daemon_file.exists
assert docker_daemon_file.user == "root"
assert docker_daemon_file.group =="root"
assert docker_daemon_file.mode == 0o644
assert docker_daemon_file.contains("{}")
def test_docker_interaction(host):
"""Validate interaction with docker."""
docker_ps = host.check_output("docker ps")
assert docker_ps == "CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES"
def test_docker_compose(host):
"""Validate docker-compose installation"""
docker_compose_bin = host.file("/usr/local/bin/docker-compose")
docker_compose_version = host.check_output("docker-compose --version")
assert docker_compose_bin.exists
assert docker_compose_bin.user == "root"
assert docker_compose_bin.group == "root"
assert docker_compose_bin.mode == 0o755
assert docker_compose_version == "Docker Compose version "+ r'^v\d+\.\d+\.\d+$'

View File

@ -7,13 +7,13 @@
append: true append: true
loop: "{{ install_docker_users }}" loop: "{{ install_docker_users }}"
- name: "Enable docker service" - name: "Copy daemon.json template"
ansible.builtin.service: ansible.builtin.template:
name: docker src: daemon.json.j2
enabled: true dest: "{{ install_docker_daemon_dir }}/daemon.json"
owner: root
- name: "Start docker service" group: root
ansible.builtin.service: mode: '0644'
name: docker notify:
state: restarted - "systemctl-enable-docker"
when: install_docker_start_service - "systemctl-restart-docker"

View File

@ -9,7 +9,7 @@
register: _docker_compose_new_release register: _docker_compose_new_release
- name: "Check current compose version" - name: "Check current compose version"
command: "{{ install_docker_compose_path }} --version --short" ansible.builtin.command: "{{ install_docker_compose_path }} --version --short"
register: _docker_compose_old_release register: _docker_compose_old_release
check_mode: false check_mode: false
changed_when: false changed_when: false

View File

@ -20,4 +20,3 @@
owner: root owner: root
group: root group: root
mode: '0755' mode: '0755'

1
templates/daemon.json.j2 Normal file
View File

@ -0,0 +1 @@
{{ install_docker_daemon_options | to_nice_json }}