Skip to content

Commit

Permalink
add support for sub log directories in requests/logs
Browse files Browse the repository at this point in the history
grizzly implementation of Biometria-se#248.
  • Loading branch information
mgor committed Aug 24, 2023
1 parent 3e50850 commit a564f1e
Show file tree
Hide file tree
Showing 5 changed files with 351 additions and 277 deletions.
5 changes: 5 additions & 0 deletions grizzly/tasks/clients/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,12 @@ def __init__(

context_root = environ.get('GRIZZLY_CONTEXT_ROOT', None)
assert context_root is not None, 'environment variable GRIZZLY_CONTEXT_ROOT is not set!'

self.log_dir = Path(context_root) / 'logs'
log_dir = environ.get('GRIZZLY_LOG_DIR', None)
if log_dir is not None:
self.log_dir /= log_dir

self.log_dir.mkdir(parents=True, exist_ok=True)
self._scenario = copy(self.__scenario__)
self._scenario._tasks = self.__scenario__._tasks
Expand Down
7 changes: 6 additions & 1 deletion grizzly/users/base/request_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,13 @@ def __init__(self, environment: Environment, *args: Tuple[Any, ...], **kwargs: D
self.response_event.add_listener(self.request_logger)

self.log_dir = os.path.join(os.environ.get('GRIZZLY_CONTEXT_ROOT', '.'), 'logs')

log_dir = os.environ.get('GRIZZLY_LOG_DIR', None)
if log_dir is not None:
self.log_dir = os.path.join(self.log_dir, log_dir)

if not os.path.exists(self.log_dir):
os.mkdir(self.log_dir)
os.makedirs(self.log_dir, exist_ok=True)

self._context = merge_dicts(super().context(), RequestLogger._context)

Expand Down
107 changes: 63 additions & 44 deletions tests/unit/test_grizzly/tasks/clients/test___init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import logging

from os import environ
from pathlib import Path

import pytest

from _pytest.logging import LogCaptureFixture
Expand All @@ -12,67 +15,83 @@
from tests.fixtures import GrizzlyFixture, MockerFixture


def test_task_failing(grizzly_fixture: GrizzlyFixture, mocker: MockerFixture, caplog: LogCaptureFixture) -> None:

@client('test')
class TestTask(ClientTask):
__scenario__ = grizzly_fixture.grizzly.scenario

def get(self, parent: GrizzlyScenario) -> GrizzlyResponse:
with self.action(parent):
raise RuntimeError('failed get')
@pytest.mark.parametrize('log_prefix', [False, True,])
def test_task_failing(grizzly_fixture: GrizzlyFixture, mocker: MockerFixture, caplog: LogCaptureFixture, log_prefix: bool) -> None:
try:
@client('test')
class TestClientTask(ClientTask):
__scenario__ = grizzly_fixture.grizzly.scenario

def put(self, parent: GrizzlyScenario) -> GrizzlyResponse:
return None, 'put'
def get(self, parent: GrizzlyScenario) -> GrizzlyResponse:
with self.action(parent):
raise RuntimeError('failed get')

parent = grizzly_fixture(scenario_type=IteratorScenario)
def put(self, parent: GrizzlyScenario) -> GrizzlyResponse:
return None, 'put'

assert isinstance(parent, IteratorScenario)
if log_prefix:
environ['GRIZZLY_LOG_DIR'] = 'foobar'

parent.user._context.update({'test': 'was here'})
parent = grizzly_fixture(scenario_type=IteratorScenario)

task_factory = TestTask(RequestDirection.FROM, 'test://foo.bar', 'dummy-stuff')
assert isinstance(parent, IteratorScenario)

task = task_factory()
parent.user._context.update({'test': 'was here'})

assert task_factory._context.get('test', None) is None
parent.user._scenario.failure_exception = StopUser
task_factory = TestClientTask(RequestDirection.FROM, 'test://foo.bar', 'dummy-stuff')

with pytest.raises(StopUser):
task(parent)
assert (Path(environ['GRIZZLY_CONTEXT_ROOT']) / 'logs').exists()

assert task_factory._context.get('test', None) == 'was here'
if log_prefix:
assert (Path(environ['GRIZZLY_CONTEXT_ROOT']) / 'logs' / 'foobar').exists()
else:
assert not (Path(environ['GRIZZLY_CONTEXT_ROOT']) / 'logs' / 'foobar').exists()

parent.user._scenario.failure_exception = RestartScenario
parent.user._context.update({'test': 'is here', 'foo': 'bar'})
task = task_factory()

assert task_factory._context.get('foo', None) is None
assert task_factory._context.get('test', None) is None
parent.user._scenario.failure_exception = StopUser

with pytest.raises(RestartScenario):
task(parent)
with pytest.raises(StopUser):
task(parent)

assert parent.user._context.get('test', None) == 'is here'
assert parent.user._context.get('foo', None) == 'bar'
assert task_factory._context.get('test', None) == 'was here'

parent.user._scenario.failure_exception = None
parent.user._scenario.failure_exception = RestartScenario
parent.user._context.update({'test': 'is here', 'foo': 'bar'})

task(parent)
assert task_factory._context.get('foo', None) is None

log_error_mock = mocker.patch.object(parent.stats, 'log_error')
mocker.patch.object(parent, 'on_start', return_value=None)
mocker.patch.object(parent, 'wait', side_effect=[NotImplementedError, NotImplementedError])
parent.user.environment.catch_exceptions = True
parent.user._scenario.failure_exception = RestartScenario
with pytest.raises(RestartScenario):
task(parent)

parent.tasks.clear()
parent._task_queue.clear()
parent._task_queue.append(task)
parent.task_count = 1
assert parent.user._context.get('test', None) == 'is here'
assert parent.user._context.get('foo', None) == 'bar'

with pytest.raises(NotImplementedError):
with caplog.at_level(logging.INFO):
parent.run()
parent.user._scenario.failure_exception = None

log_error_mock.assert_called_once_with(None)
task(parent)

assert 'restarting scenario' in '\n'.join(caplog.messages)
log_error_mock = mocker.patch.object(parent.stats, 'log_error')
mocker.patch.object(parent, 'on_start', return_value=None)
mocker.patch.object(parent, 'wait', side_effect=[NotImplementedError, NotImplementedError])
parent.user.environment.catch_exceptions = True
parent.user._scenario.failure_exception = RestartScenario

parent.tasks.clear()
parent._task_queue.clear()
parent._task_queue.append(task)
parent.task_count = 1

with pytest.raises(NotImplementedError):
with caplog.at_level(logging.INFO):
parent.run()

log_error_mock.assert_called_once_with(None)

assert 'restarting scenario' in '\n'.join(caplog.messages)
finally:
try:
del environ['GRIZZLY_LOG_DIR']
except:
pass
Loading

0 comments on commit a564f1e

Please sign in to comment.