Skip to content

Commit

Permalink
Move 'wait_until' helper to testlib.utils module
Browse files Browse the repository at this point in the history
Tests are adapted accordingly.

Change-Id: Ida89ff0537aef882d3b4d1bc19463e584fc67cc2
  • Loading branch information
MatteoStifano committed Dec 13, 2023
1 parent f6cc8cc commit 2b264f4
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 32 deletions.
2 changes: 1 addition & 1 deletion tests/composition/test_automatic_host_removal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
# Copyright (C) 2022 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 import wait_until
from tests.testlib.pytest_helpers.marks import skip_if_saas_edition
from tests.testlib.site import Site
from tests.testlib.utils import wait_until

from .utils import LOGGER

Expand Down
4 changes: 2 additions & 2 deletions tests/docker/test_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from pytest import LogCaptureFixture

import tests.testlib as testlib
from tests.testlib.utils import repo_path
from tests.testlib.utils import repo_path, wait_until
from tests.testlib.version import CMKVersion, version_from_env

from cmk.utils.version import Edition, Version, versions_compatible, VersionsCompatible
Expand Down Expand Up @@ -261,7 +261,7 @@ def _start(

request.addfinalizer(lambda: c.remove(force=True))

testlib.wait_until(lambda: "### CONTAINER STARTED" in c.logs().decode("utf-8"), timeout=120)
wait_until(lambda: "### CONTAINER STARTED" in c.logs().decode("utf-8"), timeout=120)
output = c.logs().decode("utf-8")

if not is_update:
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/cmk/gui/test_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import pytest
import requests

from tests.testlib import wait_until
from tests.testlib.site import Site
from tests.testlib.utils import wait_until


@pytest.fixture(name="plugin_path")
Expand Down
3 changes: 2 additions & 1 deletion tests/integration/cmk/snmplib/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
UdpTransportTarget,
)

from tests.testlib import repo_path, wait_until
from tests.testlib import repo_path
from tests.testlib.site import Site
from tests.testlib.utils import wait_until

import cmk.utils.debug as debug
import cmk.utils.log as log
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/notifications/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

import pytest

from tests.testlib import wait_until
from tests.testlib.site import Site
from tests.testlib.utils import wait_until


@pytest.fixture(name="disable_checks")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@

import pytest

from tests.testlib import wait_until, WatchLog
from tests.testlib import WatchLog
from tests.testlib.site import Site
from tests.testlib.utils import wait_until

from cmk.utils.rulesets.definition import RuleGroup

Expand Down
3 changes: 1 addition & 2 deletions tests/plugins_integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@

import pytest

from tests.testlib import wait_until
from tests.testlib.site import get_site_factory, Site, SiteFactory
from tests.testlib.utils import current_base_branch_name, current_branch_version, run
from tests.testlib.utils import current_base_branch_name, current_branch_version, run, wait_until
from tests.testlib.version import CMKVersion, Edition, get_min_version

from tests.plugins_integration import checks
Expand Down
14 changes: 2 additions & 12 deletions tests/testlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import sys
import tempfile
import time
from collections.abc import Callable, Collection, Iterator, Mapping
from collections.abc import Collection, Iterator, Mapping
from contextlib import contextmanager
from pathlib import Path
from types import ModuleType
Expand Down Expand Up @@ -41,6 +41,7 @@
repo_path,
site_id,
virtualenv_path,
wait_until,
)
from tests.testlib.version import CMKVersion # noqa: F401 # pylint: disable=unused-import
from tests.testlib.web_session import APIError, CMKWebSession
Expand Down Expand Up @@ -151,16 +152,6 @@ def import_module_hack(pathname: str) -> ModuleType:
return module


def wait_until(condition: Callable[[], bool], timeout: float = 1, interval: float = 0.1) -> None:
start = time.time()
while time.time() - start < timeout:
if condition():
return # Success. Stop waiting...
time.sleep(interval)

raise Exception("Timeout waiting for %r to finish (Timeout: %d sec)" % (condition, timeout))


