Exemplo n.º 1
0
    def test_to_dict(self):
        # Create primitive event
        p_timestamp = EpochNSClock.generate_timestamp()
        p_event = PrimitiveEvent(timestamp=p_timestamp, data=P_DATA)
        p_hist = "p_hist"

        # Create composite event
        c_timestamp = EpochNSClock.generate_timestamp()
        c_history = BoboHistory()
        c_event = CompositeEvent(timestamp=c_timestamp,
                                 name=C_NAME,
                                 history=c_history,
                                 data=C_DATA)
        c_hist = "c_hist"

        # Create list of events for history
        events = {p_hist: [p_event], c_hist: [c_event]}

        # Create history and add events
        history = BoboHistory(events=events)

        self.assertDictEqual(history.to_dict(), {
            p_hist: [p_event.to_dict()],
            c_hist: [c_event.to_dict()]
        })
Exemplo n.º 2
0
    def test_action(self):
        # action event data
        a_timestamp = EpochNSClock.generate_timestamp()
        a_name = NAME_A
        a_success = True
        a_for_event = CompositeEvent(
            timestamp=EpochNSClock.generate_timestamp(),
            name=NAME_B,
            history=BoboHistory(),
            data={}
        )
        a_exception = EXCEPTION_A
        a_description = DESCRIPTION_A
        a_data = DATA_A
        a_event_id = EVENT_ID_A

        # create dict representation of action event
        a_dict = {
            ActionEvent.TIMESTAMP: a_timestamp,
            ActionEvent.NAME: a_name,
            ActionEvent.SUCCESS: a_success,
            ActionEvent.FOR_EVENT: a_for_event.to_dict(),
            ActionEvent.EXCEPTION: a_exception,
            ActionEvent.DESCRIPTION: a_description,
            ActionEvent.DATA: a_data,
            ActionEvent.EVENT_ID: a_event_id
        }

        # build actual action event from dict
        a_event_1 = BoboRuleBuilder.action(a_dict)
        a_event_2 = BoboRuleBuilder.event(a_dict)

        for a_event in [a_event_1, a_event_2]:
            self.assertEqual(a_event.timestamp, a_timestamp)
            self.assertEqual(a_event.name, a_name)
            self.assertEqual(a_event.success, a_success)
            self.assertEqual(a_event.exception, a_exception)
            self.assertEqual(a_event.description, a_description)
            self.assertDictEqual(a_event.data, a_data)
            self.assertEqual(a_event.event_id, a_event_id)

            c_event = a_event.for_event
            self.assertIsInstance(c_event, CompositeEvent)
            self.assertEqual(c_event.timestamp, a_for_event.timestamp)
            self.assertEqual(c_event.name, a_for_event.name)
            self.assertIsNone(c_event.history.first)
            self.assertDictEqual(c_event.data, a_for_event.data)
            self.assertEqual(c_event.event_id, a_for_event.event_id)
Exemplo n.º 3
0
    def test_subscribe_producer_composite_rejected(self):
        setup = BoboSetup()
        sub = StubSubscriberSetup()

        setup.add_complex_event(event_def=BoboComplexEvent(
            name=NAME_NFA_A, pattern=stub_pattern_1, action=NoAction()))

        # this rejects all events passed to Producer
        setup.config_producer(NoAction(bool_return=False))

        setup.subscribe_producer(NAME_NFA_A, sub)
        setup.configure()

        producer = setup.get_producer()
        producer.setup()

        c_event = CompositeEvent(timestamp=EpochNSClock.generate_timestamp(),
                                 name=NAME_NFA_A,
                                 history=BoboHistory(),
                                 data={})

        producer.on_decider_complex_event(c_event)
        producer.loop()

        self.assertEqual(c_event, sub.rejected_producer_event[0])
