Skip to content

Commit

Permalink
reland - Add piggyback composition test III
Browse files Browse the repository at this point in the history
This reverts commit b93b98f.

Change-Id: Idbad76722abac07881bcb77e941d23e192a9cd72
  • Loading branch information
DavidGerva committed Nov 29, 2024
1 parent 9f8e6b1 commit cf40236
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 1 deletion.
53 changes: 52 additions & 1 deletion tests/composition/cmk/piggyback/piggyback_test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
# conditions defined in the file COPYING, which is part of this source code package.


import signal
import subprocess
import time
from collections.abc import Iterator
from contextlib import contextmanager
from dataclasses import dataclass
from pathlib import Path
from typing import Any
from typing import Any, IO

from tests.testlib.site import AUTOMATION_USER, Site
from tests.testlib.utils import ServiceInfo
Expand Down Expand Up @@ -168,3 +170,52 @@ def _write_sitespecific_config_file(central_site: Site) -> Iterator[None]:
yield
finally:
central_site.write_text_file(global_settings_file, settings_text)


class Timeout(RuntimeError):
pass


@contextmanager
def _timeout(seconds: int, exc: Timeout) -> Iterator[None]:
"""Context manager to raise an exception after a timeout"""

def _raise_timeout(signum, frame):
raise exc

alarm_handler = signal.signal(signal.SIGALRM, _raise_timeout)
try:
signal.alarm(seconds)
yield
finally:
signal.alarm(0)
signal.signal(signal.SIGALRM, alarm_handler)


def _wait_for_piggyback_track_ready(stdout: IO[str]) -> None:
"""Wait for the cmk-broker-test to be ready"""
with _timeout(3, Timeout("`cmk-piggyback track` did not start in time")):
while line := stdout.readline():
if "Tracking incoming messages" in line:
return


def piggybacked_data_gets_updated(
source_site: Site, target_site: Site, hostname_source: str, hostname_piggybacked: str
) -> bool:
"""Track incoming piggybacked data on the target site"""

try:
track = target_site.execute(["cmk-piggyback", "track"], stdout=subprocess.PIPE, text=True)
assert track.stdout
_wait_for_piggyback_track_ready(track.stdout)

source_site.schedule_check(hostname_source, "Check_MK")
with _timeout(5, Timeout("`cmk-piggyback track` timed out after 5s")):
while line := track.stdout.readline():
if f"{hostname_source} -> {hostname_piggybacked}" in line:
return True
except Timeout:
pass

return False
66 changes: 66 additions & 0 deletions tests/composition/cmk/piggyback/test_piggyback.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
disable_piggyback_hub_globally,
disable_piggyback_hub_remote_site,
get_piggybacked_service_time,
piggybacked_data_gets_updated,
piggybacked_service_discovered,
)

Expand Down Expand Up @@ -258,3 +259,68 @@ def test_piggyback_hub_disabled_remote_site(
assert _piggybacked_service_gets_updated(
central_site, remote_site, _HOSTNAME_SOURCE_CENTRAL, _HOSTNAME_PIGGYBACKED_A
)


def _move_host(central_site: Site, to_remote_site: str, hostname_piggyback: str) -> None:
central_site.openapi.update_host_attributes(
hostname_piggyback,
update_attributes={"site": to_remote_site},
)
central_site.openapi.activate_changes_and_wait_for_completion(force_foreign_changes=True)


def test_piggyback_services_move_host(
central_site: Site,
remote_site: Site,
remote_site_2: Site,
prepare_piggyback_environment: None,
) -> None:
"""
Scenario: Moving host to another site makes the piggyback data to be monitored on the new site
- _HOSTNAME_PIGGYBACKED_A is moved from remote_site to remote_site2
- piggyback data for _HOSTNAME_PIGGYBACKED_A is not monitored and not updated on remote_site
- piggyback data for _HOSTNAME_PIGGYBACKED_A is monitored again on remote_site2
"""

with _setup_piggyback_host(central_site, remote_site.id, _HOSTNAME_PIGGYBACKED_A):
_move_host(central_site, remote_site_2.id, _HOSTNAME_PIGGYBACKED_A)
_schedule_check_and_discover(
central_site, _HOSTNAME_SOURCE_CENTRAL, _HOSTNAME_PIGGYBACKED_A
)
assert not piggybacked_data_gets_updated(
central_site, remote_site, _HOSTNAME_SOURCE_CENTRAL, _HOSTNAME_PIGGYBACKED_A
)
assert piggybacked_data_gets_updated(
central_site, remote_site_2, _HOSTNAME_SOURCE_CENTRAL, _HOSTNAME_PIGGYBACKED_A
)
assert piggybacked_service_discovered(
central_site, _HOSTNAME_SOURCE_CENTRAL, _HOSTNAME_PIGGYBACKED_A
)


def test_piggyback_host_removal(
central_site: Site,
remote_site: Site,
prepare_piggyback_environment: None,
) -> None:
"""
Scenario: Host removal stops distribution
- piggyback data for _HOSTNAME_PIGGYBACKED_A is monitored on remote_site
- remove _HOSTNAME_PIGGYBACKED_A from remote_site
- piggyback data for _HOSTNAME_PIGGYBACKED_A is not monitored and not updated on remote_site
"""

with _setup_piggyback_host(central_site, remote_site.id, _HOSTNAME_PIGGYBACKED_A):
_schedule_check_and_discover(
central_site, _HOSTNAME_SOURCE_CENTRAL, _HOSTNAME_PIGGYBACKED_A
)
assert piggybacked_data_gets_updated(
central_site, remote_site, _HOSTNAME_SOURCE_CENTRAL, _HOSTNAME_PIGGYBACKED_A
)
assert piggybacked_service_discovered(
central_site, _HOSTNAME_SOURCE_CENTRAL, _HOSTNAME_PIGGYBACKED_A
)

assert not piggybacked_data_gets_updated(
central_site, remote_site, _HOSTNAME_SOURCE_CENTRAL, _HOSTNAME_PIGGYBACKED_A
)

0 comments on commit cf40236

Please sign in to comment.