Skip to content

Commit

Permalink
chore: reset singletons in pytest to avoid data leaks between tests (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
xoxys authored Mar 3, 2022
1 parent b5d3624 commit 0ceb550
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 57 deletions.
21 changes: 0 additions & 21 deletions codecov.yml

This file was deleted.

12 changes: 0 additions & 12 deletions prometheuspvesd/test/fixtures/fixtures.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
"""Global pytest fixtures."""

import pytest
from proxmoxer import ProxmoxAPI

from prometheuspvesd import discovery


@pytest.fixture
def discovery_fixture(mocker):
mocker.patch.object(
discovery.Discovery, "_auth", return_value=mocker.create_autospec(ProxmoxAPI)
)

return discovery.Discovery()


@pytest.fixture
Expand Down
10 changes: 10 additions & 0 deletions prometheuspvesd/test/unit/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""Pytest conftest fixtures."""

import pytest

from prometheuspvesd.utils import Singleton


@pytest.fixture(autouse=True)
def reset_singletons():
Singleton._instances = {}
54 changes: 30 additions & 24 deletions prometheuspvesd/test/unit/test_discovery.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
"""Test Autostop class."""

import pytest
from proxmoxer import ProxmoxAPI

from prometheuspvesd.discovery import Discovery

pytest_plugins = [
"prometheuspvesd.test.fixtures.fixtures",
]


@pytest.fixture
def discovery(mocker):
mocker.patch.object(Discovery, "_auth", return_value=mocker.create_autospec(ProxmoxAPI))

return Discovery()


def get_mock(*args):
networks = args[0]
args = args[1:]
Expand All @@ -17,51 +29,45 @@ def get_mock(*args):
return False


def test_exclude_vmid(discovery_fixture, qemus):
discovery_fixture.config.config["exclude_vmid"] = ["100", "101", "102"]

expected = []
filtered = discovery_fixture._exclude(qemus)
def test_exclude_vmid(discovery, qemus):
discovery.config.config["exclude_vmid"] = ["100", "101", "102"]
filtered = discovery._exclude(qemus)

assert filtered == expected
discovery_fixture.config.config["exclude_vmid"] = []
assert len(filtered) == 0


def test_exclude_state(discovery_fixture, qemus):
discovery_fixture.config.config["exclude_state"] = ["prelaunch"]
filtered = discovery_fixture._exclude(qemus)
def test_exclude_state(discovery, qemus):
discovery.config.config["exclude_state"] = ["prelaunch"]
filtered = discovery._exclude(qemus)

assert len(filtered) == 2
discovery_fixture.config.config["exclude_state"] = []


def test_exclude_tags(discovery_fixture, qemus):
discovery_fixture.config.config["exclude_tags"] = ["unmonitored"]

filtered = discovery_fixture._exclude(qemus)
def test_exclude_tags(discovery, qemus):
discovery.config.config["exclude_tags"] = ["unmonitored"]
filtered = discovery._exclude(qemus)

assert len(filtered) == 2
discovery_fixture.config.config["exclude_tags"] = []


def test_validate_ip(discovery_fixture, addresses):
def test_validate_ip(discovery, addresses):
# IPv4 validation
for address in addresses["ipv4_valid"]:
assert discovery_fixture._validate_ip(address)
assert discovery._validate_ip(address)
for address in addresses["ipv4_invalid"]:
assert not discovery_fixture._validate_ip(address)
assert not discovery._validate_ip(address)

# IPv6 validation
for address in addresses["ipv6_valid"]:
assert discovery_fixture._validate_ip(address)
assert discovery._validate_ip(address)
for address in addresses["ipv6_invalid"]:
assert not discovery_fixture._validate_ip(address)
assert not discovery._validate_ip(address)


def test_get_ip_addresses(mocker, discovery_fixture, networks):
discovery_fixture.client.get.side_effect = lambda *args: get_mock(networks, *args)
def test_get_ip_addresses(mocker, discovery, networks):
discovery.client.get.side_effect = lambda *args: get_mock(networks, *args)

assert discovery_fixture._get_ip_addresses("qemu", "dummy", "dummy") == (
assert discovery._get_ip_addresses("qemu", "dummy", "dummy") == (
networks[1]["ip-addresses"][0]["ip-address"],
networks[1]["ip-addresses"][2]["ip-address"],
)

0 comments on commit 0ceb550

Please sign in to comment.