mirror of
https://github.com/thegeeklab/prometheus-pve-sd.git
synced 2024-06-02 16:09:41 +02:00
chore: add unit tests for PrometheusSD class
This commit is contained in:
parent
2ab1ad34e0
commit
04cb4639fb
|
@ -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)
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
"""Init pytest unit tests."""
|
4
prometheuspvesd/test/data/config_error.yml
Normal file
4
prometheuspvesd/test/data/config_error.yml
Normal file
|
@ -0,0 +1,4 @@
|
|||
---
|
||||
exclude_vmid:
|
||||
- 100
|
||||
- "101"
|
1
prometheuspvesd/test/fixtures/__init__.py
vendored
1
prometheuspvesd/test/fixtures/__init__.py
vendored
|
@ -0,0 +1 @@
|
|||
"""Init pytest fixtures."""
|
37
prometheuspvesd/test/fixtures/fixtures.py
vendored
37
prometheuspvesd/test/fixtures/fixtures.py
vendored
|
@ -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 [
|
||||
|
|
|
@ -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"]
|
||||
|
|
64
prometheuspvesd/test/unit/test_cli.py
Normal file
64
prometheuspvesd/test/unit/test_cli.py
Normal 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
|
|
@ -1,4 +1,4 @@
|
|||
"""Test Autostop class."""
|
||||
"""Test Discovery class."""
|
||||
|
||||
import pytest
|
||||
from proxmoxer import ProxmoxAPI
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user