Exemplo n.º 1
0
    def test_badproxies(self, mock_BarkMiddleware, mock_get_handler,
                        mock_parse, mock_ProxyConfig, mock_SafeConfigParser,
                        mock_warn):
        local_conf = {
            'proxies.arg1': 'arg1',
            'proxies.arg2': 'arg2',
            'proxies.arg3': 'arg3',
        }

        filt = middleware.bark_filter({}, **local_conf)

        self.assertFalse(mock_SafeConfigParser.called)
        mock_ProxyConfig.assert_called_once_with(dict(
            arg1='arg1',
            arg2='arg2',
            arg3='arg3',
        ))
        self.assertFalse(mock_parse.called)
        self.assertFalse(mock_get_handler.called)
        mock_warn.assert_called_once_with(
            "Cannot configure proxy handling: option 'foo' is "
            "missing from the proxy configuration")
        self.assertFalse(mock_BarkMiddleware.called)

        mid = filt('app')

        mock_BarkMiddleware.assert_called_once_with('app', {}, None)
        self.assertEqual(mid, 'mid')
Exemplo n.º 2
0
    def test_proxies(self, mock_BarkMiddleware, mock_get_handler, mock_parse,
                     mock_ProxyConfig, mock_SafeConfigParser, mock_warn):
        local_conf = {
            'proxies.arg1': 'arg1',
            'proxies.arg2': 'arg2',
            'proxies.arg3': 'arg3',
        }

        filt = middleware.bark_filter({}, **local_conf)

        self.assertFalse(mock_SafeConfigParser.called)
        mock_ProxyConfig.assert_called_once_with(dict(
            arg1='arg1',
            arg2='arg2',
            arg3='arg3',
        ))
        self.assertFalse(mock_parse.called)
        self.assertFalse(mock_get_handler.called)
        self.assertFalse(mock_warn.called)
        self.assertFalse(mock_BarkMiddleware.called)

        mid = filt('app')

        mock_BarkMiddleware.assert_called_once_with('app', {}, 'proxies')
        self.assertEqual(mid, 'mid')
Exemplo n.º 3
0
    def test_missingformat(self, mock_BarkMiddleware, mock_get_handler,
                           mock_parse, mock_ProxyConfig, mock_SafeConfigParser,
                           mock_warn):
        local_conf = {
            'ignored': 'should be ignored',
            'log1.arg1': 'argument 1',
            'log1.arg2': 'argument 2',
            'log2.format': 'format string for log2',
            'log2.type': 'other',
            'log2.arg3': 'argument 3',
            'log2.arg4': 'argument 4',
        }

        filt = middleware.bark_filter({}, **local_conf)

        self.assertFalse(mock_SafeConfigParser.called)
        self.assertFalse(mock_ProxyConfig.called)
        mock_parse.assert_called_once_with('format string for log2')
        mock_get_handler.assert_called_once_with('other', 'log2',
                                                 dict(arg3='argument 3',
                                                      arg4='argument 4'))
        mock_warn.assert_called_once_with(
            "No format specified for log 'log1'; skipping.")
        self.assertFalse(mock_BarkMiddleware.called)

        mid = filt('app')

        mock_BarkMiddleware.assert_called_once_with('app', {
            'log2': ('format string for log2', 'other'),
        }, None)
        self.assertEqual(mid, 'mid')
