migrated tests to ansible from testinfra ahead of deprecation

This commit is contained in:
Bertrand Lanson 2023-05-26 22:11:11 +02:00
parent 4b5980e240
commit d5c89c4efa
8 changed files with 89 additions and 87 deletions

View File

@ -20,7 +20,7 @@ provisioner:
defaults:
remote_tmp: /tmp/.ansible
verifier:
name: testinfra
name: ansible
scenario:
name: default
test_sequence:

View File

@ -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
)

View File

@ -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")

View File

@ -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)"

View File

@ -20,7 +20,7 @@ provisioner:
defaults:
remote_tmp: /tmp/.ansible
verifier:
name: testinfra
name: ansible
scenario:
name: with_custom_config
test_sequence:

View File

@ -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
)

View File

@ -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")

View File

@ -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)"