Exemplo n.º 4
0
    def test_action_from_producer_to_decider_not_recursive(self):
        setup = BoboSetup(recursive=False)

        setup.add_complex_event(event_def=BoboComplexEvent(
            name=NAME_NFA_A, pattern=stub_pattern_4))
        setup.config_receiver(StrDictValidator())
        setup.configure()

        decider = setup.get_decider()
        decider.setup()

        producer = setup.get_producer()
        producer.setup()

        a_event = NoAction().execute(
            CompositeEvent(timestamp=EpochNSClock.generate_timestamp(),
                           name=NAME_NFA_A,
                           history=BoboHistory(),
                           data={}))

        producer.on_action_attempt(a_event)
        producer.loop()

        handler = decider.get_all_handlers()[0]
        self.assertEqual(0, len(handler.get_all_recent()))
Exemplo n.º 5
0
    def test_add_recent(self):
        nfa, buffer, handler, handlersub = handler_setup(
            nfa_name=NFA_NAME_A, pattern=pattern_relaxed, max_recent=2)

        p_event = PrimitiveEvent(timestamp=EpochNSClock.generate_timestamp(),
                                 data={})
        sleep(0.1)
        c_event = CompositeEvent(timestamp=EpochNSClock.generate_timestamp(),
                                 name=EVENT_NAME_A,
                                 history=BoboHistory(),
                                 data={})
        sleep(0.1)
        a_event = ActionEvent(timestamp=EpochNSClock.generate_timestamp(),
                              name=EVENT_NAME_B,
                              success=True,
                              for_event=c_event)

        handler.add_recent(p_event)
        handler.add_recent(c_event)

        self.assertTrue(p_event in handler._recent)
        self.assertTrue(c_event in handler._recent)

        handler.add_recent(a_event)

        self.assertFalse(p_event in handler._recent)
        self.assertTrue(c_event in handler._recent)
        self.assertTrue(a_event in handler._recent)
Exemplo n.º 6
0
    def test_subscribe_forward_action_failure(self):
        setup = BoboSetup()
        sub = StubSubscriberSetup()

        setup.add_complex_event(event_def=BoboComplexEvent(
            name=NAME_NFA_A, pattern=stub_pattern_1, action=NoAction()))

        # this rejects all events passed to Forwarder
        setup.config_forwarder(NoAction(bool_return=False))

        setup.subscribe_forwarder(sub)
        setup.configure()

        forwarder = setup.get_forwarder()
        forwarder.setup()

        a_event = NoAction().execute(
            CompositeEvent(timestamp=EpochNSClock.generate_timestamp(),
                           name=NAME_NFA_A,
                           history=BoboHistory(),
                           data={}))

        forwarder.on_producer_action(a_event)
        forwarder.loop()

        self.assertEqual(a_event, sub.forwarder_failure_event[0])
Exemplo n.º 7
0
 def on_handler_final(self, nfa_name: str, run_id: str,
                      event: CompositeEvent):
     if not self._is_cancelled:
         self._queue_final.put_nowait({
             bdc.NFA_NAME: nfa_name,
             bdc.RUN_ID: run_id,
             bdc.EVENT: event.to_dict()
         })
Exemplo n.º 8
0
    def test_to_dict(self):
        c_timestamp = EpochNSClock.generate_timestamp()
        c_name = "c_name"
        c_history = BoboHistory()
        c_data = {"c_key": "c_value"}

        c_event = CompositeEvent(timestamp=c_timestamp,
                                 name=c_name,
                                 history=c_history,
                                 data=c_data)

        self.assertDictEqual(
            c_event.to_dict(), {
                CompositeEvent.TIMESTAMP: c_timestamp,
                CompositeEvent.NAME: c_name,
                CompositeEvent.HISTORY: c_history.to_dict(),
                CompositeEvent.DATA: c_data,
                CompositeEvent.EVENT_ID: c_event.event_id
            })
Exemplo n.º 9
0
    def test_true(self):
        timestamp = EpochNSClock.generate_timestamp()
        name = "c_name"
        history = BoboHistory()
        data = {"c_key": "c_value"}

        c_event = CompositeEvent(timestamp=timestamp,
                                 name=name,
                                 history=history,
                                 data=data)

        self.assertTrue(NoAction().execute(c_event).success)