def wait_until_liveproxyd_ready(site: Site, site_ids: Collection[str]) -> None:
def _config_available() -> bool:
return site.file_exists("etc/check_mk/liveproxyd.mk")
Expand Down Expand Up @@ -450,7 +441,6 @@ def on_time(utctime: datetime.datetime | str | int | float, timezone: str) -> It
"fake_version_and_paths",
"skip_unwanted_test_types",
"wait_until_liveproxyd_ready",
"wait_until",
"on_time",
"set_timezone",
"Site",
Expand Down
3 changes: 1 addition & 2 deletions tests/testlib/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
from pathlib import Path
from typing import Any

from tests.testlib import wait_until
from tests.testlib.site import Site
from tests.testlib.utils import run
from tests.testlib.utils import run, wait_until

from cmk.utils.hostaddress import HostName

Expand Down
15 changes: 13 additions & 2 deletions tests/testlib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
# 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.

# pylint: disable=redefined-outer-name

import dataclasses
import logging
import os
Expand All @@ -13,6 +11,9 @@
import subprocess
import sys
import textwrap

# pylint: disable=redefined-outer-name
import time
from collections.abc import Callable, Iterator, Sequence
from contextlib import contextmanager
from pathlib import Path
Expand Down Expand Up @@ -562,3 +563,13 @@ def cse_openid_oauth_provider(site_url: str) -> Iterator[subprocess.Popen]:
execute(["rm", cognito_config])
if write_global_config:
execute(["rm", global_config])


def wait_until(condition: Callable[[], bool], timeout: float = 1, interval: float = 0.1) -> None:
start = time.time()
while time.time() - start < timeout:
if condition():
return # Success. Stop waiting...
time.sleep(interval)

raise Exception("Timeout waiting for %r to finish (Timeout: %d sec)" % (condition, timeout))
12 changes: 6 additions & 6 deletions tests/unit/cmk/gui/test_background_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import pytest

import tests.testlib as testlib
from tests.testlib.utils import wait_until

import cmk.utils.log
import cmk.utils.paths
Expand Down Expand Up @@ -133,15 +133,15 @@ def test_start_job() -> None:
assert status.state == JobStatusStates.INITIALIZED

job.start(job.execute_hello)
testlib.wait_until(job.is_active, timeout=5, interval=0.1)
wait_until(job.is_active, timeout=5, interval=0.1)

with pytest.raises(BackgroundJobAlreadyRunning):
job.start(job.execute_hello)
assert job.is_active()

job.finish_hello_event.set()

testlib.wait_until(
wait_until(
lambda: job.get_status().state
not in [JobStatusStates.INITIALIZED, JobStatusStates.RUNNING],
timeout=5,
Expand All @@ -161,7 +161,7 @@ def test_stop_job() -> None:
job = DummyBackgroundJob()
job.start(job.execute_endless)

testlib.wait_until(
wait_until(
lambda: "Hanging loop" in job.get_status().loginfo["JobProgressUpdate"],
timeout=5,
interval=0.1,
Expand Down Expand Up @@ -202,7 +202,7 @@ def test_job_status_not_started() -> None:
def test_job_status_while_running() -> None:
job = DummyBackgroundJob()
job.start(job.execute_endless)
testlib.wait_until(
wait_until(
lambda: "Hanging loop" in job.get_status().loginfo["JobProgressUpdate"],
timeout=5,
interval=0.1,
Expand All @@ -227,7 +227,7 @@ def test_job_status_while_running() -> None:
def test_job_status_after_stop() -> None:
job = DummyBackgroundJob()
job.start(job.execute_endless)
testlib.wait_until(
wait_until(
lambda: "Hanging loop" in job.get_status().loginfo["JobProgressUpdate"],
timeout=5,
interval=0.1,
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/cmk/utils/test_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
from pydantic import BaseModel
from pytest import MonkeyPatch

from tests.testlib import import_module_hack, wait_until
from tests.testlib import import_module_hack
from tests.testlib.utils import wait_until

import cmk.utils.debug
import cmk.utils.store as store
Expand Down

0 comments on commit 2b264f4

Please sign in to comment.