From cc72967a758114b354e556ac3f6d13d77b913d33 Mon Sep 17 00:00:00 2001 From: Bertrand Lanson Date: Thu, 16 Mar 2023 22:18:20 +0100 Subject: [PATCH] first tests implementation --- molecule/default/molecule.yml | 2 +- molecule/default/tests/test_default.py | 20 +++++++-- molecule/with_custom_packages/converge.yml | 7 ++++ .../with_custom_packages/group_vars/all.yml | 12 ++++++ molecule/with_custom_packages/molecule.yml | 41 +++++++++++++++++++ .../with_custom_packages/requirements.yml | 4 ++ .../with_custom_packages/tests/conftest.py | 22 ++++++++++ .../tests/test_default.py | 25 +++++++++++ tasks/prerequisites.yml | 2 +- 9 files changed, 129 insertions(+), 6 deletions(-) create mode 100644 molecule/with_custom_packages/converge.yml create mode 100644 molecule/with_custom_packages/group_vars/all.yml create mode 100644 molecule/with_custom_packages/molecule.yml create mode 100644 molecule/with_custom_packages/requirements.yml create mode 100644 molecule/with_custom_packages/tests/conftest.py create mode 100644 molecule/with_custom_packages/tests/test_default.py diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml index 0a182d0..f9a8804 100644 --- a/molecule/default/molecule.yml +++ b/molecule/default/molecule.yml @@ -38,4 +38,4 @@ scenario: - idempotence - verify - cleanup - - destroy \ No newline at end of file + - destroy diff --git a/molecule/default/tests/test_default.py b/molecule/default/tests/test_default.py index 0cff669..4229110 100644 --- a/molecule/default/tests/test_default.py +++ b/molecule/default/tests/test_default.py @@ -3,8 +3,20 @@ def test_hosts_file(host): """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 - assert f.user == "root" - assert f.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') + print(pip_packages_list) + assert True == False diff --git a/molecule/with_custom_packages/converge.yml b/molecule/with_custom_packages/converge.yml new file mode 100644 index 0000000..7ac62d7 --- /dev/null +++ b/molecule/with_custom_packages/converge.yml @@ -0,0 +1,7 @@ +--- +- name: Converge + hosts: all + tasks: + - name: "Include ednxzu.manage_pip_packages" + ansible.builtin.include_role: + name: "ednxzu.manage_pip_packages" diff --git a/molecule/with_custom_packages/group_vars/all.yml b/molecule/with_custom_packages/group_vars/all.yml new file mode 100644 index 0000000..c6f3ab8 --- /dev/null +++ b/molecule/with_custom_packages/group_vars/all.yml @@ -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 diff --git a/molecule/with_custom_packages/molecule.yml b/molecule/with_custom_packages/molecule.yml new file mode 100644 index 0000000..14594d7 --- /dev/null +++ b/molecule/with_custom_packages/molecule.yml @@ -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 diff --git a/molecule/with_custom_packages/requirements.yml b/molecule/with_custom_packages/requirements.yml new file mode 100644 index 0000000..ca250b7 --- /dev/null +++ b/molecule/with_custom_packages/requirements.yml @@ -0,0 +1,4 @@ +--- +# requirements file for molecule +roles: + - name: ednxzu.manage_apt_packages diff --git a/molecule/with_custom_packages/tests/conftest.py b/molecule/with_custom_packages/tests/conftest.py new file mode 100644 index 0000000..f7ddb3f --- /dev/null +++ b/molecule/with_custom_packages/tests/conftest.py @@ -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 + ) diff --git a/molecule/with_custom_packages/tests/test_default.py b/molecule/with_custom_packages/tests/test_default.py new file mode 100644 index 0000000..99cf339 --- /dev/null +++ b/molecule/with_custom_packages/tests/test_default.py @@ -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) + diff --git a/tasks/prerequisites.yml b/tasks/prerequisites.yml index f878855..fac4de4 100644 --- a/tasks/prerequisites.yml +++ b/tasks/prerequisites.yml @@ -10,4 +10,4 @@ state: present - name: "{{ manage_pip_packages_pip3_package_name }}" version: latest - state: present \ No newline at end of file + state: present