feat(tests): added custom tests for both vagrant and docker
All checks were successful
test / Linting (push) Successful in 10s

This commit is contained in:
Bertrand Lanson 2023-12-10 18:25:43 +01:00
parent f813944d74
commit 9b6bf1684e
12 changed files with 315 additions and 1 deletions

View File

@ -3,4 +3,3 @@
roles:
- name: ednxzu.manage_repositories
- name: ednxzu.manage_apt_packages
- name: ednxzu.install_docker

View File

@ -0,0 +1,8 @@
---
- name: Converge
hosts: all
become: true
tasks:
- name: "Include ednxzu.docker_systemd_service"
ansible.builtin.include_role:
name: "ednxzu.docker_systemd_service"

View File

@ -0,0 +1,19 @@
---
docker_systemd_service_container_name: "nginx"
docker_systemd_service_image: nginx
docker_systemd_service_container_env:
TEST_ENV: test
docker_systemd_service_container_pull_image: false
docker_systemd_service_container_pull_force_source: false
docker_systemd_service_flags:
- privileged
- network: host
- cap-add:
- NET_ADMIN
docker_systemd_service_container_cmd: []
docker_systemd_service_name: "{{ docker_systemd_service_container_name }}_container"
docker_systemd_service_systemd_options: []
docker_systemd_service_enabled: true
docker_systemd_service_masked: false
docker_systemd_service_state: stopped
docker_systemd_service_restart: false

View File

@ -0,0 +1,37 @@
---
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: ansible
scenario:
name: with_custom_flags
test_sequence:
- dependency
- cleanup
- destroy
- syntax
- create
- prepare
- converge
- idempotence
- verify
- cleanup
- destroy

View File

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

View File

@ -0,0 +1,84 @@
---
- name: Verify
hosts: all
gather_facts: true
become: true
tasks:
- name: "Test: file /etc/default/nginx"
block:
- name: "Stat file /etc/default/nginx"
ansible.builtin.stat:
path: "/etc/default/nginx"
register: stat_etc_default_nginx
- name: "Slurp file /etc/default/nginx"
ansible.builtin.slurp:
src: "/etc/default/nginx"
register: slurp_etc_default_nginx
- name: "Verify file /etc/default/nginx"
vars:
nginx_expected_env_file: |
TEST_ENV=test
ansible.builtin.assert:
that:
- stat_etc_default_nginx.stat.exists
- stat_etc_default_nginx.stat.isreg
- stat_etc_default_nginx.stat.pw_name == 'root'
- stat_etc_default_nginx.stat.gr_name == 'root'
- stat_etc_default_nginx.stat.mode == '0600'
- (slurp_etc_default_nginx.content|b64decode) == nginx_expected_env_file
- name: "Test: service nginx_container"
block:
- name: "Get service nginx_container"
ansible.builtin.service_facts:
- name: "Stat file /etc/systemd/system/nginx_container.service"
ansible.builtin.stat:
path: "/etc/systemd/system/nginx_container.service"
register: stat_etc_systemd_system_nginx_container_service
- name: "Slurp file /etc/systemd/system/nginx_container.service"
ansible.builtin.slurp:
src: "/etc/systemd/system/nginx_container.service"
register: slurp_etc_systemd_system_nginx_container_service
- name: "Verify service nginx_container"
vars:
nginx_expected_service_file: |
# Ansible managed: Do NOT edit this file manually!
[Unit]
After=docker.service
PartOf=docker.service
Requires=docker.service
[Service]
EnvironmentFile=/etc/default/nginx
ExecStartPre=-/usr/bin/docker rm -f nginx
ExecStart=/usr/bin/docker run --name nginx \
--rm \
--env-file /etc/default/nginx \
--privileged \
--network "host" \
--cap-add "NET_ADMIN" \
nginx
ExecStop=/usr/bin/docker stop nginx
SyslogIdentifier=nginx
Restart=always
RestartSec=10s
[Install]
WantedBy=docker.service
ansible.builtin.assert:
that:
- stat_etc_systemd_system_nginx_container_service.stat.exists
- stat_etc_systemd_system_nginx_container_service.stat.isreg
- stat_etc_systemd_system_nginx_container_service.stat.pw_name == 'root'
- stat_etc_systemd_system_nginx_container_service.stat.gr_name == 'root'
- stat_etc_systemd_system_nginx_container_service.stat.mode == '0644'
- (slurp_etc_systemd_system_nginx_container_service.content|b64decode) == nginx_expected_service_file
- ansible_facts.services['nginx_container.service'] is defined
- ansible_facts.services['nginx_container.service']['source'] == 'systemd'
- ansible_facts.services['nginx_container.service']['state'] == 'inactive'
- ansible_facts.services['nginx_container.service']['status'] == 'enabled'

View File

