Exemplo n.º 1
0
 def test_parser_python(self):
     snipe.filters.parser = Parser(debug=True)
     self.assertEqual(str(makefilter("$'True'")), "$'True'")
     self.assertEqual(str(makefilter('$"True"')), "$'True'")
     self.assertEqual(str(makefilter('$"True"')), "$'True'")
     self.assertEqual(str(makefilter('$"True or \'flase\'"')),
                      "$\"True or 'flase'\"")
Exemplo n.º 2
0
 def test_Not(self):
     self.assertEqual(str(Not(Yes())), 'not yes')
     self.assertEqual(repr(Not(Yes())), 'Not(Yes())')
     self.assertEqual(Not(Yes()), Not(Yes()))
     self.assertNotEqual(Not(Yes()), Not(No()))
     self.assertEqual(hash(Not(Yes())), hash(Not(Yes())))
     self.assertNotEqual(hash(Not(Yes())), hash(Not(No())))
     self.assertTrue(Not(No()).simplify({}))
     self.assertFalse(
         makefilter('not foo == "bar"').simplify({'foo': 'bar'}))
     self.assertEqual(
         makefilter('not foo == "bar"').simplify({}),
         Not(Compare('==', 'foo', 'bar')))
     self.assertEqual(str(Not(And(Yes(), No()))), 'not (yes and no)')
Exemplo n.º 3
0
    async def test_filter_save(self):
        f = mocks.FE()
        w = messager.Messager(f)

        self.assertEqual(w.default_filter, 'filter default')

        F1 = 'flig == "quoz"'
        w.filter = filters.makefilter(F1)

        await w.filter_save(True)

        self.assertEqual(w.default_filter, F1)

        F2 = 'flig'
        w.filter = filters.makefilter(F2)
        w.read_oneof = returning('quoz')
        await w.filter_save()

        self.assertEqual(f.context.conf['filter']['quoz'], F2)
Exemplo n.º 4
0
    def test_filter_save(self):
        f = mocks.FE()
        w = messager.Messager(f)

        self.assertEqual(w.default_filter, 'filter default')

        F1 = 'flig == "quoz"'
        w.filter = filters.makefilter(F1)
        for _ in w.filter_save(True):
            pass

        self.assertEqual(w.default_filter, F1)

        F2 = 'flig'
        w.filter = filters.makefilter(F2)
        w.read_oneof = returning('quoz')
        for _ in w.filter_save():
            pass

        self.assertEqual(f.context.conf['filter']['quoz'], F2)
Exemplo n.º 5
0
    async def test(self):
        context = mocks.Context()
        synth = SyntheticBackend(context)
        startup = messages.StartupBackend(context)
        sink = messages.SinkBackend(context)
        a = messages.AggregatorBackend(context, [startup, synth, sink])
        await a.start()
        self.assertEqual(startup.count(), 1)
        self.assertEqual(synth.count(), 1)
        self.assertEqual(a.count(), 3)
        self.assertEqual(len(list(a.walk(a.latest(), False))), 3)
        self.assertEqual(sink.count(), 0)
        await a.send('sink', 'a message')
        self.assertEqual(sink.count(), 1)
        self.assertEqual(sink.messages[0].body, 'a message')
        await a.send('sink', 'a message\x0bwith a vt')
        self.assertEqual(sink.messages[1].body, 'a message\nwith a vt')
        sink.SOFT_NEWLINES = True
        await a.send('sink', 'another message\x0bwith a vt')
        self.assertEqual(sink.messages[2].body, 'another message with a vt')
        with self.assertRaises(util.SnipeException):
            await a.send('', 'a message')
        with self.assertRaises(util.SnipeException):
            await a.send('fnord', 'a message')
        with self.assertRaises(util.SnipeException):
            await a.send('s', 'a message')
        self.assertEqual(a.count(), 6)
        self.assertEqual(len(list(a.walk(a.latest(), False))), 6)
        self.assertEqual(len(list(a.walk(a.earliest()))), 6)
        self.assertEqual(len(list(a.walk(a.earliest(), search=True))), 5)
        self.assertEqual(
            len(list(a.walk(a.earliest(), mfilter=filters.makefilter('yes')))),
            6)
        self.assertEqual(
            len(list(a.walk(
                a.earliest(),
                mfilter=filters.makefilter('backend == "sink"'),
                search=True))),
            3)
        self.assertEqual(len(list(a.walk(
            float('Inf'),
            forward=False,
            backfill_to=0.0,
            ))), 6)
        self.assertEqual(len(list(a.walk(float('-Inf')))), 6)

        for i in range(2):  # because caching?
            forward = list(a.walk(a.earliest(), True))
            for (x, y) in list(zip([None] + forward, forward + [None]))[1:-1]:
                self.assertLess(x, y)

            backward = list(a.walk(a.latest(), False))
            for (x, y) in list(
                    zip([None] + backward, backward + [None]))[1:-1]:
                self.assertGreater(x, y)

        self.assertTrue(a.walk(forward[0], True))

        self.assertEqual(a.eldest(), synth.messages[0].time)

        with self.assertRaises(util.SnipeException):
            await a.send('nope', None)

        count = 0

        async def mock_shutdown():
            nonlocal count
            count += 1

        for backend in a:
            backend.shutdown = mock_shutdown

        await a.shutdown()

        self.assertGreater(count, 0)

        count = 0

        def mock_backfill(filter, target):
            nonlocal count
            count += 1

        for backend in a:
            backend.backfill = mock_backfill

        a.backfill(None, None)

        self.assertGreater(count, 0)

        self.assertEqual(a.destinations(), set())
        self.assertEqual(a.senders(), set())

        self.assertEqual(a.count(), 6)
        synth2 = SyntheticBackend(context)
        a.backends.append(synth2)
        await synth2.start()
        self.assertEqual(a.count(), 7)

        self.assertEqual(a.statusline(), '')

        synth2.context.ui = mocks.FE()
        synth2.state_set(messages.BackendState.BACKFILLING)

        self.assertEqual(a.statusline(), '[synthetic BACKFILLING]')
