예제 #1
0
    def create_structured_log(self):
        filename = os.path.join(self.tempdir, 'slog.log')
        writer = qvarn.FileSlogWriter()
        writer.set_filename(filename)

        slog = qvarn.StructuredLog()
        slog.add_log_writer(writer, qvarn.FilterAllow())
        return slog, writer, filename
예제 #2
0
 def _load_filter_rules(self, conf, logname):
     opt = logname + '-filter'
     if conf.has_option('main', opt):
         filename = conf.get('main', opt)
         with open(filename) as f:
             filters = yaml.safe_load(f)
         return qvarn.construct_log_filter(filters)
     else:
         return qvarn.FilterAllow()
예제 #3
0
    def test_logs_to_two_files(self):
        filename1 = os.path.join(self.tempdir, 'slog1XS')
        writer1 = qvarn.FileSlogWriter()
        writer1.set_filename(filename1)

        filename2 = os.path.join(self.tempdir, 'slog2')
        writer2 = qvarn.FileSlogWriter()
        writer2.set_filename(filename2)

        slog = qvarn.StructuredLog()
        slog.add_log_writer(writer1, qvarn.FilterAllow())
        slog.add_log_writer(writer2, qvarn.FilterAllow())

        slog.log('test', msg_text='hello')
        objs1 = self.read_log_entries(writer1)
        objs2 = self.read_log_entries(writer2)

        self.assertEqual(objs1, objs2)
예제 #4
0
    def test_reopen_logs(self):
        class MockWriter(qvarn.SlogWriter):
            def __init__(self):
                self.written = False
                self.closed = False
                self.reopened = False

            def write(self, _):
                self.written = True

            def close(self):
                self.closed = True

            def reopen(self):
                self.reopened = True

        slog = qvarn.StructuredLog()
        writer1, writer2 = MockWriter(), MockWriter()
        slog.add_log_writer(writer1, qvarn.FilterAllow())
        slog.add_log_writer(writer2, qvarn.FilterAllow())
        slog.reopen()

        self.assertTrue(writer1.reopened)
        self.assertTrue(writer2.reopened)
예제 #5
0
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import argparse
import os
import sys

import yaml
import bottle
from six.moves import configparser

import qvarn

log = qvarn.StructuredLog()
log.add_log_writer(qvarn.StdoutSlogWriter(oneline=True), qvarn.FilterAllow())
qvarn.hijack_logging(log)

DEFAULT_CONFIG = {
    'main': {
        'specdir': '',
        'log-max-files': '10',
        'log-max-bytes': '10240',
        'log': 'syslog',
        'enable_access_log': 'false',
        'access_log_entry_chunk_size': '300',
    },
    'database': {
        'type': 'postgres',  # postgres, sqlite
        'host': 'localhost',
        'port': '5432',
예제 #6
0
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import argparse
import ConfigParser
import os
import sys
import yaml

import bottle

import qvarn

log = qvarn.StructuredLog()
log.add_log_writer(qvarn.NullSlogWriter(), qvarn.FilterAllow())
qvarn.hijack_logging(log)


class BackendApplication(object):
    '''Main program of a backend application.

    This class provides the logic for command line parsing, log file
    setup, and starting of HTTP service, plus other things that are
    common to all backend applications. Backend applications are
    expected to all have the same external interface, provided by this
    class.

    This class is parameterised by calling the
    ``set_storage_preparer``, ``add_resource`` and ``add_routes``
    methods. The application actually starts when UWSGI starts the
예제 #7
0
 def test_allows_if_rule_allows_and_no_include(self):
     allow = qvarn.FilterAllow()
     include = qvarn.FilterInclude({}, allow)
     self.assertTrue(include.allow(None))
예제 #8
0
 def test_allows_always(self):
     rule = qvarn.FilterAllow()
     self.assertTrue(rule.allow(None))
예제 #9
0
 def test_denies_if_all_rules_deny(self):
     rules = [qvarn.FilterDeny(), qvarn.FilterAllow()]
     any_rule = qvarn.FilterAny(rules)
     self.assertTrue(any_rule.allow(None))
예제 #10
0
 def test_allows_if_any_rule_allows(self):
     rules = [qvarn.FilterAllow()]
     any_rule = qvarn.FilterAny(rules)
     self.assertTrue(any_rule.allow(None))
예제 #11
0
 def test_denies_if_rule_allows_and_include_is_false(self):
     allow = qvarn.FilterAllow()
     include = qvarn.FilterInclude({'include': False}, allow)
     self.assertFalse(include.allow({}))
예제 #12
0
 def test_allows_if_rule_allows_and_include_is_true(self):
     allow = qvarn.FilterAllow()
     include = qvarn.FilterInclude({'include': True}, allow)
     self.assertTrue(include.allow({}))
예제 #13
0
args, argv = parser.parse_known_args()
argparse._sys.argv = [sys.argv[0]] + argv

app = qvarn.BackendApplication()

resource = setup_version_resource(app)

if qvarn.__version_info__ == (0, 81):
    app.add_resource(resource)
elif qvarn.__version_info__ > (0, 81):
    app.add_routes([resource])
else:
    raise Exception("Unsupported Qvarn version.")

qvarn_app = app.prepare_for_uwsgi(args.specdir)
if qvarn.__version_info__ > (0, 81):
    qvarn.log.add_log_writer(StdoutSlogWriter(), qvarn.FilterAllow())
    qvarn.log.add_log_writer(FileSlogWriter('/tmp/qvarn.log'), qvarn.FilterAllow())
else:
    qvarn.log.set_log_writer(StdoutSlogWriter())


def application(environ, start_response):
    request = Request(environ)
    handler = server.find_handler(request)

    if handler:
        return handler(request, compat.start_response(start_response))
    else:
        return qvarn_app(environ, start_response)