def test_default_passwords(capfd): setup_logging(verbose=False) azafea.config.Config() capture = capfd.readouterr() assert 'Did you forget to change the PostgreSQL password?' in capture.err assert 'Did you forget to change the Redis password?' in capture.err
def test_non_default_passwords(capfd, make_config): setup_logging(verbose=False) make_config({ 'postgresql': {'password': '******'}, 'redis': {'password': '******'}, }) capture = capfd.readouterr() assert 'Did you forget to change the PostgreSQL password?' not in capture.err assert 'Did you forget to change the Redis password?' not in capture.err
def test_verbose(capfd): setup_logging(verbose=True) log = logging.getLogger(__name__) log.debug('A debugging message') log.info('An informative message') log.warning('A scary warning') log.error('Oh no!') capture = capfd.readouterr() assert 'A debugging message' in capture.out assert 'An informative message' in capture.out assert 'A scary warning' in capture.err assert 'Oh no!' in capture.err
def test_start(capfd, monkeypatch, mock_sessionmaker): config = Config() setup_logging(verbose=config.main.verbose) with monkeypatch.context() as m: m.setattr(azafea.processor, 'Redis', MockRedis) m.setattr(azafea.model, 'sessionmaker', mock_sessionmaker) proc = azafea.processor.Processor('test-worker', config) # Prevent the processor from running its main loop proc._continue = False proc.start() proc.join() capture = capfd.readouterr() assert '{test-worker} Starting' in capture.out
def test_process_with_error(capfd, monkeypatch, make_config, mock_sessionmaker): def failing_process(*args, **kwargs): raise ValueError('Oh no!') def mock_get_callable(module_name, callable_name): return failing_process with monkeypatch.context() as m: m.setattr(azafea.config, 'get_callable', mock_get_callable) config = make_config({ 'main': { 'verbose': True }, 'queues': { 'some-queue': { 'handler': 'azafea.tests.test_processor' } }, }) setup_logging(verbose=config.main.verbose) with monkeypatch.context() as m: m.setattr(azafea.model, 'sessionmaker', mock_sessionmaker) m.setattr(azafea.processor, 'Redis', MockRedis) proc = azafea.processor.Processor('test-worker', config) proc.start() # Give the process a bit of time to start before sending the signal; 1s should be way enough # to pass at least once in the main loop time.sleep(1) os.kill(proc.pid, SIGTERM) proc.join() capture = capfd.readouterr() assert '{test-worker} Starting' in capture.out print(capture.out) assert "{test-worker} Pulled b'value' from the some-queue queue" in capture.out assert ( '{test-worker} An error occured while processing an event from the some-queue queue ' 'with azafea.tests.test_processor.failing_process\nDetails:' ) in capture.err assert 'ValueError: Oh no!' in capture.err assert 'Ran Redis command: LPUSH errors-some-queue value' in capture.out
def test_start(capfd, monkeypatch): config = Config() setup_logging(verbose=config.main.verbose) with monkeypatch.context() as m: m.setattr(azafea.controller, 'Processor', MockProcessor) controller = azafea.controller.Controller(config) controller.start() number = get_cpu_count() assert len(controller._processors) == number capture = capfd.readouterr() assert f'Starting the controller with {number} workers' in capture.out for i in range(1, number + 1): assert f'{{worker-{i}}} Starting' in capture.out
def test_new_event_no_payload_but_payload_given(capfd): from azafea.logging import setup_logging from azafea.event_processors.endless.metrics.events._base import SingularEvent setup_logging(verbose=False) class TestEventNoPayloadButPayloadGiven(SingularEvent): __tablename__ = 'test_singular_no_payload_but_payload_given' __event_uuid__ = '00000000-0000-0000-0000-000000000000' __payload_type__ = None payload = GLib.Variant('mv', GLib.Variant('i', 1)) TestEventNoPayloadButPayloadGiven(payload=payload) capture = capfd.readouterr() assert ( f'Metric event 00000000-0000-0000-0000-000000000000 takes no payload, ' 'but got <1>') in capture.err
def test_sigterm_handler(capfd, make_config): config = make_config({'main': {'number_of_workers': 1}}) setup_logging(verbose=config.main.verbose) with pytest.raises(SystemExit) as exc_info: controller = azafea.controller.Controller(config) controller._processors = [MockProcessor('test-worker', config)] controller._handle_exit_signals(SIGTERM, None) for proc in controller._processors: assert proc.joined assert proc.terminated capture = capfd.readouterr() assert 'Received SIGTERM, waiting for workers to finish…' in capture.out assert 'All workers finished, exiting' in capture.out assert exc_info.value.args == (0, )
def test_override_num_workers(capfd, monkeypatch, make_config): config = make_config({'main': {'number_of_workers': 1}}) setup_logging(verbose=config.main.verbose) with monkeypatch.context() as m: m.setattr(azafea.controller, 'Processor', MockProcessor) controller = azafea.controller.Controller(config) controller.start() number = get_cpu_count() assert len(controller._processors) == 1 capture = capfd.readouterr() assert 'Starting the controller with 1 worker' in capture.out assert '{worker-1} Starting' in capture.out if number > 1: assert f'{{worker-{number}}} Starting' not in capture.out
def test_start_then_sigterm(capfd, monkeypatch, make_config, mock_sessionmaker): def process(*args, **kwargs): pass def mock_get_callable(module_name, callable_name): return process with monkeypatch.context() as m: m.setattr(azafea.config, 'get_callable', mock_get_callable) config = make_config({ 'main': { 'verbose': True }, 'queues': { 'some-queue': { 'handler': 'azafea.tests.test_processor' } }, }) setup_logging(verbose=config.main.verbose) with monkeypatch.context() as m: m.setattr(azafea.processor, 'Redis', MockRedis) m.setattr(azafea.model, 'sessionmaker', mock_sessionmaker) proc = azafea.processor.Processor('test-worker', config) proc.start() # Give the process a bit of time to start before sending the signal; 1s should be way enough # to pass at least once in the main loop time.sleep(1) os.kill(proc.pid, SIGTERM) proc.join() capture = capfd.readouterr() assert '{test-worker} Starting' in capture.out assert "{test-worker} Pulled b'value' from the some-queue queue" in capture.out assert '{test-worker} Received SIGTERM, finishing the current task…' in capture.out
def test_reset_logging(capfd): setup_logging(verbose=False) log = logging.getLogger(__name__) log.debug('A debugging message') capture = capfd.readouterr() assert 'A debugging message' not in capture.out setup_logging(verbose=True) log.debug('A debugging message') capture = capfd.readouterr() assert 'A debugging message' in capture.out setup_logging(verbose=False) log.debug('A debugging message') capture = capfd.readouterr() assert 'A debugging message' not in capture.out