chore: add unit tests for PrometheusSD class

This commit is contained in:
Robert Kaussow 2022-03-06 00:11:49 +01:00
parent 2ab1ad34e0
commit 04cb4639fb
Signed by: xoxys
GPG Key ID: 4E692A2EAECC03C0
9 changed files with 122 additions and 3 deletions

View File

@ -254,7 +254,7 @@ class Config():
except jsonschema.exceptions.ValidationError as e:
schema_error = "Failed validating '{validator}' in schema{schema}\n{message}".format(
validator=e.validator,
schema=format_as_index(list(e.relative_schema_path)[:-1]),
schema=format_as_index(list(e.relative_schema_path)[:-1], 0),
message=e.message
)
raise prometheuspvesd.exception.ConfigError("Configuration error", schema_error)

View File

@ -0,0 +1 @@
"""Init pytest unit tests."""

View File

@ -0,0 +1,4 @@
---
exclude_vmid:
- 100
- "101"

View File

@ -0,0 +1 @@
"""Init pytest fixtures."""

View File

@ -3,6 +3,43 @@
import pytest
@pytest.fixture
def environment():
return {
"PROMETHEUS_PVE_SD_PVE_USER": "dummyuser",
"PROMETHEUS_PVE_SD_PVE_PASSWORD": "dummypass",
"PROMETHEUS_PVE_SD_PVE_SERVER": "dummyserver",
}
@pytest.fixture
def defaults():
return {
"exclude_state": [],
"exclude_tags": [],
"exclude_vmid": [],
"logging": {
"format": "console",
"level": "WARNING"
},
"loop_delay": 300,
"metrics": {
"address": "127.0.0.1",
"enabled": True,
"port": 8000
},
"output_file": "/home/rknet/rkau2905/.cache/prometheus-pve-sd/pve.json",
"pve": {
"auth_timeout": 5,
"password": "dummypass",
"server": "dummyserver",
"user": "dummyuser",
"verify_ssl": True
},
"service": True,
}
@pytest.fixture
def qemus():
return [

View File

@ -1,4 +1,6 @@
"""Pytest conftest fixtures."""
import os
import sys
import pytest
@ -8,3 +10,13 @@ from prometheuspvesd.utils import Singleton
@pytest.fixture(autouse=True)
def reset_singletons():
Singleton._instances = {}
@pytest.fixture(autouse=True)
def reset_os_environment():
os.environ = {}
@pytest.fixture(autouse=True)
def reset_sys_argv():
sys.argv = ["prometheus-pve-sd"]

View File

@ -0,0 +1,64 @@
"""Test CLI class."""
import os
import pytest
from proxmoxer import ProxmoxAPI
from prometheuspvesd.cli import PrometheusSD
from prometheuspvesd.discovery import Discovery
from prometheuspvesd.exception import APIError
from prometheuspvesd.config import Config
pytest_plugins = [
"prometheuspvesd.test.fixtures.fixtures",
]
def test_cli_args(mocker, environment, defaults):
mocker.patch.dict(os.environ, environment)
mocker.patch("sys.argv", [
"prometheus-pve-sd",
"--no-service",
"-vvv",
])
mocker.patch.object(Discovery, "_auth", return_value=mocker.create_autospec(ProxmoxAPI))
mocker.patch.object(PrometheusSD, "_fetch", return_value=True)
pve = PrometheusSD()
defaults["service"] = False
defaults["logging"]["level"] = "DEBUG"
assert pve.config.config == defaults
def test_cli_config_error(mocker, environment, defaults):
mocker.patch.dict(os.environ, environment)
mocker.patch(
"sys.argv", [
"prometheus-pve-sd",
"-c",
"./prometheuspvesd/test/data/config.yml",
]
)
mocker.patch.object(Discovery, "_auth", return_value=mocker.create_autospec(ProxmoxAPI))
mocker.patch.object(PrometheusSD, "_fetch", return_value=True)
with pytest.raises(SystemExit) as e:
PrometheusSD()
assert e.value.code == 1
def test_cli_api_error(mocker, environment, defaults):
mocker.patch.dict(os.environ, environment)
mocker.patch.dict(Config.SETTINGS, {"service": {"default": False}})
mocker.patch.object(Discovery, "_auth", side_effect=APIError("Dummy API Exception"))
mocker.patch.object(PrometheusSD, "_fetch", return_value=True)
with pytest.raises(SystemExit) as e:
PrometheusSD()
assert e.value.code == 1

View File

@ -1,4 +1,4 @@
"""Test Autostop class."""
"""Test Discovery class."""
import pytest
from proxmoxer import ProxmoxAPI

View File

@ -10,7 +10,7 @@
ignore = D102, D103, D105, D107, D202, W503
max-line-length = 99
inline-quotes = double
exclude = .git, __pycache__, build, dist, test, *.pyc, *.egg-info, .cache, .eggs, env*
exclude = .git, __pycache__, build, dist, *.pyc, *.egg-info, .cache, .eggs, env*
[yapf]
based_on_style = google