Skip to content

Commit

Permalink
remove testsuite logging plugin (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitek committed May 30, 2024
1 parent 8a30a37 commit d1a92a9
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 303 deletions.
10 changes: 0 additions & 10 deletions tests/daemons/test_service_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,13 @@ def dummy_daemon(mockserver):
return pathlib.Path(__file__).parent / 'daemons/dummy_daemon.py'


@pytest.fixture
def logger_plugin(pytestconfig):
return pytestconfig.pluginmanager.getplugin('testsuite_logger')


async def test_service_daemon(
mockserver,
dummy_daemon,
logger_plugin,
health_check,
):
async with service_daemon.start(
args=[sys.executable, dummy_daemon],
logger_plugin=logger_plugin,
health_check=health_check,
subprocess_options={'stdout': subprocess.PIPE, 'bufsize': 0},
):
Expand All @@ -65,7 +58,6 @@ async def test_service_daemon(
async def test_service_wait_custom_health(
mockserver,
dummy_daemon,
logger_plugin,
pytestconfig,
wait_service_started,
):
Expand Down Expand Up @@ -99,15 +91,13 @@ async def test_service_daemon_failure(
dummy_daemon,
daemon_args,
expected_message,
logger_plugin,
health_check,
):
with pytest.raises(spawn.ExitCodeError) as exc:
start_command = [dummy_daemon] + daemon_args
async with service_daemon.start(
start_command,
health_check=health_check,
logger_plugin=logger_plugin,
subprocess_options={'stdout': subprocess.PIPE, 'bufsize': 0},
):
pass
Expand Down
22 changes: 6 additions & 16 deletions testsuite/daemons/pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,8 @@ async def aclose(self) -> None:
class _DaemonStore:
cells: Dict[str, DaemonInstance]

def __init__(self, logger_plugin) -> None:
def __init__(self) -> None:
self.cells = {}
self.logger_plugin = logger_plugin

async def aclose(self) -> None:
for daemon in self.cells.values():
Expand Down Expand Up @@ -97,9 +96,7 @@ def has_running_daemons(self) -> bool:
return False

async def _close_daemon(self, daemon: DaemonInstance):
with self.logger_plugin.temporary_suspend() as log_manager:
await daemon.aclose()
log_manager.clear()
await daemon.aclose()


class EnsureDaemonStartedFixture(fixture_class.Fixture):
Expand Down Expand Up @@ -158,16 +155,11 @@ def __call__(
warnings.warn(CHECK_URL_DEPRECATION, PendingDeprecationWarning)

pytestconfig = self._fixture_pytestconfig
logger_plugin = pytestconfig.pluginmanager.getplugin(
'testsuite_logger',
)

shutdown_timeout = (
self._fixture_pytestconfig.option.service_shutdown_timeout
)
shutdown_timeout = pytestconfig.option.service_shutdown_timeout
if shutdown_signal is None:
shutdown_signal = SHUTDOWN_SIGNALS[
self._fixture_pytestconfig.option.service_shutdown_signal
pytestconfig.option.service_shutdown_signal
]

health_check = service_daemon.make_health_check(
Expand Down Expand Up @@ -198,7 +190,6 @@ async def spawn():
session_factory=self._fixture_service_client_session_factory,
subprocess_options=subprocess_options,
setup_service=setup_service,
logger_plugin=logger_plugin,
subprocess_spawner=subprocess_spawner,
stdout_handler=stdout_handler,
stderr_handler=stderr_handler,
Expand Down Expand Up @@ -434,9 +425,8 @@ def service_client_options(


@pytest.fixture(scope='session')
async def _global_daemon_store(loop, pytestconfig):
logger_plugin = pytestconfig.pluginmanager.getplugin('testsuite_logger')
store = _DaemonStore(logger_plugin)
async def _global_daemon_store(loop):
store = _DaemonStore()
async with compat.aclosing(store):
yield store

Expand Down
36 changes: 16 additions & 20 deletions testsuite/daemons/service_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ async def start(
*,
health_check: HealthCheckType,
session_factory: ClientSessionFactory = aiohttp.ClientSession,
logger_plugin,
env: Optional[Dict[str, str]] = None,
shutdown_signal: int = signal.SIGINT,
shutdown_timeout: float = 120,
Expand All @@ -44,25 +43,22 @@ async def start(
stdout_handler=None,
stderr_handler=None,
) -> AsyncGenerator[Optional[subprocess.Popen], None]:
with logger_plugin.temporary_suspend() as log_manager:
async with session_factory() as session:
async with _service_daemon(
args=args,
env=env,
shutdown_signal=shutdown_signal,
shutdown_timeout=shutdown_timeout,
poll_retries=poll_retries,
subprocess_options=subprocess_options,
setup_service=setup_service,
subprocess_spawner=subprocess_spawner,
health_check=health_check,
session=session,
stdout_handler=stdout_handler,
stderr_handler=stderr_handler,
) as process:
log_manager.clear()
log_manager.resume()
yield process
async with session_factory() as session:
async with _service_daemon(
args=args,
env=env,
shutdown_signal=shutdown_signal,
shutdown_timeout=shutdown_timeout,
poll_retries=poll_retries,
subprocess_options=subprocess_options,
setup_service=setup_service,
subprocess_spawner=subprocess_spawner,
health_check=health_check,
session=session,
stdout_handler=stdout_handler,
stderr_handler=stderr_handler,
) as process:
yield process


async def service_wait(
Expand Down
25 changes: 23 additions & 2 deletions testsuite/environment/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import subprocess
import sys

from testsuite.logging import logger as testsuite_logger
from testsuite.utils import colors

from . import control
from . import shell
Expand All @@ -24,6 +24,27 @@
logger = logging.getLogger(__name__)


class ColoredLevelFormatter(logging.Formatter):
LEVEL_COLORS = {
logging.DEBUG: colors.Colors.GRAY,
logging.INFO: colors.Colors.BRIGHT_GREEN,
logging.WARNING: colors.Colors.YELLOW,
logging.ERROR: colors.Colors.RED,
logging.CRITICAL: colors.Colors.BRIGHT_RED,
}

def __init__(self, *, colors_enabled=False):
super().__init__()
self._colors_enabled = colors_enabled

def format(self, record: logging.LogRecord):
message = super().format(record)
if not self._colors_enabled:
return f'{record.levelname} {message}'
color = self.LEVEL_COLORS.get(record.levelno, colors.Colors.DEFAULT)
return f'{color}{record.levelname}{colors.Colors.DEFAULT} {message}'


def csv_arg(value: str):
result = []
for arg in value.split(','):
Expand Down Expand Up @@ -115,7 +136,7 @@ def _setup_logging(log_level):
root_logger.setLevel(log_level)
handler = logging.StreamHandler(sys.stderr)
handler.setFormatter(
testsuite_logger.ColoredLevelFormatter(
ColoredLevelFormatter(
colors_enabled=sys.stderr.isatty(),
),
)
Expand Down
Empty file removed testsuite/logging/__init__.py
Empty file.
147 changes: 0 additions & 147 deletions testsuite/logging/logger.py

This file was deleted.

Loading

0 comments on commit d1a92a9

Please sign in to comment.