From 5540f7fda5312db691446c55c187a57a52f3477a Mon Sep 17 00:00:00 2001 From: Matt Crees Date: Tue, 21 May 2024 17:16:05 +0100 Subject: [PATCH] Remove symlinks for molecule tests We are seeing failures with newer versions of Ansible due to the presence of symlinks. For example: ``` error: can't copy 'ansible/roles/mrlesmithjr.manage_lvm/molecule/kvm/tests': doesn't exist or not a regular file ``` --- molecule/kvm/tests | 1 - molecule/kvm/tests/conftest.py | 20 ++++++++++++++++ molecule/kvm/tests/test_default.py | 33 ++++++++++++++++++++++++++ molecule/kvmonlyvg/tests/conftest.py | 21 +++++++++++++++- molecule/kvmsinglelv/tests/conftest.py | 21 +++++++++++++++- 5 files changed, 93 insertions(+), 3 deletions(-) delete mode 120000 molecule/kvm/tests create mode 100644 molecule/kvm/tests/conftest.py create mode 100644 molecule/kvm/tests/test_default.py mode change 120000 => 100644 molecule/kvmonlyvg/tests/conftest.py mode change 120000 => 100644 molecule/kvmsinglelv/tests/conftest.py diff --git a/molecule/kvm/tests b/molecule/kvm/tests deleted file mode 120000 index 5456f39..0000000 --- a/molecule/kvm/tests +++ /dev/null @@ -1 +0,0 @@ -../default/tests \ No newline at end of file diff --git a/molecule/kvm/tests/conftest.py b/molecule/kvm/tests/conftest.py new file mode 100644 index 0000000..ba0f1e8 --- /dev/null +++ b/molecule/kvm/tests/conftest.py @@ -0,0 +1,20 @@ +"""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/kvm/tests/test_default.py b/molecule/kvm/tests/test_default.py new file mode 100644 index 0000000..29437c4 --- /dev/null +++ b/molecule/kvm/tests/test_default.py @@ -0,0 +1,33 @@ +"""Role testing files using testinfra.""" + + +def test_lvm_package_shall_be_installed(host): + assert host.package("lvm2").is_installed + + +def test_non_persistent_volume_group_is_created(host): + command = """sudo vgdisplay | grep -c 'my_vg'""" + cmd = host.run(command) + assert "1" in cmd.stdout + + +def test_mylv_logical_volume_is_created(host): + command = """sudo lvs -o lv_name my_vg --separator='|' --noheadings \ + | grep -c 'my_lv'""" + cmd = host.run(command) + assert int(cmd.stdout.rstrip()) >= 1 + + +def test_mylv_logical_volume2_is_created(host): + command = """sudo lvs -o lv_name my_vg --separator='|' --noheadings \ + | grep -c 'my_lw'""" + cmd = host.run(command) + assert int(cmd.stdout.rstrip()) >= 1 + + +def test_volume_is_mounted(host): + host.file("/var/lib/mountpoint").mode == 0o731 + + +def test_volume2_is_mounted(host): + host.file("/var/lib/mountpoint2").mode == 0o731 diff --git a/molecule/kvmonlyvg/tests/conftest.py b/molecule/kvmonlyvg/tests/conftest.py deleted file mode 120000 index add822b..0000000 --- a/molecule/kvmonlyvg/tests/conftest.py +++ /dev/null @@ -1 +0,0 @@ -../../default/tests/conftest.py \ No newline at end of file diff --git a/molecule/kvmonlyvg/tests/conftest.py b/molecule/kvmonlyvg/tests/conftest.py new file mode 100644 index 0000000..ba0f1e8 --- /dev/null +++ b/molecule/kvmonlyvg/tests/conftest.py @@ -0,0 +1,20 @@ +"""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/kvmsinglelv/tests/conftest.py b/molecule/kvmsinglelv/tests/conftest.py deleted file mode 120000 index add822b..0000000 --- a/molecule/kvmsinglelv/tests/conftest.py +++ /dev/null @@ -1 +0,0 @@ -../../default/tests/conftest.py \ No newline at end of file diff --git a/molecule/kvmsinglelv/tests/conftest.py b/molecule/kvmsinglelv/tests/conftest.py new file mode 100644 index 0000000..ba0f1e8 --- /dev/null +++ b/molecule/kvmsinglelv/tests/conftest.py @@ -0,0 +1,20 @@ +"""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 + )