Skip to content

Commit

Permalink
Move classes from conftest into separate module
Browse files Browse the repository at this point in the history
The goal is to get rid of imports from tests/unit/conftest.py file.
For this purpose FixPluginLegacy, DummyNotificationHandler and
DummyLicensingHandler classes were moved from the conftest
into a separate module mocks_and_helpers.py.

Jira ticket: CMK-21864

Change-Id: I31c21eaf1b20199b73591d95f7c0a909f650715d
  • Loading branch information
asyash26 committed Feb 28, 2025
1 parent 861fe35 commit 01d7482
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 57 deletions.
5 changes: 4 additions & 1 deletion tests/unit/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ package(default_visibility = [":__subpackages__"])
py_library(
name = "conftest",
testonly = True,
srcs = ["conftest.py"],
srcs = [
"conftest.py",
"mocks_and_helpers.py",
],
deps = [
"//cmk:lib_cmk",
"//packages/cmk-livestatus-client:py_livestatus",
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/checks/test_generic_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import pytest
import time_machine

from tests.unit.conftest import FixPluginLegacy
from tests.unit.mocks_and_helpers import FixPluginLegacy

from . import generictests

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/checks/test_generic_legacy_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import pytest

from tests.unit.conftest import FixPluginLegacy
from tests.unit.mocks_and_helpers import FixPluginLegacy

from cmk.utils.sectionname import SectionName

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/cmk/snmplib/test_snmp_scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import pytest

from tests.unit.conftest import FixPluginLegacy
from tests.unit.mocks_and_helpers import FixPluginLegacy

from cmk.ccc.exceptions import MKSNMPError, OnError

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/cmk/utils/test_man_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from tests.testlib.common.repo import repo_path

from tests.unit.conftest import FixPluginLegacy
from tests.unit.mocks_and_helpers import FixPluginLegacy

from cmk.utils import man_pages

Expand Down
54 changes: 2 additions & 52 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
repo_path,
)

from tests.unit.mocks_and_helpers import DummyLicensingHandler, FixPluginLegacy

import livestatus

import cmk.ccc.debug
Expand All @@ -33,12 +35,6 @@
import cmk.utils.caching
import cmk.utils.paths
from cmk.utils import redis, tty
from cmk.utils.licensing.handler import (
LicenseState,
LicensingHandler,
NotificationHandler,
UserEffect,
)
from cmk.utils.livestatus_helpers.testing import (
mock_livestatus_communication,
MockLiveStatusConnection,
Expand All @@ -50,7 +46,6 @@
)

import cmk.crypto.password_hashing
from cmk.agent_based.legacy import discover_legacy_checks, FileLoader, find_plugin_files

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -299,22 +294,6 @@ def agent_based_plugins() -> AgentBasedPlugins:
return get_previously_loaded_plugins()


class FixPluginLegacy:
"""Access legacy dicts like `check_info`"""

def __init__(self) -> None:
result = discover_legacy_checks(
find_plugin_files(str(repo_path() / "cmk/base/legacy_checks")),
FileLoader(
precomile_path=cmk.utils.paths.precompiled_checks_dir,
local_path="/not_relevant_for_test",
makedirs=store.makedirs,
),
raise_errors=True,
)
self.check_info = {p.name: p for p in result.sane_check_info}


@pytest.fixture(scope="session")
def fix_plugin_legacy() -> Iterator[FixPluginLegacy]:
yield FixPluginLegacy()
Expand Down Expand Up @@ -385,35 +364,6 @@ def fixture_monkeypatch_module() -> Iterator[pytest.MonkeyPatch]:
yield mp


class DummyNotificationHandler(NotificationHandler):
def manage_notification(self) -> None:
pass


class DummyLicensingHandler(LicensingHandler):
@classmethod
def make(cls) -> "DummyLicensingHandler":
return cls()

@property
def state(self) -> LicenseState:
return LicenseState.LICENSED

@property
def message(self) -> str:
return ""

def effect_core(self, num_services: int, num_hosts_shadow: int) -> UserEffect:
return UserEffect(header=None, email=None, block=None)

def effect(self, licensing_settings_link: str | None = None) -> UserEffect:
return UserEffect(header=None, email=None, block=None)

@property
def notification_handler(self) -> NotificationHandler:
return DummyNotificationHandler(email_notification=None)


@pytest.fixture(name="is_licensed", scope="module")
def fixture_is_licensed(monkeypatch_module: pytest.MonkeyPatch) -> None:
monkeypatch_module.setattr(
Expand Down
68 changes: 68 additions & 0 deletions tests/unit/mocks_and_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env python3
# Copyright (C) 2025 Checkmk GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

from tests.testlib.common.repo import (
repo_path,
)

import cmk.ccc.debug
from cmk.ccc import store

import cmk.utils.caching
import cmk.utils.paths
from cmk.utils.licensing.handler import (
LicenseState,
LicensingHandler,
NotificationHandler,
UserEffect,
)

import cmk.crypto.password_hashing
from cmk.agent_based.legacy import discover_legacy_checks, FileLoader, find_plugin_files


class FixPluginLegacy:
"""Access legacy dicts like `check_info`"""

def __init__(self) -> None:
result = discover_legacy_checks(
find_plugin_files(str(repo_path() / "cmk/base/legacy_checks")),
FileLoader(
precomile_path=cmk.utils.paths.precompiled_checks_dir,
local_path="/not_relevant_for_test",
makedirs=store.makedirs,
),
raise_errors=True,
)
self.check_info = {p.name: p for p in result.sane_check_info}


class DummyNotificationHandler(NotificationHandler):
def manage_notification(self) -> None:
pass


class DummyLicensingHandler(LicensingHandler):
@classmethod
def make(cls) -> "DummyLicensingHandler":
return cls()

@property
def state(self) -> LicenseState:
return LicenseState.LICENSED

@property
def message(self) -> str:
return ""

def effect_core(self, num_services: int, num_hosts_shadow: int) -> UserEffect:
return UserEffect(header=None, email=None, block=None)

def effect(self, licensing_settings_link: str | None = None) -> UserEffect:
return UserEffect(header=None, email=None, block=None)

@property
def notification_handler(self) -> NotificationHandler:
return DummyNotificationHandler(email_notification=None)

0 comments on commit 01d7482

Please sign in to comment.