import os import testinfra.utils.ansible_runner test_hosts = testinfra.utils.ansible_runner.AnsibleRunner( os.environ["MOLECULE_INVENTORY_FILE"]).get_hosts("all") def test_user_and_group_exist(host): user = host.user("blackbox_exporter") assert user.exists assert user.group == "blackbox_exporter" def test_service_enabled_and_running(host): svc = host.service("blackbox_exporter") assert svc.is_enabled assert svc.is_running def test_binary_and_symlink(host): binary = host.file("/opt/blackbox_exporter/blackbox_exporter") symlink = host.file("/usr/local/bin/blackbox_exporter") assert binary.exists assert binary.mode & 0o111 assert binary.user == "blackbox_exporter" assert binary.group == "blackbox_exporter" assert symlink.exists assert symlink.is_symlink assert symlink.linked_to == "/opt/blackbox_exporter/blackbox_exporter" def test_config_directory(host): cfg_dir = host.file("/etc/blackbox_exporter") assert cfg_dir.exists assert cfg_dir.is_directory assert cfg_dir.user == "blackbox_exporter" assert cfg_dir.group == "blackbox_exporter" assert cfg_dir.mode == 0o755 def test_config_file(host): cfg = host.file("/etc/blackbox_exporter/blackbox.yml") assert cfg.exists assert cfg.user == "blackbox_exporter" assert cfg.group == "blackbox_exporter" assert cfg.mode == 0o644 assert cfg.contains("modules:") assert cfg.contains("http_2xx") def test_systemd_unit_file(host): unit = host.file("/etc/systemd/system/blackbox_exporter.service") assert unit.exists assert unit.user == "root" assert unit.group == "root" assert unit.mode == 0o644 assert unit.contains("ExecStart=/usr/local/bin/blackbox_exporter") def test_listen_socket(host): socket = host.socket("tcp://0.0.0.0:9115") assert socket.is_listening def test_logs_exist(host): journalctl = host.check_output("journalctl -u blackbox_exporter --no-pager") assert "Listening on" in journalctl or "level=info" in journalctl