Example #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)
    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
        }
    def _create_system(self):
        mc = tr.MarkovChain([])
        vm_triggers = [
            ev.Trigger(
                event_builder=MonascaFakeMetricBuilder("vm.mem.used_mb"),
                node_check=dck.TrueCheck(),
                prob_check=pck.NoProbCheck()),
            ev.Trigger(event_builder=MonascaFakeMetricBuilder("cpu.idle_perc"),
                       node_check=dck.TrueCheck(),
                       prob_check=pck.NoProbCheck()),
            ev.Trigger(event_builder=MonascaFakeMetricBuilder(
                "cpu.total_logical_cores"),
                       node_check=dck.TrueCheck(),
                       prob_check=pck.NoProbCheck())
        ]
        host_trigger = ev.Trigger(
            event_builder=MonascaFakeMetricBuilder("mem.total_mb"),
            node_check=dck.TrueCheck(),
            prob_check=pck.NoProbCheck())

        return [
            # vm.mem.used_mb
            base.StateNode(3, mc, vm_triggers[0], _id="vm1"),
            base.StateNode(1, mc, vm_triggers[0], _id="vm2"),
            # cpu.idle_perc
            base.StateNode(0.75, mc, vm_triggers[1], _id="vm1"),
            base.StateNode(0.75, mc, vm_triggers[1], _id="vm2"),
            # cpu.total_logical_cores
            base.StateNode(3, mc, vm_triggers[2], _id="vm1"),
            base.StateNode(2, mc, vm_triggers[2], _id="vm2"),
            # mem.total_mb
            base.StateNode(5, mc, host_trigger, _id="host1"),
            base.StateNode(6, mc, host_trigger, _id="host2"),
        ]
Example #4
0
 def test_transition_with_true_check(self):
     tr = t.Transition(0, 1, pck.NoProbCheck(), dck.TrueCheck())
     state = DummyState(0)
     self.assertFalse(tr(state, 1))
     state1 = DummyState(123456)
     state.dependencies.append(state1)
     self.assertTrue(tr(state, 1))
     self.assertEqual(state.state, 1)
Example #5
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)
Example #7
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
        }