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 test_markov_chain(self): tr1 = t.Transition(0, 1, pck.ProbCheck(1.0)) tr2 = t.Transition(1, 2, pck.ProbCheck(1.0)) mc = t.MarkovChain([tr1, tr2]) state1 = DummyState(0) state2 = DummyState(1) mc.apply_on(state1, 1) mc.apply_on(state2, 1) self.assertEqual(state1.state, 1) self.assertEqual(state2.state, 2) mc.apply_on(state1, 1) mc.apply_on(state2, 1) self.assertEqual(state1.state, 2) self.assertEqual(state2.state, 2)
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)
def test_transition(self): tr = t.Transition(0, 1, pck.ProbCheck(1.0)) state = DummyState(0) self.assertTrue(tr(state, 1)) self.assertEqual(state.state, 1) state = DummyState(2) self.assertFalse(tr(state, 1)) self.assertEqual(state.state, 2)
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_model(self): """Defines the Markov chain transitions. The transition will happen with the probability defined by ProbCheck, which will be evaluated each server_sleep_in_seconds time (defined by config). The values are quite low because the time when these are evaluated is the same as when the traffic is evaluated. Over time, the probability accumulation is much higher, though. """ tr_stop_normal = tr.Transition(from_state=STATE_STOP, to_state=STATE_NORMAL, prob_check=pck.ProbCheck(0.8)) tr_normal_stop = tr.Transition(from_state=STATE_NORMAL, to_state=STATE_STOP, prob_check=pck.ProbCheck(0.001)) tr_normal_attack = tr.Transition(from_state=STATE_NORMAL, to_state=STATE_ATTACK, prob_check=pck.ProbCheck(0.0001)) tr_attack_normal = tr.Transition(from_state=STATE_ATTACK, to_state=STATE_NORMAL, prob_check=pck.ProbCheck(0.2)) return tr.MarkovChain([ tr_stop_normal, tr_normal_stop, tr_normal_attack, tr_attack_normal ])
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 }