Exemplo n.º 6
0
    def test(self):
        context = mocks.Context()
        synth = SyntheticBackend(context)
        startup = messages.StartupBackend(context)
        sink = messages.SinkBackend(context)
        a = messages.AggregatorBackend(context, [startup, synth, sink])
        a.start()
        self.assertEqual(startup.count(), 1)
        self.assertEqual(synth.count(), 1)
        self.assertEqual(a.count(), 3)
        self.assertEqual(len(list(a.walk(None, False))), 3)
        list(a.send('sink', 'a message'))
        self.assertEqual(a.count(), 4)
        self.assertEqual(len(list(a.walk(None, False))), 4)
        self.assertEqual(len(list(a.walk(None))), 4)
        self.assertEqual(len(list(a.walk(None, search=True))), 3)
        self.assertEqual(
            len(list(a.walk(None, filter=filters.makefilter('yes')))),
            4)
        self.assertEqual(
            len(list(a.walk(
                None,
                filter=filters.makefilter('backend == "sink"'),
                search=True))),
            1)
        self.assertEqual(len(list(a.walk(
            float('Inf'),
            forward=False,
            backfill_to=0.0,
            ))), 4)
        self.assertEqual(len(list(a.walk(float('-Inf')))), 4)

        for i in range(2):  # because caching?
            forward = list(a.walk(None, True))
            for (x, y) in list(zip([None] + forward, forward + [None]))[1:-1]:
                self.assertLess(x, y)

            backward = list(a.walk(None, False))
            for (x, y) in list(
                    zip([None] + backward, backward + [None]))[1:-1]:
                self.assertGreater(x, y)

        self.assertTrue(a.walk(forward[0], True))

        self.assertEqual(a.eldest(), synth.messages[0].time)

        self.assertRaises(
            util.SnipeException, lambda: a.send('nope', None).send(None))

        count = 0

        @asyncio.coroutine
        def mock_shutdown():
            nonlocal count
            count += 1

        for backend in a:
            backend.shutdown = mock_shutdown

        loop = asyncio.get_event_loop()
        loop.run_until_complete(a.shutdown())

        self.assertGreater(count, 0)

        count = 0

        def mock_backfill(filter, target):
            nonlocal count
            count += 1

        for backend in a:
            backend.backfill = mock_backfill

        a.backfill(None, None)

        self.assertGreater(count, 0)

        self.assertEqual(a.destinations(), set())
        self.assertEqual(a.senders(), set())

        self.assertEqual(a.count(), 4)
        synth2 = SyntheticBackend(context)
        a.add(synth2)
        self.assertEqual(a.count(), 5)
