Exemplo n.º 1
0
 def test_next_state_should_use_available_transitions(self):
     tr1 = tr.Transition(
         from_state=0,
         to_state=1,
         deps_check=dck.TrueCheck(),
         prob_check=pck.NoProbCheck()
     )
     tr2 = tr.Transition(
         from_state=1,
         to_state=2,
         deps_check=dck.EqCheck(1),
         prob_check=pck.NoProbCheck()
     )
     mc = tr.MarkovChain([tr1, tr2])
     n1 = base.StateNode(0, mc, None)
     n2 = base.StateNode(1, mc, None)
     n3 = base.StateNode(0, mc, None)
     n1.dependencies.append(n2)
     n2.dependencies.append(n3)
     # First round
     n1.next_state(1, set())
     self.assertEqual(n1.state, 1)
     self.assertEqual(n2.state, 1)
     self.assertEqual(n3.state, 0)
     # Second round
     n1.next_state(1, set())
     self.assertEqual(n1.state, 2)
     self.assertEqual(n2.state, 1)
     self.assertEqual(n3.state, 0)
Exemplo n.º 2
0
 def test_combiner_or_dep_check(self):
     state = DummyState()
     state1 = DummyState(1)
     state.dependencies.append(state1)
     state1.dependencies.append(DummyState(2))
     dc = dck.OrCheck(c1=dck.DepCheck(dck.EqCheck(1)),
                      c2=dck.DepCheck(dck.DepCheck(dck.EqCheck(2))))
     self.assertTrue(dc(state))
     self.assertFalse(dc(state1))
     state1.dependencies[0].state = 1
     self.assertTrue(dc(state))
     self.assertTrue(dc(state1))
     state1.state = 2
     self.assertFalse(dc(state))
     state = DummyState()
     self.assertFalse(dc(state))
    def _create_event_triggers(self):
        triggers = self._config["triggers"]
        sup_tr = triggers["support"]
        user_support = ev.Trigger(
            prob_check=pck.ProbCheck(sup_tr["get_called"]),
            node_check=dck.AnyDepCheck(dck.NeqCheck("run")),
            event_builder=ev.EventBuilder(
                "User complained for poor web service"))
        webs1 = ev.Trigger(
            node_check=dck.OrCheck(
                dck.EqCheck("stop"), dck.DepCheck(dck.EqCheck("off")),
                dck.DepCheck(dck.DepCheck(dck.EqCheck("off")))),
            prob_check=pck.NoProbCheck(),
            event_builder=ev.EventBuilder("Application is down"))
        webs2 = ev.Trigger(
            node_check=dck.EqCheck("slow"),
            prob_check=pck.NoProbCheck(),
            event_builder=ev.EventBuilder("Application is slow"))
        host = ev.Trigger(
            node_check=dck.OrCheck(dck.EqCheck("off"),
                                   dck.DepCheck(dck.EqCheck("off"))),
            prob_check=pck.NoProbCheck(),
            event_builder=ev.EventBuilder("Host is unreachable or down"))
        switch = ev.Trigger(
            node_check=dck.EqCheck("off"),
            prob_check=pck.NoProbCheck(),
            event_builder=ev.EventBuilder("Switch is unreachable or down"))

        return {
            "switch": switch,
            "host": host,
            "web_service": [webs1, webs2],
            "support": user_support
        }
Exemplo n.º 4
0
 def test_second_order_dep_check(self):
     state = DummyState()
     state1 = DummyState()
     state.dependencies.append(state1)
     state1.dependencies.append(DummyState(1))
     dc = dck.DepCheck(dck.DepCheck(dck.EqCheck(1)))
     self.assertTrue(dc(state))
     state = DummyState()
     self.assertFalse(dc(state))
     self.assertFalse(dc(state1))
Exemplo n.º 5
0
    def _create_trigger(self, prob, state, event_msg):
        """Aux function to create an event trigger

        :param prob: float between 0 and 1 -- probability of the event
        being triggered
        :param state: str -- State where this event can be triggered
        :param event_msg: str -- message that will be sent for this event
        """
        return events.Trigger(prob_check=pck.ProbCheck(prob),
                              node_check=dck.EqCheck(state),
                              event_builder=events.EventBuilder(event_msg))