Exemplo n.º 10
0
    def test_subscribe_decider_composite(self):
        setup = BoboSetup()
        sub = StubSubscriberSetup()

        setup.add_complex_event(event_def=BoboComplexEvent(
            name=NAME_NFA_A, pattern=stub_pattern_1, action=NoAction()))

        setup.subscribe_decider(NAME_NFA_A, sub)
        setup.configure()

        decider = setup.get_decider()
        decider.setup()

        c_event = CompositeEvent(timestamp=EpochNSClock.generate_timestamp(),
                                 name=NAME_NFA_A,
                                 history=BoboHistory(),
                                 data={})

        decider.on_handler_final(NAME_NFA_A, RUN_ID_A, c_event)

        self.assertEqual(c_event, sub.decider_complex_event[0])
Exemplo n.º 11
0
    def test_subscribe_forward_composite_success(self):
        setup = BoboSetup()
        sub = StubSubscriberSetup()

        setup.add_complex_event(event_def=BoboComplexEvent(
            name=NAME_NFA_A, pattern=stub_pattern_1, action=NoAction()))

        setup.subscribe_forwarder(sub)
        setup.configure()

        forwarder = setup.get_forwarder()
        forwarder.setup()

        c_event = CompositeEvent(timestamp=EpochNSClock.generate_timestamp(),
                                 name=NAME_NFA_A,
                                 history=BoboHistory(),
                                 data={})

        forwarder.on_accepted_producer_event(c_event)
        forwarder.loop()

        self.assertEqual(c_event, sub.forwarder_success_event[0])
Exemplo n.º 12
0
    def composite(d: dict) -> CompositeEvent:
        """Creates a CompositeEvent instance from a serialized representation
        of one.

        :param d: A dict representation of a CompositeEvent instance.
        :type d: dict

        :raises RuntimeError: Timestamp not found in dict.
        :raises RuntimeError: Name not found in dict.
        :raises RuntimeError: History not found in dict.
        :raises RuntimeError: Event ID not found in dict.

        :return: A CompositeEvent instance.
        """

        timestamp = d.get(CompositeEvent.TIMESTAMP)
        if timestamp is None:
            raise RuntimeError("Timestamp not found in dict.")

        name = d.get(CompositeEvent.NAME)
        if name is None:
            raise RuntimeError("Name not found in dict.")

        history_dict = d.get(CompositeEvent.HISTORY)
        if history_dict is None:
            raise RuntimeError("History not found in dict.")

        history = BoboRuleBuilder.history(history_dict)
        data = d.get(BoboEvent.DATA)

        event_id = d.get(CompositeEvent.EVENT_ID)
        if event_id is None:
            raise RuntimeError("Event ID not found in dict.")

        return CompositeEvent(timestamp=timestamp,
                              name=name,
                              history=history,
                              data=data,
                              event_id=event_id)
Exemplo n.º 13
0
    def test_composite_from_producer_to_forwarder(self):
        setup = BoboSetup(recursive=True)

        setup.add_complex_event(event_def=BoboComplexEvent(
            name=NAME_NFA_A, pattern=stub_pattern_4))
        setup.config_receiver(StrDictValidator())
        setup.configure()

        producer = setup.get_producer()
        producer.setup()

        forwarder = setup.get_forwarder()
        forwarder.setup()

        c_event = CompositeEvent(timestamp=EpochNSClock.generate_timestamp(),
                                 name=NAME_NFA_A,
                                 history=BoboHistory(),
                                 data={})

        producer.on_decider_complex_event(c_event)
        producer.loop()

        self.assertEqual(c_event, forwarder._event_queue.get_nowait())
Exemplo n.º 14
0
    def test_subscribe_producer_action(self):
        setup = BoboSetup()
        sub = StubSubscriberSetup()

        setup.add_complex_event(event_def=BoboComplexEvent(
            name=NAME_NFA_A, pattern=stub_pattern_1, action=NoAction()))

        setup.subscribe_producer(NAME_NFA_A, sub)
        setup.configure()

        producer = setup.get_producer()
        producer.setup()

        a_event = NoAction().execute(
            CompositeEvent(timestamp=EpochNSClock.generate_timestamp(),
                           name=NAME_NFA_A,
                           history=BoboHistory(),
                           data={}))

        producer.on_action_attempt(a_event)
        producer.loop()

        self.assertEqual(a_event, sub.producer_action[0])
