Exemplo n.º 1
0
    def test_writing_events_on_event_writer(self):
        """Write a pair of events with an EventWriter, and ensure that they
        are being encoded immediately and correctly onto the output stream"""
        out = StringIO()
        err = StringIO()

        ew = EventWriter(out, err)

        e = Event(
            data="This is a test of the emergency broadcast system.",
            stanza="fubar",
            time="%.3f" % 1372275124.466,
            host="localhost",
            index="main",
            source="hilda",
            sourcetype="misc",
            done=True,
            unbroken=True
        )
        ew.write_event(e)

        found = ET.fromstring("%s</stream>" % out.getvalue())
        expected = ET.parse(data_open("data/stream_with_one_event.xml")).getroot()

        self.assertTrue(xml_compare(expected, found))
        self.assertEqual(err.getvalue(), "")

        ew.write_event(e)
        ew.close()

        found = ET.fromstring(out.getvalue())
        expected = ET.parse(data_open("data/stream_with_two_events.xml")).getroot()

        self.assertTrue(xml_compare(expected, found))
Exemplo n.º 2
0
    def test_writing_events_on_event_writer(self):
        """Write a pair of events with an EventWriter, and ensure that they
        are being encoded immediately and correctly onto the output stream"""
        out = StringIO()
        err = StringIO()

        ew = EventWriter(out, err)

        e = Event(data="This is a test of the emergency broadcast system.",
                  stanza="fubar",
                  time="%.3f" % 1372275124.466,
                  host="localhost",
                  index="main",
                  source="hilda",
                  sourcetype="misc",
                  done=True,
                  unbroken=True)
        ew.write_event(e)

        found = ET.fromstring("%s</stream>" % out.getvalue())
        expected = ET.parse(
            data_open("data/stream_with_one_event.xml")).getroot()

        self.assertTrue(xml_compare(expected, found))
        self.assertEqual(err.getvalue(), "")

        ew.write_event(e)
        ew.close()

        found = ET.fromstring(out.getvalue())
        expected = ET.parse(
            data_open("data/stream_with_two_events.xml")).getroot()

        self.assertTrue(xml_compare(expected, found))
Exemplo n.º 3
0
def test_error_in_event_writer():
    """An event which cannot write itself onto an output stream
    (such as because it doesn't have a data field set)
    should write an error. Check that it does so."""

    ew = EventWriter(sys.stdout, sys.stderr)
    e = Event()
    with pytest.raises(ValueError) as excinfo:
        ew.write_event(e)
    assert str(
        excinfo.value
    ) == "Events must have at least the data field set to be written to XML."
Exemplo n.º 4
0
    def test_error_in_event_writer(self):
        """An event which cannot write itself onto an output stream
        (such as because it doesn't have a data field set)
        should write an error. Check that it does so."""
        out = StringIO()
        err = StringIO()

        ew = EventWriter(out, err)
        e = Event()

        with self.assertRaises(ValueError):
            ew.write_event(e)
            self.assertTrue(err.getvalue().startswith(EventWriter.WARN))
Exemplo n.º 5
0
    def test_error_in_event_writer(self):
        """An event which cannot write itself onto an output stream
        (such as because it doesn't have a data field set)
        should write an error. Check that it does so."""
        out = StringIO()
        err = StringIO()

        ew = EventWriter(out, err)
        e = Event()

        with self.assertRaises(ValueError):
            ew.write_event(e)
            self.assertTrue(err.getvalue().startswith(EventWriter.WARN))
Exemplo n.º 6
0
    def test_error_in_event_writer(self):
        """An event which cannot write itself onto an output stream
        (such as because it doesn't have a data field set)
        should write an error. Check that it does so."""
        out = StringIO()
        err = StringIO()

        ew = EventWriter(out, err)
        e = Event()
        try:
            ew.write_event(e)
            self.assertTrue(False)
        except ValueError as e:
            self.assertEqual("Events must have at least the data field set to be written to XML.", str(e))
Exemplo n.º 7
0
    def test_error_in_event_writer(self):
        """An event which cannot write itself onto an output stream
        (such as because it doesn't have a data field set)
        should write an error. Check that it does so."""
        out = BytesIO()
        err = BytesIO()

        ew = EventWriter(out, err)
        e = Event()
        try:
            ew.write_event(e)
            self.assertTrue(False)
        except ValueError as e:
            self.assertEqual("Events must have at least the data field set to be written to XML.", str(e))
Exemplo n.º 8
0
def test_writing_events_on_event_writer(capsys):
    """Write a pair of events with an EventWriter, and ensure that they
    are being encoded immediately and correctly onto the output stream"""

    ew = EventWriter(sys.stdout, sys.stderr)

    e = Event(data="This is a test of the emergency broadcast system.",
              stanza="fubar",
              time="%.3f" % 1372275124.466,
              host="localhost",
              index="main",
              source="hilda",
              sourcetype="misc",
              done=True,
              unbroken=True)
    ew.write_event(e)

    captured = capsys.readouterr()

    first_out_part = captured.out

    with data_open("data/stream_with_one_event.xml") as data:
        found = ET.fromstring("%s</stream>" % first_out_part)
        expected = ET.parse(data).getroot()

        assert xml_compare(expected, found)
        assert captured.err == ""

    ew.write_event(e)
    ew.close()

    captured = capsys.readouterr()
    with data_open("data/stream_with_two_events.xml") as data:
        found = ET.fromstring(first_out_part + captured.out)
        expected = ET.parse(data).getroot()

        assert xml_compare(expected, found)