@ -0,0 +1,8 @@
---
- name: Converge
hosts: all
become: true
tasks:
- name: "Include ednxzu.docker_systemd_service"
ansible.builtin.include_role:
name: "ednxzu.docker_systemd_service"

View File

@ -0,0 +1,19 @@
---
docker_systemd_service_container_name: "nginx"
docker_systemd_service_image: nginx
docker_systemd_service_container_env:
TEST_ENV: test
docker_systemd_service_container_pull_image: true
docker_systemd_service_container_pull_force_source: true
docker_systemd_service_flags:
- privileged
- network: host
- cap-add:
- NET_ADMIN
docker_systemd_service_container_cmd: []
docker_systemd_service_name: "{{ docker_systemd_service_container_name }}_container"
docker_systemd_service_systemd_options: []
docker_systemd_service_enabled: true
docker_systemd_service_masked: false
docker_systemd_service_state: started
docker_systemd_service_restart: true

View File

@ -0,0 +1,35 @@
---
dependency:
name: galaxy
options:
requirements-file: ./requirements.yml
driver:
name: vagrant
provider:
name: libvirt
platforms:
- name: instance
box: generic/${MOLECULE_TEST_OS}
cpus: 4
memory: 4096
provisioner:
name: ansible
config_options:
defaults:
remote_tmp: /tmp/.ansible
verifier:
name: ansible
scenario:
name: with_custom_flags_vagrant
test_sequence:
- dependency
- cleanup
- destroy
- syntax
- create
- prepare
- converge
- idempotence
- verify
- cleanup
- destroy

View File

@ -0,0 +1,10 @@
---
- name: Prepare
hosts: all
become: true
tasks:
- name: "Install docker"
ansible.builtin.include_role:
name: ednxzu.install_docker
vars:
install_docker_python_packages: true

View File

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

View File

@ -0,0 +1,84 @@
---
- name: Verify
hosts: all
gather_facts: true
become: true
tasks:
- name: "Test: file /etc/default/nginx"
block:
- name: "Stat file /etc/default/nginx"
ansible.builtin.stat:
path: "/etc/default/nginx"
register: stat_etc_default_nginx
- name: "Slurp file /etc/default/nginx"
ansible.builtin.slurp:
src: "/etc/default/nginx"
register: slurp_etc_default_nginx
- name: "Verify file /etc/default/nginx"
vars:
nginx_expected_env_file: |
TEST_ENV=test
ansible.builtin.assert:
that:
- stat_etc_default_nginx.stat.exists
- stat_etc_default_nginx.stat.isreg
- stat_etc_default_nginx.stat.pw_name == 'root'
- stat_etc_default_nginx.stat.gr_name == 'root'
- stat_etc_default_nginx.stat.mode == '0600'
- (slurp_etc_default_nginx.content|b64decode) == nginx_expected_env_file
- name: "Test: service nginx_container"
block:
- name: "Get service nginx_container"
ansible.builtin.service_facts:
- name: "Stat file /etc/systemd/system/nginx_container.service"
ansible.builtin.stat:
path: "/etc/systemd/system/nginx_container.service"
register: stat_etc_systemd_system_nginx_container_service
- name: "Slurp file /etc/systemd/system/nginx_container.service"
ansible.builtin.slurp:
src: "/etc/systemd/system/nginx_container.service"
register: slurp_etc_systemd_system_nginx_container_service
- name: "Verify service nginx_container"
vars:
nginx_expected_service_file: |
# Ansible managed: Do NOT edit this file manually!
[Unit]
After=docker.service
PartOf=docker.service
Requires=docker.service
[Service]
EnvironmentFile=/etc/default/nginx
ExecStartPre=-/usr/bin/docker rm -f nginx
ExecStart=/usr/bin/docker run --name nginx \
--rm \
--env-file /etc/default/nginx \
--privileged \
--network "host" \
--cap-add "NET_ADMIN" \
nginx
ExecStop=/usr/bin/docker stop nginx
SyslogIdentifier=nginx
Restart=always
RestartSec=10s
[Install]
WantedBy=docker.service
ansible.builtin.assert:
that:
- stat_etc_systemd_system_nginx_container_service.stat.exists
- stat_etc_systemd_system_nginx_container_service.stat.isreg
- stat_etc_systemd_system_nginx_container_service.stat.pw_name == 'root'
- stat_etc_systemd_system_nginx_container_service.stat.gr_name == 'root'
- stat_etc_systemd_system_nginx_container_service.stat.mode == '0644'
- (slurp_etc_systemd_system_nginx_container_service.content|b64decode) == nginx_expected_service_file
- ansible_facts.services['nginx_container.service'] is defined
- ansible_facts.services['nginx_container.service']['source'] == 'systemd'
- ansible_facts.services['nginx_container.service']['state'] == 'running'
- ansible_facts.services['nginx_container.service']['status'] == 'enabled'