Skip to content

Commit

Permalink
testlib: Make global settings update more flexible
Browse files Browse the repository at this point in the history
Not all global settings go to the same file. Therefore, the target file is now
an argument to the respective functions.

This change also corrects the file to which the agent bakery logging settings
are written when setting up for the composition tests. Before, these settings
were written to the wrong file.

Change-Id: I45b8dcd698b84c56ec137f735c636f0adb5851bd
  • Loading branch information
jherbel committed Feb 7, 2025
1 parent 178dcb4 commit 7e57c7c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
8 changes: 7 additions & 1 deletion tests/composition/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
)
from tests.testlib.site import (
get_site_factory,
GlobalSettingsUpdate,
Site,
tracing_config_from_env,
)
Expand Down Expand Up @@ -86,7 +87,12 @@ def _central_site(request: pytest.FixtureRequest, ensure_cron: None) -> Iterator
description=request.node.name,
auto_restart_httpd=True,
tracing_config=tracing_config_from_env(os.environ),
global_settings_update={"agent_bakery_logging": 10},
global_settings_updates=[
GlobalSettingsUpdate(
relative_path=Path("etc") / "check_mk" / "conf.d" / "wato" / "global.mk",
update={"agent_bakery_logging": 10},
),
],
) as central_site:
with _increased_logging_level(central_site):
yield central_site
Expand Down
42 changes: 28 additions & 14 deletions tests/testlib/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import sys
import time
import urllib.parse
from collections.abc import Callable, Iterator, Mapping, Sequence
from collections.abc import Callable, Iterable, Iterator, Mapping, Sequence
from contextlib import contextmanager, nullcontext, suppress
from dataclasses import dataclass
from getpass import getuser
Expand Down Expand Up @@ -76,8 +76,6 @@ class TracingConfig:


class Site:
_GLOBAL_SETTINGS_FILE: Final = "etc/check_mk/multisite.d/wato/global.mk"

def __init__(
self,
version: CMKVersion,
Expand Down Expand Up @@ -1473,19 +1471,32 @@ def start_active_services(self) -> None:
lambda: self.is_global_flag_enabled("execute_service_checks"), timeout=60, interval=1
)

def read_global_settings(self) -> dict[str, object]:
def read_global_settings(self, relative_path: Path) -> dict[str, object]:
global_settings: dict[str, object] = {}
exec(self.read_file(self._GLOBAL_SETTINGS_FILE), {}, global_settings)
exec(self.read_file(relative_path), {}, global_settings)
return global_settings

def write_global_settings(self, global_settings: Mapping[str, object]) -> None:
def write_global_settings(
self,
relative_path: Path,
global_settings: Mapping[str, object],
) -> None:
self.write_text_file(
self._GLOBAL_SETTINGS_FILE,
relative_path,
"\n".join(f"{key} = {repr(val)}" for key, val in global_settings.items()),
)

def update_global_settings(self, update: dict[str, object]) -> None:
self.write_global_settings(self.read_global_settings() | update)
def update_global_settings(self, relative_path: Path, update: dict[str, object]) -> None:
self.write_global_settings(
relative_path,
self.read_global_settings(relative_path) | update,
)


@dataclass(frozen=True)
class GlobalSettingsUpdate:
relative_path: Path
update: dict[str, object]


class SiteFactory:
Expand Down Expand Up @@ -1822,7 +1833,7 @@ def get_test_site_ctx(
save_results: bool = True,
report_crashes: bool = True,
tracing_config: TracingConfig = NO_TRACING,
global_settings_update: dict[str, object] | None = None,
global_settings_updates: Iterable[GlobalSettingsUpdate] = (),
) -> Iterator[Site]:
yield from self.get_test_site(
name=name,
Expand All @@ -1833,7 +1844,7 @@ def get_test_site_ctx(
save_results=save_results,
report_crashes=report_crashes,
tracing_config=tracing_config,
global_settings_update=global_settings_update,
global_settings_updates=global_settings_updates,
)

def get_test_site(
Expand All @@ -1846,7 +1857,7 @@ def get_test_site(
save_results: bool = True,
report_crashes: bool = True,
tracing_config: TracingConfig = NO_TRACING,
global_settings_update: dict[str, object] | None = None,
global_settings_updates: Iterable[GlobalSettingsUpdate] = (),
) -> Iterator[Site]:
"""Return a fully set-up test site (for use in site fixtures)."""
reuse_site = os.environ.get("REUSE", "0") == "1"
Expand All @@ -1869,8 +1880,11 @@ def get_test_site(

try:
self.setup_customers(site, ["customer1", "customer2"])
if global_settings_update:
site.update_global_settings(global_settings_update)
for global_settings_update in global_settings_updates:
site.update_global_settings(
global_settings_update.relative_path,
global_settings_update.update,
)
self.initialize_site(
site,
init_livestatus=init_livestatus,
Expand Down

0 comments on commit 7e57c7c

Please sign in to comment.