Example #1
0
def test_process_config(mocker, has_actions):
    class DummyPattern(object):
        def __init__(self, pattern=None):
            self.pattern = pattern

        def __eq__(self, other):
            return self.pattern == other.pattern

        def __hash__(self):
            return hash(self.pattern)

    warning_echo = mocker.patch('logstapo.config.warning_echo')
    mocker.patch('logstapo.config._Pattern', DummyPattern)
    action_from_config = mocker.patch('logstapo.actions.Action.from_config')
    data = {
        'regexps': {
            '__src': '(?P<source>.)',
            'rex': '%(src)(?P<message>.)'
        },
        'logs': {
            'test': {
                'file': 'test.log',
                'regex': 'rex',
                'actions': 'spam' if has_actions else [],
                'garbage': 'crap*',
                'ignore': 'boring'
            }
        },
        'actions': {
            'spam': {
                'type': 'smtp',
                'to': '*****@*****.**'
            }
        } if has_actions else {}
    }
    rv = config.process_config(data)
    assert warning_echo.called == (not has_actions)
    # regexps
    assert rv['regexps'] == {'rex': re.compile('(?P<source>.)(?P<message>.)')}
    # logs
    assert rv['logs'] == {
        'test': {
            'files': ('test.log', ),
            'regexps': ('rex', ),
            'actions': ('spam', ) if has_actions else (),
            'ignore': {
                DummyPattern(): [DummyPattern('boring')]
            },
            'garbage': [DummyPattern('crap*')]
        }
    }
    # actions
    if has_actions:
        assert rv['actions'].keys() == {'spam'}
        action_from_config.assert_called_once_with('smtp',
                                                   {'to': '*****@*****.**'})
    else:
        assert not rv['actions']
        assert not action_from_config.called
Example #2
0
def test_process_config_invalid():
    with pytest.raises(config.ConfigError):
        config.process_config(None)
    with pytest.raises(config.ConfigError):
        config.process_config([])
    with pytest.raises(config.ConfigError):
        config.process_config({})
    data = {'regexps': {'test': '(?P<source>.)(?P<message>.)'},
            'logs': {'test': {}}}
    with pytest.raises(config.ConfigError):
        config.process_config(data)
Example #3
0
def _config_callback(ctx, param, value):
    try:
        data = parse_config(value)
        config = process_config(data)
    except ConfigError as exc:
        error_echo('Could not load config file')
        error_echo(str(exc))
        ctx.exit(1)
    else:
        config['verbosity'] = ctx.params['verbose']
        config['debug'] = ctx.params['debug']
        config['dry_run'] = ctx.params['dry_run']
        return config
Example #4
0
def _config_callback(ctx, param, value):
    try:
        data = parse_config(value)
        config = process_config(data)
    except ConfigError as exc:
        error_echo('Could not load config file')
        error_echo(str(exc))
        ctx.exit(1)
    else:
        config['verbosity'] = ctx.params['verbose']
        config['debug'] = ctx.params['debug']
        config['dry_run'] = ctx.params['dry_run']
        return config
Example #5
0
def test_process_config_invalid():
    with pytest.raises(config.ConfigError):
        config.process_config(None)
    with pytest.raises(config.ConfigError):
        config.process_config([])
    with pytest.raises(config.ConfigError):
        config.process_config({})
    data = {
        'regexps': {
            'test': '(?P<source>.)(?P<message>.)'
        },
        'logs': {
            'test': {}
        }
    }
    with pytest.raises(config.ConfigError):
        config.process_config(data)
Example #6
0
def test_process_config(mocker, has_actions):
    class DummyPattern(object):
        def __init__(self, pattern=None):
            self.pattern = pattern

        def __eq__(self, other):
            return self.pattern == other.pattern

        def __hash__(self):
            return hash(self.pattern)

    warning_echo = mocker.patch('logstapo.config.warning_echo')
    mocker.patch('logstapo.config._Pattern', DummyPattern)
    action_from_config = mocker.patch('logstapo.actions.Action.from_config')
    data = {'regexps': {'__src': '(?P<source>.)', 'rex': '%(src)(?P<message>.)'},
            'logs': {'test': {'file': 'test.log',
                              'regex': 'rex',
                              'actions': 'spam' if has_actions else [],
                              'garbage': 'crap*',
                              'ignore': 'boring'}},
            'actions': {'spam': {'type': 'smtp', 'to': '*****@*****.**'}} if has_actions else {}}
    rv = config.process_config(data)
    assert warning_echo.called == (not has_actions)
    # regexps
    assert rv['regexps'] == {'rex': re.compile('(?P<source>.)(?P<message>.)')}
    # logs
    assert rv['logs'] == {'test': {'files': ('test.log',),
                                   'regexps': ('rex',),
                                   'actions': ('spam',) if has_actions else (),
                                   'ignore': {DummyPattern(): [DummyPattern('boring')]},
                                   'garbage': [DummyPattern('crap*')]}}
    # actions
    if has_actions:
        assert rv['actions'].keys() == {'spam'}
        action_from_config.assert_called_once_with('smtp', {'to': '*****@*****.**'})
    else:
        assert not rv['actions']
        assert not action_from_config.called