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))
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))
def test_write_xml_is_sane(self): """Check that EventWriter.write_xml_document writes sensible XML to the output stream.""" out = StringIO() err = StringIO() ew = EventWriter(out, err) expected_xml = ET.parse(data_open("data/event_maximal.xml")).getroot() ew.write_xml_document(expected_xml) found_xml = ET.fromstring(out.getvalue()) self.assertTrue(xml_compare(expected_xml, found_xml))
def test_write_xml_is_sane(capsys): """Check that EventWriter.write_xml_document writes sensible XML to the output stream.""" ew = EventWriter(sys.stdout, sys.stderr) with data_open("data/event_maximal.xml") as data: expected_xml = ET.parse(data).getroot() ew.write_xml_document(expected_xml) captured = capsys.readouterr() found_xml = ET.fromstring(captured.out) assert xml_compare(expected_xml, found_xml)
def test_xml_of_event_with_minimal_configuration(self): """Generate XML from an event object with a small number of fields, and see if it matches what we expect.""" stream = StringIO() event = Event(data="This is a test of the emergency broadcast system.", stanza="fubar", time="%.3f" % 1372187084.000) event.write_to(stream) constructed = ET.fromstring(stream.getvalue()) expected = ET.parse(data_open("data/event_minimal.xml")).getroot() self.assertTrue(xml_compare(expected, constructed))
def test_xml_of_event_with_minimal_configuration(self): """Generate XML from an event object with a small number of fields, and see if it matches what we expect.""" stream = StringIO() event = Event( data="This is a test of the emergency broadcast system.", stanza="fubar", time="%.3f" % 1372187084.000 ) event.write_to(stream) constructed = ET.fromstring(stream.getvalue()) expected = ET.parse(data_open("data/event_minimal.xml")).getroot() self.assertTrue(xml_compare(expected, constructed))
def test_xml_of_event_with_minimal_configuration(capsys): """Generate XML from an event object with a small number of fields, and see if it matches what we expect.""" event = Event(data="This is a test of the emergency broadcast system.", stanza="fubar", time="%.3f" % 1372187084.000) event.write_to(sys.stdout) captured = capsys.readouterr() constructed = ET.fromstring(captured.out) with data_open("data/event_minimal.xml") as data: expected = ET.parse(data).getroot() assert xml_compare(expected, constructed)
def write_xml_document(self, document): """Writes a string representation of an ``ElementTree`` object to the output stream. :param document: An ``ElementTree`` object. """ self._out.write(ET.tostring(document)) self._out.flush()
def test_xml_of_event_with_more_configuration(self): """Generate XML from an even object with all fields set, see if it matches what we expect""" stream = StringIO() event = Event(data="This is a test of the emergency broadcast system.", stanza="fubar", time="%.3f" % 1372274622.493, host="localhost", index="main", source="hilda", sourcetype="misc", done=True, unbroken=True) event.write_to(stream) constructed = ET.fromstring(stream.getvalue()) expected = ET.parse(data_open("data/event_maximal.xml")).getroot() self.assertTrue(xml_compare(expected, constructed))
def test_xml_of_event_with_more_configuration(self): """Generate XML from an even object with all fields set, see if it matches what we expect""" stream = StringIO() event = Event( data="This is a test of the emergency broadcast system.", stanza="fubar", time="%.3f" % 1372274622.493, host="localhost", index="main", source="hilda", sourcetype="misc", done=True, unbroken=True ) event.write_to(stream) constructed = ET.fromstring(stream.getvalue()) expected = ET.parse(data_open("data/event_maximal.xml")).getroot() self.assertTrue(xml_compare(expected, constructed))
def test_xml_of_event_with_more_configuration(capsys): """Generate XML from an even object with all fields set, see if it matches what we expect""" event = Event(data="This is a test of the emergency broadcast system.", stanza="fubar", time="%.3f" % 1372274622.493, host="localhost", index="main", source="hilda", sourcetype="misc", done=True, unbroken=True) event.write_to(sys.stdout) captured = capsys.readouterr() constructed = ET.fromstring(captured.out) with data_open("data/event_maximal.xml") as data: expected = ET.parse(data).getroot() assert xml_compare(expected, constructed)
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)
def write_event(self, event): """Writes an ``Event`` object to Splunk. :param event: An ``Event`` object. """ if not self.header_written: self._out.write("<stream>") self.header_written = True xml_element = event.get_element() write_str = ET.tostring(xml_element, encoding='UTF-8') write_str = write_str[write_str.find('\n') + 1:] self._out.write(write_str) self._out.flush()