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))
Exemple #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))
    def test_generate_xml_from_scheme(self):
        """Checks that the XML generated by a Scheme object with all its fields set and
        some arguments added matches what we expect."""

        scheme = Scheme("abcd")
        scheme.description = u"쎼 and 쎶 and <&> für"
        scheme.streaming_mode = Scheme.streaming_mode_simple
        scheme.use_external_validation = "false"
        scheme.use_single_instance = "true"

        arg1 = Argument(name="arg1")
        scheme.add_argument(arg1)

        arg2 = Argument(
            name="arg2",
            description=u"쎼 and 쎶 and <&> für",
            validation="is_pos_int('some_name')",
            data_type=Argument.data_type_number,
            required_on_edit=True,
            required_on_create=True
        )
        scheme.add_argument(arg2)

        constructed = scheme.to_xml()
        expected = ET.parse(data_open("data/scheme_without_defaults.xml")).getroot()

        self.assertTrue(xml_compare(expected, constructed))
    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_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)
Exemple #6
0
    def test_generate_xml_from_scheme_with_arg_title(self):
        """Checks that the XML generated by a Scheme object with all its fields set and
        some arguments added matches what we expect. Also sets the title on an argument."""

        scheme = Scheme("abcd")
        scheme.description = u"쎼 and 쎶 and <&> für"
        scheme.streaming_mode = Scheme.streaming_mode_simple
        scheme.use_external_validation = "false"
        scheme.use_single_instance = "true"

        arg1 = Argument(name="arg1")
        scheme.add_argument(arg1)

        arg2 = Argument(name="arg2",
                        description=u"쎼 and 쎶 and <&> für",
                        validation="is_pos_int('some_name')",
                        data_type=Argument.data_type_number,
                        required_on_edit=True,
                        required_on_create=True,
                        title="Argument for ``test_scheme``")
        scheme.add_argument(arg2)

        constructed = scheme.to_xml()

        expected = ET.parse(
            data_open("data/scheme_without_defaults_and_argument_title.xml")
        ).getroot()
        self.assertEqual("Argument for ``test_scheme``", arg2.title)

        self.assertTrue(xml_compare(expected, constructed))
    def test_generate_xml_from_scheme_with_default_values(self):
        """Checks the Scheme generated by creating a Scheme object and setting no fields on it.
        This test checks for sane defaults in the class."""

        scheme = Scheme("abcd")

        constructed = scheme.to_xml()
        expected = ET.parse(data_open("data/scheme_with_defaults.xml")).getroot()

        self.assertTrue(xml_compare(expected, constructed))
Exemple #8
0
    def test_generate_xml_from_scheme_with_default_values(self):
        """Checks the Scheme generated by creating a Scheme object and setting no fields on it.
        This test checks for sane defaults in the class."""

        scheme = Scheme("abcd")

        constructed = scheme.to_xml()
        expected = ET.parse(
            data_open("data/scheme_with_defaults.xml")).getroot()

        self.assertTrue(xml_compare(expected, constructed))
    def test_generate_xml_from_argument_with_default_values(self):
        """Checks that the XML produced from an Argument class that is initialized but has no additional manipulations
        made to it is what we expect. This is mostly a check of the default values."""

        argument = Argument("some_name")

        root = ET.Element("")
        constructed = argument.add_to_document(root)

        expected = ET.parse(data_open("data/argument_with_defaults.xml")).getroot()

        self.assertTrue(xml_compare(expected, constructed))
Exemple #10
0
    def test_generate_xml_from_argument_with_default_values(self):
        """Checks that the XML produced from an Argument class that is initialized but has no additional manipulations
        made to it is what we expect. This is mostly a check of the default values."""

        argument = Argument("some_name")

        root = ET.Element("")
        constructed = argument.add_to_document(root)

        expected = ET.parse(
            data_open("data/argument_with_defaults.xml")).getroot()

        self.assertTrue(xml_compare(expected, constructed))
Exemple #11
0
    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(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))
Exemple #13
0
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)
Exemple #14
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)
    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))
Exemple #16
0
    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))
Exemple #17
0
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)
Exemple #18
0
    def test_generate_xml_from_argument(self):
        """Checks that the XML generated by an Argument class with all its possible values set is what we expect."""

        argument = Argument(name="some_name",
                            description=u"쎼 and 쎶 and <&> für",
                            validation="is_pos_int('some_name')",
                            data_type=Argument.data_type_boolean,
                            required_on_edit="true",
                            required_on_create="true")

        root = ET.Element("")
        constructed = argument.add_to_document(root)

        expected = ET.parse(
            data_open("data/argument_without_defaults.xml")).getroot()

        self.assertTrue(xml_compare(expected, constructed))
    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_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_generate_xml_from_argument(self):
        """Checks that the XML generated by an Argument class with all its possible values set is what we expect."""

        argument = Argument(
            name="some_name",
            description=u"쎼 and 쎶 and <&> für",
            validation="is_pos_int('some_name')",
            data_type=Argument.data_type_boolean,
            required_on_edit="true",
            required_on_create="true"
        )

        root = ET.Element("")
        constructed = argument.add_to_document(root)

        expected = ET.parse(data_open("data/argument_without_defaults.xml")).getroot()

        self.assertTrue(xml_compare(expected, constructed))
Exemple #22
0
    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_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_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))
Exemple #25
0
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_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))