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_logging_errors_with_event_writer(self): """Check that the log method on EventWriter produces the expected error message.""" out = StringIO() err = StringIO() ew = EventWriter(out, err) ew.log(EventWriter.ERROR, "Something happened!") self.assertEqual("ERROR Something happened!\n", err.getvalue())
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_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))
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_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))
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_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))
def test_error_on_script_with_null_scheme(self): """A script that returns a null scheme should generate no output on stdout and an error on stderr saying that it the scheme was null.""" # Override abstract methods class NewScript(Script): def get_scheme(self): return None def stream_events(self, inputs, ew): # not used return script = NewScript() out = BytesIO() err = BytesIO() ew = EventWriter(out, err) in_stream = StringIO() args = [TEST_SCRIPT_PATH, "--scheme"] return_value = script.run_script(args, ew, in_stream) self.assertEqual(b"", out.getvalue()) self.assertEqual( b"FATAL Modular input script returned a null scheme.\n", err.getvalue()) self.assertNotEqual(0, return_value)
def test_failed_validation(self): """Check that failed validation writes sensible XML to stdout.""" # Override abstract methods class NewScript(Script): def get_scheme(self): return None def validate_input(self, definition): raise ValueError("Big fat validation error!") def stream_events(self, inputs, ew): # unused return script = NewScript() out = BytesIO() err = BytesIO() ew = EventWriter(out, err) args = [TEST_SCRIPT_PATH, "--validate-arguments"] return_value = script.run_script(args, ew, data_open("data/validation.xml")) expected = ET.parse(data_open("data/validation_error.xml")).getroot() found = ET.fromstring(out.getvalue().decode('utf-8')) self.assertEqual(b"", err.getvalue()) self.assertTrue(xml_compare(expected, found)) self.assertNotEqual(0, return_value)
def test_successful_validation(self): """Check that successful validation yield no text and a 0 exit value.""" # Override abstract methods class NewScript(Script): def get_scheme(self): return None def validate_input(self, definition): # always succeed... return def stream_events(self, inputs, ew): # unused return script = NewScript() out = StringIO() err = StringIO() ew = EventWriter(out, err) args = [TEST_SCRIPT_PATH, "--validate-arguments"] return_value = script.run_script(args, ew, data_open("data/validation.xml")) self.assertEqual("", err.getvalue()) self.assertEqual("", out.getvalue()) self.assertEqual(0, return_value)
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 run(self, args): """Runs this modular input :param args: List of command line arguments passed to this script. :returns: An integer to be used as the exit value of this program. """ # call the run_script function, which handles the specifics of running # a modular input return self.run_script(args, EventWriter(), sys.stdin)
def test_scheme_properly_generated_by_script(self): """Check that a scheme generated by a script is what we expect.""" # Override abstract methods class NewScript(Script): def get_scheme(self): scheme = Scheme("abcd") scheme.description = u"\uC3BC and \uC3B6 and <&> f\u00FCr" scheme.streaming_mode = scheme.streaming_mode_simple scheme.use_external_validation = False scheme.use_single_instance = True arg1 = Argument("arg1") scheme.add_argument(arg1) arg2 = Argument("arg2") arg2.description = u"\uC3BC and \uC3B6 and <&> f\u00FCr" arg2.data_type = Argument.data_type_number arg2.required_on_create = True arg2.required_on_edit = True arg2.validation = "is_pos_int('some_name')" scheme.add_argument(arg2) return scheme def stream_events(self, inputs, ew): # not used return script = NewScript() out = BytesIO() err = BytesIO() ew = EventWriter(out, err) args = [TEST_SCRIPT_PATH, "--scheme"] return_value = script.run_script(args, ew, err) self.assertEqual(b"", err.getvalue()) self.assertEqual(0, return_value) found = ET.fromstring(out.getvalue()) expected = ET.parse( data_open("data/scheme_without_defaults.xml")).getroot() self.assertTrue(xml_compare(expected, found))
def test_write_events(self): """Check that passing an input definition and writing a couple events goes smoothly.""" # Override abstract methods class NewScript(Script): def get_scheme(self): return None def stream_events(self, inputs, ew): event = 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(event) ew.write_event(event) script = NewScript() input_configuration = data_open("data/conf_with_2_inputs.xml") out = BytesIO() err = BytesIO() ew = EventWriter(out, err) return_value = script.run_script([TEST_SCRIPT_PATH], ew, input_configuration) self.assertEqual(0, return_value) self.assertEqual(b"", err.getvalue()) expected = ET.parse( data_open("data/stream_with_two_events.xml")).getroot() found = ET.fromstring(out.getvalue()) self.assertTrue(xml_compare(expected, found))
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 test_service_property(self): """ Check that Script.service returns a valid Service instance as soon as the stream_events method is called, but not before. """ # Override abstract methods class NewScript(Script): def __init__(self, test): super(NewScript, self).__init__() self.test = test def get_scheme(self): return None def stream_events(self, inputs, ew): service = self.service self.test.assertIsInstance(service, Service) self.test.assertEqual(str(service.authority), inputs.metadata['server_uri']) script = NewScript(self) input_configuration = data_open("data/conf_with_2_inputs.xml") out = BytesIO() err = BytesIO() ew = EventWriter(out, err) self.assertEqual(script.service, None) return_value = script.run_script([TEST_SCRIPT_PATH], ew, input_configuration) self.assertEqual(0, return_value) self.assertEqual(b"", err.getvalue()) return
def __init__(self, timeout=30, **kwargs): scheme_args = { 'title': "JWT Webhook", 'description': "Retrieve data from jwt webhook using SSL", 'use_single_instance': True } args = [ IntegerField('port', 'Port', 'The port to run the web-server on', none_allowed=False, empty_allowed=False), Field( 'secret', 'Secret', 'The secret key to decode the JWT encoded payload, leave it empty if the payload is not JWT encoded.', none_allowed=True, empty_allowed=True), Field( 'path', 'Path', 'A wildcard that the path of requests must match (paths generally begin with a "/" and can include a wildcard)', none_allowed=True, empty_allowed=True), FilePathField( 'key_file', 'SSL Certificate Key File', 'The path to the SSL certificate key file (if the certificate requires a key); typically uses .KEY file extension', none_allowed=True, empty_allowed=True, validate_file_existence=True), FilePathField( 'cert_file', 'SSL Certificate File', 'The path to the SSL certificate file (if you want to use encryption); typically uses .DER, .PEM, .CRT, .CER file extensions', none_allowed=False, empty_allowed=False, validate_file_existence=True), Field( 'password', 'Password', 'The password to decrypt the private key, leave it empty if the private key is not encrypted.', none_allowed=True, empty_allowed=True), ] ModularInput.__init__(self, scheme_args, args, logger_name="webhook_modular_input", sleep_interval=60) EventWriter.__init__(self, output=sys.stdout, error=sys.stderr) if timeout > 0: self.timeout = timeout else: self.timeout = 30 self.http_daemons = {}
def event_writer(output_stream, error_stream): return EventWriter(output_stream, error_stream)