示例#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_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"),
        ]
    def _create_system(self):
        triggers = self._create_event_triggers()
        markov_chains = self._create_markov_chain_models()
        graph = self._config["graph"]
        nodes = {}
        support_node = base.StateNode(None,
                                      markov_chains["support"],
                                      triggers["support"],
                                      _id="support_1")

        for k in graph.keys():
            node_name, node_type = k.split(":")
            if node_type == "host":
                nodes[node_name] = base.StateNode("on",
                                                  markov_chains["host"],
                                                  triggers["host"],
                                                  _id=node_name)
            elif node_type == "switch":
                nodes[node_name] = base.StateNode("on",
                                                  markov_chains["switch"],
                                                  triggers["switch"],
                                                  _id=node_name)
            elif node_type == "web_service":
                webs = base.StateNode("run",
                                      markov_chains["web_service"],
                                      triggers["web_service"],
                                      _id=node_name)
                support_node.dependencies.append(webs)
                nodes[node_name] = webs

        for k, v in graph.iteritems():
            node_name, _ = k.split(":")

            for depend_on in v:
                if depend_on not in nodes:
                    logger.warn("Configuration error: '{}'"
                                " is not a proper dependency"
                                " of '{}'".format(depend_on, node_name))
                else:
                    n = nodes[node_name]
                    o = nodes[depend_on]
                    n.dependencies.append(o)

        return [support_node]
示例#4
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")
示例#5
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)
示例#6
0
    def _create_system(self):
        """Markov Chain of IPTables being triggered in each state.

        Implements the Markov Chain model corresponding to the IPTables that
        are triggered depending on the state.
        In order to create the model, we have to create the triggers
        (events with some probability for each state),
        and the Markov chain model (probabilities of state transitions).
        """
        logger.debug("Creating IPTables System")
        triggers = self._create_event_triggers()
        logger.debug("Generated " + str(len(triggers)) +
                     " IPTables event triggers")
        markov_chain = self._create_markov_chain_model()
        logger.debug("Created Markov chain model for IPTables")
        support_node = base.StateNode(STATE_STOP, markov_chain, triggers)
        return [support_node]