Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix test failures #201

Merged
merged 10 commits into from
Feb 29, 2024
4 changes: 3 additions & 1 deletion trollflow2/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,10 @@ def setup_queued_logging(log_queue, config=None):
def remove_handlers_from_config(config):
"""Remove handlers from config."""
config.pop("handlers", None)
for logger in config["loggers"]:
for logger in config.get("loggers", []):
config["loggers"][logger].pop("handlers", None)
if config.get("root", None):
config["root"].pop("handlers", None)


def queued_logging(func):
Expand Down
26 changes: 13 additions & 13 deletions trollflow2/tests/test_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,11 +382,11 @@ def setUp(self):
def test_run_does_not_call_process_directly(self):
"""Test that process is called through Process."""
from trollflow2.launcher import Runner
with mock.patch('trollflow2.launcher.yaml.load'),\
mock.patch('trollflow2.launcher.open'),\
mock.patch('trollflow2.launcher.generate_messages') as generate_messages,\
mock.patch('trollflow2.launcher.process') as process,\
mock.patch('trollflow2.launcher.check_results'),\
with mock.patch('trollflow2.launcher.yaml.load'), \
mock.patch('trollflow2.launcher.open'), \
mock.patch('trollflow2.launcher.generate_messages') as generate_messages, \
mock.patch('trollflow2.launcher.process') as process, \
mock.patch('trollflow2.launcher.check_results'), \
mock.patch('multiprocessing.get_context'):
generate_messages.side_effect = ['foo', KeyboardInterrupt]
prod_list = {'product_list': {}}
Expand All @@ -400,8 +400,8 @@ def test_run_does_not_call_process_directly(self):
def test_run_relies_on_listener(self):
"""Test running relies on listener."""
from trollflow2.launcher import Runner
with mock.patch('trollflow2.launcher.yaml.load') as yaml_load,\
mock.patch('trollflow2.launcher.open'),\
with mock.patch('trollflow2.launcher.yaml.load') as yaml_load, \
mock.patch('trollflow2.launcher.open'), \
mock.patch('multiprocessing.get_context') as get_context, \
mock.patch('trollflow2.launcher.ListenerContainer') as lc_:
msg = mock.MagicMock()
Expand All @@ -420,7 +420,7 @@ def test_run_relies_on_listener(self):
runner.run()
except KeyboardInterrupt:
pass
listener.output_queue.called_once()
listener.output_queue.get.assert_called_once()
lc_.assert_called_with(addresses=None, nameserver='localhost',
topics=['/topic1', '/topic2'])
# Subscriber topics are removed from config
Expand Down Expand Up @@ -451,9 +451,9 @@ def test_subprocess_is_spawned(self):
def run_on_a_simple_product_list(config):
"""Run a simple (fake) product list."""
from trollflow2.launcher import Runner
with mock.patch('trollflow2.launcher.yaml.load') as yaml_load,\
mock.patch('trollflow2.launcher.open'),\
mock.patch('multiprocessing.get_context') as get_context,\
with mock.patch('trollflow2.launcher.yaml.load') as yaml_load, \
mock.patch('trollflow2.launcher.open'), \
mock.patch('multiprocessing.get_context') as get_context, \
mock.patch('trollflow2.launcher.ListenerContainer') as lc_:

msg = mock.MagicMock()
Expand Down Expand Up @@ -486,8 +486,8 @@ def setUp(self):
def test_run_keyboard_interrupt(self):
"""Test interrupting the run with a ctrl-C."""
from trollflow2.launcher import Runner
with mock.patch('trollflow2.launcher.yaml.load'),\
mock.patch('trollflow2.launcher.open'),\
with mock.patch('trollflow2.launcher.yaml.load'), \
mock.patch('trollflow2.launcher.open'), \
mock.patch('trollflow2.launcher.ListenerContainer') as lc_:
listener = mock.MagicMock()
get = mock.Mock()
Expand Down
44 changes: 42 additions & 2 deletions trollflow2/tests/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@

import pytest

from trollflow2.logging import (create_logged_process, logging_on,
queued_logging)
from trollflow2.logging import (DEFAULT_LOG_CONFIG, create_logged_process,
logging_on, queued_logging)


def test_queued_logging_has_a_listener():
Expand Down Expand Up @@ -129,6 +129,7 @@ def run_subprocess(loggers):
proc = create_logged_process(target=fun, args=(loggers,))
proc.start()
proc.join()
return proc


@queued_logging
Expand Down Expand Up @@ -182,3 +183,42 @@ def duplicate_lines(contents):
"""Make sure there are no duplicate lines."""
lines = contents.strip().split("\n")
return len(lines) != len(set(lines))


def test_logging_config_without_loggers(tmp_path):
"""Test that the log configs without loggers work."""
logfile = tmp_path / "mylog"
LOG_CONFIG_TO_FILE = {'version': 1,
'formatters': {'simple': {'format': '%(asctime)s - %(name)s - %(levelname)s - %(message)s'}},
'handlers': {'file': {'class': 'logging.FileHandler',
'filename': logfile,
'formatter': 'simple'}},
"root": {"level": "DEBUG", "handlers": ["file"]}
}

with logging_on(LOG_CONFIG_TO_FILE):
run_subprocess(["foo1", "foo2"])
with open(logfile) as fd:
file_contents = fd.read()

assert not duplicate_lines(file_contents)
assert "root debug" in file_contents
assert "root info" in file_contents
assert "root warning" in file_contents


def test_default_logging_config_works_with_subprocesses(capsys):
"""Test that the default log config works."""
LOG_CONFIG_TO_FILE = DEFAULT_LOG_CONFIG
captured = capsys.readouterr()
with logging_on(LOG_CONFIG_TO_FILE):
proc = run_subprocess(["foo1", "foo2"])
captured = capsys.readouterr()
assert proc.exitcode == 0
err = captured.err
assert not duplicate_lines(err)
assert "root debug" in err
assert "root info" in err
assert "root warning" in err
assert "foo1 debug" in err
assert "foo2 debug" in err
6 changes: 3 additions & 3 deletions trollflow2/tests/test_trollflow2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1552,9 +1552,9 @@ def test_discard_old_data(self):
with mock.patch('trollflow2.plugins.get_config_value') as get_config_value:
get_config_value.return_value = None
job = {'product_list': None, 'input_mda': {'start_time': dt.datetime(2020, 3, 18)}}
self.assertIsNone(check_metadata(job))
get_config_value.return_value = {'start_time': -2e6}
self.assertIsNone(check_metadata(job))
assert check_metadata(job) is None
get_config_value.return_value = {'start_time': -20e6}
assert check_metadata(job) is None
get_config_value.return_value = {'start_time': -60}
with self.assertRaises(AbortProcessing):
check_metadata(job)
Expand Down
Loading