ansible-collection-prometheus/roles/prometheus/molecule/default/tests/test_default.py
gardar 083ff4ef36
skip_changelog: make testinfra tests capture logs on service failure
Signed-off-by: gardar <gardar@users.noreply.github.com>
2023-08-18 15:47:32 +00:00

83 lines
2.2 KiB
Python

from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import os
import yaml
import testinfra.utils.ansible_runner
import pytest
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')
@pytest.fixture()
def AnsibleDefaults():
with open("defaults/main.yml", 'r') as stream:
return yaml.full_load(stream)
@pytest.mark.parametrize("dirs", [
"/etc/prometheus",
"/etc/prometheus/console_libraries",
"/etc/prometheus/consoles",
"/etc/prometheus/rules",
"/etc/prometheus/file_sd",
"/var/lib/prometheus"
])
def test_directories(host, dirs):
d = host.file(dirs)
assert d.is_directory
assert d.exists
@pytest.mark.parametrize("files", [
"/etc/prometheus/prometheus.yml",
"/etc/prometheus/console_libraries/prom.lib",
"/etc/prometheus/consoles/prometheus.html",
"/etc/prometheus/web.yml",
"/etc/systemd/system/prometheus.service",
"/usr/local/bin/prometheus",
"/usr/local/bin/promtool"
])
def test_files(host, files):
f = host.file(files)
assert f.exists
assert f.is_file
@pytest.mark.parametrize("files", [
"/etc/prometheus/rules/ansible_managed.rules"
])
def test_absent(host, files):
f = host.file(files)
assert f.exists
def test_user(host):
assert host.group("prometheus").exists
assert host.user("prometheus").exists
def test_service(host):
s = host.service("prometheus")
try:
assert s.is_running
except AssertionError:
# Capture service logs
journal_output = host.run('journalctl -u prometheus --since "1 hour ago"')
print("\n==== journalctl -u prometheus Output ====\n")
print(journal_output)
print("\n============================================\n")
raise # Re-raise the original assertion error
def test_socket(host):
s = host.socket("tcp://0.0.0.0:9090")
assert s.is_listening
def test_version(host, AnsibleDefaults):
version = os.getenv('PROMETHEUS', AnsibleDefaults['prometheus_version'])
run = host.run("/usr/local/bin/prometheus --version")
out = run.stdout + run.stderr
assert "prometheus, version " + version in out