Exemplo n.º 6
0
 def test_collect_events_should_be_populated_by_trigger(self):
     some_trigger = ev.Trigger(
         node_check=dck.EqCheck(0),
         prob_check=pck.NoProbCheck(),
         event_builder=ev.EventBuilder("test")
     )
     node = base.StateNode(0, None, some_trigger)
     events = []
     node.collect_events(1, datetime.datetime.now(),
                         markov_mocks.MockRequestBuilder(events))
     self.assertTrue(len(events) == 1)
     self.assertEqual(events[0]["event"].msg, "test", "a")
 def test_trigger_should_create_event_when_necessary(self):
     some_trigger = ev.Trigger(node_check=dck.EqCheck(0),
                               prob_check=pck.NoProbCheck(),
                               event_builder=ev.EventBuilder(""))
     events = []
     some_trigger.apply_on(DummyState(), 1, datetime.datetime.now(),
                           markov_mocks.MockRequestBuilder(events))
     self.assertEqual(len(events), 1)
     events = []
     some_trigger.apply_on(DummyState(1), 1, datetime.datetime.now(),
                           markov_mocks.MockRequestBuilder(events))
     self.assertEqual(len(events), 0)
Exemplo n.º 8
0
 def test_next_state_update_only_deps_and_deps_in_first(self):
     tr1 = tr.Transition(
         from_state=0,
         to_state=1,
         prob_check=pck.NoProbCheck()
     )
     tr2 = tr.Transition(
         from_state=1,
         to_state=2,
         deps_check=dck.EqCheck(1),
         prob_check=pck.NoProbCheck()
     )
     mc = tr.MarkovChain([tr1, tr2])
     n1 = base.StateNode(0, mc, None)
     n2 = base.StateNode(1, mc, None)
     n3 = base.StateNode(0, mc, None)
     n1.dependencies.append(n2)
     n2.dependencies.append(n3)
     n2.next_state(1, set())
     self.assertEqual(n1.state, 0)
     self.assertEqual(n2.state, 2)
     self.assertEqual(n3.state, 1)
    def _create_markov_chain_models(self):
        transitions = self._config["transitions"]

        # Switch Transitions
        sw_tr = transitions["switch"]
        tr1 = tr.Transition(from_state="on",
                            to_state="off",
                            prob_check=pck.ProbCheck(sw_tr["on=>off"]))
        tr2 = tr.Transition(from_state="off",
                            to_state="on",
                            prob_check=pck.ProbCheck(sw_tr["off=>on"]))
        switch_mc = tr.MarkovChain([tr1, tr2])

        # Host Transitions
        hs_tr = transitions["host"]
        tr1 = tr.Transition(from_state="on",
                            to_state="off",
                            prob_check=pck.ProbCheck(hs_tr["on=>off"]))
        tr2 = tr.Transition(from_state="off",
                            to_state="on",
                            prob_check=pck.ProbCheck(hs_tr["off=>on"]))
        host_mc = tr.MarkovChain([tr1, tr2])

        # Web service Transitions
        ws_tr = transitions["web_service"]
        tr1 = tr.Transition(from_state="run",
                            to_state="stop",
                            prob_check=pck.NoProbCheck(),
                            deps_check=dck.EqCheck("off"))
        tr2 = tr.Transition(from_state="slow",
                            to_state="stop",
                            prob_check=pck.NoProbCheck(),
                            deps_check=dck.EqCheck("off"))
        tr3 = tr.Transition(from_state="slow",
                            to_state="run",
                            prob_check=pck.NoProbCheck(),
                            deps_check=dck.AndCheck(
                                dck.EqCheck("on"),
                                dck.DepCheck(dck.EqCheck("off"))))
        tr4 = tr.Transition(from_state="run",
                            to_state="slow",
                            prob_check=pck.ProbCheck(ws_tr["run=>slow"]),
                            deps_check=dck.AndCheck(
                                dck.EqCheck("on"),
                                dck.DepCheck(dck.EqCheck("on"))))
        tr5 = tr.Transition(from_state="slow",
                            to_state="run",
                            prob_check=pck.ProbCheck(ws_tr["slow=>run"]),
                            deps_check=dck.AndCheck(
                                dck.EqCheck("on"),
                                dck.DepCheck(dck.EqCheck("on"))))
        tr6 = tr.Transition(from_state="stop",
                            to_state="run",
                            prob_check=pck.ProbCheck(ws_tr["stop=>run"]),
                            deps_check=dck.EqCheck("on"))
        webs_mc = tr.MarkovChain([tr1, tr2, tr3, tr4, tr5, tr6])

        # User support markov chain
        sup_mc = tr.MarkovChain([])

        return {
            "switch": switch_mc,
            "host": host_mc,
            "web_service": webs_mc,
            "support": sup_mc
        }