From 95467d081de5ea11b8bc6dda240e786a63313455 Mon Sep 17 00:00:00 2001 From: Bertrand Lanson Date: Sat, 15 Apr 2023 14:50:25 +0200 Subject: [PATCH] added custom scenario, and some unit tests --- defaults/main.yml | 2 +- molecule/default/requirements.yml | 3 ++ molecule/default/tests/test_default.py | 16 ++++++-- molecule/with_custom_config/converge.yml | 7 ++++ .../group_vars/all.yml | 4 -- molecule/with_custom_config/molecule.yml | 41 +++++++++++++++++++ molecule/with_custom_config/requirements.yml | 3 ++ molecule/with_custom_config/tests/conftest.py | 22 ++++++++++ .../with_custom_config/tests/test_default.py | 18 ++++++++ 9 files changed, 107 insertions(+), 9 deletions(-) create mode 100644 molecule/default/requirements.yml create mode 100644 molecule/with_custom_config/converge.yml rename molecule/{default => with_custom_config}/group_vars/all.yml (88%) create mode 100644 molecule/with_custom_config/molecule.yml create mode 100644 molecule/with_custom_config/requirements.yml create mode 100644 molecule/with_custom_config/tests/conftest.py create mode 100644 molecule/with_custom_config/tests/test_default.py diff --git a/defaults/main.yml b/defaults/main.yml index fa07fa7..278146a 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -4,7 +4,7 @@ manage_netplan_config_file: /etc/netplan/ansible-config.yaml # this MUST be .yam manage_netplan_renderer: networkd # supported value is 'NetworkManager' or 'networkd' manage_netplan_remove_existing: false manage_netplan_search_domain: example.org -manage_netplan_install: false +manage_netplan_install: true manage_netplan_apply: false manage_netplan_configuration: {} # network: diff --git a/molecule/default/requirements.yml b/molecule/default/requirements.yml new file mode 100644 index 0000000..e9320f9 --- /dev/null +++ b/molecule/default/requirements.yml @@ -0,0 +1,3 @@ +--- +# requirements file for molecule +roles: [] diff --git a/molecule/default/tests/test_default.py b/molecule/default/tests/test_default.py index 0cff669..29799ac 100644 --- a/molecule/default/tests/test_default.py +++ b/molecule/default/tests/test_default.py @@ -3,8 +3,16 @@ 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_netplan_storage(host): + """Validate /etc/netplan directory.""" + etc_netplan = host.file("/etc/netplan") + assert etc_netplan.exists + assert etc_netplan.is_directory + assert etc_netplan.user == "root" + assert etc_netplan.group =="root" + assert etc_netplan.mode == 0o755 diff --git a/molecule/with_custom_config/converge.yml b/molecule/with_custom_config/converge.yml new file mode 100644 index 0000000..74b1609 --- /dev/null +++ b/molecule/with_custom_config/converge.yml @@ -0,0 +1,7 @@ +--- +- name: Converge + hosts: all + tasks: + - name: "Include ednxzu.manage_netplan" + ansible.builtin.include_role: + name: "ednxzu.manage_netplan" diff --git a/molecule/default/group_vars/all.yml b/molecule/with_custom_config/group_vars/all.yml similarity index 88% rename from molecule/default/group_vars/all.yml rename to molecule/with_custom_config/group_vars/all.yml index b2de2dc..e30cfde 100644 --- a/molecule/default/group_vars/all.yml +++ b/molecule/with_custom_config/group_vars/all.yml @@ -10,14 +10,10 @@ manage_netplan_configuration: version: 2 ethernets: eth1: - match: - macaddress: 0c:c4:7a:4d:50:a2 dhcp4: false link-local: [] set-name: eth1 eth2: - match: - macaddress: 0c:c4:7a:4d:50:a3 dhcp4: true mtu: 1500 link-local: [] diff --git a/molecule/with_custom_config/molecule.yml b/molecule/with_custom_config/molecule.yml new file mode 100644 index 0000000..0f4babb --- /dev/null +++ b/molecule/with_custom_config/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_config + test_sequence: + - dependency + - lint + - cleanup + - destroy + - syntax + - create + - prepare + - converge + - idempotence + - verify + - cleanup + - destroy diff --git a/molecule/with_custom_config/requirements.yml b/molecule/with_custom_config/requirements.yml new file mode 100644 index 0000000..e9320f9 --- /dev/null +++ b/molecule/with_custom_config/requirements.yml @@ -0,0 +1,3 @@ +--- +# requirements file for molecule +roles: [] diff --git a/molecule/with_custom_config/tests/conftest.py b/molecule/with_custom_config/tests/conftest.py new file mode 100644 index 0000000..f7ddb3f --- /dev/null +++ b/molecule/with_custom_config/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_config/tests/test_default.py b/molecule/with_custom_config/tests/test_default.py new file mode 100644 index 0000000..29799ac --- /dev/null +++ b/molecule/with_custom_config/tests/test_default.py @@ -0,0 +1,18 @@ +"""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_netplan_storage(host): + """Validate /etc/netplan directory.""" + etc_netplan = host.file("/etc/netplan") + assert etc_netplan.exists + assert etc_netplan.is_directory + assert etc_netplan.user == "root" + assert etc_netplan.group =="root" + assert etc_netplan.mode == 0o755