Exemplo n.º 4
0
    def test_together(self, mock_BarkMiddleware, mock_get_handler, mock_parse,
                      mock_ProxyConfig, mock_SafeConfigParser, mock_warn):
        local_conf = {
            'ignored': 'should be ignored',
            'config': '/etc/config',
            'log1.format': 'format string for log1',
            'log1.arg1': 'argument 1',
            'log1.arg2': 'argument 2',
            'log2.format': 'format string for log2',
            'log2.type': 'other',
            'log2.arg3': 'argument 3',
            'log2.arg4': 'argument 4',
            'proxies.arg8': 'argument 8',
            'proxies.arg9': 'argument 9',
        }

        filt = middleware.bark_filter({}, **local_conf)

        mock_SafeConfigParser.assert_called_once_with()
        mock_SafeConfigParser.return_value.assert_has_calls([
            mock.call.read(['/etc/config']),
            mock.call.sections(),
            mock.call.options('log1'),
            mock.call.options('proxies'),
        ])
        mock_ProxyConfig.assert_called_once_with(dict(
            arg6='argument 6',
            arg7='argument 7',
            arg8='argument 8',
            arg9='argument 9',
        ))
        mock_parse.assert_has_calls([
            mock.call('format string for log1'),
            mock.call('format string for log2'),
        ], any_order=True)
        mock_get_handler.assert_has_calls([
            mock.call('file', 'log1',
                      dict(arg1='argument 1', arg2='argument 2',
                           arg5='argument 5')),
            mock.call('other', 'log2',
                      dict(arg3='argument 3', arg4='argument 4')),
        ], any_order=True)
        self.assertFalse(mock_warn.called)
        self.assertFalse(mock_BarkMiddleware.called)

        mid = filt('app')

        mock_BarkMiddleware.assert_called_once_with('app', {
            'log1': ('format string for log1', 'file'),
            'log2': ('format string for log2', 'other'),
        }, 'proxies')
        self.assertEqual(mid, 'mid')
Exemplo n.º 5
0
    def test_noconf(self, mock_BarkMiddleware, mock_get_handler, mock_parse,
                    mock_ProxyConfig, mock_SafeConfigParser, mock_warn):
        filt = middleware.bark_filter({})

        self.assertFalse(mock_SafeConfigParser.called)
        self.assertFalse(mock_ProxyConfig.called)
        self.assertFalse(mock_parse.called)
        self.assertFalse(mock_get_handler.called)
        self.assertFalse(mock_warn.called)
        self.assertFalse(mock_BarkMiddleware.called)

        mid = filt('app')

        mock_BarkMiddleware.assert_called_once_with('app', {}, None)
        self.assertEqual(mid, 'mid')
Exemplo n.º 6
0
def construct(delay=None, proxies=None, **kwargs):
    # Build the configuration
    local_conf = {}
    for logname, format in kwargs.items():
        local_conf['%s.format' % logname] = format
        local_conf['%s.type' % logname] = 'memory'

    # Add proxy information
    if proxies:
        for key, value in proxies.items():
            local_conf['proxies.%s' % key] = value

    # Construct the filter
    with mock.patch('bark.handlers._lookup_handler',
                    return_value=MemoryHandler):
        filt = middleware.bark_filter({}, **local_conf)

    # Construct the application
    app = Application(delay=delay)

    # Return the stack
    return filt(app)
Exemplo n.º 7
0
    def test_missinghandler(self, mock_BarkMiddleware, mock_get_handler,
                            mock_parse, mock_ProxyConfig,
                            mock_SafeConfigParser, mock_warn):
        local_conf = {
            'ignored': 'should be ignored',
            'log1.format': 'format string for log1',
            'log1.arg1': 'argument 1',
            'log1.arg2': 'argument 2',
            'log2.format': 'format string for log2',
            'log2.type': 'other',
            'log2.arg3': 'argument 3',
            'log2.arg4': 'argument 4',
        }

        filt = middleware.bark_filter({}, **local_conf)

        self.assertFalse(mock_SafeConfigParser.called)
        self.assertFalse(mock_ProxyConfig.called)
        mock_parse.assert_has_calls([
            mock.call('format string for log1'),
            mock.call('format string for log2'),
        ], any_order=True)
        mock_get_handler.assert_has_calls([
            mock.call('file', 'log1',
                      dict(arg1='argument 1', arg2='argument 2')),
            mock.call('other', 'log2',
                      dict(arg3='argument 3', arg4='argument 4')),
        ], any_order=True)
        print mock_warn.mock_calls
        mock_warn.assert_called_once_with(
            "Cannot load handler of type 'file' for log 'log1': failure")
        self.assertFalse(mock_BarkMiddleware.called)

        mid = filt('app')

        mock_BarkMiddleware.assert_called_once_with('app', {
            'log2': ('format string for log2', 'other'),
        }, None)
        self.assertEqual(mid, 'mid')