def test_event_without_enough_fields_fails(self):
     """Check that events without data throw an error"""
     with self.assertRaises(ValueError):
         event = Event()
         stream = StringIO()
         event.write_to(stream)
     self.assertTrue(True)
Esempio n. 2
0
 def test_event_without_enough_fields_fails(self):
     """Check that events without data throw an error"""
     with self.assertRaises(ValueError):
         event = Event()
         stream = StringIO()
         event.write_to(stream)
     self.assertTrue(True)
Esempio n. 3
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))
Esempio n. 4
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))
Esempio n. 5
0
 def stream_events(self, inputs, ew):
     for input_name, input_item in inputs.inputs.iteritems():
         stations = input_item["stations"].split(" ")
         for station in stations:
             url = ENDPOINT.format(id=station)
             xmlstring = requests.get(url).content
             root = ET.fromstring(xmlstring)
             items = root.find('channel').findall('item')
             for item in items:
                 data = get_dict(item)
                 data['id'] = station
                 event = Event()
                 event.stanza = input_name
                 event.sourceType = "ndbc:observations"
                 event.data = json.dumps(data)
                 ew.write_event(event)
Esempio n. 6
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))
Esempio n. 7
0
 def stream_events(self, inputs, ew):
     for input_name, input_item in inputs.inputs.iteritems():
         plat = input_item["latitude"]
         plon = input_item["longitude"]
         pradius = input_item["radius"]
         url = ENDPOINT.format(lat=plat, lon=plon, radius=pradius)
         xmlstring = requests.get(url).content
         root = ET.fromstring(xmlstring)
         items = root.find('channel').findall('item')
         for item in items:
             data = get_dict(item)
             event = Event()
             event.stanza = input_name
             event.sourceType = "ndbc:observations"
             event.data = json.dumps(data)
             ew.write_event(event)
Esempio n. 8
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)
Esempio n. 9
0
 def stream_events(self, inputs, ew):
     for input_name, input_item in inputs.inputs.iteritems():
         plat = input_item["latitude"]
         plon = input_item["longitude"]
         pradius = input_item["radius"]
         url = ENDPOINT.format(lat=plat, lon=plon, radius=pradius)
         xmlstring = requests.get(url).content
         root = ET.fromstring(xmlstring)
         items = root.find('channel').findall('item')
         for item in items:
             data = get_dict(item)
             event = Event()
             event.stanza = input_name
             event.sourceType = "ndbc:observations"
             event.data = json.dumps(data)
             ew.write_event(event)
Esempio n. 10
0
 def stream_events(self, inputs, ew):
     for input_name, input_item in inputs.inputs.iteritems():
         xmlstring = requests.get(ENDPOINT).content
         root = ET.fromstring(xmlstring)
         time = root.get('created')
         stations = root.findall('station')
         for station in stations:
             data = dict()
             data['time'] = time
             for name in station.attrib:
                 value = station.get(name)
                 if name == 'elev' or name == 'lat' or name == 'lon':
                     value = float(value)
                 data[name] = value
             event = Event()
             event.stanza = input_name
             event.data = json.dumps(data)
             ew.write_event(event)
Esempio n. 11
0
 def stream_events(self, inputs, ew):
     for input_name, input_item in inputs.inputs.iteritems():
         xmlstring = requests.get(ENDPOINT).content
         root = ET.fromstring(xmlstring)
         time = root.get('created')
         stations = root.findall('station')
         for station in stations:
             data = dict()
             data['time'] = time
             for name in station.attrib:
                 value = station.get(name)
                 if name == 'elev' or name == 'lat' or name == 'lon':
                     value = float(value)
                 data[name] = value
             event = Event()
             event.stanza = input_name
             event.sourceType = "ndbc:activestations"
             event.data = json.dumps(data)
             ew.write_event(event)
Esempio n. 12
0
 def output_results(payload, clientip):
     event = Event(data=json.dumps(payload),
                   time="%.3f" % time.time(),
                   index=index,
                   host=clientip,
                   source=host,
                   sourcetype=sourcetype,
                   done=True,
                   unbroken=True)
     self.write_event(event)
Esempio n. 13
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))
Esempio n. 14
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."
Esempio n. 15
0
 def json_event(self, item, timestamp):
     logger.log(logging.DEBUG, 'start index.')
     data = json.dumps(item)
     index = self.server.metadata['index']
     sourcetype = self.server.metadata['sourcetype']
     source = self.server.server_data.url
     event = Event(data=data,
                   time=timestamp,
                   index=index,
                   source=source,
                   sourcetype=sourcetype)
     self.event_writer.write_event(event)
     logger.log(logging.DEBUG, 'end index.')
Esempio n. 16
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))
Esempio n. 17
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)
Esempio n. 18
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))
            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)
Esempio n. 20
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))
Esempio n. 21
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)
Esempio n. 22
0
 def create_event(self, input_name, sourcetype, obj):
     event = Event()
     event.stanza = input_name
     event.sourceType = sourcetype
     event.data = self.to_json(obj)
     return event
Esempio n. 23
0
def test_event_without_enough_fields_fails(capsys):
    """Check that events without data throw an error"""
    with pytest.raises(ValueError), capsys.disabled():
        event = Event()
        event.write_to(sys.stdout)
Esempio n. 24
0
 def create_event(self, input_name, sourcetype, obj):
     event = Event()
     event.stanza = input_name
     event.sourceType = sourcetype
     event.data = self.to_json(obj)
     return event