# Path: splunklib/modularinput/event.py
# class Event(object):
#     def __init__(self, data=None, stanza=None, time=None, host=None, index=None, source=None,
#                  sourcetype=None, done=True, unbroken=True):
#     def write_to(self, stream):
# 
# Path: splunklib/modularinput/event_writer.py
# class EventWriter(object):
#     """``EventWriter`` writes events and error messages to Splunk from a modular input.
#     Its two important methods are ``writeEvent``, which takes an ``Event`` object,
#     and ``log``, which takes a severity and an error message.
#     """
# 
#     # Severities that Splunk understands for log messages from modular inputs.
#     # Do not change these
#     DEBUG = "DEBUG"
#     INFO = "INFO"
#     WARN = "WARN"
#     ERROR = "ERROR"
#     FATAL = "FATAL"
# 
#     def __init__(self, output = sys.stdout, error = sys.stderr):
#         """
#         :param output: Where to write the output; defaults to sys.stdout.
#         :param error: Where to write any errors; defaults to sys.stderr.
#         """
#         self._out = output
#         self._err = error
# 
#         # has the opening <stream> tag been written yet?
#         self.header_written = False
# 
#     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
# 
#         event.write_to(self._out)
# 
#     def log(self, severity, message):
#         """Logs messages about the state of this modular input to Splunk.
#         These messages will show up in Splunk's internal logs.
# 
#         :param severity: ``string``, severity of message, see severities defined as class constants.
#         :param message: ``string``, message to log.
#         """
# 
#         self._err.write("%s %s\n" % (severity, message))
#         self._err.flush()
# 
#     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(ensure_str(ET.tostring(document)))
#         self._out.flush()
# 
#     def close(self):
#         """Write the closing </stream> tag to make this XML well formed."""
#         self._out.write("</stream>")
#         self._out.flush()


import sys
import pytest
from tests.modularinput.modularinput_testlib import unittest, xml_compare, data_open
from splunklib.modularinput.event import Event, ET
from splunklib.modularinput.event_writer import EventWriter
        expected = ET.parse(data).getroot()

        assert xml_compare(expected, found)

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."

def test_logging_errors_with_event_writer(capsys):
    """Check that the log method on EventWriter produces the
    expected error message."""

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

    ew.log(EventWriter.ERROR, "Something happened!")

    captured = capsys.readouterr()
    assert captured.err == "ERROR Something happened!\n"

def test_write_xml_is_sane(capsys):
    """Check that EventWriter.write_xml_document writes sensible
    XML to the output stream."""


# Path: splunklib/modularinput/event.py
# class Event(object):
#     def __init__(self, data=None, stanza=None, time=None, host=None, index=None, source=None,
#                  sourcetype=None, done=True, unbroken=True):
#     def write_to(self, stream):
# 
# Path: splunklib/modularinput/event_writer.py
# class EventWriter(object):
#     """``EventWriter`` writes events and error messages to Splunk from a modular input.
#     Its two important methods are ``writeEvent``, which takes an ``Event`` object,
#     and ``log``, which takes a severity and an error message.
#     """
# 
#     # Severities that Splunk understands for log messages from modular inputs.
#     # Do not change these
#     DEBUG = "DEBUG"
#     INFO = "INFO"
#     WARN = "WARN"
#     ERROR = "ERROR"
#     FATAL = "FATAL"
# 
#     def __init__(self, output = sys.stdout, error = sys.stderr):
#         """
#         :param output: Where to write the output; defaults to sys.stdout.
#         :param error: Where to write any errors; defaults to sys.stderr.
#         """
#         self._out = output
#         self._err = error
# 
#         # has the opening <stream> tag been written yet?
#         self.header_written = False
# 
#     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
# 
#         event.write_to(self._out)
# 
#     def log(self, severity, message):
#         """Logs messages about the state of this modular input to Splunk.
#         These messages will show up in Splunk's internal logs.
# 
#         :param severity: ``string``, severity of message, see severities defined as class constants.
#         :param message: ``string``, message to log.
#         """
# 
#         self._err.write("%s %s\n" % (severity, message))
#         self._err.flush()
# 
#     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(ensure_str(ET.tostring(document)))
#         self._out.flush()
# 
#     def close(self):
#         """Write the closing </stream> tag to make this XML well formed."""
#         self._out.write("</stream>")
#         self._out.flush()


# Path: tests/modularinput/test_event.py
import sys
import pytest
from tests.modularinput.modularinput_testlib import unittest, xml_compare, data_open
from splunklib.modularinput.event import Event, ET
from splunklib.modularinput.event_writer import EventWriter

        expected = ET.parse(data).getroot()

        assert xml_compare(expected, found)

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."

def test_logging_errors_with_event_writer(capsys):
    """Check that the log method on EventWriter produces the
    expected error message."""

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

    ew.log(EventWriter.ERROR, "Something happened!")

    captured = capsys.readouterr()
    assert captured.err == "ERROR Something happened!\n"

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)