Exemplo n.º 15
0
    def on_run_final(self,
                     run_id: str,
                     history: BoboHistory,
                     notify: bool = True) -> None:
        """
        :raises RuntimeError: Run ID not found.
        """

        with self._lock:
            run = self.runs.get(run_id)

            if run is not None:
                run.set_final(history=history, notify=False)

                event = CompositeEvent(
                    timestamp=EpochNSClock.generate_timestamp(),
                    name=self.nfa.name,
                    history=history)

                self.remove_run(run_id)

                if notify:
                    self._notify_final(run_id, event)
Exemplo n.º 16
0
def generate_composite_event(name: str) -> CompositeEvent:
    return CompositeEvent(
        timestamp=EpochNSClock.generate_timestamp(),
        name=name,
        history=BoboHistory(),
        data={})
Exemplo n.º 17
0
    def test_action_none(self):
        # action event data
        a_timestamp = EpochNSClock.generate_timestamp()
        a_name = NAME_A
        a_success = True
        a_for_event = CompositeEvent(
            timestamp=EpochNSClock.generate_timestamp(),
            name=NAME_B,
            history=BoboHistory(),
            data={}
        )
        a_exception = EXCEPTION_A
        a_description = DESCRIPTION_A
        a_data = DATA_A
        a_event_id = EVENT_ID_A

        with self.assertRaises(RuntimeError):
            BoboRuleBuilder.action({
                ActionEvent.TIMESTAMP: None,
                ActionEvent.NAME: a_name,
                ActionEvent.SUCCESS: a_success,
                ActionEvent.FOR_EVENT: a_for_event.to_dict(),
                ActionEvent.EXCEPTION: a_exception,
                ActionEvent.DESCRIPTION: a_description,
                ActionEvent.DATA: a_data,
                ActionEvent.EVENT_ID: a_event_id
            })

        with self.assertRaises(RuntimeError):
            BoboRuleBuilder.action({
                ActionEvent.TIMESTAMP: a_timestamp,
                ActionEvent.NAME: None,
                ActionEvent.SUCCESS: a_success,
                ActionEvent.FOR_EVENT: a_for_event.to_dict(),
                ActionEvent.EXCEPTION: a_exception,
                ActionEvent.DESCRIPTION: a_description,
                ActionEvent.DATA: a_data,
                ActionEvent.EVENT_ID: a_event_id
            })

        with self.assertRaises(RuntimeError):
            BoboRuleBuilder.action({
                ActionEvent.TIMESTAMP: a_timestamp,
                ActionEvent.NAME: a_name,
                ActionEvent.SUCCESS: None,
                ActionEvent.FOR_EVENT: a_for_event.to_dict(),
                ActionEvent.EXCEPTION: a_exception,
                ActionEvent.DESCRIPTION: a_description,
                ActionEvent.DATA: a_data,
                ActionEvent.EVENT_ID: a_event_id
            })

        with self.assertRaises(RuntimeError):
            BoboRuleBuilder.action({
                ActionEvent.TIMESTAMP: a_timestamp,
                ActionEvent.NAME: a_name,
                ActionEvent.SUCCESS: a_success,
                ActionEvent.FOR_EVENT: None,
                ActionEvent.EXCEPTION: a_exception,
                ActionEvent.DESCRIPTION: a_description,
                ActionEvent.DATA: a_data,
                ActionEvent.EVENT_ID: a_event_id
            })

        with self.assertRaises(RuntimeError):
            BoboRuleBuilder.action({
                ActionEvent.TIMESTAMP: a_timestamp,
                ActionEvent.NAME: a_name,
                ActionEvent.SUCCESS: a_success,
                ActionEvent.FOR_EVENT: a_for_event.to_dict(),
                ActionEvent.EXCEPTION: a_exception,
                ActionEvent.DESCRIPTION: a_description,
                ActionEvent.DATA: a_data,
                ActionEvent.EVENT_ID: None
            })