Exemplo n.º 7
0
    def test_Parser(self):
        snipe.filters.parser = Parser(debug=True)
        self.assertEqual(makefilter('yes'), Yes())
        self.assertEqual(makefilter('yes and no'), And(Yes(), No()))
        self.assertEqual(makefilter('foo = "bar"'), Compare('=', 'foo', 'bar'))
        self.assertEqual(makefilter('foo < "bar"'), Compare('<', 'foo', 'bar'))
        self.assertEqual(makefilter('"bar" = foo'), Compare('=', 'foo', 'bar'))
        self.assertEqual(makefilter('foo = bar'),
                         Compare('=', 'foo', Identifier('bar')))
        self.assertEqual(makefilter('foo = /bar/'),
                         RECompare('=', 'foo', 'bar'))
        self.assertEqual(makefilter('/bar/ = foo'),
                         RECompare('=', 'foo', 'bar'))
        self.assertEqual(makefilter('1 = 1'), Yes())
        self.assertEqual(makefilter('1 = 2'), No())
        self.assertEqual(makefilter('"foo" = /foo/'), Yes())
        self.assertEqual(makefilter('"foo" = /bar/'), No())
        self.assertEqual(makefilter('"Foo" = /foo/i'), Yes())
        self.assertEqual(makefilter('"Foo" != /foo/i'), No())
        self.assertEqual(makefilter('(yes or no) and yes'),
                         And(Or(Yes(), No()), Yes()))
        self.assertEqual(makefilter('yes xor no'), Xor(Yes(), No()))

        self.assertTrue(makefilter('foo = "bar"')(mocks.Message(foo='bar')))
        self.assertFalse(makefilter('foo = "bar"')(mocks.Message(foo='baz')))

        self.assertTrue(makefilter('foo = /b.*/')(mocks.Message(foo='bar')))
        self.assertTrue(makefilter('foo = /b.*/')(mocks.Message(foo='baz')))
        self.assertFalse(makefilter('foo = /b.*/')(mocks.Message(foo='quux')))

        self.assertTrue(
            makefilter('foo = bar')(mocks.Message(foo='quux', bar='quux')))

        self.assertFalse(
            makefilter('foo = bar')(mocks.Message(foo='quux', bar='quuux')))

        self.assertTrue(
            makefilter('foo = "bar"')(mocks.Message(
                foo='Bar',
                Foo='bar',
            )))
        self.assertFalse(
            makefilter('not foo = "bar"')(mocks.Message(
                foo='Bar',
                Foo='bar',
            )))
        self.assertFalse(
            makefilter('foo == "bar"')(mocks.Message(
                foo='Bar',
                Foo='bar',
            )))
        self.assertTrue(
            makefilter('foo = /bar/')(mocks.Message(
                foo='Bar',
                Foo='bar',
            )))
        self.assertFalse(
            makefilter('foo == /bar/')(mocks.Message(
                foo='Bar',
                Foo='bar',
            )))
        self.assertFalse(
            makefilter('foo == /bar[/')(mocks.Message(
                foo='Bar',
                Foo='bar',
            )))

        self.assertEqual(str(makefilter('foo == "bar"')), 'foo == "bar"')
        self.assertEqual(str(makefilter('"bar" == foo')), 'foo == "bar"')

        self.assertEqual(str(makefilter('yes and yes and yes')),
                         'yes and yes and yes')

        self.assertEqual(str(makefilter('no and yes and yes')),
                         'no and yes and yes')

        self.assertEqual(str(makefilter('yes and no and yes')),
                         'yes and no and yes')

        self.assertEqual(str(makefilter('yes and yes and no')),
                         'yes and yes and no')

        self.assertTrue(makefilter('foo')(mocks.Message(foo=True)))
        self.assertFalse(makefilter('foo')(mocks.Message(foo=0)))
        self.assertFalse(makefilter('foo')(mocks.Message(foo=0)))

        self.assertIs(makefilter(''), None)

        self.assertFalse(makefilter('filter foo')(mocks.Message()))
Exemplo n.º 8
0
    def test(self):
        context = mocks.Context()
        synth = SyntheticBackend(context)
        startup = messages.StartupBackend(context)
        sink = messages.SinkBackend(context)
        a = messages.AggregatorBackend(context, [startup, synth, sink])
        a.start()
        self.assertEqual(startup.count(), 1)
        self.assertEqual(synth.count(), 1)
        self.assertEqual(a.count(), 3)
        self.assertEqual(len(list(a.walk(None, False))), 3)
        list(a.send('sink', 'a message'))
        self.assertEqual(a.count(), 4)
        self.assertEqual(len(list(a.walk(None, False))), 4)
        self.assertEqual(len(list(a.walk(None))), 4)
        self.assertEqual(len(list(a.walk(None, search=True))), 3)
        self.assertEqual(
            len(list(a.walk(None, filter=filters.makefilter('yes')))), 4)
        self.assertEqual(
            len(
                list(
                    a.walk(None,
                           filter=filters.makefilter('backend == "sink"'),
                           search=True))), 1)
        self.assertEqual(
            len(list(a.walk(
                float('Inf'),
                forward=False,
                backfill_to=0.0,
            ))), 4)
        self.assertEqual(len(list(a.walk(float('-Inf')))), 4)

        for i in range(2):  # because caching?
            forward = list(a.walk(None, True))
            for (x, y) in list(zip([None] + forward, forward + [None]))[1:-1]:
                self.assertLess(x, y)

            backward = list(a.walk(None, False))
            for (x, y) in list(zip([None] + backward,
                                   backward + [None]))[1:-1]:
                self.assertGreater(x, y)

        self.assertTrue(a.walk(forward[0], True))

        self.assertEqual(a.eldest(), synth.messages[0].time)

        self.assertRaises(util.SnipeException,
                          lambda: a.send('nope', None).send(None))

        count = 0

        @asyncio.coroutine
        def mock_shutdown():
            nonlocal count
            count += 1

        for backend in a:
            backend.shutdown = mock_shutdown

        loop = asyncio.get_event_loop()
        loop.run_until_complete(a.shutdown())

        self.assertGreater(count, 0)

        count = 0

        def mock_backfill(filter, target):
            nonlocal count
            count += 1

        for backend in a:
            backend.backfill = mock_backfill

        a.backfill(None, None)

        self.assertGreater(count, 0)

        self.assertEqual(a.destinations(), set())
        self.assertEqual(a.senders(), set())

        self.assertEqual(a.count(), 4)
        synth2 = SyntheticBackend(context)
        a.add(synth2)
        self.assertEqual(a.count(), 5)