first tests implementation

This commit is contained in:
Bertrand Lanson 2023-03-16 22:18:20 +01:00
parent 9f85f58ac4
commit cc72967a75
9 changed files with 129 additions and 6 deletions

View File

@ -3,8 +3,20 @@
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_python_pip_packages_installed(host):
assert f.user == "root" """Validate python3 and pip are installed"""
assert f.group == "root" apt_package_python3 = host.package("python3")
apt_package_pip = host.package("python3-pip")
assert apt_package_python3.is_installed
assert apt_package_pip.is_installed
def test_packages_not_installed(host):
"""Validate vim is installed"""
pip_packages_list = host.pip_package.get_packages(pip_path='pip')
print(pip_packages_list)
assert True == False

View File

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

View File

@ -0,0 +1,12 @@
---
manage_pip_packages_install_prereqs: true
manage_pip_packages_list:
- name: docker
version_constraint: latest
state: present
- name: yamllint
version_constraint: '==1.24'
state: present
- name: vault-cli
version_constraint: '<3.1.0'
state: present

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_packages
test_sequence:
- dependency
- lint
- cleanup
- destroy
- syntax
- create
- prepare
- converge
- idempotence
- verify
- cleanup
- destroy

View File

@ -0,0 +1,4 @@
---
# requirements file for molecule
roles:
- name: ednxzu.manage_apt_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,25 @@
"""Role testing files using testinfra."""
import json
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_python_pip_packages_installed(host):
"""Validate python3 and pip are installed"""
apt_package_python3 = host.package("python3")
apt_package_pip = host.package("python3-pip")
assert apt_package_python3.is_installed
assert apt_package_pip.is_installed
def test_packages_not_installed(host):
"""Validate vim is installed"""
pip_packages_list = host.pip_package.get_packages(pip_path='pip')
assert pip_packages_list['pip']
assert pip_packages_list['docker']
assert pip_packages_list['zeubi']
print(pip_packages_list)