2022-03-06 12:48:40 +00:00
|
|
|
"""Test Discovery class."""
|
2022-02-26 15:45:02 +00:00
|
|
|
|
2024-01-24 11:50:39 +00:00
|
|
|
import logging
|
|
|
|
|
2022-03-03 10:16:02 +00:00
|
|
|
import pytest
|
|
|
|
from proxmoxer import ProxmoxAPI
|
|
|
|
|
2022-04-04 20:43:00 +00:00
|
|
|
from prometheuspvesd.client import ProxmoxClient
|
2022-03-03 10:16:02 +00:00
|
|
|
from prometheuspvesd.discovery import Discovery
|
|
|
|
|
2022-02-26 15:45:02 +00:00
|
|
|
pytest_plugins = [
|
|
|
|
"prometheuspvesd.test.fixtures.fixtures",
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2024-01-24 11:50:39 +00:00
|
|
|
def records_to_messages(records):
|
|
|
|
return [r.getMessage() for r in records]
|
|
|
|
|
|
|
|
|
2022-03-03 10:16:02 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def discovery(mocker):
|
2022-04-04 20:43:00 +00:00
|
|
|
mocker.patch.object(ProxmoxClient, "_auth", return_value=mocker.create_autospec(ProxmoxAPI))
|
2022-03-03 10:16:02 +00:00
|
|
|
|
|
|
|
return Discovery()
|
|
|
|
|
|
|
|
|
|
|
|
def test_exclude_vmid(discovery, qemus):
|
|
|
|
discovery.config.config["exclude_vmid"] = ["100", "101", "102"]
|
2022-03-30 18:48:39 +00:00
|
|
|
filtered = discovery._filter(qemus)
|
2022-02-26 15:45:02 +00:00
|
|
|
|
2022-03-03 10:16:02 +00:00
|
|
|
assert len(filtered) == 0
|
2022-03-03 08:08:27 +00:00
|
|
|
|
|
|
|
|
2022-03-03 10:16:02 +00:00
|
|
|
def test_exclude_state(discovery, qemus):
|
|
|
|
discovery.config.config["exclude_state"] = ["prelaunch"]
|
2022-03-30 18:48:39 +00:00
|
|
|
filtered = discovery._filter(qemus)
|
2022-03-03 08:08:27 +00:00
|
|
|
|
|
|
|
assert len(filtered) == 2
|
|
|
|
|
|
|
|
|
2024-01-24 11:50:39 +00:00
|
|
|
def test_exclude_tags(discovery, qemus, local_caplog):
|
2022-03-03 10:16:02 +00:00
|
|
|
discovery.config.config["exclude_tags"] = ["unmonitored"]
|
2022-03-03 08:08:27 +00:00
|
|
|
|
2024-01-24 11:50:39 +00:00
|
|
|
with local_caplog(level=logging.DEBUG) as caplog:
|
|
|
|
filtered = discovery._filter(qemus)
|
|
|
|
|
|
|
|
assert (
|
|
|
|
"vmid 100: discovered tags: ['unmonitored', 'excluded', 'postgres']"
|
|
|
|
in records_to_messages(caplog.records)
|
|
|
|
)
|
|
|
|
assert "vmid 100: excluded by tags: ['unmonitored']"
|
2022-03-03 08:08:27 +00:00
|
|
|
assert len(filtered) == 2
|
2022-02-27 16:56:08 +00:00
|
|
|
|
|
|
|
|
2022-03-30 19:34:35 +00:00
|
|
|
@pytest.mark.parametrize(
|
2024-01-24 11:50:39 +00:00
|
|
|
"testinput,expected",
|
|
|
|
[
|
2022-03-30 19:34:35 +00:00
|
|
|
(["monitored"], 1),
|
|
|
|
(["monitored", "postgres"], 2),
|
|
|
|
([], 3),
|
2024-01-24 11:50:39 +00:00
|
|
|
],
|
2022-03-30 19:34:35 +00:00
|
|
|
)
|
|
|
|
def test_include_tags(discovery, qemus, testinput, expected):
|
|
|
|
discovery.config.config["include_tags"] = testinput
|
2022-03-30 18:48:39 +00:00
|
|
|
filtered = discovery._filter(qemus)
|
|
|
|
|
2022-03-30 19:34:35 +00:00
|
|
|
assert len(filtered) == expected
|
2022-03-30 18:48:39 +00:00
|
|
|
|
|
|
|
|
2024-01-24 11:50:39 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"testinput,expected",
|
|
|
|
[
|
|
|
|
(["101", "100"], 2),
|
|
|
|
([], 3),
|
|
|
|
],
|
|
|
|
)
|
2022-03-30 19:34:35 +00:00
|
|
|
def test_include_vmid(discovery, qemus, testinput, expected):
|
|
|
|
discovery.config.config["include_vmid"] = testinput
|
2022-03-30 18:48:39 +00:00
|
|
|
filtered = discovery._filter(qemus)
|
|
|
|
|
2022-03-30 19:34:35 +00:00
|
|
|
assert len(filtered) == expected
|
2022-03-30 18:48:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_include_and_exclude_tags(discovery, qemus):
|
|
|
|
discovery.config.config["include_tags"] = ["postgres"]
|
|
|
|
discovery.config.config["exclude_tags"] = ["unmonitored"]
|
|
|
|
filtered = discovery._filter(qemus)
|
|
|
|
|
|
|
|
assert len(filtered) == 0
|
|
|
|
|
|
|
|
|
2022-03-03 10:16:02 +00:00
|
|
|
def test_validate_ip(discovery, addresses):
|
2022-02-27 16:56:08 +00:00
|
|
|
# IPv4 validation
|
|
|
|
for address in addresses["ipv4_valid"]:
|
2022-03-03 10:16:02 +00:00
|
|
|
assert discovery._validate_ip(address)
|
2022-02-27 16:56:08 +00:00
|
|
|
for address in addresses["ipv4_invalid"]:
|
2022-03-03 10:16:02 +00:00
|
|
|
assert not discovery._validate_ip(address)
|
2022-02-27 16:56:08 +00:00
|
|
|
|
|
|
|
# IPv6 validation
|
|
|
|
for address in addresses["ipv6_valid"]:
|
2022-03-03 10:16:02 +00:00
|
|
|
assert discovery._validate_ip(address)
|
2022-02-27 16:56:08 +00:00
|
|
|
for address in addresses["ipv6_invalid"]:
|
2022-03-03 10:16:02 +00:00
|
|
|
assert not discovery._validate_ip(address)
|
2022-02-27 16:56:08 +00:00
|
|
|
|
|
|
|
|
2022-03-03 10:16:02 +00:00
|
|
|
def test_get_ip_addresses(mocker, discovery, networks):
|
2022-04-04 20:43:00 +00:00
|
|
|
mocker.patch.object(ProxmoxClient, "get_network_interfaces", return_value=networks)
|
2022-02-27 16:56:08 +00:00
|
|
|
|
2022-03-03 10:16:02 +00:00
|
|
|
assert discovery._get_ip_addresses("qemu", "dummy", "dummy") == (
|
2022-02-27 16:56:08 +00:00
|
|
|
networks[1]["ip-addresses"][0]["ip-address"],
|
|
|
|
networks[1]["ip-addresses"][2]["ip-address"],
|
|
|
|
)
|
2022-04-04 20:43:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_get_ip_addresses_from_instance_config(mocker, discovery, instance_config):
|
|
|
|
mocker.patch.object(ProxmoxClient, "get_network_interfaces", return_value=[])
|
|
|
|
mocker.patch.object(ProxmoxClient, "get_instance_config", return_value=instance_config)
|
|
|
|
|
|
|
|
assert discovery._get_ip_addresses("qemu", "dummy", "dummy") == (
|
|
|
|
"192.0.2.25",
|
|
|
|
"2001:db8::666:77:8888",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_propagate(
|
|
|
|
mocker, discovery, nodes, qemus, instance_config, agent_info, networks, inventory
|
|
|
|
):
|
|
|
|
mocker.patch.object(ProxmoxClient, "get_nodes", return_value=nodes)
|
|
|
|
mocker.patch.object(ProxmoxClient, "get_all_vms", return_value=qemus)
|
|
|
|
mocker.patch.object(ProxmoxClient, "get_instance_config", return_value=instance_config)
|
|
|
|
mocker.patch.object(ProxmoxClient, "get_agent_info", return_value=agent_info)
|
|
|
|
mocker.patch.object(ProxmoxClient, "get_network_interfaces", return_value=networks)
|
|
|
|
|
|
|
|
assert discovery.propagate() == inventory
|