From d5c89c4efaec68a15483a6cafb3597ce957c37ae Mon Sep 17 00:00:00 2001 From: Bertrand Lanson Date: Fri, 26 May 2023 22:11:11 +0200 Subject: [PATCH] migrated tests to ansible from testinfra ahead of deprecation --- molecule/default/molecule.yml | 2 +- molecule/default/tests/conftest.py | 22 ---------- molecule/default/tests/test_default.py | 20 --------- molecule/default/verify.yml | 43 ++++++++++++++++++ molecule/with_custom_config/molecule.yml | 2 +- molecule/with_custom_config/tests/conftest.py | 22 ---------- .../with_custom_config/tests/test_default.py | 21 --------- molecule/with_custom_config/verify.yml | 44 +++++++++++++++++++ 8 files changed, 89 insertions(+), 87 deletions(-) delete mode 100644 molecule/default/tests/conftest.py delete mode 100644 molecule/default/tests/test_default.py create mode 100644 molecule/default/verify.yml delete mode 100644 molecule/with_custom_config/tests/conftest.py delete mode 100644 molecule/with_custom_config/tests/test_default.py create mode 100644 molecule/with_custom_config/verify.yml diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml index 7a62eb2..49efc7f 100644 --- a/molecule/default/molecule.yml +++ b/molecule/default/molecule.yml @@ -20,7 +20,7 @@ provisioner: defaults: remote_tmp: /tmp/.ansible verifier: - name: testinfra + name: ansible scenario: name: default test_sequence: diff --git a/molecule/default/tests/conftest.py b/molecule/default/tests/conftest.py deleted file mode 100644 index f7ddb3f..0000000 --- a/molecule/default/tests/conftest.py +++ /dev/null @@ -1,22 +0,0 @@ -"""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/default/tests/test_default.py b/molecule/default/tests/test_default.py deleted file mode 100644 index 969de0c..0000000 --- a/molecule/default/tests/test_default.py +++ /dev/null @@ -1,20 +0,0 @@ -"""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_resolv_conf_file(host): - """Validate resolv.conf file.""" - tmp_resolv_conf = host.file("/tmp/resolv.conf") - assert tmp_resolv_conf.exists - assert tmp_resolv_conf.user == "root" - assert tmp_resolv_conf.group == "root" - assert tmp_resolv_conf.mode == 0o644 - assert tmp_resolv_conf.contains("search local.lan") - assert tmp_resolv_conf.contains("nameserver 1.1.1.1") - assert tmp_resolv_conf.contains("nameserver 8.8.8.8") diff --git a/molecule/default/verify.yml b/molecule/default/verify.yml new file mode 100644 index 0000000..67fbbaf --- /dev/null +++ b/molecule/default/verify.yml @@ -0,0 +1,43 @@ +--- +- name: Verify + hosts: all + gather_facts: false + tasks: + - name: "Test: file /etc/hosts" + block: + - name: "Stat file /etc/hosts" + ansible.builtin.stat: + path: "/etc/hosts" + register: stat_etc_hosts + + - name: "Verify file /etc/hosts" + ansible.builtin.assert: + that: + - stat_etc_hosts.stat.exists + - stat_etc_hosts.stat.isreg + - stat_etc_hosts.stat.pw_name == 'root' + - stat_etc_hosts.stat.gr_name == 'root' + + - name: "Test: file /tmp/resolv.conf" + block: + - name: "Stat file /tmp/resolv.conf" + ansible.builtin.stat: + path: "/tmp/resolv.conf" + register: stat_tmp_resolv_conf + + - name: "Slurp file /tmp/resolv.conf" + ansible.builtin.slurp: + src: "/tmp/resolv.conf" + register: slurp_tmp_resolv_conf + + - name: "Verify file /tmp/resolv.conf" + ansible.builtin.assert: + that: + - stat_tmp_resolv_conf.stat.exists + - stat_tmp_resolv_conf.stat.isreg + - stat_tmp_resolv_conf.stat.pw_name == 'root' + - stat_tmp_resolv_conf.stat.gr_name == 'root' + - stat_tmp_resolv_conf.stat.mode == '0644' + - "'search local.lan' in (slurp_tmp_resolv_conf.content|b64decode)" + - "'nameserver 1.1.1.1' in (slurp_tmp_resolv_conf.content|b64decode)" + - "'nameserver 8.8.8.8' in (slurp_tmp_resolv_conf.content|b64decode)" diff --git a/molecule/with_custom_config/molecule.yml b/molecule/with_custom_config/molecule.yml index 6132acb..4df62e9 100644 --- a/molecule/with_custom_config/molecule.yml +++ b/molecule/with_custom_config/molecule.yml @@ -20,7 +20,7 @@ provisioner: defaults: remote_tmp: /tmp/.ansible verifier: - name: testinfra + name: ansible scenario: name: with_custom_config test_sequence: diff --git a/molecule/with_custom_config/tests/conftest.py b/molecule/with_custom_config/tests/conftest.py deleted file mode 100644 index f7ddb3f..0000000 --- a/molecule/with_custom_config/tests/conftest.py +++ /dev/null @@ -1,22 +0,0 @@ -"""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 deleted file mode 100644 index 7b2859e..0000000 --- a/molecule/with_custom_config/tests/test_default.py +++ /dev/null @@ -1,21 +0,0 @@ -"""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_resolv_conf_file(host): - """Validate resolv.conf file.""" - tmp_resolv_conf = host.file("/tmp/resolv.conf") - assert tmp_resolv_conf.exists - assert tmp_resolv_conf.user == "root" - assert tmp_resolv_conf.group == "root" - assert tmp_resolv_conf.mode == 0o644 - assert tmp_resolv_conf.contains("search example.org az1.example.org") - assert tmp_resolv_conf.contains("nameserver 10.1.20.53") - assert tmp_resolv_conf.contains("nameserver 10.1.20.54") - assert tmp_resolv_conf.contains("options edns0 rotate") diff --git a/molecule/with_custom_config/verify.yml b/molecule/with_custom_config/verify.yml new file mode 100644 index 0000000..a839168 --- /dev/null +++ b/molecule/with_custom_config/verify.yml @@ -0,0 +1,44 @@ +--- +- name: Verify + hosts: all + gather_facts: false + tasks: + - name: "Test: file /etc/hosts" + block: + - name: "Stat file /etc/hosts" + ansible.builtin.stat: + path: "/etc/hosts" + register: stat_etc_hosts + + - name: "Verify file /etc/hosts" + ansible.builtin.assert: + that: + - stat_etc_hosts.stat.exists + - stat_etc_hosts.stat.isreg + - stat_etc_hosts.stat.pw_name == 'root' + - stat_etc_hosts.stat.gr_name == 'root' + + - name: "Test: file /tmp/resolv.conf" + block: + - name: "Stat file /tmp/resolv.conf" + ansible.builtin.stat: + path: "/tmp/resolv.conf" + register: stat_tmp_resolv_conf + + - name: "Slurp file /tmp/resolv.conf" + ansible.builtin.slurp: + src: "/tmp/resolv.conf" + register: slurp_tmp_resolv_conf + + - name: "Verify file /tmp/resolv.conf" + ansible.builtin.assert: + that: + - stat_tmp_resolv_conf.stat.exists + - stat_tmp_resolv_conf.stat.isreg + - stat_tmp_resolv_conf.stat.pw_name == 'root' + - stat_tmp_resolv_conf.stat.gr_name == 'root' + - stat_tmp_resolv_conf.stat.mode == '0644' + - "'search example.org az1.example.org' in (slurp_tmp_resolv_conf.content|b64decode)" + - "'nameserver 10.1.20.53' in (slurp_tmp_resolv_conf.content|b64decode)" + - "'nameserver 10.1.20.54' in (slurp_tmp_resolv_conf.content|b64decode)" + - "'options edns0 rotate' in (slurp_tmp_resolv_conf.content|b64decode)"